Repository navigation
Conversation
These names sit outside shortest paths. The modularity matrices, google_matrix and normalized_laplacian_matrix give NetworkX's NaN entries and dropped rows for NaN and infinite weights. When a weight is negative, when the weights selected are all 0 (the modularity matrices) or when one is NaN (normalized_laplacian_matrix), they use NetworkX's arithmetic on the same array, so NaN lands where NetworkX puts it, as beside an edge of weight 0, and sums that cancel round as NetworkX's do. These go to NetworkX, where its answer follows its edge order, differs between its versions, or is an error: - weighted clustering and average_clustering with a NaN weight, or with a largest weight of 0, which NetworkX divides by; - hits with a NaN, infinite or negative weight, or every weight 0; - pagerank where a node's out-weights sum to +inf or -inf, or hold both signs, three or more of them, as only such a sum can change with the order of addition.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 20-wiener-efficiency #136 +/- ##
========================================================
+ Coverage 92.88% 93.12% +0.24%
========================================================
Files 182 183 +1
Lines 11227 11685 +458
Branches 1274 1326 +52
========================================================
+ Hits 10428 10882 +454
- Misses 546 550 +4
Partials 253 253 🚀 New features to boost your workflow:
|
eriknw
force-pushed
the
37-nonfinite-weights
branch
2 times, most recently
from
December 2, 2026 23:57
9b45333 to
804d15d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Give NetworkX's answers for NaN, infinite and nonpositive weights in the weighted names that are not shortest paths. NetworkX meets these values with NumPy and SciPy arithmetic, Python's
maxor ARPACK, and the matrix operations here gave other answers forclustering,average_clustering, both modularity matrices,google_matrix,normalized_laplacian_matrix,pagerankandhits. A weight of 0 or below matters where NetworkX divides by the largest weight or by a sum of weights, or takes a square root of a sum: the largest can be 0, and a sum can be 0, below 0, or one that cancels, so that its rounding follows the order NetworkX adds in. Where NetworkX's arithmetic settles the answer, this follows it. Where NetworkX's answer depends on its edge order, or differs between its versions, the call is declined, and NetworkX answers it under backend priority.modularity_matrixanddirected_modularity_matrix: a NaN or infinite weight among those the nodelist selects makesmNaN or infinite. NetworkX's denseA - outer(k, k) / (2 * m)then gives NaN in each row and column whosekis not finite, and where a zero row or column crosses one (0 times inf). The sparse product here had no entry there. Weights selected that are all 0 makem0, and NetworkX's quotient is then NaN everywhere, also at a node with no weight selected. Weights of both signs can makemcancel, to 0 or to a small number, by NetworkX's order of addition. In these cases the matrices are now computed with NetworkX's arithmetic on the same SciPy array, and NaN and inf fall in the same entries as NetworkX's, and the other entries agree within rounding. With a NaN or infinite weight, every other entry isAless a zero.google_matrix: NetworkX divides the whole dense row by its sum, so a row that sums to NaN (a NaN weight, or +inf with -inf) is NaN throughout. It was NaN only at the row's edges. With a negative weight, a row can cancel to 0, which NetworkX takes as dangling, or to a small number, which it divides by. Weights 0.1, 0.2 and -0.3 sum to 2.8e-17 in NumPy's order and to 5.6e-17 from left to right, and the row's entries came out at half of NetworkX's. With a negative weight,google_matrixnow runs NetworkX's own dense arithmetic on the same array.normalized_laplacian_matrix:L = D - Aholds an entry, and SciPy drops a zero there. Here the diagonal was 1, and NaN also fell whereLis 0: at an edge of weight 0, and on the diagonal of a node whose weights other than its self-loop sum to 0. Weights that are all 0 or above give no NaN and stay on the GraphBLAS path.clusteringandaverage_clusteringwith a weight: NetworkX scales bymaxover its edges in order. Python'smaxkeeps a NaN it starts with and passes over one it meets later. So a NaN on the first edge makes every value NaN, and a NaN elsewhere only its own triangles. A matrix holds no edge order, so a NaN weight is declined. The check comes beforenodesis read, so an iterator reaches NetworkX whole. Infinite weights give the same maximum in any order and are still served. A largest weight of 0, where every weight is 0 or below, makes NetworkX divide by zero: it raises ZeroDivisionError for Python numbers, and gives NaN or infinities for NumPy ones. That is declined too; here those calls were answered. A largest weight below 0 scales every weight to a positive one, as here, and weights of both signs scale as NetworkX's do, so both are served.pagerankis declined where a node's out-weights sum to +inf or -inf, from infinite weights or from finite ones whose sum overflows. NetworkX scales that row by 1 / sum, which is 0:A sum that is NaN fails to converge here and in NetworkX, as before.
pagerankis also declined where a node's out-weights hold both signs, three or more of them. Such a sum can cancel to 0 or to a small number by NetworkX's order of addition, and NetworkX takes the node as dangling only at 0. A sum of two weights does not depend on their order, adding a zero changes nothing, and weights of one sign differ by rounding only, so those are still served.hitswith a NaN or infinite weight is declined. NetworkX 3.6 raises ArpackError there. This backend raised PowerIterationFailedConvergence, which is what NetworkX 3.7 raises.hitswith a negative weight, or with every weight 0, is declined. Each step divides the hub values by their largest. NetworkX takes it over every node, counting 0 for a node with no hub value; the power iteration here takes it over the values it holds.A graph with no edges still gets 0.0 for every node, as the backend's notes say.
Unweighted calls are unchanged: a conversion made with
weight=Noneholds no weights.volumeand the cut names (cut_size,normalized_cut_size,conductance,edge_expansion,mixing_expansion,node_expansion,boundary_expansion) already answer as NetworkX does on NaN and infinite edge weights, so they are unchanged. With an infinite undirected self-loop they do not, because the conversion stores that self-loop as NaN. 38-inf-self-loops fixes that.With 38-inf-self-loops applied as well, every name here answers as NetworkX does on NaN and infinite self-loops too. A NaN self-loop in
google_matrix, which 38-inf-self-loops leaves as it was, is covered here: its row sums to NaN.Cost: the check reads the converted values once, cached on the conversion. The sign checks read the minimum and maximum cached on the conversion (
_signsinnxapi/_utils.py). The other checks look only at the row sums. NetworkX's arithmetic runs only where the weights call for it. With a nodelist, the modularity matrices also read the weights selected once more, to check them.The value check,
_read_apartinnxapi/_utils.py, is a copy of the one in 34-nan-weights. Once both are in, one copy goes.Stacked on #121.