Skip to content

Model

Additive

chromatinhd.models.pred.model.multiscale.Model

Bases: FlowModel

Predicting region expression from raw fragments using an additive model across fragments from the same cell

Parameters:

Name Type Description Default
dummy

whether to use a dummy model that just counts fragments.

required
n_frequencies

the number of frequencies to use for sine encoding

required
reduce

the reduction to use for pooling fragments across regions and cells

required
nonlinear

whether to use a non-linear activation function

required
n_embedding_dimensions

the number of embedding dimensions

required
dropout_rate

the dropout rate

required
Source code in src/chromatinhd/models/pred/model/better.py
 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
 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
 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
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
class Model(FlowModel):
    """
    Predicting region expression from raw fragments using an additive model across fragments from the same cell

    Parameters:
        dummy:
            whether to use a dummy model that just counts fragments.
        n_frequencies:
            the number of frequencies to use for sine encoding
        reduce:
            the reduction to use for pooling fragments across regions and cells
        nonlinear:
            whether to use a non-linear activation function
        n_embedding_dimensions:
            the number of embedding dimensions
        dropout_rate:
            the dropout rate
    """

    transcriptome = Linked()
    """The transcriptome"""

    fragments = Linked()
    """The fragments"""

    fold = Stored()
    """The cells used for training, test and validation"""

    layer = Stored()
    """The layer of the transcriptome"""

    region_oi = Stored()
    """The region of interest"""

    @classmethod
    def create(
        cls,
        fragments: Fragments,
        transcriptome: Transcriptome,
        fold=None,
        layer: str | None = None,
        path: str | os.PathLike = None,
        dummy: bool = False,
        n_frequencies: int = (1000, 500, 250, 125, 63, 31),
        reduce: str = "sum",
        nonlinear: bool = "silu",
        n_embedding_dimensions: int = 100,
        embedding_to_expression_initialization: str = "default",
        dropout_rate_fragment_embedder: float = 0.0,
        n_layers_fragment_embedder=1,
        residual_fragment_embedder=True,
        batchnorm_fragment_embedder=False,
        layernorm_fragment_embedder=False,
        n_layers_embedding2expression=5,
        dropout_rate_embedding2expression: float = 0.0,
        residual_embedding2expression=True,
        batchnorm_embedding2expression=False,
        layernorm_embedding2expression=True,
        overwrite=False,
        encoder=None,
        pooler=None,
        distance_encoder="direct",
        library_size_encoder="linear",
        library_size_encoder_kwargs=None,
        region_oi=None,
        encoder_kwargs=None,
        fragment_embedder_kwargs=None,
        **kwargs: Any,
    ) -> None:
        """
        Create the model

        Parameters:
            fragments:
                the fragments
            transcriptome:
                the transcriptome
            fold:
                the fold
            layer:
                which layer from the transcriptome to use for training and inference, will use the first layer if None
            path:
                the path to save the model
            dummy:
                whether to use a dummy model that just counts fragments.
            n_frequencies:
                the number of frequencies to use for the encoding
            reduce:
                the reduction to use for pooling fragments across regions and cells
            nonlinear:
                whether to use a non-linear activation function
            n_embedding_dimensions:
                the number of embedding dimensions
            dropout_rate:
                the dropout rate
        """
        self = super(Model, cls).create(
            path=path, fragments=fragments, transcriptome=transcriptome, fold=fold, reset=overwrite
        )

        self.fold = fold

        if layer is not None:
            self.layer = layer
        else:
            layer = list(self.transcriptome.layers.keys())[0]
        self.layer = layer

        if region_oi is None:
            region_oi = fragments.var.index[0]
        self.region_oi = region_oi

        if fragment_embedder_kwargs is None:
            fragment_embedder_kwargs = {}

        if dummy is True:
            self.fragment_embedder = FragmentEmbedderCounter()
        else:
            self.fragment_embedder = FragmentEmbedder(
                n_frequencies=n_frequencies,
                nonlinear=nonlinear,
                n_layers=n_layers_fragment_embedder,
                n_embedding_dimensions=n_embedding_dimensions,
                dropout_rate=dropout_rate_fragment_embedder,
                residual=residual_fragment_embedder,
                batchnorm=batchnorm_fragment_embedder,
                layernorm=layernorm_fragment_embedder,
                fragments=self.fragments,
                encoder=encoder,
                distance_encoder=distance_encoder,
                encoder_kwargs=encoder_kwargs,
                **fragment_embedder_kwargs,
            )
        self.embedding_region_pooler = EmbeddingGenePooler(
            self.fragment_embedder.n_embedding_dimensions, reduce=reduce, pooler=pooler
        )

        n_input_embedding_dimensions = self.fragment_embedder.n_embedding_dimensions

        # library size encoder
        if library_size_encoder == "linear":
            library_size_encoder_kwargs = library_size_encoder_kwargs or {}
            self.library_size_encoder = LibrarySizeEncoder(fragments, **library_size_encoder_kwargs)
            n_input_embedding_dimensions += self.library_size_encoder.n_embedding_dimensions
        elif library_size_encoder is None:
            self.library_size_encoder = None
        else:
            raise ValueError(library_size_encoder + " is not a valid library size encoder")

        # embedding to expression
        self.embedding_to_expression = EmbeddingToExpression(
            fragments=fragments,
            n_input_embedding_dimensions=n_input_embedding_dimensions,
            n_embedding_dimensions=self.fragment_embedder.n_embedding_dimensions,
            initialization=embedding_to_expression_initialization,
            n_layers=n_layers_embedding2expression,
            residual=residual_embedding2expression,
            dropout_rate=dropout_rate_embedding2expression,
            batchnorm=batchnorm_embedding2expression,
            layernorm=layernorm_embedding2expression,
            nonlinear=nonlinear,
        )

        return self

    def forward(self, data):
        """
        Make a prediction given a data object
        """
        assert data.minibatch.n_regions == 1

        fragment_embedding = self.fragment_embedder(data)
        cell_region_embedding = self.embedding_region_pooler(
            fragment_embedding,
            data.fragments.local_cellxregion_ix,
            data.minibatch.n_cells,
            data.minibatch.n_regions,
        )
        if hasattr(self, "library_size_encoder") and (self.library_size_encoder is not None):
            library_size_encoding = self.library_size_encoder(data).unsqueeze(-2)
            cell_region_embedding = torch.cat([cell_region_embedding, library_size_encoding], dim=-1)
        expression_predicted = self.embedding_to_expression(cell_region_embedding)
        self.expression_predicted = expression_predicted

        return expression_predicted

    def forward_loss(self, data):
        """
        Make a prediction and calculate the loss
        """
        expression_predicted = self.forward(data)
        expression_true = data.transcriptome.value
        return paircor_loss(expression_predicted, expression_true)
        # return pairzmse_loss(expression_predicted, expression_true)

    def forward_region_loss(self, data):
        """
        Make a prediction and calculate the loss on a per region basis
        """
        expression_predicted = self.forward(data)
        expression_true = data.transcriptome.value
        return region_paircor_loss(expression_predicted, expression_true)

    def forward_multiple(self, data, fragments_oi, min_fragments=1):
        """
        Make multiple predictions based on different sets of fragments

        Parameters:
            data:
                the data object
            fragments_oi:
                an iterator of boolean arrays indicating which fragments to use
            min_fragments:
                the minimum number of fragments that have to remove before re-calculating the prediction
        """
        fragment_embedding = self.fragment_embedder(data)

        total_n_fragments = torch.bincount(
            data.fragments.local_cellxregion_ix,
            minlength=data.minibatch.n_regions * data.minibatch.n_cells,
        ).reshape((data.minibatch.n_cells, data.minibatch.n_regions))

        total_cell_region_embedding = self.embedding_region_pooler.forward(
            fragment_embedding,
            data.fragments.local_cellxregion_ix,
            data.minibatch.n_cells,
            data.minibatch.n_regions,
        )
        cell_region_embedding = total_cell_region_embedding

        if hasattr(self, "library_size_encoder"):
            cell_region_embedding = torch.cat(
                [cell_region_embedding, self.library_size_encoder(data).unsqueeze(-2)],
                dim=-1,
            )

        total_expression_predicted = self.embedding_to_expression.forward(cell_region_embedding)

        for fragments_oi_ in fragments_oi:
            if (fragments_oi_ is not None) and ((~fragments_oi_).sum().item() > min_fragments):
                lost_fragments_oi = ~fragments_oi_
                lost_local_cellxregion_ix = data.fragments.local_cellxregion_ix[lost_fragments_oi]
                n_fragments = total_n_fragments - torch.bincount(
                    lost_local_cellxregion_ix,
                    minlength=data.minibatch.n_regions * data.minibatch.n_cells,
                ).reshape((data.minibatch.n_cells, data.minibatch.n_regions))

                cell_region_embedding = total_cell_region_embedding - self.embedding_region_pooler.forward(
                    fragment_embedding[lost_fragments_oi],
                    lost_local_cellxregion_ix,
                    data.minibatch.n_cells,
                    data.minibatch.n_regions,
                )

                if hasattr(self, "library_size_encoder"):
                    cell_region_embedding = torch.cat(
                        [
                            cell_region_embedding,
                            self.library_size_encoder(data).unsqueeze(-2),
                        ],
                        dim=-1,
                    )

                expression_predicted = self.embedding_to_expression.forward(cell_region_embedding)
            else:
                n_fragments = total_n_fragments
                expression_predicted = total_expression_predicted

            yield expression_predicted, n_fragments

    def forward_multiple2(self, data, fragments_oi, min_fragments=1):
        fragment_embedding = self.fragment_embedder(data)

        total_n_fragments = torch.bincount(
            data.fragments.local_cellxregion_ix,
            minlength=data.minibatch.n_regions * data.minibatch.n_cells,
        ).reshape((data.minibatch.n_cells, data.minibatch.n_regions))

        total_cell_region_embedding = self.embedding_region_pooler.forward(
            fragment_embedding,
            data.fragments.local_cellxregion_ix,
            data.minibatch.n_cells,
            data.minibatch.n_regions,
        )
        cell_region_embedding = total_cell_region_embedding

        if hasattr(self, "library_size_encoder"):
            cell_region_embedding = torch.cat(
                [cell_region_embedding, self.library_size_encoder(data).unsqueeze(-2)],
                dim=-1,
            )

        # total_expression_predicted = self.embedding_to_expression.forward(
        #     cell_region_embedding
        # )

        tot = 0.0

        cell_region_embeddings = []

        start = time.time()

        for fragments_oi_ in fragments_oi:
            if (fragments_oi_ is not None) and ((~fragments_oi_).sum().item() > min_fragments):
                lost_fragments_oi = ~fragments_oi_
                lost_local_cellxregion_ix = data.fragments.local_cellxregion_ix[lost_fragments_oi]
                n_fragments = total_n_fragments - torch.bincount(
                    lost_local_cellxregion_ix,
                    minlength=data.minibatch.n_regions * data.minibatch.n_cells,
                ).reshape((data.minibatch.n_cells, data.minibatch.n_regions))

                cell_region_embedding = total_cell_region_embedding - self.embedding_region_pooler.forward(
                    fragment_embedding[lost_fragments_oi],
                    lost_local_cellxregion_ix,
                    data.minibatch.n_cells,
                    data.minibatch.n_regions,
                )

                if hasattr(self, "library_size_encoder"):
                    cell_region_embedding = torch.cat(
                        [
                            cell_region_embedding,
                            self.library_size_encoder(data).unsqueeze(-2),
                        ],
                        dim=-1,
                    )

                cell_region_embeddings.append(cell_region_embedding)

                # expression_predicted = self.embedding_to_expression.forward(cell_region_embedding)
                # end = time.time()
                # tot += end - start

            else:
                cell_region_embedding = total_cell_region_embedding
                if hasattr(self, "library_size_encoder"):
                    cell_region_embedding = torch.cat(
                        [
                            cell_region_embedding,
                            self.library_size_encoder(data).unsqueeze(-2),
                        ],
                        dim=-1,
                    )
                cell_region_embeddings.append(cell_region_embedding)
                # n_fragments = total_n_fragments
                # expression_predicted = total_expression_predicted
        cell_region_embeddings = torch.stack(cell_region_embeddings, dim=0)
        expression_predicted = self.embedding_to_expression.forward(cell_region_embeddings)

        for expression_predicted_, n_fragments in zip(expression_predicted, total_n_fragments):
            yield expression_predicted_, n_fragments

        tot = time.time() - start
        print(tot)

    def train_model(
        self,
        fold: list = None,
        fragments: Fragments = None,
        transcriptome: Transcriptome = None,
        device=None,
        lr=1e-4,
        n_epochs=1000,
        pbar=True,
        n_regions_step=1,
        n_cells_step=20000,
        weight_decay=1e-1,
        checkpoint_every_epoch=1,
        optimizer="adam",
        n_cells_train=None,
        **kwargs,
    ):
        """
        Train the model
        """
        if fold is None:
            fold = self.fold
        assert fold is not None

        if fragments is None:
            fragments = self.fragments
        if transcriptome is None:
            transcriptome = self.transcriptome

        # set up minibatchers and loaders
        if self.region_oi is not None:
            fragments.var["ix"] = np.arange(len(fragments.var))
            region_ixs = fragments.var["ix"].loc[[self.region_oi]].values
        else:
            region_ixs = range(fragments.n_regions)

        if n_cells_train is not None:
            cells_train = fold["cells_train"][:n_cells_train]
        else:
            cells_train = fold["cells_train"]

        minibatcher_train = Minibatcher(
            cells_train,
            region_ixs,
            n_regions_step=n_regions_step,
            n_cells_step=n_cells_step,
        )
        minibatcher_validation = Minibatcher(
            fold["cells_validation"],
            region_ixs,
            n_regions_step=10,
            n_cells_step=20000,
            permute_cells=False,
            permute_regions=False,
        )

        if device is None:
            device = get_default_device()

        loaders_train = LoaderPool(
            TranscriptomeFragments,
            dict(
                transcriptome=transcriptome,
                fragments=fragments,
                cellxregion_batch_size=minibatcher_train.cellxregion_batch_size,
                layer=self.layer,
                region_oi=self.region_oi,
            ),
            n_workers=2,
        )
        loaders_validation = LoaderPool(
            TranscriptomeFragments,
            dict(
                transcriptome=transcriptome,
                fragments=fragments,
                cellxregion_batch_size=minibatcher_validation.cellxregion_batch_size,
                layer=self.layer,
                region_oi=self.region_oi,
            ),
            n_workers=2,
        )

        if optimizer == "adam":
            optimizer = Adam(self.parameters(), lr=lr, weight_decay=weight_decay)
        elif optimizer == "adamw":
            optimizer = AdamW(
                self.parameters(),
                lr=lr,
                weight_decay=weight_decay,
            )
        elif optimizer == "radam":
            optimizer = RAdam(
                self.parameters(),
                lr=lr,
                weight_decay=weight_decay,
            )
        elif optimizer == "lbfgs":
            optimizer = Adamax(
                self.parameters(),
                lr=lr,
                weight_decay=weight_decay,
            )
        else:
            raise ValueError()

        trainer = SharedTrainer(
            self,
            loaders_train,
            loaders_validation,
            minibatcher_train,
            minibatcher_validation,
            optimizer,
            n_epochs=n_epochs,
            checkpoint_every_epoch=checkpoint_every_epoch,
            optimize_every_step=1,
            device=device,
            pbar=pbar,
            **kwargs,
        )

        self.trace = trainer.trace

        trainer.train()

    def get_prediction(
        self,
        fragments=None,
        transcriptome=None,
        cells=None,
        cell_ixs=None,
        device=None,
        return_raw=False,
    ):
        """
        Returns the prediction of a dataset
        """

        if fragments is None:
            fragments = self.fragments
        if transcriptome is None:
            transcriptome = self.transcriptome
        if cell_ixs is None:
            if cells is None:
                cells = fragments.obs.index
            fragments.obs["ix"] = np.arange(len(fragments.obs))
            cell_ixs = fragments.obs.loc[cells]["ix"].values
        if cells is None:
            cells = fragments.obs.index[cell_ixs]

        regions = [self.region_oi]
        region_ixs = [fragments.var.index.get_loc(self.region_oi)]

        if device is None:
            device = get_default_device()

        minibatches = Minibatcher(
            cell_ixs,
            region_ixs,
            n_regions_step=500,
            n_cells_step=1000,
            # n_cells_step=200,
            use_all_cells=True,
            use_all_regions=True,
            permute_cells=False,
            permute_regions=False,
        )
        loaders = LoaderPool(
            TranscriptomeFragments,
            dict(
                transcriptome=transcriptome,
                fragments=fragments,
                cellxregion_batch_size=minibatches.cellxregion_batch_size,
                layer=self.layer,
                region_oi=self.region_oi,
            ),
            n_workers=10,
        )
        loaders.initialize(minibatches)

        predicted = np.zeros((len(cell_ixs), len(region_ixs)))
        expected = np.zeros((len(cell_ixs), len(region_ixs)))
        n_fragments = np.zeros((len(cell_ixs), len(region_ixs)))

        cell_mapping = np.zeros(fragments.n_cells, dtype=np.int64)
        cell_mapping[cell_ixs] = np.arange(len(cell_ixs))

        region_mapping = np.zeros(fragments.n_regions, dtype=np.int64)
        region_mapping[region_ixs] = np.arange(len(region_ixs))

        self.eval()
        self = self.to(device)

        for data in loaders:
            data = data.to(device)
            with torch.no_grad():
                pred_mb = self.forward(data)
            predicted[
                np.ix_(
                    cell_mapping[data.minibatch.cells_oi],
                    region_mapping[data.minibatch.regions_oi],
                )
            ] = pred_mb.cpu().numpy()
            expected[
                np.ix_(
                    cell_mapping[data.minibatch.cells_oi],
                    region_mapping[data.minibatch.regions_oi],
                )
            ] = data.transcriptome.value.cpu().numpy()
            n_fragments[
                np.ix_(
                    cell_mapping[data.minibatch.cells_oi],
                    region_mapping[data.minibatch.regions_oi],
                )
            ] = (
                torch.bincount(
                    data.fragments.local_cellxregion_ix,
                    minlength=len(data.minibatch.cells_oi) * len(data.minibatch.regions_oi),
                )
                .reshape(len(data.minibatch.cells_oi), len(data.minibatch.regions_oi))
                .cpu()
                .numpy()
            )

        self = self.to("cpu")

        if return_raw:
            return predicted, expected, n_fragments

        result = xr.Dataset(
            {
                "predicted": xr.DataArray(
                    predicted,
                    dims=(fragments.obs.index.name, fragments.var.index.name),
                    coords={
                        fragments.obs.index.name: cells,
                        fragments.var.index.name: regions,
                    },
                ),
                "expected": xr.DataArray(
                    expected,
                    dims=(fragments.obs.index.name, fragments.var.index.name),
                    coords={
                        fragments.obs.index.name: cells,
                        fragments.var.index.name: regions,
                    },
                ),
                "n_fragments": xr.DataArray(
                    n_fragments,
                    dims=(fragments.obs.index.name, fragments.var.index.name),
                    coords={
                        fragments.obs.index.name: cells,
                        fragments.var.index.name: regions,
                    },
                ),
            }
        )
        return result

    def get_prediction_censored(
        self,
        censorer,
        fragments=None,
        transcriptome=None,
        cells=None,
        cell_ixs=None,
        regions=None,
        region_ixs=None,
        device=None,
        min_fragments=5,
    ):
        """
        Returns the prediction of multiple censored dataset
        """
        if fragments is None:
            fragments = self.fragments
        if transcriptome is None:
            transcriptome = self.transcriptome

        if cell_ixs is None:
            if cells is None:
                cells = fragments.obs.index
            fragments.obs["ix"] = np.arange(len(fragments.obs))
            cell_ixs = fragments.obs.loc[cells]["ix"].values
        if cells is None:
            cells = fragments.obs.index[cell_ixs]

        region_ixs = [fragments.var.index.get_loc(self.region_oi)]

        if device is None:
            device = get_default_device()

        minibatcher = Minibatcher(
            cell_ixs,
            region_ixs,
            n_regions_step=500,
            n_cells_step=5000,
            use_all_cells=True,
            use_all_regions=True,
            permute_cells=False,
            permute_regions=False,
        )

        loader = TranscriptomeFragments(
            transcriptome=transcriptome,
            fragments=fragments,
            cellxregion_batch_size=minibatcher.cellxregion_batch_size,
            layer=self.layer,
            region_oi=self.region_oi,
        )

        predicted = np.zeros((len(censorer), len(cell_ixs), len(region_ixs)), dtype=float)
        expected = np.zeros((len(cell_ixs), len(region_ixs)), dtype=float)
        n_fragments = np.zeros((len(censorer), len(cell_ixs), len(region_ixs)), dtype=int)

        predicted = []
        n_fragments = []

        cell_mapping = np.zeros(fragments.n_cells, dtype=np.int64)
        cell_mapping[cell_ixs] = np.arange(len(cell_ixs))
        region_mapping = np.zeros(fragments.n_regions, dtype=np.int64)
        region_mapping[region_ixs] = np.arange(len(region_ixs))

        self.eval()
        self.to(device)
        assert len(minibatcher) == 1
        for minibatch in minibatcher:
            data = loader.load(minibatch)
            data = data.to(device)
            fragments_oi = censorer(data)

            with torch.no_grad():
                for (
                    design_ix,
                    (
                        pred_mb,
                        n_fragments_oi_mb,
                    ),
                ) in enumerate(self.forward_multiple(data, fragments_oi, min_fragments=min_fragments)):
                    predicted.append(pred_mb)
                    n_fragments.append(n_fragments_oi_mb)
            expected = data.transcriptome.value.cpu().numpy()

        self.to("cpu")
        predicted = torch.stack(predicted, axis=0).cpu().numpy()
        n_fragments = torch.stack(n_fragments, axis=0).cpu().numpy()

        return predicted, expected, n_fragments

    def get_performance_censored(
        self,
        censorer,
        fragments=None,
        transcriptome=None,
        cells=None,
        cell_ixs=None,
        regions=None,
        region_ixs=None,
        device=None,
        min_fragments=5,
    ):
        """
        Returns the prediction of multiple censored dataset
        """
        if fragments is None:
            fragments = self.fragments
        if transcriptome is None:
            transcriptome = self.transcriptome

        if cell_ixs is None:
            if cells is None:
                cells = fragments.obs.index
            fragments.obs["ix"] = np.arange(len(fragments.obs))
            cell_ixs = fragments.obs.loc[cells]["ix"].values
        if cells is None:
            cells = fragments.obs.index[cell_ixs]

        region_ixs = [fragments.var.index.get_loc(self.region_oi)]

        if device is None:
            device = get_default_device()

        minibatcher = Minibatcher(
            cell_ixs,
            region_ixs,
            n_regions_step=500,
            n_cells_step=5000,
            use_all_cells=True,
            use_all_regions=True,
            permute_cells=False,
            permute_regions=False,
        )

        loader = TranscriptomeFragments(
            transcriptome=transcriptome,
            fragments=fragments,
            cellxregion_batch_size=minibatcher.cellxregion_batch_size,
            layer=self.layer,
            region_oi=self.region_oi,
        )

        deltacors = []
        losts = []
        effects = []

        self.eval()
        self.to(device)
        for minibatch in minibatcher:
            data = loader.load(minibatch).to(device)
            fragments_oi = censorer(data)

            with torch.no_grad():
                for (
                    design_ix,
                    (
                        pred_mb,
                        n_fragments_oi_mb,
                    ),
                ) in enumerate(self.forward_multiple(data, fragments_oi, min_fragments=min_fragments)):
                    if design_ix == 0:
                        cor_baseline = paircor(pred_mb, data.transcriptome.value).cpu().numpy()
                        deltacor = 0
                        n_fragments_baseline = n_fragments_oi_mb.sum().cpu().numpy()
                        prediction_baseline = pred_mb.cpu().numpy()
                        lost = 0
                        effect = 0
                    else:
                        cor = paircor(pred_mb, data.transcriptome.value).cpu().numpy()
                        deltacor = cor - cor_baseline
                        lost = n_fragments_baseline - n_fragments_oi_mb.sum().cpu().numpy()
                        effect = (pred_mb.cpu().numpy() - prediction_baseline).mean()

                        deltacors.append(deltacor)
                        losts.append(lost)
                        effects.append(effect)

        self.to("cpu")

        deltacors = np.concatenate(deltacors)
        print(deltacors)

        return deltacors, losts, effects

