Skip to content

Commit c8776d8

Browse files
authored
Remove As methods on ColumnType. (#1864)
* Remove ColumnType.AsVector. * Remove ColumnType.AsPrimitive. * Remove ColumnType.AsKey.
1 parent abcb2e7 commit c8776d8

Some content is hidden

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

41 files changed

+119
-131
lines changed

src/Microsoft.ML.Core/Data/ColumnType.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ private protected ColumnType(Type rawType, DataKind rawKind)
8080
[BestFriend]
8181
internal bool IsPrimitive { get; }
8282

83-
/// <summary>
84-
/// Equivalent to <c>as <see cref="PrimitiveType"/></c>.
85-
/// </summary>
86-
[BestFriend]
87-
internal PrimitiveType AsPrimitive => IsPrimitive ? (PrimitiveType)this : null;
88-
8983
/// <summary>
9084
/// Whether this type is a standard numeric type. External code should use <c>is <see cref="NumberType"/></c>.
9185
/// </summary>
@@ -140,12 +134,6 @@ internal bool IsBool
140134
[BestFriend]
141135
internal bool IsKey { get; }
142136

143-
/// <summary>
144-
/// Equivalent to <c>as <see cref="KeyType"/></c>.
145-
/// </summary>
146-
[BestFriend]
147-
internal KeyType AsKey => IsKey ? (KeyType)this : null;
148-
149137
/// <summary>
150138
/// Zero return means either it's not a key type or the cardinality is unknown. External code should first
151139
/// test whether this is of type <see cref="KeyType"/>, then if so get the <see cref="KeyType.Count"/> property
@@ -166,12 +154,6 @@ internal bool IsBool
166154
[BestFriend]
167155
internal bool IsVector { get; }
168156

169-
/// <summary>
170-
/// Equivalent to <c>as <see cref="VectorType"/></c>.
171-
/// </summary>
172-
[BestFriend]
173-
internal VectorType AsVector => IsVector ? (VectorType)this : null;
174-
175157
/// <summary>
176158
/// For non-vector types, this returns the column type itself (i.e., return <c>this</c>).
177159
/// </summary>

src/Microsoft.ML.Core/Data/MetadataUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ public static VectorType GetCategoricalType(int rangeCount)
165165
return new VectorType(NumberType.I4, rangeCount, 2);
166166
}
167167

168-
private static volatile ColumnType _scoreColumnSetIdType;
168+
private static volatile KeyType _scoreColumnSetIdType;
169169

