Skip to content
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

Shallow copies of views aren't intuitive #383

Open
ivirshup opened this issue May 21, 2020 · 0 comments
Open

Shallow copies of views aren't intuitive #383

ivirshup opened this issue May 21, 2020 · 0 comments
Labels

Comments

@ivirshup
Copy link
Member

I believe this is underlying cause of scverse/scanpy#1239

Example

from anndata import AnnData
import numpy as np
from copy import copy

orig = AnnData(np.ones((5, 5)))

view = orig[::2]
view_cp = copy(orig[::2])

display(view)
# View of AnnData object with n_obs × n_vars = 3 × 5

display(view_cp)
# View of AnnData object with n_obs × n_vars = 3 × 5

At first, normal enough. But on trying to assign:

view.obsm["val"] = np.ones(view.shape)
view
# AnnData object with n_obs × n_vars = 3 × 5
#     obsm: 'val'

view_cp.obsm["val"] = np.ones(view_cp.shape)
view_cp
# View of AnnData object with n_obs × n_vars = 3 × 5

Explanation

orig = AnnData(np.ones((5, 5)))

view_cp_base = orig[::2]
view_cp = copy(view_cp_base)

view_cp.obsm["val"] = np.ones(view_cp.shape)

view_cp
# View of AnnData object with n_obs × n_vars = 3 × 5

view_cp_base
# AnnData object with n_obs × n_vars = 3 × 5
#     obsm: 'val'

view_cp's attributes maintained a reference to view_cp_base, since the shallow copy does not copy the values of the attributes. So when view_cp.obsm was updated, view_cp_base got actualized.

This behaviour would not occur if we were lazy about generating the aligned mapping instances as suggested in #363 (comment)

@ivirshup ivirshup added the bug label May 21, 2020
@flying-sheep flying-sheep added Bug 🐛 and removed bug labels Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants