private static void RunPerformanceTest()
        {
            int iterations = 25000;

            Console.OutputEncoding = Encoding.UTF8;
            Stopwatch stopwatch = new Stopwatch();
            long      total     = 0;

            for (int i = 0; i < iterations; i++)
            {
                Console.SetCursorPosition(0, 0);
                stopwatch.Restart();

                Table table = new Table("One", "Two", "Three");
                table.Config = TableConfiguration.Unicode();
                table.AddRow("1", "2", "3");
                table.AddRow("Short", "item", "Here");
                table.AddRow("Longer items go here", "stuff", "stuff");

                string tableString = table.ToString();

                total += stopwatch.ElapsedTicks;
                Console.WriteLine(i);
            }
            Console.WriteLine(total / iterations);
        }
        private static void ShowExampleTables()
        {
            Table table = new Table("One", "Two", "Three");

            table.AddRow("1", "2", "3");
            table.AddRow("Short", "item", "Here");
            table.AddRow("Longer items go here", "stuff stuff", "stuff");

            table.Config = TableConfiguration.Default();
            string test = String.Empty.PadRight(Console.WindowWidth, '-');

            Console.Write(test);
            Console.SetCursorPosition(0, 0);
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.Markdown();
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.MySql();
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.MySqlSimple();
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.Unicode();
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.UnicodeAlt();
            Console.Write(table.ToString());
            Console.WriteLine();
        }
예제 #3
0
        private static void ShowExampleTables()
        {
            ShowAlignedTables();
            Console.WriteLine();

            Table table = new Table("One", "Two", "Three");

            table.AddRow("1", "2", "3");
            table.AddRow("Short", "item", "Here");
            table.AddRow("Longer items go here", "stuff stuff", "stuff");

            table.Config = TableConfiguration.Default();

            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.Markdown();
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.MySql();
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.MySqlSimple();
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.Unicode();
            Console.Write(table.ToString());
            Console.WriteLine();
            table.Config = TableConfiguration.UnicodeAlt();
            Console.Write(table.ToString());
            Console.WriteLine();
        }
예제 #4
0
        /// <summary>
        /// Create table
        /// </summary>
        /// <param name="columns"></param>
        /// <param name="rows"></param>
        /// <param name="outputType"></param>
        /// <param name="hasInnerRows"></param>
        /// <returns></returns>
        private static string Create(IEnumerable <ColumnHeader> columns,
                                     IEnumerable <object[]> rows,
                                     TableOutputType outputType,
                                     bool hasInnerRows)
        {
            var table = new Table(columns.ToArray());

            switch (outputType)
            {
            case TableOutputType.Text: table.Config = TableConfiguration.Default(); break;

            case TableOutputType.Unicode: table.Config = TableConfiguration.Unicode(); break;

            case TableOutputType.UnicodeAlt: table.Config = TableConfiguration.UnicodeAlt(); break;

            case TableOutputType.Markdown: table.Config = TableConfiguration.Markdown(); break;

            case TableOutputType.Html: table.Config = TableConfiguration.Unicode(); break;

            default: break;
            }

            table.Config.hasInnerRows = hasInnerRows;
            table.AddRows(rows);

            var ret = table.ToString();

            if (outputType == TableOutputType.Html)
            {
                ret = ToHtml(ret);
            }

            return(ret);
        }
예제 #5
0
        private static void RenderingTables()
        {
            var rand = new Random();
            int num1 = rand.Next(), num2 = rand.Next(), num3 = rand.Next();

            Table table = new Table("One", "Two", "Three")
                          .AddRow(num1++, num2++, num3++)
                          .AddRow("Short", "item", "Here")
                          .AddRow("Longer items go here", "stuff stuff", "stuff");

            table.Config = TableConfiguration.Unicode();

            var table2 = new Table("Log")
                         .AddRow($"{DateTime.Now} log something 1")
                         .AddRow($"{DateTime.Now} log something 2")
                         .AddRow($"{DateTime.Now} log something 3")
                         .AddRow($"{DateTime.Now} log something 4")
                         .AddRow($"{DateTime.Now} log something 5 ver long ........");

            table2.Config = TableConfiguration.Unicode();
            var tables = new ConsoleTables(table, table2);

            System.Console.WriteLine(tables.ToString());

            System.Console.WriteLine("Press CTRL+C to terminate watch log.");
        }
