Skip to content

Commit 9dfaa38

Browse files
yaeldMSTomFinley
authored andcommitted
Fix bug in TextLoader throwing null exception in some conditions (#3056)
1 parent db4ecc0 commit 9dfaa38

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,12 +1288,9 @@ private static bool TryParseSchema(IHost host, IMultiStreamSource files,
12881288
ch.Assert(h.Loader == null || h.Loader is ICommandLineComponentFactory);
12891289
var loader = h.Loader as ICommandLineComponentFactory;
12901290

1291-
if (loader == null || string.IsNullOrWhiteSpace(loader.Name))
1292-
goto LDone;
1293-
1294-
// Make sure the loader binds to us.
1295-
var info = host.ComponentCatalog.GetLoadableClassInfo<SignatureDataLoader>(loader.Name);
1296-
if (info.Type != typeof(ILegacyDataLoader) || info.ArgType != typeof(Options))
1291+
// Make sure that the schema is described using either the syntax TextLoader{<settings>} or the syntax Text{<settings>},
1292+
// where "settings" is a string that can be parsed by CmdParser into an object of type TextLoader.Options.
1293+
if (loader == null || string.IsNullOrWhiteSpace(loader.Name) || (loader.Name != LoaderSignature && loader.Name != "Text"))
12971294
goto LDone;
12981295

12991296
var optionsNew = new Options();

test/Microsoft.ML.Tests/TextLoaderTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,19 @@ public void ThrowsExceptionWithPropertyName()
598598
catch (NullReferenceException) { };
599599
}
600600

601+
[Fact]
602+
public void ParseSchemaFromTextFile()
603+
{
604+
var mlContext = new MLContext(seed: 1);
605+
var fileName = GetDataPath(TestDatasets.adult.trainFilename);
606+
var loader = mlContext.Data.CreateTextLoader(new TextLoader.Options(), new MultiFileSource(fileName));
607+
var data = loader.Load(new MultiFileSource(fileName));
608+
Assert.NotNull(data.Schema.GetColumnOrNull("Label"));
609+
Assert.NotNull(data.Schema.GetColumnOrNull("Workclass"));
610+
Assert.NotNull(data.Schema.GetColumnOrNull("Categories"));
611+
Assert.NotNull(data.Schema.GetColumnOrNull("NumericFeatures"));
612+
}
613+
601614
public class QuoteInput
602615
{
603616
[LoadColumn(0)]

0 commit comments

Comments
 (0)