Exemple #1
0
        /// <summary>
        /// Converts a column into another type.
        /// </summary>
        /// <param name="colType"column type></param>
        /// <returns>new column</returns>
        public IDataColumn AsType(DataViewType colType)
        {
            if (Kind == colType)
            {
                return(this);
            }
            if (Kind.IsVector() || colType.IsVector())
            {
                throw new NotImplementedException();
            }
            else
            {
                switch (Kind.RawKind())
                {
                case DataKind.Int32:
                    switch (colType.RawKind())
                    {
                    case DataKind.Single:
                        return(new DataColumn <float>(NumericHelper.Convert(_data as int[], float.NaN)));

                    default:
                        throw new NotImplementedException($"No conversion from '{Kind}' to '{colType.RawKind()}'.");
                    }

                default:
                    throw new NotImplementedException($"No conversion from '{Kind}' to '{colType.RawKind()}'.");
                }
            }
        }
 public static Tuple <DataKind, ArrayKind> GetKindArray(DataViewType type)
 {
     if (type.IsVector())
     {
         int dc = type.AsVector().DimCount();
         return(new Tuple <DataKind, ArrayKind>(type.ItemType().RawKind(), dc == 1 &&
                                                type.AsVector().GetDim(0) > 0 ? ArrayKind.Array : ArrayKind.VBuffer));
     }
     else
     {
         return(new Tuple <DataKind, ArrayKind>(type.RawKind(), ArrayKind.None));
     }
 }
 /// <summary>
 /// Saves a type into a stream.
 /// </summary>
 public static void WriteType(ModelSaveContext ctx, DataViewType type)
 {
     ctx.Writer.Write(type.IsVector());
     if (type.IsVector())
     {
         ctx.Writer.Write(type.AsVector().DimCount());
         for (int i = 0; i < type.AsVector().DimCount(); ++i)
         {
             ctx.Writer.Write(type.AsVector().GetDim(i));
         }
         ctx.Writer.Write((byte)type.AsVector().ItemType().RawKind());
     }
     else if (type.IsKey())
     {
         throw Contracts.ExceptNotImpl("Key cannot be serialized yet.");
     }
     else
     {
         ctx.Writer.Write((byte)type.RawKind());
     }
 }
Exemple #4
0
 private void ValidateInputs(out int indexLabel, out int indexTime, out DataViewType typeLabel, out DataViewType typeTime)
 {
     indexLabel = SchemaHelper.GetColumnIndex(Source.Schema, _args.columns[0].Source);
     typeLabel  = Source.Schema[indexLabel].Type;
     if (typeLabel.IsVector())
     {
         if (typeLabel.AsVector().DimCount() != 1 || typeLabel.AsVector().GetDim(0) != 1)
         {
             throw Host.ExceptNotImpl("Not implemented yet for multiple dimensions.");
         }
     }
     if (typeLabel.RawKind() != DataKind.Single)
     {
         throw Host.ExceptNotImpl("InputColumn must be R4.");
     }
     indexTime = SchemaHelper.GetColumnIndex(Source.Schema, _args.timeColumn);
     typeTime  = Source.Schema[indexTime].Type;
     if (typeTime.RawKind() != DataKind.Single)
     {
         throw Host.ExceptNotImpl("Time columne must be R4.");
     }
 }
        public static object GetMissingOrDefaultMissingValue(DataViewType kind, object subcase = null)
        {
            if (kind.IsVector())
            {
                return(null);
            }
            else
            {
                switch (kind.RawKind())
                {
                case DataKind.Boolean:
                    throw new NotSupportedException("No missing value for boolean. Convert to int.");

                case DataKind.Int32:
                    return(int.MinValue);

                case DataKind.UInt32:
                    return(uint.MaxValue);

                case DataKind.Int64:
                    return(long.MinValue);

                case DataKind.Single:
                    return(float.NaN);

                case DataKind.Double:
                    return(double.NaN);

                case DataKind.String:
                    return(subcase is string?(object)(string)null : DvText.NA);

                default:
                    throw new NotImplementedException($"Unknown missing value for type '{kind}'.");
                }
            }
        }
        public static object GetMissingValue(DataViewType kind, object subcase = null)
        {
            if (kind.IsVector())
            {
                return(null);
            }
            else
            {
                switch (kind.RawKind())
                {
                case DataKind.Boolean:
                    throw new NotImplementedException("NA is not available for bool");

                case DataKind.Int32:
                    throw new NotImplementedException("NA is not available for int");

                case DataKind.UInt32:
                    return(0);

                case DataKind.Int64:
                    throw new NotImplementedException("NA is not available for long");

                case DataKind.Single:
                    return(float.NaN);

                case DataKind.Double:
                    return(double.NaN);

                case DataKind.String:
                    return(subcase is string?(object)(string)null : DvText.NA);

                default:
                    throw new NotImplementedException($"Unknown missing value for type '{kind}'.");
                }
            }
        }
