コード例 #1
0
        /// <summary>
        /// An implementation of <see cref="DataViewTypeAttribute.Register"/>.
        /// </summary>
        public override void Register()
        {
            var enumerableType = typeof(IDictionary <,>);
            var type           = enumerableType.MakeGenericType(_keyType, _valueType);

            DataViewTypeManager.Register(new OnnxMapType(_keyType, _valueType), type, new[] { this });
        }
コード例 #2
0
        /// <summary>
        /// An implementation of <see cref="DataViewTypeAttribute.Register"/>.
        /// </summary>
        public override void Register()
        {
            var enumerableType = typeof(IEnumerable <>);
            var type           = enumerableType.MakeGenericType(_elemType);

            DataViewTypeManager.Register(new OnnxSequenceType(_elemType), type, new[] { this });
        }
コード例 #3
0
        public void GetTypeWithAdditionalDataViewTypeAttributes()
        {
            var a = new DataViewAlienBodyType(7788);
            var b = new AlienTypeAttributeAttribute(8877);
            var c = new ColumnNameAttribute("foo");
            var d = new AlienTypeAttributeAttribute(8876);


            DataViewTypeManager.Register(a, typeof(AlienBody), b);
            Assert.True(DataViewTypeManager.Knows(a));
            Assert.True(DataViewTypeManager.Knows(typeof(AlienBody), new Attribute[] { b, c }));
            // "a" is associated with typeof(AlienBody) with "b," so the call below should return true.
            Assert.Equal(a, DataViewTypeManager.GetDataViewType(typeof(AlienBody), new Attribute[] { b, c }));
            Assert.Throws <ArgumentOutOfRangeException>(() => DataViewTypeManager.Knows(typeof(AlienBody), new Attribute[] { b, d }));
        }
コード例 #4
0
        private static OpCode GetAssignmentOpCode(Type t, IEnumerable <Attribute> attributes)
        {
            // REVIEW: This should be a Dictionary<Type, OpCode> based solution.
            // DvTypes, strings, arrays, all nullable types, VBuffers and RowId.
            if (t == typeof(ReadOnlyMemory <char>) || t == typeof(string) || t.IsArray ||
                (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(VBuffer <>)) ||
                (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable <>)) ||
                t == typeof(DateTime) || t == typeof(DateTimeOffset) || t == typeof(TimeSpan) ||
                t == typeof(DataViewRowId) || DataViewTypeManager.Knows(t, attributes))
            {
                return(OpCodes.Stobj);
            }

            // Simple primitive types.
            if (t == typeof(Single))
            {
                return(OpCodes.Stind_R4);
            }
            if (t == typeof(Double))
            {
                return(OpCodes.Stind_R8);
            }
            if (t == typeof(sbyte) || t == typeof(byte) || t == typeof(bool))
            {
                return(OpCodes.Stind_I1);
            }
            if (t == typeof(short) || t == typeof(ushort))
            {
                return(OpCodes.Stind_I2);
            }
            if (t == typeof(int) || t == typeof(uint))
            {
                return(OpCodes.Stind_I4);
            }
            if (t == typeof(long) || t == typeof(ulong))
            {
                return(OpCodes.Stind_I8);
            }
            throw Contracts.ExceptNotSupp("Type '{0}' is not supported.", t.FullName);
        }
コード例 #5
0
 // A function implicitly invoked by ML.NET when processing a custom
 // type. It binds a DataViewType to a custom type plus its attributes.
 public override void Register()
 {
     DataViewTypeManager.Register(new DataViewAlienBodyType(RaceId),
                                  typeof(AlienBody), this);
 }
コード例 #6
0
 /// <summary>
 /// Create the corresponding <see cref="DataViewType"/> for ONNX map.
 /// </summary>
 /// <param name="keyType">Key type of the associated ONNX map.</param>
 /// <param name="valueType">Value type of the associated ONNX map.</param>
 public OnnxMapType(Type keyType, Type valueType) : base(typeof(IDictionary <,>).MakeGenericType(keyType, valueType))
 {
     DataViewTypeManager.Register(this, RawType, new[] { new OnnxMapTypeAttribute(keyType, valueType) });
 }
