10000 TST: np.take out dtype by lorentzenchr · Pull Request #25600 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TST: np.take out dtype #25600

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

lorentzenchr
Copy link
Contributor
@lorentzenchr lorentzenchr commented Jan 16, 2024

Fixes gh-25588, gh-16319, gh-22766, and gh-21676

I'm a numpy newbie. While this PR adds a test case and makes the added test pass, I don't fully see all the consequences of my changes, in particular how PyArray_FromArray works.

@lorentzenchr lorentzenchr changed the title TST add test case FIX: np.take out dtype Jan 16, 2024
@charris charris changed the title FIX: np.take out dtype BUG: np.take out dtype Jan 16, 2024
@charris charris changed the title BUG: np.take out dtype TST: np.take out dtype Jan 16, 2024
@lorentzenchr
Copy link
Contributor Author

@charris I first wanted the CI to finish to prove that the test triggers a failure.

@@ -303,7 +303,7 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis,
*/
flags |= NPY_ARRAY_ENSURECOPY;
}
dtype = PyArray_DESCR(self);
dtype = PyArray_DESCR(out);
Copy link
Member

Choose a reason for hiding this comment

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

Tests are failing, but that isn't surprising. The dtype must be self, because this ensures that the core operation happens with a single identitcal dtype (the input arrays, at the cost of a copy if needed).

In either case, this sis just not right. What is probably required is to add force-casting to the flags NPY_FORCECAST(?). But additionally add an explicit cast safety check.

TBH, there should be many other functions that implement out= which get it right.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you can point me to a function from where to steal that would help me a lot.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure how consistent we are. There are two possibilities:

  1. You allow same-kind casts (probably), but make sure the direction of the cast check is correct.
  2. You only allow equivalent/no casts.

I don't care much, probably 1. is just as well, we tend to copy anyway often enough, and frankly, you could probably integrate the casting intot he core loop in the future in principle.

I think this should be OK as a (admittedly a bit rough) example: https://github.com/numpy/numpy/blob/main/numpy/_core/src/multiarray/multiarraymodule.c#L592

It doesn't use writeback if copy, but that logic should be fine (except forcing the cast).

Copy link
Member

Choose a reason for hiding this comment

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

After seeing that take is used internally in a few more "ufunc like" operations, and those use same-kind casting (actually we can also use NPY_DEFAULT_ASSIGN_CASTING or so in C:
I would lean with going with that same-kind casting here.


This appears to not just fix take, but a chain of other operations. Might be worthwhile to try to finish it.
The example I gave actually gives a DeprecationWarning which would make sense (unlike ufuncs, etc. we are not planning on casting= to allow a very simple resolution for all the places this kicks in.)

But for 2.0, I can also see just doing away with this: It is highly untypical for NumPy in other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is your exact proposal? What should be done?

Copy link
Member

Choose a reason for hiding this comment

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

I would go with testing for same-kind casting of the output, and otherwise give a DeprecationWarning, since it isn't a big hassle to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without clearer instructions, I'm going to have to close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: np.take cast to out argument
2 participants
0