Example #1
0
 protected static string TestIsFloatItem(DataViewType type)
 {
     if (type.GetItemType() == NumberDataViewType.Single)
     {
         return(null);
     }
     return("Expected Single or a vector of Single");
 }
Example #2
0
 protected static string TestIsKey(DataViewType type)
 {
     if (type.GetItemType().GetKeyCount() > 0)
     {
         return(null);
     }
     return("Expected Key type of known cardinality");
 }
Example #3
0
 protected static string TestIsTextItem(DataViewType type)
 {
     if (type.GetItemType() is TextDataViewType)
     {
         return(null);
     }
     return("Expected Text type");
 }
            private ColInfo(string name, DataViewType colType, Segment[] segs, int sizeBase)
            {
                Contracts.AssertNonEmpty(name);
                Contracts.AssertValueOrNull(segs);
                Contracts.Assert(sizeBase > 0);

                Name = name;
                Contracts.Assert(colType.GetItemType().GetRawKind() != 0);
                ColType  = colType;
                Segments = segs;
                SizeBase = sizeBase;
            }
Example #5
0
            public ColInfo(string name, int colSrc, DataViewType typeSrc, VectorDataViewType slotTypeSrc)
            {
                Contracts.AssertNonEmpty(name);
                Contracts.Assert(colSrc >= 0);
                Contracts.AssertValue(typeSrc);
                Contracts.AssertValueOrNull(slotTypeSrc);
                Contracts.Assert(slotTypeSrc == null || typeSrc.GetItemType().Equals(slotTypeSrc.ItemType));

                Name        = name;
                Source      = colSrc;
                TypeSrc     = typeSrc;
                SlotTypeSrc = slotTypeSrc;
            }
