Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions graphblas/io/_networkx.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ def from_networkx(G, nodelist=None, dtype=None, weight="weight", name=None):
G = G.subgraph(nodelist)

index = dict(zip(nodelist, range(nlen), strict=True))
if weight is None and not G.is_multigraph() and (dtype is None or dtype.kind in "biufc"):
# Every entry is 1, so read only the coordinates: row by row in nodelist
# order, straight from the adjacency, which for an undirected graph already
# holds both directions and each self-loop once. No tuple and no value per
# edge makes this two to three times faster than the general path below.
nbrs = [G._adj[u] for u in nodelist]
rows = np.repeat(np.arange(nlen, dtype=np.uint64), [len(nbr) for nbr in nbrs])
if rows.size: # with no edge, the general path gives the dtype (scipy's float64)
cols = np.fromiter((index[v] for nbr in nbrs for v in nbr), np.uint64, rows.size)
dtype = np.int64 if dtype is None else dtype # as the general path infers the 1s
return Matrix.from_coo(rows, cols, 1, dtype=dtype, nrows=nlen, ncols=nlen, name=name)
coefficients = zip(
*((index[u], index[v], wt) for u, v, wt in G.edges(data=weight, default=1)),
strict=True,
Expand Down
30 changes: 30 additions & 0 deletions graphblas/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,36 @@ def test_from_networkx_uniform_weights_are_iso(graph_cls, selfloop, weight):
assert M.ss.is_iso


@pytest.mark.skipif("not nx")
@pytest.mark.parametrize("graph_cls", [nx.Graph, nx.DiGraph] if nx else [])
@pytest.mark.parametrize("dtype", [None, bool, "float32"])
def test_from_networkx_structure_matches_the_general_path(graph_cls, dtype):
# weight=None reads only the coordinates. An attribute no edge carries takes
# the general path and gives every entry the default 1, so the two agree.
G = graph_cls()
G.add_edges_from([("a", "b"), ("b", ("t", 1)), (("t", 1), ("t", 1)), ("b", "a")])
G.add_node(frozenset({1}))
edgeless = graph_cls()
edgeless.add_nodes_from(range(3))
cases = [
(G, None),
(G, list(G)[::-1]),
(G, [("t", 1), "a"]),
(G, [frozenset({1})]),
(edgeless, None),
]
for H, nodelist in cases:
M = gb.io.from_networkx(H, nodelist=nodelist, weight=None, dtype=dtype)
expected = gb.io.from_networkx(H, nodelist=nodelist, weight="absent", dtype=dtype)
assert M.isequal(expected, check_dtype=True)
if suitesparse and M.nvals > 0:
assert M.ss.is_iso
with pytest.raises(nx.NetworkXError, match="no nodes"):
gb.io.from_networkx(graph_cls(), weight=None)
with pytest.raises(nx.NetworkXError, match="not in G"):
gb.io.from_networkx(G, nodelist=["a", "missing"], weight=None)


@pytest.mark.skipif("not nx")
@pytest.mark.parametrize("wt", [np.inf, -np.inf, 1e308])
def test_from_networkx_undirected_selfloop_keeps_its_weight(wt):
Expand Down
Loading