Skip to content

Backport PR #3351: Simplify scale #3635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/3351.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix zappy compatibility for clip_array {smaller}`P Angerer`
14 changes: 11 additions & 3 deletions src/scanpy/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
_MemoryArray = NDArray | CSBase
_SupportedArray = _MemoryArray | DaskArray

_SA = TypeVar("_SA", bound=_SupportedArray)

_ForT = TypeVar("_ForT", bound=Callable | type)


Expand Down Expand Up @@ -273,9 +275,7 @@ def get_igraph_from_adjacency(adjacency: CSBase, *, directed: bool = False) -> G
import igraph as ig

sources, targets = adjacency.nonzero()
weights = adjacency[sources, targets]
if isinstance(weights, np.matrix):
weights = weights.A1
weights = dematrix(adjacency[sources, targets]).ravel()
g = ig.Graph(directed=directed)
g.add_vertices(adjacency.shape[0]) # this adds adjacency.shape[0] vertices
g.add_edges(list(zip(sources, targets, strict=True)))
Expand Down Expand Up @@ -836,6 +836,14 @@ def _check_nonnegative_integers_dask(X: DaskArray) -> DaskArray:
return X.map_blocks(check_nonnegative_integers, dtype=bool, drop_axis=(0, 1))


def dematrix(x: _SA | np.matrix) -> _SA:
if isinstance(x, np.matrix):
return x.A
if isinstance(x, DaskArray) and isinstance(x._meta, np.matrix):
return x.map_blocks(np.asarray, meta=np.array([], dtype=x.dtype))
return x


def select_groups(
adata: AnnData,
groups_order_subset: Iterable[str] | Literal["all"] = "all",
Expand Down
15 changes: 6 additions & 9 deletions src/scanpy/preprocessing/_deprecated/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from scipy import sparse

from ..._compat import CSBase, old_positionals
from ..._utils import dematrix


@old_positionals("max_fraction", "mult_with_mean")
Expand Down Expand Up @@ -39,15 +40,11 @@ def normalize_per_cell_weinreb16_deprecated(
msg = "Choose max_fraction between 0 and 1."
raise ValueError(msg)

counts_per_cell = x.sum(1)
if isinstance(counts_per_cell, np.matrix):
counts_per_cell = counts_per_cell.A1
gene_subset = np.all(x <= counts_per_cell[:, None] * max_fraction, axis=0)
if isinstance(gene_subset, np.matrix):
gene_subset = gene_subset.A1
tc_include = x[:, gene_subset].sum(1)
if isinstance(tc_include, np.matrix):
tc_include = tc_include.A1
counts_per_cell = dematrix(x.sum(1)).ravel()
gene_subset = dematrix(
np.all(x <= counts_per_cell[:, None] * max_fraction, axis=0)
).ravel()
tc_include = dematrix(x[:, gene_subset].sum(1)).ravel()

x_norm = (
x.multiply(sparse.csr_matrix(1 / tc_include[:, None])) # noqa: TID251
Expand Down
7 changes: 3 additions & 4 deletions src/scanpy/preprocessing/_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .. import logging as logg
from .._compat import CSBase, DaskArray, old_positionals
from .._utils import axis_mul_or_truediv, axis_sum, view_to_actual
from .._utils import axis_mul_or_truediv, axis_sum, dematrix, view_to_actual
from ..get import _get_obs_rep, _set_obs_rep

try:
Expand Down Expand Up @@ -216,9 +216,8 @@ def normalize_total( # noqa: PLR0912, PLR0915
counts_per_cell = np.ravel(counts_per_cell)

# at least one cell as more than max_fraction of counts per cell

gene_subset = axis_sum((x > counts_per_cell[:, None] * max_fraction), axis=0)
gene_subset = np.asarray(np.ravel(gene_subset) == 0)
hi_exp = dematrix(x > counts_per_cell[:, None] * max_fraction)
gene_subset = axis_sum(hi_exp, axis=0) == 0

msg += (
". The following highly-expressed genes are not considered during "
Expand Down
Loading
Loading