-
-
Notifications
You must be signed in to change notification settings - Fork 11k
WIP: MAINT: Made clip into an ufunc #7876
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
Conversation
Nice efforts! As is, I think what might work is if you create a special type resolver for it (there may already be one which does it also). We could consider creating the special type resolver also, and give a FutureWarning that in the future it will not always cast to a floating value magically, but will also honor integer loops, etc. I don't have the time right now, but if you can't find it, just make a note or I will probably forget to give more hints. |
A bit of a look, it looks like you are on the right track. I would say that you can put it into a single loop if possible. Not sure about the error on first sight, it sounds a bit like it does not have an |
Thanks for your comments. I can replicate the previous type behaviour by creating a type resolver that forces the casting to be unsafe. I'm not sure how to introduce any warnings, because the type resolver doesn't seem to have access to the target array type, so the error can't be raised there. I come across another problem: Does anyone have any ideas how to address these issues? Thanks |
There seems to be another NaN issue. On all but Travis build nr 5, it shows error: However, when I change
, which yields
, to
, it shows
Can anyone advise on what causes this? Thanks |
Hmm, I don't really know floating point flag details. You could check for NaN first to avoid the flag being set probably, or possible unset the flag (@juliantaylor might know without thinking much). About giving a warning, one thing we did before was introduce special casting flag like "FORCE_CAST_BUT_WARN", it is a bit of a cludge, but works. |
Could #5142 (behavior of |
☔ The latest upstream changes (presumably #8475) made this pull request unmergeable. Please resolve the merge conflicts. |
Since homu mentioned it, looked at it again. In principle I still really like it, and I think the NaN problems can probably be gotten around (mostly) (on linux there is a comparison function which will not set the floating point error flags for example). One problem I still see is that it seems that the clip function may allow for If you ever pick it up again or decide its too complex, we can still warm up the old PR to just fix the out problem.... |
I'll take another look at this this weekend, see if I can finish it / make some progress. |
@drumstok: Think you'll return to this, or do you want someone else to take over? |
In order to solve issue #7633, it was suggested to make clip into an ufunc. This is an partial implementation of that. Left to do: - Implement proper typecasting - Propely handle None and NaN - Add a faster loop to loops.c.src to speed up if min or max are not arrays - Update the docs - Create benchmarks and test the new implementation - Remove remnants of the old implementation from - - numeric.py - - fromnumeric.py - - numpy_api.py - - multiarray_api.* - - arraytypes.*.src - - calculation.*
In order to solve issue #7633, it was suggested to make clip into an ufunc. This is an partial implementation of that. The tests should pass except for test_clip_nan (test_numeric.TestClip), which tests NaN behaviour if either min or max is missing. Ufuncs doen't seem to allow for missing arguements, so ideal (f)min/max are used. However, the expected behaviour is asymmetrical: propagate NaN for the first, but not for the other arguments. None of the existing ufuncs has this behaviour. Left to do: - Handle asymmetrical NaN behaviour is either min or max is missing - Implement warning if unsafe typecasting is used - Add a faster loop to loops.c.src to speed up if min or max are not arrays - Update the docs - Create benchmarks and test the new implementation - Remove remnants of the old implementation from - - numeric.py - - fromnumeric.py - - numpy_api.py - - multiarray_api.* - - arraytypes.*.src - - calculation.*
I have rebased and incorporated #8475 in the ufunc docstrings. However, having looked again at the work that still needs to be done, I am fine with someone taking over. The complications that arise from the NaN's and the allowing of missing arguments make this a bit too complicated for me at the moment. ( @eric-wieser ) |
Continued in #12519 |
I guess we can close it for now then in favor of the new approach. The 3 ufunc approach seems more realistic anyway. Thanks all! |
In order to solve issue #7633, it was suggested at #7873 to make clip into an ufunc.
This is an partial implementation of that. Left to do:
numeric.py
fromnumeric.py
numpy_api.py
multiarray_api.*
arraytypes.*.src
calculation.*
But before I continue, I'd like to ask the following:
test_clip_with_out_array_int32
fails with messageTypeError: ufunc 'clip' output (typecode 'd') could not be coerced to provided output parameter (typecode 'i') according to the casting rule ''same_kind''
because not all the arguments are of the same type.arraytypes.c
fastclip
is written down very concisely by doing almost all types at once. Inloops.c.src
, however, all definitions are split into the main categories (ints, floats, etc.). So far, I've followed that expanded approach, but the concise version seems nicer. Is it acceptable to add the concise combined version to the bottom ofloops.c.src
instead?