170170
/// <summary>
171171
/// The type of the ScoreColumnSetId metadata.
172172
/// </summary>
173-
public static ColumnType ScoreColumnSetIdType
173+
public static KeyType ScoreColumnSetIdType
174174
{
175175
get
176176
{

src/Microsoft.ML.Data/Commands/ScoreCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private void RunCore(IChannel ch)
210210
private bool ShouldAddColumn(Schema schema, int i, uint scoreSet, bool outputNamesAndLabels)
211211
{
212212
uint scoreSetId = 0;
213-
if (schema.TryGetMetadata(MetadataUtils.ScoreColumnSetIdType.AsPrimitive, MetadataUtils.Kinds.ScoreColumnSetId, i, ref scoreSetId)
213+
if (schema.TryGetMetadata(MetadataUtils.ScoreColumnSetIdType, MetadataUtils.Kinds.ScoreColumnSetId, i, ref scoreSetId)
214214
&& scoreSetId == scoreSet)
215215
{
216216
return true;

src/Microsoft.ML.Data/Data/Conversion.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,12 @@ public bool TryGetStandardConversion(ColumnType typeSrc, ColumnType typeDst,
412412

413413
conv = null;
414414
identity = false;
415-
if (typeSrc.IsKey)
415+
if (typeSrc is KeyType keySrc)
416416
{
417-
var keySrc = typeSrc.AsKey;
418-
419417
// Key types are only convertable to compatible key types or unsigned integer
420418
// types that are large enough.
421-
if (typeDst.IsKey)
419+
if (typeDst is KeyType keyDst)
422420
{
423-
var keyDst = typeDst.AsKey;
424421
// We allow the Min value to shift. We currently don't allow the counts to vary.
425422
// REVIEW: Should we allow the counts to vary? Allowing the dst to be bigger is trivial.
426423
// Smaller dst means mapping values to NA.
@@ -451,11 +448,11 @@ public bool TryGetStandardConversion(ColumnType typeSrc, ColumnType typeDst,
451448
// REVIEW: Should we look for illegal values and force them to zero? If so, then
452449
// we'll need to set identity to false.
453450
}
454-
else if (typeDst.IsKey)
451+
else if (typeDst is KeyType keyDst)
455452
{
456453
if (!typeSrc.IsText)
457454
return false;
458-
conv = GetKeyParse(typeDst.AsKey);
455+
conv = GetKeyParse(keyDst);
459456
return true;
460457
}
461458
else if (!typeDst.IsStandardScalar)
@@ -490,10 +487,10 @@ public bool TryGetStringConversion<TSrc>(ColumnType type, out ValueMapper<TSrc,
490487
Contracts.CheckValue(type, nameof(type));
491488
Contracts.Check(type.RawType == typeof(TSrc), "Wrong TSrc type argument");
492489

493-
if (type.IsKey)
490+
if (type is KeyType keyType)
494491
{
495492
// Key string conversion always works.
496-
conv = GetKeyStringConversion<TSrc>(type.AsKey);
493+
conv = GetKeyStringConversion<TSrc>(keyType);
497494
return true;
498495
}
499496
return TryGetStringConversion(out conv);
@@ -572,8 +569,8 @@ public TryParseMapper<TDst> GetTryParseConversion<TDst>(ColumnType typeDst)
572569
"Parse conversion only supported for standard types");
573570
Contracts.Check(typeDst.RawType == typeof(TDst), "Wrong TDst type parameter");
574571

575-
if (typeDst.IsKey)
576-
return GetKeyTryParse<TDst>(typeDst.AsKey);
572+
if (typeDst is KeyType keyType)
573+
return GetKeyTryParse<TDst>(keyType);
577574

578575
Contracts.Assert(_tryParseDelegates.ContainsKey(typeDst.RawKind));
579576
return (TryParseMapper<TDst>)_tryParseDelegates[typeDst.RawKind];

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ private static ColumnType MakeColumnType(SchemaShape.Column inputCol)
5555
{
5656
ColumnType curType = inputCol.ItemType;
5757
if (inputCol.IsKey)
58-
curType = new KeyType(curType.AsPrimitive.RawKind, 0, AllKeySizes);
58+
curType = new KeyType(((PrimitiveType)curType).RawKind, 0, AllKeySizes);
5959
if (inputCol.Kind == SchemaShape.Column.VectorKind.VariableVector)
60-
curType = new VectorType(curType.AsPrimitive, 0);
60+
curType = new VectorType((PrimitiveType)curType, 0);
6161
else if (inputCol.Kind == SchemaShape.Column.VectorKind.Vector)
62-
curType = new VectorType(curType.AsPrimitive, AllVectorSizes);
62+
curType = new VectorType((PrimitiveType)curType, AllVectorSizes);
6363
return curType;
6464
}
6565

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,8 @@ public void Save(ModelSaveContext ctx)
840840
Contracts.Assert((DataKind)(byte)type.RawKind == type.RawKind);
841841
ctx.Writer.Write((byte)type.RawKind);
842842
ctx.Writer.WriteBoolByte(type.IsKey);
843-
if (type.IsKey)
843+
if (type is KeyType key)
844844
{
845-
var key = type.AsKey;
846845
ctx.Writer.WriteBoolByte(key.Contiguous);
847846
ctx.Writer.Write(key.Min);
848847
ctx.Writer.Write(key.Count);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,14 @@ public Parser(TextLoader parent)
663663
{
664664
var info = _infos[i];
665665

666-
if (info.ColType.ItemType.IsKey)
666+
if (info.ColType is KeyType keyType)
667667
{
668-
if (!info.ColType.IsVector)
669-
_creator[i] = cache.GetCreatorOne(info.ColType.AsKey);
670-
else
671-
_creator[i] = cache.GetCreatorVec(info.ColType.ItemType.AsKey);
668+
_creator[i] = cache.GetCreatorOne(keyType);
669+
continue;
670+
}
671+
else if (info.ColType is VectorType vectorType && vectorType.ItemType is KeyType vectorKeyType)
672+
{
673+
_creator[i] = cache.GetCreatorVec(vectorKeyType);
672674
continue;
673675
}
674676

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,8 @@ private TextLoader.Column GetColumn(string name, ColumnType type, int? start)
489489
{
490490
DataKind? kind;
491491
KeyRange keyRange = null;
492-
if (type.ItemType.IsKey)
492+
if (type.ItemType is KeyType key)
493493
{
494-
var key = type.ItemType.AsKey;
495494
if (!key.Contiguous)
496495
keyRange = new KeyRange(key.Min, contiguous: false);
497496
else if (key.Count == 0)

src/Microsoft.ML.Data/DataLoadSave/Transpose/TransposeLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ public VectorType GetSlotType(int col)
659659
var view = _parent._entries[col].GetViewOrNull();
660660
if (view == null)
661661
return null;
662-
return view.Schema.GetColumnType(0).AsVector;
662+
return view.Schema.GetColumnType(0) as VectorType;
663663
}
664664
}
665665

src/Microsoft.ML.Data/DataLoadSave/Transpose/TransposeSaver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public bool IsColumnSavable(ColumnType type)
7070
// an artificial vector type out of this. Obviously if you can't make a vector
7171
// out of the items, then you could not save each slot's values.
7272
var itemType = type.ItemType;
73-
var primitiveType = itemType.AsPrimitive;
73+
var primitiveType = itemType as PrimitiveType;
7474
if (primitiveType == null)
7575
return false;
7676
var vectorType = new VectorType(primitiveType, size: 2);

0 commit comments

Comments
 (0)