Beispiel #1
0
        ISchemaBoundMapper ISchemaBindableMapper.Bind(IHostEnvironment env, RoleMappedSchema schema)
        {
            Contracts.CheckValue(env, nameof(env));

            using (var ch = env.Register("SchemaBindableWrapper").Start("Bind"))
            {
                ch.CheckValue(schema, nameof(schema));
                if (schema.Feature?.Type is DataViewType type)
                {
                    // Ensure that the feature column type is compatible with the needed input type.
                    var typeIn = ValueMapper != null ? ValueMapper.InputType : new VectorType(NumberDataViewType.Single);
                    if (type != typeIn)
                    {
                        VectorType typeVectorType   = type as VectorType;
                        VectorType typeInVectorType = typeIn as VectorType;

                        DataViewType typeItemType   = typeVectorType?.ItemType ?? type;
                        DataViewType typeInItemType = typeInVectorType?.ItemType ?? typeIn;

                        if (!typeItemType.Equals(typeInItemType))
                        {
                            throw ch.Except("Incompatible features column type item type: '{0}' vs '{1}'", typeItemType, typeInItemType);
                        }
                        if ((typeVectorType != null) != (typeInVectorType != null))
                        {
                            throw ch.Except("Incompatible features column type: '{0}' vs '{1}'", type, typeIn);
                        }
                        // typeIn can legally have unknown size.
                        int typeVectorSize   = typeVectorType?.Size ?? 0;
                        int typeInVectorSize = typeInVectorType?.Size ?? 0;
                        if (typeVectorSize != typeInVectorSize && typeInVectorSize > 0)
                        {
                            throw ch.Except("Incompatible features column type: '{0}' vs '{1}'", type, typeIn);
                        }
                    }
                }
                return(BindCore(ch, schema));
            }
        }
        /// <summary>
        /// Equivalent to calling Equals(ColumnType) for non-vector types. For vector type,
        /// returns true if current and other vector types have the same size and item type.
        /// </summary>
        public static bool SameSizeAndItemType(this DataViewType columnType, DataViewType other)
        {
            if (other == null)
            {
                return(false);
            }

            if (columnType.Equals(other))
            {
                return(true);
            }

            // For vector types, we don't care about the factoring of the dimensions.
            if (!(columnType is VectorType vectorType) || !(other is VectorType otherVectorType))
            {
                return(false);
            }
            if (!vectorType.ItemType.Equals(otherVectorType.ItemType))
            {
                return(false);
            }
            return(vectorType.Size == otherVectorType.Size);
        }