/// <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()}'."); } } }
/// <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()); } }
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)); } }
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}'."); } } }