Skip to content

Commit

Permalink
Deprecate square matrix slicing in .uns
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Dec 23, 2019
1 parent bbc886c commit 142e6d0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
15 changes: 13 additions & 2 deletions anndata/_core/anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,10 @@ def strings_to_categoricals(self, df: Optional[pd.DataFrame] = None):

_sanitize = strings_to_categoricals # backwards compat

def _slice_uns_sparse_matrices_inplace(self, uns: MutableMapping, oidx: Index1D):
def _slice_uns_sparse_matrices_inplace(
self, uns: MutableMapping, oidx: Index1D, keys: Tuple[str, ...] = ()
):
# TODO: remove
# slice sparse spatrices of n_obs × n_obs in self.uns
if not (
isinstance(oidx, slice)
Expand All @@ -1108,11 +1111,19 @@ def _slice_uns_sparse_matrices_inplace(self, uns: MutableMapping, oidx: Index1D)
for k, v in uns.items():
# treat nested dicts
if isinstance(v, Mapping):
self._slice_uns_sparse_matrices_inplace(v, oidx)
self._slice_uns_sparse_matrices_inplace(v, oidx, (*keys, k))
if isinstance(v, sparse.spmatrix) and v.shape == (
self.n_obs,
self.n_obs,
):
path = "".join(f"['{key}']" for key in (*keys, k))
warnings.warn(
f"During AnnData slicing, found matrix at .obs{path} "
"that happens to be dimensioned at n_obs×n_obs "
f"({self.n_obs}×{self.n_obs}). "
"This slicing behavior will soon go away.",
DeprecationWarning,
)
uns[k] = v.tocsc()[:, oidx].tocsr()[oidx, :]

def _inplace_subset_var(self, index: Index1D):
Expand Down
5 changes: 4 additions & 1 deletion anndata/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ def test_slicing_graphs():
np.array([[1, 2], [3, 4], [5, 6]]),
uns=dict(neighbors=dict(connectivities=sp.csr_matrix(np.ones((3, 3))))),
)
adata_sub = adata[[0, 1], :]
with pytest.warns(
DeprecationWarning, match=r".obs\['neighbors'\]\['connectivities'\] .*(3×3)"
):
adata_sub = adata[[0, 1], :]
assert adata_sub.uns["neighbors"]["connectivities"].shape[0] == 2
assert adata.uns["neighbors"]["connectivities"].shape[0] == 3
assert adata_sub.copy().uns["neighbors"]["connectivities"].shape[0] == 2
Expand Down
1 change: 1 addition & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- :attr:`~anndata.AnnData.filename` and :attr:`~anndata.AnnData.isbacked` will be unified under a new name.
- The types of :attr:`~anndata.AnnData.raw`, :attr:`~anndata.AnnData.layers`, :attr:`~anndata.AnnData.obsm`,
:attr:`~anndata.AnnData.varm`, :attr:`~anndata.AnnData.obsp` and :attr:`~anndata.AnnData.varp` will be exported.
- Square matrices in :attr:`~anndata.AnnData.uns` will no longer be sliced (use `.{obs,var}p` instead).


0.7rc1 :small:`December 23, 2019`
Expand Down

0 comments on commit 142e6d0

Please sign in to comment.