Skip to content

Commit 12d91f9

Browse files
authored
Improve exception message and make consistent with ExceptSchemaMismatch (#2217)
1 parent e383091 commit 12d91f9

File tree

47 files changed

+120
-115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+120
-115
lines changed

src/Microsoft.ML.CpuMath/ProbabilityFunctions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public static double Erfinv(double x)
119119
/// <returns>One intepretation is, the value at which the standard normal CDF evaluates to p.</returns>
120120
public static double Probit(double p)
121121
{
122+
Contracts.CheckParam(0 <= p && p <= 1, nameof(p), "Input probability should be in range 0 to 1.");
123+
122124
double q = p - 0.5;
123125
double r = 0.0;
124126
if (Math.Abs(q) <= 0.425)
@@ -135,8 +137,6 @@ public static double Probit(double p)
135137
else
136138
r = 1 - p;
137139

138-
Contracts.CheckParam(r >= 0, nameof(p), "Illegal input value");
139-
140140
r = Math.Sqrt(-Math.Log(r));
141141
double retval = 0.0;
142142
if (r < 5)

src/Microsoft.ML.Data/DataLoadSave/DataOperationsCatalog.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public IDataView FilterByKeyColumnFraction(IDataView input, string columnName, d
8787

8888
var type = input.Schema[columnName].Type;
8989
if (type.GetKeyCount() == 0)
90-
throw Environment.ExceptSchemaMismatch(nameof(columnName), "filter", columnName, "a known cardinality key", type.ToString());
90+
throw Environment.ExceptSchemaMismatch(nameof(columnName), "filter", columnName, "KeyType", type.ToString());
9191
return new RangeFilter(Environment, input, columnName, lowerBound, upperBound, false);
9292
}
9393
}

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

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

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
9696
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
9797
var t = score.Type;
9898
if (t != NumberType.Float)
99-
throw Host.Except("Score column '{0}' has type '{1}' but must be R4", score, t).MarkSensitive(MessageSensitivity.Schema);
99+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "float", t.ToString());
100100
Host.Check(schema.Label.HasValue, "Could not find the label column");
101101
t = schema.Label.Value.Type;
102102
if (t != NumberType.Float && t.GetKeyCount() != 2)
103-
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);
103+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "float or a KeyType with cardinality 2", t.ToString());
104104
}
105105

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

643643
bool hasRows = false;
@@ -671,7 +671,7 @@ private protected override void PrintFoldResultsCore(IChannel ch, Dictionary<str
671671
// Find the number of anomalies, and the thresholds.
672672
int numAnomIndex;
673673
if (!overall.Schema.TryGetColumnIndex(AnomalyDetectionEvaluator.OverallMetrics.NumAnomalies, out numAnomIndex))
674-
throw Host.Except("Could not find the 'NumAnomalies' column");
674+
throw Host.ExceptSchemaMismatch(nameof(overall.Schema), "number of anomalies", AnomalyDetectionEvaluator.OverallMetrics.NumAnomalies);
675675

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

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

+6-6
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 != NumberType.Float)
132-
throw host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "R4", t.ToString());
132+
throw host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "float", 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.GetKeyCount() != 2)
136-
throw host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "R4, R8, BL or a 2-value key", t.ToString());
136+
throw host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "float, double, bool, or a KeyType with cardinality 2", t.ToString());
137137
}
138138

139139
private protected override void CheckCustomColumnTypesCore(RoleMappedSchema schema)
@@ -145,7 +145,7 @@ private protected override void CheckCustomColumnTypesCore(RoleMappedSchema sche
145145
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.ExceptSchemaMismatch(nameof(schema), "probability", prob[0].Name, "R4", probType.ToString());
148+
throw host.ExceptSchemaMismatch(nameof(schema), "probability", prob[0].Name, "float", probType.ToString());
149149
}
150150
else if (!_useRaw)
151151
{
@@ -1098,18 +1098,18 @@ private void CheckInputColumnTypes(Schema schema)
10981098

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

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

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

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
100100
if (type != null && type != NumberType.Float && !(type is KeyType keyType && keyType.Count > 0))
101101
{
102102
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name,
103-
"R4 or key of known cardinality", type.ToString());
103+
"float or KeyType", type.ToString());
104104
}
105105

