Skip to content

Commit

Permalink
Pajek reader: Allow giving labels instead of weights
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Jun 10, 2023
1 parent b2c2761 commit aabf527
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
32 changes: 21 additions & 11 deletions orangecontrib/network/network/readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 18 additions & 3 deletions orangecontrib/network/network/tests/test_readwrite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import unittest
from pkg_resources import resource_filename
from tempfile import NamedTemporaryFile

import numpy as np
Expand All @@ -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),
Expand All @@ -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:
Expand Down Expand Up @@ -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)),
Expand Down
12 changes: 12 additions & 0 deletions orangecontrib/network/network/tests/towns.net
Original file line number Diff line number Diff line change
@@ -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?

0 comments on commit aabf527

Please sign in to comment.