|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from functools import partial |
| 4 | + |
| 5 | +import pytest |
| 6 | +from anndata import read_h5ad |
| 7 | + |
| 8 | +import scanpy as sc |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + ("name", "func", "msg"), |
| 13 | + [ |
| 14 | + pytest.param("PCA", sc.pp.pca, " with chunked as False", id="pca"), |
| 15 | + pytest.param( |
| 16 | + "PCA", partial(sc.pp.pca, layer="X_copy"), " from layers", id="pca_layer" |
| 17 | + ), |
| 18 | + pytest.param( |
| 19 | + "regress_out", |
| 20 | + partial(sc.pp.regress_out, keys=["n_counts", "percent_mito"]), |
| 21 | + "", |
| 22 | + id="regress_out", |
| 23 | + ), |
| 24 | + pytest.param( |
| 25 | + "dendrogram", partial(sc.tl.dendrogram, groupby="cat"), "", id="dendrogram" |
| 26 | + ), |
| 27 | + pytest.param("tsne", sc.tl.tsne, "", id="tsne"), |
| 28 | + pytest.param("scale", sc.pp.scale, "", id="scale"), |
| 29 | + pytest.param( |
| 30 | + "downsample_counts", |
| 31 | + partial(sc.pp.downsample_counts, counts_per_cell=1000), |
| 32 | + "", |
| 33 | + id="downsample_counts", |
| 34 | + ), |
| 35 | + pytest.param( |
| 36 | + "filter_genes", |
| 37 | + partial(sc.pp.filter_genes, max_cells=1000), |
| 38 | + "", |
| 39 | + id="filter_genes", |
| 40 | + ), |
| 41 | + pytest.param( |
| 42 | + "filter_cells", |
| 43 | + partial(sc.pp.filter_cells, max_genes=1000), |
| 44 | + "", |
| 45 | + id="filter_cells", |
| 46 | + ), |
| 47 | + pytest.param( |
| 48 | + "rank_genes_groups", |
| 49 | + partial(sc.tl.rank_genes_groups, groupby="cat"), |
| 50 | + "", |
| 51 | + id="rank_genes_groups", |
| 52 | + ), |
| 53 | + pytest.param( |
| 54 | + "score_genes", |
| 55 | + partial(sc.tl.score_genes, gene_list=map(str, range(100))), |
| 56 | + "", |
| 57 | + id="score_genes", |
| 58 | + ), |
| 59 | + ], |
| 60 | +) |
| 61 | +def test_backed_error(backed_adata, name, func, msg): |
| 62 | + with pytest.raises( |
| 63 | + NotImplementedError, |
| 64 | + match=f"{name} is not implemented for matrices of type {type(backed_adata.X)}{msg}", |
| 65 | + ): |
| 66 | + func(backed_adata) |
| 67 | + |
| 68 | + |
| 69 | +def test_log1p_backed_errors(backed_adata): |
| 70 | + with pytest.raises( |
| 71 | + NotImplementedError, |
| 72 | + match="log1p is not implemented for backed AnnData with backed mode not r+", |
| 73 | + ): |
| 74 | + sc.pp.log1p(backed_adata, chunked=True) |
| 75 | + backed_adata.file.close() |
| 76 | + backed_adata = read_h5ad(backed_adata.filename, backed="r+") |
| 77 | + with pytest.raises( |
| 78 | + NotImplementedError, |
| 79 | + match=f"log1p is not implemented for matrices of type {type(backed_adata.X)} without `chunked=True`", |
| 80 | + ): |
| 81 | + sc.pp.log1p(backed_adata) |
| 82 | + backed_adata.layers["X_copy"] = backed_adata.X |
| 83 | + layer_type = type(backed_adata.layers["X_copy"]) |
| 84 | + with pytest.raises( |
| 85 | + NotImplementedError, |
| 86 | + match=f"log1p is not implemented for matrices of type {layer_type} from layers", |
| 87 | + ): |
| 88 | + sc.pp.log1p(backed_adata, layer="X_copy") |
| 89 | + backed_adata.file.close() |
| 90 | + |
| 91 | + |
| 92 | +def test_scatter_backed(backed_adata): |
| 93 | + sc.pp.pca(backed_adata, chunked=True) |
| 94 | + sc.pl.scatter(backed_adata, color="0", basis="pca") |
| 95 | + |
| 96 | + |
| 97 | +def test_dotplot_backed(backed_adata): |
| 98 | + sc.pl.dotplot(backed_adata, ["0", "1", "2", "3"], groupby="cat") |
0 commit comments