Documentation for the model
model description
scprint2.model.model
Classes:
| Name | Description |
|---|---|
scPRINT2 |
|
scPRINT2
Bases: LightningModule, PyTorchModelHubMixin
scPRINT-2: Single-Cell Pretrained Regulatory Inference Network Transformer.
A foundation model for single-cell biology that learns cell and gene representations through self-supervised learning on large-scale single-cell RNA-seq data. The model can be used for: - Cell type classification and annotation - Gene expression denoising and imputation - Cell embedding generation for downstream analysis - Gene regulatory network inference via attention patterns - Multi-species gene expression modeling
Architecture Overview
- Gene Encoder: Embeds gene identities (optionally with pretrained embeddings)
- Expression Encoder: Encodes expression values (continuous, binned, or metacell)
- Position Encoder: Optional genomic position encoding
- Transformer: Main attention-based encoder (various attention mechanisms)
- Cell Transformer: Optional separate transformer for cell embeddings
- Decoders: Expression reconstruction, classification, and MVC decoders
The model supports multiple training objectives
- Masked expression prediction (like BERT)
- Denoising autoencoding
- Cell embedding contrastive learning (ECS and CCE losses)
- Multi-class cell type classification with hierarchical labels
- Multi-view coding (MVC) for robust representations
| Parameters: |
|
|---|
| Attributes: |
|
|---|
Example
Initialize model
model = scPrint2( ... genes=gene_list, ... organisms=["NCBITaxon:9606"], ... d_model=512, ... nlayers=12, ... classes={"cell_type_ontology_term_id": 100}, ... )
Configure training
model.noise = [0.4, 0.6] model.mask_ratio = [0.15, 0.3]
Train with PyTorch Lightning
trainer = L.Trainer(max_epochs=100) trainer.fit(model, datamodule)
Generate embeddings
model.pred_embedding = ["cell_type_ontology_term_id"] predictions = trainer.predict(model, datamodule)
Note
The model is designed to work with scDataLoader's DataModule and Collator. Gene order must match between model initialization and data loading.
Methods:
| Name | Description |
|---|---|
add_organism |
Add a new organism to an existing model for transfer learning. |
configure_optimizers |
@see pl.LightningModule |
forward |
Complete forward pass through the scPRINT-2 model. |
log_adata |
log_adata will log an adata from predictions. |
on_fit_start |
@see pl.LightningModule |
on_load_checkpoint |
Handle checkpoint loading with backward compatibility. |
on_predict_epoch_end |
@see pl.LightningModule will |
on_predict_epoch_start |
@see pl.LightningModule |
on_test_start |
@see pl.LightningModule |
on_validation_epoch_end |
@see pl.LightningModule |
optimizer_step |
@see pl.LightningModule |
predict_step |
embed given gene expression, encode the gene embedding and cell embedding. |
training_step |
training_step defines the train loop. It is independent of forward |
validation_step |
validation_step defines the validation loop. It is independent of forward |
| Attributes: |
|
|---|
Source code in scprint2/model/model.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
genes
property
Get flattened list of all genes in the model's vocabulary.
For multi-organism models, concatenates genes from all organisms in consistent order.
| Returns: |
|
|---|
add_organism
Add a new organism to an existing model for transfer learning.
Extends the gene vocabulary and embeddings to include genes from a new organism. Useful for applying a pretrained model to a new species.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Note
Only genes present in both genes and emb (and locs if provided)
will be added. The model's gene encoder is expanded in-place.
Source code in scprint2/model/model.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 | |
configure_optimizers
@see pl.LightningModule
Source code in scprint2/model/model.py
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 | |
forward
Complete forward pass through the scPRINT-2 model.
Encodes input expression data, processes through transformer(s), and decodes into expression predictions and cell classifications.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
output = model( ... gene_pos=batch["genes"], ... expression=batch["x"], ... req_depth=batch["depth"], ... do_class=True, ... ) predictions = output["mean"] cell_types = output["cls_output_cell_type_ontology_term_id"].argmax(-1)
Source code in scprint2/model/model.py
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 | |
log_adata
log_adata will log an adata from predictions. It will log to tensorboard and wandb if available
see @utils.log_adata
Source code in scprint2/model/model.py
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 | |
on_fit_start
@see pl.LightningModule
Source code in scprint2/model/model.py
1589 1590 1591 1592 1593 1594 1595 | |
on_load_checkpoint
Handle checkpoint loading with backward compatibility.
Automatically handles: - Different class configurations between checkpoint and current model - Legacy parameter names and structures - Encoder/decoder mismatches with datamodule - Gene vocabulary differences - Early stopping callback state
Called automatically by PyTorch Lightning during checkpoint loading.
| Parameters: |
|
|---|
Note
Prints warnings when configurations differ between checkpoint and current model. These should be reviewed to ensure expected behavior.
Source code in scprint2/model/model.py
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 | |
on_predict_epoch_end
@see pl.LightningModule will
Source code in scprint2/model/model.py
2624 2625 2626 2627 2628 2629 2630 | |
on_predict_epoch_start
@see pl.LightningModule
Source code in scprint2/model/model.py
2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 | |
on_test_start
@see pl.LightningModule
Source code in scprint2/model/model.py
2314 2315 2316 2317 2318 | |
on_validation_epoch_end
@see pl.LightningModule
Source code in scprint2/model/model.py
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 | |
optimizer_step
@see pl.LightningModule
Source code in scprint2/model/model.py
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 | |
predict_step
embed given gene expression, encode the gene embedding and cell embedding.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/model.py
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 | |
training_step
training_step defines the train loop. It is independent of forward
@see pl.LightningModule
| Returns: |
|
|---|
Source code in scprint2/model/model.py
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 | |
validation_step
validation_step defines the validation loop. It is independent of forward @see pl.LightningModule
| Parameters: |
|
|---|
Source code in scprint2/model/model.py
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 | |
losses
scprint2.model.loss
Classes:
| Name | Description |
|---|---|
AdversarialDiscriminatorLoss |
|
Functions:
| Name | Description |
|---|---|
contrastive_loss |
Computes NT-Xent loss (InfoNCE) between two sets of vectors. |
criterion_neg_log_bernoulli |
Compute the negative log-likelihood of Bernoulli distribution |
ecs |
ecs Computes the similarity of cell embeddings based on a threshold. |
grad_reverse |
grad_reverse Reverses the gradient of the input tensor. |
hierarchical_classification |
Computes the classification loss for a given batch of predictions and ground truth labels. |
masked_mae |
Compute the masked MAE loss between input and target. |
masked_mse |
Compute the masked MSE loss between input and target. |
masked_nb |
Compute the masked negative binomial loss between input and target. |
masked_relative_error |
Compute the masked relative error between input and target. |
mse |
Compute the MSE loss between input and target. |
nb |
Computes the negative binomial (NB) loss. |
nb_dist |
nb_dist Computes the negative binomial distribution. |
within_sample |
Compute dissimilarity between embeddings within each sample |
zinb |
Computes zero-inflated negative binomial (ZINB) loss. |
AdversarialDiscriminatorLoss
Bases: Module
Discriminator for the adversarial training for batch correction.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint2/model/loss.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint2/model/loss.py
367 368 369 370 371 372 373 374 375 376 377 378 | |
contrastive_loss
Computes NT-Xent loss (InfoNCE) between two sets of vectors.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Note
- Assumes x[i] and y[i] are positive pairs
- All other combinations are considered negative pairs
- Uses cosine similarity scaled by temperature
Source code in scprint2/model/loss.py
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 | |
criterion_neg_log_bernoulli
Compute the negative log-likelihood of Bernoulli distribution
Source code in scprint2/model/loss.py
160 161 162 163 164 165 166 167 | |
ecs
ecs Computes the similarity of cell embeddings based on a threshold.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/loss.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
grad_reverse
grad_reverse Reverses the gradient of the input tensor.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/loss.py
392 393 394 395 396 397 398 399 400 401 402 403 | |
hierarchical_classification
Computes the classification loss for a given batch of predictions and ground truth labels.
| Parameters: |
|
|---|
| Raises: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/loss.py
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 | |
masked_mae
Compute the masked MAE loss between input and target. MAE = mean absolute error
Source code in scprint2/model/loss.py
41 42 43 44 45 46 47 48 | |
masked_mse
Compute the masked MSE loss between input and target.
Source code in scprint2/model/loss.py
15 16 17 18 19 20 21 22 23 24 25 | |
masked_nb
Compute the masked negative binomial loss between input and target.
Source code in scprint2/model/loss.py
51 52 53 54 55 56 57 58 | |
masked_relative_error
Compute the masked relative error between input and target.
Source code in scprint2/model/loss.py
170 171 172 173 174 175 176 177 178 | |
mse
Compute the MSE loss between input and target.
Source code in scprint2/model/loss.py
28 29 30 31 32 33 34 35 36 37 38 | |
nb
Computes the negative binomial (NB) loss.
This function was adapted from scvi-tools.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/loss.py
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 | |
nb_dist
nb_dist Computes the negative binomial distribution.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/loss.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
within_sample
Compute dissimilarity between embeddings within each sample using a combination of cosine and L2 distance
| Parameters: |
|
|---|
Source code in scprint2/model/loss.py
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 | |
zinb
Computes zero-inflated negative binomial (ZINB) loss.
This function was modified from scvi-tools.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/loss.py
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 | |
utils
scprint2.model.utils
Classes:
| Name | Description |
|---|---|
Attention |
|
WeightedMasker |
|
Functions:
| Name | Description |
|---|---|
downsample_profile |
This function downsamples the expression profile of a given single cell RNA matrix. |
make_adata |
This function creates an AnnData object from the given input parameters. |
simple_masker |
Randomly mask a batch of data. |
test |
Test the given model on the full set of benchmarks and save the results to JSON files. |
zinb_sample |
zinb_sample This function generates a sample from a Zero-Inflated Negative Binomial (ZINB) distribution. |
Attention
Initialize the Attention class.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
add_attn |
Aggregate the attention or data based on the precomp_attn flag. |
add_qk |
Add data to the internal storage. |
get |
Get the aggregated attention or data. |
Source code in scprint2/model/utils.py
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 | |
add_attn
Aggregate the attention or data based on the precomp_attn flag.
| Parameters: |
|
|---|
Source code in scprint2/model/utils.py
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 579 580 581 582 583 584 585 586 587 588 589 590 | |
add_qk
Add data to the internal storage.
| Parameters: |
|
|---|
Source code in scprint2/model/utils.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
get
Get the aggregated attention or data.
| Returns: |
|
|---|
Source code in scprint2/model/utils.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
WeightedMasker
Randomly mask a batch of data.
| Parameters: |
|
|---|
Source code in scprint2/model/utils.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
downsample_profile
This function downsamples the expression profile of a given single cell RNA matrix.
The noise is applied based on the renoise parameter, the total counts of the matrix, and the number of genes. The function first calculates the noise threshold (scaler) based on the renoise parameter. It then generates an initial matrix count by applying a Poisson distribution to a random tensor scaled by the total counts and the number of genes. The function then models the sampling zeros by applying a Poisson distribution to a random tensor scaled by the noise threshold, the total counts, and the number of genes. The function also models the technical zeros by generating a random tensor and comparing it to the noise threshold. The final matrix count is calculated by subtracting the sampling zeros from the initial matrix count and multiplying by the technical zeros. The function ensures that the final matrix count is not less than zero by taking the maximum of the final matrix count and a tensor of zeros. The function returns the final matrix count.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/utils.py
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 | |
make_adata
This function creates an AnnData object from the given input parameters.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/utils.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 274 275 276 277 278 279 280 281 282 283 | |
simple_masker
Randomly mask a batch of data.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/utils.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
test
Test the given model on the full set of benchmarks and save the results to JSON files.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/utils.py
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | |
zinb_sample
zinb_sample This function generates a sample from a Zero-Inflated Negative Binomial (ZINB) distribution.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/utils.py
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 | |
encoder and decoder modules
scprint2.model.encoders
Classes:
| Name | Description |
|---|---|
CategoryValueEncoder |
|
ContinuousValueEncoder |
|
DPositionalEncoding |
The PositionalEncoding module applies a positional encoding to a sequence of vectors. |
EasyExprGNN |
|
ExprBasedFT |
|
GNN |
|
GeneEncoder |
|
PositionalEncoding |
|
CategoryValueEncoder
Bases: Module
Encodes categorical values into a vector using an embedding layer and layer normalization.
| Parameters: |
|
|---|
Note: not used in the current version of scprint-2.
Source code in scprint2/model/encoders.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
ContinuousValueEncoder
Bases: Module
Encode real number values to a vector using neural nets projection.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint2/model/encoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint2/model/encoders.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
DPositionalEncoding
Bases: Module
The PositionalEncoding module applies a positional encoding to a sequence of vectors. This is necessary for the Transformer model, which does not have any inherent notion of position in a sequence. The positional encoding is added to the input embeddings and allows the model to attend to positions in the sequence.
| Parameters: |
|
|---|
Note: not used in the current version of scprint-2.
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint2/model/encoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint2/model/encoders.py
234 235 236 237 238 239 240 241 | |
EasyExprGNN
Bases: Module
Easy Expression Graph Neural Network
The main GNN used in scPRINT-2 for expression encoding. It is inspired from the DeepSets architecture to aggregate neighbor information.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Forward pass of the Easy Expression GNN |
Source code in scprint2/model/encoders.py
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 | |
forward
Forward pass of the Easy Expression GNN
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/encoders.py
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 | |
ExprBasedFT
Bases: Module
Encode real number values to a vector using neural nets projection.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint2/model/encoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint2/model/encoders.py
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 | |
GNN
Bases: Module
Graph Neural Network model
Another implementation of a GNN layer that can be used for expression encoding. Supports GCN, GAT, GraphSAGE, and DeepSets architectures.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Forward pass |
Source code in scprint2/model/encoders.py
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 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | |
forward
Forward pass
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/encoders.py
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | |
GeneEncoder
Bases: Module
Encodes gene sequences into a continuous vector space using an embedding layer. Uses memory mapping for efficient access to large embedding files.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
__del__ |
Cleanup method to ensure proper handling of memory-mapped file. |
forward |
Forward pass of the encoder. |
Source code in scprint2/model/encoders.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 | |
__del__
Cleanup method to ensure proper handling of memory-mapped file.
Source code in scprint2/model/encoders.py
116 117 118 119 120 121 122 | |
forward
Forward pass of the encoder.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/encoders.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
PositionalEncoding
Bases: Module
The PositionalEncoding module applies a positional encoding to a sequence of vectors. This is necessary for the Transformer model, which does not have any inherent notion of position in a sequence. The positional encoding is added to the input embeddings and allows the model to attend to positions in the sequence.
| Parameters: |
|
|---|
Note: not used in the current version of scprint-2.
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint2/model/encoders.py
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 | |
forward
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/encoders.py
169 170 171 172 173 174 175 176 177 178 179 | |
scprint2.model.decoders
Classes:
| Name | Description |
|---|---|
ClsDecoder |
|
ExprDecoder |
|
GraphSDEExprDecoder |
|
MVCDecoder |
|
VAEDecoder |
|
ClsDecoder
Bases: Module
ClsDecoder Decoder for classification task.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint2/model/decoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint2/model/decoders.py
259 260 261 262 263 264 265 | |
ExprDecoder
Bases: Module
ExprDecoder Decoder for the gene expression prediction.
Will output the mean, variance and zero logits, parameters of a zero inflated negative binomial distribution.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
x is the output of the transformer, (batch, seq_len, d_model) |
Source code in scprint2/model/decoders.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
forward
x is the output of the transformer, (batch, seq_len, d_model)
Source code in scprint2/model/decoders.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
GraphSDEExprDecoder
Bases: Module
Initialize the ExprNeuralSDEDecoder module.
| Parameters: |
|
|---|
Source code in scprint2/model/decoders.py
9 10 11 12 13 14 15 16 17 18 19 20 21 | |
MVCDecoder
Bases: Module
MVCDecoder Decoder for masked value prediction of cell embeddings.
Uses gene embeddings with cell embeddings to predict mean, variance, and zero logits parameters of a zero-inflated negative binomial distribution.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint2/model/decoders.py
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 | |
forward
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/decoders.py
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 | |
VAEDecoder
Bases: Module
VAEDecoder for variational autoencoding of cell embeddings.
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Forward pass through VAE. |
kl_divergence |
Compute KL divergence between N(mu, var) and N(0, 1). |
reparameterize |
Reparameterization trick to sample from N(mu, var) from N(0,1). |
Source code in scprint2/model/decoders.py
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 | |
forward
Forward pass through VAE.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/decoders.py
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 | |
kl_divergence
Compute KL divergence between N(mu, var) and N(0, 1).
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/decoders.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
reparameterize
Reparameterization trick to sample from N(mu, var) from N(0,1).
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint2/model/decoders.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
scprint2.model.fsq
Finite Scalar Quantization: VQ-VAE Made Simple - https://arxiv.org/abs/2309.15505 Code adapted from Jax version in Appendix A.1
Classes:
| Name | Description |
|---|---|
FSQ |
|
Functions:
| Name | Description |
|---|---|
round_ste |
Round with straight through gradients. |
FSQ
Bases: Module
Methods:
| Name | Description |
|---|---|
bound |
Bound |
codes_to_indices |
Converts a |
forward |
einstein notation |
indices_to_codes |
Inverse of |
quantize |
Quantizes z, returns quantized zhat, same shape as z. |
Source code in scprint2/model/fsq.py
49 50 51 52 53 54 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 | |
bound
Bound z, an array of shape (..., d).
Source code in scprint2/model/fsq.py
99 100 101 102 103 104 | |
codes_to_indices
Converts a code to an index in the codebook.
Source code in scprint2/model/fsq.py
120 121 122 123 124 | |
forward
einstein notation b - batch n - sequence (or flattened spatial dimensions) d - feature dimension, which is also log2(codebook size) c - number of codebook dim
Source code in scprint2/model/fsq.py
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 | |
indices_to_codes
Inverse of codes_to_indices.
Source code in scprint2/model/fsq.py
126 127 128 129 130 131 132 133 134 135 136 137 | |
quantize
Quantizes z, returns quantized zhat, same shape as z.
Source code in scprint2/model/fsq.py
106 107 108 109 110 | |
round_ste
Round with straight through gradients.
Source code in scprint2/model/fsq.py
39 40 41 42 | |