Exemple #1
0
        public static void Main(string[] args)
        {
            DataFrame df = new DataFrame();

            df.AddColumn("name", typeof(string));
            df.AddColumn("age", typeof(int));


            df.AddRow("John", 27);
            df.AddRow("Michael", 98);
            df.AddRow("Rue", 39);
            df.AddRow("Peeta", 78);

            df.PrimaryKey = df["name"];


            DataFrame df2 = df[df["age"] > 40];

            // DataFrame df2 = df[df["age"] > 40 & df["name"] == "Michael"];

            Console.WriteLine(df);
            Console.WriteLine("======================");
            Console.WriteLine(df2);


            Console.WriteLine();
            //  df.SaveToFile("test_df.cdf", Encoding.UTF8);
            Console.WriteLine("Finished.");
        }
Exemple #2
0
        public void TestWiden()
        {
            var df = new DataFrame();

            df.AddColumn("x", new int[] { 1, 2, 3 });
            df.AddColumn("y", new string[] { "a", "b", "c" });

            var result = df.Widen(4, 5, new string[] { "x", "y" });

            CollectionAssert.AreEqual(new string[] { "x_0", "y_0", "x_1", "y_1", "x_2", "y_2", "x_3", "y_3" }, result.ColumnNames);

            Assert.AreEqual(1, result.GetColumn(0).Count);
            Assert.AreEqual(1, result.GetColumn(1).Count);
            Assert.AreEqual(1, result.GetColumn(2).Count);
            Assert.AreEqual(1, result.GetColumn(3).Count);
            Assert.AreEqual(1, result.GetColumn(4).Count);
            Assert.AreEqual(1, result.GetColumn(5).Count);
            Assert.AreEqual(1, result.GetColumn(6).Count);
            Assert.AreEqual(1, result.GetColumn(7).Count);

            Assert.AreEqual(1, result["x_0"].GetObject(0));
            Assert.AreEqual("a", result["y_0"].GetObject(0));
            Assert.AreEqual(2, result["x_1"].GetObject(0));
            Assert.AreEqual("b", result["y_1"].GetObject(0));
            Assert.AreEqual(3, result["x_2"].GetObject(0));
            Assert.AreEqual("c", result["y_2"].GetObject(0));
            Assert.AreEqual(0, result["x_3"].GetObject(0));
            Assert.AreEqual(null, result["y_3"].GetObject(0));
        }
Exemple #3
0
        protected override DataFrameBuilder AddColumns(DataFrameBuilder dataFrameBuilder)
        {
            dataFrameBuilder = AddColumnForAxis(dataFrameBuilder, dataFrameBuilder.BaseAxis);
            var points = dataFrameBuilder.Points;
            var apexes = new double?[points.Count];
            var starts = new double?[points.Count];
            var fwhms  = new double?[points.Count];

            for (int i = 0; i < points.Count; i++)
            {
                var point = points[i];
                if (point.IsMissing)
                {
                    continue;
                }
                starts[i] = point.Z;
                var middleErrorTag = point.Tag as MiddleErrorTag;
                if (middleErrorTag != null)
                {
                    apexes[i] = middleErrorTag.Middle;
                    fwhms[i]  = middleErrorTag.Error;
                }
            }
            var dataFrame = new DataFrame(dataFrameBuilder.ValueAxis.Title.Text, dataFrameBuilder.Points.Count);

            dataFrame        = dataFrame.AddColumn(new DataColumn <double?>(@"Apex", apexes));
            dataFrame        = dataFrame.AddColumn(new DataColumn <double?>(@"Start", starts));
            dataFrame        = dataFrame.AddColumn(GetColumnForAxis(dataFrameBuilder, dataFrameBuilder.ValueAxis).SetTitle(@"End"));
            dataFrame        = dataFrame.AddColumn(new DataColumn <double?>(@"FWHM", fwhms));
            dataFrameBuilder = dataFrameBuilder.AddColumn(dataFrame);
            return(dataFrameBuilder);
        }
Exemple #4
0
            public DataFrame Predict(double[] features)
            {
                DataFrame pred = null;
                var       df   = new DataFrame();

                df.AddColumn("Label", new float[] { 0f });
                for (int i = 0; i < features.Length; ++i)
                {
                    df.AddColumn(string.Format("F{0}", i), new float[] { (float)features[i] });
                }
                _pipeline.Predict(df, ref pred);
                return(pred);
            }
Exemple #5
0
            public DataFrame PredictBatch(int nf, double[] features)
            {
                DataFrame pred = null;
                var       df   = new DataFrame();
                int       N    = features.Length / nf;

                df.AddColumn("Label", Enumerable.Range(0, N).Select(i => (float)features[nf * i]).ToArray());
                for (int i = 0; i < nf; ++i)
                {
                    df.AddColumn(string.Format("F{0}", i),
                                 Enumerable.Range(0, N).Select(k => (float)features[nf * k + i]).ToArray());
                }
                _pipeline.Predict(df, ref pred);
                return(pred);
            }
        public void TestIndexerGetSlice()
        {
            Pair col  = new Pair("Column1", new Series(new NDArray(1, 2, 3)));
            Pair col1 = new Pair("Column2", new Series(new NDArray(4, 5, 6)));

            DataFrame df = new DataFrame(col, col1);

            DataFrame i = df[new Slice(1, 3), new Slice(0, 2)];

            DataFrame t = new DataFrame();

            t.AddColumn(new Pair("Column1", new Series(new NDArray(2, 3), new NDArray(1, 2))));
            t.AddColumn(new Pair("Column2", new Series(new NDArray(5, 6), new NDArray(1, 2))));

            Assert.True(i.Equals(t), "Arrays are not equal.");
        }
        public void DataFrameColumnManipulations()
        {
            DataList column1 = new DataList <int>("Integer");
            DataList column2 = new DataList <double>("Double");
            DataList column3 = new DataList <DateTime>("Timestamp");

            DataFrame frame = new DataFrame(column1, column2);

            Assert.IsTrue(frame.Columns.Count == 2);

            Assert.IsTrue(frame.Columns[0].Name == column1.Name);
            Assert.IsTrue(frame.Columns[0].StorageType == column1.StorageType);
            Assert.IsTrue(frame.Column <object>(column1.Name).Name == column1.Name);

            frame.AddColumn(column3);
            Assert.IsTrue(frame.Columns.Count == 3);

            Assert.IsTrue(frame.Columns[2].Name == column3.Name);
            Assert.IsTrue(frame.Columns[2].StorageType == column3.StorageType);
            Assert.IsTrue(frame.Column <object>(column3.Name).Name == column3.Name);

            frame.RemoveColumn(column1.Name);
            Assert.IsTrue(frame.Columns.Count == 2);

            Assert.IsTrue(frame.Columns[0].Name == column2.Name);
            Assert.IsTrue(frame.Columns[0].StorageType == column2.StorageType);
            Assert.IsTrue(frame.Column <object>(column2.Name).Name == column2.Name);
        }
