Skip to content

Commit

Permalink
Use HideEnumValueAttribute for both manifest and C# API generation. (d…
Browse files Browse the repository at this point in the history
…otnet#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.
  • Loading branch information
TomFinley authored and eerhardt committed Jul 27, 2018
1 parent 69033bd commit 2e486c2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 44 deletions.
3 changes: 1 addition & 2 deletions src/Microsoft.ML.Transforms/NAReplaceTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum ReplacementKind
Mean,
Minimum,
Maximum,
SpecifiedValue,

[HideEnumValue]
Def = DefaultValue,
Expand All @@ -53,8 +54,6 @@ public enum ReplacementKind
[HideEnumValue]
Max = Maximum,

[HideEnumValue]
SpecifiedValue,
[HideEnumValue]
Val = SpecifiedValue,
[HideEnumValue]
Expand Down
18 changes: 4 additions & 14 deletions src/Microsoft.ML/CSharpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11058,14 +11058,10 @@ namespace Transforms
{
public enum NAHandleTransformReplacementKind
{
Default = 0,
Def = 0,
DefaultValue = 0,
Mean = 1,
Minimum = 2,
Min = 2,
Maximum = 3,
Max = 3
Maximum = 3
}


Expand Down Expand Up @@ -11153,7 +11149,7 @@ public void AddColumn(string outputColumn, string inputColumn)
/// <summary>
/// The replacement method to utilize
/// </summary>
public NAHandleTransformReplacementKind ReplaceWith { get; set; } = NAHandleTransformReplacementKind.Def;
public NAHandleTransformReplacementKind ReplaceWith { get; set; } = NAHandleTransformReplacementKind.DefaultValue;

/// <summary>
/// Whether to impute values by slot
Expand Down Expand Up @@ -11527,17 +11523,11 @@ namespace Transforms
{
public enum NAReplaceTransformReplacementKind
{
Default = 0,
DefaultValue = 0,
Def = 0,
Mean = 1,
Min = 2,
Minimum = 2,
Max = 3,
Maximum = 3,
SpecifiedValue = 4,
Val = 4,
Value = 4
SpecifiedValue = 4
}


Expand Down Expand Up @@ -11625,7 +11615,7 @@ public void AddColumn(string outputColumn, string inputColumn)
/// <summary>
/// The replacement method to utilize
/// </summary>
public NAReplaceTransformReplacementKind ReplacementKind { get; set; } = NAReplaceTransformReplacementKind.Def;
public NAReplaceTransformReplacementKind ReplacementKind { get; set; } = NAReplaceTransformReplacementKind.DefaultValue;

/// <summary>
/// Whether to impute values by slot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private static JToken BuildTypeToken(IExceptionContext ectx, FieldInfo fieldInfo
case TlcModule.DataKind.Enum:
jo = new JObject();
jo[FieldNames.Kind] = typeEnum.ToString();
var values = Enum.GetNames(type);
var values = Enum.GetNames(type).Where(n => type.GetField(n).GetCustomAttribute<HideEnumValueAttribute>() == null);
jo[FieldNames.Values] = new JArray(values);
return jo;
case TlcModule.DataKind.Array:
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.ML/Runtime/Internal/Tools/CSharpApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.ML.Runtime;
using Microsoft.ML.Runtime.CommandLine;
using Microsoft.ML.Runtime.Data;
Expand Down Expand Up @@ -156,6 +157,8 @@ private void GenerateEnums(IndentingTextWriter writer, Type inputType, string cu
for (int i = 0; i < names.Length; i++)
{
var name = names[i];
if (type.GetField(name).GetCustomAttribute<HideEnumValueAttribute>() != null)
continue;
var value = values.GetValue(i);
writer.WriteLine(prefix);
if (enumType == typeof(int))
Expand Down
17 changes: 15 additions & 2 deletions src/Microsoft.ML/Runtime/Internal/Tools/CSharpGeneratorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.CSharp;
using Microsoft.ML.Runtime.CommandLine;
using Microsoft.ML.Runtime.EntryPoints;
Expand Down Expand Up @@ -279,10 +280,22 @@ public static string GetValue(ModuleCatalog catalog, Type fieldType, object fiel
case TlcModule.DataKind.Bool:
return (bool)fieldValue ? "true" : "false";
case TlcModule.DataKind.Enum:
string enumAsString = fieldValue.ToString();
if (fieldType.GetField(enumAsString).GetCustomAttribute<HideEnumValueAttribute>() != null)
{
// The default value for the enum has the hiding attribute on it. We will search for
// alternate names. Regrettably I see no way beyond a manual scan.

string unhiddenName = Enum.GetNames(fieldType).Zip(Enum.GetValues(fieldType).Cast<object>(), (name, val) => (name, val))
.Where(pair => pair.val.Equals(fieldValue))
.Where(pair => fieldType.GetField(pair.name).GetCustomAttribute<HideEnumValueAttribute>() == null)
.Select(pair => pair.name).FirstOrDefault();
enumAsString = unhiddenName ?? throw Contracts.Except($"Could not find unhidden alternative for '{fieldValue}' in type '{fieldType}'");
}
if (generatedClasses.IsGenerated(fieldType.FullName))
return generatedClasses.GetApiName(fieldType, rootNameSpace) + "." + fieldValue;
return generatedClasses.GetApiName(fieldType, rootNameSpace) + "." + enumAsString;
else
return generatedClasses.GetApiName(fieldType, "Runtime") + "." + fieldValue;
return generatedClasses.GetApiName(fieldType, "Runtime") + "." + enumAsString;
case TlcModule.DataKind.Char:
return $"'{GetCharAsString((char)fieldValue)}'";
case TlcModule.DataKind.Component:
Expand Down
30 changes: 5 additions & 25 deletions test/BaselineOutput/Common/EntryPoints/core_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15390,14 +15390,10 @@
"Type": {
"Kind": "Enum",
"Values": [
"Default",
"Def",
"DefaultValue",
"Mean",
"Minimum",
"Min",
"Maximum",
"Max"
"Maximum"
]
},
"Desc": "The replacement method to utilize",
Expand Down Expand Up @@ -15478,14 +15474,10 @@
"Type": {
"Kind": "Enum",
"Values": [
"Default",
"Def",
"DefaultValue",
"Mean",
"Minimum",
"Min",
"Maximum",
"Max"
"Maximum"
]
},
"Desc": "The replacement method to utilize",
Expand Down Expand Up @@ -15780,17 +15772,11 @@
"Type": {
"Kind": "Enum",
"Values": [
"Default",
"DefaultValue",
"Def",
"Mean",
"Min",
"Minimum",
"Max",
"Maximum",
"SpecifiedValue",
"Val",
"Value"
"SpecifiedValue"
]
},
"Desc": "The replacement method to utilize",
Expand Down Expand Up @@ -15856,17 +15842,11 @@
"Type": {
"Kind": "Enum",
"Values": [
"Default",
"DefaultValue",
"Def",
"Mean",
"Min",
"Minimum",
"Max",
"Maximum",
"SpecifiedValue",
"Val",
"Value"
"SpecifiedValue"
]
},
"Desc": "The replacement method to utilize",
Expand All @@ -15876,7 +15856,7 @@
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": "Def"
"Default": "Default"
},
{
"Name": "ImputeBySlot",
Expand Down

0 comments on commit 2e486c2

Please sign in to comment.