public static DataViewType ReadType(ModelLoadContext ctx)
        {
            bool isVector = ctx.Reader.ReadBoolean();

            if (isVector)
            {
                int dimCount = ctx.Reader.ReadInt32();
                if (dimCount != 1)
                {
                    throw Contracts.ExceptNotImpl("Number of dimensions should be 1.");
                }
                var dims = new int[dimCount];
                for (int i = 0; i < dimCount; ++i)
                {
                    dims[i] = ctx.Reader.ReadInt32();
                }
                var kind = (DataKind)ctx.Reader.ReadByte();
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(kind), dims[0]));
            }
            else
            {
                var kind = (DataKind)ctx.Reader.ReadByte();
                return(FromKind(kind));
            }
        }
        /// <summary>
        /// From DataKind to DataViewType
        /// </summary>
        public static DataViewType FromKind(DataKind kind)
        {
            switch (kind)
            {
            case DataKind.String:
                return(TextDataViewType.Instance);

            case DataKind.Boolean:
                return(BooleanDataViewType.Instance);

            case DataKind.DateTime:
                return(DateTimeDataViewType.Instance);

            case DataKind.TimeSpan:
                return(TimeSpanDataViewType.Instance);

            default:
                return(ColumnTypeHelper.NumberFromKind(kind));
            }
        }
Example #3
0
        /// <summary>
        /// From DataKind to ColumnType
        /// </summary>
        public static ColumnType FromKind(DataKind kind)
        {
            switch (kind)
            {
            case DataKind.TX:
                return(TextType.Instance);

            case DataKind.Bool:
                return(BoolType.Instance);

            case DataKind.DateTime:
                return(DateTimeType.Instance);

            case DataKind.TimeSpan:
                return(TimeSpanType.Instance);

            default:
                return(ColumnTypeHelper.NumberFromKind(kind));
            }
        }
        /// <summary>
        /// Returns the data kind based on a type.
        /// </summary>
        public static DataViewType GetColumnType(Type type)
        {
            if (type == typeof(bool))
            {
                return(BooleanDataViewType.Instance);
            }
            if (type == typeof(byte))
            {
                return(NumberDataViewType.Byte);
            }
            if (type == typeof(ushort))
            {
                return(NumberDataViewType.UInt16);
            }
            if (type == typeof(uint))
            {
                return(NumberDataViewType.UInt32);
            }
            if (type == typeof(int))
            {
                return(NumberDataViewType.Int32);
            }
            if (type == typeof(Int64))
            {
                return(NumberDataViewType.Int64);
            }
            if (type == typeof(float))
            {
                return(NumberDataViewType.Single);
            }
            if (type == typeof(double))
            {
                return(NumberDataViewType.Double);
            }
            if (type == typeof(ReadOnlyMemory <char>) || type == typeof(string) || type == typeof(DvText))
            {
                return(ColumnTypeHelper.PrimitiveFromKind(DataKind.String));
            }

            if (type == typeof(VBuffer <bool>) || type == typeof(VBufferEqSort <bool>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.Boolean)));
            }
            if (type == typeof(VBuffer <byte>) || type == typeof(VBufferEqSort <byte>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.SByte)));
            }
            if (type == typeof(VBuffer <ushort>) || type == typeof(VBufferEqSort <ushort>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.UInt16)));
            }
            if (type == typeof(VBuffer <uint>) || type == typeof(VBufferEqSort <uint>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.UInt32)));
            }
            if (type == typeof(VBuffer <int>) || type == typeof(VBufferEqSort <int>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.Int32)));
            }
            if (type == typeof(VBuffer <Int64>) || type == typeof(VBufferEqSort <Int64>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.Int64)));
            }
            if (type == typeof(VBuffer <float>) || type == typeof(VBufferEqSort <float>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.Single)));
            }
            if (type == typeof(VBuffer <double>) || type == typeof(VBufferEqSort <double>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.Double)));
            }
            if (type == typeof(VBuffer <ReadOnlyMemory <char> >) || type == typeof(VBuffer <string>) || type == typeof(VBuffer <DvText>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.String)));
            }
            if (type == typeof(VBufferEqSort <string>) || type == typeof(VBufferEqSort <DvText>))
            {
                return(new VectorDataViewType(ColumnTypeHelper.PrimitiveFromKind(DataKind.String)));
            }

            throw Contracts.ExceptNotSupp("Unsupported output type {0}.", type);
        }
        public static string ToString(DataViewSchema schema, string sep = "; ",
                                      bool vectorVec = true, bool keepHidden = false)
        {
            var    builder = new StringBuilder();
            string name, type;
            string si;
            int    lag = 0;

            for (int i = 0; i < schema.Count; ++i)
            {
                if (!keepHidden && schema[i].IsHidden)
                {
                    continue;
                }
                if (builder.Length > 0)
                {
                    builder.Append(sep);
                }
                name = schema[i].Name;
                var t = schema[i].Type;
                if (vectorVec || (!t.IsVector() && !t.IsKey()))
                {
                    var vecType = schema[i].Type;
                    if (vecType.IsVector())
                    {
                        var dimension = vecType.VectorSize() >= 0 ? string.Format(",{0}", vecType.VectorSize()) : string.Empty;
                        type = string.Format("Vec<{0}{1}>", ColumnTypeHelper.DataViewType2Internal(vecType.ItemType()), dimension);
                        si   = (i + lag).ToString();
                    }
                    else
                    {
                        type = ColumnTypeHelper.DataViewType2Internal(vecType.ItemType()).ToString();
                    }
                    si = (i + lag).ToString();
                }
                else
                {
                    if (t.IsVector())
                    {
                        if (t.AsVector().DimCount() != 1)
                        {
                            throw Contracts.ExceptNotSupp("Only vector with one dimension are supported.");
                        }
                        type = t.ItemType().RawKind().ToString();
                        si   = string.Format("{0}-{1}", i + lag, i + lag + t.AsVector().GetDim(0) - 1);
                        lag += t.AsVector().GetDim(0) - 1;
                    }
                    else if (t.IsKey())
                    {
                        var k = t.AsKey();
                        type = k.Count > 0
                                    ? string.Format("{0}[{1}]", k.RawKind(), k.Count)
                                    : string.Format("{0}[{1}]", k.RawKind(), "*");
                        si = i.ToString();
                    }
                    else
                    {
                        throw Contracts.ExceptNotImpl(string.Format("Unable to process type '{0}'.", t));
                    }
                }

                builder.Append(string.Format("{0}:{1}:{2}", name, type, si));
            }
            return(builder.ToString());
        }
