/// <summary> /// Read a data view from a text file using <see cref="TextLoader"/>. /// </summary> /// <param name="catalog">The <see cref="DataOperations"/> catalog.</param> /// <param name="path">Specifies a file from which to read.</param> /// <param name="args">Defines the settings of the load operation.</param> public static IDataView ReadFromTextFile(this DataOperations catalog, string path, TextLoader.Arguments args = null) { Contracts.CheckNonEmpty(path, nameof(path)); var env = catalog.GetEnvironment(); var source = new MultiFileSource(path); return(new TextLoader(env, args, source).Read(source)); }
/// <summary> /// Read a data view from a binary file using <see cref="BinaryLoader"/>. /// </summary> /// <param name="catalog">The catalog.</param> /// <param name="path">The path to the file to read from.</param> public static IDataView ReadFromBinary(this DataOperations catalog, string path) { Contracts.CheckNonEmpty(path, nameof(path)); var env = catalog.GetEnvironment(); var reader = new BinaryLoader(env, new BinaryLoader.Arguments(), path); return(reader); }
/// <summary> /// Read a data view from a Stream on a binary file using <see cref="BinaryLoader"/>. /// </summary> /// <param name="catalog">The catalog.</param> /// <param name="stream">The stream to read from.</param> public static IDataView ReadFromBinary(this DataOperations catalog, Stream stream) { Contracts.CheckValue(stream, nameof(stream)); var env = catalog.GetEnvironment(); var reader = new BinaryLoader(env, new BinaryLoader.Arguments(), stream); return(reader); }
/// <summary> /// Read a data view from an <see cref="IMultiStreamSource"/> on a binary file using <see cref="BinaryLoader"/>. /// </summary> /// <param name="catalog">The catalog.</param> /// <param name="fileSource">The file source to read from. This can be a <see cref="MultiFileSource"/>, for example.</param> public static IDataView ReadFromBinary(this DataOperations catalog, IMultiStreamSource fileSource) { Contracts.CheckValue(fileSource, nameof(fileSource)); var env = catalog.GetEnvironment(); var reader = new BinaryLoader(env, new BinaryLoader.Arguments(), fileSource); return(reader); }
/// <summary> /// Read a data view from a text file using <see cref="TextLoader"/>. /// </summary> /// <param name="catalog">The catalog.</param> /// <param name="columns">The columns of the schema.</param> /// <param name="advancedSettings">The delegate to set additional settings</param> /// <param name="path">The path to the file</param> /// <returns>The data view.</returns> public static IDataView ReadFromTextFile(this DataOperations catalog, TextLoader.Column[] columns, string path, Action <TextLoader.Arguments> advancedSettings = null) { Contracts.CheckNonEmpty(path, nameof(path)); var env = catalog.GetEnvironment(); // REVIEW: it is almost always a mistake to have a 'trainable' text loader here. // Therefore, we are going to disallow data sample. var reader = new TextLoader(env, columns, advancedSettings, dataSample: null); return(reader.Read(new MultiFileSource(path))); }
/// <summary> /// Save the data view into a binary stream. /// </summary> /// <param name="catalog">The catalog.</param> /// <param name="data">The data view to save.</param> /// <param name="stream">The stream to write to.</param> /// <param name="keepHidden">Whether to keep hidden columns in the dataset.</param> public static void SaveAsBinary(this DataOperations catalog, IDataView data, Stream stream, bool keepHidden = false) { Contracts.CheckValue(catalog, nameof(catalog)); Contracts.CheckValue(data, nameof(data)); Contracts.CheckValue(stream, nameof(stream)); var env = catalog.GetEnvironment(); var saver = new BinarySaver(env, new BinarySaver.Arguments()); using (var ch = env.Start("Saving data")) DataSaverUtils.SaveDataView(ch, saver, data, stream, keepHidden); }
/// <summary> /// Read a data view from a text file using <see cref="TextLoader"/>. /// </summary> /// <param name="catalog">The catalog.</param> /// <param name="columns">The columns of the schema.</param> /// <param name="hasHeader">Whether the file has a header.</param> /// <param name="separatorChar">The character used as separator between data points in a row. By default the tab character is used as separator.</param> /// <param name="path">The path to the file.</param> /// <returns>The data view.</returns> public static IDataView ReadFromTextFile(this DataOperations catalog, string path, Column[] columns, bool hasHeader = false, char separatorChar = '\t') { Contracts.CheckNonEmpty(path, nameof(path)); var env = catalog.GetEnvironment(); // REVIEW: it is almost always a mistake to have a 'trainable' text loader here. // Therefore, we are going to disallow data sample. var reader = new TextLoader(env, columns, hasHeader, separatorChar, dataSample: null); return(reader.Read(new MultiFileSource(path))); }
/// <summary> /// Save the data view as text. /// </summary> /// <param name="catalog">The catalog.</param> /// <param name="data">The data view to save.</param> /// <param name="stream">The stream to write to.</param> /// <param name="separator">The column separator.</param> /// <param name="headerRow">Whether to write the header row.</param> /// <param name="schema">Whether to write the header comment with the schema.</param> /// <param name="keepHidden">Whether to keep hidden columns in the dataset.</param> public static void SaveAsText(this DataOperations catalog, IDataView data, Stream stream, char separator = '\t', bool headerRow = true, bool schema = true, bool keepHidden = false) { Contracts.CheckValue(catalog, nameof(catalog)); Contracts.CheckValue(data, nameof(data)); Contracts.CheckValue(stream, nameof(stream)); var env = catalog.GetEnvironment(); var saver = new TextSaver(env, new TextSaver.Arguments { Separator = separator.ToString(), OutputHeader = headerRow, OutputSchema = schema }); using (var ch = env.Start("Saving data")) DataSaverUtils.SaveDataView(ch, saver, data, stream, keepHidden); }