Skip to content

Commit

Permalink
(fix): write out full PCA results when not run before neighbors (scve…
Browse files Browse the repository at this point in the history
…rse#3079)

Co-authored-by: Philipp A <[email protected]>
  • Loading branch information
ilan-gold and flying-sheep authored Jun 3, 2024
1 parent 874d99b commit 3d03f2e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/1.10.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

* Compatibility with `matplotlib` 3.9 {pr}`2999` {smaller}`I Virshup`
* Add clear errors where `backed` mode-like matrices (i.e., from `sparse_dataset`) are not supported {pr}`3048` {smaller}`I gold`
* Write out full pca results when `_choose_representation` is called i.e., `~sc.pp.neighbors` without `sc.pp.pca` {pr}`3078` {smaller}`I gold`
* Fix deprecated use of `.A` with sparse matrices {pr}`3084` {smaller}`P Angerer`

```{rubric} Performance
Expand Down
10 changes: 10 additions & 0 deletions scanpy/tests/test_neighbors_key_added.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ def test_neighbors_key_added(adata):
)


def test_neighbors_pca_keys_added_without_previous_pca_run(adata):
assert "pca" not in adata.uns and "X_pca" not in adata.obsm
with pytest.warns(
UserWarning,
match=r".*Falling back to preprocessing with `sc.pp.pca` and default params",
):
sc.pp.neighbors(adata, n_neighbors=n_neighbors, random_state=0)
assert "pca" in adata.uns


# test functions with neighbors_key and obsp
@needs.igraph
@needs.leidenalg
Expand Down
7 changes: 4 additions & 3 deletions scanpy/tools/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

import numpy as np
Expand Down Expand Up @@ -37,14 +38,14 @@ def _choose_representation(
X = adata.obsm["X_pca"][:, :n_pcs]
logg.info(f" using 'X_pca' with n_pcs = {X.shape[1]}")
else:
logg.warning(
warnings.warn(
f"You’re trying to run this on {adata.n_vars} dimensions of `.X`, "
"if you really want this, set `use_rep='X'`.\n "
"Falling back to preprocessing with `sc.pp.pca` and default params."
)
n_pcs_pca = n_pcs if n_pcs is not None else settings.N_PCS
X = pca(adata.X, n_comps=n_pcs_pca)
adata.obsm["X_pca"] = X
pca(adata, n_comps=n_pcs_pca)
X = adata.obsm["X_pca"]
else:
logg.info(" using data matrix X directly")
X = adata.X
Expand Down

0 comments on commit 3d03f2e

Please sign in to comment.