Exemple #8
0
        public static void AddColumnToDataFrameFloat(DataFrame df, string name, float[] values)
        {
            var cpy = new float[values.Length];

            Array.Copy(values, cpy, values.Length);
            df.AddColumn(name, cpy);
        }
            public DataFrame GetArgsAsDataFrame()
            {
                var df = new DataFrame();

                if (Arguments != null)
                {
                    df.AddColumn("Name", Arguments.Select(c => c.Name).ToArray());
                    df.AddColumn("ShortName", Arguments.Select(c => c.ShortName).ToArray());
                    df.AddColumn("DefaultValue", Arguments.Select(c => c.DefaultValue).ToArray());
                    df.AddColumn("Help", Arguments.Select(c => c.Help).ToArray());
                }
                else if (Info.LoaderType != null)
                {
                    // Arguments class.
                    var atts          = Info.LoaderType.CustomAttributes;
                    var names         = new List <string>();
                    var shortNames    = new List <string>();
                    var defaultValues = new List <string>();
                    var help          = new List <string>();
                    foreach (var p in atts)
                    {
                        var n = "";
                        var s = "";
                        var d = "";
                        var h = "";
                        foreach (var at in p.NamedArguments)
                        {
                            if (at.MemberName == "Name")
                            {
                                n = at.ToString();
                            }
                            else if (at.MemberName == "Alias")
                            {
                                s = at.ToString();
                            }
                            else if (at.MemberName == "Default")
                            {
                                d = at.ToString();
                            }
                            else if (at.MemberName == "Desc")
                            {
                                h = at.ToString();
                            }
                        }
                        names.Add(n);
                        shortNames.Add(s);
                        defaultValues.Add(d);
                        help.Add(h);
                    }
                    var decl = Info.LoaderType;
                    df.AddColumn("Name", names.ToArray());
                    df.AddColumn("ShortName", shortNames.ToArray());
                    df.AddColumn("DefaultValue", defaultValues.ToArray());
                    df.AddColumn("Help", help.ToArray());
                }
                return(df);
            }
Exemple #10
0
        public void TestPivot()
        {
            var df = new DataFrame();

            df.AddColumn("x", new string[] { "a", "b", "a", "b", "c" });
            df.AddColumn("y", new int[] { 10, 10, 20, 20, 20 });
            df.AddColumn("v1", new double[] { 1, 2, 3, 4, 5 });
            df.AddColumn("v2", new string[] { "foo", "bar", "baz", "qux", "quux" });

            var p = df.Pivot("x", "y", new string[] { "v1", "v2" });

            CollectionAssert.AreEqual(new string[] { "x", "10_v1", "10_v2", "20_v1", "20_v2" }, p.ColumnNames);
            Assert.AreEqual(3, p.RowCount);

            CollectionAssert.AreEqual(new object[] { "a", "b", "c" }, p["x"].ToObjectArray());
            CollectionAssert.AreEqual(new object[] { 1.0, 2.0, 0.0 }, p["10_v1"].ToObjectArray());
            CollectionAssert.AreEqual(new object[] { "baz", "qux", "quux" }, p["20_v2"].ToObjectArray());
        }
Exemple #11
0
        public static void AddColumnToDataFrameInt64(DataFrame df, string name, Int64[] values)
        {
            var bval = new Int64[values.Length];

            for (int i = 0; i < values.Length; ++i)
            {
                bval[i] = values[i];
            }
            df.AddColumn(name, bval);
        }
Exemple #12
0
        public static void AddColumnToDataFrameString(DataFrame df, string name, string[] values)
        {
            var bval = new DvText[values.Length];

            for (int i = 0; i < values.Length; ++i)
            {
                bval[i] = new DvText(values[i]);
            }
            df.AddColumn(name, bval);
        }
Exemple #13
0
        public void TestConcatenate()
        {
            var df1 = new DataFrame();

            df1.AddColumn("x", new int[] { 1, 2 });
            df1.AddColumn("y", new string[] { "a", "b" });

            var df2 = new DataFrame();

            df2.AddColumn("x", new int[] { 3, 4 });
            df2.AddColumn("z", new string[] { "c", "d" });

            var result = DataFrame.Concatenate(df1, df2);

            CollectionAssert.AreEqual(new string[] { "x", "y", "z" }, result.ColumnNames);

            Assert.AreEqual(4, result[0].Count);
            Assert.AreEqual(4, result[1].Count);
            Assert.AreEqual(4, result[2].Count);

            CollectionAssert.AreEqual(new object[] { 1, 2, 3, 4 }, result[0].ToObjectArray());
            CollectionAssert.AreEqual(new object[] { "a", "b", null, null }, result[1].ToObjectArray());
            CollectionAssert.AreEqual(new object[] { null, null, "c", "d" }, result[2].ToObjectArray());
        }
