Skip to content

Commit 88b0ec0

Browse files
committed
Say good-bye to transpose ISchema
1 parent 95f1530 commit 88b0ec0

File tree

11 files changed

+76
-164
lines changed

11 files changed

+76
-164
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ internal static void RunOnData(TextWriter writer, Arguments args, IDataView data
9191
foreach (var view in viewChainReversed.Reverse())
9292
{
9393
writer.WriteLine("---- {0} ----", view.GetType().Name);
94-
PrintSchema(writer, args, view.Schema, (view as ITransposeDataView)?.TransposeSchema);
94+
PrintSchema(writer, args, view.Schema, (view as ITransposeDataView)?.TransposeSlotTypeHolder);
9595
}
9696
}
9797
else
98-
PrintSchema(writer, args, data.Schema, (data as ITransposeDataView)?.TransposeSchema);
98+
PrintSchema(writer, args, data.Schema, (data as ITransposeDataView)?.TransposeSlotTypeHolder);
9999
}
100100

101101
/// <summary>
@@ -113,12 +113,12 @@ private static IEnumerable<IDataView> GetViewChainReversed(IDataView data)
113113
}
114114
}
115115

116-
private static void PrintSchema(TextWriter writer, Arguments args, Schema schema, ITransposeSchema tschema)
116+
private static void PrintSchema(TextWriter writer, Arguments args, Schema schema, ITransposeSlotTypeHolder transposeSlotTypeHolder)
117117
{
118118
Contracts.AssertValue(writer);
119119
Contracts.AssertValue(args);
120120
Contracts.AssertValue(schema);
121-
Contracts.AssertValueOrNull(tschema);
121+
Contracts.AssertValueOrNull(transposeSlotTypeHolder);
122122
#if !CORECLR
123123
if (args.ShowJson)
124124
{
@@ -137,7 +137,7 @@ private static void PrintSchema(TextWriter writer, Arguments args, Schema schema
137137
{
138138
var name = schema[col].Name;
139139
var type = schema[col].Type;
140-
var slotType = tschema == null ? null : tschema.GetSlotType(col);
140+
var slotType = transposeSlotTypeHolder == null ? null : transposeSlotTypeHolder.GetSlotType(col);
141141
itw.WriteLine("{0}: {1}{2}", name, type, slotType == null ? "" : " (T)");
142142

143143
bool metaVals = args.ShowMetadataValues;

src/Microsoft.ML.Data/Data/ITransposeDataView.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ namespace Microsoft.ML.Data
2020
/// <see cref="IDataView"/>.
2121
///
2222
/// The interface only advertises that columns may be accessible in slot-wise fashion. A column
23-
/// is accessible in this fashion iff <see cref="TransposeSchema"/>'s
24-
/// <see cref="ITransposeSchema.GetSlotType"/> returns a non-null value.
23+
/// is accessible in this fashion iff <see cref="TransposeSlotTypeHolder"/>'s
24+
/// <see cref="ITransposeSlotTypeHolder.GetSlotType"/> returns a non-null value.
2525
/// </summary>
2626
[BestFriend]
2727
internal interface ITransposeDataView : IDataView
2828
{
2929
/// <summary>
3030
/// An enhanced schema, containing information on the transposition properties, if any,
31-
/// of each column. Note that there is no contract or suggestion that this property
32-
/// should be equal to <see cref="IDataView.Schema"/>.
31+
/// of each column.
3332
/// </summary>
34-
ITransposeSchema TransposeSchema { get; }
33+
ITransposeSlotTypeHolder TransposeSlotTypeHolder { get; }
3534

3635
/// <summary>
3736
/// Presents a cursor over the slots of a transposable column, or throws if the column
@@ -44,15 +43,15 @@ internal interface ITransposeDataView : IDataView
4443
/// The transpose schema returns the schema information of the view we have transposed.
4544
/// </summary>
4645
[BestFriend]
47-
internal interface ITransposeSchema : ISchema
46+
internal interface ITransposeSlotTypeHolder
4847
{
4948
/// <summary>
50-
/// Analogous to <see cref="ISchema.GetColumnType"/>, except instead of returning the type of value
51-
/// accessible through the <see cref="RowCursor"/>, returns the item type of value accessible
52-
/// through the <see cref="SlotCursor"/>. This will return <c>null</c> iff this particular
53-
/// column is not transposable, that is, it cannot be viewed in a slotwise fashion. Observe from
54-
/// the return type that this will always be a vector type. This vector type should be of fixed
55-
/// size and one dimension.
49+
/// Analogous to <see cref="ColumnType"/> in <see cref="Schema"/>, except instead of returning
50+
/// the type of value accessible through the <see cref="RowCursor"/>, returns the item type of
51+
/// value accessible through the <see cref="SlotCursor"/>. This will return <c>null</c> iff
52+
/// this particular column is not transposable, that is, it cannot be viewed in a slotwise fashion.
53+
/// Observe from the return type that this will always be a vector type. This vector type should
54+
/// be of fixed size and one dimension.
5655
/// </summary>
5756
VectorType GetSlotType(int col);
5857
}

src/Microsoft.ML.Data/Data/SlotCursor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ private protected SlotCursor(IChannelProvider provider)
4545

4646
/// <summary>
4747
/// The slot type for this cursor. Note that this should equal the
48-
/// <see cref="ITransposeSchema.GetSlotType"/> for the column from which this slot cursor
48+
/// <see cref="ITransposeSlotTypeHolder.GetSlotType"/> for the column from which this slot cursor
4949
/// was created.
5050
/// </summary>
5151
public abstract VectorType GetSlotType();
5252

5353
/// <summary>
5454
/// A getter delegate for the slot values. The type <typeparamref name="TValue"/> must correspond
55-
/// to the item type from <see cref="ITransposeSchema.GetSlotType"/>.
55+
/// to the item type from <see cref="ITransposeSlotTypeHolder.GetSlotType"/>.
5656
/// </summary>
5757
public abstract ValueGetter<VBuffer<TValue>> GetGetter<TValue>();
5858

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ private CompositeDataLoader(IHost host, TransformEx[] transforms)
408408

409409
View = transforms[transforms.Length - 1].Transform;
410410
_tview = View as ITransposeDataView;
411-
_transposeSchema = _tview?.TransposeSchema ?? new TransposerUtils.SimpleTransposeSchema(View.Schema);
411+
_transposeSlotTypeHolder = _tview?.TransposeSlotTypeHolder ?? new TransposerUtils.SimpleTransposeSchema(View.Schema);
412412

413413
var srcLoader = transforms[0].Transform.Source as IDataLoader;
414414

@@ -565,8 +565,8 @@ private static string GenerateTag(int index)
565565

566566
public Schema Schema => View.Schema;
567567

568-
private readonly ITransposeSchema _transposeSchema;
569-
ITransposeSchema ITransposeDataView.TransposeSchema => _transposeSchema;
568+
private readonly ITransposeSlotTypeHolder _transposeSlotTypeHolder;
569+
ITransposeSlotTypeHolder ITransposeDataView.TransposeSlotTypeHolder => _transposeSlotTypeHolder;
570570

571571
public RowCursor GetRowCursor(Func<int, bool> predicate, Random rand = null)
572572
{
@@ -585,7 +585,7 @@ public RowCursor[] GetRowCursorSet(Func<int, bool> predicate, int n, Random rand
585585
public SlotCursor GetSlotCursor(int col)
586586
{
587587
_host.CheckParam(0 <= col && col < Schema.Count, nameof(col));
588-
if (_transposeSchema?.GetSlotType(col) == null)
588+
if (_transposeSlotTypeHolder?.GetSlotType(col) == null)
589589
{
590590
throw _host.ExceptParam(nameof(col), "Bad call to GetSlotCursor on untransposable column '{0}'",
591591
Schema[col].Name);

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

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ protected override void VerifyView(IDataView view)
320320
private readonly IMultiStreamSource _file;
321321
private readonly IHost _host;
322322
private readonly Header _header;
323-
private readonly SchemaImpl _schema;
323+
private readonly TransposeSlotTypeHolder _transposeSlotTypeHolder;
324324

325325
// This is a sub-IDV holding the schema, and optionally the data stored in row-wise format.
326326
private readonly SubIdvEntry.SchemaSubIdv _schemaEntry;
@@ -364,9 +364,9 @@ private static VersionInfo GetVersionInfo()
364364
// inspect the schema. We also want to ensure that the useful property that
365365
// a cursor and view's schemas are the same, is preserved, which allows us
366366
// to use the cursors from the schema view if convenient to do so.
367-
public Schema Schema { get { return _schemaEntry.GetView().Schema; } }
367+
public Schema Schema => _schemaEntry.GetView().Schema;
368368

369-
ITransposeSchema ITransposeDataView.TransposeSchema { get { return _schema; } }
369+
ITransposeSlotTypeHolder ITransposeDataView.TransposeSlotTypeHolder => _transposeSlotTypeHolder;
370370

371371
/// <summary>
372372
/// Whether the master schema sub-IDV has the actual data.
@@ -411,7 +411,7 @@ public TransposeLoader(IHostEnvironment env, Arguments args, IMultiStreamSource
411411
_entries = new SubIdvEntry.TransposedSubIdv[_header.ColumnCount];
412412
for (int c = 0; c < _entries.Length; ++c)
413413
_entries[c] = new SubIdvEntry.TransposedSubIdv(this, reader, c);
414-
_schema = new SchemaImpl(this);
414+
_transposeSlotTypeHolder = new TransposeSlotTypeHolder(this);
415415
if (!HasRowData)
416416
{
417417
_colTransposers = new Transposer[_header.ColumnCount];
@@ -445,7 +445,7 @@ private TransposeLoader(IHost host, ModelLoadContext ctx, IMultiStreamSource fil
445445
_entries = new SubIdvEntry.TransposedSubIdv[_header.ColumnCount];
446446
for (int c = 0; c < _entries.Length; ++c)
447447
_entries[c] = new SubIdvEntry.TransposedSubIdv(this, reader, c);
448-
_schema = new SchemaImpl(this);
448+
_transposeSlotTypeHolder = new TransposeSlotTypeHolder(this);
449449
if (!HasRowData)
450450
{
451451
_colTransposers = new Transposer[_header.ColumnCount];
@@ -608,50 +608,20 @@ private unsafe Header InitHeader(BinaryReader reader)
608608
return header;
609609
}
610610

611-
private sealed class SchemaImpl : ITransposeSchema
611+
private sealed class TransposeSlotTypeHolder : ITransposeSlotTypeHolder
612612
{
613613
private readonly TransposeLoader _parent;
614614
private Schema Schema { get { return _parent.Schema; } }
615615
private IHost Host { get { return _parent._host; } }
616616
public int ColumnCount { get { return Schema.Count; } }
617617

618-
public SchemaImpl(TransposeLoader parent)
618+
public TransposeSlotTypeHolder(TransposeLoader parent)
619619
{
620620
Contracts.AssertValue(parent);
621621
_parent = parent;
622622
var view = parent._schemaEntry.GetView().Schema;
623623
}
624624

625-
public string GetColumnName(int col)
626-
{
627-
return Schema[col].Name;
628-
}
629-
630-
public bool TryGetColumnIndex(string name, out int col)
631-
{
632-
return Schema.TryGetColumnIndex(name, out col);
633-
}
634-
635-
public ColumnType GetColumnType(int col)
636-
{
637-
return Schema[col].Type;
638-
}
639-
640-
public ColumnType GetMetadataTypeOrNull(string kind, int col)
641-
{
642-
return Schema[col].Metadata.Schema.GetColumnOrNull(kind)?.Type;
643-
}
644-
645-
public IEnumerable<KeyValuePair<string, ColumnType>> GetMetadataTypes(int col)
646-
{
647-
return Schema[col].Metadata.Schema.Select(c => new KeyValuePair<string, ColumnType>(c.Name, c.Type));
648-
}
649-
650-
public void GetMetadata<TValue>(string kind, int col, ref TValue value)
651-
{
652-
Schema[col].Metadata.GetValue(kind, ref value);
653-
}
654-
655625
public VectorType GetSlotType(int col)
656626
{
657627
Host.CheckParam(0 <= col && col < ColumnCount, nameof(col));
@@ -696,7 +666,7 @@ public SlotCursor GetSlotCursor(int col)
696666
_host.CheckParam(0 <= col && col < _header.ColumnCount, nameof(col));
697667
// We don't want the type error, if there is one, to be handled by the get-getter, because
698668
// at the point we've gotten the interior cursor, but not yet constructed the slot cursor.
699-
ColumnType cursorType = _schema.GetSlotType(col).ItemType;
669+
ColumnType cursorType = _transposeSlotTypeHolder.GetSlotType(col).ItemType;
700670
RowCursor inputCursor = view.GetRowCursor(c => true);
701671
try
702672
{
@@ -781,8 +751,8 @@ private Transposer EnsureAndGetTransposer(int col)
781751
_host.AssertValue(view);
782752
_host.Assert(view.Schema.Count == 1);
783753
var trans = _colTransposers[col] = Transposer.Create(_host, view, false, new int[] { 0 });
784-
_host.Assert(((ITransposeDataView)trans).TransposeSchema.ColumnCount == 1);
785-
_host.Assert(((ITransposeDataView)trans).TransposeSchema.GetSlotType(0).ValueCount == Schema[col].Type.ValueCount);
754+
_host.Assert(trans.Schema.Count == 1);
755+
_host.Assert(trans.TransposeSlotTypeHolder.GetSlotType(0).ValueCount == Schema[col].Type.ValueCount);
786756
}
787757
}
788758
}
@@ -843,7 +813,7 @@ private void Init(int col)
843813
Ch.Assert(0 <= col && col < Schema.Count);
844814
Ch.Assert(_colToActivesIndex[col] >= 0);
845815
var type = Schema[col].Type;
846-
Ch.Assert(((ITransposeDataView)_parent).TransposeSchema.GetSlotType(col).ValueCount == _parent._header.RowCount);
816+
Ch.Assert(((ITransposeDataView)_parent).TransposeSlotTypeHolder.GetSlotType(col).ValueCount == _parent._header.RowCount);
847817
Action<int> func = InitOne<int>;
848818
if (type.IsVector)
849819
func = InitVec<int>;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void SaveTransposedData(IChannel ch, Stream stream, ITransposeDataView d
109109
header.Signature = TransposeLoader.Header.SignatureValue;
110110
header.Version = TransposeLoader.Header.WriterVersion;
111111
header.CompatibleVersion = TransposeLoader.Header.WriterVersion;
112-
VectorType slotType = data.TransposeSchema.GetSlotType(cols[0]);
112+
VectorType slotType = data.TransposeSlotTypeHolder.GetSlotType(cols[0]);
113113
ch.AssertValue(slotType);
114114
header.RowCount = slotType.ValueCount;
115115
header.ColumnCount = cols.Length;

0 commit comments

Comments
 (0)