scverse / anndata

Annotated data.
http://anndata.readthedocs.io
BSD 3-Clause "New" or "Revised" License
570 stars 152 forks source link

Modifying `uns` on a view changes the parent object #1118

Open grst opened 1 year ago

grst commented 1 year ago

Please make sure these conditions are met

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

github-actions[bot] commented 11 months ago

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!

flying-sheep commented 11 months ago

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[bot] commented 8 months ago

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!