コード例 #7
0
ファイル: ImageType.cs プロジェクト: artemiusgreat/ML-NET
 public override void Register()
 {
     DataViewTypeManager.Register(new ImageDataViewType(Height, Width), typeof(Bitmap), this);
 }
コード例 #8
0
 /// <summary>
 /// Create the corresponding <see cref="DataViewType"/> for ONNX sequence.
 /// </summary>
 /// <param name="elementType">The element type of a sequence.</param>
 public OnnxSequenceType(Type elementType) : base(MakeNativeType(elementType))
 {
     DataViewTypeManager.Register(this, RawType, new[] { new OnnxSequenceTypeAttribute(elementType) });
 }
コード例 #9
0
        public void TestTypeManager()
        {
            // Semantically identical DataViewTypes should produce the same hash code.
            var a     = new DataViewAlienBodyType(9527);
            var aCode = a.GetHashCode();
            var b     = new DataViewAlienBodyType(9527);
            var bCode = b.GetHashCode();

            Assert.Equal(aCode, bCode);

            // Semantically identical attributes should produce the same hash code.
            var c     = new AlienTypeAttributeAttribute(1228);
            var cCode = c.GetHashCode();
            var d     = new AlienTypeAttributeAttribute(1228);
            var dCode = d.GetHashCode();

            Assert.Equal(cCode, dCode);

            // Check registering the same type pair is OK.
            // Note that "a" and "b" should be identical.
            DataViewTypeManager.Register(a, typeof(AlienBody));
            DataViewTypeManager.Register(a, typeof(AlienBody));
            DataViewTypeManager.Register(b, typeof(AlienBody));
            DataViewTypeManager.Register(b, typeof(AlienBody));

            // Check if register of (a, typeof(AlienBody)) successes.
            Assert.True(DataViewTypeManager.Knows(a));
            Assert.True(DataViewTypeManager.Knows(b));
            Assert.True(DataViewTypeManager.Knows(typeof(AlienBody)));
            Assert.Equal(a, DataViewTypeManager.GetDataViewType(typeof(AlienBody)));
            Assert.Equal(b, DataViewTypeManager.GetDataViewType(typeof(AlienBody)));

            // Make sure registering the same type twice throws.
            bool isWrong = false;

            try
            {
                // "a" has been registered with AlienBody without any attribute, so the user can't
                // register "a" again with AlienBody plus the attribute "c."
                DataViewTypeManager.Register(a, typeof(AlienBody), c);
            }
            catch
            {
                isWrong = true;
            }
            Assert.True(isWrong);

            // Make sure registering the same type twice throws.
            isWrong = false;
            try
            {
                // AlienBody has been registered with "a," so user can't register it with
                // "new DataViewAlienBodyType(5566)" again.
                DataViewTypeManager.Register(new DataViewAlienBodyType(5566), typeof(AlienBody));
            }
            catch
            {
                isWrong = true;
            }
            Assert.True(isWrong);

            // Register a type with attribute.
            var e = new DataViewAlienBodyType(7788);
            var f = new AlienTypeAttributeAttribute(8877);

            DataViewTypeManager.Register(e, typeof(AlienBody), f);
            Assert.True(DataViewTypeManager.Knows(e));
            Assert.True(DataViewTypeManager.Knows(typeof(AlienBody), new[] { f }));
            // "e" is associated with typeof(AlienBody) with "f," so the call below should return true.
            Assert.Equal(e, DataViewTypeManager.GetDataViewType(typeof(AlienBody), new[] { f }));
            // "a" is associated with typeof(AlienBody) without any attribute, so the call below should return false.
            Assert.NotEqual(a, DataViewTypeManager.GetDataViewType(typeof(AlienBody), new[] { f }));
        }