Skip to content

Commit ca5b116

Browse files
committed
Rename new class to CustomMappingFactory.
1 parent f321116 commit ca5b116

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

docs/code/MlNetCookBook.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -977,17 +977,17 @@ Here is a complete example that saves and loads a model with a custom mapping.
977977
/// <summary>
978978
/// One class that contains the custom mapping functionality that we need for our model.
979979
///
980-
/// It has a [CustomMappingTransformerFactoryAttribute] on it and
981-
/// derives from CustomMappingTransformerFactory{TSrc, TDst}.
980+
/// It has a <see cref="CustomMappingFactoryAttributeAttribute"/> on it and
981+
/// derives from <see cref="CustomMappingFactory{TSrc, TDst}"/>.
982982
/// </summary>
983-
[CustomMappingTransformerFactory(nameof(CustomMappings.IncomeMapping))]
984-
public class CustomMappings : CustomMappingTransformerFactory<InputRow, OutputRow>
983+
[CustomMappingFactoryAttribute(nameof(CustomMappings.IncomeMapping))]
984+
public class CustomMappings : CustomMappingFactory<InputRow, OutputRow>
985985
{
986986
// This is the custom mapping. We now separate it into a method, so that we can use it both in training and in loading.
987987
public static void IncomeMapping(InputRow input, OutputRow output) => output.Label = input.Income > 50000;
988988

989989
// This factory method will be called when loading the model to get the mapping operation.
990-
public override Action<InputRow, OutputRow> GetTransformer()
990+
public override Action<InputRow, OutputRow> GetMapping()
991991
{
992992
return IncomeMapping;
993993
}

src/Microsoft.ML.Transforms/CustomMappingTransformerFactory.cs renamed to src/Microsoft.ML.Transforms/CustomMappingFactory.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,39 @@
88
namespace Microsoft.ML.Transforms
99
{
1010
/// <summary>
11-
/// Place this attribute onto a type to cause it to be considered a custom mapping transformer factory.
11+
/// Place this attribute onto a type to cause it to be considered a custom mapping factory.
1212
/// </summary>
1313
[AttributeUsage(AttributeTargets.Class)]
14-
public sealed class CustomMappingTransformerFactoryAttribute : ExtensionBaseAttribute
14+
public sealed class CustomMappingFactoryAttributeAttribute : ExtensionBaseAttribute
1515
{
16-
public CustomMappingTransformerFactoryAttribute(string contractName)
16+
public CustomMappingFactoryAttributeAttribute(string contractName)
1717
: base(contractName)
1818
{
1919
}
2020
}
2121

22-
internal interface ICustomMappingTransformerFactory
22+
internal interface ICustomMappingFactory
2323
{
24-
ITransformer CreateTransformerObject(IHostEnvironment env, string contractName);
24+
ITransformer CreateTransformer(IHostEnvironment env, string contractName);
2525
}
2626

2727
/// <summary>
28-
/// The base type for custom mapping transformer factories.
28+
/// The base type for custom mapping factories.
2929
/// </summary>
3030
/// <typeparam name="TSrc">The type that describes what 'source' columns are consumed from the input <see cref="IDataView"/>.</typeparam>
3131
/// <typeparam name="TDst">The type that describes what new columns are added by this transform.</typeparam>
32-
public abstract class CustomMappingTransformerFactory<TSrc, TDst> : ICustomMappingTransformerFactory
32+
public abstract class CustomMappingFactory<TSrc, TDst> : ICustomMappingFactory
3333
where TSrc : class, new()
3434
where TDst : class, new()
3535
{
36-
public abstract Action<TSrc, TDst> GetTransformer();
36+
/// <summary>
37+
/// Returns the mapping delegate that maps from <typeparamref name="TSrc"/> inputs to <typeparamref name="TDst"/> outputs.
38+
/// </summary>
39+
public abstract Action<TSrc, TDst> GetMapping();
3740

38-
ITransformer ICustomMappingTransformerFactory.CreateTransformerObject(IHostEnvironment env, string contractName)
41+
ITransformer ICustomMappingFactory.CreateTransformer(IHostEnvironment env, string contractName)
3942
{
40-
Action<TSrc, TDst> mapAction = GetTransformer();
43+
Action<TSrc, TDst> mapAction = GetMapping();
4144
return new CustomMappingTransformer<TSrc, TDst>(env, mapAction, contractName);
4245
}
4346
}

src/Microsoft.ML.Transforms/LambdaTransform.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ private static ITransformer Create(IHostEnvironment env, ModelLoadContext ctx)
6868

6969
var contractName = ctx.LoadString();
7070

71-
object factoryObject = env.ComponentCatalog.GetExtensionValue(env, typeof(CustomMappingTransformerFactoryAttribute), contractName);
72-
if (!(factoryObject is ICustomMappingTransformerFactory transformerFactory))
71+
object factoryObject = env.ComponentCatalog.GetExtensionValue(env, typeof(CustomMappingFactoryAttributeAttribute), contractName);
72+
if (!(factoryObject is ICustomMappingFactory mappingFactory))
7373
{
74-
throw env.Except($"The class with contract '{contractName}' must derive from '{typeof(CustomMappingTransformerFactory<,>).FullName}'.");
74+
throw env.Except($"The class with contract '{contractName}' must derive from '{typeof(CustomMappingFactory<,>).FullName}'.");
7575
}
7676

77-
return transformerFactory.CreateTransformerObject(env, contractName);
77+
return mappingFactory.CreateTransformer(env, contractName);
7878
}
7979

8080
/// <summary>

test/Microsoft.ML.Tests/Scenarios/Api/CookbookSamples/CookbookSamplesDynamicApi.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,17 +492,17 @@ public void CustomTransformer()
492492
/// <summary>
493493
/// One class that contains the custom mapping functionality that we need for our model.
494494
///
495-
/// It has a <see cref="CustomMappingTransformerFactoryAttribute"/> on it and
496-
/// derives from <see cref="CustomMappingTransformerFactory{TSrc, TDst}"/>.
495+
/// It has a <see cref="CustomMappingFactoryAttributeAttribute"/> on it and
496+
/// derives from <see cref="CustomMappingFactory{TSrc, TDst}"/>.
497497
/// </summary>
498-
[CustomMappingTransformerFactory(nameof(CustomMappings.IncomeMapping))]
499-
public class CustomMappings : CustomMappingTransformerFactory<InputRow, OutputRow>
498+
[CustomMappingFactoryAttribute(nameof(CustomMappings.IncomeMapping))]
499+
public class CustomMappings : CustomMappingFactory<InputRow, OutputRow>
500500
{
501501
// This is the custom mapping. We now separate it into a method, so that we can use it both in training and in loading.
502502
public static void IncomeMapping(InputRow input, OutputRow output) => output.Label = input.Income > 50000;
503503

504504
// This factory method will be called when loading the model to get the mapping operation.
505-
public override Action<InputRow, OutputRow> GetTransformer()
505+
public override Action<InputRow, OutputRow> GetMapping()
506506
{
507507
return IncomeMapping;
508508
}

test/Microsoft.ML.Tests/Transformers/CustomMappingTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ public class MyOutput
3030
public string Together { get; set; }
3131
}
3232

33-
[CustomMappingTransformerFactory("MyLambda")]
34-
public class MyLambda : CustomMappingTransformerFactory<MyInput, MyOutput>
33+
[CustomMappingFactoryAttribute("MyLambda")]
34+
public class MyLambda : CustomMappingFactory<MyInput, MyOutput>
3535
{
3636
public static void MyAction(MyInput input, MyOutput output)
3737
{
3838
output.Together = $"{input.Float1} + {string.Join(", ", input.Float4)}";
3939
}
4040

41-
public override Action<MyInput, MyOutput> GetTransformer()
41+
public override Action<MyInput, MyOutput> GetMapping()
4242
{
4343
return MyAction;
4444
}

0 commit comments

Comments
 (0)