예제 #6
0
        /// <summary>
        /// Helper method to write the machine learning pipeline to the console.
        /// </summary>
        /// <param name="mlContext">The machine learning context.</param>
        /// <param name="dataView">The data view object holding the data.</param>
        /// <param name="pipeline">The machine learning pipeline.</param>
        /// <param name="numberOfRows">The maximum number of rows to write.</param>
        public static void WritePipeline(MLContext mlContext, IDataView dataView, IEstimator <ITransformer> pipeline, int numberOfRows = 4)
        {
            // helper method to write a value to the console table
            object WriteValue(object value)
            {
                if (value is VBuffer <float> )
                {
                    return("<vector>");
                }
                else
                {
                    return(value);
                }
            }

            // get a preview of the transformed data
            var transformer     = pipeline.Fit(dataView);
            var transformedData = transformer.Transform(dataView);
            var preview         = transformedData.Preview(maxRows: numberOfRows);

            // set up a console table
            var table = new Table(
                TableConfiguration.Unicode(),
                (from c in preview.ColumnView select c.Column.Name).ToArray());

            // fill the table with results
            foreach (var row in preview.RowView)
            {
                table.AddRow((from c in row.Values select WriteValue(c.Value)).ToArray());
            }

            // write the table
            Console.WriteLine(table.ToString());
        }
예제 #7
0
        public static void WriteTable(this ICollection <Claim> claims, TextWriter writer = null)
        {
            var table = new Table("id", "add player", "drop player")
            {
                Config = TableConfiguration.Unicode()
            };

            if (claims.Any())
            {
                table.AddRows(claims
                              .OrderBy(c => c.Priority)
                              .ThenBy(c => c.Id)
                              .Select(c => new[]
                {
                    c.Id.ToString(),
                    c.Add.Description,
                    c.Drop.Description
                }));
            }
            else
            {
                table.AddRow("", "", "");
            }

            (writer ?? Console.Out).Write(new ConsoleTables(table));
        }
        public static void Print(Competion competion)
        {
            if (competion == null)
            {
                throw new ArgumentNullException(nameof(competion));
            }

            var printPlayers     = new Table("Player");
            var printWinners     = new Table("Comp ID", "Game ID", "Winner");
            var printCompetitors = new Table("Game ID", "Name", "Score", "Winner");

            // printWinners.Config = TableConfiguration.Markdown();
            printPlayers.Config     = TableConfiguration.Unicode();
            printWinners.Config     = TableConfiguration.Unicode();
            printCompetitors.Config = TableConfiguration.Unicode();

            foreach (var name in competion.Players.Select(p => p.FullName))
            {
                printPlayers.AddRow(name);
            }

            foreach (var game in competion.Games)
            {
                printWinners.AddRow(competion.Id, game.Id, game.Winner.Player.FullName);

                foreach (var competitor in game.Competitors)
                {
                    printCompetitors.AddRow(game.Id, competitor.Player.FullName, competitor.Score, competitor.Id == game.Winner.Id ? ":) - Winner" : ":(");
                }
            }

            var tables = new ConsoleTables(printPlayers, printWinners, printCompetitors);

            Console.Write(tables.ToString());
        }
예제 #9
0
        public static void Run()
        {
            Console.OutputEncoding = Encoding.UTF8;

            Clock.BenchmarkTime(() =>
            {
                Table table  = new Table("One", "Two", "Three");
                table.Config = TableConfiguration.Unicode();
                table.AddRow("1", "2", "3");
                table.AddRow("Short", "item", "Here");
                table.AddRow("Longer items go here", "stuff", "stuff");

                string tableString = table.ToString();
            }, 100, 500);
        }
