|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using Microsoft.ML.Runtime; |
| 7 | +using Microsoft.ML.Runtime.Api; |
| 8 | +using Microsoft.ML.Runtime.Data; |
| 9 | +using Microsoft.ML.Runtime.EntryPoints; |
| 10 | +using Microsoft.ML.Runtime.Internal.Utilities; |
| 11 | + |
| 12 | +namespace Microsoft.ML.Data |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Creates data source for pipeline based on provided collection of data. |
| 16 | + /// </summary> |
| 17 | + public static class CollectionDataSource |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// Creates pipeline data source. Support shuffle. |
| 21 | + /// </summary> |
| 22 | + public static ILearningPipelineLoader Create<T>(IList<T> data) where T : class |
| 23 | + { |
| 24 | + return new ListDataSource<T>(data); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Creates pipeline data source which can't be shuffled. |
| 29 | + /// </summary> |
| 30 | + public static ILearningPipelineLoader Create<T>(IEnumerable<T> data) where T : class |
| 31 | + { |
| 32 | + return new EnumerableDataSource<T>(data); |
| 33 | + } |
| 34 | + |
| 35 | + private abstract class BaseDataSource<TInput> : ILearningPipelineLoader where TInput : class |
| 36 | + { |
| 37 | + private Data.DataViewReference _dataViewEntryPoint; |
| 38 | + private IDataView _dataView; |
| 39 | + |
| 40 | + public ILearningPipelineStep ApplyStep(ILearningPipelineStep previousStep, Experiment experiment) |
| 41 | + { |
| 42 | + Contracts.Assert(previousStep == null); |
| 43 | + _dataViewEntryPoint = new Data.DataViewReference(); |
| 44 | + var importOutput = experiment.Add(_dataViewEntryPoint); |
| 45 | + return new CollectionDataSourcePipelineStep(importOutput.Data); |
| 46 | + } |
| 47 | + |
| 48 | + public void SetInput(IHostEnvironment environment, Experiment experiment) |
| 49 | + { |
| 50 | + _dataView = GetDataView(environment); |
| 51 | + environment.CheckValue(_dataView, nameof(_dataView)); |
| 52 | + experiment.SetInput(_dataViewEntryPoint.Data, _dataView); |
| 53 | + } |
| 54 | + |
| 55 | + public abstract IDataView GetDataView(IHostEnvironment environment); |
| 56 | + } |
| 57 | + |
| 58 | + private class EnumerableDataSource<TInput> : BaseDataSource<TInput> where TInput : class |
| 59 | + { |
| 60 | + private readonly IEnumerable<TInput> _enumerableCollection; |
| 61 | + |
| 62 | + public EnumerableDataSource(IEnumerable<TInput> collection) |
| 63 | + { |
| 64 | + Contracts.CheckValue(collection, nameof(collection)); |
| 65 | + _enumerableCollection = collection; |
| 66 | + } |
| 67 | + |
| 68 | + public override IDataView GetDataView(IHostEnvironment environment) |
| 69 | + { |
| 70 | + return ComponentCreation.CreateStreamingDataView(environment, _enumerableCollection); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private class ListDataSource<TInput> : BaseDataSource<TInput> where TInput : class |
| 75 | + { |
| 76 | + private readonly IList<TInput> _listCollection; |
| 77 | + |
| 78 | + public ListDataSource(IList<TInput> collection) |
| 79 | + { |
| 80 | + Contracts.CheckParamValue(Utils.Size(collection) > 0, collection, nameof(collection), "Must be non-empty"); |
| 81 | + _listCollection = collection; |
| 82 | + } |
| 83 | + |
| 84 | + public override IDataView GetDataView(IHostEnvironment environment) |
| 85 | + { |
| 86 | + return ComponentCreation.CreateDataView(environment, _listCollection); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private class CollectionDataSourcePipelineStep : ILearningPipelineDataStep |
| 91 | + { |
| 92 | + public CollectionDataSourcePipelineStep(Var<IDataView> data) |
| 93 | + { |
| 94 | + Data = data; |
| 95 | + } |
| 96 | + |
| 97 | + public Var<IDataView> Data { get; } |
| 98 | + public Var<ITransformModel> Model => null; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments