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
16 changes: 15 additions & 1 deletion graphblas_algorithms/classes/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,22 @@ def drop_unrequested_values(G, weight):
NetworkX reads a missing attribute. NetworkX's conversion cache is one way a graph with
other values arrives: it serves a conversion to a later call that asks for less. `PRESERVE`
is for the wrappers that rebuild their input and so want whatever values it holds.

A call that reads complex values is refused, with NotImplementedError, so that NetworkX
answers it or raises its own error. NetworkX compares weights, or makes floats of them, and
so raises TypeError on most complex ones; GraphBLAS has no complex `min` or `max`, nor the
`min_plus` semiring of shortest paths, and its complex arithmetic elsewhere would answer
where NetworkX raises, or answer otherwise. Every wrapper that reads its weights asks here
first, on any graph: a conversion made for this call, one NetworkX cached for another, or
one given directly.
"""
if weight is PRESERVE or (weight is not None and weight == G._weight):
if weight is PRESERVE:
return G
if weight is not None and weight == G._weight:
if G._A.dtype.np_type.kind == "c":
raise NotImplementedError(
f"Edge attribute {weight!r} has complex values; networkx serves the call"
)
return G
if _has_no_values_to_drop(G):
return G # the usual unweighted conversion is not copied
Expand Down
4 changes: 4 additions & 0 deletions graphblas_algorithms/nxapi/boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def edge_boundary(G, nbunch1, nbunch2=None, data=False, keys=False, default=None
# A graph with no values has none to yield: `data=True` gives an empty mapping per edge
# and a named attribute the caller's default, as in NetworkX.
reads_values = bool(is_multigraph or (data and G._weight is not None))
if data is True and G._weight is not None:
# The values are yielded from the matrix, so complex ones are refused, as for a call
# that names their attribute: NetworkX's own answer keeps each value's type.
G = to_graph(G, weight=G._weight)
v1 = G.set_to_vector(nbunch1, ignore_extra=True)
v2 = G.set_to_vector(nbunch2, ignore_extra=True)
result = algorithms.edge_boundary(G, v1, v2, is_weighted=reads_values)
Expand Down
6 changes: 5 additions & 1 deletion graphblas_algorithms/nxapi/generators/ego.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def ego_graph(G, n, radius=1, center=True, undirected=False, distance=None):
# A subgraph keeps the input's edge data whatever `distance` names. NetworkX reads a
# `distance` the edges do not carry as 1, so the radius is then a hop count.
G = to_graph(G, weight=PRESERVE)
is_weighted = distance is not None and G._weight == distance
if is_weighted:
# The lengths are read, and complex ones refused, as for a call that names them.
G = to_graph(G, weight=distance)
if n not in G:
# NetworkX's shortest-path helpers raise these, by whether `distance` is given.
if distance is None:
Expand All @@ -23,7 +27,7 @@ def ego_graph(G, n, radius=1, center=True, undirected=False, distance=None):
radius=radius,
center=center,
undirected=undirected,
is_weighted=distance is not None and G._weight == distance,
is_weighted=is_weighted,
)
except algorithms.exceptions.Unbounded as e:
# What NetworkX's Dijkstra raises on a negative cycle.
Expand Down
8 changes: 7 additions & 1 deletion graphblas_algorithms/nxapi/shortest_paths/weighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@


def all_pairs_bellman_ford_path_length(G, weight="weight", *, chunksize="10 MiB"):
G = to_graph(G, weight=weight)
# Read on the call, so that a refusal (complex weights) reaches NetworkX's dispatcher,
# which can still answer instead, rather than the caller reading the generator.
return _all_pairs_bellman_ford_path_length(G, chunksize)


def _all_pairs_bellman_ford_path_length(G, chunksize):
# Larger chunksize offers more parallelism, but uses more memory.
# Chunksize indicates for how many source nodes to compute at one time.
# The default is to choose the number of rows so the result, if dense,
# will be about 10MB.
G = to_graph(G, weight=weight)
chunksize = normalize_chunksize(chunksize, len(G) * G._A.dtype.np_type.itemsize, len(G))
if chunksize is None:
# All at once
Expand Down
5 changes: 5 additions & 0 deletions graphblas_algorithms/nxapi/tests/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ def test_modularity_follows_networkx(name, weight, resolution):
G, partition = _partitioned()[name]
expected = quality.modularity(G, partition, weight=weight, resolution=resolution)
for H in (G, backend(G, weight="weight")):
if name == "complex weights" and weight == "weight":
# Complex weights are left to NetworkX: tests/test_complex_weights.py.
with pytest.raises(NotImplementedError):
quality.modularity(H, partition, weight, resolution, backend="graphblas")
continue
rv = quality.modularity(H, partition, weight, resolution, backend="graphblas")
assert rv == pytest.approx(expected, rel=1e-12, abs=1e-15)

Expand Down
Loading
Loading