Skip to content

80 cache inference - #180

Open
eriknw wants to merge 1 commit into
python-graphblas:79-self-edgesfrom
eriknw:80-cache-inference
Open

eriknw wants to merge 1 commit into
python-graphblas:79-self-edgesfrom
eriknw:80-cache-inference

Conversation

@eriknw

@eriknw eriknw commented Dec 4, 2026

Copy link
Copy Markdown
Member

A graph reads some properties from others it has already cached. Four of those readings were wrong, and one place looked in the cache under names it never uses. Each is checked here against what a fresh graph computes, and a new test does that for random fill orders.

  • Edge counts. count_element+ or count_element- read from a cached count_rowwise or count_columnwise counted the counts, giving the number of rows (or columns) with an edge rather than the number of edges. It now adds them up. A Graph path 0-1-2, which stores 4 entries, said 3. No function in the backend asks for these two today, so no NetworkX answer changes; they are wrong only through get_property.
  • Negative loops. has_negative_diagonal took a missing min_element- for 0, so a negative weight off the diagonal was read as a negative loop. A missing fact now tells nothing, and the diagonal is read. NetworkX-facing: a DiGraph with edges 0->1 of weight -2, 1->2 of 3 and a loop of 1 on node 2 has no negative cycle, but after min_element+ is cached, nx.negative_edge_cycle said True; it returns as soon as the flag is set. (The Bellman-Ford functions also read the flag, but look at the diagonal before they raise.) On main that takes a graph converted with DiGraph.from_networkx; with 20 wiener efficiency #121 or with the cache stamp branch, whose is_iso caches min_element+, a plain NetworkX graph gets there too: nx.single_source_bellman_ford_path_length(G, 0, backend="graphblas"), then nx.negative_edge_cycle(G, backend="graphblas") gives True on each of them alone and False merged with this. These lines are the same as 34 nan, infinite and negative weights #132's.
  • Reciprocal degrees. recip_degrees+ and recip_degrees- built with the diagonal took its dtype through unary.one, so they were floats on a graph of floats, integers on a fresh one, by the order they were filled in. They now use unary.one[int] on every path and match a fresh graph's INT64. No NetworkX answer changes: reciprocity, overall_reciprocity and directed clustering divide into floats, and give the same values before and after on a graph with a loop, in the four orders tried.
  • Clustering of one node. single_clustering looked for degrees- and degrees+ in the cache by those names, but the cache keeps them under the names they resolve to (count_rowwise-/+), so it never found them and read the node's row again. Speed only: answers are unchanged, and the rows read for 4 nodes after degree_centrality go from 7 to 4.
  • Off-diagonal reductions from the triangles. A - scalar reduction (such as max_element-) read from cached L- and U- called Matrix.reduce, which does not exist. It now reduces each triangle to a scalar, the same lines as 06 cluster #105 (and so 20 wiener efficiency #121). NetworkX-facing: weighted nx.clustering of a Graph with a self-loop, over all nodes, stops raising AttributeError and now gives main's per-node number, which still differs from NetworkX until 06 cluster #105. On a triangle with edges of weight 1, 2 and 3 and a loop of weight 9 on node 0, that is 0.6057 for each node against NetworkX's 0.2019: clustering's own formula on main divides by the heaviest edge off the diagonal, and 06 cluster #105 divides by the heaviest edge. Merged with 20 wiener efficiency #121, every node matches NetworkX. So this is best merged after 06 cluster #105, or an error on that call becomes main's wrong number.
  • Fill orders. tests/test_cache_inference.py builds graphs of 1 to 6 nodes (floats, integers or booleans, with loops on some nodes or none, one weight or several) and asks 1 to 10 things of each in a seeded random order: one property (of 54 for a Graph, 58 for a DiGraph, with the plus, min, max, times and count reductions), three at once, a vector under a random mask, or an algorithm (degree centralities, and on a DiGraph reciprocity). Each answer, dtype included, must equal the answer of a graph of its own; three at once are compared one by one. Four seeds of 100 orders per class run in about a second. Over 3000 orders, 79 self edges #177 gives 174 wrong answers on a Graph and 221 on a DiGraph (main, 174 and 519); this gives none, for seeds 0 and 1, in NetworkX dev, 3.7 and 3.6.1.
  • What the fill orders leave out. any reductions, whose answer depends on the order values are read. iso_value, which raises KeyError after is_iso is cached as False; 20 wiener efficiency #121 rewrites both. Graphs with no nodes, since python-graphblas 2026.11 counts the entries of a 0 by 0 matrix as 1.
  • Targeted tests. Edge counts from each of the four row and column counts, with and without a loop, with offdiag cached first or not; the negative diagonal on five weightings, from five sets of cached facts; negative_edge_cycle after min_element+; the reciprocal degrees' dtype after six fill orders, masked and not, for float, integer and boolean graphs; and the rows read by the clustering of one node after degrees- or degrees+ is cached. On 79 self edges #177, 68 of the 142 tests fail, in each of the three NetworkX versions.
  • Mutants. Of 24 mutants (counting the counts again, in each of the eight places and all at once, or adding up every reduction; the old Matrix.reduce; a missing min_element- taken for 0, in either case; the negative-weight checks loosened; unary.one back in each of the six places; the clustering's old names, and a cached degrees+ used without taking off the loop), 22 fail these tests in dev, 3.7 and 3.6.1. The two that pass count the counts where the + count reads cached - row or column counts. That case is not reached: each - row or column getter settles has_self_edges first and, when it is False, caches itself as the + as well, and no order of up to three of 12 related properties gets there.
  • Suites. The repo's tests pass in dev, 3.7 and 3.6.1. NetworkX's own tests through the backend give the same result for every test on 79 self edges #177 and with this, in each of the three.
  • Merges. Clean with 20 wiener efficiency #121, 34 nan, infinite and negative weights #132 and the cache stamp branch; the repo's tests pass on each. This leaves DiGraph's has_self_edges as 79 self edges #177 has it.

Stacked on #177.

A graph reads some properties from others it has already cached, and
four of those readings went wrong.

The count of edges, read from cached counts of the rows or columns,
counted them again, giving the number of rows with an edge. It now adds
them up.

Whether the diagonal holds a negative weight was read as yes from a
negative weight anywhere when the least weight off the diagonal was not
cached, since its absence was taken for 0. A missing fact now tells
nothing, and the diagonal is read. A graph with a loop of weight 1 and
an edge of weight -2 was otherwise said to have a negative cycle.

The reciprocal degrees are counts, but those built with the diagonal
took its dtype: floats on a graph of floats, integers otherwise, by the
order they were filled in. They are now integers on every path.

The off-diagonal reduction from the cached triangles called a reduce
that a Matrix lacks; it now reduces each to a scalar. And the
clustering of one node looked for its degrees under names the cache
does not use, so it read them from the matrix every time.
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.80220% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.25%. Comparing base (6ad0db0) to head (1e7a821).

Additional details and impacted files
@@                Coverage Diff                @@
##           79-self-edges     #180      +/-   ##
=================================================
+ Coverage          81.35%   83.25%   +1.90%     
=================================================
  Files                121      122       +1     
  Lines               4553     4718     +165     
  Branches             750      774      +24     
=================================================
+ Hits                3704     3928     +224     
+ Misses               657      619      -38     
+ Partials             192      171      -21     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknw eriknw mentioned this pull request Dec 4, 2026
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