Пример #1
0
 private static InternalSchemaDefinition GetInternalSchemaDefinition <TRow>(IHostEnvironment env, DataViewSchema schema)
     where TRow : class
 {
     Contracts.AssertValue(env);
     env.AssertValue(schema);
     return(InternalSchemaDefinition.Create(typeof(TRow), GetSchemaDefinition <TRow>(env, schema)));
 }
Пример #2
0
        public static InputRow <TRow> CreateInputRow <TRow>(IHostEnvironment env, SchemaDefinition schemaDefinition = null)
            where TRow : class
        {
            Contracts.AssertValue(env);
            env.AssertValueOrNull(schemaDefinition);
            var internalSchemaDefn = schemaDefinition == null
                ? InternalSchemaDefinition.Create(typeof(TRow), SchemaDefinition.Direction.Read)
                : InternalSchemaDefinition.Create(typeof(TRow), schemaDefinition);

            return(new InputRow <TRow>(env, internalSchemaDefn));
        }
Пример #3
0
        /// <summary>
        /// Create a Cursorable object on a given data view.
        /// </summary>
        /// <param name="env">Host environment.</param>
        /// <param name="data">The underlying data view.</param>
        /// <param name="ignoreMissingColumns">Whether to ignore missing columns in the data view.</param>
        /// <param name="schemaDefinition">The optional user-provided schema.</param>
        /// <returns>The constructed Cursorable.</returns>
        public static TypedCursorable <TRow> Create(IHostEnvironment env, IDataView data, bool ignoreMissingColumns, SchemaDefinition schemaDefinition)
        {
            Contracts.AssertValue(env);
            env.AssertValue(data);
            env.AssertValueOrNull(schemaDefinition);

            var outSchema = schemaDefinition == null
                ? InternalSchemaDefinition.Create(typeof(TRow), SchemaDefinition.Direction.Write)
                : InternalSchemaDefinition.Create(typeof(TRow), schemaDefinition);

            return(new TypedCursorable <TRow>(env, data, ignoreMissingColumns, outSchema));
        }
Пример #4
0
 /// <summary>
 /// Returns whether the column type <paramref name="colType"/> can be bound to field <paramref name="memberInfo"/>.
 /// They must both be vectors or scalars, and the raw data kind should match.
 /// </summary>
 private static bool IsCompatibleType(ColumnType colType, MemberInfo memberInfo)
 {
     InternalSchemaDefinition.GetVectorAndKind(memberInfo, out bool isVector, out DataKind kind);
     if (isVector)
     {
         return(colType.IsVector && colType.ItemType.RawKind == kind);
     }
     else
     {
         return(!colType.IsVector && colType.RawKind == kind);
     }
 }
Пример #5
0
 /// <summary>
 /// Returns whether the column type <paramref name="colType"/> can be bound to field <paramref name="memberInfo"/>.
 /// They must both be vectors or scalars, and the raw data type should match.
 /// </summary>
 private static bool IsCompatibleType(ColumnType colType, MemberInfo memberInfo)
 {
     InternalSchemaDefinition.GetVectorAndItemType(memberInfo, out bool isVector, out Type itemType);
     if (isVector)
     {
         return(colType is VectorType vectorType && vectorType.ItemType.RawType == itemType);
     }
     else
     {
         return(!(colType is VectorType) && colType.RawType == itemType);
     }
 }
Пример #6
0
        public static StreamingDataView <TRow> CreateFromEnumerable <TRow>(IHostEnvironment env, IEnumerable <TRow> data,
                                                                           SchemaDefinition schemaDefinition = null)
            where TRow : class
        {
            Contracts.AssertValue(env);
            env.AssertValue(data);
            env.AssertValueOrNull(schemaDefinition);
            var internalSchemaDefn = schemaDefinition == null
                ? InternalSchemaDefinition.Create(typeof(TRow), SchemaDefinition.Direction.Read)
                : InternalSchemaDefinition.Create(typeof(TRow), schemaDefinition);

            return(new StreamingDataView <TRow>(env, data, internalSchemaDefn));
        }
Пример #7
0
            private static Delegate[] MakePeeks(InternalSchemaDefinition schemaDef)
            {
                var peeks = new Delegate[schemaDef.Columns.Length];

                for (var i = 0; i < peeks.Length; i++)
                {
                    var currentColumn = schemaDef.Columns[i];
                    peeks[i] = currentColumn.IsComputed
                        ? currentColumn.Generator
                        : ApiUtils.GeneratePeek <InputRow <TRow>, TRow>(currentColumn);
                }
                return(peeks);
            }
