|
5 | 5 | using System;
|
6 | 6 | using System.Collections.Generic;
|
7 | 7 | using System.Data;
|
| 8 | +using System.Data.Common; |
8 | 9 | using System.Linq;
|
| 10 | +using System.Reflection; |
| 11 | +using System.Runtime.CompilerServices; |
| 12 | +using System.Text; |
9 | 13 | using Microsoft.ML;
|
10 | 14 | using Microsoft.ML.CommandLine;
|
11 | 15 | using Microsoft.ML.Data;
|
@@ -98,6 +102,71 @@ void ICanSaveModel.Save(ModelSaveContext ctx)
|
98 | 102 | /// <param name="source">The source from which to load data.</param>
|
99 | 103 | public IDataView Load(DatabaseSource source) => new BoundLoader(this, source);
|
100 | 104 |
|
| 105 | + internal static DatabaseLoader CreateDatabaseLoader<TInput>(IHostEnvironment host) |
| 106 | + { |
| 107 | + var userType = typeof(TInput); |
| 108 | + |
| 109 | + var fieldInfos = userType.GetFields(BindingFlags.Public | BindingFlags.Instance); |
| 110 | + |
| 111 | + var propertyInfos = |
| 112 | + userType |
| 113 | + .GetProperties(BindingFlags.Public | BindingFlags.Instance) |
| 114 | + .Where(x => x.CanRead && x.GetGetMethod() != null && x.GetIndexParameters().Length == 0); |
| 115 | + |
| 116 | + var memberInfos = (fieldInfos as IEnumerable<MemberInfo>).Concat(propertyInfos).ToArray(); |
| 117 | + |
| 118 | + if (memberInfos.Length == 0) |
| 119 | + throw host.ExceptParam(nameof(TInput), $"Should define at least one public, readable field or property in {nameof(TInput)}."); |
| 120 | + |
| 121 | + var columns = new List<Column>(); |
| 122 | + |
| 123 | + for (int index = 0; index < memberInfos.Length; index++) |
| 124 | + { |
| 125 | + var memberInfo = memberInfos[index]; |
| 126 | + var mappingAttrName = memberInfo.GetCustomAttribute<ColumnNameAttribute>(); |
| 127 | + |
| 128 | + var column = new Column(); |
| 129 | + column.Name = mappingAttrName?.Name ?? memberInfo.Name; |
| 130 | + |
| 131 | + var mappingAttr = memberInfo.GetCustomAttribute<LoadColumnAttribute>(); |
| 132 | + |
| 133 | + if (mappingAttr is object) |
| 134 | + { |
| 135 | + var sources = mappingAttr.Sources.Select((source) => Range.FromTextLoaderRange(source)).ToArray(); |
| 136 | + column.Source = sources.Single().Min; |
| 137 | + } |
| 138 | + |
| 139 | + InternalDataKind dk; |
| 140 | + switch (memberInfo) |
| 141 | + { |
| 142 | + case FieldInfo field: |
| 143 | + if (!InternalDataKindExtensions.TryGetDataKind(field.FieldType.IsArray ? field.FieldType.GetElementType() : field.FieldType, out dk)) |
| 144 | + throw Contracts.Except($"Field {memberInfo.Name} is of unsupported type."); |
| 145 | + |
| 146 | + break; |
| 147 | + |
| 148 | + case PropertyInfo property: |
| 149 | + if (!InternalDataKindExtensions.TryGetDataKind(property.PropertyType.IsArray ? property.PropertyType.GetElementType() : property.PropertyType, out dk)) |
| 150 | + throw Contracts.Except($"Property {memberInfo.Name} is of unsupported type."); |
| 151 | + break; |
| 152 | + |
| 153 | + default: |
| 154 | + Contracts.Assert(false); |
| 155 | + throw Contracts.ExceptNotSupp("Expected a FieldInfo or a PropertyInfo"); |
| 156 | + } |
| 157 | + |
| 158 | + column.Type = dk.ToDbType(); |
| 159 | + |
| 160 | + columns.Add(column); |
| 161 | + } |
| 162 | + |
| 163 | + var options = new Options |
| 164 | + { |
| 165 | + Columns = columns.ToArray() |
| 166 | + }; |
| 167 | + return new DatabaseLoader(host, options); |
| 168 | + } |
| 169 | + |
101 | 170 | /// <summary>
|
102 | 171 | /// Describes how an input column should be mapped to an <see cref="IDataView"/> column.
|
103 | 172 | /// </summary>
|
@@ -128,6 +197,86 @@ public sealed class Column
|
128 | 197 | public KeyCount KeyCount;
|
129 | 198 | }
|
130 | 199 |
|
| 200 | + /// <summary> |
| 201 | + /// Specifies the range of indices of input columns that should be mapped to an output column. |
| 202 | + /// </summary> |
| 203 | + public sealed class Range |
| 204 | + { |
| 205 | + public Range() { } |
| 206 | + |
| 207 | + /// <summary> |
| 208 | + /// A range representing a single value. Will result in a scalar column. |
| 209 | + /// </summary> |
| 210 | + /// <param name="index">The index of the field of the text file to read.</param> |
| 211 | + public Range(int index) |
| 212 | + { |
| 213 | + Contracts.CheckParam(index >= 0, nameof(index), "Must be non-negative"); |
| 214 | + Min = index; |
| 215 | + Max = index; |
| 216 | + } |
| 217 | + |
| 218 | + /// <summary> |
| 219 | + /// A range representing a set of values. Will result in a vector column. |
| 220 | + /// </summary> |
| 221 | + /// <param name="min">The minimum inclusive index of the column.</param> |
| 222 | + /// <param name="max">The maximum-inclusive index of the column. If <c>null</c> |
| 223 | + /// indicates that the <see cref="TextLoader"/> should auto-detect the legnth |
| 224 | + /// of the lines, and read untill the end.</param> |
| 225 | + public Range(int min, int? max) |
| 226 | + { |
| 227 | + Contracts.CheckParam(min >= 0, nameof(min), "Must be non-negative"); |
| 228 | + Contracts.CheckParam(!(max < min), nameof(max), "If specified, must be greater than or equal to " + nameof(min)); |
| 229 | + |
| 230 | + Min = min; |
| 231 | + Max = max; |
| 232 | + // Note that without the following being set, in the case where there is a single range |
| 233 | + // where Min == Max, the result will not be a vector valued but a scalar column. |
| 234 | + ForceVector = true; |
| 235 | + AutoEnd = max == null; |
| 236 | + } |
| 237 | + |
| 238 | + /// <summary> |
| 239 | + /// The minimum index of the column, inclusive. |
| 240 | + /// </summary> |
| 241 | + [Argument(ArgumentType.Required, HelpText = "First index in the range")] |
| 242 | + public int Min; |
| 243 | + |
| 244 | + /// <summary> |
| 245 | + /// The maximum index of the column, inclusive. If <see langword="null"/> |
| 246 | + /// indicates that the <see cref="TextLoader"/> should auto-detect the legnth |
| 247 | + /// of the lines, and read untill the end. |
| 248 | + /// If <see cref="Max"/> is specified, the field <see cref="AutoEnd"/> is ignored. |
| 249 | + /// </summary> |
| 250 | + [Argument(ArgumentType.AtMostOnce, HelpText = "Last index in the range")] |
| 251 | + public int? Max; |
| 252 | + |
| 253 | + /// <summary> |
| 254 | + /// Whether this range extends to the end of the line, but should be a fixed number of items. |
| 255 | + /// If <see cref="Max"/> is specified, the field <see cref="AutoEnd"/> is ignored. |
| 256 | + /// </summary> |
| 257 | + [Argument(ArgumentType.AtMostOnce, |
| 258 | + HelpText = "This range extends to the end of the line, but should be a fixed number of items", |
| 259 | + ShortName = "auto")] |
| 260 | + public bool AutoEnd; |
| 261 | + |
| 262 | + /// <summary> |
| 263 | + /// Whether this range includes only other indices not specified. |
| 264 | + /// </summary> |
| 265 | + [Argument(ArgumentType.AtMostOnce, HelpText = "This range includes only other indices not specified", ShortName = "other")] |
| 266 | + public bool AllOther; |
| 267 | + |
| 268 | + /// <summary> |
| 269 | + /// Force scalar columns to be treated as vectors of length one. |
| 270 | + /// </summary> |
| 271 | + [Argument(ArgumentType.AtMostOnce, HelpText = "Force scalar columns to be treated as vectors of length one", ShortName = "vector")] |
| 272 | + public bool ForceVector; |
| 273 | + |
| 274 | + internal static Range FromTextLoaderRange(TextLoader.Range range) |
| 275 | + { |
| 276 | + return new Range(range.Min, range.Max); |
| 277 | + } |
| 278 | + } |
| 279 | + |
131 | 280 | /// <summary>
|
132 | 281 | /// The settings for <see cref="DatabaseLoader"/>
|
133 | 282 | /// </summary>
|
|
0 commit comments