示例#1
0
        /// <summary>
        /// Return whether the given column index is hidden in the given schema.
        /// </summary>
        public static bool IsHidden(this Schema schema, int col)
        {
            Contracts.CheckValue(schema, nameof(schema));
            string name = schema.GetColumnName(col);
            int    top;
            bool   tmp = schema.TryGetColumnIndex(name, out top);

            Contracts.Assert(tmp); // This would only be false if the implementation of schema were buggy.
            return(!tmp || top != col);
        }
示例#2
0
 private void BuildNameDict(int[] indexCopy, bool drop, out int[] sources, out int[] dropped, out Dictionary <string, int> nameToCol, bool user)
 {
     Contracts.AssertValue(indexCopy);
     foreach (int col in indexCopy)
     {
         if (col < 0 || _input.ColumnCount <= col)
         {
             const string fmt = "Column index {0} invalid for input with {1} columns";
             if (user)
             {
                 throw Contracts.ExceptUserArg(nameof(Arguments.Index), fmt, col, _input.ColumnCount);
             }
             else
             {
                 throw Contracts.ExceptDecode(fmt, col, _input.ColumnCount);
             }
         }
     }
     if (drop)
     {
         sources = Enumerable.Range(0, _input.ColumnCount).Except(indexCopy).ToArray();
         dropped = indexCopy;
     }
     else
     {
         sources = indexCopy;
         dropped = null;
     }
     if (user)
     {
         Contracts.CheckUserArg(sources.Length > 0, nameof(Arguments.Index), "Choose columns by index has no output columns");
     }
     else
     {
         Contracts.CheckDecode(sources.Length > 0, "Choose columns by index has no output columns");
     }
     nameToCol = new Dictionary <string, int>();
     for (int c = 0; c < sources.Length; ++c)
     {
         nameToCol[_input.GetColumnName(sources[c])] = c;
     }
 }
 public ISlotCursor GetSlotCursor(int col)
 {
     _host.CheckParam(0 <= col && col < Schema.ColumnCount, nameof(col));
     if (TransposeSchema?.GetSlotType(col) == null)
     {
         throw _host.ExceptParam(nameof(col), "Bad call to GetSlotCursor on untransposable column '{0}'",
                                 Schema.GetColumnName(col));
     }
     _host.AssertValue(_tview);
     return(_tview.GetSlotCursor(col));
 }
示例#4
0
        private static void PrintSchema(TextWriter writer, Arguments args, Schema schema, ITransposeSchema tschema)
        {
            Contracts.AssertValue(writer);
            Contracts.AssertValue(args);
            Contracts.AssertValue(schema);
            Contracts.AssertValueOrNull(tschema);
#if !CORECLR
            if (args.ShowJson)
            {
                writer.WriteLine("Json Schema not supported.");
                return;
            }
#endif
            int colLim = schema.ColumnCount;

            var itw = new IndentedTextWriter(writer, "  ");
            itw.WriteLine("{0} columns:", colLim);
            using (itw.Nest())
            {
                var names = default(VBuffer <ReadOnlyMemory <char> >);
                for (int col = 0; col < colLim; col++)
                {
                    var name     = schema.GetColumnName(col);
                    var type     = schema.GetColumnType(col);
                    var slotType = tschema == null ? null : tschema.GetSlotType(col);
                    itw.WriteLine("{0}: {1}{2}", name, type, slotType == null ? "" : " (T)");

                    bool metaVals = args.ShowMetadataValues;
                    if (metaVals || args.ShowMetadataTypes)
                    {
                        ShowMetadata(itw, schema, col, metaVals);
                        continue;
                    }

                    if (!args.ShowSlots)
                    {
                        continue;
                    }
                    if (!type.IsKnownSizeVector)
                    {
                        continue;
                    }
                    ColumnType typeNames;
                    if ((typeNames = schema.GetMetadataTypeOrNull(MetadataUtils.Kinds.SlotNames, col)) == null)
                    {
                        continue;
                    }
                    if (typeNames.VectorSize != type.VectorSize || !typeNames.ItemType.IsText)
                    {
                        Contracts.Assert(false, "Unexpected slot names type");
                        continue;
                    }
                    schema.GetMetadata(MetadataUtils.Kinds.SlotNames, col, ref names);
                    if (names.Length != type.VectorSize)
                    {
                        Contracts.Assert(false, "Unexpected length of slot names vector");
                        continue;
                    }

                    using (itw.Nest())
                    {
                        bool verbose = args.Verbose ?? false;
                        foreach (var kvp in names.Items(all: verbose))
                        {
                            if (verbose || !kvp.Value.IsEmpty)
                            {
                                itw.WriteLine("{0}:{1}", kvp.Key, kvp.Value);
                            }
                        }
                    }
                }
            }
        }
            public Contents(ModelLoadContext ctx, Schema input, Func <ColumnType[], string> testTypes)
            {
                Contracts.CheckValue(ctx, nameof(ctx));
                Contracts.CheckValue(input, nameof(input));
                Contracts.CheckValueOrNull(testTypes);

                Input = input;

                // *** Binary format ***
                // int: number of added columns
                // for each added column
                //   int: id of output column name
                //   int: number of input column names
                //   int[]: ids of input column names
                int cinfo = ctx.Reader.ReadInt32();

                Contracts.CheckDecode(cinfo > 0);

                Infos = new ColInfo[cinfo];
                Names = new string[cinfo];
                for (int i = 0; i < cinfo; i++)
                {
                    Names[i] = ctx.LoadNonEmptyString();

                    int csrc = ctx.Reader.ReadInt32();
                    Contracts.CheckDecode(csrc > 0);
                    int[] indices  = new int[csrc];
                    var   srcTypes = new ColumnType[csrc];
                    int?  srcSize  = 0;
                    for (int j = 0; j < csrc; j++)
                    {
                        string src = ctx.LoadNonEmptyString();
                        if (!input.TryGetColumnIndex(src, out indices[j]))
                        {
                            throw Contracts.Except("Source column '{0}' is required but not found", src);
                        }
                        srcTypes[j] = input.GetColumnType(indices[j]);
                        var size = srcTypes[j].ValueCount;
                        srcSize = size == 0 ? null : checked (srcSize + size);
                    }

                    if (testTypes != null)
                    {
                        string reason = testTypes(srcTypes);
                        if (reason != null)
                        {
                            throw Contracts.Except("Source columns '{0}' have invalid types: {1}. Source types: '{2}'.",
                                                   string.Join(", ", indices.Select(k => input.GetColumnName(k))),
                                                   reason,
                                                   string.Join(", ", srcTypes.Select(type => type.ToString())));
                        }
                    }

                    Infos[i] = new ColInfo(srcSize.GetValueOrDefault(), indices, srcTypes);
                }
            }
示例#6
0
 public string GetColumnName(int col)
 {
     return(_inputSchema.GetColumnName(col));
 }