Skip to content

Adopt scanpy's use/out parameters - #882

Merged
Intron7 merged 9 commits into
mainfrom
use-out-accessors
Nov 11, 2026
Merged

Intron7 merged 9 commits into
mainfrom
use-out-accessors

Conversation

@Intron7

@Intron7 Intron7 commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

This matches scanpy 1.13's switch to use/out (scverse/scanpy#4359) for every scanpy-mirrored function in pp, tl and get, plus the rsc-only functions that take the same kinds of arguments (dsb, wnn, harmony_integrate, kmeans, …).

  • use names where a function reads from: an anndata.acc accessor (A.layers["counts"], A.obsm["pca"], a graph A.obsp["nn"]) or its string form ("layers.counts").
  • out names where it writes: False for the default place, True to return the result, an accessor, or for functions with several outputs a mapping of only the outputs to write. A single accessor names the secondary outputs after the primary one, e.g. neighbors(out=A.obsp["nn"]) writes nn, nn_distances and uns["nn"].
  • Functions with several outputs return a …Result NamedTuple for out=True and take a …Out TypedDict (NormalizeTotalResult, QcMetricsResult, ScaleResult, PcaResult, ScrubletResult, NeighborsResult, WnnResult, CellCycleResult); they are exported from rsc.pp/rsc.tl and listed in the API docs.
  • Groupings and covariates take refs such as A.obs["batch"] (batch, covariates).
  • Under scanpy-v1, string batch values of harmony_integrate are obs column names, as in scanpy (compat=True); under scanpy-v2-preview they are resolved as accessors.
  • layer, obsm, use_rep, use_raw, neighbors_key, obsp, adjacency, key_added, batch_key, inplace and copy are deprecated like in scanpy and keep working with a FutureWarning; filter_cells/filter_genes keep copy.

The shared helpers live in get/_out.py, a trimmed port of scanpy's get/_use.py and get/_out.py.

