Ejemplo n.º 1
0
        /// <summary>
        /// Checks whether a column kind in a <see cref="RoleMappedData"/> is unique, and its type
        /// is a <see cref="InternalDataKind.U4"/> key of known cardinality.
        /// </summary>
        /// <param name="data">The training examples</param>
        /// <param name="role">The column role to try to extract</param>
        /// <param name="col">The extracted schema column</param>
        /// <param name="isDecode">Whether a non-user error should be thrown as a decode</param>
        /// <returns>The type cast to a key-type</returns>
        private static KeyDataViewType CheckRowColumnType(RoleMappedData data, RoleMappedSchema.ColumnRole role, out DataViewSchema.Column col, bool isDecode)
        {
            Contracts.AssertValue(data);
            Contracts.AssertValue(role.Value);

            const string format2 = "There should be exactly one column with role {0}, but {1} were found instead";

            if (!data.Schema.HasUnique(role))
            {
                int kindCount = Utils.Size(data.Schema.GetColumns(role));
                if (isDecode)
                {
                    throw Contracts.ExceptDecode(format2, role.Value, kindCount);
                }
                throw Contracts.Except(format2, role.Value, kindCount);
            }
            col = data.Schema.GetColumns(role)[0];

            // REVIEW tfinley: Should we be a bit less restrictive? This doesn't seem like
            // too terrible of a restriction.
            const string    format = "Column '{0}' with role {1} should be a known cardinality U4 key, but is instead '{2}'";
            KeyDataViewType keyType;

            if (!TryMarshalGoodRowColumnType(col.Type, out keyType))
            {
                if (isDecode)
                {
                    throw Contracts.ExceptDecode(format, col.Name, role.Value, col.Type);
                }
                throw Contracts.Except(format, col.Name, role.Value, col.Type);
            }
            return(keyType);
        }
        private static RoleMappedData GetDataRoles(IHostEnvironment env, Arguments input)
        {
            var roles = new List <KeyValuePair <RoleMappedSchema.ColumnRole, string> >();

            if (input.LabelColumns != null)
            {
                env.Check(input.LabelColumns.Length == 1, "LabelColumns expected one column name to be specified.");
                roles.Add(RoleMappedSchema.ColumnRole.Label.Bind(input.LabelColumns[0]));
            }

            if (input.GroupColumns != null)
            {
                env.Check(input.GroupColumns.Length == 1, "GroupColumns expected one column name to be specified.");
                roles.Add(RoleMappedSchema.ColumnRole.Group.Bind(input.GroupColumns[0]));
            }

            if (input.WeightColumns != null)
            {
                env.Check(input.WeightColumns.Length == 1, "WeightColumns expected one column name to be specified.");
                roles.Add(RoleMappedSchema.ColumnRole.Weight.Bind(input.WeightColumns[0]));
            }

            if (input.NameColumns != null)
            {
                env.Check(input.NameColumns.Length == 1, "NameColumns expected one column name to be specified.");
                roles.Add(RoleMappedSchema.ColumnRole.Name.Bind(input.NameColumns[0]));
            }

            if (input.NumericFeatureColumns != null)
            {
                var numericFeature = new RoleMappedSchema.ColumnRole(ColumnPurpose.NumericFeature.ToString());
                foreach (var colName in input.NumericFeatureColumns)
                {
                    var item = numericFeature.Bind(colName);
                    roles.Add(item);
                }
            }

            if (input.CategoricalFeatureColumns != null)
            {
                var categoricalFeature = new RoleMappedSchema.ColumnRole(ColumnPurpose.CategoricalFeature.ToString());
                foreach (var colName in input.CategoricalFeatureColumns)
                {
                    var item = categoricalFeature.Bind(colName);
                    roles.Add(item);
                }
            }

            if (input.TextFeatureColumns != null)
            {
                var textFeature = new RoleMappedSchema.ColumnRole(ColumnPurpose.TextFeature.ToString());
                foreach (var colName in input.TextFeatureColumns)
                {
                    var item = textFeature.Bind(colName);
                    roles.Add(item);
                }
            }

            if (input.ImagePathColumns != null)
            {
                var imagePath = new RoleMappedSchema.ColumnRole(ColumnPurpose.ImagePath.ToString());
                foreach (var colName in input.ImagePathColumns)
                {
                    var item = imagePath.Bind(colName);
                    roles.Add(item);
                }
            }

            return(new RoleMappedData(input.TrainingData, roles));
        }