예제 #1
0
 protected static string TestIsKey(ColumnType type)
 {
     if (type.GetItemType().GetKeyCount() > 0)
     {
         return(null);
     }
     return("Expected Key type of known cardinality");
 }
예제 #2
0
 protected static string TestIsTextItem(ColumnType type)
 {
     if (type.GetItemType() is TextType)
     {
         return(null);
     }
     return("Expected Text type");
 }
예제 #3
0
 protected static string TestIsFloatItem(ColumnType type)
 {
     if (type.GetItemType() == NumberType.Float)
     {
         return(null);
     }
     return("Expected R4 or a vector of R4");
 }
예제 #4
0
            public ColInfo(string name, int colSrc, ColumnType typeSrc, VectorType 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;
            }
            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(ComputedReturnType, "return type", out bool isVector, out Type itemType);
                Contracts.Assert(isVector == ColumnType is VectorType);
                Contracts.Assert(itemType == ColumnType.GetItemType().RawType);
            }
예제 #6
0
            public Impl(IHostEnvironment env, string name, IDataView input, OneToOneColumn col,
                        ColumnType 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)
                        {
                            int keyCount = _typeDst.GetItemType().GetKeyCount();
                            Host.Assert(keyCount > 0);
                            MetadataUtils.MetadataGetter <VBuffer <ReadOnlyMemory <char> > > mdGetter =
                                (int c, ref VBuffer <ReadOnlyMemory <char> > dst) => keyValueGetter(ref dst);
                            bldr.AddGetter(MetadataUtils.Kinds.KeyValues, new VectorType(TextType.Instance, keyCount), mdGetter);
                        }
                        if (slotNamesGetter != null)
                        {
                            int vectorSize = _typeDst.GetVectorSize();
                            Host.Assert(vectorSize > 0);
                            MetadataUtils.MetadataGetter <VBuffer <ReadOnlyMemory <char> > > mdGetter =
                                (int c, ref VBuffer <ReadOnlyMemory <char> > dst) => slotNamesGetter(ref dst);
                            bldr.AddGetter(MetadataUtils.Kinds.SlotNames, new VectorType(TextType.Instance, vectorSize), mdGetter);
                        }
                    }
                }
                Metadata.Seal();
            }
        // 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, ColumnType typeSrc, ColumnType 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, ColumnType, 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));
        }
예제 #8
0
 protected static string TestIsFloatItem(ColumnType type)
 {
     if (type.GetItemType() == NumberType.Float)
         return null;
     return "Expected R4 or a vector of R4";
 }
예제 #9
0
 protected static string TestIsTextItem(ColumnType type)
 {
     if (type.GetItemType() is TextType)
         return null;
     return "Expected Text type";
 }