Skip to content

49 complex weights - #149

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:49-complex-weights
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:49-complex-weights

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026

Copy link
Copy Markdown
Member

Leave every call that reads complex edge weights to NetworkX, which answers it or raises its own error. Calls that read no values, and rebuilds that only carry them, are still served.

  • What NetworkX does: it compares weights (Dijkstra, Bellman-Ford) or makes floats of them (scipy arrays of floats), so it raises TypeError on Python complex weights and on mixes of complex and float. On numpy complex weights some names answer in complex numbers.
  • What the backend did. A sweep of 60 names that read or carry edge values (edge_boundary twice, with data="weight" and data=True), on undirected and directed random graphs, with six kinds of complex weights: Python complex, np.complex64, np.complex128, complex beside floats, one complex among floats, and complex with no imaginary part. Each call went through NetworkX's dispatcher with graphblas first and fallback on. Of 690 calls (678 in NetworkX 3.6.1), 396 differed from NetworkX (394 in 3.6.1):
    • 222 raised KeyError, in 20 names: the shortest-path names, the eccentricity and Wiener families, clustering, average_clustering and ego_graph. GraphBLAS has no complex min or max, nor the min_plus semiring: the errors read "min does not work with FC64", or "plus does not work with FC64" for min_plus.
    • 76 answered where NetworkX raises, in 9 names: pagerank, hits, closeness_centrality, harmonic_centrality, average_shortest_path_length, eigenvector_centrality, katz_centrality, google_matrix and harmonic_diameter.
    • 93 (91 in 3.6.1) answered something other than NetworkX's answer, by value or by type: a Python complex where NetworkX gives an np.complex64, for one.
    • After this change, none of the 690 differ, in NetworkX 3.6.1, 3.7 or dev.
  • The check is in drop_unrequested_values. Every wrapper calls it through to_graph, to_directed_graph or to_undirected_graph, with the attribute it reads.
    • When that attribute is the one the graph holds and the graph's matrix is complex, it raises NotImplementedError, and NetworkX tries the next backend: NetworkX itself.
    • It reads the matrix's type, which the conversion inferred from the values, so it adds no pass over the edges.
    • It sees every graph a call gets: a conversion made for the call, and a conversion NetworkX cached for another call and serves to this one. A rebuild such as k_core keeps every attribute, and NetworkX serves that conversion to a later weighted pagerank. It also sees a backend graph that was given directly.
    • The names that read "weight" whatever they are asked (hits, tournament_matrix) pass through it as well.
  • Three wrappers needed a line more:
    • edge_boundary(data=True) and ego_graph(distance=...) read values from a conversion that keeps every attribute, so they now also ask by the attribute's name.
    • all_pairs_bellman_ford_path_length converted inside its generator, so a refusal would have reached the caller reading it, where NetworkX can no longer answer instead. It now converts on the call and returns the generator.
  • Still served:
    • A call that reads no values. On a graph with complex weights, an unweighted call gives the answer the backend gives for the same edges with float weights.
    • A rebuild that carries the values: compose, disjoint_union, union, reverse, k_core, k_corona, k_crust, k_shell, k_truss, and ego_graph without distance. The graph it returns equals NetworkX's, value for value.
    • As before, a rebuild's values come back in the matrix's one type: an np.complex64 as a Python complex, a float beside a complex as a complex. These are equal values; the existing rule for rebuilds lets a change of type alone through.
  • Now NetworkX's, though the backend matched before:
    • adjacency_matrix and tournament_matrix matched NetworkX in every call of the sweep, and a few other names matched for some kinds of weight. modularity matched for Python complex weights, but with numpy complex weights it gave a Python complex where NetworkX gives np.complex64 (with float32 rounding) or np.complex128.
    • These are now answered by NetworkX, more slowly. Serving them would need an exemption name by name, and checks of their arguments (adjacency_matrix's dtype).
  • Not covered: calling the backend's own functions (graphblas_algorithms.nxapi) on a NetworkX graph, outside the dispatcher. There the function converts the graph itself, for some names to floats, and is not checked; nothing could answer instead.
  • Tests (graphblas_algorithms/tests/test_complex_weights.py):
    • Coverage: every name NetworkX dispatches to the backend with an edge attribute is listed. A check fails if a new one is not.
    • Readers: each name that reads values is called with Python complex, np.complex64, and one complex with no imaginary part among floats, on undirected and directed graphs. Through the dispatcher, with graphblas first and fallback on, the result equals NetworkX's value, within 1e-12 and of the same type, or NetworkX's exception type and message. Called with backend="graphblas", it raises NotImplementedError.
    • Rebuilds and unweighted calls are still served.
    • Also: the 6-cycle with every edge 1.5 + 0.5j, the cached rebuild conversion, a backend graph given directly, and the all-pairs Bellman-Ford refusal on the call.
  • One existing test changed: modularity with complex weights expected the backend's answer, and now expects the call to be declined.
  • NetworkX's tests for the names touched (21 files: shortest paths, boundary, ego, link analysis, the centralities, cuts, clustering, distance measures, Wiener, graph matrices, community quality, tournament) pass through the backend before and after this change, with fallback on and off. That is 1557 in NetworkX dev, 1541 in 3.7 and 970 in 3.6.1, with the backend called the same number of times. NetworkX's full suite through the backend (dev) is unchanged: 9569 passed, 31 skipped, 743 xfailed.

Stacked on #121.

A call that reads complex edge values now raises NotImplementedError
in the backend, so that NetworkX answers it or raises its own error.
GraphBLAS has no complex min or max, nor the min_plus semiring, so
shortest paths and the names built on them raised KeyError ("plus does
not work with FC64") where NetworkX raises TypeError; and pagerank,
closeness, harmonic and others answered where NetworkX raises.

The check is in drop_unrequested_values, which every wrapper calls
with the attribute it reads. It tests the matrix's type, which the
conversion inferred from the values, so it needs no pass over the
edges, and it sees every graph a call gets: a conversion made for it,
one NetworkX cached for another call and serves to this one, or a
backend graph given directly. A call that reads no values is still
served, as is a rebuild that carries them (k_core, union, reverse).

edge_boundary with data=True and ego_graph with distance read the
values of a conversion that keeps every attribute, so they now ask
the check by the attribute's name. all_pairs_bellman_ford_path_length
converts on the call rather than when its generator is first read,
where NetworkX could no longer answer instead.
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.68508% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.94%. Comparing base (5b54ace) to head (58eeb82).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           20-wiener-efficiency     #149      +/-   ##
========================================================
+ Coverage                 92.88%   92.94%   +0.05%     
========================================================
  Files                       182      183       +1     
  Lines                     11227    11406     +179     
  Branches                   1274     1299      +25     
========================================================
+ Hits                      10428    10601     +173     
- Misses                      546      549       +3     
- Partials                    253      256       +3     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant