Skip to content

Commit

Permalink
Test or remove all remaining (except graph)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Jan 10, 2025
1 parent a83fe73 commit c3cdd25
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/scanpy/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,8 @@ def check_nonnegative_integers(X: _SupportedArray) -> bool | DaskArray:


@check_nonnegative_integers.register(np.ndarray)
@check_nonnegative_integers.register(sparse.spmatrix)
@check_nonnegative_integers.register(sparse.csr_matrix)
@check_nonnegative_integers.register(sparse.csc_matrix)
def _check_nonnegative_integers_in_mem(X: _MemoryArray) -> bool:
from numbers import Integral

Expand Down
4 changes: 2 additions & 2 deletions src/scanpy/external/tl/_harmony_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def harmony_timeseries(
**X_harmony** - :class:`~numpy.ndarray` (:attr:`~anndata.AnnData.obsm`, dtype `float`)
force directed layout
**harmony_aff** - :class:`~scipy.sparse.spmatrix` (:attr:`~anndata.AnnData.obsp`, dtype `float`)
**harmony_aff** - :class:`~scipy.sparse.csr_matrix` (:attr:`~anndata.AnnData.obsp`, dtype `float`)
affinity matrix
**harmony_aff_aug** - :class:`~scipy.sparse.spmatrix` (:attr:`~anndata.AnnData.obsp`, dtype `float`)
**harmony_aff_aug** - :class:`~scipy.sparse.csr_matrix` (:attr:`~anndata.AnnData.obsp`, dtype `float`)
augmented affinity matrix
**harmony_timepoint_var** - `str` (:attr:`~anndata.AnnData.uns`)
The name of the variable passed as `tp`
Expand Down
2 changes: 1 addition & 1 deletion src/scanpy/external/tl/_palantir.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def palantir(
Array of Diffusion components.
- palantir_EigenValues - :class:`~numpy.ndarray` (:attr:`~anndata.AnnData.uns`, dtype `float`)
Array of corresponding eigen values.
- palantir_diff_op - :class:`~scipy.sparse.spmatrix` (:attr:`~anndata.AnnData.obsp`, dtype `float`)
- palantir_diff_op - :class:`~scipy.sparse.csr_matrix` (:attr:`~anndata.AnnData.obsp`, dtype `float`)
The diffusion operator matrix.
**Multi scale space results**,
Expand Down
5 changes: 2 additions & 3 deletions src/scanpy/preprocessing/_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

import pandas as pd
from numpy.typing import NDArray
from scipy.sparse import spmatrix

from .._compat import _LegacyRandom
from .._utils import RNGLike, SeedLike
Expand All @@ -61,7 +60,7 @@
"min_counts", "min_genes", "max_counts", "max_genes", "inplace", "copy"
)
def filter_cells(
data: AnnData | spmatrix | np.ndarray | DaskArray,
data: AnnData | _CSMatrix | np.ndarray | DaskArray,
*,
min_counts: int | None = None,
min_genes: int | None = None,
Expand Down Expand Up @@ -209,7 +208,7 @@ def filter_cells(
"min_counts", "min_cells", "max_counts", "max_cells", "inplace", "copy"
)
def filter_genes(
data: AnnData | spmatrix | np.ndarray | DaskArray,
data: AnnData | _CSMatrix | np.ndarray | DaskArray,
*,
min_counts: int | None = None,
min_cells: int | None = None,
Expand Down
22 changes: 14 additions & 8 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from scipy import sparse

import scanpy as sc
from scanpy._compat import DaskArray
from testing.scanpy._helpers.data import pbmc68k_reduced
from testing.scanpy._pytest.params import ARRAY_TYPES

Expand Down Expand Up @@ -117,26 +116,33 @@ def test_correctness(metric, size, expected):
np.testing.assert_equal(metric(adata, vals=connected), expected)


@pytest.mark.parametrize("array_type", ARRAY_TYPES)
def test_graph_metrics_w_constant_values(metric, array_type, threading):
@pytest.mark.parametrize(
"array_type", [*ARRAY_TYPES, pytest.param(sparse.coo_matrix, id="scipy_coo")]
)
def test_graph_metrics_w_constant_values(
request: pytest.FixtureRequest, metric, array_type, threading
):
if "dask" in array_type.__name__:
reason = "DaskArray not yet supported"
request.applymarker(pytest.mark.xfail(reason=reason))

# https://github.com/scverse/scanpy/issues/1806
pbmc = pbmc68k_reduced()
XT = array_type(pbmc.raw.X.T.copy())
XT = pbmc.raw.X.T.copy()
g = pbmc.obsp["connectivities"].copy()
equality_check = partial(np.testing.assert_allclose, atol=1e-11)

if isinstance(XT, DaskArray):
pytest.skip("DaskArray yet not supported")

const_inds = np.random.choice(XT.shape[0], 10, replace=False)
with warnings.catch_warnings():
warnings.simplefilter("ignore", sparse.SparseEfficiencyWarning)
XT_zero_vals = XT.copy()
XT_zero_vals[const_inds, :] = 0
XT_zero_vals = array_type(XT_zero_vals)
XT_const_vals = XT.copy()
XT_const_vals[const_inds, :] = 42
XT_const_vals = array_type(XT_const_vals)

results_full = metric(g, XT)
results_full = metric(g, array_type(XT))
# TODO: Check for warnings
with pytest.warns(
UserWarning, match=r"10 variables were constant, will return nan for these"
Expand Down
9 changes: 6 additions & 3 deletions tests/test_qc_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ def test_segments_binary():


@pytest.mark.parametrize(
"cls", [np.asarray, sparse.csr_matrix, sparse.csc_matrix, sparse.coo_matrix]
"array_type", [*ARRAY_TYPES, pytest.param(sparse.coo_matrix, id="scipy_coo")]
)
def test_top_segments(cls):
a = cls(np.ones((300, 100)))
def test_top_segments(request: pytest.FixtureRequest, array_type):
if "dask" in array_type.__name__:
reason = "DaskArray not yet supported"
request.applymarker(pytest.mark.xfail(reason=reason))
a = array_type(np.ones((300, 100)))
seg = top_segment_proportions(a, [50, 100])
assert (seg[:, 0] == 0.5).all()
assert (seg[:, 1] == 1.0).all()
Expand Down
6 changes: 4 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest
from anndata.tests.helpers import asarray
from packaging.version import Version
from scipy.sparse import csr_matrix, issparse
from scipy.sparse import coo_matrix, csr_matrix, issparse

from scanpy._compat import DaskArray, _legacy_numpy_gen, pkg_version
from scanpy._utils import (
Expand Down Expand Up @@ -151,7 +151,9 @@ def test_elem_mul(array_type):
np.testing.assert_array_equal(res, expd)


@pytest.mark.parametrize("array_type", ARRAY_TYPES)
@pytest.mark.parametrize(
"array_type", [*ARRAY_TYPES, pytest.param(coo_matrix, id="scipy_coo")]
)
def test_axis_sum(array_type):
m1 = array_type(asarray([[0, 1, 1], [1, 0, 1]]))
expd_0 = np.array([1, 1, 2])
Expand Down

0 comments on commit c3cdd25

Please sign in to comment.