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

Modifying uns on a view changes the parent object #1118

Open
3 tasks done
grst opened this issue Sep 1, 2023 · 3 comments
Open
3 tasks done

Modifying uns on a view changes the parent object #1118

grst opened this issue Sep 1, 2023 · 3 comments

Comments

@grst
Copy link
Contributor

grst commented Sep 1, 2023

Please make sure these conditions are met

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of anndata.
  • (optional) I have confirmed this bug exists on the master branch of anndata.

Report

While working on #1070 I found the following

Code:

import numpy as np
from anndata import AnnData

adata = AnnData(np.zeros((3,3)))
adata.uns["arr"] = np.zeros((3,3))
v = adata[:2, :2]
v.uns["arr"][:, 1] = 1
>>> v.uns["arr"]
array([[0., 1., 0.],
       [0., 1., 0.],
       [0., 1., 0.]])
>>> # parent should be unmodified, but it is!
>>> adata.uns["arr"]
array([[0., 1., 0.],
       [0., 1., 0.],
       [0., 1., 0.]])

Versions

anndata main@a01b80e0e990e2977e7b4fca9b3cd38264606af8

Copy link

github-actions bot commented Nov 1, 2023

This issue has been automatically marked as stale because it has not had recent activity.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Nov 1, 2023
@flying-sheep
Copy link
Member

flying-sheep commented Nov 7, 2023

I’m pretty sure we currently only support view.uns['...'] = new_value. Nested mutable objects aren’t really handled in a special way.

We could fix that for classes we support (pd.DataFrames, np.ndarrays, dict, …)

Failing test based on your example code:

def test_modify_view_uns_array():
    adata = ad.AnnData()
    adata.uns["arr"] = np.zeros((3,3))
    v = adata[:2, :2]
    v.uns["arr"][:, 1] = 1
    np.testing.assert_equal(adata.uns["arr"], np.zeros((3,3)))

@github-actions github-actions bot removed the stale label Nov 8, 2023
Copy link

github-actions bot commented Jan 8, 2024

This issue has been automatically marked as stale because it has not had recent activity.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants