SCRNA API References¶
Trainer¶
protoplast.scrna.anndata.trainer.RayTrainRunner
¶
A class to initialize the training this class automatically initializes Ray cluster or
detect whether an existing cluster exist if there is an existing cluster it will automatically
connect to it refer to ray.init() behavior
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Model
|
type[LightningModule]
|
PyTorch Lightning model class |
required |
Ds
|
type[DistributedAnnDataset]
|
DistributedAnnDataset class |
required |
model_keys
|
list[str]
|
Keys to pass to model from |
required |
metadata_cb
|
Callable[[AnnData, dict], None]
|
Callback to mutate metadata recommended for passing data from |
cell_line_metadata_cb
|
before_dense_cb
|
Callable[[Tensor, str | int], Tensor]
|
Callback to perform before densification of sparse matrix where the data at this point is still a sparse CSR Tensor, by default None |
None
|
after_dense_cb
|
Callable[[Tensor, str | int], Tensor]
|
Callback to perform after densification of sparse matrix where the data at this point is a dense Tensor, by default None |
None
|
shuffle_strategy
|
ShuffleStrategy
|
Strategy to split or randomize the data during the training, by default SequentialShuffleStrategy |
SequentialShuffleStrategy
|
runtime_env_config
|
dict | None
|
These env config is to pass the RayTrainer processes, by default None |
None
|
address
|
str | None
|
Override ray address, by default None |
None
|
ray_trainer_strategy
|
Strategy | None
|
Override Ray Trainer Strategy if this is None it will default to RayDDP, by default None |
None
|
sparse_key
|
str
|
description, by default "X", |
'X'
|
Returns:
| Type | Description |
|---|---|
RayTrainRunner
|
Use this class to start the training |
Source code in src/protoplast/scrna/anndata/trainer.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
inference(file_paths: list[str], result_storage_path: str, ckpt_path: str, prediction_format: Literal['csv', 'parquet'] = 'csv', enable_progress_bar: bool = True, batch_size=2000)
¶
Start inference in a single process order is guarantee to be the same as input file don't use this in a distributed cluster
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str]
|
List of h5ad AnnData files |
required |
result_storage_path
|
str
|
Path to store the prediction result |
required |
ckpt_path
|
str
|
Path of the checkpoint to run inference |
required |
enable_progress_bar
|
bool
|
Whether to enable Trainer progress bar or not, by default True |
True
|
batch_size
|
int
|
How much data to fetch from disk, by default to 2000 |
2000
|
Source code in src/protoplast/scrna/anndata/trainer.py
par_inference(file_paths: list[str], ckpt_path: str | None = None, result_storage_path: str = '~/protoplast_results', batch_size: int = 2000, prefetch_factor: int = 4, thread_per_worker: int | None = None, num_workers: int | None = None, is_gpu: bool = True, resource_per_worker: dict | None = None, enable_progress_bar: bool = True, prediction_format: Literal['csv', 'parquet'] = 'csv', **kwargs)
¶
Start parallel inference the order of the result is not guaranteed to be the same as input file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str]
|
List of h5ad AnnData files |
required |
batch_size
|
int
|
How much data to fetch from disk, by default to 2000 |
2000
|
prefetch_factor
|
int
|
Total data fetch is prefetch_factor * batch_size, by default 4 |
4
|
thread_per_worker
|
int | None
|
Amount of worker for each dataloader, by default None |
None
|
num_workers
|
int | None
|
Override number of Ray processes default to number of GPU(s) in the cluster, by default None |
None
|
is_gpu
|
bool
|
By default True turn this off if your system don't have any GPU, by default True |
True
|
resource_per_worker
|
dict | None
|
This get pass to Ray you can specify how much CPU or GPU each Ray process get, by default None |
None
|
ckpt_path
|
str | None
|
Path of the checkpoint if this is specified it will train from checkpoint otherwise it will start the training from scratch, by default None |
None
|
enable_progress_bar
|
bool
|
Whether to enable Trainer progress bar or not, by default True |
True
|
Returns:
| Type | Description |
|---|---|
Result
|
The inference result from RayTrainer |
Source code in src/protoplast/scrna/anndata/trainer.py
train(file_paths: list[str], batch_size: int = 2000, test_size: float = 0.0, val_size: float = 0.2, prefetch_factor: int = 4, max_epochs: int = 1, thread_per_worker: int | None = None, num_workers: int | None = None, result_storage_path: str = '~/protoplast_results', ckpt_path: str | None = None, is_gpu: bool = True, random_seed: int | None = 42, resource_per_worker: dict | None = None, is_shuffled: bool = False, enable_progress_bar: bool = True, **kwargs)
¶
Start the training
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str]
|
List of h5ad AnnData files |
required |
batch_size
|
int
|
How much data to fetch from disk, by default to 2000 |
2000
|
test_size
|
float
|
Fraction of test data for example 0.1 means 10% will be split for testing default to 0.0 |
0.0
|
val_size
|
float
|
Fraction of validation data for example 0.2 means 20% will be split for validation, default to 0.2 |
0.2
|
prefetch_factor
|
int
|
Total data fetch is prefetch_factor * batch_size, by default 4 |
4
|
max_epochs
|
int
|
How many epoch(s) to train with, by default 1 |
1
|
thread_per_worker
|
int | None
|
Amount of worker for each dataloader, by default None |
None
|
num_workers
|
int | None
|
Override number of Ray processes default to number of GPU(s) in the cluster, by default None |
None
|
result_storage_path
|
str
|
Path to store the loss, validation and checkpoint, by default "~/protoplast_results" |
'~/protoplast_results'
|
ckpt_path
|
str | None
|
Path of the checkpoint if this is specified it will train from checkpoint otherwise it will start the training from scratch, by default None |
None
|
is_gpu
|
bool
|
By default True turn this off if your system don't have any GPU, by default True |
True
|
random_seed
|
int | None
|
Set this to None for real training but for benchmarking and result replication you can adjust the seed here, by default 42 |
42
|
resource_per_worker
|
dict | None
|
This get pass to Ray you can specify how much CPU or GPU each Ray process get, by default None |
None
|
enable_progress_bar
|
bool
|
Whether to enable Trainer progress bar or not, by default True |
True
|
Returns:
| Type | Description |
|---|---|
Result
|
The training result from RayTrainer |
Source code in src/protoplast/scrna/anndata/trainer.py
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
DataModule¶
Wrapper around Dataset on how the data should be forward to the Lightning Model support hooks at various Lifecycle when the data get pass to the model
protoplast.scrna.anndata.torch_dataloader.AnnDataModule
¶
Bases: LightningDataModule
Source code in src/protoplast/scrna/anndata/torch_dataloader.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
Dataset¶
For fetching and sending data to the model
protoplast.scrna.anndata.torch_dataloader.DistributedAnnDataset
¶
Bases: IterableDataset
Dataset that support multiworker distribution this version will yield the data in a sequential manner
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str]
|
List of files |
required |
indices
|
list[list[int]]
|
List of indices from |
required |
metadata
|
dict
|
Metadata dictionary for sending data to the model or other logical purposes |
required |
sparse_key
|
str
|
AnnData key for the sparse matrix usually it is "X" if "layers" please use the dot notation for example "layers.attr" where attr is the key in the layer you want to refer to |
required |
mini_batch_size
|
int
|
How many observation to send to the model must be less than |
None
|
Source code in src/protoplast/scrna/anndata/torch_dataloader.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
transform(start: int, end: int)
¶
The subclass should implement the logic to get more data for the cell. It can leverage this super function
to efficiently get X as a sparse tensor. An example of how to get to more data from the cell is
self.ad.obs["key"][start:end] where you must only fetch a subset of this data with start and end
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
int
|
Starting index of this batch |
required |
end
|
int
|
Ending index of this batch |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Usually a tensor, a list of tensor or dictionary with tensor value |
Source code in src/protoplast/scrna/anndata/torch_dataloader.py
protoplast.scrna.anndata.strategy.ShuffleStrategy
¶
Bases: ABC
Strategy on how to data should be split and shuffle during the training
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str]
|
List of file paths |
required |
batch_size
|
int
|
How much data to fetch |
required |
total_workers
|
int
|
Total workers this is equal to number of processes times number of threads per process |
required |
test_size
|
float | None
|
Fraction of test data for example 0.1 means 10% will be split for testing, by default None |
None
|
validation_size
|
float | None
|
Fraction of validation data for example 0.2 means 20% will be split for validation, by default None |
None
|
random_seed
|
int | None
|
Seed to randomize the split set this to None if you want this to be completely random, by default 42 |
42
|
metadata_cb
|
Callable[[AnnData, dict], None] | None
|
Callback to mutate metadata recommended for passing data from |
None
|
is_shuffled
|
bool
|
Whether to shuffle the data or not this will be deprecated soon, by default True |
True
|
Source code in src/protoplast/scrna/anndata/strategy.py
protoplast.scrna.anndata.strategy.SplitInfo
dataclass
¶
Source code in src/protoplast/scrna/anndata/strategy.py
mini_batch_size: int | None = None
class-attribute
instance-attribute
¶
Information on how to split the data this will get pass to the Dataset to know which part of the data they need to access
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
list[str]
|
List of files |
required |
train_indices
|
list[list[str]]
|
List of indices for training |
required |
val_indices
|
list[list[str]]
|
List of indices for validation |
required |
test_indices
|
list[list[str]]
|
List of indices for testing |
required |
metadata
|
dict[str, any]
|
Data to pass on to the Dataset and model |
required |
mini_batch_size
|
int | None
|
How much data to send to the model |
required |
protoplast.scrna.anndata.strategy.SequentialShuffleStrategy
¶
Bases: ShuffleStrategy
Return the data in a sequential way randomness is not guarantee there is a high chance the data will come from nearby rows this might affect your training accuracy depending on how the anndata are ordered you can overcome this by preshuffling the data manually yourself if this is an issue
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str]
|
List of file paths |
required |
batch_size
|
int
|
How much data to fetch |
required |
total_workers
|
int
|
Total workers this is equal to number of processes times number of threads per process |
required |
test_size
|
float | None
|
Fraction of test data for example 0.1 means 10% will be split for testing, by default None |
None
|
validation_size
|
float | None
|
Fraction of validation data for example 0.2 means 20% will be split for validation, by default None |
None
|
random_seed
|
int | None
|
Seed to randomize the split set this to None if you want this to be completely random, by default 42 |
42
|
metadata_cb
|
Callable[[AnnData, dict], None] | None
|
Callback to mutate metadata recommended for passing data from |
None
|
is_shuffled
|
bool
|
Whether to shuffle the data or not this will be deprecated soon, by default True |
False
|
pre_fetch_then_batch
|
int | None
|
The prefetch factor the total size of data fetch will be equal to |
16
|
drop_last
|
bool
|
If there is true drop the remainder, default to True otherwise duplicate the data to make sure the data is evenly distributed to all the workers |
True
|
Source code in src/protoplast/scrna/anndata/strategy.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |