Exemplo n.º 1
0
        public void TestReadWriteMatrixRoundTrip()
        {
            var mdata = PerseusFactory.CreateMatrixData();

            mdata.AddStringColumn("StringCol", "", new []
            {
                "Regular text",
                "\"Regular quoted text\"",
                "\"Quote stops\" in the middle",
                "\"Escaped\tseparator\"with extra",
            });
            mdata.AddNumericColumn("NumCol", "", mdata.StringColumns[0].Select(_ => 1.0).ToArray());
            mdata.AddCategoryColumn("CatCol", "", mdata.StringColumns[0].Select(_ => new string[0]).ToArray());
            string content;

            using (var memory = new MemoryStream())
                using (var writer = new StreamWriter(memory))
                {
                    PerseusUtils.WriteMatrix(mdata, writer);
                    writer.Flush();
                    content = Encoding.UTF8.GetString(memory.ToArray());
                }
            var mdata2      = PerseusFactory.CreateMatrixData();
            var processInfo = new ProcessInfo(new Settings(), s => { }, i => { }, 1);

            PerseusUtils.ReadMatrix(mdata2, processInfo, () => new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(content))), "name", '\t');
            var expected = mdata.StringColumns[0];

            expected[1] = expected[1].Trim('\"');
            CollectionAssert.AreEqual(mdata.StringColumns[0], mdata2.StringColumns[0]);
        }
Exemplo n.º 2
0
        public void WriteMatrixTest()
        {
            // main data
            IMatrixData mdata = PerseusFactory.CreateMatrixData(new double[, ] {
                { 1, 2, 3 }, { 3, 4, 5 }
            },
                                                                new List <string> {
                "col1", "col2", "col3"
            });

            // annotation rows
            mdata.AddCategoryRow("catrow", "this is catrow", new[] { new[] { "cat1" }, new[] { "cat1", "cat2" }, new[] { "cat2" } });
            mdata.AddNumericRow("numrow", "this is numrow", new[] { -1.0, 1, 2 });
            // annotation columns
            mdata.AddStringColumn("strcol1", "this is stringcol1", new[] { "1", "2" });
            mdata.AddStringColumn("strcol2", "", new[] { "", "hallo" });
            mdata.AddNumericColumn("numcol", "", new[] { 1.0, 2.0 });
            mdata.AddMultiNumericColumn("multnumcol", "this is multnumcol", new[] { new[] { -2.0, 2.0 }, new double[] {} });
            mdata.AddCategoryColumn("catcol", "", new[] { new[] { "cat1", "cat1.1" }, new[] { "cat2", "cat1" } });

            string mdataStr;

            using (MemoryStream memstream = new MemoryStream())
                using (StreamWriter writer = new StreamWriter(memstream)) {
                    PerseusUtils.WriteMatrix(mdata, writer);
                    writer.Flush();
                    mdataStr = Encoding.UTF8.GetString(memstream.ToArray());
                }

            IMatrixData mdata2 = PerseusFactory.CreateMatrixData();

            PerseusUtils.ReadMatrix(mdata2, new ProcessInfo(new Settings(), status => { }, progress => { }, 1), () => {
                StreamReader tmpStream = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(mdataStr)));
                return(tmpStream);
            }, "matrix1", '\t');

            Assert.AreEqual(2, mdata2.RowCount);
            Assert.AreEqual(3, mdata2.ColumnCount);

            Assert.AreEqual(2, mdata2.StringColumnCount);
            Assert.AreEqual(1, mdata2.NumericColumnCount);
            Assert.AreEqual(1, mdata2.CategoryColumnCount);
            Assert.AreEqual(1, mdata2.MultiNumericColumnCount);

            Assert.AreEqual("hallo", mdata2.StringColumns[mdata2.StringColumnNames.FindIndex(col => col.Equals("strcol2"))][1]);

            Assert.AreEqual(1, mdata2.CategoryRowCount);
            Assert.AreEqual(1, mdata2.NumericRowCount);
        }