-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Avoid unsafe casts from float to unsigned int #9964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fefab07
7813826
2e5a93c
dabed1d
22edc3b
d82300a
d1c7d9b
89a50b6
63e784e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,11 +231,21 @@ | |
xp = get_array_namespace(data) | ||
if xp == np: | ||
# numpy currently doesn't have a astype: | ||
return data.astype(dtype, **kwargs) | ||
Check warning on line 234 in xarray/core/duck_array_ops.py
|
||
return xp.astype(data, dtype, **kwargs) | ||
return data.astype(dtype, **kwargs) | ||
|
||
|
||
def view(data, *args, **kwargs): | ||
if hasattr(data, "__array_namespace__"): | ||
xp = get_array_namespace(data) | ||
if xp == np: | ||
# numpy currently doesn't have a view: | ||
return data.view(*args, **kwargs) | ||
return xp.view(data, *args, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which package supports this? I can't find xp.view in the standard https://data-apis.org/array-api/draft/API_specification/index.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I didn't realize this was implementing a standard when @kmuehlbauer brought it up in #9815. As in #9815, it works for NumPy and Dask. I don't know about any other packages other than tests don't seem to fail about it specifically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't check this fully, but to be honest, I also tried it the same way locally. @Illviljan can you suggest a way forward here, so that it is usable for numpy and dask? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can also try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using But using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. view and astype are very different things no? view simply reinterprets the bytes as a different type, astype preserves the value and may change number of bytes to do so, for example. I'm not really sure what to do here. Perhaps just xfail? Or else you'll have to track down what netcdf-java does, which is the ultimate source of this _Unsigned convention IIRC There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checking in here again to see if you had an idea which way to move forward. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry @QuLogic for the delay. I'm not sure what's the best way forward. I've consulted GDAL's netcdf driver and they use some construct like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @QuLogic According to the suggestions of @Illviljan and @dcherian (and while looking again numpy/numpy#22951) I've pushed a fix which I believe might fix the issue, or at least avoid the undefined cast. Would you mind checking this on your workflows? Thanks! If this works we could clean up the PR and go ahead and merge. |
||
return data.view(*args, **kwargs) | ||
|
||
|
||
def asarray(data, xp=np, dtype=None): | ||
converted = data if is_duck_array(data) else xp.asarray(data) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.