Skip to content

37 nan, infinite and nonpositive weights - #136

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

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:37-nonfinite-weights

Conversation

@eriknw

@eriknw eriknw commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

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 max or ARPACK, and the matrix operations here gave other answers for clustering, average_clustering, both modularity matrices, google_matrix, normalized_laplacian_matrix, pagerank and hits. 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_matrix and directed_modularity_matrix: a NaN or infinite weight among those the nodelist selects makes m NaN or infinite. NetworkX's dense A - outer(k, k) / (2 * m) then gives NaN in each row and column whose k is 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 make m 0, and NetworkX's quotient is then NaN everywhere, also at a node with no weight selected. Weights of both signs can make m cancel, 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 is A less 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_matrix now runs NetworkX's own dense arithmetic on the same array.

  • normalized_laplacian_matrix:

    • For a node whose weights sum to +inf, 1 / sqrt(inf) is 0. NetworkX's sparse diagonal drops that zero, so the node's row and column are empty. Here they held entries.
    • A sum that is NaN or below zero makes 1 / sqrt(sum) NaN. With a NaN or a negative weight, the matrix is now computed with NetworkX's SciPy arithmetic on the same array. NetworkX's sparse products put NaN only where L = D - A holds an entry, and SciPy drops a zero there. Here the diagonal was 1, and NaN also fell where L is 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.
  • clustering and average_clustering with a weight: NetworkX scales by max over its edges in order. Python's max keeps 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 before nodes is 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.

  • pagerank is 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:

    • NetworkX 3.6 then takes the node as dangling and passes its rank on.
    • 3.7 and later do not, so the node's rank is lost.

    A sum that is NaN fails to converge here and in NetworkX, as before. pagerank is 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.

  • hits with a NaN or infinite weight is declined. NetworkX 3.6 raises ArpackError there. This backend raised PowerIterationFailedConvergence, which is what NetworkX 3.7 raises.

  • hits with 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.

    • With every weight 0, the largest is 0. NetworkX 3.7 does not converge and 3.6's ARPACK raises, while this skipped the division and returned zeros.
    • With a negative weight, the hub values can all be below 0, and the two maxima then differ.

    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=None holds no weights.

  • volume and 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 (_signs in nxapi/_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_apart in nxapi/_utils.py, is a copy of the one in 34-nan-weights. Once both are in, one copy goes.

Stacked on #121.

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

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.28326% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.12%. Comparing base (5b54ace) to head (804d15d).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eriknw
eriknw force-pushed the 37-nonfinite-weights branch 2 times, most recently from 9b45333 to 804d15d Compare December 2, 2026 23:57
@eriknw eriknw changed the title 37 nonfinite weights 37 nan, infinite and nonpositive weights Dec 2, 2026
@eriknw eriknw mentioned this pull request Dec 3, 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