Documentation for the model
model description
scprint.model.model
Classes:
| Name | Description |
|---|---|
scPrint |
|
scPrint
Bases: LightningModule, PyTorchModelHubMixin
scPRINT transformer for single cell biology and the inference of Gene Regulatory networks
| Parameters: |
|
|---|
Notes
for other parameters of the model that are not part of its class definition, see @trainer.trainer.py
| Raises: |
|
|---|
Methods:
| Name | Description |
|---|---|
configure_optimizers |
@see pl.LightningModule |
forward |
forward also called on self(), a full forward pass on the model |
log_adata |
log_adata will log an adata from predictions. |
on_fit_start |
@see pl.LightningModule |
on_predict_epoch_end |
@see pl.LightningModule will |
on_predict_epoch_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 |
Source code in scprint/model/model.py
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 | |
configure_optimizers
@see pl.LightningModule
Source code in scprint/model/model.py
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 | |
forward
forward also called on self(), a full forward pass on the model
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/model.py
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 | |
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 scprint/model/model.py
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 | |
on_fit_start
@see pl.LightningModule
Source code in scprint/model/model.py
849 850 851 852 853 854 855 | |
on_predict_epoch_end
@see pl.LightningModule will
Source code in scprint/model/model.py
1641 1642 1643 1644 1645 1646 1647 | |
on_predict_epoch_start
@see pl.LightningModule
Source code in scprint/model/model.py
1423 1424 1425 1426 1427 1428 1429 1430 1431 | |
on_validation_epoch_end
@see pl.LightningModule
Source code in scprint/model/model.py
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 | |
optimizer_step
@see pl.LightningModule
Source code in scprint/model/model.py
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 | |
predict_step
embed given gene expression, encode the gene embedding and cell embedding.
| Returns: |
|
|---|
Source code in scprint/model/model.py
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 | |
training_step
training_step defines the train loop. It is independent of forward
@see pl.LightningModule
| Returns: |
|
|---|
Source code in scprint/model/model.py
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 | |
validation_step
validation_step defines the validation loop. It is independent of forward @see pl.LightningModule
| Parameters: |
|
|---|
Source code in scprint/model/model.py
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 | |
losses
scprint.model.loss
Classes:
| Name | Description |
|---|---|
AdversarialDiscriminatorLoss |
|
Functions:
| Name | Description |
|---|---|
classification |
Computes the classification loss for a given batch of predictions and ground truth labels. |
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. |
masked_mae |
Compute the masked MAE loss between input and target. |
masked_mse |
Compute the masked MSE 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. |
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 scprint/model/loss.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint/model/loss.py
278 279 280 281 282 283 284 285 286 287 288 | |
classification
Computes the classification loss for a given batch of predictions and ground truth labels.
| Parameters: |
|
|---|
| Raises: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/loss.py
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 | |
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 scprint/model/loss.py
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 | |
criterion_neg_log_bernoulli
Compute the negative log-likelihood of Bernoulli distribution
Source code in scprint/model/loss.py
90 91 92 93 94 95 96 97 | |
ecs
ecs Computes the similarity of cell embeddings based on a threshold.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/loss.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
grad_reverse
grad_reverse Reverses the gradient of the input tensor.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/loss.py
302 303 304 305 306 307 308 309 310 311 312 313 | |
masked_mae
Compute the masked MAE loss between input and target. MAE = mean absolute error
Source code in scprint/model/loss.py
34 35 36 37 38 39 40 41 | |
masked_mse
Compute the masked MSE loss between input and target.
Source code in scprint/model/loss.py
25 26 27 28 29 30 31 | |
masked_relative_error
Compute the masked relative error between input and target.
Source code in scprint/model/loss.py
100 101 102 103 104 105 106 107 108 | |
mse
Compute the MSE loss between input and target.
Source code in scprint/model/loss.py
14 15 16 17 18 19 20 21 22 | |
within_sample
Compute dissimilarity between embeddings within each sample using a combination of cosine and L2 distance
Source code in scprint/model/loss.py
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 | |
zinb
Computes zero-inflated negative binomial (ZINB) loss.
This function was modified from scvi-tools.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/loss.py
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 | |
utils
scprint.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 comp_attn flag. |
add_qk |
Add data to the internal storage. |
get |
Get the aggregated attention or data. |
Source code in scprint/model/utils.py
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 | |
add_attn
Aggregate the attention or data based on the comp_attn flag.
| Parameters: |
|
|---|
Source code in scprint/model/utils.py
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 | |
add_qk
Add data to the internal storage.
| Parameters: |
|
|---|
Source code in scprint/model/utils.py
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 | |
get
Get the aggregated attention or data.
| Returns: |
|
|---|
Source code in scprint/model/utils.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | |
WeightedMasker
Randomly mask a batch of data.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/utils.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | |
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 scprint/model/utils.py
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 | |
make_adata
This function creates an AnnData object from the given input parameters.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/utils.py
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 | |
simple_masker
Randomly mask a batch of data.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/utils.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
test
Test the given model on the full set of benchmarks and save the results to JSON files.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/utils.py
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 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 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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 | |
zinb_sample
zinb_sample This function generates a sample from a Zero-Inflated Negative Binomial (ZINB) distribution.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scprint/model/utils.py
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 | |
encoder and decoder modules
scprint.model.encoders
Classes:
| Name | Description |
|---|---|
CategoryValueEncoder |
|
ContinuousValueEncoder |
|
DPositionalEncoding |
The PositionalEncoding module applies a positional encoding to a sequence of vectors. |
GeneEncoder |
|
PositionalEncoding |
|
CategoryValueEncoder
Bases: Module
Encodes categorical values into a vector using an embedding layer and layer normalization.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Note: not used in the current version of scprint.
Source code in scprint/model/encoders.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
ContinuousValueEncoder
Bases: Module
Encode real number values to a vector using neural nets projection.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint/model/encoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint/model/encoders.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
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.
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint/model/encoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint/model/encoders.py
151 152 153 154 155 156 157 158 | |
GeneEncoder
Bases: Module
Encodes gene sequences into a continuous vector space using an embedding layer.
The output is then normalized using a LayerNorm.
| Parameters: |
|
|---|
Note: not used in the current version of scprint.
Source code in scprint/model/encoders.py
10 11 12 13 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 | |
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.
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint/model/encoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint/model/encoders.py
91 92 93 94 95 96 97 98 | |
scprint.model.decoders
Classes:
| Name | Description |
|---|---|
ClsDecoder |
|
ExprDecoder |
|
GraphSDEExprDecoder |
|
MVCDecoder |
|
ClsDecoder
Bases: Module
ClsDecoder Decoder for classification task.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint/model/decoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint/model/decoders.py
235 236 237 238 239 240 241 | |
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 scprint/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 scprint/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 88 | |
GraphSDEExprDecoder
Bases: Module
Initialize the ExprNeuralSDEDecoder module.
| Parameters: |
|
|---|
Source code in scprint/model/decoders.py
9 10 11 12 13 14 15 16 17 18 19 20 21 | |
MVCDecoder
Bases: Module
MVCDecoder Decoder for the masked value prediction for cell embeddings.
Will use the gene embeddings with the cell embeddings to predict the mean, variance and zero logits
| Parameters: |
|
|---|
Methods:
| Name | Description |
|---|---|
forward |
Args: |
Source code in scprint/model/decoders.py
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 | |
forward
| Parameters: |
|
|---|
Source code in scprint/model/decoders.py
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 | |