Skip to content

handle boolean type in construction utils. #183

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
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
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Api/ApiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static OpCode GetAssignmentOpCode(Type t)
// REVIEW: This should be a Dictionary<Type, OpCode> based solution.
// DvTexts, strings, arrays, and VBuffers.
if (t == typeof(DvInt8) || t == typeof(DvInt4) || t == typeof(DvInt2) || t == typeof(DvInt1) ||
t == typeof(DvBool) || t == typeof(DvText) || t == typeof(string) || t.IsArray ||
t == typeof(DvBool) || t==typeof(bool?) || t == typeof(DvText) || t == typeof(string) || t.IsArray ||
(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(VBuffer<>)) || t == typeof(DvDateTime) ||
t == typeof(DvDateTimeZone) || t == typeof(DvTimeSpan) || t == typeof(UInt128))
{
Expand Down
37 changes: 37 additions & 0 deletions src/Microsoft.ML.Api/DataViewConstructionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ private Delegate CreateGetter(int index)
Ch.Assert(colType.IsText);
return CreateStringToTextGetter(index);
}
else if (outputType == typeof(bool))
{
// Bool -> DvBool
Ch.Assert(colType.IsBool);
return CreateBooleanToDvBoolGetter(index);
}
else if (outputType == typeof(bool?))
{
// Bool? -> DvBool
Ch.Assert(colType.IsBool);
return CreateNullableBooleanToDvBoolGetter(index);
}

// T -> T
Ch.Assert(colType.RawType == outputType);
del = CreateDirectGetter<int>;
Expand Down Expand Up @@ -197,6 +210,30 @@ private Delegate CreateStringToTextGetter(int index)
});
}

private Delegate CreateBooleanToDvBoolGetter(int index)
{
var peek = DataView._peeks[index] as Peek<TRow, bool>;
Ch.AssertValue(peek);
bool buf = false;
return (ValueGetter<DvBool>)((ref DvBool dst) =>
{
peek(GetCurrentRowObject(), Position, ref buf);
dst = (DvBool)buf;
});
}

private Delegate CreateNullableBooleanToDvBoolGetter(int index)
{
var peek = DataView._peeks[index] as Peek<TRow, bool?>;
Ch.AssertValue(peek);
bool? buf = null;
return (ValueGetter<DvBool>)((ref DvBool dst) =>
{
peek(GetCurrentRowObject(), Position, ref buf);
dst = buf.HasValue ? (DvBool)buf.Value : DvBool.NA;
});
}

private Delegate CreateArrayToVBufferGetter<TDst>(int index)
{
var peek = DataView._peeks[index] as Peek<TRow, TDst[]>;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/Data/DataKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public static bool TryGetDataKind(this Type type, out DataKind kind)
kind = DataKind.R8;
else if (type == typeof(DvText))
kind = DataKind.TX;
else if (type == typeof(DvBool) || type == typeof(bool))
else if (type == typeof(DvBool) || type == typeof(bool) ||type ==typeof(bool?))
kind = DataKind.BL;
else if (type == typeof(DvTimeSpan))
kind = DataKind.TS;
Expand Down
49 changes: 49 additions & 0 deletions test/Microsoft.ML.Tests/LearningPipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,54 @@ public void NoTransformPipeline()
pipeline.Add(new FastForestBinaryClassifier());
var model = pipeline.Train<Data, Prediction>();
}

public class BooleanLabelData
{
[ColumnName("Features")]
[VectorType(2)]
public float[] Features;

[ColumnName("Label")]
public bool Label;
}

[Fact]
public void BooleanLabelPipeline()
{
var data = new BooleanLabelData[1];
data[0] = new BooleanLabelData();
data[0].Features = new float[] { 0.0f, 1.0f };
data[0].Label = false;
var pipeline = new LearningPipeline();
pipeline.Add(CollectionDataSource.Create(data));
pipeline.Add(new FastForestBinaryClassifier());
var model = pipeline.Train<Data, Prediction>();
}

public class NullableBooleanLabelData
{
[ColumnName("Features")]
[VectorType(2)]
public float[] Features;

[ColumnName("Label")]
public bool? Label;
}

[Fact]
public void NullableBooleanLabelPipeline()
{
var data = new NullableBooleanLabelData[2];
data[0] = new NullableBooleanLabelData();
data[0].Features = new float[] { 0.0f, 1.0f };
data[0].Label = null;
data[1] = new NullableBooleanLabelData();
data[1].Features = new float[] { 1.0f, 0.0f };
data[1].Label = false;
var pipeline = new LearningPipeline();
pipeline.Add(CollectionDataSource.Create(data));
pipeline.Add(new FastForestBinaryClassifier());
var model = pipeline.Train<Data, Prediction>();
}
}
}