-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
WIP: make views of different type checks less strict #5508
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
f7457d8
to
f71c964
Compare
It should now pass all the tests, lets see what Travis thinks, and I think I have the logic pretty much figured out. Still missing tests and probably a rewrite of the docs. |
There don't seem to be any specific tests for |
int j, axis; | ||
int nd = PyArray_NDIM(self); | ||
|
||
if (nd == 0 || PyDataType_HASSUBARRAY(newtype)) { |
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.
As far as I understand this forbids structured arrays, e.g. fails the following code:
dt_head = np.dtype([('dx', '>u4'), ('dy', '>u4'), ('dz', '>u4')])
dt = np.dtype([('head', dt_head), ('data', '>u4', 10)])
y = np.zeros(1, dt)
y[0]['head'].view('>u4')[:] = 5
Any reason not to support it? Apart from that, that it is not working in master or previous releases either ;o).
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.
That's definitely out of the scope of this PR, but yes, it is a good question for which I don't have a definite answer. It would seem that, as long as there are no Python objects, all you should care about is the element size. Actually, with this PR you can do the following, which removes the [0]
(but would work with a 1 item slice like [:1]
) and uses an intermediate conversion to a void dtype:
>>> y['head'].view('|V12').view('>u4')[:] = 5
>>> y
array([((5L, 5L, 5L), [0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L])],
dtype=[('head', [('dx', '>u4'), ('dy', '>u4'), ('dz', '>u4')]), ('data', '>u4', (10,))])
So unless I am missing something horribly bad that could be happening, that check should also be revisited.
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.
That's definitely out of the scope of this PR
Any way, thanks for consideration! And of course, I can fully understand that it is out of scope. Was worth a try :o).
My current work around which works in master and previous versions is
>>> y[0]['head'].view('>3u4')[:] = 5
>>>
array([((5L, 5L, 5L), [0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L])],
dtype=[('head', [('dx', '>u4'), ('dy', '>u4'), ('dz', '>u4')]), ('data', '>u4', (10,))])
Still it would be nice to do that directly without the need to calculate the size (here the hardcode 3) in advance via size = dt_head.itemsize / np.dtype(">u4").itemsize
...
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.
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.
thanks for letting me know, I'll have a look into your links!
@jaimefrio Needs a rebase. |
Read thought it, and it makes sense, though there are still a couple lines I need to go over. I have some more conceptual questions/comments: Does it often happen in real life that the minimal stride axis is not the first or last axis? What's an example of that? Also, I am curious if anyone knows examples in the wild where it is useful to view using a different itemsize (eg
By the way, I looked into the history of the code which does this (in |
Relax the previous condition (that the whole array be contiguous) to allow a view with a dtype of a different size to be produced. See the code comment for details, basically now only the dimension with the smallest absolute value stride is required to be contiguous.
I have rebased, and I think this should now pass all tests. Before anyone gets tempted to merge it:
|
Not sure. Ian seems to have the proposal of adding dimensions for the view, which somewhat makes sense but I am not quite sure that the logic actually is very consistent with fractional size diffs and larger vs. smaller types. |
2 comments: First, in that list discussion it was suggested we could add an extra 'order' or 'axis' keyword arg to What about having a
Unlike
(and implementation in |
Second, I like this PR as is. (...maybe with corrections for size-1 axes). It might be a little unintuitive but that's quickly fixed by a brief section in the docs about "views with different itemsizes", along with a big warning that you have to think carefully about memory layout if you want to change the itemsize. I think we should have a new section in the user guide on views (right before byteswapping), the last section of which could be something like this:
|
☔ The latest upstream changes (presumably #6208) made this pull request unmergeable. Please resolve the merge conflicts. |
@jaimefrio Needs rebase. |
Should we bring views of non-contiguous arrays up again on the mailing list? |
This seems somewhat interesting, but after looking at it and considering that it was still a work in progress, it seems to me that the best thing may be to close it until it is brought up again (with more use-cases maybe). Especially, since it does not seem we were able to quite hash out the desired behaviour and some of it is pretty surprising! EDIT: If anyone disagrees, please just comment. |
This PR allows to take views of a different type even if they are not in a single memory block, as long as they have a dimension along which the stride is the same as the element size and this is unique. The following examples would raise an error in current master, but work now:
I would like to have some more eyes on the logic of what is and is not possible: while this is much more flexible than before, it may be possible to push the boundary a little further out, e.g. the requirement that the minium stride be equal to the element size may not be necessary.