/// <summary> /// Take <paramref name="count"/> rows from <paramref name="input"/>. /// </summary> /// <remarks> /// Returns returns an <see cref="IDataView"/> with the first <paramref name="count"/> rows from <paramref name="input"/>. /// </remarks> /// <param name="input">The input data.</param> /// <param name="count">Number of rows to take.</param> /// <example> /// <format type="text/markdown"> /// <![CDATA[ /// [!code-csharp[TakeRows](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/DataOperations/TakeRows.cs)] /// ]]> /// </format> /// </example> public IDataView TakeRows(IDataView input, long count) { _env.CheckValue(input, nameof(input)); _env.CheckUserArg(count > 0, nameof(count), "Must be greater than zero."); var options = new SkipTakeFilter.TakeOptions() { Count = count }; return(new SkipTakeFilter(_env, options, input)); }
public static CommonOutputs.TransformOutput TakeFilter(IHostEnvironment env, SkipTakeFilter.TakeOptions input) { Contracts.CheckValue(env, nameof(env)); var host = env.Register("TakeFilter"); host.CheckValue(input, nameof(input)); EntryPointUtils.CheckInputArgs(host, input); var xf = SkipTakeFilter.Create(host, input, input.Data); return(new CommonOutputs.TransformOutput { Model = new TransformModelImpl(env, xf, input.Data), OutputData = xf }); }
private void RunCore(IChannel ch) { Host.AssertValue(ch); IDataView data = CreateAndSaveLoader(); if (!string.IsNullOrWhiteSpace(ImplOptions.Columns)) { var keepColumns = ImplOptions.Columns .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); if (Utils.Size(keepColumns) > 0) { data = ColumnSelectingTransformer.CreateKeep(Host, data, keepColumns); } } IDataSaver saver; if (ImplOptions.Saver != null) { saver = ImplOptions.Saver.CreateComponent(Host); } else { saver = new TextSaver(Host, new TextSaver.Arguments() { Dense = ImplOptions.Dense }); } var cols = new List <int>(); for (int i = 0; i < data.Schema.Count; i++) { if (!ImplOptions.KeepHidden && data.Schema[i].IsHidden) { continue; } var type = data.Schema[i].Type; if (saver.IsColumnSavable(type)) { cols.Add(i); } else { ch.Info(MessageSensitivity.Schema, "The column '{0}' will not be written as it has unsavable column type.", data.Schema[i].Name); } } Host.NotSensitive().Check(cols.Count > 0, "No valid columns to save"); // Send the first N lines to console. if (ImplOptions.Rows > 0) { var args = new SkipTakeFilter.TakeOptions() { Count = ImplOptions.Rows }; data = SkipTakeFilter.Create(Host, args, data); } var textSaver = saver as TextSaver; // If it is a text saver, utilize a special utility for this purpose. if (textSaver != null) { textSaver.WriteData(data, true, cols.ToArray()); } else { using (MemoryStream mem = new MemoryStream()) { using (Stream wrapStream = new SubsetStream(mem)) saver.SaveData(wrapStream, data, cols.ToArray()); mem.Seek(0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader(mem)) { string result = reader.ReadToEnd(); ch.Info(MessageSensitivity.UserData | MessageSensitivity.Schema, result); } } } }