Exemple #14
0
        public void TestGroupBy()
        {
            var df = new DataFrame();

            df.AddColumn("x", new string[] { "1", "2", "3", "2" });
            df.AddColumn("y", new string[] { "a", "b", "c", "d" });

            var g = df.GroupBy("x");

            CollectionAssert.AreEqual(new object[] { "1", "2", "3" }, g.Keys);

            CollectionAssert.AreEqual(new string[] { "x", "y" }, g["1"].ColumnNames);
            CollectionAssert.AreEqual(new string[] { "x", "y" }, g["2"].ColumnNames);
            CollectionAssert.AreEqual(new string[] { "x", "y" }, g["3"].ColumnNames);

            CollectionAssert.AreEqual(new object[] { "1" }, g["1"].GetColumn("x").ToObjectArray());
            CollectionAssert.AreEqual(new object[] { "a" }, g["1"].GetColumn("y").ToObjectArray());

            CollectionAssert.AreEqual(new object[] { "2", "2" }, g["2"].GetColumn("x").ToObjectArray());
            CollectionAssert.AreEqual(new object[] { "b", "d" }, g["2"].GetColumn("y").ToObjectArray());

            CollectionAssert.AreEqual(new object[] { "3" }, g["3"].GetColumn("x").ToObjectArray());
            CollectionAssert.AreEqual(new object[] { "c" }, g["3"].GetColumn("y").ToObjectArray());
        }
Exemple #15
0
        public void TestForeach()
        {
            var d = new DataFrame();

            d.AddColumn("foo", new object[] { 1, 2, 3, 4 });

            Assert.AreEqual(4, d.Count);

            int c = 1;

            foreach (PSObject e in d)
            {
                Assert.AreEqual(c, e.Properties["foo"].Value);
                ++c;
            }
        }
        public void AddColumn()
        {
            NDArray index = new NDArray(DateTime.Parse("2018-03-12"), DateTime.Parse("2018-03-13"), DateTime.Parse("2018-03-14"));

            Pair col1 = new Pair("Open", new Series(new NDArray(1592.599976, 1615.959961, 1597.000000), index));
            Pair col2 = new Pair("High", new Series(new NDArray(1605.329956, 1617.540039, 1606.439941), index));
            Pair col3 = new Pair("Low", new Series(new NDArray(1586.699951, 1578.010010, 1590.890015), index));
            Pair col4 = new Pair("Close", new Series(new NDArray(1598.390015, 1588.180054, 1591.000000), index));
            Pair col5 = new Pair("Adj Close", new Series(new NDArray(1598.390015, 1588.180054, 1591.000000), index));

            DataFrame df1 = new DataFrame(col1, col2, col3, col4, col5);

            Pair col6 = new Pair("Volume", new Series(new NDArray(5174200, 6531900, 4175400), index));

            df1.AddColumn(col6);

            DataFrame t = df1["Volume"];

            DataFrame t2 = new DataFrame(new Pair("Volume", new Series(new NDArray(5174200, 6531900, 4175400), index)));

            Assert.True(t.Equals(t2), "Arrays are not equal.");
        }
        protected override void EndProcessing()
        {
            var df = new DataFrame();

            if (Transpose)
            {
                bool first = true;
                foreach (int[] seq in Combinatorics.Sequences(Symbols, true))
                {
                    if (first)
                    {
                        for (var column = 0; column < seq.Length; ++column)
                        {
                            df.AddColumn <int>("c" + column);
                        }
                        first = false;
                    }

                    for (var column = 0; column < seq.Length; ++column)
                    {
                        df.GetColumn(column).SetObject(column, seq[column]);
                    }
                }
            }
            else
            {
                int column = 0;
                foreach (int[] seq in Combinatorics.Sequences(Symbols, true))
                {
                    df.DefineNewColumn("c" + column, new DataFrameColumn <int>(df, seq));
                    ++column;
                }
            }

            WriteObject(df);
        }
        public static DataFrame CreateDataFrameWithAllTypes()
        {
            var df = new DataFrame();

            df.AddColumn("cbool", new bool[] { true, false });
            df.AddColumn("cint", new[] { 1, 0 });
            df.AddColumn("cuint", new uint[] { 3, 4 });
            df.AddColumn("cint64", new Int64[] { 3, 4 });
            df.AddColumn("cfloat", new[] { 6f, 7f });
            df.AddColumn("cdouble", new[] { 6.5, 7.5 });
            df.AddColumn("ctext", new[] { "t1", "t2" });

            df.AddColumn("vbool", new bool[][] { new bool[] { true, false, true }, new bool[] { true, false, true } });
            df.AddColumn("vint", new int[][] { new int[] { 1, 0 }, new int[] { 2, 3 } });
            df.AddColumn("vuint", new uint[][] { new uint[] { 3, 4 }, new uint[] { 5, 6 } });
            df.AddColumn("vint64", new Int64[][] { new Int64[] { 3, 4 }, new Int64[] { 5, 6 } });
            df.AddColumn("vfloat", new float[][] { new float[] { 6f, 7f }, new float[] { 8f, 9f, 10f } });
            df.AddColumn("vdouble", new double[][] { new double[] { 6.6, 7.6 }, new double[] { 8.6, 9.6, 10.6 } });
            df.AddColumn("vtext", new string[][] { new string[] { "t1", "t2" }, new string[] { "t5", "t6", "t8" } });

            return(df);
        }
