9
9
namespace Microsoft . ML . Runtime . Data
10
10
{
11
11
/// <summary>
12
- /// This contains information about a column in an IDataView. It is essentially a convenience
13
- /// cache containing the name, column index, and column type for the column.
12
+ /// This contains information about a column in an <see cref="IDataView"/>. It is essentially a convenience cache
13
+ /// containing the name, column index, and column type for the column. The intended usage is that users of <see cref="RoleMappedSchema"/>
14
+ /// will have a convenient method of getting the index and type without having to separately query it through the <see cref="ISchema"/>,
15
+ /// since practically the first thing a consumer of a <see cref="RoleMappedSchema"/> will want to do once they get a mappping is get
16
+ /// the type and index of the corresponding column.
14
17
/// </summary>
15
18
public sealed class ColumnInfo
16
19
{
@@ -71,12 +74,20 @@ public static ColumnInfo CreateFromIndex(ISchema schema, int index)
71
74
}
72
75
73
76
/// <summary>
74
- /// Encapsulates an ISchema plus column role mapping information. It has convenience fields for
75
- /// several common column roles, but can hold an arbitrary set of column infos. The convenience
76
- /// fields are non-null iff there is a unique column with the corresponding role. When there are
77
- /// no such columns or more than one such column, the field is null. The Has, HasUnique, and
78
- /// HasMultiple methods provide some cardinality information.
79
- /// Note that all columns assigned roles are guaranteed to be non-hidden in this schema.
77
+ /// Encapsulates an <see cref="ISchema"/> plus column role mapping information. The purpose of role mappings is to
78
+ /// provide information on what the intended usage is for. That is: while a given data view may have a column named
79
+ /// "Features", by itself that is insufficient: the trainer must be fed a role mapping that says that the role
80
+ /// mapping for features is filled by that "Features" column. This allows things like columns not named "Features"
81
+ /// to actually fill that role (as opposed to insisting on a hard coding, or having every trainer have to be
82
+ /// individually configured). Also, by being a one-to-many mapping, it is a way for learners that can consume
83
+ /// multiple features columns to consume that information.
84
+ ///
85
+ /// This class has convenience fields for several common column roles (se.g., <see cref="Feature"/>, <see
86
+ /// cref="Label"/>), but can hold an arbitrary set of column infos. The convenience fields are non-null iff there is
87
+ /// a unique column with the corresponding role. When there are no such columns or more than one such column, the
88
+ /// field is null. The <see cref="Has"/>, <see cref="HasUnique"/>, and <see cref="HasMultiple"/> methods provide
89
+ /// some cardinality information. Note that all columns assigned roles are guaranteed to be non-hidden in this
90
+ /// schema.
80
91
/// </summary>
81
92
public sealed class RoleMappedSchema
82
93
{
@@ -85,18 +96,16 @@ public sealed class RoleMappedSchema
85
96
private const string GroupString = "Group" ;
86
97
private const string WeightString = "Weight" ;
87
98
private const string NameString = "Name" ;
88
- private const string IdString = "Id" ;
89
99
private const string FeatureContributionsString = "FeatureContributions" ;
90
100
91
101
public struct ColumnRole
92
102
{
93
- public static ColumnRole Feature { get { return new ColumnRole ( FeatureString ) ; } }
94
- public static ColumnRole Label { get { return new ColumnRole ( LabelString ) ; } }
95
- public static ColumnRole Group { get { return new ColumnRole ( GroupString ) ; } }
96
- public static ColumnRole Weight { get { return new ColumnRole ( WeightString ) ; } }
97
- public static ColumnRole Name { get { return new ColumnRole ( NameString ) ; } }
98
- public static ColumnRole Id { get { return new ColumnRole ( IdString ) ; } }
99
- public static ColumnRole FeatureContributions { get { return new ColumnRole ( FeatureContributionsString ) ; } }
103
+ public static ColumnRole Feature => FeatureString ;
104
+ public static ColumnRole Label => LabelString ;
105
+ public static ColumnRole Group => GroupString ;
106
+ public static ColumnRole Weight => WeightString ;
107
+ public static ColumnRole Name => NameString ;
108
+ public static ColumnRole FeatureContributions => FeatureContributionsString ;
100
109
101
110
public readonly string Value ;
102
111
@@ -152,11 +161,6 @@ public static KeyValuePair<ColumnRole, string> CreatePair(ColumnRole role, strin
152
161
/// </summary>
153
162
public readonly ColumnInfo Name ;
154
163
155
- /// <summary>
156
- /// The Id column, when there is exactly one (null otherwise).
157
- /// </summary>
158
- public readonly ColumnInfo Id ;
159
-
160
164
// Maps from role to the associated column infos.
161
165
private readonly Dictionary < string , IReadOnlyList < ColumnInfo > > _map ;
162
166
@@ -194,9 +198,6 @@ private RoleMappedSchema(ISchema schema, Dictionary<string, IReadOnlyList<Column
194
198
case NameString :
195
199
Name = cols [ 0 ] ;
196
200
break ;
197
- case IdString :
198
- Id = cols [ 0 ] ;
199
- break ;
200
201
}
201
202
}
202
203
}
@@ -224,8 +225,8 @@ private static void Add(Dictionary<string, List<ColumnInfo>> map, ColumnRole rol
224
225
225
226
private static Dictionary < string , List < ColumnInfo > > MapFromNames ( ISchema schema , IEnumerable < KeyValuePair < ColumnRole , string > > roles )
226
227
{
227
- Contracts . AssertValue ( schema , "schema" ) ;
228
- Contracts . AssertValue ( roles , "roles" ) ;
228
+ Contracts . AssertValue ( schema ) ;
229
+ Contracts . AssertValue ( roles ) ;
229
230
230
231
var map = new Dictionary < string , List < ColumnInfo > > ( ) ;
231
232
foreach ( var kvp in roles )
@@ -241,8 +242,8 @@ private static Dictionary<string, List<ColumnInfo>> MapFromNames(ISchema schema,
241
242
242
243
private static Dictionary < string , List < ColumnInfo > > MapFromNamesOpt ( ISchema schema , IEnumerable < KeyValuePair < ColumnRole , string > > roles )
243
244
{
244
- Contracts . AssertValue ( schema , "schema" ) ;
245
- Contracts . AssertValue ( roles , "roles" ) ;
245
+ Contracts . AssertValue ( schema ) ;
246
+ Contracts . AssertValue ( roles ) ;
246
247
247
248
var map = new Dictionary < string , List < ColumnInfo > > ( ) ;
248
249
foreach ( var kvp in roles )
@@ -334,6 +335,13 @@ public IEnumerable<KeyValuePair<ColumnRole, string>> GetColumnRoleNames(ColumnRo
334
335
}
335
336
}
336
337
338
+ /// <summary>
339
+ /// Returns the <see cref="ColumnInfo"/> corresponding to <paramref name="role"/> if there is
340
+ /// exactly one such mapping, and otherwise throws an exception.
341
+ /// </summary>
342
+ /// <param name="role">The role to look up</param>
343
+ /// <returns>The info corresponding to that role, assuming there was only one column
344
+ /// mapped to that</returns>
337
345
public ColumnInfo GetUniqueColumn ( ColumnRole role )
338
346
{
339
347
var infos = GetColumns ( role ) ;
@@ -398,9 +406,9 @@ public static RoleMappedSchema CreateOpt(ISchema schema, IEnumerable<KeyValuePai
398
406
}
399
407
400
408
/// <summary>
401
- /// Encapsulates an IDataView plus a corresponding RoleMappedSchema. Note that the schema of the
402
- /// RoleMappedSchema is guaranteed to be the same schema of the IDataView, that is,
403
- /// Data.Schema == Schema .Schema.
409
+ /// Encapsulates an <see cref=" IDataView"/> plus a corresponding <see cref=" RoleMappedSchema"/>.
410
+ /// Note that the schema of <see cref="RoleMappedSchema.Schema"/> of <see cref="Schema"/> is
411
+ /// guaranteed to equal the the <see cref="ISchematized .Schema"/> of <see cref="Data"/> .
404
412
/// </summary>
405
413
public sealed class RoleMappedData
406
414
{
0 commit comments