-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
ENH: Add shape to *_like() array creation #13046
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
f885c08
5ad61b6
fd3e270
b7202a7
013cbce
807f512
95bbfd0
43b9828
fa7fd75
d57e6d3
f1b3e91
d24ac10
ec26417
40e7e9e
928952d
e394356
d938fb9
99a55e0
c087801
db7614b
669ea71
f53afbe
c3ac08e
695b836
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1214,6 +1214,12 @@ PyArray_NewLikeArrayWithShape(PyArrayObject *prototype, NPY_ORDER order, | |
dims = PyArray_DIMS(prototype); | ||
new_shape = 0; | ||
} | ||
else if (order == NPY_KEEPORDER && (ndim != PyArray_NDIM(prototype) || | ||
!(PyArray_IS_C_CONTIGUOUS(prototype) || PyArray_IS_F_CONTIGUOUS(prototype)))) { | ||
PyErr_SetString(PyExc_ValueError, | ||
"mismatching ndim or non-C/F-layout arrays can not keep order"); | ||
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. Why are you checking 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. Because we can't keep the order when there's no particular memory layout, just like in the discussion here: 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. My current understanding of 'K' is that we try to keep the memory order if possible, not in all circumstances. Indeed, the former code fell back on recomputing the strides when the prototype was neither c/f continguous. 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. First of all, we should make sure that this does not switch to an error suddenly. I agree that in most cases I think the default may actually be There are some strides where it would be unambiguous, but I am not sure it is worth to bother about those. 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. Unless I'm miserably misunderstanding some of the comments, I think that there are two clashing opinions:
Feel free to correct me if I'm really misunderstanding it, otherwise, it would be nice to have here @charris, @eric-wieser and @seberg commenting here to reach an agreement before I do any further changes regarding this matter. 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. @eric-wieser could you let me know about your findings then? I'm sorry, but I don't understand what you mean with your previous statement, it may be getting a little too complex for my understanding of NumPy handling of strides and orders. 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. Sure, here's the case I'm thinking of: >>> a = np.zeros((20, 30, 40)).transpose((1, 0, 2))[::10,::10,::10]
>>> a.strides
(3200, 96000, 80)
>>> np.copy(a, order='K').strides
(32, 96, 8) note how the order of the strides is kept. This is the behavior I would expect for 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. @eric-wieser I believe the new commit I pushed covers now the case you're looking for. Extending the case from your previous comment, this is what we get with this new commit: >>> np.empty_like(a, shape=(3, 2, 4), order='K').strides
(32, 96, 8) Can you confirm this is what you had in mind? 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. That behavior looks correct to me now. Well take one more look at this tonight. 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. @eric-wieser just checking, have you had the chance to have a look at this? Do you think we're good to merge this now? |
||
return ret; | ||
} | ||
|
||
/* If no override data type, use the one from the prototype */ | ||
if (dtype == NULL) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks previous behavior which did not require the prototype to be contiguous.
I'm not sure why an error should be raised, why not just override the shape and go with C order when a new shape is specified and order == 'K'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@charris what you're saying is like my original implementation. I modified this based on @eric-wieser's request here #13046 (comment). For me it's fine either way.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@charris: This shouldn't break any existing code once my comment below is addressed. The second check here makes no sense to me either.
The thinking is that requesting the order (of the strides) to be kept is nonsensical if the the number of strides is not the same.