-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathValueMapping.cs
More file actions
1294 lines (1154 loc) · 67.3 KB
/
Copy pathValueMapping.cs
File metadata and controls
1294 lines (1154 loc) · 67.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.ML;
using Microsoft.ML.CommandLine;
using Microsoft.ML.Data;
using Microsoft.ML.Data.IO;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.Model.OnnxConverter;
using Microsoft.ML.Runtime;
using Microsoft.ML.Transforms;
[assembly: LoadableClass(ValueMappingTransformer.Summary, typeof(IDataTransform), typeof(ValueMappingTransformer),
typeof(ValueMappingTransformer.Options), typeof(SignatureDataTransform),
ValueMappingTransformer.UserName, "ValueMapping", "ValueMappingTransformer", ValueMappingTransformer.ShortName,
"TermLookup", "Lookup", "LookupTransform", DocName = "transform/ValueMappingTransformer.md")]
[assembly: LoadableClass(ValueMappingTransformer.Summary, typeof(IDataTransform), typeof(ValueMappingTransformer), null, typeof(SignatureLoadDataTransform),
"Value Mapping Transform", ValueMappingTransformer.LoaderSignature, ValueMappingTransformer.TermLookupLoaderSignature)]
[assembly: LoadableClass(ValueMappingTransformer.Summary, typeof(ValueMappingTransformer), null, typeof(SignatureLoadModel),
"Value Mapping Transform", ValueMappingTransformer.LoaderSignature)]
[assembly: LoadableClass(typeof(IRowMapper), typeof(ValueMappingTransformer), null, typeof(SignatureLoadRowMapper),
ValueMappingTransformer.UserName, ValueMappingTransformer.LoaderSignature)]
namespace Microsoft.ML.Transforms
{
/// <summary>
/// Estimator for <see cref="ValueMappingTransformer"/> creating a key-value map using the pairs of values in the input data
/// <see cref="PrimitiveDataViewType"/>
/// </summary>
/// <remarks>
/// <format type="text/markdown">< and [key](xref:Microsoft.ML.Data.KeyDataViewType) type. |
/// | Output column data type | Vector or primitive numeric, boolean, text, [System.DateTime](xref:System.DateTime) and [key](xref:Microsoft.ML.Data.KeyDataViewType) type. |
/// | Exportable to ONNX | No |
///
/// Given two sets of values, one serving as the key, and the other as the value of a Dictionary, the ValueMappingEstimator builds up this dictionary so that when given a specific key it will return a
/// specific value.The ValueMappingEstimator supports keys and values of different [System.Type](xref:System.Type) to support different data types.
/// Examples for using a ValueMappingEstimator are:
/// * Converting a string value to a string value, this can be useful for grouping (i.e. 'cat', 'dog', 'horse' maps to 'mammals').
/// * Converting a string value to a integer value (i.e. converting the text description like quality to an numeric where 'good' maps to 1, 'poor' maps to 0.
/// * Converting a integer value to a string value and have the string value represented as a [key](xref:Microsoft.ML.Data.KeyDataViewType) type.
/// (i.e. convert zip codes to a state string value, which will generate a unique integer value that can be used as a label.
///
/// Values can be repeated to allow for multiple keys to map to the same value, however keys can not be repeated. The mapping between keys and values
/// can be specified either through lists, where the key list and value list must be the same size or can be done through an [System.IDataView](xref:Microsoft.ML.IDataView).
///
/// Check the See Also section for links to usage examples.
/// ]]></format>
/// </remarks>
/// <seealso cref="ConversionsExtensionsCatalog.MapValue(TransformsCatalog.ConversionTransforms, string, IDataView, DataViewSchema.Column, DataViewSchema.Column, string)"/>
public class ValueMappingEstimator : TrivialEstimator<ValueMappingTransformer>
{
private readonly (string outputColumnName, string inputColumnName)[] _columns;
/// <summary>
/// Constructs the ValueMappingEstimator, key type -> value type mapping
/// </summary>
/// <param name="env">The environment to use.</param>
/// <param name="lookupMap">An instance of <see cref="IDataView"/> that contains the key and value columns.</param>
/// <param name="keyColumn">Name of the key column in <paramref name="lookupMap"/>.</param>
/// <param name="valueColumn">Name of the value column in <paramref name="lookupMap"/>.</param>
/// <param name="columns">The list of names of the input columns to apply the transformation, and the name of the resulting column.</param>
internal ValueMappingEstimator(IHostEnvironment env, IDataView lookupMap, DataViewSchema.Column keyColumn, DataViewSchema.Column valueColumn, params (string outputColumnName, string inputColumnName)[] columns)
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(ValueMappingEstimator)),
new ValueMappingTransformer(env, lookupMap, keyColumn, valueColumn, columns))
{
_columns = columns;
}
/// <summary>
/// Returns the <see cref="SchemaShape"/> of the schema which will be produced by the transformer.
/// Used for schema propagation and verification in a pipeline.
/// </summary>
public sealed override SchemaShape GetOutputSchema(SchemaShape inputSchema)
{
Host.CheckValue(inputSchema, nameof(inputSchema));
var resultDic = inputSchema.ToDictionary(x => x.Name);
// Decide the output VectorKind for the columns
// based on the value type of the dictionary
var vectorKind = SchemaShape.Column.VectorKind.Scalar;
if (Transformer.ValueColumnType is VectorDataViewType)
{
vectorKind = SchemaShape.Column.VectorKind.Vector;
if (Transformer.ValueColumnType.GetVectorSize() == 0)
vectorKind = SchemaShape.Column.VectorKind.VariableVector;
}
// Set the data type of the output column
// if the output VectorKind is a vector or variable vector then
// this is the data type of items stored in the vector.
var isKey = Transformer.ValueColumnType is KeyDataViewType;
var columnType = (isKey) ? NumberDataViewType.UInt32 :
Transformer.ValueColumnType.GetItemType();
var metadataShape = SchemaShape.Create(Transformer.ValueColumnMetadata.Schema);
foreach (var (outputColumnName, inputColumnName) in _columns)
{
if (!inputSchema.TryFindColumn(inputColumnName, out var originalColumn))
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "input", inputColumnName);
if (originalColumn.Kind == SchemaShape.Column.VectorKind.VariableVector ||
originalColumn.Kind == SchemaShape.Column.VectorKind.Vector)
{
if (Transformer.ValueColumnType is VectorDataViewType)
throw Host.ExceptNotSupp("Column '{0}' cannot be mapped to values when the column and the map values are both vector type.", inputColumnName);
// if input to the estimator is of vector type then output should always be vector.
// The transformer maps each item in input vector to the values in the dictionary
// producing a vector as output.
vectorKind = originalColumn.Kind;
}
// Create the Value column
var col = new SchemaShape.Column(outputColumnName, vectorKind, columnType, isKey, metadataShape);
resultDic[outputColumnName] = col;
}
return new SchemaShape(resultDic.Values);
}
}
/// <summary>
/// Estimator for <see cref="ValueMappingTransformer"/> creating a key-value map using the pairs of values in the input data
/// <see cref="PrimitiveDataViewType"/>
/// </summary>
/// <remarks>
/// <format type="text/markdown">< and [key](xref:Microsoft.ML.Data.KeyDataViewType) type.|
/// | Output column data type | Vector or primitive numeric, boolean, text, [System.DateTime](xref:System.DateTime) and [key](xref:Microsoft.ML.Data.KeyDataViewType) type.|
/// | Exportable to ONNX | No |
///
/// Given two sets of values, one serving as the key, and the other as the value of a Dictionary, the ValueMappingEstimator builds up this dictionary so that when given a specific key it will return a
/// specific value.The ValueMappingEstimator supports keys and values of different [System.Type](xref:System.Type) to support different data types.
/// Examples for using a ValueMappingEstimator are:
/// * Converting a string value to a string value, this can be useful for grouping (i.e. 'cat', 'dog', 'horse' maps to 'mammals').
/// * Converting a string value to a integer value (i.e. converting the text description like quality to an numeric where 'good' maps to 1, 'poor' maps to 0.
/// * Converting a integer value to a string value and have the string value represented as a [key](xref:Microsoft.ML.Data.KeyDataViewType) type.
/// (i.e. convert zip codes to a state string value, which will generate a unique integer value that can be used as a label.
///
/// Values can be repeated to allow for multiple keys to map to the same value, however keys can not be repeated. The mapping between keys and values
/// can be specified either through lists, where the key list and value list must be the same size or can be done through an [System.IDataView](xref:Microsoft.ML.IDataView).
///
/// Check the See Also section for links to usage examples.
/// ]]></format>
/// </remarks>
/// <typeparam name="TKey">Specifies the key type.</typeparam>
/// <typeparam name="TValue">Specifies the value type.</typeparam>
/// <seealso cref="ConversionsExtensionsCatalog.MapValue{TInputType, TOutputType}(TransformsCatalog.ConversionTransforms, string, IEnumerable{KeyValuePair{TInputType, TOutputType}}, string, bool)"/>
public sealed class ValueMappingEstimator<TKey, TValue> : ValueMappingEstimator
{
/// <summary>
/// Constructs the ValueMappingEstimator, key type -> value array type mapping
/// </summary>
/// <param name="env">The environment to use.</param>
/// <param name="lookupMap">A <see cref="IDataView"/> containing key column and value column.</param>
/// <param name="keyColumn">The key column in <paramref name="lookupMap"/>.</param>
/// <param name="valueColumn">The value column in <paramref name="lookupMap"/>.</param>
/// <param name="columns">The list of columns to apply.</param>
internal ValueMappingEstimator(IHostEnvironment env, IDataView lookupMap, DataViewSchema.Column keyColumn, DataViewSchema.Column valueColumn, params (string outputColumnName, string inputColumnName)[] columns)
: base(env, lookupMap, keyColumn, valueColumn, columns)
{
}
}
/// <summary>
/// The DataViewHelper provides a set of static functions to create a DataView given a list of keys and values.
/// </summary>
internal class DataViewHelper
{
/// <summary>
/// Helper function to retrieve the Primitive type given a Type
/// </summary>
internal static PrimitiveDataViewType GetPrimitiveType(Type rawType, out bool isVectorType)
{
Type type = rawType;
isVectorType = false;
if (type.IsArray)
{
type = rawType.GetElementType();
isVectorType = true;
}
if (!type.TryGetDataKind(out InternalDataKind kind))
throw new InvalidOperationException($"Unsupported type {type} used in mapping.");
return ColumnTypeExtensions.PrimitiveTypeFromKind(kind);
}
/// <summary>
/// Helper function for a reverse lookup given value. This is used for generating the metadata of the value column.
/// </summary>
private static ValueGetter<VBuffer<ReadOnlyMemory<char>>> GetKeyValueGetter<TKey>(TKey[] keys)
{
return
(ref VBuffer<ReadOnlyMemory<char>> dst) =>
{
var editor = VBufferEditor.Create(ref dst, keys.Length);
for (int i = 0; i < keys.Length; i++)
editor.Values[i] = keys[i].ToString().AsMemory();
dst = editor.Commit();
};
}
/// <summary>
/// Helper function to add a column to an ArrayDataViewBuilder. This handles the case if the type is a string.
/// </summary>
internal static void AddColumnWrapper<T>(ArrayDataViewBuilder builder, string columnName, PrimitiveDataViewType primitiveType, T[] values)
{
if (typeof(T) == typeof(string))
builder.AddColumn(columnName, values.Select(x => x.ToString()).ToArray());
else
builder.AddColumn(columnName, primitiveType, values);
}
/// <summary>
/// Helper function to add a column to an ArrayDataViewBuilder. This handles the case if the type is an array of strings.
/// </summary>
internal static void AddColumnWrapper<T>(ArrayDataViewBuilder builder, string columnName, PrimitiveDataViewType primitiveType, T[][] values)
{
if (typeof(T) == typeof(string))
{
var convertedValues = new List<ReadOnlyMemory<char>[]>();
foreach (var value in values)
{
var converted = value.Select(x => x.ToString().AsMemory());
convertedValues.Add(converted.ToArray());
}
builder.AddColumn(columnName, primitiveType, convertedValues.ToArray());
}
else
builder.AddColumn(columnName, primitiveType, values);
}
/// <summary>
/// Helper function to create an IDataView given a list of key and vector-based values
/// </summary>
internal static IDataView CreateDataView<TKey, TValue>(IHostEnvironment env,
IEnumerable<TKey> keys,
IEnumerable<TValue[]> values,
string keyColumnName,
string valueColumnName)
{
var keyType = GetPrimitiveType(typeof(TKey), out bool isKeyVectorType);
var valueType = GetPrimitiveType(typeof(TValue), out bool isValueVectorType);
var dataViewBuilder = new ArrayDataViewBuilder(env);
AddColumnWrapper(dataViewBuilder, keyColumnName, keyType, keys.ToArray());
AddColumnWrapper(dataViewBuilder, valueColumnName, valueType, values.ToArray());
return dataViewBuilder.GetDataView();
}
/// <summary>
/// Helper function that builds the IDataView given a list of keys and non-vector values
/// </summary>
internal static IDataView CreateDataView<TKey, TValue>(IHostEnvironment env,
IEnumerable<TKey> keys,
IEnumerable<TValue> values,
string keyColumnName,
string valueColumnName,
bool treatValuesAsKeyTypes)
{
var keyType = GetPrimitiveType(typeof(TKey), out bool isKeyVectorType);
var valueType = GetPrimitiveType(typeof(TValue), out bool isValueVectorType);
var dataViewBuilder = new ArrayDataViewBuilder(env);
AddColumnWrapper(dataViewBuilder, keyColumnName, keyType, keys.ToArray());
if (treatValuesAsKeyTypes)
{
// When treating the values as KeyTypes, generate the unique
// set of values. This is used for generating the metadata of
// the column.
HashSet<TValue> valueSet = new HashSet<TValue>();
foreach (var v in values)
{
if (valueSet.Contains(v))
continue;
valueSet.Add(v);
}
var metaKeys = valueSet.ToArray();
// Key Values are treated in one of two ways:
// If the values are of type uint or ulong, these values are used directly as the keys types and no new keys are created.
// If the values are not of uint or ulong, then key values are generated as uints starting from 1, since 0 is missing key.
if (valueType.RawType == typeof(uint))
{
uint[] indices = values.Select((x) => Convert.ToUInt32(x)).ToArray();
dataViewBuilder.AddColumn(valueColumnName, GetKeyValueGetter(metaKeys), (ulong)metaKeys.Length, indices);
}
else if (valueType.RawType == typeof(ulong))
{
ulong[] indices = values.Select((x) => Convert.ToUInt64(x)).ToArray();
dataViewBuilder.AddColumn(valueColumnName, GetKeyValueGetter(metaKeys), (ulong)metaKeys.Length, indices);
}
else
{
// When generating the indices, treat each value as being unique, i.e. two values that are the same will
// be assigned the same index. The dictionary is used to maintain uniqueness, indices will contain
// the full list of indices (equal to the same length of values).
Dictionary<TValue, uint> keyTypeValueMapping = new Dictionary<TValue, uint>();
uint[] indices = new uint[values.Count()];
// Start the index at 1
uint index = 1;
for (int i = 0; i < values.Count(); ++i)
{
TValue value = values.ElementAt(i);
if (!keyTypeValueMapping.ContainsKey(value))
{
keyTypeValueMapping.Add(value, index);
index++;
}
var keyValue = keyTypeValueMapping[value];
indices[i] = keyValue;
}
dataViewBuilder.AddColumn(valueColumnName, GetKeyValueGetter(metaKeys), (ulong)metaKeys.Count(), indices);
}
}
else
AddColumnWrapper(dataViewBuilder, valueColumnName, valueType, values.ToArray());
return dataViewBuilder.GetDataView();
}
}
/// <summary>
/// <see cref="ITransformer"/> resulting from fitting a <see cref="ValueMappingEstimator"/>.
/// </summary>
public class ValueMappingTransformer : OneToOneTransformerBase
{
internal const string Summary = "Maps text values columns to new columns using a map dataset.";
internal const string LoaderSignature = "ValueMappingTransformer";
internal const string UserName = "Value Mapping Transform";
internal const string ShortName = "ValueMap";
internal const string TermLookupLoaderSignature = "TermLookupTransform";
// Stream names for the binary idv streams.
private const string DefaultMapName = "DefaultMap.idv";
internal static string DefaultKeyColumnName = "Key";
internal static string DefaultValueColumnName = "Value";
private readonly ValueMap _valueMap;
private readonly byte[] _dataView;
internal DataViewType ValueColumnType => _valueMap.ValueColumn.Type;
internal DataViewSchema.Annotations ValueColumnMetadata => _valueMap.ValueColumn.Annotations;
private static VersionInfo GetVersionInfo()
{
return new VersionInfo(
modelSignature: "VALUMAPG",
verWrittenCur: 0x00010001, // Initial.
verReadableCur: 0x00010001,
verWeCanReadBack: 0x00010001,
loaderSignature: LoaderSignature,
loaderAssemblyName: typeof(ValueMappingTransformer).Assembly.FullName);
}
private static VersionInfo GetTermLookupVersionInfo()
{
return new VersionInfo(
modelSignature: "TXTLOOKT",
// verWrittenCur: 0x00010001, // Initial.
verWrittenCur: 0x00010002, // Dropped sizeof(Float).
verReadableCur: 0x00010002,
verWeCanReadBack: 0x00010002,
loaderSignature: LoaderSignature,
loaderAssemblyName: typeof(ValueMappingTransformer).Assembly.FullName);
}
internal sealed class Column : OneToOneColumn
{
internal static Column Parse(string str)
{
var res = new Column();
if (res.TryParse(str))
return res;
return null;
}
internal bool TryUnparse(StringBuilder sb)
{
Contracts.AssertValue(sb);
return TryUnparseCore(sb);
}
}
internal sealed class Options
{
[Argument(ArgumentType.Multiple | ArgumentType.Required, HelpText = "New column definition(s) (optional form: name:src)", Name = "Column", ShortName = "col", SortOrder = 1)]
public Column[] Columns;
[Argument(ArgumentType.AtMostOnce, IsInputFileName = true, HelpText = "The data file containing the terms", ShortName = "data", SortOrder = 2)]
public string DataFile;
[Argument(ArgumentType.AtMostOnce, HelpText = "The name of the column containing the keys", ShortName = "keyCol, term, TermColumn")]
public string KeyColumn;
[Argument(ArgumentType.AtMostOnce, HelpText = "The name of the column containing the values", ShortName = "valueCol, value")]
public string ValueColumn;
[Argument(ArgumentType.Multiple, HelpText = "The data loader", NullName = "<Auto>", SignatureType = typeof(SignatureDataLoader))]
public IComponentFactory<IMultiStreamSource, ILegacyDataLoader> Loader;
[Argument(ArgumentType.AtMostOnce,
HelpText = "Specifies whether the values are key values or numeric, only valid when loader is not specified and the type of data is not an idv.",
ShortName = "key")]
public bool ValuesAsKeyType = true;
}
internal ValueMappingTransformer(IHostEnvironment env, IDataView lookupMap,
DataViewSchema.Column lookupKeyColumn, DataViewSchema.Column lookupValueColumn, (string outputColumnName, string inputColumnName)[] columns)
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(ValueMappingTransformer)), columns)
{
_valueMap = CreateValueMapFromDataView(lookupMap, lookupKeyColumn, lookupValueColumn);
// Create the byte array of the original IDataView, this is used for saving out the data.
_dataView = GetBytesFromDataView(Host, lookupMap, lookupKeyColumn.Name, lookupValueColumn.Name);
}
private ValueMap CreateValueMapFromDataView(IDataView dataView, DataViewSchema.Column keyColumn, DataViewSchema.Column valueColumn)
{
DataViewSchema.Column? column = null;
// Check if key column is valid to the used IDataView.
column = dataView.Schema.GetColumnOrNull(keyColumn.Name);
Host.Check(column.HasValue, "The selected column " + nameof(keyColumn) + " is not included in the targeted IDataView " + nameof(dataView));
var retrievedKeyColumn = column.Value;
Host.Check(retrievedKeyColumn.Index == keyColumn.Index, nameof(keyColumn) + "'s column index doesn't match that of the associated column in " + nameof(dataView));
Host.Check(retrievedKeyColumn.Type == keyColumn.Type, nameof(keyColumn) + "'s column type doesn't match that of the associated column in " + nameof(dataView));
Host.Check(retrievedKeyColumn.Annotations == keyColumn.Annotations, nameof(keyColumn) + "'s column annotations don't match those of the associated column in " + nameof(dataView));
// Check if value column is valid to the used IDataView.
column = dataView.Schema.GetColumnOrNull(valueColumn.Name);
Host.Check(column.HasValue, "The selected column " + nameof(valueColumn) + " is not included in the targeted IDataView " + nameof(dataView));
var retrievedValueColumn = column.Value;
Host.Check(retrievedValueColumn.Index == valueColumn.Index, nameof(valueColumn) + "'s column index doesn't match that of the associated column in " + nameof(dataView));
Host.Check(retrievedValueColumn.Type == valueColumn.Type, nameof(valueColumn) + "'s column type doesn't match that of the associated column in " + nameof(dataView));
Host.Check(retrievedValueColumn.Annotations == valueColumn.Annotations, nameof(valueColumn) + "'s column annotations don't match those of the associated column in " + nameof(dataView));
// Extract key-column pairs to form dictionary from the used IDataView.
var valueMap = ValueMap.Create(keyColumn, valueColumn);
using (var cursor = dataView.GetRowCursor(dataView.Schema[keyColumn.Index], dataView.Schema[valueColumn.Index]))
valueMap.Train(Host, cursor);
return valueMap;
}
private static TextLoader.Column GenerateValueColumn(IHostEnvironment env,
IDataView loader,
string valueColumnName,
int keyIdx,
int valueIdx,
string fileName)
{
// Scan the source to determine the min max of the column
ulong keyMin = ulong.MaxValue;
ulong keyMax = ulong.MinValue;
// scan the input to create convert the values as key types
using (var cursor = loader.GetRowCursorForAllColumns())
{
using (var ch = env.Start($"Processing key values from file {fileName}"))
{
var getKey = cursor.GetGetter<ReadOnlyMemory<char>>(cursor.Schema[keyIdx]);
var getValue = cursor.GetGetter<ReadOnlyMemory<char>>(cursor.Schema[valueIdx]);
int countNonKeys = 0;
ReadOnlyMemory<char> key = default;
ReadOnlyMemory<char> value = default;
while (cursor.MoveNext())
{
getKey(ref key);
getValue(ref value);
ulong res;
// Try to parse the text as a key value between 1 and ulong.MaxValue. If this succeeds and res>0,
// we update max and min accordingly. If res==0 it means the value is missing, in which case we ignore it for
// computing max and min.
if (Data.Conversion.Conversions.DefaultInstance.TryParseKey(in value, ulong.MaxValue - 1, out res))
{
if (res < keyMin && res != 0)
keyMin = res;
if (res > keyMax)
keyMax = res;
}
// If parsing as key did not succeed, the value can still be 0, so we try parsing it as a ulong. If it succeeds,
// then the value is 0, and we update min accordingly.
else if (Microsoft.ML.Data.Conversion.Conversions.DefaultInstance.TryParse(in value, out res))
{
keyMin = 0;
}
//If parsing as a ulong fails, we increment the counter for the non-key values.
else
{
if (countNonKeys < 5)
ch.Warning($"Key '{key}' in mapping file is mapped to non key value '{value}'");
countNonKeys++;
}
}
if (countNonKeys > 0)
ch.Warning($"Found {countNonKeys} non key values in the file '{fileName}'");
if (keyMin > keyMax)
{
keyMin = 0;
keyMax = uint.MaxValue - 1;
ch.Warning($"Did not find any valid key values in the file '{fileName}'");
}
else
ch.Info($"Found key values in the range {keyMin} to {keyMax} in the file '{fileName}'");
}
}
TextLoader.Column valueColumn = new TextLoader.Column(valueColumnName, DataKind.UInt32, 1);
if (keyMax < int.MaxValue)
valueColumn.KeyCount = new KeyCount(keyMax + 1);
else if (keyMax < uint.MaxValue)
valueColumn.KeyCount = new KeyCount();
else
{
valueColumn.Type = DataKind.UInt64.ToInternalDataKind();
valueColumn.KeyCount = new KeyCount();
}
return valueColumn;
}
private static ValueMappingTransformer CreateTransformInvoke<TKey, TValue>(IHostEnvironment env,
IDataView idv,
string keyColumnName,
string valueColumnName,
bool treatValuesAsKeyTypes,
(string outputColumnName, string inputColumnName)[] columns)
{
// Read in the data
// scan the input to create convert the values as key types
List<TKey> keys = new List<TKey>();
List<TValue> values = new List<TValue>();
var keyColumn = idv.Schema[keyColumnName];
var valueColumn = idv.Schema[valueColumnName];
using (var cursor = idv.GetRowCursorForAllColumns())
{
using (var ch = env.Start("Processing key values"))
{
TKey key = default;
TValue value = default;
var getKey = cursor.GetGetter<TKey>(keyColumn);
var getValue = cursor.GetGetter<TValue>(valueColumn);
while (cursor.MoveNext())
{
try
{
getKey(ref key);
}
catch (InvalidOperationException)
{
ch.Warning("Invalid key parsed, row will be skipped.");
continue;
}
try
{
getValue(ref value);
}
catch (InvalidOperationException)
{
ch.Warning("Invalid value parsed for key {key}, row will be skipped.");
continue;
}
keys.Add(key);
values.Add(value);
}
}
}
var lookupMap = DataViewHelper.CreateDataView(env, keys, values, keyColumnName, valueColumnName, treatValuesAsKeyTypes);
return new ValueMappingTransformer(env, lookupMap, lookupMap.Schema[keyColumnName], lookupMap.Schema[valueColumnName], columns);
}
private static IDataTransform Create(IHostEnvironment env, Options options, IDataView input)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(options, nameof(options));
env.CheckUserArg(!string.IsNullOrWhiteSpace(options.DataFile), nameof(options.DataFile));
env.CheckValueOrNull(options.KeyColumn);
env.CheckValueOrNull(options.ValueColumn);
var keyColumnName = (string.IsNullOrEmpty(options.KeyColumn)) ? DefaultKeyColumnName : options.KeyColumn;
var valueColumnName = (string.IsNullOrEmpty(options.ValueColumn)) ? DefaultValueColumnName : options.ValueColumn;
IMultiStreamSource fileSource = new MultiFileSource(options.DataFile);
IDataView loader;
if (options.Loader != null)
{
loader = options.Loader.CreateComponent(env, fileSource);
}
else
{
var extension = Path.GetExtension(options.DataFile);
if (extension.Equals(".idv", StringComparison.OrdinalIgnoreCase))
loader = new BinaryLoader(env, new BinaryLoader.Arguments(), fileSource);
else if (extension.Equals(".tdv"))
loader = new TransposeLoader(env, new TransposeLoader.Arguments(), fileSource);
else
{
// The user has not specified how to load this file. This will attempt to load the
// data file as two text columns. If the user has also specified ValuesAsKeyTypes,
// this will default to the key column as a text column and the value column as a uint column
// Set the keyColumnName and valueColumnName to the default values.
keyColumnName = DefaultKeyColumnName;
valueColumnName = DefaultValueColumnName;
TextLoader.Column keyColumn = default;
TextLoader.Column valueColumn = default;
// Default to a text loader. KeyType and ValueType are assumed to be string
// types unless ValueAsKeyType is specified.
if (options.ValuesAsKeyType)
{
keyColumn = new TextLoader.Column(keyColumnName, DataKind.String, 0);
valueColumn = new TextLoader.Column(valueColumnName, DataKind.String, 1);
var txtArgs = new TextLoader.Options()
{
Columns = new TextLoader.Column[]
{
keyColumn,
valueColumn
}
};
try
{
var textLoader = TextLoader.LoadFile(env, txtArgs, fileSource);
valueColumn = GenerateValueColumn(env, textLoader, valueColumnName, 0, 1, options.DataFile);
}
catch (Exception ex)
{
throw env.Except(ex, $"Failed to parse the lookup file '{options.DataFile}' in ValueMappingTransformerer");
}
}
else
{
keyColumn = new TextLoader.Column(keyColumnName, DataKind.String, 0);
valueColumn = new TextLoader.Column(valueColumnName, DataKind.Single, 1);
}
loader = TextLoader.Create(
env,
new TextLoader.Options()
{
Columns = new TextLoader.Column[]
{
keyColumn,
valueColumn
}
},
fileSource);
}
}
env.AssertValue(loader);
env.Assert(loader.Schema.TryGetColumnIndex(keyColumnName, out int keyColumnIndex));
env.Assert(loader.Schema.TryGetColumnIndex(valueColumnName, out int valueColumnIndex));
ValueMappingTransformer transformer = null;
(string outputColumnName, string inputColumnName)[] columns = options.Columns.Select(x => (x.Name, x.Source)).ToArray();
transformer = new ValueMappingTransformer(env, loader, loader.Schema[keyColumnName], loader.Schema[valueColumnName], columns);
return transformer.MakeDataTransform(input);
}
/// <summary>
/// Helper function to determine the model version that is being loaded.
/// </summary>
private static bool CheckModelVersion(ModelLoadContext ctx, VersionInfo versionInfo)
{
try
{
ctx.CheckVersionInfo(versionInfo);
return true;
}
catch (Exception)
{
//consume
return false;
}
}
private protected static ValueMappingTransformer Create(IHostEnvironment env, ModelLoadContext ctx)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(ctx, nameof(ctx));
// Checks for both the TermLookup for backwards compatibility
var termLookupModel = CheckModelVersion(ctx, GetTermLookupVersionInfo());
env.Check(termLookupModel || CheckModelVersion(ctx, GetVersionInfo()));
// *** Binary format ***
// int: number of added columns
// for each added column
// string: output column name
// string: input column name
// Binary stream of mapping
var length = ctx.Reader.ReadInt32();
var columns = new (string outputColumnName, string inputColumnName)[length];
for (int i = 0; i < length; i++)
{
columns[i].outputColumnName = ctx.LoadNonEmptyString();
columns[i].inputColumnName = ctx.LoadNonEmptyString();
}
byte[] rgb = null;
Action<BinaryReader> fn = r => rgb = ReadAllBytes(env, r);
if (!ctx.TryLoadBinaryStream(DefaultMapName, fn))
throw env.ExceptDecode();
var binaryLoader = GetLoader(env, rgb);
var keyColumnName = (termLookupModel) ? "Term" : DefaultKeyColumnName;
return new ValueMappingTransformer(env, binaryLoader, binaryLoader.Schema[keyColumnName], binaryLoader.Schema[DefaultValueColumnName], columns);
}
private static byte[] ReadAllBytes(IExceptionContext ectx, BinaryReader rdr)
{
Contracts.AssertValue(ectx);
ectx.AssertValue(rdr);
ectx.Assert(rdr.BaseStream.CanSeek);
long size = rdr.BaseStream.Length;
ectx.CheckDecode(size <= int.MaxValue);
var rgb = new byte[(int)size];
int cb = rdr.Read(rgb, 0, rgb.Length);
ectx.CheckDecode(cb == rgb.Length);
return rgb;
}
private static IDataTransform Create(IHostEnvironment env, ModelLoadContext ctx, IDataView input)
=> Create(env, ctx).MakeDataTransform(input);
private static IRowMapper Create(IHostEnvironment env, ModelLoadContext ctx, DataViewSchema inputSchema)
=> Create(env, ctx).MakeRowMapper(inputSchema);
private protected static PrimitiveDataViewType GetPrimitiveType(Type rawType, out bool isVectorType)
{
Type type = rawType;
isVectorType = false;
if (type.IsArray)
{
type = rawType.GetElementType();
isVectorType = true;
}
if (!type.TryGetDataKind(out InternalDataKind kind))
throw Contracts.Except($"Unsupported type {type} used in mapping.");
return ColumnTypeExtensions.PrimitiveTypeFromKind(kind);
}
private protected override void SaveModel(ModelSaveContext ctx)
{
Host.CheckValue(ctx, nameof(ctx));
ctx.SetVersionInfo(GetVersionInfo());
SaveColumns(ctx);
// Save out the byte stream of the IDataView of the data source
ctx.SaveBinaryStream(DefaultMapName, w => w.Write(_dataView));
}
/// <summary>
/// Base class that contains the mapping of keys to values.
/// </summary>
private abstract class ValueMap
{
public DataViewSchema.Column KeyColumn { get; }
public DataViewSchema.Column ValueColumn { get; }
public ValueMap(DataViewSchema.Column keyColumn, DataViewSchema.Column valueColumn)
{
KeyColumn = keyColumn;
ValueColumn = valueColumn;
}
internal static ValueMap Create(DataViewSchema.Column keyColumn, DataViewSchema.Column valueColumn)
{
Func<DataViewSchema.Column, DataViewSchema.Column, ValueMap> del = CreateValueMapInvoke<int, int>;
var method = del.Method.GetGenericMethodDefinition().MakeGenericMethod(keyColumn.Type.RawType, valueColumn.Type.RawType);
return (ValueMap)method.Invoke(null, new object[] { keyColumn, valueColumn });
}
private static ValueMap CreateValueMapInvoke<TKey, TValue>(DataViewSchema.Column keyColumn, DataViewSchema.Column valueColumn) =>
new ValueMap<TKey, TValue>(keyColumn, valueColumn);
public abstract void Train(IHostEnvironment env, DataViewRowCursor cursor);
public abstract Delegate GetGetter(DataViewRow input, int index);
public abstract IDataView GetDataView(IHostEnvironment env);
public abstract TKey[] GetKeys<TKey>();
public abstract TValue[] GetValues<TValue>();
}
/// <summary>
/// Implementation mapping class that maps a key of TKey to a specified value of TValue.
/// </summary>
private class ValueMap<TKey, TValue> : ValueMap
{
private static readonly FuncStaticMethodInfo1<TValue, TValue> _getVectorMethodInfo
= new FuncStaticMethodInfo1<TValue, TValue>(GetVector<int>);
private static readonly FuncStaticMethodInfo1<TValue, TValue> _getValueMethodInfo
= new FuncStaticMethodInfo1<TValue, TValue>(GetValue<int>);
private readonly Dictionary<TKey, TValue> _mapping;
private TValue _missingValue;
private Dictionary<TKey, TValue> CreateDictionary()
{
if (typeof(TKey) == typeof(ReadOnlyMemory<char>))
return new Dictionary<ReadOnlyMemory<char>, TValue>(new ReadOnlyMemoryUtils.ReadonlyMemoryCharComparer()) as Dictionary<TKey, TValue>;
return new Dictionary<TKey, TValue>();
}
public ValueMap(DataViewSchema.Column keyColumn, DataViewSchema.Column valueColumn) : base(keyColumn, valueColumn)
{
_mapping = CreateDictionary();
}
/// <summary>
/// Generates the mapping based on the IDataView
/// </summary>
public override void Train(IHostEnvironment env, DataViewRowCursor cursor)
{
// Validate that the conversion is supported for non-vector types
bool identity;
ValueMapper<ReadOnlyMemory<char>, TValue> conv;
// For keys that are not in the mapping, the missingValue will be returned.
_missingValue = default;
if (!(ValueColumn.Type is VectorDataViewType))
{
// For handling missing values, this follows how a missing value is handled when loading from a text source.
// First check if there is a String->ValueType conversion method. If so, call the conversion method with an
// empty string, the returned value will be the new missing value.
// NOTE this will return NA for R4 and R8 types.
if (Data.Conversion.Conversions.DefaultInstance.TryGetStandardConversion<ReadOnlyMemory<char>, TValue>(
TextDataViewType.Instance,
ValueColumn.Type,
out conv,
out identity))
{
TValue value = default;
conv(string.Empty.AsMemory(), ref value);
_missingValue = value;
}
}
var keyGetter = cursor.GetGetter<TKey>(KeyColumn);
var valueGetter = cursor.GetGetter<TValue>(ValueColumn);
while (cursor.MoveNext())
{
TKey key = default;
TValue value = default;
keyGetter(ref key);
valueGetter(ref value);
if (_mapping.ContainsKey(key))
throw env.Except($"Duplicate keys in data '{key}'");
_mapping.Add(key, value);
}
}
private TValue MapValue(TKey key)
{
if (_mapping.ContainsKey(key))
{
if (ValueColumn.Type is VectorDataViewType vectorType)
return Utils.MarshalInvoke(_getVectorMethodInfo, vectorType.ItemType.RawType, _mapping[key]);
else
return Utils.MarshalInvoke(_getValueMethodInfo, ValueColumn.Type.RawType, _mapping[key]);
}
else
return _missingValue;
}
public override Delegate GetGetter(DataViewRow input, int index)
{
var column = input.Schema[index];
if (column.Type is VectorDataViewType)
{
var src = default(VBuffer<TKey>);
var getSrc = input.GetGetter<VBuffer<TKey>>(column);
ValueGetter<VBuffer<TValue>> retVal =
(ref VBuffer<TValue> dst) =>
{
getSrc(ref src);
var editor = VBufferEditor.Create(ref dst, src.Length);
var values = src.GetValues();
src.GetIndices().CopyTo(editor.Indices);
for (int ich = 0; ich < values.Length; ich++)
{
editor.Values[ich] = MapValue(values[ich]);
}
dst = editor.Commit();
};
return retVal;
}
else
{
var src = default(TKey);
var getSrc = input.GetGetter<TKey>(column);
ValueGetter<TValue> retVal =
(ref TValue dst) =>
{
getSrc(ref src);
dst = MapValue(src);
};
return retVal;
}
}
public override IDataView GetDataView(IHostEnvironment env)
=> DataViewHelper.CreateDataView(env,
_mapping.Keys,
_mapping.Values,
ValueMappingTransformer.DefaultKeyColumnName,
ValueMappingTransformer.DefaultValueColumnName,
ValueColumn.Type is KeyDataViewType);
private static TValue GetVector<T>(TValue value)
{
if (value is VBuffer<T> valueRef)
{
VBuffer<T> dest = default;
valueRef.CopyTo(ref dest);
if (dest is TValue destRef)
return destRef;
}
return default;
}
private static TValue GetValue<T>(TValue value) => value;
public override T[] GetKeys<T>()
{
return _mapping.Keys.Cast<T>().ToArray();
}
public override T[] GetValues<T>()
{
return _mapping.Values.Cast<T>().ToArray();
}
}
/// <summary>
/// Retrieves the byte array given a dataview and columns
/// </summary>
private static byte[] GetBytesFromDataView(IHost host, IDataView lookup, string keyColumn, string valueColumn)
{
Contracts.AssertValue(host);
host.AssertValue(lookup);
host.AssertNonEmpty(keyColumn);
host.AssertNonEmpty(valueColumn);
var schema = lookup.Schema;
if (!schema.GetColumnOrNull(keyColumn).HasValue)
throw host.ExceptUserArg(nameof(Options.KeyColumn), $"Key column not found: '{keyColumn}'");
if (!schema.GetColumnOrNull(valueColumn).HasValue)
throw host.ExceptUserArg(nameof(Options.ValueColumn), $"Value column not found: '{valueColumn}'");
var cols = new List<(string outputColumnName, string inputColumnName)>()
{
(DefaultKeyColumnName, keyColumn),
(DefaultValueColumnName, valueColumn)