Skip to content

Convert from networkx without the scipy detour - #600

Open
eriknw wants to merge 4 commits into
19-scalar-fast-pathsfrom
20-networkx-ingestion
Open

Convert from networkx without the scipy detour#600
eriknw wants to merge 4 commits into
19-scalar-fast-pathsfrom
20-networkx-ingestion

Conversation

@eriknw

@eriknw eriknw commented Aug 4, 2026

Copy link
Copy Markdown
Member

from_networkx routed every conversion through scipy CSR, paying the
scipy.sparse import (over 100ms) plus an extra coo to csr materialization.
It now builds COO arrays straight from the networkx adjacency and calls
Matrix.from_coo.

  • Simple graphs with numeric weights (809c7e4): the node selection
    preamble mirrors nx.to_scipy_sparse_array, so ordering, subsetting, and
    error behavior match exactly; undirected graphs symmetrize with the same
    diagonal correction nx uses. Non-numeric and array-valued weights keep
    the scipy fallback and its identical errors. graphblas without scipy
    installed can now convert simple graphs (previously ImportError).
  • Multigraphs (89c3e75): the directed branch passes dup_op=plus so
    parallel edges accumulate exactly as scipy's coo to csr summation does.
    Numeric multigraphs no longer need scipy at all; non-numeric weights
    still defer to it.

Parity with the retained scipy path is pinned by tests over parallel edges,
parallel self-loops, reciprocal parallel edges, weights that cancel to zero,
absent weight attributes, weight=None, bool and int weights, and nodelist
permutations and subsets.


Stack created with GitHub Stacks CLIGive Feedback 💬

@eriknw eriknw changed the title 20 networkx ingestion Convert from networkx without the scipy detour Aug 4, 2026
@eriknw
eriknw marked this pull request as ready for review August 4, 2026 16:07
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 89c3e75 to 5f347b7 Compare August 4, 2026 16:12
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch 2 times, most recently from b339cb8 to d088e70 Compare August 5, 2026 03:18
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from d088e70 to 3e0c056 Compare August 5, 2026 17:44
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 3e0c056 to 14e68f5 Compare August 5, 2026 18:03
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 14e68f5 to a6e6e62 Compare August 5, 2026 18:05
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from a6e6e62 to 2901f07 Compare August 6, 2026 07:59
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 2901f07 to 5839782 Compare August 6, 2026 15:39
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 5839782 to 9ed5084 Compare August 6, 2026 15:41
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 9ed5084 to 4dc2ee7 Compare August 6, 2026 20:36
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 4dc2ee7 to 6ad41be Compare August 6, 2026 20:42
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 6ad41be to 7e083a0 Compare August 7, 2026 02:48
eriknw added 4 commits August 7, 2026 00:09
from_networkx always routed nx -> scipy CSR -> Matrix, so every
conversion paid for scipy.sparse (importing it alone costs over 100ms)
plus an extra coo -> csr materialization. Simple graphs with numeric
weights now build COO arrays straight from the nx adjacency and call
Matrix.from_coo. The node selection preamble mirrors
nx.to_scipy_sparse_array so ordering, subsetting, and error behavior
match exactly; undirected graphs are symmetrized with the same diagonal
correction nx uses (self-loop entries appear as wt + wt - wt under
dup_op=plus, which is exact in IEEE arithmetic).

Kept on the scipy fallback automatically: multigraphs (scipy's
duplicate-coordinate summation matches exactly) and weights that do not
form a 1-D numeric array. That second condition covers non-numeric
attributes, including all-string weights, which infer a <U dtype rather
than object; the fallback raises the identical ValueError. It also
covers array-valued weights: sequence attributes of uniform length
infer a 2-D numeric array, which passes a dtype-kind test but which
from_coo would read as a UDT, so they keep raising ValueError as before
rather than quietly widening what from_networkx accepts.

graphblas without scipy installed can now convert simple graphs
(previously ImportError); multigraphs still need scipy.
A networkx multigraph fell back to scipy in from_networkx because the
direct path could not sum the weights of parallel edges. The directed
branch now passes dup_op=plus when the graph is a multigraph, so
parallel edges accumulate exactly as scipy's coo -> csr summation does.
Simple graphs keep dup_op=None and are unchanged, and the undirected
branch already summed because it uses plus for the self-loop diagonal
correction. Numeric multigraphs no longer need scipy at all; non-numeric
weights still defer to it.

Parity with the retained scipy path holds for MultiGraph and
MultiDiGraph with parallel edges, parallel self-loops, reciprocal
parallel edges, parallel edges whose weights cancel to zero (both sides
keep the explicit zero), absent weight attributes (nx defaults to 1),
weight=None, bool and int weights, and nodelist permutations and
subsets. New tests pin the multigraph diagonal sum and assert the
numeric path never reaches the scipy fallback.
@eriknw
eriknw force-pushed the 20-networkx-ingestion branch from 7e083a0 to 29c4957 Compare August 7, 2026 05:09
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