Skip to content

Commit ac287d8

Browse files
committed
Improve error reporting uniformity through usage of SchemaMismatch contracts helpers.
1 parent beee99e commit ac287d8

8 files changed

+28
-33
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
129129
var host = Host.SchemaSensitive();
130130
var t = score.Type;
131131
if (t.IsVector || t.ItemType != NumberType.Float)
132-
throw host.SchemaSensitive().Except("Score column '{0}' has type '{1}' but must be R4", score, t);
132+
throw host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "R4", t.ToString());
133133
host.Check(schema.Label.HasValue, "Could not find the label column");
134134
t = schema.Label.Value.Type;
135135
if (t != NumberType.R4 && t != NumberType.R8 && t != BoolType.Instance && t.KeyCount != 2)
136-
throw host.SchemaSensitive().Except("Label column '{0}' has type '{1}' but must be R4, R8, BL or a 2-value key", schema.Label.Value.Name, t);
136+
throw host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "R4, R8, BL or a 2-value key", t.ToString());
137137
}
138138

139139
private protected override void CheckCustomColumnTypesCore(RoleMappedSchema schema)
@@ -142,14 +142,14 @@ private protected override void CheckCustomColumnTypesCore(RoleMappedSchema sche
142142
var host = Host.SchemaSensitive();
143143
if (prob != null)
144144
{
145-
host.Check(prob.Count == 1, "Cannot have multiple probability columns");
145+
host.CheckParam(prob.Count == 1, nameof(schema), "Cannot have multiple probability columns");
146146
var probType = prob[0].Type;
147147
if (probType != NumberType.Float)
148-
throw host.SchemaSensitive().Except("Probability column '{0}' has type '{1}' but must be R4", prob[0].Name, probType);
148+
throw host.ExceptSchemaMismatch(nameof(schema), "probability", prob[0].Name, "R4", probType.ToString());
149149
}
150150
else if (!_useRaw)
151151
{
152-
throw host.Except(
152+
throw host.ExceptParam(nameof(schema),
153153
"Cannot compute the predicted label from the probability column because it does not exist");
154154
}
155155
}

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,14 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
9999
ColumnType type = schema.Label?.Type;
100100
if (type != null && type != NumberType.Float && !(type is KeyType keyType && keyType.Count > 0))
101101
{
102-
throw Host.Except("Clustering evaluator: label column '{0}' type must be {1} or Key of known cardinality." +
103-
" Provide a correct label column, or none: it is optional.",
104-
schema.Label.Value.Name, NumberType.Float);
102+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name,
103+
"R4 or key of known cardinality", type.ToString());
105104
}
106105

107106
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
108107
type = score.Type;
109108
if (!type.IsKnownSizeVector || type.ItemType != NumberType.Float)
110-
throw Host.Except("Scores column '{0}' type must be a float vector of known size", score.Name);
109+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "R4 vector of known size", type.ToString());
111110
}
112111

113112
private protected override void CheckCustomColumnTypesCore(RoleMappedSchema schema)
@@ -118,8 +117,8 @@ private protected override void CheckCustomColumnTypesCore(RoleMappedSchema sche
118117
var t = schema.Feature.Value.Type;
119118
if (!t.IsKnownSizeVector || t.ItemType != NumberType.Float)
120119
{
121-
throw Host.Except("Features column '{0}' type must be {1} vector of known-size",
122-
schema.Feature.Value.Name, NumberType.Float);
120+
throw Host.ExceptSchemaMismatch(nameof(schema), "features", schema.Feature.Value.Name,
121+
"R4 vector of known size", t.ToString());
123122
}
124123
}
125124
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
7777
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
7878
var t = score.Type;
7979
if (t.VectorSize < 2 || t.ItemType != NumberType.Float)
80-
throw Host.Except("Score column '{0}' has type {1} but must be a vector of two or more items of type R4", score.Name, t);
80+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "vector of two or more items of type R4", t.ToString());
8181
Host.CheckParam(schema.Label.HasValue, nameof(schema), "Could not find the label column");
8282
t = schema.Label.Value.Type;
8383
if (t != NumberType.Float && t.KeyCount <= 0)
84-
throw Host.Except("Label column '{0}' has type {1} but must be a float or a known-cardinality key", schema.Label.Value.Name, t);
84+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "float or a known-cardinality key", t.ToString());
8585
}
8686

8787
private protected override Aggregator GetAggregatorCore(RoleMappedSchema schema, string stratName)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
6060
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
6161
var t = score.Type;
6262
if (t.VectorSize == 0 || t.ItemType != NumberType.Float)
63-
throw Host.Except("Score column '{0}' has type '{1}' but must be a known length vector of type R4", score.Name, t);
63+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "known size vector of R4", t.ToString());
6464
Host.Check(schema.Label.HasValue, "Could not find the label column");
6565
t = schema.Label.Value.Type;
6666
if (!t.IsKnownSizeVector || (t.ItemType != NumberType.R4 && t.ItemType != NumberType.R8))
67-
throw Host.Except("Label column '{0}' has type '{1}' but must be a known-size vector of R4 or R8", schema.Label.Value.Name, t);
67+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "known size vector of R4 or R8", t.ToString());
6868
}
6969

7070
private protected override Aggregator GetAggregatorCore(RoleMappedSchema schema, string stratName)

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
5858
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
5959
var t = score.Type;
6060
if (t.VectorSize == 0 || (t.ItemType != NumberType.R4 && t.ItemType != NumberType.R8))
61-
{
62-
throw Host.Except(
63-
"Score column '{0}' has type '{1}' but must be a known length vector of type R4 or R8", score.Name, t);
64-
}
61+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "vector of type R4 or R8", t.ToString());
6562
Host.CheckParam(schema.Label.HasValue, nameof(schema), "Must contain a label column");
6663
t = schema.Label.Value.Type;
6764
if (t != NumberType.R4)
68-
throw Host.Except("Label column '{0}' has type '{1}' but must be R4", schema.Label.Value.Name, t);
65+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", schema.Label.Value.Name, "R4", t.ToString());
6966
}
7067

7168
private protected override Aggregator GetAggregatorCore(RoleMappedSchema schema, string stratName)

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
8989
var t = schema.Label.Value.Type;
9090
if (t != NumberType.Float && !(t is KeyType))
9191
{
92-
throw Host.ExceptUserArg(nameof(RankerMamlEvaluator.Arguments.LabelColumn), "Label column '{0}' has type '{1}' but must be R4 or a key",
93-
schema.Label.Value.Name, t);
92+
throw Host.ExceptSchemaMismatch(nameof(RankerMamlEvaluator.Arguments.LabelColumn),
93+
"label", schema.Label.Value.Name, "R4 or a key", t.ToString());
9494
}
95-
var scoreInfo = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
96-
if (scoreInfo.Type != NumberType.Float)
95+
var scoreCol = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
96+
if (scoreCol.Type != NumberType.Float)
9797
{
98-
throw Host.ExceptUserArg(nameof(RankerMamlEvaluator.Arguments.ScoreColumn), "Score column '{0}' has type '{1}' but must be R4",
99-
scoreInfo.Name, t);
98+
throw Host.ExceptSchemaMismatch(nameof(RankerMamlEvaluator.Arguments.ScoreColumn),
99+
"score", scoreCol.Name, "R4", t.ToString());
100100
}
101101
}
102102

@@ -105,9 +105,8 @@ private protected override void CheckCustomColumnTypesCore(RoleMappedSchema sche
105105
var t = schema.Group.Value.Type;
106106
if (!(t is KeyType))
107107
{
108-
throw Host.ExceptUserArg(nameof(RankerMamlEvaluator.Arguments.GroupIdColumn),
109-
"Group column '{0}' has type '{1}' but must be a key",
110-
schema.Group.Value.Name, t);
108+
throw Host.ExceptSchemaMismatch(nameof(RankerMamlEvaluator.Arguments.GroupIdColumn),
109+
"group", schema.Group.Value.Name, "key", t.ToString());
111110
}
112111
}
113112

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
5757
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
5858
var t = score.Type;
5959
if (t != NumberType.Float)
60-
throw Host.Except("Score column '{0}' has type '{1}' but must be R4", score, t);
61-
Host.Check(schema.Label.HasValue, "Could not find the label column");
60+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "R4", t.ToString());
61+
Host.CheckParam(schema.Label.HasValue, nameof(schema), "Could not find the label column");
6262
t = schema.Label.Value.Type;
6363
if (t != NumberType.R4)
64-
throw Host.Except("Label column '{0}' has type '{1}' but must be R4", schema.Label.Value.Name, t);
64+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "R4", t.ToString());
6565
}
6666

6767
private protected override Aggregator GetAggregatorCore(RoleMappedSchema schema, string stratName)

src/Microsoft.ML.TimeSeries/AdaptiveSingularSpectrumSequenceModeler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ internal override void Train(RoleMappedData data)
12291229
_host.CheckParam(data.Schema.Feature.HasValue, nameof(data), "Must have features column.");
12301230
var featureCol = data.Schema.Feature.Value;
12311231
if (featureCol.Type != NumberType.Float)
1232-
throw _host.ExceptParam(nameof(data), "The feature column has type '{0}', but must be a float.", featureCol.Type);
1232+
throw _host.ExceptSchemaMismatch(nameof(data), "feature", featureCol.Name, "R4", featureCol.Type.ToString());
12331233

12341234
Single[] dataArray = new Single[_trainSize];
12351235
int col = featureCol.Index;

0 commit comments

Comments
 (0)