public static Bindings Create(OneToOneTransformBase parent, ModelLoadContext ctx, ISchema input, ITransposeSchema transInput, Func <ColumnType, string> testType) { Contracts.AssertValue(parent); var host = parent.Host; host.CheckValue(ctx, nameof(ctx)); host.AssertValue(input); host.AssertValueOrNull(transInput); host.AssertValueOrNull(testType); // *** Binary format *** // int: number of added columns // for each added column // int: id of output column name // int: id of input column name int cinfo = ctx.Reader.ReadInt32(); host.CheckDecode(cinfo > 0); var names = new string[cinfo]; var infos = new ColInfo[cinfo]; for (int i = 0; i < cinfo; i++) { string dst = ctx.LoadNonEmptyString(); names[i] = dst; // Note that in old files, the source name may be null indicating that // the source column has the same name as the added column. string tmp = ctx.LoadStringOrNull(); string src = tmp ?? dst; host.CheckDecode(!string.IsNullOrEmpty(src)); int colSrc; if (!input.TryGetColumnIndex(src, out colSrc)) { throw host.Except("Source column '{0}' is required but not found", src); } var type = input.GetColumnType(colSrc); if (testType != null) { string reason = testType(type); if (reason != null) { throw host.Except(InvalidTypeErrorFormat, src, type, reason); } } var slotType = transInput == null ? null : transInput.GetSlotType(colSrc); infos[i] = new ColInfo(dst, colSrc, type, slotType); } return(new Bindings(parent, infos, input, false, names)); }
public SlotCursor GetSlotCursor(int col) { _host.CheckParam(0 <= col && col < Schema.Count, nameof(col)); if (_transposeSchema?.GetSlotType(col) == null) { throw _host.ExceptParam(nameof(col), "Bad call to GetSlotCursor on untransposable column '{0}'", Schema[col].Name); } _host.AssertValue(_tview); return(_tview.GetSlotCursor(col)); }
public VectorType GetSlotType(int col) { _parent.Host.CheckParam(0 <= col && col < ColumnCount, nameof(col)); bool isSrc; int index = MapColumnIndex(out isSrc, col); if (isSrc) { if (_inputTransposed != null) { return(_inputTransposed.GetSlotType(index)); } return(null); } return(_parent.GetSlotTypeCore(index)); }
public static Bindings Create(OneToOneTransformBase parent, OneToOneColumn[] column, ISchema input, ITransposeSchema transInput, Func <ColumnType, string> testType) { Contracts.AssertValue(parent); var host = parent.Host; host.CheckUserArg(Utils.Size(column) > 0, nameof(column)); host.AssertValue(input); host.AssertValueOrNull(transInput); host.AssertValueOrNull(testType); var names = new string[column.Length]; var infos = new ColInfo[column.Length]; for (int i = 0; i < names.Length; i++) { var item = column[i]; host.CheckUserArg(item.TrySanitize(), nameof(OneToOneColumn.Name), "Invalid new column name"); names[i] = item.Name; int colSrc; if (!input.TryGetColumnIndex(item.Source, out colSrc)) { throw host.ExceptUserArg(nameof(OneToOneColumn.Source), "Source column '{0}' not found", item.Source); } var type = input.GetColumnType(colSrc); if (testType != null) { string reason = testType(type); if (reason != null) { throw host.ExceptUserArg(nameof(OneToOneColumn.Source), InvalidTypeErrorFormat, item.Source, type, reason); } } var slotType = transInput == null ? null : transInput.GetSlotType(colSrc); infos[i] = new ColInfo(names[i], colSrc, type, slotType); } return(new Bindings(parent, infos, input, true, names)); }
private static void PrintSchema(TextWriter writer, Arguments args, Schema schema, ITransposeSchema tschema) { Contracts.AssertValue(writer); Contracts.AssertValue(args); Contracts.AssertValue(schema); Contracts.AssertValueOrNull(tschema); #if !CORECLR if (args.ShowJson) { writer.WriteLine("Json Schema not supported."); return; } #endif int colLim = schema.Count; var itw = new IndentedTextWriter(writer, " "); itw.WriteLine("{0} columns:", colLim); using (itw.Nest()) { var names = default(VBuffer <ReadOnlyMemory <char> >); for (int col = 0; col < colLim; col++) { var name = schema[col].Name; var type = schema[col].Type; var slotType = tschema == null ? null : tschema.GetSlotType(col); itw.WriteLine("{0}: {1}{2}", name, type, slotType == null ? "" : " (T)"); bool metaVals = args.ShowMetadataValues; if (metaVals || args.ShowMetadataTypes) { ShowMetadata(itw, schema, col, metaVals); continue; } if (!args.ShowSlots) { continue; } if (!type.IsKnownSizeVector) { continue; } ColumnType typeNames; if ((typeNames = schema[col].Metadata.Schema.GetColumnOrNull(MetadataUtils.Kinds.SlotNames)?.Type) == null) { continue; } if (typeNames.VectorSize != type.VectorSize || !typeNames.ItemType.IsText) { Contracts.Assert(false, "Unexpected slot names type"); continue; } schema[col].Metadata.GetValue(MetadataUtils.Kinds.SlotNames, ref names); if (names.Length != type.VectorSize) { Contracts.Assert(false, "Unexpected length of slot names vector"); continue; } using (itw.Nest()) { bool verbose = args.Verbose ?? false; foreach (var kvp in names.Items(all: verbose)) { if (verbose || !kvp.Value.IsEmpty) { itw.WriteLine("{0}:{1}", kvp.Key, kvp.Value); } } } } } }