Skip to content

Modified how DataViewTypes are registered #4187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Microsoft.ML.Data/Data/DataViewTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static class DataViewTypeManager
/// </summary>
internal static DataViewType GetDataViewType(Type type, IEnumerable<Attribute> typeAttributes = null)
{
//Filter attributes as we only care about DataViewTypeAttributes
typeAttributes = typeAttributes == null ? typeAttributes : typeAttributes.Where(attr => attr.GetType().IsSubclassOf(typeof(DataViewTypeAttribute)));
lock (_lock)
{
// Compute the ID of type with extra attributes.
Expand All @@ -73,6 +75,8 @@ internal static DataViewType GetDataViewType(Type type, IEnumerable<Attribute> t
/// </summary>
internal static bool Knows(Type type, IEnumerable<Attribute> typeAttributes = null)
{
//Filter attributes as we only care about DataViewTypeAttributes
typeAttributes = typeAttributes == null ? typeAttributes : typeAttributes.Where(attr => attr.GetType().IsSubclassOf(typeof(DataViewTypeAttribute)));
lock (_lock)
{
// Compute the ID of type with extra attributes.
Expand Down Expand Up @@ -113,6 +117,16 @@ internal static bool Knows(DataViewType dataViewType)
/// <param name="typeAttributes">The <see cref="Attribute"/>s attached to <paramref name="type"/>.</param>
public static void Register(DataViewType dataViewType, Type type, IEnumerable<Attribute> typeAttributes = null)
{
if (typeAttributes != null)
{
foreach (var attr in typeAttributes)
{
if (!attr.GetType().IsSubclassOf(typeof(DataViewTypeAttribute)))
{
throw Contracts.ExceptParam(nameof(type), $"Type {type} has an attribute that is not of DataViewTypeAttribute.");
}
}
}
lock (_lock)
{
if (_bannedRawTypes.Contains(type))
Expand Down
17 changes: 16 additions & 1 deletion test/Microsoft.ML.Core.Tests/UnitTests/TestCustomTypeRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,26 @@ public void TestTypeManager()
DataViewTypeManager.Register(e, typeof(AlienBody), new[] { f });
Assert.True(DataViewTypeManager.Knows(e));
Assert.True(DataViewTypeManager.Knows(typeof(AlienBody), new[] { f }));
Assert.True(DataViewTypeManager.Knows(typeof(AlienBody), new[] { f }));
// "e" is associated with typeof(AlienBody) with "f," so the call below should return true.
Assert.Equal(e, DataViewTypeManager.GetDataViewType(typeof(AlienBody), new[] { f }));
// "a" is associated with typeof(AlienBody) without any attribute, so the call below should return false.
Assert.NotEqual(a, DataViewTypeManager.GetDataViewType(typeof(AlienBody), new[] { f }));
}

[Fact]
public void RegisterTypeWithAdditionalAttributes()
{
var a = new DataViewAlienBodyType(7788);
var b = new AlienTypeAttributeAttribute(8877);
var c = new ColumnNameAttribute("SomeName");

Assert.Throws<ArgumentOutOfRangeException>(() => DataViewTypeManager.Register(a, typeof(AlienBody), new Attribute[] { b, c }));

DataViewTypeManager.Register(a, typeof(AlienBody), new Attribute[] { b });
Assert.True(DataViewTypeManager.Knows(a));
Assert.True(DataViewTypeManager.Knows(typeof(AlienBody), new Attribute[] { b, c }));
// "a" is associated with typeof(AlienBody) with "b," so the call below should return true.
Assert.Equal(a, DataViewTypeManager.GetDataViewType(typeof(AlienBody), new Attribute[] { b, c }));
}
}
}
67 changes: 67 additions & 0 deletions test/Microsoft.ML.Tests/OnnxSequenceTypeWithAttributesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 System.Drawing;
using Microsoft.ML.Data;
using Microsoft.ML.RunTests;
using Microsoft.ML.Transforms.Image;
using Microsoft.ML.Transforms.Onnx;
using Xunit;
using Xunit.Abstractions;
using System.Linq;
using System.IO;

namespace Microsoft.ML.Tests
{
public class OnnxSequenceTypeWithAttributesTest : BaseTestBaseline
{
public class OutputObj
{
[ColumnName("output")]
[OnnxSequenceType(typeof(IDictionary<string, float>))]
public IEnumerable<IDictionary<string, float>> Output;
}
public class FloatInput
{
[ColumnName("input")]
[VectorType(3)]
public float[] Input { get; set; }
}

public OnnxSequenceTypeWithAttributesTest(ITestOutputHelper output) : base(output)
{
}
public static PredictionEngine<FloatInput, OutputObj> LoadModel(string onnxModelFilePath)
{
var ctx = new MLContext();
var dataView = ctx.Data.LoadFromEnumerable(new List<FloatInput>());

var pipeline = ctx.Transforms.ApplyOnnxModel(
modelFile: onnxModelFilePath,
outputColumnNames: new[] { "output" }, inputColumnNames: new[] { "input" });

var model = pipeline.Fit(dataView);
return ctx.Model.CreatePredictionEngine<FloatInput, OutputObj>(model);
}

[Fact]
public void OnnxSequenceTypeWithColumnNameAttributeTest()
{
var modelFile = @"zipmap/TestZipMapString.onnx";
var predictor = LoadModel(modelFile);

FloatInput input = new FloatInput() { Input = new float[] { 1.0f, 2.0f, 3.0f } };
var output = predictor.Predict(input);
var onnx_out = output.Output.FirstOrDefault();
Assert.True(onnx_out.Count == 3, "Output missing data.");
var keys = new List<string>(onnx_out.Keys);
for(var i =0; i < onnx_out.Count; ++i)
{
Assert.Equal(onnx_out[keys[i]], input.Input[i]);
}

}
}
}