Behaviour changes:

  • calculate_qc_metrics now follows the preset like scanpy: it returns the metrics under scanpy-v1 (scanpy's old inplace=False default) and writes them under scanpy-v2-preview. Pass out=False to keep writing them.
  • umap accepts scanpy's rep. cuML lays out the graph's connected components from the graph alone, so it is not read.
  • An explicit key_added=None under the v2 preset now writes to the v2 keys, as in scanpy.
  • scrublet fills NaN for cells its internal filter removed, instead of raising a KeyError in batched runs.

Tests: the non-dask suite passes apart from failures that also happen on main (test_gpu_lda_matches_sklearn, decoupler gsea). The dask tests match main file by file.

Match scanpy 1.13 (scverse/scanpy#4359): `use` names where a function
reads from (an anndata.acc accessor or its string form) and `out` where it
writes (False for the default place, True to return the result, an
accessor, or a mapping for functions with several outputs). Functions
with several outputs return a ...Result NamedTuple for out=True and take a
...Out TypedDict. Groupings and covariates take refs such as
A.obs["batch"].

Deprecate layer, obsm, use_rep, use_raw, neighbors_key, obsp, adjacency,
key_added, batch_key, inplace and copy as scanpy does; filter_cells and
filter_genes keep copy.

Signed-off-by: Intron7 <[email protected]>
@codecov-commenter

codecov-commenter commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.61316% with 44 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.42%. Comparing base (a948cc2) to head (30ed171).

Files with missing lines Patch % Lines
src/rapids_singlecell/get/_out.py 93.50% 13 Missing ⚠️
src/rapids_singlecell/tools/_utils.py 90.47% 4 Missing ⚠️
...ids_singlecell/preprocessing/_harmony_integrate.py 88.46% 3 Missing ⚠️
...ds_singlecell/preprocessing/_neighbors/__init__.py 94.00% 3 Missing ⚠️
...ids_singlecell/preprocessing/_scrublet/__init__.py 95.31% 3 Missing ⚠️
src/rapids_singlecell/tools/_embedding_density.py 91.66% 3 Missing ⚠️
src/rapids_singlecell/get/_aggregated.py 84.61% 2 Missing ⚠️
src/rapids_singlecell/preprocessing/_dsb.py 90.47% 2 Missing ⚠️
src/rapids_singlecell/preprocessing/_normalize.py 97.67% 2 Missing ⚠️
...rc/rapids_singlecell/preprocessing/_regress_out.py 95.12% 2 Missing ⚠️
... and 6 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #882      +/-   ##
==========================================
+ Coverage   90.91%   91.42%   +0.51%     
==========================================
  Files         130      131       +1     
  Lines       13706    14277     +571     
==========================================
+ Hits        12461    13053     +592     
+ Misses       1245     1224      -21     
Files with missing lines Coverage Δ
src/rapids_singlecell/_settings.py 95.74% <100.00%> (+0.18%) ⬆️
src/rapids_singlecell/pertpy_gpu/_base.py 91.86% <100.00%> (+0.20%) ⬆️
src/rapids_singlecell/pp.py 100.00% <100.00%> (ø)
src/rapids_singlecell/preprocessing/__init__.py 100.00% <100.00%> (ø)
...pids_singlecell/preprocessing/_harmony/__init__.py 98.59% <100.00%> (+0.01%) ⬆️
...rapids_singlecell/preprocessing/_neighbors/_wnn.py 97.30% <100.00%> (+0.10%) ⬆️
src/rapids_singlecell/preprocessing/_qc.py 94.11% <100.00%> (+1.26%) ⬆️
src/rapids_singlecell/squidpy_gpu/_niche.py 91.06% <100.00%> (-0.04%) ⬇️
src/rapids_singlecell/tl.py 100.00% <100.00%> (ø)
src/rapids_singlecell/tools/__init__.py 100.00% <100.00%> (ø)
... and 23 more

... and 2 files with indirect coverage changes

Port scverse/scanpy#4333: pp.pca takes `mask` with anndata.acc references
and deprecates `mask_var`; pca, scale and rank_genes_groups share one
`_mask_arg`, the PCA mask is stored in `.uns` as scanpy stores it, and
ingest reads that form.

Signed-off-by: Intron7 <[email protected]>
- normalize_total: norm_factor on sparse and dense Dask input; results
  not written back to their source no longer modify it
- anndata 0.12/0.13.0-0.13.2: guard the anndata.acc imports and
  resolve(vec=...), with a compat test that hides anndata.acc
- scale(inplace=False) returns the matrix again
- regress_out: ndarray/Index keys and vectors held on the GPU
- normalize_shifted_clr(inplace=False) rejects a conflicting out
- use=A.obsm["X"] no longer falls back to adata.X
- embedding_density: reject non-obsm use and non-categorical groupby
  for out=True
- mixscape: the PCA recompute rewrites uns as well
- HVG and scrublet with use= no longer rebuild obs_names or copy the
  whole AnnData
- release notes for the normalize_total, neighbors/bbknn and draw_graph
  alignments with scanpy

Signed-off-by: Intron7 <[email protected]>

@flying-sheep flying-sheep left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surface-level this looks good, I like that you converted the non-scanpy functions too. Of course I couldn’t look into the details, it’s too much.

*,
batch_key: str | None = None,
use_rep: str | None = None,
batches: AdRef | str | None = None,

@flying-sheep flying-sheep Nov 10, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is called batch in other APIs. might be that I introduced the same inconsistency in scanpy.

@flying-sheep flying-sheep Nov 10, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup I did, fixing now! scverse/scanpy#4505

good thing I spotted it here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also fixed here

@Intron7
Intron7 merged commit c99f3fe into main Nov 11, 2026
25 of 29 checks passed
@Intron7
Intron7 deleted the use-out-accessors branch November 11, 2026 07:34
Intron7 added a commit that referenced this pull request Nov 11, 2026
After the rebase onto #882 as merged:
- import `A` through `pytest.importorskip("anndata.acc")` in the seven test
  modules this branch adds it to, as the other tests do since #882
- test_pca_deprecated_params: assert the `mask_var` deprecation warning of
  the check added with the `mask` rename

Signed-off-by: Intron7 <[email protected]>
Zethson added a commit that referenced this pull request Nov 15, 2026
Bumps notebooks 0b059a3 -> f2a9c13, pulling in
scverse/rapids-singlecell-tutorials#13, #14 and #15: explicit
read_h5ad readers, mask_var=None in brain_1M, read_elem_lazy without the
anndata<0.12 gate, rsc.gr.spatial_neighbors_grid in 04_squidpy
(re-executed), and removal of a broken trailing cell in 03_decoupler.
The notebooks target the released 0.18.1 API; the scanpy 1.13 use/out
migration from #882 is not applied to them yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants