Skip to content

08 operators - #107

Open
eriknw wants to merge 5 commits into
python-graphblas:07-counts-cutsfrom
eriknw:08-operators
Open

eriknw wants to merge 5 commits into
python-graphblas:07-counts-cutsfrom
eriknw:08-operators

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Fix the graph operators when the two graphs have different node sets or kinds, and decline what one matrix cannot hold so NetworkX builds it.

  • compose no longer drops edges when the node sets differ
  • full_join with edge data is declined
  • union and full_join with rename keep graph attributes and raise NetworkX's full overlap message
  • Mixing a Graph and a DiGraph raises NetworkXError in compose, union, disjoint_union, intersection and full_join; difference and symmetric_difference decline the pair
  • A graph-returning call gives edge weights back in the one type their matrix holds (1 as 1.0 when ints and floats mix); each name's backend notes say so

Stacked on #106.

When the two graphs had different node sets, compose wrote its result
in three blocks, each wrong in its own way: the shared block was
assigned rather than accumulated, dropping edges only G had there; the
blocks between a shared node and a node only H has were never written;
and H's new nodes were numbered by H's own ids plus G's row count, which
can address past the end of the matrix. compose([(0, 1), (1, 2)],
[(0, 1), (2, 3)]) returned only (0, 1), and compose([(0, 1), (1, 2)],
[(2, 3), (3, 4)]) raised KeyError: 3.

G's nodes now keep their ids, H's other nodes follow in H's order, and
all of H's edges go in with one assignment accumulated by
binary.second, so an edge in both graphs takes H's value, as in
NetworkX.

A graph with no edges no longer disagrees about the edge weight with a
weighted operand: compose, union and disjoint_union of an edgeless
graph and a weighted one refused, where NetworkX answers with the
weighted one's edges.
NetworkX gives the edges a join adds no data, so a full_join where
either graph has edge data is a graph of two kinds of edge, bare and
weighted, which one matrix cannot hold. The kernel refused it, but only
after both graphs were converted. full_join now declines it in can_run,
before any conversion, reading NetworkX edge data or a backend graph's
weight, and NetworkX builds the result. On backend graphs there is no
NetworkX graph to fall back on, so the call raises NotImplementedError;
convert with to_networkx() first.

full_join's rename defaults to (None, None), as NetworkX's does.
union and full_join rebuild an operand they rename, with prefixed node
keys, and the rebuild started with no graph attributes, so the result
had none: union(G, H, rename=("a", "b")) returned {} where NetworkX
merges the two graphs' attributes. The rebuild now keeps them.

When the node sets overlap, both raise NetworkXError, and NetworkX's
message goes on to name rename and disjoint_union, the part that tells
a caller what to do instead. The message is now NetworkX's in full.
compose, union, disjoint_union, intersection and full_join of a Graph
and a DiGraph returned a graph of the first one's type, built from both
matrices as they were. With the Graph first, that was an undirected
graph whose matrix held some edges in one direction only. NetworkX
raises NetworkXError, "All graphs must be directed or undirected.", and
now so does this backend. One check serves every operator and takes
over their multigraph checks; it runs first, as in NetworkX, so a pair
whose edge data would also be refused gets the same error.

NetworkX's difference and symmetric_difference accept the pair, and
their answer depends on the order in which the Graph reports its edges:
Graph([(0, 1)]) built with node 1 first, minus DiGraph([(0, 1)]), keeps
the edge, and built with node 0 first it does not. This backend did not
follow that order, and its undirected results could be malformed in the
same way, so it now declines the pair for those two and NetworkX
serves it.
compose, disjoint_union, ego_graph, k_truss, reverse and union return
a graph rebuilt from one matrix, so its edge weights come back in the
matrix's one type: weights that mix 1 and 2.5 come back as 1.0 and
2.5, an int past 2**53 mixed with floats comes back rounded, and NumPy
scalars come back as Python numbers, where NetworkX hands back the
objects it was given. Each of these names now says so in the
additional_docs NetworkX shows with its documentation. full_join also
rebuilds its inputs, but leaves a weighted graph to NetworkX.
@eriknw eriknw mentioned this pull request Dec 2, 2026
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.03382% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.63%. Comparing base (54ac506) to head (c42d069).

Additional details and impacted files
@@                Coverage Diff                 @@
##           07-counts-cuts     #107      +/-   ##
==================================================
+ Coverage           85.87%   86.63%   +0.76%     
==================================================
  Files                 138      139       +1     
  Lines                6405     6562     +157     
  Branches              898      903       +5     
==================================================
+ Hits                 5500     5685     +185     
+ Misses                654      635      -19     
+ Partials              251      242       -9     
🚀 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