예제 #10
0
        /// <summary>
        /// Create table
        /// </summary>
        /// <param name="columns"></param>
        /// <param name="rows"></param>
        /// <param name="outputType"></param>
        /// <param name="hasInnerRows"></param>
        /// <returns></returns>
        private static string Create(IEnumerable <ColumnHeader> columns,
                                     IEnumerable <object[]> rows,
                                     TableOutputType outputType,
                                     bool hasInnerRows)
        {
            string ret;

            if (rows.Count() == 0)
            {
                ret = "";
            }
            else if (outputType == TableOutputType.Html)
            {
                ret = ToHtml(columns, rows);
            }
            else if (outputType == TableOutputType.Json)
            {
                ret = ToJson(columns.ToArray(), rows, false);
            }
            else if (outputType == TableOutputType.JsonPretty)
            {
                ret = ToJson(columns.ToArray(), rows, true);
            }
            else
            {
                var table = new Table(columns.ToArray());

                switch (outputType)
                {
                case TableOutputType.Text: table.Config = TableConfiguration.Default(); break;

                case TableOutputType.Unicode: table.Config = TableConfiguration.Unicode(); break;

                case TableOutputType.UnicodeAlt: table.Config = TableConfiguration.UnicodeAlt(); break;

                case TableOutputType.Markdown: table.Config = TableConfiguration.Markdown(); break;

                default: break;
                }

                table.Config.hasInnerRows = hasInnerRows;
                table.AddRows(rows);
                ret = table.ToString();
            }

            return(ret);
        }
예제 #11
0
        /// <summary>
        /// Helper method to write the machine learning pipeline to the console.
        /// </summary>
        /// <param name="preview">The data preview to write.</param>
        /// <param name="numberOfRows">The maximum number of rows to write.</param>
        public static void WritePreview(DataDebuggerPreview preview, int numberOfRows = 10)
        {
            // set up a console table
            var table = new Table(
                TableConfiguration.Unicode(),
                (from c in preview.ColumnView select c.Column.Name).ToArray());

            // fill the table with results
            foreach (var row in preview.RowView)
            {
                table.AddRow((from c in row.Values
                              select c.Value is VBuffer <float>? "<vector>" : c.Value
                              ).ToArray());
            }

            // write the table
            Console.WriteLine(table.ToString());
        }
예제 #12
0
        /// <summary>
        /// Helper method to write a single column preview to the console.
        /// </summary>
        /// <param name="preview">The data preview to write.</param>
        /// <param name="column">The name of the column to write.</param>
        public static void WritePreviewColumn(DataDebuggerPreview preview, string column)
        {
            // set up a console table
            var table = new Table(TableConfiguration.Unicode(), new ColumnHeader(column));

            // fill the table with results
            foreach (var row in preview.RowView)
            {
                foreach (var col in row.Values)
                {
                    if (col.Key == column)
                    {
                        var vector = (VBuffer <float>)col.Value;
                        table.AddRow(string.Concat(vector.DenseValues()));
                    }
                }
            }

            // write the table
            Console.WriteLine(table.ToString());
        }
예제 #13
0
        public static void WriteTable(this ICollection <PlayerResponse> players, TextWriter writer = null)
        {
            var table = new Table("id", "name", "pos", "team")
            {
                Config = TableConfiguration.Unicode()
            };

            if (players.Any())
            {
                table.AddRows(players
                              .OrderBy(p => p.Name.Last, StringComparer.OrdinalIgnoreCase)
                              .ThenBy(p => p.Name.First, StringComparer.OrdinalIgnoreCase)
                              .Select(p => new[] { p.PlayerId.ToString(), p.Name.Full, p.Position, p.TeamAbbr }));
            }
            else
            {
                table.AddRow("", "", "", "");
            }

            (writer ?? Console.Out).Write(new ConsoleTables(table));
        }
