private CompositeDataLoader(IHost host, TransformEx[] transforms)
        {
            Contracts.AssertValue(host, "host");
            _host = host;
            _host.AssertNonEmpty(transforms);

            _view    = transforms[transforms.Length - 1].Transform;
            _tview   = _view as ITransposeDataView;
            _tschema = _tview == null ? new TransposerUtils.SimpleTransposeSchema(_view.Schema) : _tview.TransposeSchema;

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

#if DEBUG
            // Assert that the transforms array is consistent: first one starts with loader,
            // they are chained together, the loader is not a composite.
            for (int i = 1; i < transforms.Length; i++)
            {
                _host.Assert(transforms[i].Transform.Source == transforms[i - 1].Transform, "Transforms are not linked");
            }

            _host.AssertValue(srcLoader, "loader", "Transform chain doesn't start with a loader");
            _host.Assert(!(srcLoader is CompositeDataLoader), "Can't have composite source loader");
#endif

            _loader     = srcLoader;
            _transforms = transforms;
        }
Exemple #2
0
            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));
            }
Exemple #3
0
            private Bindings(OneToOneTransformBase parent, ColInfo[] infos,
                             ISchema input, bool user, string[] names)
                : base(input, user, names)
            {
                Contracts.AssertValue(parent);
                Contracts.AssertValue(parent.Host);
                Contracts.Assert(Utils.Size(infos) == InfoCount);

                _parent          = parent;
                _inputTransposed = _parent.InputTranspose == null ? null : _parent.InputTranspose.TransposeSchema;
                Contracts.Assert((_inputTransposed == null) == (_parent.InputTranspose == null));
                Infos = infos;
            }
Exemple #4
0
            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);
                            }
                        }
                    }
                }
            }
        }