Skip to content

Commit

Permalink
API to return ColumnPairs for a OneToOneTransformer. (#3088)
Browse files Browse the repository at this point in the history
* API to return ColumnPairs for a transformer.

* PR feedback.

* PR feedback.
  • Loading branch information
codemzs authored Mar 26, 2019
1 parent 7cc1b89 commit a18be69
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/Microsoft.ML.Data/Transforms/ExtensionsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using Microsoft.ML.Data;
using Microsoft.ML.Runtime;
Expand Down Expand Up @@ -36,10 +37,12 @@ public InputOutputColumnPair(string outputColumnName, string inputColumnName = n
}

[BestFriend]
internal static (string outputColumnName, string inputColumnName)[] ConvertToValueTuples(InputOutputColumnPair[] infos)
{
return infos.Select(info => (info.OutputColumnName, info.InputColumnName)).ToArray();
}
internal static (string outputColumnName, string inputColumnName)[] ConvertToValueTuples(InputOutputColumnPair[] infos) =>
infos.Select(info => (info.OutputColumnName, info.InputColumnName)).ToArray();

[BestFriend]
internal static IReadOnlyList<InputOutputColumnPair> ConvertFromValueTuples((string outputColumnName, string inputColumnName)[] infos) =>
infos.Select(info => new InputOutputColumnPair(info.outputColumnName, info.inputColumnName)).ToList().AsReadOnly();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.ML.Data
public abstract class OneToOneTransformerBase : RowToRowTransformerBase
{
[BestFriend]
private protected readonly (string outputColumnName, string inputColumnName)[] ColumnPairs;
internal readonly (string outputColumnName, string inputColumnName)[] ColumnPairs;

[BestFriend]
private protected OneToOneTransformerBase(IHost host, params (string outputColumnName, string inputColumnName)[] columns) : base(host)
Expand Down
18 changes: 18 additions & 0 deletions src/Microsoft.ML.Experimental/OneToOneTransformerBaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Microsoft.ML.Data;

namespace Microsoft.ML.Experimental
{
public static class OneToOneTransformerBaseExtensions
{
/// <summary>
/// Returns the names of the input-output column pairs on which the transformation is applied.
/// </summary>
public static IReadOnlyList<InputOutputColumnPair> GetColumnPairs(this OneToOneTransformerBase transformer) =>
InputOutputColumnPair.ConvertFromValueTuples(transformer.ColumnPairs);
}
}
24 changes: 24 additions & 0 deletions test/Microsoft.ML.Tests/Transformers/NormalizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,30 @@ public void NormalizerExperimentalExtensions()
Done();
}

[Fact]
public void NormalizerExperimentalExtensionGetColumnPairs()
{
string dataPath = GetDataPath(TestDatasets.iris.trainFilename);

var loader = new TextLoader(Env, new TextLoader.Options
{
Columns = new[] {
new TextLoader.Column("Label", DataKind.Single, 0),
new TextLoader.Column("input", DataKind.Single, new[]{new TextLoader.Range(1, 4) }),
}
});

var data = loader.Load(dataPath);
var est = ML.Transforms.Normalize("output", "input", NormalizingEstimator.NormalizationMode.MinMax);
var t = est.Fit(data);

Assert.Single(t.GetColumnPairs());
Assert.Equal("output", t.GetColumnPairs()[0].OutputColumnName);
Assert.Equal("input", t.GetColumnPairs()[0].InputColumnName);

Done();
}

[Fact]
public void LpGcNormAndWhiteningWorkout()
{
Expand Down

0 comments on commit a18be69

Please sign in to comment.