Skip to content

Commit 2e486c2

Browse files
TomFinleyeerhardt
authored andcommitted
Use HideEnumValueAttribute for both manifest and C# API generation. (dotnet#356)
* Use HideEnumValueAttribute for both manifest and C# API generation. * Unhide NAReplaceTransform.ReplacementKind.SpecifiedValue. This may require some other PR to resolve the corresponding issues.
1 parent 69033bd commit 2e486c2

File tree

6 files changed

+29
-44
lines changed

6 files changed

+29
-44
lines changed

src/Microsoft.ML.Transforms/NAReplaceTransform.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public enum ReplacementKind
4343
Mean,
4444
Minimum,
4545
Maximum,
46+
SpecifiedValue,
4647

4748
[HideEnumValue]
4849
Def = DefaultValue,
@@ -53,8 +54,6 @@ public enum ReplacementKind
5354
[HideEnumValue]
5455
Max = Maximum,
5556

56-
[HideEnumValue]
57-
SpecifiedValue,
5857
[HideEnumValue]
5958
Val = SpecifiedValue,
6059
[HideEnumValue]

src/Microsoft.ML/CSharpApi.cs

+4-14
Original file line numberDiff line numberDiff line change
@@ -11058,14 +11058,10 @@ namespace Transforms
1105811058
{
1105911059
public enum NAHandleTransformReplacementKind
1106011060
{
11061-
Default = 0,
11062-
Def = 0,
1106311061
DefaultValue = 0,
1106411062
Mean = 1,
1106511063
Minimum = 2,
11066-
Min = 2,
11067-
Maximum = 3,
11068-
Max = 3
11064+
Maximum = 3
1106911065
}
1107011066

1107111067

@@ -11153,7 +11149,7 @@ public void AddColumn(string outputColumn, string inputColumn)
1115311149
/// <summary>
1115411150
/// The replacement method to utilize
1115511151
/// </summary>
11156-
public NAHandleTransformReplacementKind ReplaceWith { get; set; } = NAHandleTransformReplacementKind.Def;
11152+
public NAHandleTransformReplacementKind ReplaceWith { get; set; } = NAHandleTransformReplacementKind.DefaultValue;
1115711153

1115811154
/// <summary>
1115911155
/// Whether to impute values by slot
@@ -11527,17 +11523,11 @@ namespace Transforms
1152711523
{
1152811524
public enum NAReplaceTransformReplacementKind
1152911525
{
11530-
Default = 0,
1153111526
DefaultValue = 0,
11532-
Def = 0,
1153311527
Mean = 1,
11534-
Min = 2,
1153511528
Minimum = 2,
11536-
Max = 3,
1153711529
Maximum = 3,
11538-
SpecifiedValue = 4,
11539-
Val = 4,
11540-
Value = 4
11530+
SpecifiedValue = 4
1154111531
}
1154211532

1154311533

@@ -11625,7 +11615,7 @@ public void AddColumn(string outputColumn, string inputColumn)
1162511615
/// <summary>
1162611616
/// The replacement method to utilize
1162711617
/// </summary>
11628-
public NAReplaceTransformReplacementKind ReplacementKind { get; set; } = NAReplaceTransformReplacementKind.Def;
11618+
public NAReplaceTransformReplacementKind ReplacementKind { get; set; } = NAReplaceTransformReplacementKind.DefaultValue;
1162911619

1163011620
/// <summary>
1163111621
/// Whether to impute values by slot

src/Microsoft.ML/Runtime/EntryPoints/JsonUtils/JsonManifestUtils.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private static JToken BuildTypeToken(IExceptionContext ectx, FieldInfo fieldInfo
344344
case TlcModule.DataKind.Enum:
345345
jo = new JObject();
346346
jo[FieldNames.Kind] = typeEnum.ToString();
347-
var values = Enum.GetNames(type);
347+
var values = Enum.GetNames(type).Where(n => type.GetField(n).GetCustomAttribute<HideEnumValueAttribute>() == null);
348348
jo[FieldNames.Values] = new JArray(values);
349349
return jo;
350350
case TlcModule.DataKind.Array:

src/Microsoft.ML/Runtime/Internal/Tools/CSharpApiGenerator.cs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.IO;
88
using System.Linq;
9+
using System.Reflection;
910
using Microsoft.ML.Runtime;
1011
using Microsoft.ML.Runtime.CommandLine;
1112
using Microsoft.ML.Runtime.Data;
@@ -156,6 +157,8 @@ private void GenerateEnums(IndentingTextWriter writer, Type inputType, string cu
156157
for (int i = 0; i < names.Length; i++)
157158
{
158159
var name = names[i];
160+
if (type.GetField(name).GetCustomAttribute<HideEnumValueAttribute>() != null)
161+
continue;
159162
var value = values.GetValue(i);
160163
writer.WriteLine(prefix);
161164
if (enumType == typeof(int))

src/Microsoft.ML/Runtime/Internal/Tools/CSharpGeneratorUtils.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.CodeDom;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using System.Reflection;
910
using Microsoft.CSharp;
1011
using Microsoft.ML.Runtime.CommandLine;
1112
using Microsoft.ML.Runtime.EntryPoints;
@@ -279,10 +280,22 @@ public static string GetValue(ModuleCatalog catalog, Type fieldType, object fiel
279280
case TlcModule.DataKind.Bool:
280281
return (bool)fieldValue ? "true" : "false";
281282
case TlcModule.DataKind.Enum:
283+
string enumAsString = fieldValue.ToString();
284+
if (fieldType.GetField(enumAsString).GetCustomAttribute<HideEnumValueAttribute>() != null)
285+
{
286+
// The default value for the enum has the hiding attribute on it. We will search for
287+
// alternate names. Regrettably I see no way beyond a manual scan.
288+
289+
string unhiddenName = Enum.GetNames(fieldType).Zip(Enum.GetValues(fieldType).Cast<object>(), (name, val) => (name, val))
290+
.Where(pair => pair.val.Equals(fieldValue))
291+
.Where(pair => fieldType.GetField(pair.name).GetCustomAttribute<HideEnumValueAttribute>() == null)
292+
.Select(pair => pair.name).FirstOrDefault();
293+
enumAsString = unhiddenName ?? throw Contracts.Except($"Could not find unhidden alternative for '{fieldValue}' in type '{fieldType}'");
294+
}
282295
if (generatedClasses.IsGenerated(fieldType.FullName))
283-
return generatedClasses.GetApiName(fieldType, rootNameSpace) + "." + fieldValue;
296+
return generatedClasses.GetApiName(fieldType, rootNameSpace) + "." + enumAsString;
284297
else
285-
return generatedClasses.GetApiName(fieldType, "Runtime") + "." + fieldValue;
298+
return generatedClasses.GetApiName(fieldType, "Runtime") + "." + enumAsString;
286299
case TlcModule.DataKind.Char:
287300
return $"'{GetCharAsString((char)fieldValue)}'";
288301
case TlcModule.DataKind.Component:

test/BaselineOutput/Common/EntryPoints/core_manifest.json

+5-25
Original file line numberDiff line numberDiff line change
@@ -15390,14 +15390,10 @@
1539015390
"Type": {
1539115391
"Kind": "Enum",
1539215392
"Values": [
15393-
"Default",
15394-
"Def",
1539515393
"DefaultValue",
1539615394
"Mean",
1539715395
"Minimum",
15398-
"Min",
15399-
"Maximum",
15400-
"Max"
15396+
"Maximum"
1540115397
]
1540215398
},
1540315399
"Desc": "The replacement method to utilize",
@@ -15478,14 +15474,10 @@
1547815474
"Type": {
1547915475
"Kind": "Enum",
1548015476
"Values": [
15481-
"Default",
15482-
"Def",
1548315477
"DefaultValue",
1548415478
"Mean",
1548515479
"Minimum",
15486-
"Min",
15487-
"Maximum",
15488-
"Max"
15480+
"Maximum"
1548915481
]
1549015482
},
1549115483
"Desc": "The replacement method to utilize",
@@ -15780,17 +15772,11 @@
1578015772
"Type": {
1578115773
"Kind": "Enum",
1578215774
"Values": [
15783-
"Default",
1578415775
"DefaultValue",
15785-
"Def",
1578615776
"Mean",
15787-
"Min",
1578815777
"Minimum",
15789-
"Max",
1579015778
"Maximum",
15791-
"SpecifiedValue",
15792-
"Val",
15793-
"Value"
15779+
"SpecifiedValue"
1579415780
]
1579515781
},
1579615782
"Desc": "The replacement method to utilize",
@@ -15856,17 +15842,11 @@
1585615842
"Type": {
1585715843
"Kind": "Enum",
1585815844
"Values": [
15859-
"Default",
1586015845
"DefaultValue",
15861-
"Def",
1586215846
"Mean",
15863-
"Min",
1586415847
"Minimum",
15865-
"Max",
1586615848
"Maximum",
15867-
"SpecifiedValue",
15868-
"Val",
15869-
"Value"
15849+
"SpecifiedValue"
1587015850
]
1587115851
},
1587215852
"Desc": "The replacement method to utilize",
@@ -15876,7 +15856,7 @@
1587615856
"Required": false,
1587715857
"SortOrder": 150.0,
1587815858
"IsNullable": false,
15879-
"Default": "Def"
15859+
"Default": "Default"
1588015860
},
1588115861
{
1588215862
"Name": "ImputeBySlot",

0 commit comments

Comments
 (0)