예제 #14
0
        /// <summary>
        /// Helper method to write the machine learning pipeline to the console.
        /// </summary>
        /// <param name="preview">The data preview to write.</param>
        public static void WritePreview(DataDebuggerPreview preview)
        {
            // set up a console table
            var table = new Table(
                TableConfiguration.Unicode(),
                preview.ColumnView.Select(c => new ColumnHeader(c.Column.Name)).ToArray());

            // fill the table with results
            foreach (var row in preview.RowView)
            {
                table.AddRow((from c in row.Values
                              select c.Value is VBuffer <float>? "<vector>" : c.Value
                              ).ToArray());
            }

            // write the table
            Console.WriteLine(table.ToString());

            // set up a console table
            table = new Table(TableConfiguration.Unicode(), new ColumnHeader("Location"));

            // fill the table with results
            foreach (var row in preview.RowView)
            {
                foreach (var col in row.Values)
                {
                    if (col.Key == "Location")
                    {
                        var vector = (VBuffer <float>)col.Value;
                        table.AddRow(string.Concat(vector.DenseValues()));
                    }
                }
            }

            // write the table
            Console.WriteLine(table.ToString());
        }
예제 #15
0
        /// <summary>
        /// The main program entry point.
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        static void Main(string[] args)
        {
            // create a machine learning context
            var context = new MLContext();

            // load data
            Console.WriteLine("Loading data....");
            var columnDef = new TextLoader.Column[]
            {
                new TextLoader.Column(nameof(Digit.PixelValues), DataKind.Single, 1, 784),
                new TextLoader.Column("Number", DataKind.Single, 0)
            };
            var trainDataView = context.Data.LoadFromTextFile(
                path: trainDataPath,
                columns: columnDef,
                hasHeader: true,
                separatorChar: ',');
            var testDataView = context.Data.LoadFromTextFile(
                path: testDataPath,
                columns: columnDef,
                hasHeader: true,
                separatorChar: ',');


            // build a training pipeline
            // step 1: map the number column to a key value and store in the label column
            var pipeline = context.Transforms.Conversion.MapValueToKey(
                outputColumnName: "Label",
                inputColumnName: "Number",
                keyOrdinality: ValueToKeyMappingEstimator.KeyOrdinality.ByValue)

                           // step 2: concatenate all feature columns
                           .Append(context.Transforms.Concatenate(
                                       "Features",
                                       nameof(Digit.PixelValues)))

                           // step 3: cache data to speed up training
                           .AppendCacheCheckpoint(context)

                           // step 4: train the model with SDCA
                           .Append(context.MulticlassClassification.Trainers.SdcaMaximumEntropy(
                                       labelColumnName: "Label",
                                       featureColumnName: "Features"))

                           // step 5: map the label key value back to a number
                           .Append(context.Transforms.Conversion.MapKeyToValue(
                                       outputColumnName: "Number",
                                       inputColumnName: "Label"));

            // train the model
            Console.WriteLine("Training model....");
            var model = pipeline.Fit(trainDataView);

            // use the model to make predictions on the test data
            Console.WriteLine("Evaluating model....");
            var predictions = model.Transform(testDataView);

            // evaluate the predictions
            var metrics = context.MulticlassClassification.Evaluate(
                data: predictions,
                labelColumnName: "Number",
                scoreColumnName: "Score");

            // show evaluation metrics
            Console.WriteLine($"Evaluation metrics");
            Console.WriteLine($"    MicroAccuracy:    {metrics.MicroAccuracy:0.###}");
            Console.WriteLine($"    MacroAccuracy:    {metrics.MacroAccuracy:0.###}");
            Console.WriteLine($"    LogLoss:          {metrics.LogLoss:#.###}");
            Console.WriteLine($"    LogLossReduction: {metrics.LogLossReduction:#.###}");
            Console.WriteLine();

            // grab three digits from the test data
            var digits     = context.Data.CreateEnumerable <Digit>(testDataView, reuseRowObject: false).ToArray();
            var testDigits = new Digit[] { digits[5], digits[16], digits[28], digits[63], digits[129] };

            // create a prediction engine
            var engine = context.Model.CreatePredictionEngine <Digit, DigitPrediction>(model);

            // set up a table to show the predictions
            var table = new Table(TableConfiguration.Unicode());

            table.AddColumn("Digit");
            for (var i = 0; i < 10; i++)
            {
                table.AddColumn($"P{i}");
            }

            // predict each test digit
            for (var i = 0; i < testDigits.Length; i++)
            {
                var prediction = engine.Predict(testDigits[i]);
                table.AddRow(
                    testDigits[i].Number,
                    prediction.Score[0].ToString("P2"),
                    prediction.Score[1].ToString("P2"),
                    prediction.Score[2].ToString("P2"),
                    prediction.Score[3].ToString("P2"),
                    prediction.Score[4].ToString("P2"),
                    prediction.Score[5].ToString("P2"),
                    prediction.Score[6].ToString("P2"),
                    prediction.Score[7].ToString("P2"),
                    prediction.Score[8].ToString("P2"),
                    prediction.Score[9].ToString("P2"));
            }

            // show results
            Console.WriteLine(table.ToString());
            Console.ReadKey();
        }
