-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
432 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from __future__ import annotations | ||
|
||
import cupy as cp | ||
from cupyx.scipy import sparse as cusparse | ||
from anndata.tests.helpers import as_dense_dask_array, as_sparse_dask_array | ||
import pytest | ||
|
||
from dask_cuda import LocalCUDACluster | ||
from dask_cuda.utils_test import IncreasedCloseTimeoutNanny | ||
from dask.distributed import Client | ||
|
||
|
||
def as_sparse_cupy_dask_array(X): | ||
da = as_sparse_dask_array(X) | ||
da = da.rechunk((da.shape[0]//2, da.shape[1])) | ||
da = da.map_blocks(cusparse.csr_matrix, dtype = X.dtype) | ||
return da | ||
|
||
def as_dense_cupy_dask_array(X): | ||
X = as_dense_dask_array(X) | ||
X = X.map_blocks(cp.array) | ||
X = X.rechunk((X.shape[0]//2, X.shape[1])) | ||
return X | ||
|
||
from dask_cuda import initialize | ||
from dask_cuda import LocalCUDACluster | ||
from dask_cuda.utils_test import IncreasedCloseTimeoutNanny | ||
from dask.distributed import Client | ||
|
||
@pytest.fixture(scope="module") | ||
def cluster(): | ||
|
||
cluster = LocalCUDACluster( | ||
CUDA_VISIBLE_DEVICES ="0", | ||
protocol="tcp", | ||
scheduler_port=0, | ||
worker_class=IncreasedCloseTimeoutNanny, | ||
) | ||
yield cluster | ||
cluster.close() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def client(cluster): | ||
|
||
client = Client(cluster) | ||
yield client | ||
client.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import annotations | ||
|
||
import cupy as cp | ||
import numpy as np | ||
from cupyx.scipy import sparse as cusparse | ||
from scipy import sparse | ||
from conftest import as_dense_cupy_dask_array, as_sparse_cupy_dask_array | ||
import rapids_singlecell as rsc | ||
|
||
from scanpy.datasets import pbmc3k_processed | ||
|
||
def test_pca_sparse_dask(client): | ||
sparse_ad = pbmc3k_processed() | ||
default = pbmc3k_processed() | ||
sparse_ad.X = sparse.csr_matrix(sparse_ad.X.astype(np.float64)) | ||
default.X = as_sparse_cupy_dask_array(default.X.astype(np.float64)) | ||
rsc.pp.pca(sparse_ad) | ||
rsc.pp.pca(default) | ||
|
||
cp.testing.assert_allclose( | ||
np.abs(sparse_ad.obsm["X_pca"]), | ||
cp.abs(default.obsm["X_pca"].compute()), | ||
rtol=1e-7, | ||
atol=1e-6, | ||
) | ||
|
||
cp.testing.assert_allclose( | ||
np.abs(sparse_ad.varm["PCs"]), np.abs(default.varm["PCs"]), rtol=1e-7, atol=1e-6 | ||
) | ||
|
||
cp.testing.assert_allclose( | ||
np.abs(sparse_ad.uns["pca"]["variance_ratio"]), | ||
np.abs(default.uns["pca"]["variance_ratio"]), | ||
rtol=1e-7, | ||
atol=1e-6, | ||
) | ||
|
||
def test_pca_dense_dask(client): | ||
sparse_ad = pbmc3k_processed() | ||
default = pbmc3k_processed() | ||
sparse_ad.X = cp.array(sparse_ad.X.astype(np.float64)) | ||
default.X = as_dense_cupy_dask_array(default.X.astype(np.float64)) | ||
rsc.pp.pca(sparse_ad, svd_solver="full") | ||
rsc.pp.pca(default, svd_solver="full") | ||
|
||
cp.testing.assert_allclose( | ||
np.abs(sparse_ad.obsm["X_pca"]), | ||
cp.abs(default.obsm["X_pca"].compute()), | ||
rtol=1e-7, | ||
atol=1e-6, | ||
) | ||
|
||
cp.testing.assert_allclose( | ||
np.abs(sparse_ad.varm["PCs"]), np.abs(default.varm["PCs"]), rtol=1e-7, atol=1e-6 | ||
) | ||
|
||
cp.testing.assert_allclose( | ||
np.abs(sparse_ad.uns["pca"]["variance_ratio"]), | ||
np.abs(default.uns["pca"]["variance_ratio"]), | ||
rtol=1e-7, | ||
atol=1e-6, | ||
) |
Oops, something went wrong.