Skip to content

Commit 38f137b

Browse files
committed
replaced Except with ExceptSchemMismatch
1 parent f4197f9 commit 38f137b

19 files changed

+61
-56
lines changed

src/Microsoft.ML.Data/DataView/TypedCursor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private TypedCursorable(IHostEnvironment env, IDataView data, bool ignoreMissing
102102
{
103103
if (ignoreMissingColumns)
104104
continue;
105-
throw _host.Except("Column '{0}' not found in the data view", col.ColumnName);
105+
throw _host.ExceptSchemaMismatch(nameof(_data.Schema), "", col.ColumnName);
106106
}
107107
var realColType = _data.Schema[colIndex].Type;
108108
if (!IsCompatibleType(realColType, col.MemberInfo))

src/Microsoft.ML.Data/Evaluators/AnomalyDetectionEvaluator.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
9595
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
9696
var t = score.Type;
9797
if (t != NumberType.Float)
98-
throw Host.Except("Score column '{0}' has type '{1}' but must be R4", score, t).MarkSensitive(MessageSensitivity.Schema);
98+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "float", t.ToString());
9999
Host.Check(schema.Label.HasValue, "Could not find the label column");
100100
t = schema.Label.Value.Type;
101101
if (t != NumberType.Float && t.GetKeyCount() != 2)
102-
throw Host.Except("Label column '{0}' has type '{1}' but must be R4 or a 2-value key", schema.Label.Value.Name, t).MarkSensitive(MessageSensitivity.Schema);
102+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "float or a 2-value key", t.ToString());
103103
}
104104

105105
private protected override Aggregator GetAggregatorCore(RoleMappedSchema schema, string stratName)
@@ -630,13 +630,13 @@ private protected override void PrintFoldResultsCore(IChannel ch, Dictionary<str
630630
{
631631
int index;
632632
if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.Instance, out index))
633-
throw Host.Except("Data view does not contain the 'Instance' column");
633+
throw Host.ExceptSchemaMismatch(nameof(top.Schema), "instance", AnomalyDetectionEvaluator.TopKResultsColumns.Instance);
634634
var instanceGetter = cursor.GetGetter<ReadOnlyMemory<char>>(index);
635635
if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.AnomalyScore, out index))
636-
throw Host.Except("Data view does not contain the 'Anomaly Score' column");
636+
throw Host.ExceptSchemaMismatch(nameof(top.Schema), "anomaly score", AnomalyDetectionEvaluator.TopKResultsColumns.AnomalyScore);
637637
var scoreGetter = cursor.GetGetter<Single>(index);
638638
if (!top.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.TopKResultsColumns.Label, out index))
639-
throw Host.Except("Data view does not contain the 'Label' column");
639+
throw Host.ExceptSchemaMismatch(nameof(top.Schema), "label", AnomalyDetectionEvaluator.TopKResultsColumns.Label);
640640
var labelGetter = cursor.GetGetter<Single>(index);
641641

642642
bool hasRows = false;
@@ -665,12 +665,12 @@ private protected override void PrintFoldResultsCore(IChannel ch, Dictionary<str
665665

666666
IDataView overall;
667667
if (!metrics.TryGetValue(MetricKinds.OverallMetrics, out overall))
668-
throw Host.Except("No overall metrics found");
668+
throw Host.ExceptSchemaMismatch(nameof(overall.Schema), "overall metrics", MetricKinds.OverallMetrics);
669669

670670
// Find the number of anomalies, and the thresholds.
671671
int numAnomIndex;
672672
if (!overall.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.OverallMetrics.NumAnomalies, out numAnomIndex))
673-
throw Host.Except("Could not find the 'NumAnomalies' column");
673+
throw Host.ExceptSchemaMismatch(nameof(overall.Schema), "number of anomalies", AnomalyDetectionEvaluator.OverallMetrics.NumAnomalies);
674674

675675
int stratCol;
676676
var hasStrat = overall.Schema.TryGetColumnIndex(MetricKinds.ColumnNames.StratCol, out stratCol);

src/Microsoft.ML.Data/Evaluators/BinaryClassifierEvaluator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1097,18 +1097,18 @@ private void CheckInputColumnTypes(Schema schema)
10971097

10981098
var t = schema[(int) LabelIndex].Type;
10991099
if (t != NumberType.R4 && t != NumberType.R8 && t != BoolType.Instance && t.GetKeyCount() != 2)
1100-
throw Host.Except("Label column '{0}' has type '{1}' but must be R4, R8, BL or a 2-value key", LabelCol, t);
1100+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", LabelCol, "float, double, bool or a 2-value key", t.ToString());
11011101

