Skip to content

Fix TextLoader constructor and add exception message #3788

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 4 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,10 +1456,13 @@ internal static TextLoader CreateTextLoader<TInput>(IHostEnvironment host,
var propertyInfos =
userType
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.CanRead && x.CanWrite && x.GetGetMethod() != null && x.GetSetMethod() != null && x.GetIndexParameters().Length == 0);
.Where(x => x.CanRead && x.GetGetMethod() != null && x.GetIndexParameters().Length == 0);

var memberInfos = (fieldInfos as IEnumerable<MemberInfo>).Concat(propertyInfos).ToArray();

if (memberInfos.Length == 0)
throw host.ExceptParam(nameof(TInput), $"Should define at least one public, readable field or property in {nameof(TInput)}.");

var columns = new List<Column>();

for (int index = 0; index < memberInfos.Length; index++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ public static TextLoader CreateTextLoader(this DataOperationsCatalog catalog,
/// <summary>
/// Create a text loader <see cref="TextLoader"/> by inferencing the dataset schema from a data model type.
/// </summary>
/// <typeparam name="TInput">Defines the schema of the data to be loaded. Use public fields or properties
/// decorated with <see cref="LoadColumnAttribute"/> (and possibly other attributes) to specify the column
/// names and their data types in the schema of the loaded data.</typeparam>
/// <param name="catalog">The <see cref="DataOperationsCatalog"/> catalog.</param>
/// <param name="separatorChar">Column separator character. Default is '\t'</param>
/// <param name="hasHeader">Does the file contains header?</param>
/// <param name="dataSample">The optional location of a data sample. The sample can be used to infer column names and number of slots in each column.</param>
/// <param name="dataSample">The optional location of a data sample. The sample can be used to infer information
/// about the columns, such as slot names.</param>
/// <param name="allowQuoting">Whether the input may include quoted values,
/// which can contain separator characters, colons,
/// and distinguish empty values from missing values. When true, consecutive separators
Expand Down
93 changes: 93 additions & 0 deletions test/Microsoft.ML.Tests/TextLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.ML.Data;
using Microsoft.ML.Model;
using Microsoft.ML.RunTests;
Expand Down Expand Up @@ -795,5 +796,97 @@ public void TestTextLoaderKeyTypeBackCompat()
Assert.True(result.Schema[featureIdx].Type is KeyDataViewType keyType && keyType.Count == typeof(uint).ToMaxInt());
}
}

private class IrisNoFields
{
}

private class IrisPrivateFields
{
[LoadColumn(0)]
private float SepalLength;

[LoadColumn(1)]
private float SepalWidth { get; }

public float GetSepalLenght()
=> SepalLength;

public void SetSepalLength(float sepalLength)
{
SepalLength = sepalLength;
}
}
private class IrisPublicGetProperties
{
[LoadColumn(0)]
public float SepalLength { get; }

[LoadColumn(1)]
public float SepalWidth { get; }
}

private class IrisPublicFields
{
public IrisPublicFields(float sepalLength, float sepalWidth)
{
SepalLength = sepalLength;
SepalWidth = sepalWidth;
}

[LoadColumn(0)]
public readonly float SepalLength;

[LoadColumn(1)]
public float SepalWidth;
}

private class IrisPublicProperties
{
[LoadColumn(0)]
public float SepalLength { get; set; }

[LoadColumn(1)]
public float SepalWidth { get; set; }
}

[Fact]
public void TestTextLoaderNoFields()
{
var dataPath = GetDataPath(TestDatasets.irisData.trainFilename);
var mlContext = new MLContext();

// Class with get property only.
var dataIris = mlContext.Data.CreateTextLoader<IrisPublicGetProperties>(separatorChar: ',').Load(dataPath);
var oneIrisData = mlContext.Data.CreateEnumerable<IrisPublicProperties>(dataIris, false).First();
Assert.True(oneIrisData.SepalLength != 0 && oneIrisData.SepalWidth != 0);

// Class with read only fields.
dataIris = mlContext.Data.CreateTextLoader<IrisPublicFields>(separatorChar: ',').Load(dataPath);
oneIrisData = mlContext.Data.CreateEnumerable<IrisPublicProperties>(dataIris, false).First();
Assert.True(oneIrisData.SepalLength != 0 && oneIrisData.SepalWidth != 0);

// Class with no fields.
try
{
dataIris = mlContext.Data.CreateTextLoader<IrisNoFields>(separatorChar: ',').Load(dataPath);
Assert.False(true);
}
catch (Exception ex)
{
Assert.StartsWith("Should define at least one public, readable field or property in TInput.", ex.Message);
}

// Class with no public readable fields.
try
{
dataIris = mlContext.Data.CreateTextLoader<IrisPrivateFields>(separatorChar: ',').Load(dataPath);
Assert.False(true);
}
catch (Exception ex)
{
Assert.StartsWith("Should define at least one public, readable field or property in TInput.", ex.Message);
}
}
}
}