fold = Stored() class-attribute instance-attribute

The cells used for training, test and validation

fragments = Linked() class-attribute instance-attribute

The fragments

layer = Stored() class-attribute instance-attribute

The layer of the transcriptome

region_oi = Stored() class-attribute instance-attribute

The region of interest

transcriptome = Linked() class-attribute instance-attribute

The transcriptome

create(fragments, transcriptome, fold=None, layer=None, path=None, dummy=False, n_frequencies=(1000, 500, 250, 125, 63, 31), reduce='sum', nonlinear='silu', n_embedding_dimensions=100, embedding_to_expression_initialization='default', dropout_rate_fragment_embedder=0.0, n_layers_fragment_embedder=1, residual_fragment_embedder=True, batchnorm_fragment_embedder=False, layernorm_fragment_embedder=False, n_layers_embedding2expression=5, dropout_rate_embedding2expression=0.0, residual_embedding2expression=True, batchnorm_embedding2expression=False, layernorm_embedding2expression=True, overwrite=False, encoder=None, pooler=None, distance_encoder='direct', library_size_encoder='linear', library_size_encoder_kwargs=None, region_oi=None, encoder_kwargs=None, fragment_embedder_kwargs=None, **kwargs) classmethod

Create the model

Parameters:

Name Type Description Default
fragments Fragments

the fragments

required
transcriptome Transcriptome

the transcriptome

required
fold

the fold

None
layer str | None

which layer from the transcriptome to use for training and inference, will use the first layer if None

None
path str | PathLike

the path to save the model

None
dummy bool

whether to use a dummy model that just counts fragments.

False
n_frequencies int

the number of frequencies to use for the encoding

(1000, 500, 250, 125, 63, 31)
reduce str

the reduction to use for pooling fragments across regions and cells

'sum'
nonlinear bool

whether to use a non-linear activation function

'silu'
n_embedding_dimensions int

the number of embedding dimensions

100
dropout_rate

the dropout rate

required
Source code in src/chromatinhd/models/pred/model/better.py
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
@classmethod
def create(
    cls,
    fragments: Fragments,
    transcriptome: Transcriptome,
    fold=None,
    layer: str | None = None,
    path: str | os.PathLike = None,
    dummy: bool = False,
    n_frequencies: int = (1000, 500, 250, 125, 63, 31),
    reduce: str = "sum",
    nonlinear: bool = "silu",
    n_embedding_dimensions: int = 100,
    embedding_to_expression_initialization: str = "default",
    dropout_rate_fragment_embedder: float = 0.0,
    n_layers_fragment_embedder=1,
    residual_fragment_embedder=True,
    batchnorm_fragment_embedder=False,
    layernorm_fragment_embedder=False,
    n_layers_embedding2expression=5,
    dropout_rate_embedding2expression: float = 0.0,
    residual_embedding2expression=True,
    batchnorm_embedding2expression=False,
    layernorm_embedding2expression=True,
    overwrite=False,
    encoder=None,
    pooler=None,
    distance_encoder="direct",
    library_size_encoder="linear",
    library_size_encoder_kwargs=None,
    region_oi=None,
    encoder_kwargs=None,
    fragment_embedder_kwargs=None,
    **kwargs: Any,
) -> None:
    """
    Create the model

    Parameters:
        fragments:
            the fragments
        transcriptome:
            the transcriptome
        fold:
            the fold
        layer:
            which layer from the transcriptome to use for training and inference, will use the first layer if None
        path:
            the path to save the model
        dummy:
            whether to use a dummy model that just counts fragments.
        n_frequencies:
            the number of frequencies to use for the encoding
        reduce:
            the reduction to use for pooling fragments across regions and cells
        nonlinear:
            whether to use a non-linear activation function
        n_embedding_dimensions:
            the number of embedding dimensions
        dropout_rate:
            the dropout rate
    """
    self = super(Model, cls).create(
        path=path, fragments=fragments, transcriptome=transcriptome, fold=fold, reset=overwrite
    )

    self.fold = fold

    if layer is not None:
        self.layer = layer
    else:
        layer = list(self.transcriptome.layers.keys())[0]
    self.layer = layer

    if region_oi is None:
        region_oi = fragments.var.index[0]
    self.region_oi = region_oi

    if fragment_embedder_kwargs is None:
        fragment_embedder_kwargs = {}

    if dummy is True:
        self.fragment_embedder = FragmentEmbedderCounter()
    else:
        self.fragment_embedder = FragmentEmbedder(
            n_frequencies=n_frequencies,
            nonlinear=nonlinear,
            n_layers=n_layers_fragment_embedder,
            n_embedding_dimensions=n_embedding_dimensions,
            dropout_rate=dropout_rate_fragment_embedder,
            residual=residual_fragment_embedder,
            batchnorm=batchnorm_fragment_embedder,
            layernorm=layernorm_fragment_embedder,
            fragments=self.fragments,
            encoder=encoder,
            distance_encoder=distance_encoder,
            encoder_kwargs=encoder_kwargs,
            **fragment_embedder_kwargs,
        )
    self.embedding_region_pooler = EmbeddingGenePooler(
        self.fragment_embedder.n_embedding_dimensions, reduce=reduce, pooler=pooler
    )

    n_input_embedding_dimensions = self.fragment_embedder.n_embedding_dimensions

    # library size encoder
    if library_size_encoder == "linear":
        library_size_encoder_kwargs = library_size_encoder_kwargs or {}
        self.library_size_encoder = LibrarySizeEncoder(fragments, **library_size_encoder_kwargs)
        n_input_embedding_dimensions += self.library_size_encoder.n_embedding_dimensions
    elif library_size_encoder is None:
        self.library_size_encoder = None
    else:
        raise ValueError(library_size_encoder + " is not a valid library size encoder")

    # embedding to expression
    self.embedding_to_expression = EmbeddingToExpression(
        fragments=fragments,
        n_input_embedding_dimensions=n_input_embedding_dimensions,
        n_embedding_dimensions=self.fragment_embedder.n_embedding_dimensions,
        initialization=embedding_to_expression_initialization,
        n_layers=n_layers_embedding2expression,
        residual=residual_embedding2expression,
        dropout_rate=dropout_rate_embedding2expression,
        batchnorm=batchnorm_embedding2expression,
        layernorm=layernorm_embedding2expression,
        nonlinear=nonlinear,
    )

    return self