106106
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
107107
type = score.Type;
108108
if (!(type is VectorType vectorType) || !vectorType.IsKnownSize || vectorType.ItemType != NumberType.Float)
109-
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "R4 vector of known size", type.ToString());
109+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "known-size vector of float", type.ToString());
110110
}
111111

112112
private protected override void CheckCustomColumnTypesCore(RoleMappedSchema schema)
@@ -754,7 +754,7 @@ private void CheckInputColumnTypes(Schema schema)
754754

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

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

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

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

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

510510
public virtual void Save(ModelSaveContext ctx)

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

+5-5
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 scoreType = score.Type as VectorType;
7979
if (scoreType == null || scoreType.Size < 2 || scoreType.ItemType != NumberType.Float)
80-
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "vector of two or more items of type R4", scoreType.ToString());
80+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "vector of two or more items of type float", scoreType.ToString());
8181
Host.CheckParam(schema.Label.HasValue, nameof(schema), "Could not find the label column");
8282
var labelType = schema.Label.Value.Type;
8383
if (labelType != NumberType.Float && labelType.GetKeyCount() <= 0)
84-
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "float or a known-cardinality key", labelType.ToString());
84+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "float or KeyType", labelType.ToString());
8585
}
8686

8787
private protected override Aggregator GetAggregatorCore(RoleMappedSchema schema, string stratName)
@@ -815,10 +815,10 @@ private void CheckInputColumnTypes(Schema schema)
815815

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

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

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

+4-4
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 as VectorType;
6262
if (t == null || !t.IsKnownSize || t.ItemType != NumberType.Float)
63-
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "known size vector of R4", t.ToString());
63+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "known-size vector of float", t.ToString());
6464
Host.Check(schema.Label.HasValue, "Could not find the label column");
6565
t = schema.Label.Value.Type as VectorType;
6666
if (t == null || !t.IsKnownSize || (t.ItemType != NumberType.R4 && t.ItemType != NumberType.R8))
67-
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "known size vector of R4 or R8", t.ToString());
67+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "known-size vector of float or double", t.ToString());
6868
}
6969

7070
private protected override Aggregator GetAggregatorCore(RoleMappedSchema schema, string stratName)
@@ -548,7 +548,7 @@ private void CheckInputColumnTypes(Schema schema, out VectorType labelType, out
548548

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

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

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ private protected override void CheckScoreAndLabelTypes(RoleMappedSchema schema)
5858
var score = schema.GetUniqueColumn(MetadataUtils.Const.ScoreValueKind.Score);
5959
var t = score.Type as VectorType;
6060
if (t == null || t.Size == 0 || (t.ItemType != NumberType.R4 && t.ItemType != NumberType.R8))
61-
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "vector of type R4 or R8", t.ToString());
61+
throw Host.ExceptSchemaMismatch(nameof(schema), "score", score.Name, "vector of float or double", t.ToString());
6262
Host.CheckParam(schema.Label.HasValue, nameof(schema), "Must contain a label column");
6363
var labelType = schema.Label.Value.Type;
6464
if (labelType != NumberType.R4)
65-
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "R4", t.ToString());
65+
throw Host.ExceptSchemaMismatch(nameof(schema), "label", schema.Label.Value.Name, "float", t.ToString());
6666
}
6767

6868
private protected override Aggregator GetAggregatorCore(RoleMappedSchema schema, string stratName)
@@ -446,13 +446,13 @@ private void CheckInputColumnTypes(Schema schema)
446446

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

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

0 commit comments

Comments
 (0)