Example #6
0
            public void AssertRep()
            {
                // Check that all fields have values.
                Contracts.AssertNonEmpty(ColumnName);
                Contracts.AssertValue(ColumnType);
                Contracts.AssertValueOrNull(Generator);

                // If Column is computed type, it must have a generator.
                Contracts.Assert(IsComputed == (Generator != null));

                // Column must have either a generator or a memberInfo value.
                Contracts.Assert((Generator == null) != (MemberInfo == null));

                // Additional Checks if there is a generator.
                if (Generator == null)
                {
                    return;
                }
                Contracts.AssertValue(ReturnParameterInfo);

                // Checks input parameters are (someClass, long, ref value) in that order.
                var parameterInfos = Generator.GetMethodInfo().GetParameters().ToArray();
                var parameterTypes = (from pInfo in parameterInfos select pInfo.ParameterType).ToArray();

                Contracts.Assert(parameterTypes.Length == 3);
                Contracts.Assert(parameterTypes[2].IsByRef);
                Contracts.Assert(parameterTypes[1] == typeof(long));
                Contracts.Assert(!(parameterTypes[0].GetTypeInfo().IsPrimitive || parameterTypes[0] == typeof(string)));

                // Check that generator returns void.
                Contracts.Assert(Generator.GetMethodInfo().ReturnType == typeof(void));

                // Checks that the return type of the generator is compatible with ColumnType.
                GetVectorAndItemType("return type", ComputedReturnType, null, out bool isVector, out Type itemType);
                Contracts.Assert(isVector == ColumnType is VectorDataViewType);
                Contracts.Assert(itemType == ColumnType.GetItemType().RawType);
            }
        // REVIEW: It would be nice to support propagation of select metadata.
        public static IDataView Create <TSrc, TDst>(IHostEnvironment env, string name, IDataView input,
                                                    string src, string dst, DataViewType typeSrc, DataViewType typeDst, ValueMapper <TSrc, TDst> mapper,
                                                    ValueGetter <VBuffer <ReadOnlyMemory <char> > > keyValueGetter = null, ValueGetter <VBuffer <ReadOnlyMemory <char> > > slotNamesGetter = null)
        {
            Contracts.CheckValue(env, nameof(env));
            env.CheckNonEmpty(name, nameof(name));
            env.CheckValue(input, nameof(input));
            env.CheckNonEmpty(src, nameof(src));
            env.CheckNonEmpty(dst, nameof(dst));
            env.CheckValue(typeSrc, nameof(typeSrc));
            env.CheckValue(typeDst, nameof(typeDst));
            env.CheckValue(mapper, nameof(mapper));
            env.Check(keyValueGetter == null || typeDst.GetItemType() is KeyType);
            env.Check(slotNamesGetter == null || typeDst.IsKnownSizeVector());

            if (typeSrc.RawType != typeof(TSrc))
            {
                throw env.ExceptParam(nameof(mapper),
                                      "The source column type '{0}' doesn't match the input type of the mapper", typeSrc);
            }
            if (typeDst.RawType != typeof(TDst))
            {
                throw env.ExceptParam(nameof(mapper),
                                      "The destination column type '{0}' doesn't match the output type of the mapper", typeDst);
            }

            bool tmp = input.Schema.TryGetColumnIndex(src, out int colSrc);

            if (!tmp)
            {
                throw env.ExceptParam(nameof(src), "The input data doesn't have a column named '{0}'", src);
            }
            var typeOrig = input.Schema[colSrc].Type;

            // REVIEW: Ideally this should support vector-type conversion. It currently doesn't.
            bool     ident;
            Delegate conv;

            if (typeOrig.SameSizeAndItemType(typeSrc))
            {
                ident = true;
                conv  = null;
            }
            else if (!Conversions.Instance.TryGetStandardConversion(typeOrig, typeSrc, out conv, out ident))
            {
                throw env.ExceptParam(nameof(mapper),
                                      "The type of column '{0}', '{1}', cannot be converted to the input type of the mapper '{2}'",
                                      src, typeOrig, typeSrc);
            }

            var       col = new Column(src, dst);
            IDataView impl;

            if (ident)
            {
                impl = new Impl <TSrc, TDst, TDst>(env, name, input, col, typeDst, mapper, keyValueGetter: keyValueGetter, slotNamesGetter: slotNamesGetter);
            }
            else
            {
                Func <IHostEnvironment, string, IDataView, Column, DataViewType, ValueMapper <int, int>,
                      ValueMapper <int, int>, ValueGetter <VBuffer <ReadOnlyMemory <char> > >, ValueGetter <VBuffer <ReadOnlyMemory <char> > >,
                      Impl <int, int, int> > del = CreateImpl <int, int, int>;
                var meth = del.GetMethodInfo().GetGenericMethodDefinition()
                           .MakeGenericMethod(typeOrig.RawType, typeof(TSrc), typeof(TDst));
                impl = (IDataView)meth.Invoke(null, new object[] { env, name, input, col, typeDst, conv, mapper, keyValueGetter, slotNamesGetter });
            }

            return(new OpaqueDataView(impl));
        }
            public Impl(IHostEnvironment env, string name, IDataView input, OneToOneColumn col,
                        DataViewType typeDst, ValueMapper <T1, T2> map1, ValueMapper <T2, T3> map2 = null,
                        ValueGetter <VBuffer <ReadOnlyMemory <char> > > keyValueGetter             = null, ValueGetter <VBuffer <ReadOnlyMemory <char> > > slotNamesGetter = null)
                : base(env, name, new[] { col }, input, x => null)
            {
                Host.Assert(typeDst.RawType == typeof(T3));
                Host.AssertValue(map1);
                Host.Assert(map2 != null || typeof(T2) == typeof(T3));

                _typeDst = typeDst;
                _map1    = map1;
                _map2    = map2;

                if (keyValueGetter != null || slotNamesGetter != null)
                {
                    using (var bldr = Metadata.BuildMetadata(0))
                    {
                        if (keyValueGetter != null)
                        {
                            AnnotationUtils.AnnotationGetter <VBuffer <ReadOnlyMemory <char> > > mdGetter =
                                (int c, ref VBuffer <ReadOnlyMemory <char> > dst) => keyValueGetter(ref dst);
                            bldr.AddGetter(AnnotationUtils.Kinds.KeyValues, new VectorType(TextDataViewType.Instance, _typeDst.GetItemType().GetKeyCountAsInt32(Host)), mdGetter);
                        }
                        if (slotNamesGetter != null)
                        {
                            int vectorSize = _typeDst.GetVectorSize();
                            Host.Assert(vectorSize > 0);
                            AnnotationUtils.AnnotationGetter <VBuffer <ReadOnlyMemory <char> > > mdGetter =
                                (int c, ref VBuffer <ReadOnlyMemory <char> > dst) => slotNamesGetter(ref dst);
                            bldr.AddGetter(AnnotationUtils.Kinds.SlotNames, new VectorType(TextDataViewType.Instance, vectorSize), mdGetter);
                        }
                    }
                }
                Metadata.Seal();
            }