|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.ML.Runtime.Api; |
| 6 | +using Microsoft.ML.Runtime.Data; |
| 7 | +using Microsoft.ML.Runtime.Data.IO; |
| 8 | +using Microsoft.ML.Runtime.Model; |
| 9 | +using Microsoft.ML.Runtime.RunTests; |
| 10 | +using Microsoft.ML.Runtime.Tools; |
| 11 | +using System.IO; |
| 12 | +using Xunit; |
| 13 | +using Xunit.Abstractions; |
| 14 | + |
| 15 | +namespace Microsoft.ML.Tests.Transformers |
| 16 | +{ |
| 17 | + public sealed class ConcatTests : TestDataPipeBase |
| 18 | + { |
| 19 | + public ConcatTests(ITestOutputHelper output) : base(output) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + void TestConcat() |
| 25 | + { |
| 26 | + string dataPath = GetDataPath("adult.test"); |
| 27 | + |
| 28 | + var source = new MultiFileSource(dataPath); |
| 29 | + var loader = new TextLoader(Env, new TextLoader.Arguments |
| 30 | + { |
| 31 | + Column = new[]{ |
| 32 | + new TextLoader.Column("float1", DataKind.R4, 0), |
| 33 | + new TextLoader.Column("float4", DataKind.R4, new[]{new TextLoader.Range(0), new TextLoader.Range(2), new TextLoader.Range(4), new TextLoader.Range(10) }), |
| 34 | + new TextLoader.Column("vfloat", DataKind.R4, new[]{new TextLoader.Range(0), new TextLoader.Range(2), new TextLoader.Range(4), new TextLoader.Range(10, null) { AutoEnd = false, VariableEnd = true } }) |
| 35 | + }, |
| 36 | + Separator = ",", |
| 37 | + HasHeader = true |
| 38 | + }, new MultiFileSource(dataPath)); |
| 39 | + var data = loader.Read(source); |
| 40 | + |
| 41 | + ColumnType GetType(ISchema schema, string name) |
| 42 | + { |
| 43 | + Assert.True(schema.TryGetColumnIndex(name, out int cIdx), $"Could not find '{name}'"); |
| 44 | + return schema.GetColumnType(cIdx); |
| 45 | + } |
| 46 | + var pipe = new ConcatEstimator(Env, "f1", "float1") |
| 47 | + .Append(new ConcatEstimator(Env, "f2", "float1", "float1")) |
| 48 | + .Append(new ConcatEstimator(Env, "f3", "float4", "float1")) |
| 49 | + .Append(new ConcatEstimator(Env, "f4", "vfloat", "float1")); |
| 50 | + |
| 51 | + data = TakeFilter.Create(Env, data, 10); |
| 52 | + data = pipe.Fit(data).Transform(data); |
| 53 | + |
| 54 | + ColumnType t; |
| 55 | + t = GetType(data.Schema, "f1"); |
| 56 | + Assert.True(t.IsVector && t.ItemType == NumberType.R4 && t.VectorSize == 1); |
| 57 | + t = GetType(data.Schema, "f2"); |
| 58 | + Assert.True(t.IsVector && t.ItemType == NumberType.R4 && t.VectorSize == 2); |
| 59 | + t = GetType(data.Schema, "f3"); |
| 60 | + Assert.True(t.IsVector && t.ItemType == NumberType.R4 && t.VectorSize == 5); |
| 61 | + t = GetType(data.Schema, "f4"); |
| 62 | + Assert.True(t.IsVector && t.ItemType == NumberType.R4 && t.VectorSize == 0); |
| 63 | + |
| 64 | + data = new ChooseColumnsTransform(Env, data, "f1", "f2", "f3", "f4"); |
| 65 | + |
| 66 | + var subdir = Path.Combine("Transform", "Concat"); |
| 67 | + var outputPath = GetOutputPath(subdir, "Concat1.tsv"); |
| 68 | + using (var ch = Env.Start("save")) |
| 69 | + { |
| 70 | + var saver = new TextSaver(Env, new TextSaver.Arguments { Silent = true, Dense = true }); |
| 71 | + using (var fs = File.Create(outputPath)) |
| 72 | + DataSaverUtils.SaveDataView(ch, saver, data, fs, keepHidden: false); |
| 73 | + } |
| 74 | + |
| 75 | + CheckEquality(subdir, "Concat1.tsv"); |
| 76 | + Done(); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments