Repository navigation
EHN: Add nonedge argument to from_numpy_array and complete the roundtrip - #8552
Conversation
rossbar
left a comment
There was a problem hiding this comment.
Generally LGTM, just a couple finer details re: implied dtypes and the "specialness" of nans!
| # 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 |
There was a problem hiding this comment.
Is there a more explicit way to check this? How about:
| 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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 !
Fixes #4876