anndata icon indicating copy to clipboard operation
anndata copied to clipboard

A subset of a view can get a different .obs than the view itself

Open adrian-valente opened this issue 1 year ago • 2 comments

Please make sure these conditions are met

  • [X] I have checked that this issue has not already been reported.
  • [X] 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

When a view object is created and its .obs property is modified, my understanding is that the .obs of the view and of its _adata_ref can diverge. A concrete manifestation of this can occur when subsetting the view, in which case the .obs of the subset seems to be extracted from the _adata_ref, hence being different than that of the view. Here is a minimal example.

Code:

import anndata
import numpy as np

a1 = anndata.AnnData(X=np.array([[1, 2, 3], [4, 5, 6]]), obs={'obs_names': ['aa', 'bb'], 'property': [True, True]}, var={'var_names': ['c', 'd', 'e']})
a1 = a1[:, ['c', 'd']]
a1.obs.index = a1.obs.index.map(lambda x: x[-1])
selector = np.array([True, True])

# These should be the same but they are not
print(a1[selector].obs)
print(a1.obs)

Output:

    property
aa      True
bb      True
   property
a      True
b      True

Versions

-----
anndata             0.9.1
numpy               1.22.4
session_info        1.0.0
-----
bottleneck          1.3.5
cloudpickle         2.2.1
cython_runtime      NA
dateutil            2.8.2
debugpy             1.8.0
dill                0.3.6
h5py                3.8.0
importlib_metadata  NA
mpl_toolkits        NA
mpmath              1.3.0
natsort             8.3.1
numexpr             2.8.3
opt_einsum          v3.3.0
packaging           21.3
pandas              1.4.3
pyarrow             10.0.1
pydev_ipython       NA
pydevconsole        NA
pydevd              2.9.5
pydevd_file_utils   NA
pydevd_plugins      NA
pydevd_tracing      NA
pytz                2023.3
ruamel              NA
scipy               1.7.3
six                 1.16.0
sympy               1.11.1
torch               2.0.1
tqdm                4.64.1
typing_extensions   NA
zipp                NA
-----
Python 3.9.12 (main, Apr  5 2022, 01:53:17) [Clang 12.0.0 ]
macOS-10.16-x86_64-i386-64bit
-----
Session information updated at 2023-10-12 17:09

adrian-valente avatar Oct 12 '23 15:10 adrian-valente