GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — ENH: Add `sort_compare` dtype slot to register new-style sorts by MaanasArora · Pull Request #30415 · numpy/numpy Conversation
|
| If defined, implements a comparison function for two array elements | ||
| for use in sorting and argsorting. This can be defined in place of the | ||
| custom sort functions using the ArrayMethod API (see :ref:`array-methods-sorting`) | ||
| to implement sorting for the DType. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
maybe add a mention for why you'd want to do this - e.g. if comparisons have some big cost that can be amortized over the whole array
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I've added a note - a bit on the detailed side, hopefully that works, thanks!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks for looking @ngoldbaum!
Agreed, the full loop implementation would probably be overkill in many cases. I think this would be necessary to expose the sorts on the user-dtypes too since they don't define full sorts now, so a good way to test the new infrastructure! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
e4de2bd to
0d8c026
Compare
|
Added release note and support for GIL flag. @seberg Friendly ping if you want to look at this when you get the chance! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the delay in looking at this - it really slipped too far....
This looks mostly great, some small comments in-line.
A bigger question is about the name: should it just be compare? I.e., can it not be used also for dtype comparison more generally, replacing NPY_DT_PyArray_ArrFuncs_compare? I cannot really think what the difference would be... (but if we go that route, we can adjust comparison in a follow-up PR). Let me ping @seberg on this bit...
Relatedly, I think not if we want to get rid of ArrFuncs eventually, but let me ask just in case: would it make sense to by default use NPY_DT_PyArray_ArrFuncs_compare if it is available?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| .nout = 1, | ||
| .dtypes = sort_dtypes, | ||
| .slots = sort_slots, | ||
| .flags = NPY_METH_NO_FLOATINGPOINT_ERRORS, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Given it being set here, is it still necessary to also do it in npy_default_get_(arg)sort_loop?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Ah thanks, good catch! It's not - I'll remove it from there.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It's correct in the get_loop, here it is rather irrelevant (it's a default, but for the default get_loop)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand sorry - don't they have the same effect? But I can remove it from here if it's a design / reuse thing.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You can remove it here, it doesn't matter really. The value here is what get_loop uses if you don't provide a custom get_loop. But you do, so it is effectively not used.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| .flags = NPY_METH_NO_FLOATINGPOINT_ERRORS, | ||
| }; | ||
| if (dtype->flags & NPY_NEEDS_PYAPI) { | ||
| sort_spec.flags |= NPY_METH_REQUIRES_PYAPI; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Same question here.
Also, in the other places, it uses PyDataType_REFCHK - any reason for the difference?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Same here, it's not!
I think PyDataType_REFCHK is used for descriptors and not the dtypemeta (at least type wise).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yeah, NPY_NEEDS_PYAPI flag makes more sense. REFCHK doesn't matter, so should be changed in the first one.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Maybe it's best to keep it this way? The I guess this is partly saying that defining the comparisons ufuncs is just easy enough, while sorting is a bit tedious to implement. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This looks good, sorry should have pushed this earlier. I am OK with seeing this is a bit distinct to comparisons.
Mainly, because the alternative to me would seem to be having operands for all normal comparisons + variations for NaN order for less and greater.
But... that adds a complexity that I am not sure is all that helpful (because as mentioned adding the ufunc family is pretty straight forward).
Would it be easy to use this for the SFloat (without losing the other test coverage completely since I think string ufuncs use that also?)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| PyArrayMethod_SortParameters *parameters = (PyArrayMethod_SortParameters *)context->parameters; | ||
| *flags |= NPY_METH_NO_FLOATINGPOINT_ERRORS; | ||
|
|
||
| if (PyDataType_REFCHK(context->descriptors[0])) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
As mentioned below, should use the NPY_REQUIRES_API flag here. Otherwise should be OK, if someone wants to relax this, they'll just have to implement the whole thing...
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This (and removing from registration) is done, thanks! (I'll push sfloat changes later)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| .nout = 1, | ||
| .dtypes = sort_dtypes, | ||
| .slots = sort_slots, | ||
| .flags = NPY_METH_NO_FLOATINGPOINT_ERRORS, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You can remove it here, it doesn't matter really. The value here is what get_loop uses if you don't provide a custom get_loop. But you do, so it is effectively not used.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } PyArrayMethod_SortParameters; | ||
|
|
||
| typedef int (PyArrayDTypeMeta_SortCompare)(const void *a, const void *b, | ||
| PyArrayMethod_Context *context); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Might even be better this way, but I am slightly partial towards putting the context first, just because it is the way in ufunc loops.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I cast directly to CompareFunc, so seems hard to do?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What if we make a wrapper for the old-style CompareFunc? For the numpy dtypes we could (in a follow-up PR perhaps...) then write explicit loops, in order to keep the same performance.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
No worries at all, thanks for reviewing @mhvk @seberg!
Yes, as Sebastian said, if we use this for comparison, I think we lose some functionality - also, keeping this tied to sorting makes it easier to separate concerns if a dtype has some sort-specific quirk (like some optimization that can be made for sort but not compare due to the superset/subset distinction). If we do add a general compare, it can probably be used to fill this by default, but that's for the future! One note though, this should also probably be used for
The |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Sorry, missed the top-level comment - thanks for reviewing.
Yes I think it's quite easy, in fact IIRC I used it for testing - going to push that! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Now saw your reply to my bigger comment. I guess I can see that comparison might be different for Overall, it seems fine for comparison to remain as simple as possible and be used wherever (as it is for the old-style function, I think...). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Just trying to keep playing devil's advocate a bit: the NaN case is indeed one where regular comparisons may different from what is needed for sorting, but wouldn't that be covered by having a context passed in? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Well, they are slightly different if you look ahead:
I am not opposed at all to trying to merge. The big thing is that we definitely need to change the current sorting implementations (which seems fine) and either keep the old one, or add a mini-wrapper (and live with the slight performance hit). Changing+wrapping/duplicating the generic sort loop is fine. I think the main thing is that |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Totally agree that, if we merge, the function needs to be defined differently because total order should be as rich as the Yes, one could define the full sorts if even something slightly special for sorts is needed but once this is used more commonly, it would be nice for user dtypes to try to e.g. squeeze performance with less boilerplate. I like the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.