コード例 #1
0
        public void Can_compute_outlier_function()
        {
            var items = new[] { 71, 70, 73, 70, 70, 69, 70, 72, 71, 300, 71, 69, 2 };
            var list  = items.Select((c, i) => Tuple.Create("City" + i, c));

            var dm = DataMatrixFactory.Create(list, new[] { "City", "Temperature" },
                                              new Func <Tuple <string, int>, object>[] { a => a.Item1, a => a.Item2 }, new DataMatrixColumn
            {
                Name       = "Outlier",
                DependsOn  = new[] { "Temperature" },
                Initialize = Outlier.Initialize,
                Compute    = Outlier.Compute
            });

            Assert.Equal(71, dm[0][1]);
            Assert.Equal(Outlier.OutlierType.None, dm[0][2]);

            Assert.Equal(300, dm[9][1]);
            Assert.Equal(Outlier.OutlierType.Major, dm[9][2]);

            Assert.Equal(2, dm[12][1]);
            Assert.Equal(Outlier.OutlierType.Major, dm[12][2]);

            var tb = dm.ToDataTable();

            Assert.Equal(3, tb.Columns.Count);
        }
コード例 #2
0
        public void Can_compute_standard_deviation_function()
        {
            var heights = new[] { 600, 470, 170, 430, 300 };
            var names   = new[] { "Rottweilers", "Labrador", "Dachshunds", "Terrier", "Bulldog" };
            var list    = heights.Select((c, i) => Tuple.Create(names[i], c));

            var dm = DataMatrixFactory.Create(list, new[] { "Dog", "Height" },
                                              new Func <Tuple <string, int>, object>[] { a => a.Item1, a => a.Item2 }, new DataMatrixColumn
            {
                Name       = "Deviation",
                DependsOn  = new[] { "Height" },
                Initialize = StandardDeviation.Initialize,
                Compute    = StandardDeviation.Compute
            });

            VerifyDeviation(dm, 0, "Rottweilers", 600, 2);
            VerifyDeviation(dm, 1, "Labrador", 470, 1);
            VerifyDeviation(dm, 2, "Dachshunds", 170, -2);
            VerifyDeviation(dm, 3, "Terrier", 430, 1);
            VerifyDeviation(dm, 4, "Bulldog", 300, -1);

            var tb = dm.ToDataTable();

            Assert.Equal(3, tb.Columns.Count);
        }
コード例 #3
0
        public void Can_create_dataMatrix_from_list_add_headers()
        {
            var list = LoadStudents();
            var dm   = DataMatrixFactory.Create(list, new[] { "FirstName", "LastName", "MyScore" },
                                                new Func <Student, object>[] { a => a.FirstName, a => a.LastName, a => a.Score });

            Assert.Equal(39, dm.RowCount);
            Assert.Equal(3, dm.Columns.Count);
            VerifyStudents(dm);
        }
コード例 #4
0
        public void Can_create_dataMatrix_from_list()
        {
            var list = LoadStudents();
            var dm   = DataMatrixFactory.Create(list,
                                                new Expression <Func <Student, object> >[] { a => a.FirstName, a => a.LastName, a => a.Score });

            Assert.Equal(39, dm.RowCount);
            Assert.Equal(3, dm.Columns.Count);
            VerifyStudents(dm);
        }
コード例 #5
0
        public void Can_create_dataMatrix_from_dataTable(string columns, int count)
        {
            var table = LoadStudents().ToDataTable();
            var dm    = columns == null?DataMatrixFactory.Create(table) : DataMatrixFactory.Create(table, columns);

            Assert.Equal(39, dm.RowCount);
            Assert.Equal(count, dm.Columns.Count);

            if (count == 3)
            {
                VerifyStudents(dm);
            }
        }
コード例 #6
0
        public void Can_compute_percentile_rank_function()
        {
            var table = _students.ToDataTable();
            var dm    = DataMatrixFactory.Create(table, "FirstName,LastName,Score", new DataMatrixColumn
            {
                Name       = "PercentileRank",
                DependsOn  = new[] { "Score" },
                Initialize = PercentileRank.Initialize,
                Compute    = PercentileRank.Compute
            });

            Assert.Equal(39, dm.RowCount);
            Assert.Equal(6.41, dm[0][3]);


            var tb = dm.ToDataTable();

            Assert.Equal(4, tb.Columns.Count);
        }
コード例 #7
0
        public void Can_compute_by_concat_function_and_reorder_DataTable()
        {
            var table = _students.ToDataTable();
            var dm    = DataMatrixFactory.Create(table, "Score", new DataMatrixColumn
            {
                Name      = "Name",
                DependsOn = new[] { "FirstName", "LastName" },
                Compute   = (values, args) => values[0] + " " + values[1]
            });

            Assert.Equal(39, dm.RowCount);
            Assert.Equal("David Johnson", dm[0][1]);

            var tb = dm.ToDataTable("Name", "Score");

            Assert.Equal(2, tb.Columns.Count);
            Assert.Equal("Name", tb.Columns[0].ColumnName);
            Assert.Equal("Score", tb.Columns[1].ColumnName);
        }
コード例 #8
0
ファイル: PivotExtensions.cs プロジェクト: tawani/datamatrix
        public static IDataMatrix Pivot(this IDataMatrix dm, string dataField, AggregateFunction aggregate,
                                        string[] rowFields, string[] columnFields)
        {
            var rowFieldIndexes = dm.GetIndexes(rowFields).ToArray();
            var colFieldIndexes = dm.GetIndexes(columnFields).ToArray();
            var dataFieldIndex  = dm.Columns.First(c => c.Name.ToUpper() == dataField.ToUpper()).Index;

            var items = new PivotEntry[dm.RowCount];

            for (var i = 0; i < dm.RowCount; i++)
            {
                var entry = new PivotEntry
                {
                    DataValue = dm[i][dataFieldIndex]
                };

                entry.SetRowValues(dm.GetValues(i, rowFieldIndexes).ToArray());
                entry.SetColValues(dm.GetValues(i, colFieldIndexes).ToArray());

                items[i] = entry;
            }

            var rows = (from p in items
                        group p by p.RowId into g
                        select new PivotRow
            {
                Row = g.Key,
                Cols = g.GroupBy(c => c.ColId, c => c.DataValue, (key, c) => new PivotColumn {
                    Name = key, Values = c.ToArray()
                }).ToArray()
            }).ToArray();

            var cols = new string[0];

            foreach (var row in rows)
            {
                cols = cols.Union(row.Cols.Select(c => c.Name)).ToArray();
            }
            Array.Sort(cols);

            var values = new object[rows.Length, cols.Length + 1];

            for (var rowIndex = 0; rowIndex < values.GetLength(0); rowIndex++)
            {
                values[rowIndex, 0] = rows[rowIndex].Row;
                for (var colIndex = 1; colIndex < values.GetLength(1); colIndex++)
                {
                    values[rowIndex, colIndex] = GetCellValue(rows[rowIndex].Cols, cols[colIndex - 1], aggregate);
                }
            }

            var columns = new string[cols.Length + 1];

            columns[0] = string.Join("\v", rowFields);
            for (var i = 0; i < cols.Length; i++)
            {
                columns[i + 1] = cols[i];
            }

            var result = DataMatrixFactory.Create(values, columns);

            return(result);
        }