Example #6
0
        /// <summary>
        /// Returns the data kind based on a type.
        /// </summary>
        public static ColumnType GetColumnType(Type type)
        {
            if (type == typeof(bool))
            {
                return(BoolType.Instance);
            }
            if (type == typeof(byte))
            {
                return(NumberType.U1);
            }
            if (type == typeof(ushort))
            {
                return(NumberType.U2);
            }
            if (type == typeof(uint))
            {
                return(NumberType.U4);
            }
            if (type == typeof(int))
            {
                return(NumberType.I4);
            }
            if (type == typeof(Int64))
            {
                return(NumberType.I8);
            }
            if (type == typeof(float))
            {
                return(NumberType.R4);
            }
            if (type == typeof(double))
            {
                return(NumberType.R8);
            }
            if (type == typeof(ReadOnlyMemory <char>) || type == typeof(string) || type == typeof(DvText))
            {
                return(ColumnTypeHelper.PrimitiveFromKind(DataKind.TX));
            }

            if (type == typeof(VBuffer <bool>) || type == typeof(VBufferEqSort <bool>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.BL)));
            }
            if (type == typeof(VBuffer <byte>) || type == typeof(VBufferEqSort <byte>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.U1)));
            }
            if (type == typeof(VBuffer <ushort>) || type == typeof(VBufferEqSort <ushort>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.U2)));
            }
            if (type == typeof(VBuffer <uint>) || type == typeof(VBufferEqSort <uint>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.U4)));
            }
            if (type == typeof(VBuffer <int>) || type == typeof(VBufferEqSort <int>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.I4)));
            }
            if (type == typeof(VBuffer <Int64>) || type == typeof(VBufferEqSort <Int64>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.I8)));
            }
            if (type == typeof(VBuffer <float>) || type == typeof(VBufferEqSort <float>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.R4)));
            }
            if (type == typeof(VBuffer <double>) || type == typeof(VBufferEqSort <double>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.R8)));
            }
            if (type == typeof(VBuffer <ReadOnlyMemory <char> >) || type == typeof(VBuffer <string>) || type == typeof(VBuffer <DvText>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.TX)));
            }
            if (type == typeof(VBufferEqSort <string>) || type == typeof(VBufferEqSort <DvText>))
            {
                return(new VectorType(ColumnTypeHelper.PrimitiveFromKind(DataKind.TX)));
            }

            throw Contracts.ExceptNotSupp("Unsupported output type {0}.", type);
        }