Skip to content

Commit fb10db6

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 7faef57 commit fb10db6

2 files changed

Lines changed: 45 additions & 35 deletions

File tree

src/rapids_singlecell/pertpy_gpu/_distances_standalone.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def generate_bootstrap_indices(
131131
"""
132132
Generate bootstrap indices for all groups and all bootstrap iterations.
133133
This matches the CPU implementation's random sampling logic for reproducibility.
134-
134+
135135
Parameters
136136
----------
137137
cat_offsets : cp.ndarray
@@ -142,31 +142,31 @@ def generate_bootstrap_indices(
142142
Number of bootstrap samples
143143
random_state : int
144144
Random seed for reproducibility
145-
145+
146146
Returns
147147
-------
148148
bootstrap_indices : list[list[cp.ndarray]]
149149
For each bootstrap iteration, list of indices arrays for each group
150150
Shape: [n_bootstrap][k] where each element is cp.ndarray of group_size
151151
"""
152152
import numpy as np
153-
153+
154154
# Use same RNG logic as CPU code
155155
rng = np.random.default_rng(random_state)
156-
156+
157157
# Convert to numpy for CPU-based random generation
158158
cat_offsets_np = cat_offsets.get()
159-
159+
160160
bootstrap_indices = []
161-
161+
162162
for bootstrap_iter in range(n_bootstrap):
163163
group_indices = []
164-
164+
165165
for group_idx in range(k):
166166
start_idx = cat_offsets_np[group_idx]
167167
end_idx = cat_offsets_np[group_idx + 1]
168168
group_size = end_idx - start_idx
169-
169+
170170
if group_size > 0:
171171
# Generate bootstrap indices using same logic as CPU code
172172
# rng.choice(a=X.shape[0], size=X.shape[0], replace=True)
@@ -178,9 +178,9 @@ def generate_bootstrap_indices(
178178
else:
179179
# Empty group
180180
group_indices.append(cp.array([], dtype=cp.int32))
181-
181+
182182
bootstrap_indices.append(group_indices)
183-
183+
184184
return bootstrap_indices
185185

186186

@@ -193,7 +193,7 @@ def _bootstrap_sample_cells_from_indices(
193193
) -> tuple[cp.ndarray, cp.ndarray]:
194194
"""
195195
Bootstrap sample cells using pre-generated indices.
196-
196+
197197
Parameters
198198
----------
199199
cat_offsets : cp.ndarray
@@ -204,32 +204,32 @@ def _bootstrap_sample_cells_from_indices(
204204
Number of groups
205205
bootstrap_group_indices : list[cp.ndarray]
206206
Pre-generated bootstrap indices for each group
207-
207+
208208
Returns
209209
-------
210210
new_cat_offsets, new_cell_indices : tuple[cp.ndarray, cp.ndarray]
211211
New category structure with bootstrapped cells
212212
"""
213213
new_cell_indices = []
214214
new_cat_offsets = cp.zeros(k + 1, dtype=cp.int32)
215-
215+
216216
for group_idx in range(k):
217217
start_idx = cat_offsets[group_idx]
218218
end_idx = cat_offsets[group_idx + 1]
219219
group_size = end_idx - start_idx
220-
220+
221221
if group_size > 0:
222222
# Get original cell indices for this group
223223
group_cells = cell_indices[start_idx:end_idx]
224-
224+
225225
# Use pre-generated bootstrap indices
226226
bootstrap_indices = bootstrap_group_indices[group_idx]
227227
bootstrap_cells = group_cells[bootstrap_indices]
228-
228+
229229
new_cell_indices.extend(bootstrap_cells.get().tolist())
230-
230+
231231
new_cat_offsets[group_idx + 1] = len(new_cell_indices)
232-
232+
233233
return new_cat_offsets, cp.array(new_cell_indices, dtype=cp.int32)
234234

235235

@@ -245,7 +245,7 @@ def compute_pairwise_means_gpu_bootstrap(
245245
"""
246246
Compute bootstrap statistics for between-group distances.
247247
Uses CPU-compatible random generation for reproducibility.
248-
248+
249249
Returns:
250250
means: [k, k] matrix of bootstrap means
251251
variances: [k, k] matrix of bootstrap variances
@@ -254,9 +254,9 @@ def compute_pairwise_means_gpu_bootstrap(
254254
bootstrap_indices = generate_bootstrap_indices(
255255
cat_offsets, k, n_bootstrap, random_state
256256
)
257-
257+
258258
bootstrap_results = []
259-
259+
260260
for bootstrap_iter in range(n_bootstrap):
261261
# Use pre-generated indices for this bootstrap iteration
262262
boot_cat_offsets, boot_cell_indices = _bootstrap_sample_cells_from_indices(
@@ -265,7 +265,7 @@ def compute_pairwise_means_gpu_bootstrap(
265265
k=k,
266266
bootstrap_group_indices=bootstrap_indices[bootstrap_iter],
267267
)
268-
268+
269269
# Compute distances with bootstrapped samples
270270
pairwise_means = compute_pairwise_means_gpu(
271271
embedding=embedding,
@@ -274,12 +274,12 @@ def compute_pairwise_means_gpu_bootstrap(
274274
k=k,
275275
)
276276
bootstrap_results.append(pairwise_means.get())
277-
277+
278278
# Compute statistics across bootstrap samples
279279
bootstrap_stack = cp.array(bootstrap_results) # [n_bootstrap, k, k]
280280
means = cp.mean(bootstrap_stack, axis=0)
281281
variances = cp.var(bootstrap_stack, axis=0)
282-
282+
283283
return means, variances
284284

285285

tmp_scripts/compute_edistance_standalone.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
import os
44
import time
5+
from argparse import ArgumentParser
6+
from pathlib import Path
57

68
import anndata as ad
79
import cupy as cp
810
import numpy as np
911
import pandas as pd
1012
import rmm
1113
from rmm.allocators.cupy import rmm_cupy_allocator
12-
from argparse import ArgumentParser
13-
from pathlib import Path
14+
1415
import rapids_singlecell as rsc
1516
from rapids_singlecell.pertpy_gpu._distances_standalone import pairwise_edistance_gpu
16-
from pathlib import Path
1717

1818
rmm.reinitialize(
1919
managed_memory=False, # Allows oversubscription
@@ -37,24 +37,31 @@
3737

3838
df_expected = None
3939
if bootstrap:
40-
df_cpu_bootstrap_var = pd.read_csv(home_dir / "df_cpu_bootstrap_var.csv", index_col=0)
40+
df_cpu_bootstrap_var = pd.read_csv(
41+
home_dir / "df_cpu_bootstrap_var.csv", index_col=0
42+
)
4143
df_cpu_bootstrap = pd.read_csv(home_dir / "df_cpu_bootstrap.csv", index_col=0)
4244
df_expected = df_cpu_bootstrap
4345
else:
4446
df_cpu_float64 = pd.read_csv(home_dir / "df_cpu_float64.csv", index_col=0)
4547
df_expected = df_cpu_float64
4648

47-
4849
start_time = time.time()
4950
if not bootstrap:
50-
df_gpu = pairwise_edistance_gpu(adata, groupby=obs_key, obsm_key="X_pca", bootstrap=bootstrap)
51+
df_gpu = pairwise_edistance_gpu(
52+
adata, groupby=obs_key, obsm_key="X_pca", bootstrap=bootstrap
53+
)
5154
else:
52-
df_gpu, df_gpu_var = pairwise_edistance_gpu(adata, groupby=obs_key, obsm_key="X_pca", bootstrap=bootstrap, n_bootstrap=100)
55+
df_gpu, df_gpu_var = pairwise_edistance_gpu(
56+
adata,
57+
groupby=obs_key,
58+
obsm_key="X_pca",
59+
bootstrap=bootstrap,
60+
n_bootstrap=100,
61+
)
5362
end_time = time.time()
5463
print(f"Time taken: {end_time - start_time} seconds")
5564

56-
57-
5865
is_not_close = []
5966
groups = adata.obs[obs_key].unique()
6067
k = len(groups)
@@ -67,15 +74,18 @@
6774
assert df_gpu.loc[group_x, group_y] == 0
6875
else:
6976
if not np.isclose(
70-
df_gpu.loc[group_x, group_y], df_expected.loc[group_x, group_y], atol=atol
77+
df_gpu.loc[group_x, group_y],
78+
df_expected.loc[group_x, group_y],
79+
atol=atol,
7180
):
7281
is_not_close.append(
7382
(
7483
(group_x, group_y),
7584
df_expected.loc[group_x, group_y],
7685
df_gpu.loc[group_x, group_y],
7786
np.abs(
78-
df_expected.loc[group_x, group_y] - df_gpu.loc[group_x, group_y]
87+
df_expected.loc[group_x, group_y]
88+
- df_gpu.loc[group_x, group_y]
7989
),
8090
)
8191
)

0 commit comments

Comments
 (0)