forward(data)

Make a prediction given a data object

Source code in src/chromatinhd/models/pred/model/better.py
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
def forward(self, data):
    """
    Make a prediction given a data object
    """
    assert data.minibatch.n_regions == 1

    fragment_embedding = self.fragment_embedder(data)
    cell_region_embedding = self.embedding_region_pooler(
        fragment_embedding,
        data.fragments.local_cellxregion_ix,
        data.minibatch.n_cells,
        data.minibatch.n_regions,
    )
    if hasattr(self, "library_size_encoder") and (self.library_size_encoder is not None):
        library_size_encoding = self.library_size_encoder(data).unsqueeze(-2)
        cell_region_embedding = torch.cat([cell_region_embedding, library_size_encoding], dim=-1)
    expression_predicted = self.embedding_to_expression(cell_region_embedding)
    self.expression_predicted = expression_predicted

    return expression_predicted

forward_loss(data)

Make a prediction and calculate the loss

Source code in src/chromatinhd/models/pred/model/better.py
586
587
588
589
590
591
592
def forward_loss(self, data):
    """
    Make a prediction and calculate the loss
    """
    expression_predicted = self.forward(data)
    expression_true = data.transcriptome.value
    return paircor_loss(expression_predicted, expression_true)

forward_multiple(data, fragments_oi, min_fragments=1)

Make multiple predictions based on different sets of fragments

Parameters:

Name Type Description Default
data

the data object

required
fragments_oi

an iterator of boolean arrays indicating which fragments to use

required
min_fragments

the minimum number of fragments that have to remove before re-calculating the prediction

1
Source code in src/chromatinhd/models/pred/model/better.py
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
def forward_multiple(self, data, fragments_oi, min_fragments=1):
    """
    Make multiple predictions based on different sets of fragments

    Parameters:
        data:
            the data object
        fragments_oi:
            an iterator of boolean arrays indicating which fragments to use
        min_fragments:
            the minimum number of fragments that have to remove before re-calculating the prediction
    """
    fragment_embedding = self.fragment_embedder(data)

    total_n_fragments = torch.bincount(
        data.fragments.local_cellxregion_ix,
        minlength=data.minibatch.n_regions * data.minibatch.n_cells,
    ).reshape((data.minibatch.n_cells, data.minibatch.n_regions))

    total_cell_region_embedding = self.embedding_region_pooler.forward(
        fragment_embedding,
        data.fragments.local_cellxregion_ix,
        data.minibatch.n_cells,
        data.minibatch.n_regions,
    )
    cell_region_embedding = total_cell_region_embedding

    if hasattr(self, "library_size_encoder"):
        cell_region_embedding = torch.cat(
            [cell_region_embedding, self.library_size_encoder(data).unsqueeze(-2)],
            dim=-1,
        )

    total_expression_predicted = self.embedding_to_expression.forward(cell_region_embedding)

    for fragments_oi_ in fragments_oi:
        if (fragments_oi_ is not None) and ((~fragments_oi_).sum().item() > min_fragments):
            lost_fragments_oi = ~fragments_oi_
            lost_local_cellxregion_ix = data.fragments.local_cellxregion_ix[lost_fragments_oi]
            n_fragments = total_n_fragments - torch.bincount(
                lost_local_cellxregion_ix,
                minlength=data.minibatch.n_regions * data.minibatch.n_cells,
            ).reshape((data.minibatch.n_cells, data.minibatch.n_regions))

            cell_region_embedding = total_cell_region_embedding - self.embedding_region_pooler.forward(
                fragment_embedding[lost_fragments_oi],
                lost_local_cellxregion_ix,
                data.minibatch.n_cells,
                data.minibatch.n_regions,
            )

            if hasattr(self, "library_size_encoder"):
                cell_region_embedding = torch.cat(
                    [
                        cell_region_embedding,
                        self.library_size_encoder(data).unsqueeze(-2),
                    ],
                    dim=-1,
                )

            expression_predicted = self.embedding_to_expression.forward(cell_region_embedding)
        else:
            n_fragments = total_n_fragments
            expression_predicted = total_expression_predicted

        yield expression_predicted, n_fragments

forward_region_loss(data)

Make a prediction and calculate the loss on a per region basis

Source code in src/chromatinhd/models/pred/model/better.py
595
596
597
598
599
600
601
def forward_region_loss(self, data):
    """
    Make a prediction and calculate the loss on a per region basis
    """
    expression_predicted = self.forward(data)
    expression_true = data.transcriptome.value
    return region_paircor_loss(expression_predicted, expression_true)

get_performance_censored(censorer, fragments=None, transcriptome=None, cells=None, cell_ixs=None, regions=None, region_ixs=None, device=None, min_fragments=5)

Returns the prediction of multiple censored dataset

Source code in src/chromatinhd/models/pred/model/better.py
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
def get_performance_censored(
    self,
    censorer,
    fragments=None,
    transcriptome=None,
    cells=None,
    cell_ixs=None,
    regions=None,
    region_ixs=None,
    device=None,
    min_fragments=5,
):
    """
    Returns the prediction of multiple censored dataset
    """
    if fragments is None:
        fragments = self.fragments
    if transcriptome is None:
        transcriptome = self.transcriptome

    if cell_ixs is None:
        if cells is None:
            cells = fragments.obs.index
        fragments.obs["ix"] = np.arange(len(fragments.obs))
        cell_ixs = fragments.obs.loc[cells]["ix"].values
    if cells is None:
        cells = fragments.obs.index[cell_ixs]

    region_ixs = [fragments.var.index.get_loc(self.region_oi)]

    if device is None:
        device = get_default_device()

    minibatcher = Minibatcher(
        cell_ixs,
        region_ixs,
        n_regions_step=500,
        n_cells_step=5000,
        use_all_cells=True,
        use_all_regions=True,
        permute_cells=False,
        permute_regions=False,
    )

    loader = TranscriptomeFragments(
        transcriptome=transcriptome,
        fragments=fragments,
        cellxregion_batch_size=minibatcher.cellxregion_batch_size,
        layer=self.layer,
        region_oi=self.region_oi,
    )

    deltacors = []
    losts = []
    effects = []

    self.eval()
    self.to(device)
    for minibatch in minibatcher:
        data = loader.load(minibatch).to(device)
        fragments_oi = censorer(data)

        with torch.no_grad():
            for (
                design_ix,
                (
                    pred_mb,
                    n_fragments_oi_mb,
                ),
            ) in enumerate(self.forward_multiple(data, fragments_oi, min_fragments=min_fragments)):
                if design_ix == 0:
                    cor_baseline = paircor(pred_mb, data.transcriptome.value).cpu().numpy()
                    deltacor = 0
                    n_fragments_baseline = n_fragments_oi_mb.sum().cpu().numpy()
                    prediction_baseline = pred_mb.cpu().numpy()
                    lost = 0
                    effect = 0
                else:
                    cor = paircor(pred_mb, data.transcriptome.value).cpu().numpy()
                    deltacor = cor - cor_baseline
                    lost = n_fragments_baseline - n_fragments_oi_mb.sum().cpu().numpy()
                    effect = (pred_mb.cpu().numpy() - prediction_baseline).mean()

                    deltacors.append(deltacor)
                    losts.append(lost)
                    effects.append(effect)

    self.to("cpu")

    deltacors = np.concatenate(deltacors)
    print(deltacors)

    return deltacors, losts, effects

get_prediction(fragments=None, transcriptome=None, cells=None, cell_ixs=None, device=None, return_raw=False)

Returns the prediction of a dataset

Source code in src/chromatinhd/models/pred/model/better.py
 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
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
def get_prediction(
    self,
    fragments=None,
    transcriptome=None,
    cells=None,
    cell_ixs=None,
    device=None,
    return_raw=False,
):
    """
    Returns the prediction of a dataset
    """

    if fragments is None:
        fragments = self.fragments
    if transcriptome is None:
        transcriptome = self.transcriptome
    if cell_ixs is None:
        if cells is None:
            cells = fragments.obs.index
        fragments.obs["ix"] = np.arange(len(fragments.obs))
        cell_ixs = fragments.obs.loc[cells]["ix"].values
    if cells is None:
        cells = fragments.obs.index[cell_ixs]

    regions = [self.region_oi]
    region_ixs = [fragments.var.index.get_loc(self.region_oi)]

    if device is None:
        device = get_default_device()

    minibatches = Minibatcher(
        cell_ixs,
        region_ixs,
        n_regions_step=500,
        n_cells_step=1000,
        # n_cells_step=200,
        use_all_cells=True,
        use_all_regions=True,
        permute_cells=False,
        permute_regions=False,
    )
    loaders = LoaderPool(
        TranscriptomeFragments,
        dict(
            transcriptome=transcriptome,
            fragments=fragments,
            cellxregion_batch_size=minibatches.cellxregion_batch_size,
            layer=self.layer,
            region_oi=self.region_oi,
        ),
        n_workers=10,
    )
    loaders.initialize(minibatches)

    predicted = np.zeros((len(cell_ixs), len(region_ixs)))
    expected = np.zeros((len(cell_ixs), len(region_ixs)))
    n_fragments = np.zeros((len(cell_ixs), len(region_ixs)))

    cell_mapping = np.zeros(fragments.n_cells, dtype=np.int64)
    cell_mapping[cell_ixs] = np.arange(len(cell_ixs))

    region_mapping = np.zeros(fragments.n_regions, dtype=np.int64)
    region_mapping[region_ixs] = np.arange(len(region_ixs))

    self.eval()
    self = self.to(device)

    for data in loaders:
        data = data.to(device)
        with torch.no_grad():
            pred_mb = self.forward(data)
        predicted[
            np.ix_(
                cell_mapping[data.minibatch.cells_oi],
                region_mapping[data.minibatch.regions_oi],
            )
        ] = pred_mb.cpu().numpy()
        expected[
            np.ix_(
                cell_mapping[data.minibatch.cells_oi],
                region_mapping[data.minibatch.regions_oi],
            )
        ] = data.transcriptome.value.cpu().numpy()
        n_fragments[
            np.ix_(
                cell_mapping[data.minibatch.cells_oi],
                region_mapping[data.minibatch.regions_oi],
            )
        ] = (
            torch.bincount(
                data.fragments.local_cellxregion_ix,
                minlength=len(data.minibatch.cells_oi) * len(data.minibatch.regions_oi),
            )
            .reshape(len(data.minibatch.cells_oi), len(data.minibatch.regions_oi))
            .cpu()
            .numpy()
        )

    self = self.to("cpu")

    if return_raw:
        return predicted, expected, n_fragments

    result = xr.Dataset(
        {
            "predicted": xr.DataArray(
                predicted,
                dims=(fragments.obs.index.name, fragments.var.index.name),
                coords={
                    fragments.obs.index.name: cells,
                    fragments.var.index.name: regions,
                },
            ),
            "expected": xr.DataArray(
                expected,
                dims=(fragments.obs.index.name, fragments.var.index.name),
                coords={
                    fragments.obs.index.name: cells,
                    fragments.var.index.name: regions,
                },
            ),
            "n_fragments": xr.DataArray(
                n_fragments,
                dims=(fragments.obs.index.name, fragments.var.index.name),
                coords={
                    fragments.obs.index.name: cells,
                    fragments.var.index.name: regions,
                },
            ),
        }
    )
    return result

get_prediction_censored(censorer, fragments=None, transcriptome=None, cells=None, cell_ixs=None, regions=None, region_ixs=None, device=None, min_fragments=5)

Returns the prediction of multiple censored dataset

Source code in src/chromatinhd/models/pred/model/better.py
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
def get_prediction_censored(
    self,
    censorer,
    fragments=None,
    transcriptome=None,
    cells=None,
    cell_ixs=None,
    regions=None,
    region_ixs=None,
    device=None,
    min_fragments=5,
):
    """
    Returns the prediction of multiple censored dataset
    """
    if fragments is None:
        fragments = self.fragments
    if transcriptome is None:
        transcriptome = self.transcriptome

    if cell_ixs is None:
        if cells is None:
            cells = fragments.obs.index
        fragments.obs["ix"] = np.arange(len(fragments.obs))
        cell_ixs = fragments.obs.loc[cells]["ix"].values
    if cells is None:
        cells = fragments.obs.index[cell_ixs]

    region_ixs = [fragments.var.index.get_loc(self.region_oi)]

    if device is None:
        device = get_default_device()

    minibatcher = Minibatcher(
        cell_ixs,
        region_ixs,
        n_regions_step=500,
        n_cells_step=5000,
        use_all_cells=True,
        use_all_regions=True,
        permute_cells=False,
        permute_regions=False,
    )

    loader = TranscriptomeFragments(
        transcriptome=transcriptome,
        fragments=fragments,
        cellxregion_batch_size=minibatcher.cellxregion_batch_size,
        layer=self.layer,
        region_oi=self.region_oi,
    )

    predicted = np.zeros((len(censorer), len(cell_ixs), len(region_ixs)), dtype=float)
    expected = np.zeros((len(cell_ixs), len(region_ixs)), dtype=float)
    n_fragments = np.zeros((len(censorer), len(cell_ixs), len(region_ixs)), dtype=int)

    predicted = []
    n_fragments = []

    cell_mapping = np.zeros(fragments.n_cells, dtype=np.int64)
    cell_mapping[cell_ixs] = np.arange(len(cell_ixs))
    region_mapping = np.zeros(fragments.n_regions, dtype=np.int64)
    region_mapping[region_ixs] = np.arange(len(region_ixs))

    self.eval()
    self.to(device)
    assert len(minibatcher) == 1
    for minibatch in minibatcher:
        data = loader.load(minibatch)
        data = data.to(device)
        fragments_oi = censorer(data)

        with torch.no_grad():
            for (
                design_ix,
                (
                    pred_mb,
                    n_fragments_oi_mb,
                ),
            ) in enumerate(self.forward_multiple(data, fragments_oi, min_fragments=min_fragments)):
                predicted.append(pred_mb)
                n_fragments.append(n_fragments_oi_mb)
        expected = data.transcriptome.value.cpu().numpy()

    self.to("cpu")
    predicted = torch.stack(predicted, axis=0).cpu().numpy()
    n_fragments = torch.stack(n_fragments, axis=0).cpu().numpy()

    return predicted, expected, n_fragments

train_model(fold=None, fragments=None, transcriptome=None, device=None, lr=0.0001, n_epochs=1000, pbar=True, n_regions_step=1, n_cells_step=20000, weight_decay=0.1, checkpoint_every_epoch=1, optimizer='adam', n_cells_train=None, **kwargs)

Train the model

