@nahisaho/satori 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,296 @@
1
+ ---
2
+ name: scientific-gpu-singlecell
3
+ description: |
4
+ GPU アクセラレーション シングルセル解析スキル。
5
+ rapids-singlecell / cuML / cuGraph による GPU 並列処理。
6
+ 大規模 (>1M cells) データの高速前処理・クラスタリング・
7
+ 次元削減。K-Dense: rapids-singlecell。
8
+ ---
9
+
10
+ # Scientific GPU Single-Cell
11
+
12
+ rapids-singlecell / cuML / cuGraph を活用した GPU アクセラレー
13
+ ション対応シングルセル解析パイプラインを提供する。100万細胞超
14
+ の大規模データセットの高速処理。
15
+
16
+ ## When to Use
17
+
18
+ - 大規模シングルセルデータ (>100k cells) の高速前処理が必要なとき
19
+ - GPU クラスタリング (Leiden/Louvain) を実行するとき
20
+ - GPU UMAP/t-SNE で次元削減を高速化するとき
21
+ - CPU 版 scanpy では処理時間が実用的でないとき
22
+ - 複数サンプル統合に GPU を活用するとき
23
+ - ベンチマーク (CPU vs GPU) で性能比較を行うとき
24
+
25
+ ---
26
+
27
+ ## Quick Start
28
+
29
+ ## 1. rapids-singlecell 前処理
30
+
31
+ ```python
32
+ import rapids_singlecell as rsc
33
+ import scanpy as sc
34
+ import anndata as ad
35
+ import cupy as cp
36
+ import numpy as np
37
+ import pandas as pd
38
+ import time
39
+ from pathlib import Path
40
+
41
+
42
+ def gpu_preprocessing(adata, min_genes=200, min_cells=3,
43
+ n_top_genes=2000, target_sum=10000):
44
+ """
45
+ rapids-singlecell — GPU 前処理パイプライン。
46
+
47
+ Parameters:
48
+ adata: AnnData — 入力データ
49
+ min_genes: int — 最小遺伝子数
50
+ min_cells: int — 最小細胞数
51
+ n_top_genes: int — HVG 数
52
+ target_sum: float — 正規化ターゲット
53
+ """
54
+ t0 = time.time()
55
+
56
+ # GPU メモリにデータ転送
57
+ rsc.get.anndata_to_GPU(adata)
58
+
59
+ # QC
60
+ rsc.pp.calculate_qc_metrics(adata)
61
+ rsc.pp.filter_cells(adata, min_genes=min_genes)
62
+ rsc.pp.filter_genes(adata, min_cells=min_cells)
63
+
64
+ # 正規化
65
+ rsc.pp.normalize_total(adata, target_sum=target_sum)
66
+ rsc.pp.log1p(adata)
67
+
68
+ # HVG 選択
69
+ rsc.pp.highly_variable_genes(
70
+ adata,
71
+ n_top_genes=n_top_genes,
72
+ flavor="seurat_v3",
73
+ )
74
+
75
+ # スケーリング
76
+ rsc.pp.scale(adata, max_value=10)
77
+
78
+ elapsed = time.time() - t0
79
+ print(f"GPU preprocessing: {adata.n_obs} cells × {adata.n_vars} genes "
80
+ f"({elapsed:.1f}s)")
81
+ return adata
82
+ ```
83
+
84
+ ## 2. GPU PCA & 近傍グラフ
85
+
86
+ ```python
87
+ def gpu_pca_neighbors(adata, n_comps=50, n_neighbors=15):
88
+ """
89
+ GPU PCA + 近傍グラフ構築。
90
+
91
+ Parameters:
92
+ adata: AnnData — 前処理済みデータ (GPU)
93
+ n_comps: int — PCA 成分数
94
+ n_neighbors: int — kNN 近傍数
95
+ """
96
+ t0 = time.time()
97
+
98
+ # GPU PCA
99
+ rsc.pp.pca(adata, n_comps=n_comps)
100
+
101
+ # GPU kNN
102
+ rsc.pp.neighbors(adata, n_neighbors=n_neighbors, n_pcs=n_comps)
103
+
104
+ elapsed = time.time() - t0
105
+ print(f"GPU PCA + kNN: {n_comps} PCs, k={n_neighbors} ({elapsed:.1f}s)")
106
+ return adata
107
+ ```
108
+
109
+ ## 3. GPU クラスタリング (Leiden/Louvain)
110
+
111
+ ```python
112
+ def gpu_clustering(adata, method="leiden", resolution=1.0):
113
+ """
114
+ cuGraph — GPU Leiden/Louvain クラスタリング。
115
+
116
+ Parameters:
117
+ adata: AnnData — 近傍グラフ付きデータ
118
+ method: str — "leiden" or "louvain"
119
+ resolution: float — クラスタリング解像度
120
+ """
121
+ t0 = time.time()
122
+
123
+ if method == "leiden":
124
+ rsc.tl.leiden(adata, resolution=resolution)
125
+ else:
126
+ rsc.tl.louvain(adata, resolution=resolution)
127
+
128
+ n_clusters = adata.obs[method].nunique()
129
+ elapsed = time.time() - t0
130
+
131
+ print(f"GPU {method}: {n_clusters} clusters, "
132
+ f"resolution={resolution} ({elapsed:.1f}s)")
133
+ return adata
134
+ ```
135
+
136
+ ## 4. GPU UMAP / t-SNE
137
+
138
+ ```python
139
+ def gpu_embedding(adata, method="umap", n_components=2, **kwargs):
140
+ """
141
+ GPU UMAP / t-SNE 次元削減。
142
+
143
+ Parameters:
144
+ adata: AnnData — 近傍グラフ付きデータ
145
+ method: str — "umap" or "tsne"
146
+ n_components: int — 出力次元数
147
+ """
148
+ t0 = time.time()
149
+
150
+ if method == "umap":
151
+ rsc.tl.umap(adata, n_components=n_components, **kwargs)
152
+ else:
153
+ rsc.tl.tsne(adata, n_pcs=n_components, **kwargs)
154
+
155
+ elapsed = time.time() - t0
156
+ print(f"GPU {method.upper()}: {n_components}D ({elapsed:.1f}s)")
157
+ return adata
158
+ ```
159
+
160
+ ## 5. CPU vs GPU ベンチマーク
161
+
162
+ ```python
163
+ def benchmark_cpu_vs_gpu(adata_path, n_top_genes=2000, n_comps=50):
164
+ """
165
+ CPU (scanpy) vs GPU (rapids-singlecell) ベンチマーク。
166
+
167
+ Parameters:
168
+ adata_path: str — h5ad ファイルパス
169
+ n_top_genes: int — HVG 数
170
+ n_comps: int — PCA 成分数
171
+ """
172
+ results = {}
173
+
174
+ # === CPU (scanpy) ===
175
+ adata_cpu = sc.read_h5ad(adata_path)
176
+ t0 = time.time()
177
+ sc.pp.normalize_total(adata_cpu, target_sum=1e4)
178
+ sc.pp.log1p(adata_cpu)
179
+ sc.pp.highly_variable_genes(adata_cpu, n_top_genes=n_top_genes)
180
+ adata_cpu = adata_cpu[:, adata_cpu.var["highly_variable"]].copy()
181
+ sc.pp.scale(adata_cpu, max_value=10)
182
+ sc.pp.pca(adata_cpu, n_comps=n_comps)
183
+ sc.pp.neighbors(adata_cpu)
184
+ sc.tl.leiden(adata_cpu)
185
+ sc.tl.umap(adata_cpu)
186
+ cpu_time = time.time() - t0
187
+ results["cpu_seconds"] = cpu_time
188
+ results["cpu_clusters"] = adata_cpu.obs["leiden"].nunique()
189
+
190
+ # === GPU (rapids-singlecell) ===
191
+ adata_gpu = sc.read_h5ad(adata_path)
192
+ t0 = time.time()
193
+ rsc.get.anndata_to_GPU(adata_gpu)
194
+ rsc.pp.normalize_total(adata_gpu, target_sum=1e4)
195
+ rsc.pp.log1p(adata_gpu)
196
+ rsc.pp.highly_variable_genes(adata_gpu, n_top_genes=n_top_genes,
197
+ flavor="seurat_v3")
198
+ adata_gpu = adata_gpu[:, adata_gpu.var["highly_variable"]].copy()
199
+ rsc.pp.scale(adata_gpu, max_value=10)
200
+ rsc.pp.pca(adata_gpu, n_comps=n_comps)
201
+ rsc.pp.neighbors(adata_gpu)
202
+ rsc.tl.leiden(adata_gpu)
203
+ rsc.tl.umap(adata_gpu)
204
+ gpu_time = time.time() - t0
205
+ results["gpu_seconds"] = gpu_time
206
+ results["gpu_clusters"] = adata_gpu.obs["leiden"].nunique()
207
+
208
+ results["speedup"] = cpu_time / gpu_time
209
+ results["n_cells"] = adata_cpu.n_obs
210
+
211
+ print(f"Benchmark ({results['n_cells']} cells):")
212
+ print(f" CPU: {cpu_time:.1f}s ({results['cpu_clusters']} clusters)")
213
+ print(f" GPU: {gpu_time:.1f}s ({results['gpu_clusters']} clusters)")
214
+ print(f" Speedup: {results['speedup']:.1f}x")
215
+ return results
216
+ ```
217
+
218
+ ## 6. GPU シングルセル統合パイプライン
219
+
220
+ ```python
221
+ def gpu_singlecell_pipeline(input_files, output_dir="results",
222
+ n_top_genes=3000, resolution=1.0):
223
+ """
224
+ 大規模 GPU シングルセル統合パイプライン。
225
+
226
+ Parameters:
227
+ input_files: list[str] — h5ad ファイルリスト
228
+ output_dir: str — 出力ディレクトリ
229
+ n_top_genes: int — HVG 数
230
+ resolution: float — Leiden 解像度
231
+ """
232
+ output_dir = Path(output_dir)
233
+ output_dir.mkdir(parents=True, exist_ok=True)
234
+
235
+ t_total = time.time()
236
+
237
+ # 1) データ読み込み・結合
238
+ adatas = []
239
+ for i, f in enumerate(input_files):
240
+ a = sc.read_h5ad(f)
241
+ a.obs["sample"] = f"sample_{i}"
242
+ adatas.append(a)
243
+ adata = ad.concat(adatas, join="inner")
244
+ print(f"Combined: {adata.n_obs} cells from {len(input_files)} samples")
245
+
246
+ # 2) GPU 前処理
247
+ adata = gpu_preprocessing(adata, n_top_genes=n_top_genes)
248
+
249
+ # 3) GPU PCA + kNN
250
+ adata = gpu_pca_neighbors(adata)
251
+
252
+ # 4) GPU クラスタリング
253
+ adata = gpu_clustering(adata, resolution=resolution)
254
+
255
+ # 5) GPU UMAP
256
+ adata = gpu_embedding(adata)
257
+
258
+ # 6) CPU に戻して marker 検出
259
+ rsc.get.anndata_to_CPU(adata)
260
+ sc.tl.rank_genes_groups(adata, groupby="leiden", method="wilcoxon")
261
+
262
+ # 保存
263
+ adata.write(output_dir / "gpu_singlecell.h5ad")
264
+
265
+ # マーカー遺伝子エクスポート
266
+ markers = sc.get.rank_genes_groups_df(adata, group=None)
267
+ markers.to_csv(output_dir / "markers.csv", index=False)
268
+
269
+ total_time = time.time() - t_total
270
+ print(f"GPU pipeline: {adata.n_obs} cells, "
271
+ f"{adata.obs['leiden'].nunique()} clusters ({total_time:.1f}s)")
272
+ return adata
273
+ ```
274
+
275
+ ---
276
+
277
+ ## パイプライン統合
278
+
279
+ ```
280
+ single-cell-genomics → gpu-singlecell → scvi-integration
281
+ (scanpy 標準) (GPU 高速化) (深層学習統合)
282
+ │ │ ↓
283
+ batch-correction ─────────┘ cell-type-annotation
284
+ (Harmony/scVI) │ (自動アノテーション)
285
+
286
+ atlas-construction
287
+ (大規模アトラス)
288
+ ```
289
+
290
+ ## パイプライン出力
291
+
292
+ | ファイル | 説明 | 次スキル |
293
+ |---------|------|---------|
294
+ | `results/gpu_singlecell.h5ad` | GPU 処理済み AnnData | → scvi-integration |
295
+ | `results/markers.csv` | マーカー遺伝子 | → cell-type-annotation |
296
+ | `results/benchmark.json` | CPU/GPU 比較結果 | → atlas-construction |