Пример #8
0
            public InputRowBase(IHostEnvironment env, DataViewSchema schema, InternalSchemaDefinition schemaDef, Delegate[] peeks, Func <int, bool> predicate)
            {
                Contracts.AssertValue(env);
                Host = env.Register("Row");
                Host.AssertValue(schema);
                Host.AssertValue(schemaDef);
                Host.AssertValue(peeks);
                Host.AssertValue(predicate);
                Host.Assert(schema.Count == schemaDef.Columns.Length);
                Host.Assert(schema.Count == peeks.Length);

                _colCount = schema.Count;
                Schema    = schema;
                _getters  = new Delegate[_colCount];
                for (int c = 0; c < _colCount; c++)
                {
                    _getters[c] = predicate(c) ? CreateGetter(schema[c].Type, schemaDef.Columns[c], peeks[c]) : null;
                }
            }
Пример #9
0
        private static void ValidateMemberInfo(MemberInfo memberInfo, IDataView data)
        {
            if (!SchemaDefinition.NeedToCheckMemberInfo(memberInfo))
            {
                return;
            }

            var mappingNameAttr = memberInfo.GetCustomAttribute <ColumnNameAttribute>();
            var singleName      = mappingNameAttr?.Name ?? memberInfo.Name;

            Type actualType = null;
            bool isVector   = false;
            IEnumerable <Attribute> customAttributes = null;

            switch (memberInfo)
            {
            case FieldInfo fieldInfo:
                InternalSchemaDefinition.GetMappedType(fieldInfo.FieldType, out actualType, out isVector);
                customAttributes = fieldInfo.GetCustomAttributes();
                break;

            case PropertyInfo propertyInfo:
                InternalSchemaDefinition.GetMappedType(propertyInfo.PropertyType, out actualType, out isVector);
                customAttributes = propertyInfo.GetCustomAttributes();
                break;

            default:
                Contracts.Assert(false);
                throw Contracts.ExceptNotSupp("Expected a FieldInfo or a PropertyInfo");
            }

            if (!actualType.TryGetDataKind(out _) && !DataViewTypeManager.Knows(actualType, customAttributes))
            {
                int colIndex;
                data.Schema.TryGetColumnIndex(singleName, out colIndex);
                DataViewType expectedType = data.Schema[colIndex].Type;
                if (!actualType.Equals(expectedType.RawType))
                {
                    throw Contracts.ExceptParam(nameof(actualType), $"The expected type '{expectedType.RawType}' does not match the type of the '{singleName}' member: '{actualType}'. Please change the {singleName} member to '{expectedType.RawType}'");
                }
            }
        }
Пример #10
0
        private TypedCursorable(IHostEnvironment env, IDataView data, bool ignoreMissingColumns, InternalSchemaDefinition schemaDefn)
        {
            Contracts.AssertValue(env, "env");
            _host = env.Register("TypedCursorable");
            _host.AssertValue(data);
            _host.AssertValue(schemaDefn);

            _data = data;
            // Get column indices. Throw if there are missing columns (optionally, ignore them).
            var acceptedCols = new List <InternalSchemaDefinition.Column>();
            var indices      = new List <int>();

            foreach (var col in schemaDefn.Columns)
            {
                int colIndex;
                if (!_data.Schema.TryGetColumnIndex(col.ColumnName, out colIndex))
                {
                    if (ignoreMissingColumns)
                    {
                        continue;
                    }
                    throw _host.Except("Column '{0}' not found in the data view", col.ColumnName);
                }
                var realColType = _data.Schema[colIndex].Type;
                if (!IsCompatibleType(realColType, col.MemberInfo))
                {
                    throw _host.Except(
                              "Can't bind the IDataView column '{0}' of type '{1}' to field or property '{2}' of type '{3}'.",
                              col.ColumnName, realColType, col.MemberInfo.Name, col.FieldOrPropertyType.FullName);
                }

                acceptedCols.Add(col);
                indices.Add(colIndex);
            }
            _columns       = acceptedCols.ToArray();
            _columnIndices = indices.ToArray();
            _host.Assert(_columns.Length == _columnIndices.Length);

            int n = _columns.Length;

            _pokes = new Delegate[n];
            _peeks = new Delegate[n];
            var schema = _data.Schema;

            for (int i = 0; i < n; i++)
            {
                if (_columns[i].ColumnType.IsVector)
                {
                    _peeks[i] = ApiUtils.GeneratePeek <TypedCursorable <TRow>, TRow>(_columns[i]);
                }
                _pokes[i] = ApiUtils.GeneratePoke <TypedCursorable <TRow>, TRow>(_columns[i]);
            }
        }
Пример #11
0
 public InputRow(IHostEnvironment env, InternalSchemaDefinition schemaDef)
     : base(env, SchemaExtensions.MakeSchema(GetSchemaColumns(schemaDef)), schemaDef, MakePeeks(schemaDef), c => true)
 {
     _position = -1;
 }