11021102
t = schema[ScoreIndex].Type;
11031103
if (t != NumberType.Float)
1104-
throw Host.Except("Score column '{0}' has type '{1}' but must be R4", ScoreCol, t);
1104+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", ScoreCol, "float", t.ToString());
11051105

11061106
if (_probIndex >= 0)
11071107
{
11081108
Host.Assert(!string.IsNullOrEmpty(_probCol));
11091109
t = schema[_probIndex].Type;
11101110
if (t != NumberType.Float)
1111-
throw Host.Except("Probability column '{0}' has type '{1}' but must be R4", _probCol, t);
1111+
throw Host.ExceptSchemaMismatch(nameof(schema), "probability", _probCol, "float", t.ToString());
11121112
}
11131113
else if (!_useRaw)
11141114
throw Host.Except("Cannot compute the predicted label from the probability column because it does not exist");

src/Microsoft.ML.Data/Evaluators/ClusteringEvaluator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ private void CheckInputColumnTypes(Schema schema)
753753

754754
var type = schema[(int) ScoreIndex].Type;
755755
if (!(type is VectorType vectorType) || !vectorType.IsKnownSize || vectorType.ItemType != NumberType.Float)
756-
throw Host.Except("Score column '{0}' has type {1}, but must be a float vector of known-size", ScoreCol, type);
756+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", ScoreCol, "known-size vector of float", type.ToString());
757757
}
758758
}
759759

src/Microsoft.ML.Data/Evaluators/EvaluatorBase.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,9 @@ protected PerInstanceEvaluatorBase(IHostEnvironment env, Schema schema, string s
484484
LabelCol = labelCol;
485485

486486
if (!string.IsNullOrEmpty(LabelCol) && !schema.TryGetColumnIndex(LabelCol, out LabelIndex))
487-
throw Host.Except("Did not find the label column '{0}'", LabelCol);
487+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", LabelCol);
488488
if (!schema.TryGetColumnIndex(ScoreCol, out ScoreIndex))
489-
throw Host.Except("Did not find column '{0}'", ScoreCol);
489+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", ScoreCol);
490490
}
491491

492492
protected PerInstanceEvaluatorBase(IHostEnvironment env, ModelLoadContext ctx, Schema schema)
@@ -500,9 +500,9 @@ protected PerInstanceEvaluatorBase(IHostEnvironment env, ModelLoadContext ctx,
500500
ScoreCol = ctx.LoadNonEmptyString();
501501
LabelCol = ctx.LoadStringOrNull();
502502
if (!string.IsNullOrEmpty(LabelCol) && !schema.TryGetColumnIndex(LabelCol, out LabelIndex))
503-
throw Host.Except($"Did not find the label column '{LabelCol}'");
503+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", LabelCol);
504504
if (!schema.TryGetColumnIndex(ScoreCol, out ScoreIndex))
505-
throw Host.Except($"Did not find column '{ScoreCol}'");
505+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", ScoreCol);
506506
}
507507

508508
public virtual void Save(ModelSaveContext ctx)

src/Microsoft.ML.Data/Evaluators/MultiClassClassifierEvaluator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,10 @@ private void CheckInputColumnTypes(Schema schema)
814814

815815
var scoreType = schema[ScoreIndex].Type as VectorType;
816816
if (scoreType == null || scoreType.Size < 2 || scoreType.ItemType != NumberType.Float)
817-
throw Host.Except("Score column '{0}' has type '{1}' but must be a vector of two or more items of type R4", ScoreCol, scoreType);
817+
throw Host.Except(nameof(schema), "score", "vector of two or more items of type float", ScoreCol, scoreType.ToString());
818818
var labelType = schema[LabelIndex].Type;
819819
if (labelType != NumberType.Float && labelType.GetKeyCount() <= 0)
820-
throw Host.Except("Label column '{0}' has type '{1}' but must be a float or a known-cardinality key", LabelCol, labelType);
820+
throw Host.Except(nameof(schema), "label", LabelCol, "float or key", labelType.ToString());
821821
}
822822
}
823823

@@ -999,7 +999,7 @@ private protected override IDataView GetPerInstanceMetricsCore(IDataView perInst
999999
// text file, since if there are different key counts the columns cannot be appended.
10001000
string labelName = schema.Label.Value.Name;
10011001
if (!perInst.Schema.TryGetColumnIndex(labelName, out int labelCol))
1002-
throw Host.Except("Could not find column '{0}'", labelName);
1002+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", labelName);
10031003
var labelType = perInst.Schema[labelCol].Type;
10041004
if (labelType is KeyType keyType && (!(bool)perInst.Schema[labelCol].HasKeyValues(keyType.Count) || labelType.RawType != typeof(uint)))
10051005
{

src/Microsoft.ML.Data/Evaluators/MultiOutputRegressionEvaluator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ private void CheckInputColumnTypes(Schema schema, out VectorType labelType, out
547547

548548
var t = schema[LabelIndex].Type as VectorType;
549549
if (t == null || !t.IsKnownSize || (t.ItemType != NumberType.R4 && t.ItemType != NumberType.R8))
550-
throw Host.Except("Label column '{0}' has type '{1}' but must be a known-size vector of R4 or R8", LabelCol, t);
550+
throw Host.Except(nameof(schema), "label", LabelCol, "known-size vector of float or double", t.ToString());
551551
labelType = new VectorType((PrimitiveType)t.ItemType, t.Size);
552552
var slotNamesType = new VectorType(TextType.Instance, t.Size);
553553
var builder = new MetadataBuilder();
@@ -556,7 +556,7 @@ private void CheckInputColumnTypes(Schema schema, out VectorType labelType, out
556556

557557
t = schema[ScoreIndex].Type as VectorType;
558558
if (t == null || !t.IsKnownSize || t.ItemType != NumberType.Float)
559-
throw Host.Except("Score column '{0}' has type '{1}' but must be a known length vector of type R4", ScoreCol, t);
559+
throw Host.Except(nameof(schema), "score", ScoreCol, "known-size vector of float", t.ToString());
560560
scoreType = new VectorType((PrimitiveType)t.ItemType, t.Size);
561561
builder = new MetadataBuilder();
562562
builder.AddSlotNames(t.Size, CreateSlotNamesGetter(schema, ScoreIndex, scoreType.Size, "Predicted"));

src/Microsoft.ML.Data/Evaluators/QuantileRegressionEvaluator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,13 @@ private void CheckInputColumnTypes(Schema schema)
445445

446446
var t = schema[(int)LabelIndex].Type;
447447
if (t != NumberType.R4)
448-
throw Host.Except("Label column '{0}' has type '{1}' but must be R4", LabelCol, t);
448+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", LabelCol, "float", t.ToString());
449449

450450
VectorType scoreType = schema[ScoreIndex].Type as VectorType;
451451
if (scoreType == null || scoreType.Size == 0 || (scoreType.ItemType != NumberType.R4 && scoreType.ItemType != NumberType.R8))
452452
{
453-
throw Host.Except(
454-
"Score column '{0}' has type '{1}' but must be a known length vector of type R4 or R8", ScoreCol, t);
453+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", ScoreCol, "known-size vector of float or double", t.ToString());
454+
455455
}
456456
}
457457
}

src/Microsoft.ML.Data/Evaluators/RegressionEvaluator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ private void CheckInputColumnTypes(Schema schema)
318318

319319
var t = schema[(int) LabelIndex].Type;
320320
if (t != NumberType.R4)
321-
throw Host.Except("Label column '{0}' has type '{1}' but must be R4", LabelCol, t);
321+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", LabelCol, "float", t.ToString());
322322

323323
t = schema[ScoreIndex].Type;
324324
if (t != NumberType.Float)
325-
throw Host.Except("Score column '{0}' has type '{1}' but must be R4", ScoreCol, t);
325+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", ScoreCol, "float", t.ToString());
326326
}
327327
}
328328

src/Microsoft.ML.Data/Prediction/CalibratorCatalog.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ protected CalibratorEstimatorBase(IHostEnvironment env,
9393
/// <param name="inputSchema">The input <see cref="SchemaShape"/>.</param>
9494
SchemaShape IEstimator<CalibratorTransformer<TICalibrator>>.GetOutputSchema(SchemaShape inputSchema)
9595
{
96-
Action<SchemaShape.Column, string> checkColumnValid = (SchemaShape.Column column, string expected) =>
96+
Action<SchemaShape.Column, string> checkColumnValid = (SchemaShape.Column column, string columnRole) =>
9797
{
9898
if (column.IsValid)
9999
{
100100
if (!inputSchema.TryFindColumn(column.Name, out var outCol))
101-
throw Host.Except($"{expected} column '{column.Name}' is not found");
101+
throw Host.ExceptSchemaMismatch(nameof(inputSchema), columnRole, column.Name);
102102
if (!column.IsCompatibleWith(outCol))
103-
throw Host.Except($"{expected} column '{column.Name}' is not compatible");
103+
throw Host.ExceptSchemaMismatch(nameof(inputSchema), columnRole, column.Name, column.GetTypeString(), outCol.GetTypeString());
104104
}
105105
};
106106

src/Microsoft.ML.Data/Training/TrainerEstimatorBase.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,26 @@ private void CheckInputSchema(SchemaShape inputSchema)
9494
{
9595
// Verify that all required input columns are present, and are of the same type.
9696
if (!inputSchema.TryFindColumn(FeatureColumn.Name, out var featureCol))
97-
throw Host.Except($"Feature column '{FeatureColumn.Name}' is not found");
97+
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "feature", FeatureColumn.Name);
9898
if (!FeatureColumn.IsCompatibleWith(featureCol))
99-
throw Host.Except($"Feature column '{FeatureColumn.Name}' is not compatible");
99+
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "feature", FeatureColumn.Name,
100+
FeatureColumn.GetTypeString(), featureCol.GetTypeString());
100101

101102
if (WeightColumn.IsValid)
102103
{
103104
if (!inputSchema.TryFindColumn(WeightColumn.Name, out var weightCol))
104-
throw Host.Except($"Weight column '{WeightColumn.Name}' is not found");
105+
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "weight", WeightColumn.Name);
105106
if (!WeightColumn.IsCompatibleWith(weightCol))
106-
throw Host.Except($"Weight column '{WeightColumn.Name}' is not compatible");
107+
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "weight", WeightColumn.Name,
108+
WeightColumn.GetTypeString(), weightCol.GetTypeString());
107109
}
108110

109111
// Special treatment for label column: we allow different types of labels, so the trainers
110112
// may define their own requirements on the label column.
111113
if (LabelColumn.IsValid)
112114
{
113115
if (!inputSchema.TryFindColumn(LabelColumn.Name, out var labelCol))
114-
throw Host.Except($"Label column '{LabelColumn.Name}' is not found");
116+
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "label", LabelColumn.Name);
115117
CheckLabelCompatible(labelCol);
116118
}
117119
}
@@ -122,7 +124,8 @@ protected virtual void CheckLabelCompatible(SchemaShape.Column labelCol)
122124
Host.Assert(LabelColumn.IsValid);
123125

124126
if (!LabelColumn.IsCompatibleWith(labelCol))
125-
throw Host.Except($"Label column '{LabelColumn.Name}' is not compatible");
127+
throw Host.ExceptSchemaMismatch(nameof(labelCol), "label", WeightColumn.Name,
128+
LabelColumn.GetTypeString(), labelCol.GetTypeString());
126129
}
127130

128131
protected TTransformer TrainTransformer(IDataView trainSet,

src/Microsoft.ML.Data/Transforms/NAFilter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ public NAFilter(IHost host, ModelLoadContext ctx, IDataView input)
144144
string src = ctx.LoadNonEmptyString();
145145
int index;
146146
if (!schema.TryGetColumnIndex(src, out index))
147-
throw Host.Except("Source column '{0}' not found", src);
147+
throw Host.ExceptSchemaMismatch(nameof(schema), "source", src);
148148
if (_srcIndexToInfoIndex.ContainsKey(index))
149149
throw Host.Except("Source column '{0}' specified multiple times", src);
150150

151151
var type = schema[index].Type;
152152
if (!TestType(type))
153-
throw Host.Except($"Column '{src}' has type {type} which does not support missing values, so we cannot filter on them", src);
153+
throw Host.ExceptSchemaMismatch(nameof(schema), "source", src, "scalar or vector of float, double or key", type.ToString());
154154

155155
_infos[i] = new ColInfo(index, type);
156156
_srcIndexToInfoIndex.Add(index, i);

src/Microsoft.ML.Data/Transforms/RangeFilter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ private RangeFilter(IHost host, ModelLoadContext ctx, IDataView input)
150150
var column = ctx.LoadNonEmptyString();
151151
var schema = Source.Schema;
152152
if (!schema.TryGetColumnIndex(column, out _index))
153-
throw Host.Except("column", "Source column '{0}' not found", column);
153+
throw Host.ExceptSchemaMismatch(nameof(schema), "source", column);
154154

155155
_type = schema[_index].Type;
156156
if (_type != NumberType.R4 && _type != NumberType.R8 && _type.GetKeyCount() == 0)
157-
throw Host.Except("column", "Column '{0}' does not have compatible type", column);
157+
throw Host.ExceptSchemaMismatch(nameof(schema), "source", column, "flaot, double or key", _type.ToString());
158158

159159
_min = ctx.Reader.ReadDouble();
160160
_max = ctx.Reader.ReadDouble();

src/Microsoft.ML.Data/Transforms/TransformBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public static Bindings Create(OneToOneTransformBase parent, ModelLoadContext ctx
371371

372372
int colSrc;
373373
if (!inputSchema.TryGetColumnIndex(src, out colSrc))
374-
throw host.Except("Source column '{0}' is required but not found", src);
374+
throw host.ExceptSchemaMismatch(nameof(inputSchema), "source", src);
375375
var type = inputSchema[colSrc].Type;
376376
if (testType != null)
377377
{

0 commit comments

Comments
 (0)