Skip to content

Commit 652645d

Browse files
authored
Fix Seurat n_top_genes default (#2782)
1 parent 266c054 commit 652645d

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

docs/release-notes/1.9.7.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
- Fix {func}`scanpy.pl.violin` usage of `seaborn.catplot` {pr}`2739` {smaller}`E Roellin`
88
- Fix {func}`scanpy.pp.highly_variable_genes` to handle the combinations of `inplace` and `subset` consistently {pr}`2757` {smaller}`E Roellin`
99
- Replace usage of various deprecated functionality from {mod}`anndata` and {mod}`pandas` {pr}`2678` {pr}`2779` {smaller}`P Angerer`
10+
- Allow to use default `n_top_genes` when using {func}`scanpy.pp.highly_variable_genes` flavor `'seurat_v3'` {pr}`2782` {smaller}`P Angerer`

scanpy/preprocessing/_highly_variable_genes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
22

33
import warnings
4-
from typing import Literal
4+
from inspect import signature
5+
from typing import Literal, cast
56

67
import numpy as np
78
import pandas as pd
@@ -328,7 +329,7 @@ def highly_variable_genes(
328329
max_disp: float | None = np.inf,
329330
min_mean: float | None = 0.0125,
330331
max_mean: float | None = 3,
331-
span: float | None = 0.3,
332+
span: float = 0.3,
332333
n_bins: int = 20,
333334
flavor: Literal["seurat", "cell_ranger", "seurat_v3"] = "seurat",
334335
subset: bool = False,
@@ -455,6 +456,9 @@ def highly_variable_genes(
455456
)
456457

457458
if flavor == "seurat_v3":
459+
if n_top_genes is None:
460+
sig = signature(_highly_variable_genes_seurat_v3)
461+
n_top_genes = cast(int, sig.parameters["n_top_genes"].default)
458462
return _highly_variable_genes_seurat_v3(
459463
adata,
460464
layer=layer,

scanpy/tests/test_highly_variable_genes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,16 @@ def test_higly_variable_genes_compare_to_seurat_v3():
387387
seu = pd.Index(seurat_hvg_info_batch["x"].values)
388388
assert len(seu.intersection(df.index)) / 4000 > 0.95
389389

390+
391+
@needs.skmisc
392+
def test_higly_variable_genes_seurat_v3_warning():
393+
pbmc = pbmc3k()[:200].copy()
390394
sc.pp.log1p(pbmc)
391395
with pytest.warns(
392396
UserWarning,
393397
match="`flavor='seurat_v3'` expects raw count data, but non-integers were found.",
394398
):
395-
sc.pp.highly_variable_genes(pbmc, n_top_genes=1000, flavor="seurat_v3")
399+
sc.pp.highly_variable_genes(pbmc, flavor="seurat_v3")
396400

397401

398402
def test_filter_genes_dispersion_compare_to_seurat():

0 commit comments

Comments
 (0)