Exemplo n.º 1
0
            public Cursor(IChannelProvider provider, OneToOneTransformBase parent, DataViewRowCursor input, bool[] active)
                : base(provider, input)
            {
                Ch.AssertValue(parent);
                Ch.Assert(active == null || active.Length == parent._bindings.ColumnCount);

                _bindings = parent._bindings;
                _active   = active;
                _getters  = new Delegate[parent.Infos.Length];

                // Build the disposing delegate.
                Action masterDisposer = null;

                for (int iinfo = 0; iinfo < _getters.Length; iinfo++)
                {
                    if (!IsColumnActive(Schema[parent._bindings.MapIinfoToCol(iinfo)]))
                    {
                        continue;
                    }
                    _getters[iinfo] = parent.GetGetterCore(Ch, Input, iinfo, out Action disposer);
                    if (disposer != null)
                    {
                        masterDisposer += disposer;
                    }
                }
                _disposer = masterDisposer;
            }
Exemplo n.º 2
0
            public static Bindings Create(OneToOneTransformBase parent, ModelLoadContext ctx, DataViewSchema inputSchema,
                                          ITransposeDataView transposeInput, Func <DataViewType, string> testType)
            {
                Contracts.AssertValue(parent);
                var host = parent.Host;

                host.CheckValue(ctx, nameof(ctx));
                host.AssertValue(inputSchema);
                host.AssertValueOrNull(transposeInput);
                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 (!inputSchema.TryGetColumnIndex(src, out colSrc))
                    {
                        throw host.ExceptSchemaMismatch(nameof(inputSchema), "source", src);
                    }
                    var type = inputSchema[colSrc].Type;
                    if (testType != null)
                    {
                        string reason = testType(type);
                        if (reason != null)
                        {
                            throw host.Except(InvalidTypeErrorFormat, src, type, reason);
                        }
                    }
                    var slotType = transposeInput?.GetSlotType(i);
                    infos[i] = new ColInfo(dst, colSrc, type, slotType as VectorDataViewType);
                }

                return(new Bindings(parent, infos, inputSchema, false, names));
            }
Exemplo n.º 3
0
            private Bindings(OneToOneTransformBase parent, ColInfo[] infos,
                             DataViewSchema input, bool user, string[] names)
                : base(input, user, names)
            {
                Contracts.AssertValue(parent);
                Contracts.AssertValue(parent.Host);
                Contracts.Assert(Utils.Size(infos) == InfoCount);

                _parent = parent;
                Infos   = infos;
            }
Exemplo n.º 4
0
            private Bindings(OneToOneTransformBase parent, ColInfo[] infos,
                             Schema 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;
            }
Exemplo n.º 5
0
        private protected OneToOneTransformBase(IHostEnvironment env, string name, OneToOneTransformBase transform,
                                                IDataView newInput, Func <DataViewType, string> checkType)
            : base(env, name, newInput)
        {
            Host.CheckValueOrNull(checkType);
            InputTranspose = Source as ITransposeDataView;

            OneToOneColumn[] map = transform.Infos
                                   .Select(x => new ColumnTmp
            {
                Name   = x.Name,
                Source = transform.Source.Schema[x.Source].Name,
            })
                                   .ToArray();

            _bindings = Bindings.Create(this, map, newInput.Schema, InputTranspose, checkType);
            Infos     = _bindings.Infos;
            Metadata  = new MetadataDispatcher(Infos.Length);
        }
Exemplo n.º 6
0
            public static Bindings Create(OneToOneTransformBase parent, OneToOneColumn[] column, DataViewSchema inputSchema,
                                          ITransposeDataView transposedInput, Func <DataViewType, string> testType)
            {
                Contracts.AssertValue(parent);
                var host = parent.Host;

                host.CheckUserArg(Utils.Size(column) > 0, nameof(column));
                host.AssertValue(inputSchema);
                host.AssertValueOrNull(transposedInput);
                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 (!inputSchema.TryGetColumnIndex(item.Source, out colSrc))
                    {
                        throw host.ExceptUserArg(nameof(OneToOneColumn.Source), "Source column '{0}' not found", item.Source);
                    }

                    var type = inputSchema[colSrc].Type;
                    if (testType != null)
                    {
                        string reason = testType(type);
                        if (reason != null)
                        {
                            throw host.ExceptUserArg(nameof(OneToOneColumn.Source), InvalidTypeErrorFormat, item.Source, type, reason);
                        }
                    }

                    var slotType = transposedInput?.GetSlotType(i);
                    infos[i] = new ColInfo(names[i], colSrc, type, slotType as VectorDataViewType);
                }

                return(new Bindings(parent, infos, inputSchema, true, names));
            }