Exemple #19
0
        public static int Main(string[] args)
        {
            string modelDirectory = ((args != null) && (args.Length > 0)) ? args[0]
            : "../../models";
            string solver = ((args != null) && (args.Length > 1)) ? args[1] : null;
            // Create first dataframe (for data indexed over NUTR) Add data row by row
            DataFrame df1 = new DataFrame(1, "NUTR", "n_min", "n_max");

            df1.AddRow("A", 700, 20000);
            df1.AddRow("B1", 700, 20000);
            df1.AddRow("B2", 700, 20000);
            df1.AddRow("C", 700, 20000);
            df1.AddRow("CAL", 16000, 24000);
            df1.AddRow("NA", 0.0, 50000);

            // Create second dataframe (for data indexed over FOOD) Add column by column
            DataFrame df2 = new DataFrame(1, "FOOD");

            string[] foods = { "BEEF", "CHK", "FISH", "HAM",
                               "MCH",  "MTL", "SPG",  "TUR" };
            df2.SetColumn("FOOD", foods);
            double[] contents = new double[8];
            for (int j = 0; j < 8; j++)
            {
                contents[j] = 2;
            }
            df2.AddColumn("f_min", contents);
            for (int j = 0; j < 8; j++)
            {
                contents[j] = 10;
            }
            df2.AddColumn("f_max", contents);
            double[] costs = { 3.19, 2.59, 2.29, 2.89, 1.89,
                               1.99, 1.99, 2.49 };
            df2.AddColumn("cost", costs);

            // Create third dataframe, to assign data to the AMPL entity param amt{NUTR, FOOD};
            DataFrame df3 = new DataFrame(2, "NUTR", "FOOD");

            // Populate the set columns
            string[] nutrWithMultiplicity = new string[48];
            string[] foodWithMultiplicity = new string[48];
            int      i = 0;

            for (int n = 0; n < 6; n++)
            {
                for (int f = 0; f < 8; f++)
                {
                    nutrWithMultiplicity[i]   = df1.GetRowByIndex(n)[0].Str;
                    foodWithMultiplicity[i++] = foods[f];
                }
            }
            df3.SetColumn("NUTR", nutrWithMultiplicity);
            df3.SetColumn("FOOD", foodWithMultiplicity);

            // Populate with all these values
            double[] values = { 60,     8,   8,  40,   15,  70,   25,  60,  10,  20,  15,
                                35,    15,  15,  25,   15,  15,   20,  10,  10,  15,  15,  15,  10, 20, 0, 10,
                                40,    35,  30,  50,   20, 295,  770, 440, 430, 315, 400, 370, 450,
                                968, 2180, 945, 278, 1182, 896, 1329, 1397 };
            df3.AddColumn("amt", values);

            // Create an AMPL instance
            using (AMPL ampl = new AMPL())
            {
                if (solver != null)
                {
                    ampl.SetOption("solver", solver);
                }
                // Read model only
                ampl.Read(modelDirectory + "/diet/diet.mod");
                // Assign data to NUTR, n_min and n_max
                ampl.SetData(df1, "NUTR");
                // Assign data to FOOD, f_min, f_max and cost
                ampl.SetData(df2, "FOOD");
                // Assign data to amt
                ampl.SetData(df3);
                // Solve the model
                ampl.Solve();

                // Print out the result
                Console.Write("Objective function value: {0}\n",
                              ampl.GetObjective("Total_Cost").Value);

                // Get the values of the variable Buy in a dataframe
                DataFrame results = ampl.GetVariable("Buy").GetValues();
                // Print
                Console.WriteLine(results.ToString());
            }
            return(0);
        }
Exemple #20
0
        public static int Main(string[] args)
        {
            string modelDirectory = ((args != null) && (args.Length > 0)) ? args[0]
           : "../../models";
            string solver = ((args != null) && (args.Length > 1)) ? args[1] : null;

            /*
             * // If the AMPL installation directory is not in the system search path:
             * ampl.Environment env = new ampl.Environment(
             * "full path to the AMPL installation directory");
             * // Create an AMPL instance
             * using (AMPL a = new AMPL(env)) {}
             */

            // Create an AMPL instance
            using (var ampl = new AMPL())
            {
                if (solver != null)
                {
                    ampl.SetOption("solver", solver);
                }
                // Read the model file
                ampl.Read(modelDirectory + "/diet/diet.mod");

                string[] foods = { "BEEF", "CHK", "FISH", "HAM",
                                   "MCH",  "MTL", "SPG",  "TUR" };
                double[] costs = { 3.59, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49 };
                double[] fmin  = { 2, 2, 2, 2, 2, 2, 2, 2 };
                double[] fmax  = { 10, 10, 10, 10, 10, 10, 10, 10 };

                DataFrame df = new DataFrame(1, "FOOD");
                df.SetColumn("FOOD", foods);
                df.AddColumn("cost", costs);
                df.AddColumn("f_min", fmin);
                df.AddColumn("f_max", fmax);
                ampl.SetData(df, "FOOD");

                string[] nutrients = { "A", "C", "B1", "B2", "NA", "CAL" };
                double[] nmin      = { 700, 700, 700, 700, 0, 16000 };
                double[] nmax      = { 20000, 20000, 20000, 20000, 50000, 24000 };
                df = new DataFrame(1, "NUTR");
                df.SetColumn("NUTR", nutrients);
                df.AddColumn("n_min", nmin);
                df.AddColumn("n_max", nmax);
                ampl.SetData(df, "NUTR");

                double[,] amounts =
                {
                    {  60,    8,   8,  40,   15,  70,   25,   60 },
                    {  20,    0,  10,  40,   35,  30,   50,   20 },
                    {  10,   20,  15,  35,   15,  15,   25,   15 },
                    {  15,   20,  10,  10,   15,  15,   15,   10 },
                    { 928, 2180, 945, 278, 1182, 896, 1329, 1397 },
                    { 295,  770, 440, 430,  315, 400,  379,  450 }
                };
                df = new DataFrame(2, "NUTR", "FOOD", "amt");
                df.SetMatrix(nutrients, foods, amounts);
                ampl.SetData(df);

                ampl.Solve();

                Console.WriteLine(string.Format("Objective: {0}",
                                                ampl.GetObjective("Total_Cost").Value));
            }
            return(0);
        }