Skip to content

EHN: Add nonedge argument to from_numpy_array and complete the roundtrip - #8552

Merged
dschult merged 3 commits into
networkx:mainfrom
MridulS:gh4876
Apr 16, 2026
Merged

dschult merged 3 commits into
networkx:mainfrom
MridulS:gh4876

Conversation

@MridulS

@MridulS MridulS commented Mar 5, 2026

Copy link
Copy Markdown
Member

Fixes #4876

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, just a couple finer details re: implied dtypes and the "specialness" of nans!

Comment thread networkx/tests/test_convert_numpy.py Resolved
# to the nonedge sentinel. These coordinates become edges in the graph. (convert to int from np.int64)
if python_type == "void":
edges = ((int(e[0]), int(e[1])) for e in zip(*A.nonzero()))
elif nonedge != nonedge: # to check if nonedge is nan

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more explicit way to check this? How about:

Suggested change
elif nonedge != nonedge: # to check if nonedge is nan
elif np.isnan(nonedge):

I think that will work with all the different nan's (math.nan, float("nan"), etc.) and is at least easier to understand IMO.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here would be that this will blow up with strings:

>>> A = np.array([['test', 0.0], [0.0, 'test']])
>>> G = nx.from_numpy_array(A, nonedge='test')
....
....
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

This seemed like the only way to get the isnan check cleanly even though looks magic.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just as soon "require" (though not explicitly, only in the docstring) that nonedge be a numerical scalar - we already disallow object arrays, so the only use-case you'd hit this would be it was a string array (i.e. in the above example, A has dtype U!).

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the liberty of pushing up the few changes on my mind re: test parametrization and documented values (0.0 vs. 0). I left the nan check as-is as it's not a blocker for me and is less-restrictive in its current form. Thanks @MridulS !

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Thanks @MridulS and @rossbar!

@dschult
dschult merged commit c108b89 into networkx:main Apr 16, 2026
44 checks passed
@dschult dschult added this to the 3.7 milestone Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

from_numpy_array() is ambiguous

3 participants