|
6 | 6 | from itertools import product |
7 | 7 | from typing import TYPE_CHECKING |
8 | 8 |
|
| 9 | +import h5py |
9 | 10 | import numpy as np |
10 | 11 | import pandas as pd |
11 | 12 | import pytest |
| 13 | +import zarr |
12 | 14 | from numpy import ma |
13 | 15 | from scipy import sparse as sp |
14 | 16 | from scipy.sparse import csr_matrix, issparse |
15 | 17 |
|
16 | 18 | import anndata as ad |
17 | 19 | from anndata import AnnData, ImplicitModificationWarning |
18 | 20 | from anndata._core.raw import Raw |
| 21 | +from anndata._core.sparse_dataset import sparse_dataset |
19 | 22 | from anndata._settings import settings |
20 | 23 | from anndata.tests.helpers import ( |
21 | 24 | GEN_ADATA_NO_XARRAY_ARGS, |
@@ -768,3 +771,23 @@ def test_create_adata_from_single_axis_elem( |
768 | 771 | in_memory.write_h5ad(tmp_path / "adata.h5ad") |
769 | 772 | from_disk = ad.read_h5ad(tmp_path / "adata.h5ad") |
770 | 773 | assert_equal(from_disk, in_memory) |
| 774 | + |
| 775 | + |
| 776 | +@pytest.mark.parametrize("in_x", [True, False], ids=["X", "layers"]) |
| 777 | +@pytest.mark.parametrize("is_sparse", [True, False], ids=["sparse", "dense"]) |
| 778 | +@pytest.mark.parametrize("storage", ["h5ad", "zarr"]) |
| 779 | +def test_transpose_errors_with_backed_arrays( |
| 780 | + tmp_path: Path, storage: str, *, is_sparse: bool, in_x: bool |
| 781 | +): |
| 782 | + adata = AnnData(X=csr_matrix(np.ones((3, 4))) if is_sparse else np.ones((3, 4))) |
| 783 | + path = tmp_path / f"test.{storage}" |
| 784 | + getattr(adata, f"write_{storage}")(path) |
| 785 | + f = (h5py.File if storage == "h5ad" else zarr.open)(path) |
| 786 | + raw_array = sparse_dataset(f["X"]) if is_sparse else f["X"] |
| 787 | + |
| 788 | + adata = AnnData(**({"X": raw_array} if in_x else {"layers": {"test": raw_array}})) |
| 789 | + |
| 790 | + with pytest.raises(ValueError, match=r"Cannot transpose anndata object"): |
| 791 | + adata.transpose() |
| 792 | + if storage == "h5ad": |
| 793 | + f.close() |
0 commit comments