Skip to content

Commit

Permalink
Update randomstate scrublet (#301)
Browse files Browse the repository at this point in the history
* update randomstate

* update PCA

* update get randomstate

* move to utils

* add release note

* revert pca
  • Loading branch information
Intron7 authored Nov 26, 2024
1 parent 446401b commit b478b8c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
11 changes: 11 additions & 0 deletions docs/release-notes/0.10.12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### 0.10.12 {small}`the-future`

```{rubric} Features
```
```{rubric} Performance
```
```{rubric} Bug fixes
```
```{rubric} Misc
```
* Update `get_random_state` for `scrublet `{pr}`301` {smaller}`S Dicks`
2 changes: 2 additions & 0 deletions docs/release-notes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Release notes

## Version 0.10.0
```{include} /release-notes/0.10.12.md
```
```{include} /release-notes/0.10.11.md
```
```{include} /release-notes/0.10.10.md
Expand Down
3 changes: 2 additions & 1 deletion src/rapids_singlecell/preprocessing/_scrublet/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from anndata import AnnData, concat
from cuml.neighbors import NearestNeighbors
from cupyx.scipy import sparse
from scanpy._utils import get_random_state
from scanpy.preprocessing._utils import sample_comb

from rapids_singlecell.preprocessing._utils import get_random_state

from .sparse_utils import subsample_counts

if TYPE_CHECKING:
Expand Down
12 changes: 6 additions & 6 deletions src/rapids_singlecell/preprocessing/_scrublet/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from typing import TYPE_CHECKING, Literal

import cupy as cp
from cupyx import cusparse
from cupyx.scipy import sparse

from rapids_singlecell.preprocessing._utils import _get_mean_var
from rapids_singlecell.preprocessing._utils import _get_mean_var, _sparse_to_dense

from .sparse_utils import sparse_multiply, sparse_zscore

Expand Down Expand Up @@ -59,10 +58,10 @@ def truncated_svd(

self._counts_obs_norm = self._counts_obs_norm.astype(cp.float32)
self._counts_sim_norm = self._counts_sim_norm.astype(cp.float32)
X_obs = cusparse.sparseToDense(self._counts_obs_norm)
X_obs = _sparse_to_dense(self._counts_obs_norm)
svd = TruncatedSVD(n_components=n_prin_comps, random_state=random_state).fit(X_obs)
X_obs = svd.transform(X_obs)
X_sim = svd.transform(cusparse.sparseToDense(self._counts_sim_norm))
X_sim = svd.transform(_sparse_to_dense(self._counts_sim_norm))
self.set_manifold(X_obs, X_sim)


Expand All @@ -79,8 +78,9 @@ def pca(

self._counts_obs_norm = self._counts_obs_norm.astype(cp.float32)
self._counts_sim_norm = self._counts_sim_norm.astype(cp.float32)
X_obs = cusparse.sparseToDense(self._counts_obs_norm)
X_obs = _sparse_to_dense(self._counts_obs_norm)

pca = PCA(n_components=n_prin_comps, random_state=random_state).fit(X_obs)
X_obs = pca.transform(X_obs)
X_sim = pca.transform(cusparse.sparseToDense(self._counts_sim_norm))
X_sim = pca.transform(_sparse_to_dense(self._counts_sim_norm))
self.set_manifold(X_obs, X_sim)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import cupy as cp
import numpy as np
from cupyx.scipy import sparse
from scanpy._utils import get_random_state

from rapids_singlecell.preprocessing._utils import _get_mean_var
from rapids_singlecell.preprocessing._utils import _get_mean_var, get_random_state

if TYPE_CHECKING:
from numpy.typing import NDArray
Expand Down
9 changes: 9 additions & 0 deletions src/rapids_singlecell/preprocessing/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
from typing import TYPE_CHECKING, Literal

import cupy as cp
import numpy as np
from cupyx.scipy.sparse import issparse, isspmatrix_csc, isspmatrix_csr, spmatrix

if TYPE_CHECKING:
from anndata import AnnData

from rapids_singlecell._utils import AnyRandom


def _sparse_to_dense(X: spmatrix, order: Literal["C", "F"] | None = None) -> cp.ndarray:
if order is None:
Expand Down Expand Up @@ -154,3 +157,9 @@ def _check_use_raw(adata: AnnData, use_raw: None | bool, layer: str | None) -> b
if layer is not None:
return False
return adata.raw is not None


def get_random_state(seed: AnyRandom) -> np.random.RandomState:
if isinstance(seed, np.random.RandomState):
return seed
return np.random.RandomState(seed)

0 comments on commit b478b8c

Please sign in to comment.