Skip to content

Commit

Permalink
(chore): alter tests as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ilan-gold committed Nov 14, 2024
1 parent f2af154 commit 4f97787
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 28 deletions.
6 changes: 3 additions & 3 deletions tests/test_backed_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from scipy import sparse

import anndata as ad
from anndata.compat import SpArray
from anndata.compat import SpArray, SpMatrix
from anndata.tests.helpers import (
GEN_ADATA_DASK_ARGS,
as_dense_dask_array,
Expand Down Expand Up @@ -200,8 +200,8 @@ def test_backed_raw_subset(tmp_path, array_type, subset_func, subset_func2):
var_idx = subset_func2(mem_adata.var_names)
if (
array_type is asarray
and isinstance(obs_idx, list | np.ndarray | sparse.spmatrix | SpArray)
and isinstance(var_idx, list | np.ndarray | sparse.spmatrix | SpArray)
and isinstance(obs_idx, list | np.ndarray | SpMatrix | SpArray)
and isinstance(var_idx, list | np.ndarray | SpMatrix | SpArray)
):
pytest.xfail(
"Fancy indexing does not work with multiple arrays on a h5py.Dataset"
Expand Down
18 changes: 9 additions & 9 deletions tests/test_backed_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import anndata as ad
from anndata._core.anndata import AnnData
from anndata._core.sparse_dataset import sparse_dataset
from anndata.compat import CAN_USE_SPARSE_ARRAY, SpArray
from anndata.compat import CAN_USE_SPARSE_ARRAY, SpArray, SpMatrix
from anndata.experimental import read_dispatched
from anndata.tests.helpers import AccessTrackingStore, assert_equal, subset_func

Expand Down Expand Up @@ -259,8 +259,8 @@ def test_consecutive_bool(
)
def test_dataset_append_memory(
tmp_path: Path,
sparse_format: Callable[[ArrayLike], sparse.spmatrix],
append_method: Callable[[list[sparse.spmatrix]], sparse.spmatrix],
sparse_format: Callable[[ArrayLike], SpMatrix],
append_method: Callable[[list[SpMatrix]], SpMatrix],
diskfmt: Literal["h5ad", "zarr"],
):
path = tmp_path / f"test.{diskfmt.replace('ad', '')}"
Expand Down Expand Up @@ -296,7 +296,7 @@ def test_dataset_append_memory(
)
def test_read_array(
tmp_path: Path,
sparse_format: Callable[[ArrayLike], sparse.spmatrix],
sparse_format: Callable[[ArrayLike], SpMatrix],
diskfmt: Literal["h5ad", "zarr"],
subset_func,
subset_func2,
Expand All @@ -316,7 +316,7 @@ def test_read_array(
ad.settings.use_sparse_array_on_read = True
assert issubclass(type(diskmtx[obs_idx, var_idx]), SpArray)
ad.settings.use_sparse_array_on_read = False
assert issubclass(type(diskmtx[obs_idx, var_idx]), sparse.spmatrix)
assert issubclass(type(diskmtx[obs_idx, var_idx]), SpMatrix)


@pytest.mark.parametrize(
Expand All @@ -328,8 +328,8 @@ def test_read_array(
)
def test_dataset_append_disk(
tmp_path: Path,
sparse_format: Callable[[ArrayLike], sparse.spmatrix],
append_method: Callable[[list[sparse.spmatrix]], sparse.spmatrix],
sparse_format: Callable[[ArrayLike], SpMatrix],
append_method: Callable[[list[SpMatrix]], SpMatrix],
diskfmt: Literal["h5ad", "zarr"],
):
path = tmp_path / f"test.{diskfmt.replace('ad', '')}"
Expand All @@ -356,7 +356,7 @@ def test_dataset_append_disk(
@pytest.mark.parametrize("sparse_format", [sparse.csr_matrix, sparse.csc_matrix])
def test_indptr_cache(
tmp_path: Path,
sparse_format: Callable[[ArrayLike], sparse.spmatrix],
sparse_format: Callable[[ArrayLike], SpMatrix],
):
path = tmp_path / "test.zarr"
a = sparse_format(sparse.random(10, 10))
Expand Down Expand Up @@ -438,7 +438,7 @@ def width_idx_kinds(
)
def test_data_access(
tmp_path: Path,
sparse_format: Callable[[ArrayLike], sparse.spmatrix],
sparse_format: Callable[[ArrayLike], SpMatrix],
idx_maj: Idx,
idx_min: Idx,
exp: Sequence[str],
Expand Down
10 changes: 5 additions & 5 deletions tests/test_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from anndata import AnnData, Raw, concat
from anndata._core import merge
from anndata._core.index import _subset
from anndata.compat import AwkArray, CupySparseMatrix, DaskArray, SpArray
from anndata.compat import AwkArray, CupySparseMatrix, DaskArray, SpArray, SpMatrix
from anndata.tests import helpers
from anndata.tests.helpers import (
BASE_MATRIX_PARAMS,
Expand Down Expand Up @@ -61,7 +61,7 @@ def _filled_array(a, fill_value=None):
return as_dense_dask_array(_filled_array_np(a, fill_value))


@filled_like.register(sparse.spmatrix)
@filled_like.register(SpMatrix)
def _filled_sparse(a, fill_value=None):
if fill_value is None:
return sparse.csr_matrix(a.shape)
Expand Down Expand Up @@ -200,7 +200,7 @@ def test_concatenate_roundtrip(join_type, array_type, concat_func, backwards_com
if isinstance(orig.X, SpArray):
base_type = SpArray
else:
base_type = sparse.spmatrix
base_type = SpMatrix
if isinstance(orig.X, CupySparseMatrix):
base_type = CupySparseMatrix
assert isinstance(result.X, base_type)
Expand Down Expand Up @@ -404,7 +404,7 @@ def test_concatenate_obsm_outer(obsm_adatas, fill_val):
),
)

assert isinstance(outer.obsm["sparse"], sparse.spmatrix)
assert isinstance(outer.obsm["sparse"], SpMatrix)
np.testing.assert_equal(
outer.obsm["sparse"].toarray(),
np.array(
Expand Down Expand Up @@ -1496,7 +1496,7 @@ def test_concat_X_dtype(cpu_array_type, sparse_indexer_type):
if sparse.issparse(result.X):
# See https://github.com/scipy/scipy/issues/20389 for why this doesn't work with csc
if sparse_indexer_type == np.int64 and (
issubclass(cpu_array_type, sparse.spmatrix) or adata.X.format == "csc"
issubclass(cpu_array_type, SpMatrix) or adata.X.format == "csc"
):
pytest.xfail(
"Data type int64 is not maintained for sparse matrices or csc array"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_concatenate_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
pd.DataFrame,
),
varm_types=(sparse.csr_matrix, np.ndarray, pd.DataFrame),
layers_types=(sparse.spmatrix, np.ndarray, pd.DataFrame),
layers_types=(sparse.csr_matrix, np.ndarray, pd.DataFrame),
)


Expand Down
5 changes: 3 additions & 2 deletions tests/test_io_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from scipy import sparse

import anndata as ad
from anndata.compat import SpMatrix
from anndata.tests.helpers import assert_equal, gen_adata


Expand Down Expand Up @@ -99,8 +100,8 @@ def test_dense_to_sparse_memory(tmp_path, spmtx_format, to_convert):
orig = gen_adata((50, 50), np.array)
orig.raw = orig.copy()
orig.write_h5ad(dense_path)
assert not isinstance(orig.X, sparse.spmatrix)
assert not isinstance(orig.raw.X, sparse.spmatrix)
assert not isinstance(orig.X, SpMatrix)
assert not isinstance(orig.raw.X, SpMatrix)

curr = ad.read_h5ad(dense_path, as_sparse=to_convert, as_sparse_fmt=spmtx_format)

Expand Down
5 changes: 2 additions & 3 deletions tests/test_io_dispatched.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

import h5py
import zarr
from scipy import sparse

import anndata as ad
from anndata.compat import SpArray
from anndata.compat import SpArray, SpMatrix
from anndata.experimental import read_dispatched, write_dispatched
from anndata.tests.helpers import assert_equal, gen_adata

Expand Down Expand Up @@ -96,7 +95,7 @@ def set_copy(d, **kwargs):
# TODO: Should the passed path be absolute?
path = "/" + store.path + "/" + k
if hasattr(elem, "shape") and not isinstance(
elem, sparse.spmatrix | SpArray | ad.AnnData
elem, SpMatrix | SpArray | ad.AnnData
):
if re.match(r"^/((X)|(layers)).*", path):
chunks = (M, N)
Expand Down
10 changes: 8 additions & 2 deletions tests/test_io_elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
get_spec,
)
from anndata._io.specs.registry import IORegistryError
from anndata.compat import CAN_USE_SPARSE_ARRAY, SpArray, ZarrGroup, _read_attr
from anndata.compat import (
CAN_USE_SPARSE_ARRAY,
SpArray,
SpMatrix,
ZarrGroup,
_read_attr,
)
from anndata.experimental import read_elem_as_dask
from anndata.io import read_elem, write_elem
from anndata.tests.helpers import (
Expand Down Expand Up @@ -220,7 +226,7 @@ def test_io_spec(store, value, encoding_type):
@pytest.mark.parametrize("as_dask", [False, True])
def test_io_spec_cupy(store, value, encoding_type, as_dask):
if as_dask:
if isinstance(value, sparse.spmatrix):
if isinstance(value, SpMatrix):
value = as_cupy_sparse_dask_array(value, format=encoding_type[:3])
else:
value = as_dense_cupy_dask_array(value)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
import pytest
import zarr
from numba.core.errors import NumbaDeprecationWarning
from scipy import sparse
from scipy.sparse import csc_array, csc_matrix, csr_array, csr_matrix

import anndata as ad
from anndata._io.specs.registry import IORegistryError
from anndata.compat import DaskArray, SpArray, _read_attr
from anndata.compat import DaskArray, SpArray, SpMatrix, _read_attr
from anndata.tests.helpers import as_dense_dask_array, assert_equal, gen_adata
from testing.anndata._helpers import xfail_if_numpy2_loompy

Expand Down Expand Up @@ -161,7 +160,7 @@ def test_readwrite_kitchensink(tmp_path, storage, typ, backing_h5ad, dataset_kwa
# since we tested if assigned types and loaded types are DaskArray
# this would also work if they work
if isinstance(adata_src.raw.X, SpArray):
assert isinstance(adata.raw.X, sparse.spmatrix)
assert isinstance(adata.raw.X, SpMatrix)
else:
assert isinstance(adata_src.raw.X, type(adata.raw.X) | DaskArray)
assert isinstance(
Expand Down

0 comments on commit 4f97787

Please sign in to comment.