diff --git a/orangecontrib/network/network/readwrite.py b/orangecontrib/network/network/readwrite.py index 1f13bba..8305b04 100644 --- a/orangecontrib/network/network/readwrite.py +++ b/orangecontrib/network/network/readwrite.py @@ -51,17 +51,27 @@ def read_vertices(lines): def read_edges(id_idx, lines, nvertices): - lines = [(id_idx[v1], id_idx[v2], abs(float(value))) - for v1, v2, value, *_ in (line.split()[:3] + [1] - for line in lines)] - v1s, v2s, values = zip(*lines) - values = np.array(values) - if values.size and np.all(values == values[0]): - values = np.lib.stride_tricks.as_strided( - values[0], (len(values), ), (0, )) + def fake_data(x, n): + values = np.lib.stride_tricks.as_strided(x, (n, ), (0,)) values.flags.writeable = False - return sp.coo_matrix((values, (np.array(v1s), np.array(v2s))), - shape=(nvertices, nvertices)) + return values + + + lines = [(id_idx[v1], id_idx[v2], value) + for v1, v2, value, *_ in ( + (line.split(maxsplit=2) + [None])[:3] for line in lines)] + v1s, v2s, values = zip(*lines) + try: + values = np.array(values, dtype=float) + if values.size and np.all(values == values[0]): + values = fake_data(values[0], len(values)) + edge_data = None + except ValueError: + edge_data = np.array(values) + values = fake_data(np.array(1.), len(v1s)) + return (sp.coo_matrix((values, (np.array(v1s), np.array(v2s))), + shape=(nvertices, nvertices)), + edge_data) def read_edges_list(id_idx, lines, nvertices): @@ -121,7 +131,7 @@ def check_has_vertices(): check_has_vertices() edges.append( EdgeType[part_type=="*arcs"]( - read_edges(id_idx, line_part, len(labels)), + *read_edges(id_idx, line_part, len(labels)), name=part_args.strip() or part_type[1:])) elif part_type in ("*edgeslist", "*arcslist"): check_has_vertices() diff --git a/orangecontrib/network/network/tests/test_readwrite.py b/orangecontrib/network/network/tests/test_readwrite.py index 4cf7d9a..ec00feb 100644 --- a/orangecontrib/network/network/tests/test_readwrite.py +++ b/orangecontrib/network/network/tests/test_readwrite.py @@ -1,5 +1,6 @@ import os import unittest +from pkg_resources import resource_filename from tempfile import NamedTemporaryFile import numpy as np @@ -8,12 +9,15 @@ def _fullpath(name): - return os.path.join(os.path.split(__file__)[0], name) + return os.path.join(resource_filename("orangecontrib.network", "networks"), name) + +def _fullpathtest(name): + return os.path.join(resource_filename(__name__, ""), name) class TestReadPajek(unittest.TestCase): def test_two_mode(self): - davis = readwrite.read_pajek(_fullpath("../networks/davis.net")) + davis = readwrite.read_pajek(_fullpath("davis.net")) self.assertEqual(davis.number_of_nodes(), 32) self.assertEqual( list(davis.nodes), @@ -25,6 +29,17 @@ def test_two_mode(self): ) self.assertEqual(davis.in_first_mode, 18) + def test_edge_labels(self): + net = readwrite.read_pajek(_fullpathtest("towns.net")) + self.assertEqual(net.number_of_nodes(), 4) + self.assertEqual( + list(net.nodes), + ["Ljubljana", "Kranj", "Maribor", "Novo mesto"] + ) + self.assertEqual( + list(net.edges[0].edge_data), + ['near', 'far', 'not near, not far', 'huh?']) + def test_write_pajek(self): net = readwrite.read_pajek(_fullpath("../networks/leu_by_genesets.net")) with NamedTemporaryFile("wt", suffix=".net", delete=False) as f: @@ -62,7 +77,7 @@ def test_write_pajek_multiple_edge_types(self): self.assertRaises(TypeError, readwrite.write_pajek, f, net) def test_edge_list(self): - net = readwrite.read_pajek(_fullpath("test-arcslist.net")) + net = readwrite.read_pajek(_fullpathtest("test-arcslist.net")) neighs = [(1, (2, 3, 6)), (2, (1, 4, 5, 6)), (5, (1, 2)), diff --git a/orangecontrib/network/network/tests/towns.net b/orangecontrib/network/network/tests/towns.net new file mode 100644 index 0000000..0c65e45 --- /dev/null +++ b/orangecontrib/network/network/tests/towns.net @@ -0,0 +1,12 @@ +*Network "Test" +*Description "Slovenian towns" +*Vertices +1 "Ljubljana" +2 "Kranj" +3 "Maribor" +4 "Novo mesto" +*Edges +1 2 near +1 3 far +1 4 not near, not far +3 4 huh?