|
| 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; |
| 6 | +using System.Collections.Generic; |
| 7 | +using Microsoft.Data.Analysis; |
| 8 | +using Microsoft.ML.Data; |
| 9 | + |
| 10 | +namespace Microsoft.ML |
| 11 | +{ |
| 12 | + public static class IDataViewExtensions |
| 13 | + { |
| 14 | + private const int defaultMaxRows = 100; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Returns a <see cref="Microsoft.Data.Analysis.DataFrame"/> from this <paramref name="dataView"/>. |
| 18 | + /// </summary> |
| 19 | + /// <param name="dataView">The current <see cref="IDataView"/>.</param> |
| 20 | + /// <param name="maxRows">The max number or rows in the <see cref="Microsoft.Data.Analysis.DataFrame"/>. Defaults to 100. Use -1 to construct a DataFrame using all the rows in <paramref name="dataView"/>.</param> |
| 21 | + /// <returns>A <see cref="Microsoft.Data.Analysis.DataFrame"/> with <paramref name="maxRows"/>.</returns> |
| 22 | + public static DataFrame ToDataFrame(this IDataView dataView, long maxRows = defaultMaxRows) |
| 23 | + { |
| 24 | + return ToDataFrame(dataView, maxRows, null); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Returns a <see cref="Microsoft.Data.Analysis.DataFrame"/> with the first 100 rows of this <paramref name="dataView"/>. |
| 29 | + /// </summary> |
| 30 | + /// <param name="dataView">The current <see cref="IDataView"/>.</param> |
| 31 | + /// <param name="selectColumns">The columns selected for the resultant DataFrame</param> |
| 32 | + /// <returns>A <see cref="Microsoft.Data.Analysis.DataFrame"/> with the selected columns and 100 rows.</returns> |
| 33 | + public static DataFrame ToDataFrame(this IDataView dataView, params string[] selectColumns) |
| 34 | + { |
| 35 | + return ToDataFrame(dataView, defaultMaxRows, selectColumns); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Returns a <see cref="Microsoft.Data.Analysis.DataFrame"/> with the first <paramref name="maxRows"/> of this <paramref name="dataView"/>. |
| 40 | + /// </summary> |
| 41 | + /// <param name="dataView">The current <see cref="IDataView"/>.</param> |
| 42 | + /// <param name="maxRows">The max number or rows in the <see cref="Microsoft.Data.Analysis.DataFrame"/>. Use -1 to construct a DataFrame using all the rows in <paramref name="dataView"/>.</param> |
| 43 | + /// <param name="selectColumns">The columns selected for the resultant DataFrame</param> |
| 44 | + /// <returns>A <see cref="Microsoft.Data.Analysis.DataFrame"/> with the selected columns and <paramref name="maxRows"/> rows.</returns> |
| 45 | + public static DataFrame ToDataFrame(this IDataView dataView, long maxRows, params string[] selectColumns) |
| 46 | + { |
| 47 | + DataViewSchema schema = dataView.Schema; |
| 48 | + List<DataFrameColumn> dataFrameColumns = new List<DataFrameColumn>(schema.Count); |
| 49 | + maxRows = maxRows == -1 ? long.MaxValue : maxRows; |
| 50 | + |
| 51 | + HashSet<string> selectColumnsSet = null; |
| 52 | + if (selectColumns != null && selectColumns.Length > 0) |
| 53 | + { |
| 54 | + selectColumnsSet = new HashSet<string>(selectColumns); |
| 55 | + } |
| 56 | + |
| 57 | + List<DataViewSchema.Column> activeDataViewColumns = new List<DataViewSchema.Column>(); |
| 58 | + foreach (DataViewSchema.Column dataViewColumn in schema) |
| 59 | + { |
| 60 | + if (dataViewColumn.IsHidden || (selectColumnsSet != null && !selectColumnsSet.Contains(dataViewColumn.Name))) |
| 61 | + { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + activeDataViewColumns.Add(dataViewColumn); |
| 66 | + DataViewType type = dataViewColumn.Type; |
| 67 | + if (type == BooleanDataViewType.Instance) |
| 68 | + { |
| 69 | + dataFrameColumns.Add(new BooleanDataFrameColumn(dataViewColumn.Name)); |
| 70 | + } |
| 71 | + else if (type == NumberDataViewType.Byte) |
| 72 | + { |
| 73 | + dataFrameColumns.Add(new ByteDataFrameColumn(dataViewColumn.Name)); |
| 74 | + } |
| 75 | + else if (type == NumberDataViewType.Double) |
| 76 | + { |
| 77 | + dataFrameColumns.Add(new DoubleDataFrameColumn(dataViewColumn.Name)); |
| 78 | + } |
| 79 | + else if (type == NumberDataViewType.Single) |
| 80 | + { |
| 81 | + dataFrameColumns.Add(new SingleDataFrameColumn(dataViewColumn.Name)); |
| 82 | + } |
| 83 | + else if (type == NumberDataViewType.Int32) |
| 84 | + { |
| 85 | + dataFrameColumns.Add(new Int32DataFrameColumn(dataViewColumn.Name)); |
| 86 | + } |
| 87 | + else if (type == NumberDataViewType.Int64) |
| 88 | + { |
| 89 | + dataFrameColumns.Add(new Int64DataFrameColumn(dataViewColumn.Name)); |
| 90 | + } |
| 91 | + else if (type == NumberDataViewType.SByte) |
| 92 | + { |
| 93 | + dataFrameColumns.Add(new SByteDataFrameColumn(dataViewColumn.Name)); |
| 94 | + } |
| 95 | + else if (type == NumberDataViewType.Int16) |
| 96 | + { |
| 97 | + dataFrameColumns.Add(new Int16DataFrameColumn(dataViewColumn.Name)); |
| 98 | + } |
| 99 | + else if (type == NumberDataViewType.UInt32) |
| 100 | + { |
| 101 | + dataFrameColumns.Add(new UInt32DataFrameColumn(dataViewColumn.Name)); |
| 102 | + } |
| 103 | + else if (type == NumberDataViewType.UInt64) |
| 104 | + { |
| 105 | + dataFrameColumns.Add(new UInt64DataFrameColumn(dataViewColumn.Name)); |
| 106 | + } |
| 107 | + else if (type == NumberDataViewType.UInt16) |
| 108 | + { |
| 109 | + dataFrameColumns.Add(new UInt16DataFrameColumn(dataViewColumn.Name)); |
| 110 | + } |
| 111 | + else if (type == TextDataViewType.Instance) |
| 112 | + { |
| 113 | + dataFrameColumns.Add(new StringDataFrameColumn(dataViewColumn.Name)); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + throw new NotSupportedException(String.Format(Microsoft.Data.Strings.NotSupportedColumnType, type.RawType.Name)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + using (DataViewRowCursor cursor = dataView.GetRowCursor(activeDataViewColumns)) |
| 122 | + { |
| 123 | + Delegate[] activeColumnDelegates = new Delegate[activeDataViewColumns.Count]; |
| 124 | + int columnIndex = 0; |
| 125 | + foreach (DataViewSchema.Column activeDataViewColumn in activeDataViewColumns) |
| 126 | + { |
| 127 | + Delegate valueGetter = dataFrameColumns[columnIndex].GetValueGetterUsingCursor(cursor, activeDataViewColumn); |
| 128 | + activeColumnDelegates[columnIndex] = valueGetter; |
| 129 | + columnIndex++; |
| 130 | + } |
| 131 | + while (cursor.MoveNext() && cursor.Position < maxRows) |
| 132 | + { |
| 133 | + for (int i = 0; i < activeColumnDelegates.Length; i++) |
| 134 | + { |
| 135 | + dataFrameColumns[i].AddValueUsingCursor(cursor, activeColumnDelegates[i]); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return new DataFrame(dataFrameColumns); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments