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
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 |
|
configure_optimizers
@see pl.LightningModule
Source code in scprint/model/model.py
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 |
|
forward
forward also called on self(), a full forward pass on the model
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/model.py
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 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 |
|
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
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 |
|
on_fit_start
@see pl.LightningModule
Source code in scprint/model/model.py
773 774 775 776 777 778 779 |
|
on_predict_epoch_end
@see pl.LightningModule will
Source code in scprint/model/model.py
1564 1565 1566 1567 1568 1569 1570 |
|
on_predict_epoch_start
@see pl.LightningModule
Source code in scprint/model/model.py
1347 1348 1349 1350 1351 1352 1353 1354 1355 |
|
on_validation_epoch_end
@see pl.LightningModule
Source code in scprint/model/model.py
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 |
|
optimizer_step
@see pl.LightningModule
Source code in scprint/model/model.py
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 |
|
predict_step
embed given gene expression, encode the gene embedding and cell embedding.
Returns: |
|
---|
Source code in scprint/model/model.py
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 |
|
training_step
training_step defines the train loop. It is independent of forward
@see pl.LightningModule
Returns: |
|
---|
Source code in scprint/model/model.py
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 |
|
validation_step
validation_step defines the validation loop. It is independent of forward @see pl.LightningModule
Parameters: |
|
---|
Source code in scprint/model/model.py
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 |
|
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_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 scprint/model/loss.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 |
|
forward
Parameters: |
|
---|
Source code in scprint/model/loss.py
337 338 339 340 341 342 343 344 345 346 347 |
|
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
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 |
|
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
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 |
|
criterion_neg_log_bernoulli
Compute the negative log-likelihood of Bernoulli distribution
Source code in scprint/model/loss.py
149 150 151 152 153 154 155 156 |
|
ecs
ecs Computes the similarity of cell embeddings based on a threshold.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
grad_reverse
grad_reverse Reverses the gradient of the input tensor.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
361 362 363 364 365 366 367 368 369 370 371 372 |
|
masked_mae
Compute the masked MAE loss between input and target. MAE = mean absolute error
Source code in scprint/model/loss.py
35 36 37 38 39 40 41 42 |
|
masked_mse
Compute the masked MSE loss between input and target.
Source code in scprint/model/loss.py
26 27 28 29 30 31 32 |
|
masked_nb
Compute the masked negative binomial loss between input and target.
Source code in scprint/model/loss.py
45 46 47 48 49 50 51 52 |
|
masked_relative_error
Compute the masked relative error between input and target.
Source code in scprint/model/loss.py
159 160 161 162 163 164 165 166 167 |
|
mse
Compute the MSE loss between input and target.
Source code in scprint/model/loss.py
15 16 17 18 19 20 21 22 23 |
|
nb
Computes the negative binomial (NB) loss.
This function was adapted from scvi-tools.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
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 |
|
nb_dist
nb_dist Computes the negative binomial distribution.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
within_sample
Compute dissimilarity between embeddings within each sample using a combination of cosine and L2 distance
Source code in scprint/model/loss.py
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 |
|
zinb
Computes zero-inflated negative binomial (ZINB) loss.
This function was modified from scvi-tools.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/loss.py
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 |
|
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
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 |
|
add_attn
Aggregate the attention or data based on the comp_attn flag.
Parameters: |
|
---|
Source code in scprint/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 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
|
add_qk
Add data to the internal storage.
Parameters: |
|
---|
Source code in scprint/model/utils.py
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 |
|
get
Get the aggregated attention or data.
Returns: |
|
---|
Source code in scprint/model/utils.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
|
WeightedMasker
Randomly mask a batch of data.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
|
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
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 |
|
make_adata
This function creates an AnnData object from the given input parameters.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
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 |
|
simple_masker
Randomly mask a batch of data.
Parameters: |
|
---|
Returns: |
|
---|
Source code in scprint/model/utils.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
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
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 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 |
|
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
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 |
|
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 |
|