Skip to content

68 bool weights - #166

Open
eriknw wants to merge 1 commit into
python-graphblas:61-generic-declinesfrom
eriknw:68-bool-weights
Open

eriknw wants to merge 1 commit into
python-graphblas:61-generic-declinesfrom
eriknw:68-bool-weights

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026

Copy link
Copy Markdown
Member

NetworkX adds Python's bools as the ints 1 and 0, adds two of NumPy's as a logical or (np.True_ + np.True_ is True), and some of its answers hold a weight as it was given. A weight with a bool value anywhere is now refused at conversion, so NetworkX answers. A cast of bools to int64 would match NetworkX's sums, but not the answers that hold a weight as given, nor NumPy's logical or, so it was not the rule.

  • What differed. Weights that were all bools gave a matrix of bools, which has no plus: modularity_matrix and directed_modularity_matrix raised KeyError: 'plus does not work with BOOL' where NetworkX answers; laplacian_matrix answered a matrix of bools (True for 3 and for -1); normalized_laplacian_matrix answered -1.0 for every edge where NetworkX gives -0.408 and the like. Answers that hold a weight gave 1 where NetworkX gives True: the distance along one edge from floyd_warshall_predecessor_and_distance, the edges of ego_graph. With NumPy's bools mutual_weight gave 2 where NetworkX gives True.
  • Before and after. A probe calls every weighted dispatched name on a graph of 5 nodes and 6 edges, directed and undirected, with and without a self-loop, with nine kinds of bool weights (all bools, Python's or NumPy's, all True, all False, bools beside ints or floats), with this backend first in NetworkX's priority, and compares the answer or error with NetworkX's, each scalar's kind included (True is not 1), floats within 1e-9. Before: 211 of 1548 calls differed in NetworkX dev, 219 of 1548 in 3.7 and 205 of 1512 in 3.6.1. After: none. Of those before, 67 in each were the bools' own (43 in value, 24 in type only): the same call with each bool an int matched NetworkX. The rest those int twins share, such as clustering's 0.0 where NetworkX gives the int 0; the refusal leaves the bool calls among them to NetworkX too. The int twins' calls are unchanged (150, 158 and 144 differing before and after, none of them about bools).
  • The rule. A matrix of bools says so at once. In a matrix of numbers a bool is held as 1 or 0: bools beside ints or floats become numbers, and so do bools on an undirected graph with a self-loop, whose value NetworkX's conversion negates (-True is -1) and adds back. So only the edges whose values are exactly 1 or 0 are looked up in the graph, one at a time when they are fewer than a quarter of the entries, and else all the values at once, their types gathered with map and set. The check's cost beside the conversion, on 1000000 edges (Indicative, best of 3): nothing for float weights (none exactly 1 or 0), 10% for ints from 1 to 9, and 27% where every weight is 1, as large as a read of every value.
  • Given up: a graph rebuilt from its matrix, as by reverse or k_truss, whose weights were all bools came back exactly, bools and all; NetworkX answers it now, and so it does where such a name carries another edge attribute of bools, as a flag on a graph with no weights: ego_graph, reverse, k_truss and the k_core family leave those to NetworkX too (a review found 13 such calls of 242). Names that read no edge data, such as pagerank with weight=None or triangles, still serve these graphs, and so does every name where the bools are another attribute beside numeric weights. The note on those names no longer says bools come back as numbers.
  • Tests. The probe as a test, over every weighted dispatched name; the refusal at conversion for each kind of bool, on a name that reads the values and one that rebuilds the graph, while their int twins convert; a lone bool among many other numbers, on an edge or a self-loop; NetworkX's graphs, bools kept, from reverse, k_truss, k_core and ego_graph. Tests that bools convert, and modularity's test on a NetworkX graph with bool weights, are changed to the new rule; a backend graph that already holds bools is still served. 11 mutants of the refusal each fail them, in NetworkX dev, 3.7 and 3.6.1.
  • NetworkX's tests of the weighted names (shortest paths, centrality, link analysis, linalg, clustering, cuts, structural holes, cores, distance measures, Wiener index, community quality, ego graphs and operators), through the backend in dev, 3.7 and 3.6.1: the same tests pass before and after (2098, 2055 and 1462). The repo's tests pass in all three.

Stacked on #156.

NetworkX adds Python's bools as the ints 1 and 0, adds two of NumPy's
as a logical or (np.True_ + np.True_ is True), and some of its answers
hold a weight as it was given: the distance along one edge from
floyd_warshall_predecessor_and_distance, the edges of ego_graph, the
entries of adjacency_matrix. The conversion gave weights that were all
bools a matrix of bools, which has no plus: modularity_matrix raised
KeyError, and laplacian_matrix answered in bools where NetworkX answers
in ints. Bools beside ints or floats became numbers, so an answer that
holds a weight gave 1 where NetworkX gives True, and mutual_weight gave
2 where NetworkX gives True.

A weight with a bool value anywhere is now refused at conversion, and
NetworkX answers. A matrix of bools says so at once. In a matrix of
numbers a bool is held as 1 or 0, so only the edges with those values
are looked up, one at a time when they are few, and all the values at
once, their types gathered at C speed, when they are many.
A cast to int64 would match NetworkX's sums, but not the answers that
hold a weight as given, nor NumPy's logical or.

A graph rebuilt from its matrix, as by reverse or k_truss, no longer
takes bool weights either, so its note no longer says they come back as
numbers.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.20513% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.83%. Comparing base (83d8c57) to head (20b3a34).

Additional details and impacted files
@@                   Coverage Diff                   @@
##           61-generic-declines     #166      +/-   ##
=======================================================
- Coverage                92.90%   92.83%   -0.08%     
=======================================================
  Files                      182      183       +1     
  Lines                    11294    11488     +194     
  Branches                  1281     1314      +33     
=======================================================
+ Hits                     10493    10665     +172     
- Misses                     547      560      +13     
- Partials                   254      263       +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