Skip to content

Commit 0a618ad

Browse files
authored
Cleaning our attributes classes (#2638)
1 parent 3b9d407 commit 0a618ad

File tree

17 files changed

+115
-87
lines changed

17 files changed

+115
-87
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/ImageClassification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Linq;
33
using Microsoft.ML.Data;
44

5-
namespace Microsoft.ML.Samples.Dynamic.TensorFlow
5+
namespace Microsoft.ML.Samples.Dynamic
66
{
77
public static class ImageClassification
88
{

docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/TextClassification.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.ML.Data;
44
using Microsoft.ML.Transforms.TensorFlow;
55

6-
namespace Microsoft.ML.Samples.Dynamic.TensorFlow
6+
namespace Microsoft.ML.Samples.Dynamic
77
{
88
public static class TextClassification
99
{
@@ -103,11 +103,11 @@ public class IMDBSentiment
103103
public string Sentiment_Text { get; set; }
104104

105105
/// <summary>
106-
/// This is a variable length vector designated by VectorType(0) attribute.
106+
/// This is a variable length vector designated by VectorType attribute.
107107
/// Variable length vectors are produced by applying operations such as 'TokenizeWords' on strings
108108
/// resulting in vectors of tokens of variable lengths.
109109
/// </summary>
110-
[VectorType(0)]
110+
[VectorType]
111111
public int[] VariableLenghtFeatures { get; set; }
112112
}
113113

src/Microsoft.ML.Core/CommandLine/DefaultArgumentAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.ML.CommandLine
1515
/// </summary>
1616
[AttributeUsage(AttributeTargets.Field)]
1717
[BestFriend]
18-
internal class DefaultArgumentAttribute : ArgumentAttribute
18+
internal sealed class DefaultArgumentAttribute : ArgumentAttribute
1919
{
2020
/// <summary>
2121
/// Indicates that this argument is the default argument.

src/Microsoft.ML.Core/CommandLine/EnumValueDisplayAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.ML.CommandLine
1111
/// </summary>
1212
[AttributeUsage(AttributeTargets.Field)]
1313
[BestFriend]
14-
internal class EnumValueDisplayAttribute : Attribute
14+
internal sealed class EnumValueDisplayAttribute : Attribute
1515
{
1616
public readonly string Name;
1717

src/Microsoft.ML.Core/CommandLine/HideEnumValueAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.ML.CommandLine
1111
/// </summary>
1212
[AttributeUsage(AttributeTargets.Field)]
1313
[BestFriend]
14-
internal class HideEnumValueAttribute : Attribute
14+
internal sealed class HideEnumValueAttribute : Attribute
1515
{
1616
public HideEnumValueAttribute()
1717
{

src/Microsoft.ML.Data/Data/SchemaDefinition.cs

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,82 +12,108 @@
1212
namespace Microsoft.ML.Data
1313
{
1414
/// <summary>
15-
/// Attach to a member of a class to indicate that the item type should be of class key.
15+
/// Allow member to be marked as a <see cref="KeyType"/>.
1616
/// </summary>
17+
/// <remarks>
18+
/// Can be applied only for member of following types: <see cref="byte"/>, <see cref="ushort"/>, <see cref="uint"/>, <see cref="ulong"/>
19+
/// </remarks>
1720
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
1821
public sealed class KeyTypeAttribute : Attribute
1922
{
20-
// REVIEW: Property based, but should I just have a constructor?
23+
/// <summary>
24+
/// Marks member as <see cref="KeyType"/>.
25+
/// </summary>
26+
/// <remarks>
27+
/// Cardinality of <see cref="KeyType"/> would be maximum legal value of member type.
28+
/// </remarks>
29+
public KeyTypeAttribute()
30+
{
31+
32+
}
33+
34+
/// <summary>
35+
/// Marks member as <see cref="KeyType"/> and specifies <see cref="KeyType"/> cardinality.
36+
/// </summary>
37+
/// <param name="count">Cardinality of <see cref="KeyType"/>.</param>
38+
public KeyTypeAttribute(ulong count)
39+
{
40+
KeyCount = new KeyCount(count);
41+
}
2142

2243
/// <summary>
2344
/// The key count.
2445
/// </summary>
25-
public ulong Count { get; set; }
46+
internal KeyCount KeyCount { get; }
2647
}
2748

2849
/// <summary>
29-
/// Allows a member to be marked as a vector valued field, primarily allowing one to set
50+
/// Allows a member to be marked as a <see cref="VectorType"/>, primarily allowing one to set
3051
/// the dimensionality of the resulting array.
3152
/// </summary>
3253
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
3354
public sealed class VectorTypeAttribute : Attribute
3455
{
35-
private readonly int[] _dims;
36-
3756
/// <summary>
3857
/// The length of the vectors from this vector valued field.
3958
/// </summary>
40-
public int[] Dims { get { return _dims; } }
59+
internal int[] Dims { get; }
4160

42-
public VectorTypeAttribute(params int[] dims)
61+
/// <summary>
62+
/// Mark member as single-dimensional array with unknown size.
63+
/// </summary>
64+
public VectorTypeAttribute()
4365
{
44-
_dims = dims;
45-
}
46-
}
4766

48-
/// <summary>
49-
/// Describes column information such as name and the source columns indicies that this
50-
/// column encapsulates.
51-
/// </summary>
52-
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
53-
public sealed class ColumnAttribute : Attribute
54-
{
55-
public ColumnAttribute(string ordinal, string name = null)
67+
}
68+
/// <summary>
69+
/// Mark member as single-dimensional array with specified size.
70+
/// </summary>
71+
/// <param name="size">Expected size of array. A zero value indicates that the vector type is considered to have unknown length.</param>
72+
public VectorTypeAttribute(int size)
5673
{
57-
Name = name;
74+
Contracts.CheckParam(size >= 0, nameof(size), "Should be non-negative number");
75+
Dims = new int[1] { size };
5876
}
5977

6078
/// <summary>
61-
/// Column name.
79+
/// Mark member with expected dimensions of array.
6280
/// </summary>
63-
public string Name { get; }
81+
/// <param name="dimensions">Dimensions of array. All values should be non-negative.
82+
/// A zero value indicates that the vector type is considered to have unknown length along that dimension.</param>
83+
public VectorTypeAttribute(params int[] dimensions)
84+
{
85+
foreach (var size in dimensions)
86+
{
87+
Contracts.CheckParam(size >= 0, nameof(dimensions), "Should contain only non-negative values");
88+
}
89+
Dims = dimensions;
90+
}
6491
}
6592

6693
/// <summary>
67-
/// Allows a member to specify its column name directly, as opposed to the default
94+
/// Allows a member to specify <see cref="IDataView"/> column name directly, as opposed to the default
6895
/// behavior of using the member name as the column name.
6996
/// </summary>
7097
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
7198
public sealed class ColumnNameAttribute : Attribute
7299
{
73-
private readonly string _name;
74100
/// <summary>
75101
/// Column name.
76102
/// </summary>
77-
public string Name { get { return _name; } }
103+
internal string Name { get; }
78104

79105
/// <summary>
80-
/// Allows one to specify a name to expose this column as, as opposed to simply
81-
/// the field name.
106+
/// Allows one to specify a name to expose this column as, as opposed to the default
107+
/// behavior of using the member name as the column name.
82108
/// </summary>
83109
public ColumnNameAttribute(string name)
84110
{
85-
_name = name;
111+
Name = name;
86112
}
87113
}
88114

89115
/// <summary>
90-
/// Mark this member as not being exposed as a column in the schema.
116+
/// Mark this member as not being exposed as a <see cref="IDataView"/> column in the <see cref="DataViewSchema"/>.
91117
/// </summary>
92118
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
93119
public sealed class NoColumnAttribute : Attribute
@@ -362,9 +388,8 @@ public static SchemaDefinition Create(Type userType, Direction direction = Direc
362388
if (memberInfo.GetCustomAttribute<NoColumnAttribute>() != null)
363389
continue;
364390

365-
var mappingAttr = memberInfo.GetCustomAttribute<ColumnAttribute>();
366391
var mappingNameAttr = memberInfo.GetCustomAttribute<ColumnNameAttribute>();
367-
string name = mappingAttr?.Name ?? mappingNameAttr?.Name ?? memberInfo.Name;
392+
string name = mappingNameAttr?.Name ?? memberInfo.Name;
368393
// Disallow duplicate names, because the field enumeration order is not actually
369394
// well defined, so we are not gauranteed to have consistent "hiding" from run to
370395
// run, across different .NET versions.
@@ -379,7 +404,10 @@ public static SchemaDefinition Create(Type userType, Direction direction = Direc
379404
{
380405
if (!KeyType.IsValidDataType(dataType))
381406
throw Contracts.ExceptParam(nameof(userType), "Member {0} marked with KeyType attribute, but does not appear to be a valid kind of data for a key type", memberInfo.Name);
382-
itemType = new KeyType(dataType, keyAttr.Count);
407+
if (keyAttr.KeyCount == null)
408+
itemType = new KeyType(dataType, dataType.ToMaxInt());
409+
else
410+
itemType = new KeyType(dataType, keyAttr.KeyCount.Count.GetValueOrDefault());
383411
}
384412
else
385413
itemType = ColumnTypeExtensions.PrimitiveTypeFromType(dataType);
@@ -388,7 +416,7 @@ public static SchemaDefinition Create(Type userType, Direction direction = Direc
388416
DataViewType columnType;
389417
var vectorAttr = memberInfo.GetCustomAttribute<VectorTypeAttribute>();
390418
if (vectorAttr != null && !isVector)
391-
throw Contracts.ExceptParam(nameof(userType), "Member {0} marked with VectorType attribute, but does not appear to be a vector type", memberInfo.Name);
419+
throw Contracts.ExceptParam(nameof(userType), $"Member {memberInfo.Name} marked with {nameof(VectorTypeAttribute)}, but does not appear to be a vector type", memberInfo.Name);
392420
if (isVector)
393421
{
394422
int[] dims = vectorAttr?.Dims;

src/Microsoft.ML.Data/DataLoadSave/Text/LoadColumnAttribute.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,42 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using Microsoft.Data.DataView;
78

89
namespace Microsoft.ML.Data
910
{
1011
/// <summary>
11-
/// Describes column information such as name and the source columns indices that this
12-
/// column encapsulates.
12+
/// Allow member to specify mapping to field(s) in text file.
13+
/// To override name of <see cref="IDataView"/> column use <see cref="ColumnNameAttribute"/>.
1314
/// </summary>
1415
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
1516
public sealed class LoadColumnAttribute : Attribute
1617
{
1718
/// <summary>
18-
/// Initializes new instance of <see cref="LoadColumnAttribute"/>.
19+
/// Maps member to specific field in text file.
1920
/// </summary>
20-
/// <param name="columnIndex">The index of the column in the text file.</param>
21-
public LoadColumnAttribute(int columnIndex)
21+
/// <param name="fieldIndex">The index of the field in the text file.</param>
22+
public LoadColumnAttribute(int fieldIndex)
2223
{
2324
Sources = new List<TextLoader.Range>();
24-
Sources.Add(new TextLoader.Range(columnIndex));
25+
Sources.Add(new TextLoader.Range(fieldIndex));
2526
}
2627

2728
/// <summary>
28-
/// Initializes new instance of <see cref="LoadColumnAttribute"/>.
29+
/// Maps member to range of fields in text file.
2930
/// </summary>
30-
/// <param name="start">The starting column index, for the range.</param>
31-
/// <param name="end">The ending column index, for the range.</param>
31+
/// <param name="start">The starting field index, for the range.</param>
32+
/// <param name="end">The ending field index, for the range.</param>
3233
public LoadColumnAttribute(int start, int end)
3334
{
3435
Sources = new List<TextLoader.Range>();
3536
Sources.Add(new TextLoader.Range(start, end));
3637
}
3738

3839
/// <summary>
39-
/// Initializes new instance of <see cref="LoadColumnAttribute"/>.
40+
/// Maps member to set of fields in text file.
4041
/// </summary>
41-
/// <param name="columnIndexes">Distinct text file column indices to load as part of this column.</param>
42+
/// <param name="columnIndexes">Distinct text file field indices to load as part of this column.</param>
4243
public LoadColumnAttribute(int[] columnIndexes)
4344
{
4445
Sources = new List<TextLoader.Range>();

src/Microsoft.ML.Data/Depricated/TGUIAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.ML.Internal.Internallearn
1010
// REVIEW: Get rid of this. Everything should be in the ArgumentAttribute (or a class
1111
// derived from ArgumentAttribute).
1212
[AttributeUsage(AttributeTargets.Field)]
13-
public class TGUIAttribute : Attribute
13+
internal sealed class TGUIAttribute : Attribute
1414
#pragma warning restore MSML_GeneralName
1515
{
1616
// Display parameters

src/Microsoft.ML.SamplesUtils/SamplesDatasetUtils.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,19 +643,19 @@ public static List<MulticlassClassificationExample> GenerateRandomMulticlassClas
643643
// and MatrixRowIndex=0 in MatrixElement below specifies the value at the upper-left corner in the training matrix). If user's row index
644644
// starts with 1, their row index 1 would be mapped to the 2nd row in matrix factorization module and their first row may contain no values.
645645
// This behavior is also true to column index.
646-
private const int _synthesizedMatrixFirstColumnIndex = 1;
647-
private const int _synthesizedMatrixFirstRowIndex = 1;
648-
private const int _synthesizedMatrixColumnCount = 60;
649-
private const int _synthesizedMatrixRowCount = 100;
646+
private const uint _synthesizedMatrixFirstColumnIndex = 1;
647+
private const uint _synthesizedMatrixFirstRowIndex = 1;
648+
private const uint _synthesizedMatrixColumnCount = 60;
649+
private const uint _synthesizedMatrixRowCount = 100;
650650

651651
// A data structure used to encode a single value in matrix
652652
public class MatrixElement
653653
{
654654
// Matrix column index is at most _synthesizedMatrixColumnCount + _synthesizedMatrixFirstColumnIndex.
655-
[KeyType(Count = _synthesizedMatrixColumnCount + _synthesizedMatrixFirstColumnIndex)]
655+
[KeyType(_synthesizedMatrixColumnCount + _synthesizedMatrixFirstColumnIndex)]
656656
public uint MatrixColumnIndex;
657657
// Matrix row index is at most _synthesizedMatrixRowCount + _synthesizedMatrixFirstRowIndex.
658-
[KeyType(Count = _synthesizedMatrixRowCount + _synthesizedMatrixFirstRowIndex)]
658+
[KeyType(_synthesizedMatrixRowCount + _synthesizedMatrixFirstRowIndex)]
659659
public uint MatrixRowIndex;
660660
// The value at the column MatrixColumnIndex and row MatrixRowIndex.
661661
public float Value;
@@ -665,9 +665,9 @@ public class MatrixElement
665665
// renamed to Score because Score is the default name of matrix factorization's output.
666666
public class MatrixElementForScore
667667
{
668-
[KeyType(Count = _synthesizedMatrixColumnCount + _synthesizedMatrixFirstColumnIndex)]
668+
[KeyType(_synthesizedMatrixColumnCount + _synthesizedMatrixFirstColumnIndex)]
669669
public uint MatrixColumnIndex;
670-
[KeyType(Count = _synthesizedMatrixRowCount + _synthesizedMatrixFirstRowIndex)]
670+
[KeyType(_synthesizedMatrixRowCount + _synthesizedMatrixFirstRowIndex)]
671671
public uint MatrixRowIndex;
672672
public float Score;
673673
}

test/Microsoft.ML.Benchmarks/Helpers/CIBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.ML.Benchmarks.Harness
1111
/// which we want to run on the CI.
1212
/// </summary>
1313
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
14-
public class CIBenchmark : Attribute
14+
public sealed class CIBenchmark : Attribute
1515
{
1616
}
1717
}

test/Microsoft.ML.Benchmarks/PredictionEngineBench.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ public class SentimentPrediction
168168

169169
public class BreastCancerData
170170
{
171-
[ColumnName("Label"), Column("0")]
171+
[ColumnName("Label")]
172172
public bool Label;
173173

174-
[ColumnName("Features"), Column("1-9"), VectorType(9)]
174+
[ColumnName("Features"), VectorType(9)]
175175
public float[] Features;
176176
}
177177

test/Microsoft.ML.FSharp.Tests/SmokeTests.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ open Xunit
6161
module SmokeTest1 =
6262

6363
type SentimentData() =
64-
[<LoadColumn(columnIndex = 0); ColumnName("Label"); DefaultValue>]
64+
[<LoadColumn(fieldIndex = 0); ColumnName("Label"); DefaultValue>]
6565
val mutable Sentiment : bool
66-
[<LoadColumn(columnIndex = 1); DefaultValue>]
66+
[<LoadColumn(fieldIndex =1); DefaultValue>]
6767
val mutable SentimentText : string
6868

6969
type SentimentPrediction() =
@@ -99,10 +99,10 @@ module SmokeTest2 =
9999

100100
[<CLIMutable>]
101101
type SentimentData =
102-
{ [<LoadColumn(columnIndex = 0); ColumnName("Label")>]
102+
{ [<LoadColumn(fieldIndex = 0); ColumnName("Label")>]
103103
Sentiment : bool
104104

105-
[<LoadColumn(columnIndex = 1)>]
105+
[<LoadColumn(fieldIndex = 1)>]
106106
SentimentText : string }
107107

108108
[<CLIMutable>]
@@ -137,10 +137,10 @@ module SmokeTest2 =
137137
module SmokeTest3 =
138138

139139
type SentimentData() =
140-
[<LoadColumn(columnIndex = 0); ColumnName("Label")>]
140+
[<LoadColumn(fieldIndex = 0); ColumnName("Label")>]
141141
member val Sentiment = false with get, set
142142

143-
[<LoadColumn(columnIndex = 1)>]
143+
[<LoadColumn(fieldIndex = 1)>]
144144
member val SentimentText = "".AsMemory() with get, set
145145

146146
type SentimentPrediction() =

test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ private int GetMaxIndexForOnePrediction(MNISTPrediction onePrediction)
824824

825825
public class MNISTData
826826
{
827-
[Column("0")]
828827
public long Label;
829828

830829
[VectorType(784)]

0 commit comments

Comments
 (0)