-
Notifications
You must be signed in to change notification settings - Fork 152
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
Fix ArrayView and numpy subclassing #335
Conversation
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 the PR.
I've made a suggestion about a possible shorter change.
Is this something you could easily test for? If you don't want it to accidentally get broken in the future, adding a test would be important.
anndata/_core/views.py
Outdated
if not isinstance(input_array, np.ndarray): | ||
input_array = np.array(input_array) | ||
arr = input_array.view(cls) |
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.
if not isinstance(input_array, np.ndarray): | |
input_array = np.array(input_array) | |
arr = input_array.view(cls) | |
arr = np.asanyarray(input_array).view(cls) |
Could this work instead? I think it should if your class is literally a np.ndarray subclass.
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 the suggestion! I've added a test and it works.
Any news on this? I wanted to fix the code-quality check (just for the test - either get overriding builtin when using |
Sorry, just been busy! I think this looks good, thanks! |
Hi,
I have a
numpy.ndarray
subclass with some column annotations (alsondarrays
), including aview-like object. Problem is that taking view of
adata
object first converts this tonp.ndarray
,effectively removing my subclass (
arr = np.asarray(input_array)
). This PR fixes this such that only non-arrays are converted to arrays, rest is left the same.