diff --git a/graphblas/io/_networkx.py b/graphblas/io/_networkx.py index 3e7cfba6e..5ead709ed 100644 --- a/graphblas/io/_networkx.py +++ b/graphblas/io/_networkx.py @@ -1,7 +1,6 @@ import numpy as np from ..dtypes import lookup_dtype -from ._scipy import from_scipy_sparse def from_networkx(G, nodelist=None, dtype=None, weight="weight", name=None): @@ -24,6 +23,14 @@ def from_networkx(G, nodelist=None, dtype=None, weight="weight", name=None): ------- :class:`~graphblas.Matrix` + Raises + ------ + networkx.NetworkXError + If the graph has no nodes, or ``nodelist`` is empty, repeats a node, or + names one the graph does not have (as ``nx.to_scipy_sparse_array`` does). + ValueError + If a weight is not a bool, int (at most 64 bits), float or complex scalar. + """ import networkx as nx @@ -81,28 +88,19 @@ def from_networkx(G, nodelist=None, dtype=None, weight="weight", name=None): # An explicit dtype= is applied above and never reaches this branch. values = values.astype(np.int64) if values.ndim != 1 or values.dtype.kind not in "biufc": - # Defer to scipy so the error matches the previous behavior exactly. # Two kinds of weight land here: non-numeric attributes (object arrays, - # but also e.g. all-string weights, which infer a csr conversion instead: scipy raises - # TypeError ("no supported conversion for types"), which networkx 3.4+ - # wraps in a NetworkXError that blames the sparse format while - # networkx <= 3.3 lets the TypeError propagate. - # Restate it so every supported stack reports the same error for the - # same graph. The graph and nodelist checks above are the ones - # nx.to_scipy_sparse_array makes, so a NetworkXError or TypeError from - # the fallback can only be that dtype complaint. - raise ValueError( - f"scipy.sparse does not support dtype {values.dtype}; " - "edge weights must be numeric scalars" - ) from err + # which ints beyond 64 bits also give, and e.g. all-string weights, which + # infer a