Source code in src/chromatinhd/models/pred/model/better.py
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
def train_model(
    self,
    fold: list = None,
    fragments: Fragments = None,
    transcriptome: Transcriptome = None,
    device=None,
    lr=1e-4,
    n_epochs=1000,
    pbar=True,
    n_regions_step=1,
    n_cells_step=20000,
    weight_decay=1e-1,
    checkpoint_every_epoch=1,
    optimizer="adam",
    n_cells_train=None,
    **kwargs,
):
    """
    Train the model
    """
    if fold is None:
        fold = self.fold
    assert fold is not None

    if fragments is None:
        fragments = self.fragments
    if transcriptome is None:
        transcriptome = self.transcriptome

    # set up minibatchers and loaders
    if self.region_oi is not None:
        fragments.var["ix"] = np.arange(len(fragments.var))
        region_ixs = fragments.var["ix"].loc[[self.region_oi]].values
    else:
        region_ixs = range(fragments.n_regions)

    if n_cells_train is not None:
        cells_train = fold["cells_train"][:n_cells_train]
    else:
        cells_train = fold["cells_train"]

    minibatcher_train = Minibatcher(
        cells_train,
        region_ixs,
        n_regions_step=n_regions_step,
        n_cells_step=n_cells_step,
    )
    minibatcher_validation = Minibatcher(
        fold["cells_validation"],
        region_ixs,
        n_regions_step=10,
        n_cells_step=20000,
        permute_cells=False,
        permute_regions=False,
    )

    if device is None:
        device = get_default_device()

    loaders_train = LoaderPool(
        TranscriptomeFragments,
        dict(
            transcriptome=transcriptome,
            fragments=fragments,
            cellxregion_batch_size=minibatcher_train.cellxregion_batch_size,
            layer=self.layer,
            region_oi=self.region_oi,
        ),
        n_workers=2,
    )
    loaders_validation = LoaderPool(
        TranscriptomeFragments,
        dict(
            transcriptome=transcriptome,
            fragments=fragments,
            cellxregion_batch_size=minibatcher_validation.cellxregion_batch_size,
            layer=self.layer,
            region_oi=self.region_oi,
        ),
        n_workers=2,
    )

    if optimizer == "adam":
        optimizer = Adam(self.parameters(), lr=lr, weight_decay=weight_decay)
    elif optimizer == "adamw":
        optimizer = AdamW(
            self.parameters(),
            lr=lr,
            weight_decay=weight_decay,
        )
    elif optimizer == "radam":
        optimizer = RAdam(
            self.parameters(),
            lr=lr,
            weight_decay=weight_decay,
        )
    elif optimizer == "lbfgs":
        optimizer = Adamax(
            self.parameters(),
            lr=lr,
            weight_decay=weight_decay,
        )
    else:
        raise ValueError()

    trainer = SharedTrainer(
        self,
        loaders_train,
        loaders_validation,
        minibatcher_train,
        minibatcher_validation,
        optimizer,
        n_epochs=n_epochs,
        checkpoint_every_epoch=checkpoint_every_epoch,
        optimize_every_step=1,
        device=device,
        pbar=pbar,
        **kwargs,
    )

    self.trace = trainer.trace

    trainer.train()

chromatinhd.models.pred.model.multiscale.Models

Bases: Flow

Source code in src/chromatinhd/models/pred/model/better.py
1198
1199
1200
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
1230
1231
1232
1233
1234
1235
1236
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
class Models(Flow):
    models = LinkedDict()

    transcriptome = Linked()
    """The transcriptome"""

    fragments = Linked()
    """The fragments"""

    folds = Linked()
    """The folds"""

    model_params = Stored(default=dict)
    train_params = Stored(default=dict)

    regions_oi = Stored(default=lambda: None)

    @property
    def models_path(self):
        path = self.path / "models"
        path.mkdir(exist_ok=True)
        return path

    def train_models(
        self,
        device=None,
        pbar=True,
        transcriptome=None,
        fragments=None,
        folds=None,
        regions_oi=None,
        **kwargs,
    ):
        if "device" in self.train_params and device is None:
            device = self.train_params["device"]

        if fragments is None:
            fragments = self.fragments
        if transcriptome is None:
            transcriptome = self.transcriptome
        if folds is None:
            folds = self.folds

        if regions_oi is None:
            if self.regions_oi is None:
                regions_oi = fragments.var.index
            else:
                regions_oi = self.regions_oi

        progress = itertools.product(enumerate(regions_oi), enumerate(folds))
        if pbar:
            progress = tqdm.tqdm(progress, total=len(regions_oi) * len(folds))

        for (region_ix, region), (fold_ix, fold) in progress:
            model_name = f"{region}_{fold_ix}"
            model_folder = self.models_path / (model_name)
            force = False
            if model_name not in self.models:
                force = True
            elif not self.models[model_name].o.state.exists(self.models[model_name]):
                force = True

            if force:
                model = Model.create(
                    fragments=fragments,
                    transcriptome=transcriptome,
                    fold=fold,
                    region_oi=region,
                    path=model_folder,
                    **self.model_params,
                )
                model.train_model(
                    device=device,
                    pbar=False,
                    **{**{k: v for k, v in self.train_params.items() if k not in ["device"]}, **kwargs},
                )
                model.save_state()

                model = model.to("cpu")

                self.models[model_name] = model

    def __contains__(self, ix):
        return ix in self.models

    def __getitem__(self, ix):
        return self.models[ix]

    def __setitem__(self, ix, value):
        self.models[ix] = value

    def __len__(self):
        return len(self.models)

    def __iter__(self):
        for ix in range(len(self)):
            yield self[ix]

    def get_region_cors(self, fragments, transcriptome, folds, device=None):
        regions_oi = fragments.var.index if self.regions_oi is None else self.regions_oi

        from itertools import product

        cors = []

        if device is None:
            device = get_default_device()
        for region_id, (fold_ix, fold) in product(regions_oi, enumerate(folds)):
            if region_id + "_" + str(fold_ix) in self:
                model = self[region_id + "_" + str(fold_ix)]
                prediction = model.get_prediction(fragments, transcriptome, cell_ixs=fold["cells_test"], device=device)

                cors.append(
                    {
                        fragments.var.index.name: region_id,
                        "cor": np.corrcoef(
                            prediction["predicted"].values[:, 0],
                            prediction["expected"].values[:, 0],
                        )[0, 1],
                        "cor_n_fragments": np.corrcoef(
                            prediction["n_fragments"].values[:, 0],
                            prediction["expected"].values[:, 0],
                        )[0, 1],
                    }
                )

        cors = pd.DataFrame(cors).set_index(fragments.var.index.name)
        cors["deltacor"] = cors["cor"] - cors["cor_n_fragments"]

        return cors

    @property
    def design(self):
        design_dimensions = {
            "fold": range(len(self.folds)),
            "gene": self.regions_oi,
        }
        design = crossing(**design_dimensions)
        design.index = design["gene"] + "_" + design["fold"].astype(str)
        return design

    def fitted(self, region, fold_ix):
        return f"{region}_{fold_ix}" in self.models

    def get_prediction(self, region, fold_ix, **kwargs):
        model = self[f"{region}_{fold_ix}"]
        return model.get_prediction(**kwargs)

    def trained(self, region):
        return all([f"{region}_{fold_ix}" in self.models for fold_ix in range(len(self.folds))])

folds = Linked() class-attribute instance-attribute

The folds

fragments = Linked() class-attribute instance-attribute

The fragments

transcriptome = Linked() class-attribute instance-attribute

The transcriptome