Skip to content

Commit

Permalink
Neighbors & umap fix (#82)
Browse files Browse the repository at this point in the history
* Fixes for cupy anndata

* added release note

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Intron7 and pre-commit-ci[bot] authored Oct 18, 2023
1 parent b038bcc commit 4888c86
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/release-notes/0.9.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### 0.9.0 {small}`the future``

```{rubric} Bug fixes
```

* Fixes implicit conversion issue for `Umap` and `neighbors` {pr}`82` {smaller}`S Dicks`
3 changes: 3 additions & 0 deletions docs/release-notes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Release notes

## Version 0.9.0
```{include} /release-notes/0.9.2.md
``````
```{include} /release-notes/0.9.1.md
``````
Expand Down
8 changes: 7 additions & 1 deletion src/rapids_singlecell/preprocessing/_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from anndata import AnnData
from cuml.manifold.simpl_set import fuzzy_simplicial_set
from cuml.neighbors import NearestNeighbors
from cupyx.scipy import sparse

from rapids_singlecell.tools._utils import _choose_representation

Expand Down Expand Up @@ -124,7 +125,12 @@ def neighbors(
nn = NearestNeighbors(
n_neighbors=n_neighbors, algorithm=algorithm, metric=metric, output_type="cupy"
)
X_contiguous = np.ascontiguousarray(X, dtype=np.float32)
if isinstance(X, cp.ndarray):
X_contiguous = cp.ascontiguousarray(X, dtype=np.float32)
elif isinstance(X, sparse.spmatrix):
X_contiguous = X
else:
X_contiguous = np.ascontiguousarray(X, dtype=np.float32)
nn.fit(X_contiguous)
distances = nn.kneighbors_graph(X_contiguous, mode="distance")
n_obs = adata.shape[0]
Expand Down
11 changes: 10 additions & 1 deletion src/rapids_singlecell/tools/_umap.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Literal, Optional

import cupy as cp
import numpy as np
from anndata import AnnData
from cuml import UMAP
from cuml.manifold.umap_utils import find_ab_params
from cupyx.scipy import sparse
from scanpy._utils import NeighborsView
from sklearn.utils import check_random_state

Expand Down Expand Up @@ -128,7 +130,14 @@ def umap(
500 if maxiter is None else maxiter
) # 0 is not a valid value for rapids, unlike original umap
metric = neigh_params.get("metric", "euclidean")
X_contiguous = np.ascontiguousarray(X, dtype=np.float32)

if isinstance(X, cp.ndarray):
X_contiguous = cp.ascontiguousarray(X, dtype=np.float32)
elif isinstance(X, sparse.spmatrix):
X_contiguous = X
else:
X_contiguous = np.ascontiguousarray(X, dtype=np.float32)

n_obs = adata.shape[0]
if neigh_params.get("method") == "rapids":
knn_dist = neighbors["distances"].data.reshape(n_obs, n_neighbors)
Expand Down

0 comments on commit 4888c86

Please sign in to comment.