예제 #1
0
        public void WriteDataWithAnnotationColumnsTest()
        {
            // main data
            IDataWithAnnotationColumns mdata = PerseusFactory.CreateDataWithAnnotationColumns();

            // 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.WriteDataWithAnnotationColumns(mdata, writer);
                    writer.Flush();
                    mdataStr = Encoding.UTF8.GetString(memstream.ToArray());
                }
            IMatrixData mdata3 = PerseusFactory.CreateMatrixData();

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

            Assert.AreEqual(2, mdata2.RowCount);
            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]);
        }
예제 #2
0
        public static void Read(INetworkData ndata, string outFolder, ProcessInfo processInfo)
        {
            ReadMatrixDataInto(ndata, Path.Combine(outFolder, "networks.txt"), processInfo);
            foreach (var netAttr in ndata.GetStringColumn("guid").Zip(ndata.GetStringColumn("name"), (guid, name) => new { guid, name }))
            {
                var guid      = Guid.Parse(netAttr.guid);
                var nodeTable = PerseusFactory.CreateDataWithAnnotationColumns();
                var edgeTable = PerseusFactory.CreateDataWithAnnotationColumns();
                ReadMatrixDataInto(nodeTable, Path.Combine(outFolder, $"{guid}_nodes.txt"), processInfo);
                ReadMatrixDataInto(edgeTable, Path.Combine(outFolder, $"{guid}_edges.txt"), processInfo);
                var graph      = new Graph();
                var nodeIndex  = new Dictionary <INode, int>();
                var nameToNode = new Dictionary <string, INode>();
                var nodeColumn = nodeTable.GetStringColumn("node");

                for (int row = 0; row < nodeTable.RowCount; row++)
                {
                    var node = graph.AddNode();
                    nodeIndex[node]             = row;
                    nameToNode[nodeColumn[row]] = node;
                }
                var sourceColumn = edgeTable.GetStringColumn("source");
                var targetColumn = edgeTable.GetStringColumn("target");
                var edgeIndex    = new Dictionary <IEdge, int>();
                for (int row = 0; row < edgeTable.RowCount; row++)
                {
                    var source = nameToNode[sourceColumn[row]];
                    var target = nameToNode[targetColumn[row]];
                    var edge   = graph.AddEdge(source, target);
                    edgeIndex[edge] = row;
                }
                ndata.AddNetworks(new NetworkInfo(graph, nodeTable, nodeIndex, edgeTable, edgeIndex, netAttr.name, guid));
            }
            ReadMatrixDataInto(ndata, Path.Combine(outFolder, "networks.txt"), processInfo);
        }
예제 #3
0
        public void TestReadEmptyMatrixFromFile()
        {
            var data = PerseusFactory.CreateDataWithAnnotationColumns();

            PerseusUtils.ReadDataWithAnnotationColumns(data, BaseTest.CreateProcessInfo(), () =>
            {
                var memstream = new MemoryStream(Encoding.UTF8.GetBytes("Node\n#!{Type}T\n"));
                return(new StreamReader(memstream));
            }, "test", '\t');
            Assert.AreEqual(0, data.RowCount);
        }
예제 #4
0
        public void TestWriteMultiNumericColumnWithNulls()
        {
            var data = PerseusFactory.CreateDataWithAnnotationColumns();

            data.AddMultiNumericColumn("Test", "", new double[1][]);
            data.AddStringColumn("Test2", "", new string[1]);
            Assert.AreEqual(1, data.RowCount);
            var writer = new StreamWriter(new MemoryStream());

            PerseusUtils.WriteDataWithAnnotationColumns(data, writer);
        }
예제 #5
0
        public void TestNetworkUniqueRows()
        {
            Random       RandGen = new Random();
            INetworkData ndata   = new NetworkData();

            ndata.Name        = "Random network(s)";
            ndata.Description = ndata.Name;
            var n        = 3;
            var numNodes = 100;
            var numEdges = 150;

            for (int i = 0; i < n; i++)
            {
                var graph     = new Graph();//!!!!
                var nodeTable = PerseusFactory.CreateDataWithAnnotationColumns();
                var nodeIndex = new Dictionary <INode, int>();
                var edgeTable = PerseusFactory.CreateDataWithAnnotationColumns();
                var edgeIndex = new Dictionary <IEdge, int>();
                for (int j = 0; j < numNodes; j++)
                {
                    nodeIndex[graph.AddNode()] = j;
                }
                var nodeNames = Enumerable.Range(0, graph.NumberOfNodes).Select(x => $"node {x}").ToArray();
                nodeTable.AddStringColumn("Node", "", nodeNames);
                var nodes   = graph.ToArray();
                var sources = new List <string>();
                var targets = new List <string>();
                for (int j = 0; j < numEdges; j++)
                {
                    var source = nodes[RandGen.Next(0, nodes.Length)];
                    sources.Add(nodeNames[nodeIndex[source]]);
                    var target = nodes[RandGen.Next(0, nodes.Length)];
                    targets.Add(nodeNames[nodeIndex[target]]);
                    edgeIndex[graph.AddEdge(source, target)] = j;
                }
                edgeTable.AddStringColumn("Source", "", sources.ToArray());
                edgeTable.AddStringColumn("Target", "", targets.ToArray());
                var network = new NetworkInfo(graph, nodeTable, nodeIndex, edgeTable, edgeIndex, $"Random {i}");
                ndata.AddNetworks(network);
            }
            foreach (var network in ndata)
            {
                network.UniqueRows(network.EdgeTable.StringColumns[0], network.EdgeTable.StringColumns[1], network.NodeTable.StringColumns[0], ArrayUtils.Median, RemoveDuplicateEdges.Union, RemoveDuplicateEdges.CatUnion, RemoveDuplicateEdges.MultiNumUnion);
                Assert.True(network.EdgeTable.RowCount <= 150);
                Assert.True(network.EdgeTable.RowCount == network.Graph.NumberOfEdges);
                Assert.True(network.NodeTable.RowCount == network.Graph.NumberOfNodes);
            }
        }