Exemple #7
0
        private static ValueGetter <TDst> GetGetterAsCore <TSrc, TDst>(DataViewType typeSrc, DataViewType typeDst, DataViewRow row, DataViewSchema.Column col)
        {
            Contracts.Assert(typeof(TSrc) == typeSrc.RawType);
            Contracts.Assert(typeof(TDst) == typeDst.RawType);

            bool identity;

            if (typeSrc.RawKind() == DataKind.UInt32 && typeDst.RawKind() == DataKind.UInt32)
            {
                var getter = row.GetGetter <uint>(col);
                // Multiclass future issue
                uint plus = (typeDst.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                identity = true;
                var src = default(uint);
                ValueGetter <uint> mapu =
                    (ref uint dst) =>
                {
                    getter(ref src);
                    dst = src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Single && typeDst.RawKind() == DataKind.UInt32)
            {
                var getter = row.GetGetter <float>(col);
                // Multiclass future issue
                uint plus = (typeDst.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                identity = true;
                var src = default(float);
                ValueGetter <uint> mapu =
                    (ref uint dst) =>
                {
                    getter(ref src);
                    dst = (uint)src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Int64 && typeDst.RawKind() == DataKind.UInt64)
            {
                ulong plus   = (typeDst.IsKey() ? (ulong)1 : (ulong)0) - (typeSrc.IsKey() ? (ulong)1 : (ulong)0);
                var   getter = row.GetGetter <long>(col);
                identity = true;
                var src = default(long);
                ValueGetter <ulong> mapu =
                    (ref ulong dst) =>
                {
                    getter(ref src);
                    CheckRange(src, dst);
                    dst = (ulong)src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Single && typeDst.RawKind() == DataKind.UInt64)
            {
                ulong plus   = (ulong)((typeDst.IsKey() ? 1 : 0) - (typeSrc.IsKey() ? 1 : 0));
                var   getter = row.GetGetter <ulong>(col);
                identity = true;
                var src = default(ulong);
                ValueGetter <ulong> mapu =
                    (ref ulong dst) =>
                {
                    getter(ref src);
                    CheckRange(src, dst);
                    dst = (ulong)src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Int64 && typeDst.RawKind() == DataKind.UInt32)
            {
                // Multiclass future issue
                uint plus   = (typeDst.IsKey() ? (uint)1 : (uint)0) - (typeSrc.IsKey() ? (uint)1 : (uint)0);
                var  getter = row.GetGetter <long>(col);
                identity = true;
                var src = default(long);
                ValueGetter <uint> mapu =
                    (ref uint dst) =>
                {
                    getter(ref src);
                    CheckRange(src, dst);
                    dst = (uint)src + plus;
                };
                return(mapu as ValueGetter <TDst>);
            }
            else if (typeSrc.RawKind() == DataKind.Single && typeDst.RawKind() == DataKind.String)
            {
                // Multiclass future issue
                var getter = row.GetGetter <float>(col);
                identity = true;
                var src = default(float);
                ValueGetter <DvText> mapu =
                    (ref DvText dst) =>
                {
                    getter(ref src);
                    dst = new DvText(string.Format("{0}", (int)src));
                };
                return(mapu as ValueGetter <TDst>);
            }
            else
            {
                var getter = row.GetGetter <TSrc>(col);
                var conv   = Conversions.DefaultInstance.GetStandardConversion <TSrc, TDst>(typeSrc, typeDst, out identity);
                if (identity)
                {
                    Contracts.Assert(typeof(TSrc) == typeof(TDst));
                    return((ValueGetter <TDst>)(Delegate) getter);
                }

                var src = default(TSrc);
                return
                    ((ref TDst dst) =>
                {
                    getter(ref src);
                    conv(in src, ref dst);
                });
            }
        }
 public static InternalDataKind DataViewType2Internal(DataViewType kind)
 {
     return(DataKind2Internal(kind.RawKind()));
 }