Beispiel #1
0
        protected OneToOneTransformBase(IHost host, ModelLoadContext ctx,
                                        IDataView input, Func <ColumnType, string> testType)
            : base(host, input)
        {
            Host.CheckValue(ctx, nameof(ctx));
            Host.CheckValueOrNull(testType);
            InputTranspose = Source as ITransposeDataView;

            _bindings = Bindings.Create(this, ctx, Source.Schema, InputTransposeSchema, testType);
            Infos     = _bindings.Infos;
            Metadata  = new MetadataDispatcher(Infos.Length);
        }
Beispiel #2
0
        protected OneToOneTransformBase(IHost host, OneToOneColumn[] column,
                                        IDataView input, Func <ColumnType, string> testType)
            : base(host, input)
        {
            Host.CheckUserArg(Utils.Size(column) > 0, nameof(column));
            Host.CheckValueOrNull(testType);
            InputTranspose = Source as ITransposeDataView;

            _bindings = Bindings.Create(this, column, Source.Schema, InputTransposeSchema, testType);
            Infos     = _bindings.Infos;
            Metadata  = new MetadataDispatcher(Infos.Length);
        }
        /// <summary>
        /// Computes the column type and whether multiple indicator vectors need to be concatenated.
        /// Also populates the metadata.
        /// </summary>
        private static void ComputeType(KeyToBinaryVectorTransform trans, ISchema input, int iinfo,
                                        ColInfo info, MetadataDispatcher md, out VectorType type, out bool concat, out int bitsPerColumn)
        {
            Contracts.AssertValue(trans);
            Contracts.AssertValue(input);
            Contracts.AssertValue(info);
            Contracts.Assert(info.TypeSrc.ItemType.IsKey);
            Contracts.Assert(info.TypeSrc.ItemType.KeyCount > 0);

            //Add an additional bit for all 1s to represent missing values.
            bitsPerColumn = Utils.IbitHigh((uint)info.TypeSrc.ItemType.KeyCount) + 2;

            Contracts.Assert(bitsPerColumn > 0);

            // See if the source has key names.
            var typeNames = input.GetMetadataTypeOrNull(MetadataUtils.Kinds.KeyValues, info.Source);

            if (typeNames == null || !typeNames.IsKnownSizeVector || !typeNames.ItemType.IsText ||
                typeNames.VectorSize != info.TypeSrc.ItemType.KeyCount)
            {
                typeNames = null;
            }

            // Don't pass through any source column metadata.
            using (var bldr = md.BuildMetadata(iinfo))
            {
                if (info.TypeSrc.ValueCount == 1)
                {
                    // Output is a single vector computed as the sum of the output indicator vectors.
                    concat = false;
                    type   = new VectorType(NumberType.Float, bitsPerColumn);
                    if (typeNames != null)
                    {
                        bldr.AddGetter <VBuffer <DvText> >(MetadataUtils.Kinds.SlotNames,
                                                           new VectorType(TextType.Instance, type), trans.GetKeyNames);
                    }

                    bldr.AddPrimitive(MetadataUtils.Kinds.IsNormalized, BoolType.Instance, DvBool.True);
                }
                else
                {
                    // Output is the concatenation of the multiple output indicator vectors.
                    concat = true;
                    type   = new VectorType(NumberType.Float, info.TypeSrc.ValueCount, bitsPerColumn);
                    if (typeNames != null && type.VectorSize > 0)
                    {
                        bldr.AddGetter <VBuffer <DvText> >(MetadataUtils.Kinds.SlotNames,
                                                           new VectorType(TextType.Instance, type), trans.GetSlotNames);
                    }
                }
            }
        }
Beispiel #4
0
            /// <summary>
            /// This should really be private to MetadataDispatcher, but C#'s accessibility model doesn't
            /// allow restricting to an outer class.
            /// </summary>
            internal Builder(MetadataDispatcher md, int index,
                             Schema schemaSrc = null, int indexSrc = -1, Func <string, int, bool> filterSrc = null)
            {
                Contracts.CheckValue(md, nameof(md));
                Contracts.CheckParam(0 <= index && index < md.ColCount, nameof(index));

                _index = index;
                _md    = md;
                _info  = _md.CreateInfo(schemaSrc, indexSrc, filterSrc);

                var tmp = _md.GetColInfoOrNull(_index);

                Contracts.Check(tmp == null, "Duplicate building of metadata");
            }
Beispiel #5
0
        /// <summary>
        /// Re-applying constructor.
        /// </summary>
        protected OneToOneTransformBase(IHostEnvironment env, string name, OneToOneTransformBase transform,
                                        IDataView newInput, Func <ColumnType, 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.GetColumnName(x.Source),
            })
                                   .ToArray();

            _bindings = Bindings.Create(this, map, newInput.Schema, InputTransposeSchema, checkType);
            Infos     = _bindings.Infos;
            Metadata  = new MetadataDispatcher(Infos.Length);
        }
        private static void CreateMetadata(IEnumerable <ColumnMetadataInfo> infos, Bindings bindings, out MetadataDispatcher md)
        {
            Contracts.AssertValue(bindings);

            md = new MetadataDispatcher(bindings.InfoCount);
            foreach (var colInfo in infos)
            {
                if (colInfo == null)
                {
                    continue;
                }
                int iinfo;
                var found = bindings.TryGetInfoIndex(colInfo.Name, out iinfo);
                Contracts.Assert(found);
                using (var bldr = md.BuildMetadata(iinfo))
                {
                    foreach (var info in colInfo.Infos())
                    {
                        Action <MetadataDispatcher.Builder, string, MetadataInfo <int> > del = AddGetter;
                        var meth =
                            del.GetMethodInfo().GetGenericMethodDefinition().MakeGenericMethod(info.Value.Type.RawType);
                        meth.Invoke(null, new object[] { bldr, info.Key, info.Value });
                    }
                }
            }
            md.Seal();
        }