-
-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Bug: Fix behavior of structured_to_unstructured on non-trivial dtypes #14310
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
To elaborate - easy to fix as:
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.
Or as
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.
I originally put
and this worked fine for
structured_to_unstructured. Note that if there are no fields the dtype must be given.The problem was that after supplying a dtype, the resulting array of shape "(x, y, z, 0)" for some integers x, y, z has a 0-size axis at the end, which trips up
unstructured_to_unstructuredif you try to reverse the operation, in particular the linereturn arr.view(out_dtype)[..., 0]. It does not make sense to remove the last axis if the dtype is not 0-sized too, and numpy makes it difficult in that case.Eg, this fails on that line (after adding
if n_fields == 0checks inunstructured_to_structured)Because all this was tricky and probably not what the user intended, I thought it might be best to simply disallow mucking with 0-size arrays.
Edit: This whole comment might also be summarized as: If the structured array has no fields, what should the output dtype be? No choice really makes sense.
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.
Some further investigation show that this
viewbehavior is part of what makes it difficult:in other words for
viewseems to have no effect here?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 a really good justification - can you include it in the error message?
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.
I'm not sure that's the example you want - the correct behavior of that should be to raise
ValueError, and it doubt its failure to do that is a problem for you now.But that is indeed a bug, and stems from the fact that there is not enough difference between:
np.dtype(np.void), which might mean "a byte buffer of unknown length"np.dtype("V0"), which might mean "a byte buffer of length 1"np.dtype([]), which might mean "a structured type with no fields"That was one of the motivations for introducing
PyDescr_ISUNSIZED, which currently fires on all 3 but should really only be true for the first one.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.
OK, that's fine. But that led me to think a little more about what is going on in
structured_to_unstructuredwhere I managed to convert a 0-field, itemsize-0 dtype array to a size-0 array. Consider:So I was able to find a bypass numpy's normal restrictions for the size-0 dtype in
structured_to_unstructured, but the behavior in my last comment prevented me from doing the reverse inunstructured_to_unstructured.In any case, this is the mucky size-0 stuff that we can just forget about by simply disallowing fieldless structured types in this PR.
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.
I'm fine with that, but we should probably raise
NotImplementedErroras I mention above. I'll take a look at.viewsome other time, your example looks pretty damningThere 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.
Probably worth capturing this view weirdness in a new issue.
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.
ok, Ill add one