Skip to content

Commit

Permalink
Fix layers parameter in score_genes with .raw (scverse#3155)
Browse files Browse the repository at this point in the history
  • Loading branch information
Intron7 authored Jul 25, 2024
1 parent 88fb2c8 commit 208115d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/1.11.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
```

* Add layer argument to {func}`scanpy.tl.score_genes` and {func}`scanpy.tl.score_genes_cell_cycle` {pr}`2921` {smaller}`L Zappia`
* Prevent `raw` conflict with `layer` in {func}`~scanpy.tl.score_genes` {pr}`3155` {smaller}`S Dicks`

```{rubric} Docs
```
Expand Down
6 changes: 5 additions & 1 deletion src/scanpy/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,18 @@ def _check_array_function_arguments(**kwargs):
)


def _check_use_raw(adata: AnnData, use_raw: None | bool) -> bool:
def _check_use_raw(
adata: AnnData, use_raw: None | bool, *, layer: str | None = None
) -> bool:
"""
Normalize checking `use_raw`.
My intentention here is to also provide a single place to throw a deprecation warning from in future.
"""
if use_raw is not None:
return use_raw
if layer is not None:
return False
return adata.raw is not None


Expand Down
4 changes: 1 addition & 3 deletions src/scanpy/plotting/_anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2020,9 +2020,7 @@ def _prepare_dataframe(
"""

sanitize_anndata(adata)
use_raw = _check_use_raw(adata, use_raw)
if layer is not None:
use_raw = False
use_raw = _check_use_raw(adata, use_raw, layer=layer)
if isinstance(var_names, str):
var_names = [var_names]

Expand Down
2 changes: 1 addition & 1 deletion src/scanpy/tools/_score_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def score_genes(
"""
start = logg.info(f"computing score {score_name!r}")
adata = adata.copy() if copy else adata
use_raw = _check_use_raw(adata, use_raw)
use_raw = _check_use_raw(adata, use_raw, layer=layer)
if is_backed_type(adata.X) and not use_raw:
raise NotImplementedError(
f"score_genes is not implemented for matrices of type {type(adata.X)}"
Expand Down
1 change: 1 addition & 0 deletions tests/test_score_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ def test_layer():
sc.tl.score_genes(adata, gene_set, score_name="X_score")
# score layer (`del` makes sure it actually uses the layer)
adata.layers["test"] = adata.X.copy()
adata.raw = adata
del adata.X
sc.tl.score_genes(adata, gene_set, score_name="test_score", layer="test")

Expand Down

0 comments on commit 208115d

Please sign in to comment.