예제 #16
0
        static void Main(string[] args)
        {
            var context = new MLContext();

            Console.WriteLine("Loading Data...");

            var colDef = new TextLoader.Column[] {
                new TextLoader.Column(nameof(Digit.PixelValues), DataKind.Single, 1, 784),
                new TextLoader.Column("Number", DataKind.Single, 0)
            };

            var trainDataView = context.Data.LoadFromTextFile(trainDataPath, colDef, hasHeader: true, separatorChar: ',');
            var testDataView  = context.Data.LoadFromTextFile(testDataPath, colDef, hasHeader: true, separatorChar: ',');

            var pipeline = context.Transforms.Conversion.MapValueToKey(outputColumnName: "Label", inputColumnName: "Number", keyOrdinality: ValueToKeyMappingEstimator.KeyOrdinality.ByValue)
                           .Append(context.Transforms.Concatenate("Features", nameof(Digit.PixelValues)))
                           .AppendCacheCheckpoint(context)
                           .Append(context.MulticlassClassification.Trainers.OneVersusAll(context.BinaryClassification.Trainers.FastForest(), "Label"))
                           .Append(context.Transforms.Conversion.MapKeyToValue(outputColumnName: "Number", inputColumnName: "Label"));

            Console.WriteLine("Training the model...");
            var model = pipeline.Fit(trainDataView);

            Console.WriteLine("Evaluating model...");
            var predictions = model.Transform(testDataView);

            var metrics = context.MulticlassClassification.Evaluate(predictions, labelColumnName: "Number", scoreColumnName: "Score");

            // show evaluation metrics
            Console.WriteLine($"Evaluation metrics");
            Console.WriteLine($"    MicroAccuracy:    {metrics.MicroAccuracy:0.###}");
            Console.WriteLine($"    MacroAccuracy:    {metrics.MacroAccuracy:0.###}");
            Console.WriteLine($"    LogLoss:          {metrics.LogLoss:#.###}");
            Console.WriteLine($"    LogLossReduction: {metrics.LogLossReduction:#.###}");
            Console.WriteLine();

            var digits = context.Data.CreateEnumerable <Digit>(testDataView, false).ToArray();

            var testDigits = new Digit[] {
                digits[215], // 0
                digits[202], // 1
                digits[199], // 2
                digits[200], // 3
                digits[198], // 4
                digits[207], // 5
                digits[201], // 6
                digits[220], // 7
                digits[226], // 8
                digits[235]  // 9
            };

            var engine = context.Model.CreatePredictionEngine <Digit, DigitPrediction>(model);

            var table = new BetterConsoleTables.Table(TableConfiguration.Unicode());

            table.AddColumn("Digits");
            for (var i = 0; i < 10; i++)
            {
                table.AddColumn($"P{i}");
            }

            for (var i = 0; i < testDigits.Length; i++)
            {
                var prediction = engine.Predict(testDigits[i]);
                table.AddRow(
                    testDigits[i].Number,
                    prediction.Score[0].ToString("P2"),
                    prediction.Score[1].ToString("P2"),
                    prediction.Score[2].ToString("P2"),
                    prediction.Score[3].ToString("P2"),
                    prediction.Score[4].ToString("P2"),
                    prediction.Score[5].ToString("P2"),
                    prediction.Score[6].ToString("P2"),
                    prediction.Score[7].ToString("P2"),
                    prediction.Score[8].ToString("P2"),
                    prediction.Score[9].ToString("P2"));
            }

            // show results
            Console.WriteLine(table.ToString());
        }