Skip to content

Semantic Signal Separation (\(S^3\))

Semantic Signal Separation tries to recover dimensions/axes along which most of the semantic variations can be explained. A topic in \(S^3\) is an axis of semantics in the corpus. This makes the model able to recover more nuanced topical content in documents, but is not optimal when you expect topics to be groupings of documents.

Schematic overview of S³

\(S^3\) is one of the fastest topic models out there, even rivalling vanilla NMF, when not accounting for embedding time. It also typically produces very high quality topics, and our evaluations indicate that it performs significantly better when no preprocessing is applied to texts.

How does \(S^3\) work?

Step 1: Document-embedding Decomposition

The first step is to decompose the embedding matrix using ICA, this step discovers the underlying semantics axes as latent independent components in the embeddings.

See formula
  • Let the encodings of documents in the corpus be \(X\).
  • Decompose \(X\) using FastICA: \(X = AS\), where \(A\) is the mixing matrix and \(S\) is the document-topic-matrix.

Step 2: Term Importance Estimation

Term importances for each topic are calculated by encoding the entire vocabulary of the corpus using the same embedding model, then recovering the strength of each latent component in the word embedding matrix. The strength of the components in the words will be interpreted as the words' importance in a given topic.

Visual representation of term importance approaches in S³
See formula
  • Let the matrix of word encodings be \(V\).
  • Calculate the pseudo-inverse of the mixing matrix \(C = A^{+}\), where \(C\) is the unmixing matrix.
  • Project word embeddings onto the semantic axes by multiplying them with unmixing matrix: \(W = VC^T\). \(W^T\) is then the topic-term matrix (model.components_).

There are three distinct methods to calculate term importances from word projections:

Choose a word importance method

from turftopic import SemanticSignalSeparation

model = SemanticSignalSeparation(n_components=10, feature_importance="axial")
Axial word importances are defined as the words' positions on the semantic axes. This approach selects highly relevant words for topic descriptions, but topic descriptions might share words if a word scores high on multiple axes.

The importance of word \(j\) for topic \(t\) is: \(\beta_{tj} = W_{jt}\)

from turftopic import SemanticSignalSeparation

model = SemanticSignalSeparation(n_components=10, feature_importance="angular")
Angular topics can be calculated by taking the cosine of the angle between projected word vectors and semantic axes. This allows the approach axis descriptions to be very distinct and specific to the given axis, but might include words that are not as relevant in the corpus.

\(\beta_{tj} = cos(\Theta) = \frac{W_{jt}}{||W_j||}\)

from turftopic import SemanticSignalSeparation

model = SemanticSignalSeparation(n_components=10, feature_importance="combined")
Combined word importance is a combination of axial and andular term importance, and is recommended as it balances the two approaches' strengths and weaknesses.

\(\beta_{tj} = \frac{(W_{jt})^3}{||W_j||}\)

Dynamic Topic Modeling

\(S^3\) can also be used as a dynamic topic model. Temporally changing components are found using the following steps:

  1. Fit a global \(S^3\) model over the whole corpus.
  2. Estimate unmixing matrix for each time-slice by fitting a linear regression from the embeddings in the time slice to the document-topic-matrix for the time slice estimated by the global model.
  3. Estimate term importances for each time slice the same way as the global model.
from datetime import datetime
from turftopic import SemanticSignalSeparation

ts: list[datetime] = [datetime(year=2018, month=2, day=12), ...]
corpus: list[str] = ["First document", ...]

model = SemanticSignalSeparation(10).fit_dynamic(corpus, timestamps=ts, bins=10)
model.plot_topics_over_time()

Info

Topics over time in \(S^3\) are treated slightly differently to most other models. This is because topics are not proportional in \(S^3\), and can tip below zero. In the timeslices where a topic is below zero, its negative definition is displayed.

Topics over time in a dynamic Semantic Signal Separation model.

Model Refitting

Unlike most other models in Turftopic, \(S^3\) can be refit using different parameters and random seeds without needing to initialize the model from scratch. This makes \(S^3\) incredibly convenient for exploring different numbers of topics, or adjusting the number of iterations.

Refitting the model takes a fraction of the time of initializing a new one and fitting it, as the vocabulary doesn't have to be learned or encoded by the model again.

from turftopic import SemanticSignalSeparation

model = SemanticSignalSeparation(5, random_state=42)
model.fit(corpus)

print(len(model.topic_names))
# 5

model.refit(corpus, n_components=10, random_state=30)
print(len(model.topic_names))
# 10

Interpretation

Negative terms

Terms, which rank lowest on a topic have meaning in \(S^3\). Whenever interpreting semantic axes, you should probably consider both ends of the axis. As such, when you print or export topics from \(S^3\), the lowest ranking terms will also be shown along with the highest ranking ones.

Here's an example on ArXiv ML papers:

from turftopic import SemanticSignalSeparation
from sklearn.feature_extraction.text import CountVectorizer

model = SemanticSignalSeparation(5, vectorizer=CountVectorizer(), random_state=42)
model.fit(corpus)

model.print_topics(top_k=5)
Positive Negative
0 clustering, histograms, clusterings, histogram, classifying reinforcement, exploration, planning, tactics, reinforce
1 textual, pagerank, litigants, marginalizing, entailment matlab, waveforms, microcontroller, accelerometers, microcontrollers
2 sparsestmax, denoiseing, denoising, minimizers, minimizes automation, affective, chatbots, questionnaire, attitudes
3 rebmigraph, subgraph, subgraphs, graphsage, graph adversarial, adversarially, adversarialization, adversary, security
4 clustering, estimations, algorithm, dbscan, estimation cnn, deepmind, deeplabv3, convnet, deepseenet

Concept Compass

If you want to gain a deeper understanding of terms' relation to axes, you can produce a concept compass. This involves plotting terms in a corpus along two semantic axes.

In order to use the compass in Turftopic you will need to have plotly installed:

pip install plotly

You can display a compass based on a fitted model like so:

fig = model.plot_concept_compass(topic_x=1, topic_y=4)
fig.show()
Concept Compass of ArXiv ML Papers along two semantic axes.

Image Compass

In multimodal contexts, you can also plot images along two chosen axes by using plot_image_compass().

model = SemanticSignalSeparation(10)
model.fit_multimodal(corpus, images=images)

fig = model.plot_image_compass(topic_x=0, topic_y=1)
fig.show()
Image Compass of IKEA furnitures along two semantic axes

Citation

Please cite Turftopic and Kardos et al. (2025) when using \(S^3\) in Turftopic:

@article{
  Kardos2025,
  title = {Turftopic: Topic Modelling with Contextual Representations from Sentence Transformers},
  doi = {10.21105/joss.08183},
  url = {https://doi.org/10.21105/joss.08183},
  year = {2025},
  publisher = {The Open Journal},
  volume = {10},
  number = {111},
  pages = {8183},
  author = {Kardos, Márton and Enevoldsen, Kenneth C. and Kostkan, Jan and Kristensen-McLachlan, Ross Deans and Rocca, Roberta},
  journal = {Journal of Open Source Software} 
}

@inproceedings{kardos-etal-2025-s3,
    title = "$S^3$ - Semantic Signal Separation",
    author = "Kardos, M{\'a}rton  and
      Kostkan, Jan  and
      Enevoldsen, Kenneth  and
      Vermillet, Arnault-Quentin  and
      Nielbo, Kristoffer  and
      Rocca, Roberta",
    editor = "Che, Wanxiang  and
      Nabende, Joyce  and
      Shutova, Ekaterina  and
      Pilehvar, Mohammad Taher",
    booktitle = "Proceedings of the 63rd Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
    month = jul,
    year = "2025",
    address = "Vienna, Austria",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2025.acl-long.32/",
    doi = "10.18653/v1/2025.acl-long.32",
    pages = "633--666",
    ISBN = "979-8-89176-251-0",
    abstract = "Topic models are useful tools for discovering latent semantic structures in large textual corpora. Recent efforts have been oriented at incorporating contextual representations in topic modeling and have been shown to outperform classical topic models. These approaches are typically slow, volatile, and require heavy preprocessing for optimal results. We present Semantic Signal Separation ($S^3$), a theory-driven topic modeling approach in neural embedding spaces. $S^3$ conceptualizes topics as independent axes of semantic space and uncovers these by decomposing contextualized document embeddings using Independent Component Analysis. Our approach provides diverse and highly coherent topics, requires no preprocessing, and is demonstrated to be the fastest contextual topic model, being, on average, 4.5x faster than the runner-up BERTopic. We offer an implementation of $S^3$, and all contextual baselines, in the Turftopic Python package."
}

API Reference

turftopic.models.decomp.SemanticSignalSeparation

Bases: ContextualModel, DynamicTopicModel, MultimodalModel

Separates the embedding matrix into 'semantic signals' with component analysis methods. Topics are assumed to be dimensions of semantics.

from turftopic import SemanticSignalSeparation

corpus: list[str] = ["some text", "more text", ...]

model = SemanticSignalSeparation(10).fit(corpus)
model.print_topics()

Parameters:

Name Type Description Default
n_components int

Number of topics.

10
encoder Union[Encoder, str, MultimodalEncoder]

Model to encode documents/terms, all-MiniLM-L6-v2 is the default.

'sentence-transformers/all-MiniLM-L6-v2'
vectorizer Optional[CountVectorizer]

Vectorizer used for term extraction. Can be used to prune or filter the vocabulary.

None
decomposition Optional[TransformerMixin]

Custom decomposition method to use. Can be an instance of FastICA or PCA, or basically any dimensionality reduction method. Has to have fit_transform and fit methods. If not specified, FastICA is used.

None
max_iter int

Maximum number of iterations for ICA.

200
feature_importance Literal['axial', 'angular', 'combined']

Defines whether the word's position on an axis ('axial'), it's angle to the axis ('angular') or their combination ('combined') should determine the word's importance for a topic.

'combined'
random_state Optional[int]

Random state to use so that results are exactly reproducible.

None
trf_kwargs

Keyword arguments to apply when loading the Encoder model.

None
encode_kwargs

Keyword arguments to apply encoding documents with the encoder.

None
Source code in turftopic/models/decomp.py
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 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
1196
1197
1198
1199
1200
class SemanticSignalSeparation(
    ContextualModel, DynamicTopicModel, MultimodalModel
):
    """Separates the embedding matrix into 'semantic signals' with
    component analysis methods.
    Topics are assumed to be dimensions of semantics.

    ```python
    from turftopic import SemanticSignalSeparation

    corpus: list[str] = ["some text", "more text", ...]

    model = SemanticSignalSeparation(10).fit(corpus)
    model.print_topics()
    ```

    Parameters
    ----------
    n_components: int, default 10
        Number of topics.
    encoder: str or SentenceTransformer
        Model to encode documents/terms, all-MiniLM-L6-v2 is the default.
    vectorizer: CountVectorizer, default None
        Vectorizer used for term extraction.
        Can be used to prune or filter the vocabulary.
    decomposition: TransformerMixin, default None
        Custom decomposition method to use.
        Can be an instance of FastICA or PCA, or basically any dimensionality
        reduction method. Has to have `fit_transform` and `fit` methods.
        If not specified, FastICA is used.
    max_iter: int, default 200
        Maximum number of iterations for ICA.
    feature_importance: "axial", "angular" or "combined", default "combined"
        Defines whether the word's position on an axis ('axial'), it's angle to the axis ('angular')
        or their combination ('combined') should determine the word's importance for a topic.
    random_state: int, default None
        Random state to use so that results are exactly reproducible.
    trf_kwargs: dict, default None
        Keyword arguments to apply when loading the Encoder model.
    encode_kwargs: dict, default None
        Keyword arguments to apply encoding documents with the encoder.
    """

    def __init__(
        self,
        n_components: int = 10,
        encoder: Union[
            Encoder, str, MultimodalEncoder
        ] = "sentence-transformers/all-MiniLM-L6-v2",
        vectorizer: Optional[CountVectorizer] = None,
        decomposition: Optional[TransformerMixin] = None,
        max_iter: int = 200,
        feature_importance: Literal[
            "axial", "angular", "combined"
        ] = "combined",
        random_state: Optional[int] = None,
        trf_kwargs=None,
        encode_kwargs=None,
    ):
        self.n_components = n_components
        self.encoder = encoder
        self.feature_importance = feature_importance
        self.trf_kwargs = trf_kwargs
        self.encode_kwargs = encode_kwargs
        self.load_encoder()
        self.validate_encoder()
        if vectorizer is None:
            self.vectorizer = default_vectorizer()
        else:
            self.vectorizer = vectorizer
        self.max_iter = max_iter
        self.random_state = random_state
        if decomposition is None:
            self.decomposition = FastICA(
                n_components, max_iter=max_iter, random_state=random_state
            )
        else:
            self.decomposition = decomposition

    def estimate_components(
        self, feature_importance: Literal["axial", "angular", "combined"]
    ) -> np.ndarray:
        """Reestimates components based on the chosen feature_importance method."""
        if feature_importance == "axial":
            self.components_ = self.axial_components_
        elif feature_importance == "angular":
            self.components_ = self.angular_components_
        elif feature_importance == "combined":
            self.components_ = (
                np.square(self.axial_components_) * self.angular_components_
            )
        if hasattr(self, "axial_temporal_components_"):
            if feature_importance == "axial":
                self.temporal_components_ = self.axial_temporal_components_
            elif feature_importance == "angular":
                self.temporal_components_ = self.angular_temporal_components_
            elif feature_importance == "combined":
                self.temporal_components_ = (
                    np.square(self.axial_temporal_components_)
                    * self.angular_temporal_components_
                )
        return self.components_

    @property
    def has_negative_side(self) -> bool:
        return True

    def fit_transform(
        self, raw_documents, y=None, embeddings: Optional[np.ndarray] = None
    ) -> np.ndarray:
        console = Console()
        self.embeddings = embeddings
        with console.status("Fitting model") as status:
            if self.embeddings is None:
                status.update("Encoding documents")
                self.embeddings = self.encode_documents(raw_documents)
                console.log("Documents encoded.")
            status.update("Decomposing embeddings")
            if isinstance(self.decomposition, FastICA) and (y is not None):
                warnings.warn(
                    "y is specified but decomposition method is FastICA, which can't use labels. y will be ignored. Use a metric learning method for semi-supervised S^3."
                )
            doc_topic = self.decomposition.fit_transform(self.embeddings, y=y)
            self.topic_vectors_ = self.decomposition.components_
            console.log("Decomposition done.")
            status.update("Extracting terms.")
            vocab = self.vectorizer.fit(raw_documents).get_feature_names_out()
            console.log("Term extraction done.")
            status.update("Encoding vocabulary")
            self.vocab_embeddings = self.encode_documents(vocab)
            if self.vocab_embeddings.shape[1] != self.embeddings.shape[1]:
                raise ValueError(
                    NOT_MATCHING_ERROR.format(
                        n_dims=self.embeddings.shape[1],
                        n_word_dims=self.vocab_embeddings.shape[1],
                    )
                )
            console.log("Vocabulary encoded.")
            status.update("Estimating term importances")
            vocab_topic = self.decomposition.transform(self.vocab_embeddings)
            self.axial_components_ = vocab_topic.T
            if self.feature_importance == "axial":
                self.components_ = self.axial_components_
            elif self.feature_importance == "angular":
                self.components_ = self.angular_components_
            elif self.feature_importance == "combined":
                self.components_ = (
                    np.square(self.axial_components_)
                    * self.angular_components_
                )
            self.top_documents = self.get_top_documents(
                raw_documents, document_topic_matrix=doc_topic
            )
            self.negative_documents = self.get_top_documents(
                raw_documents, document_topic_matrix=doc_topic, positive=False
            )
            self.document_topic_matrix = doc_topic
            self.n_documents_ = doc_topic.shape[0]
            console.log("Model fitting done.")
        return doc_topic

    def fit_supervised(
        self,
        raw_documents,
        y: np.ndarray,
        embeddings: Optional[np.ndarray] = None,
    ):
        console = Console()
        self.embeddings = embeddings
        self.n_components = y.shape[1]
        with console.status("Fitting model") as status:
            if self.embeddings is None:
                status.update("Encoding documents")
                self.embeddings = self.encode_documents(raw_documents)
                console.log("Documents encoded.")
            status.update("Solving OLS")
            self.decomposition = DecompLinearRegression(fit_intercept=False)
            doc_topic = self.decomposition.fit_transform(self.embeddings, y=y)
            self.topic_vectors_ = self.decomposition.components_
            console.log("OLS done.")
            status.update("Extracting terms.")
            vocab = self.vectorizer.fit(raw_documents).get_feature_names_out()
            console.log("Term extraction done.")
            status.update("Encoding vocabulary")
            self.vocab_embeddings = self.encode_documents(vocab)
            if self.vocab_embeddings.shape[1] != self.embeddings.shape[1]:
                raise ValueError(
                    NOT_MATCHING_ERROR.format(
                        n_dims=self.embeddings.shape[1],
                        n_word_dims=self.vocab_embeddings.shape[1],
                    )
                )
            console.log("Vocabulary encoded.")
            status.update("Estimating term importances")
            vocab_topic = self.decomposition.transform(self.vocab_embeddings)
            self.axial_components_ = vocab_topic.T
            if self.feature_importance == "axial":
                self.components_ = self.axial_components_
            elif self.feature_importance == "angular":
                self.components_ = self.angular_components_
            elif self.feature_importance == "combined":
                self.components_ = (
                    np.square(self.axial_components_)
                    * self.angular_components_
                )
            self.top_documents = self.get_top_documents(
                raw_documents, document_topic_matrix=doc_topic
            )
            self.negative_documents = self.get_top_documents(
                raw_documents, document_topic_matrix=doc_topic, positive=False
            )
            self.document_topic_matrix = doc_topic
            console.log("Model fitting done.")
        return self

    def fit_transform_multimodal(
        self,
        raw_documents: list[str],
        images: list[ImageRepr],
        y=None,
        embeddings: Optional[MultimodalEmbeddings] = None,
    ) -> np.ndarray:
        self.validate_embeddings(embeddings)
        console = Console()
        self.images = images
        self.multimodal_embeddings = embeddings
        with console.status("Fitting model") as status:
            if self.multimodal_embeddings is None:
                status.update("Encoding documents")
                self.multimodal_embeddings = self.encode_multimodal(
                    raw_documents, images
                )
                console.log("Documents encoded.")
            self.embeddings = self.multimodal_embeddings["document_embeddings"]
            status.update("Decomposing embeddings")
            if isinstance(self.decomposition, FastICA) and (y is not None):
                warnings.warn(
                    "Supervisory signal is specified but decomposition method is FastICA. y will be ignored. Use a metric learning method for supervised S^3."
                )
            doc_topic = self.decomposition.fit_transform(self.embeddings, y=y)
            self.topic_vectors_ = self.decomposition.components_
            console.log("Decomposition done.")
            status.update("Extracting terms.")
            vocab = self.vectorizer.fit(raw_documents).get_feature_names_out()
            console.log("Term extraction done.")
            status.update("Encoding vocabulary")
            self.vocab_embeddings = self.encode_documents(vocab)
            if self.vocab_embeddings.shape[1] != self.embeddings.shape[1]:
                raise ValueError(
                    NOT_MATCHING_ERROR.format(
                        n_dims=self.embeddings.shape[1],
                        n_word_dims=self.vocab_embeddings.shape[1],
                    )
                )
            console.log("Vocabulary encoded.")
            status.update("Estimating term importances")
            vocab_topic = self.decomposition.transform(self.vocab_embeddings)
            self.axial_components_ = vocab_topic.T
            if self.feature_importance == "axial":
                self.components_ = self.axial_components_
            elif self.feature_importance == "angular":
                self.components_ = self.angular_components_
            elif self.feature_importance == "combined":
                self.components_ = (
                    np.square(self.axial_components_)
                    * self.angular_components_
                )
            console.log("Model fitting done.")
            status.update("Transforming images")
            self.image_topic_matrix = self.transform(
                [], embeddings=self.multimodal_embeddings["image_embeddings"]
            )
            self.top_images = self.collect_top_images(
                images, self.image_topic_matrix
            )
            self.negative_images = self.collect_top_images(
                images, self.image_topic_matrix, negative=True
            )
            self.top_documents = self.get_top_documents(
                raw_documents, document_topic_matrix=doc_topic
            )
            self.negative_documents = self.get_top_documents(
                raw_documents, document_topic_matrix=doc_topic, positive=False
            )
            console.log("Images transformed")
            self.n_documents_ = doc_topic.shape[0]
        return doc_topic

    def plot_topics_with_images(self, n_columns: int = 3, grid_size: int = 4):
        try:
            import plotly.graph_objects as go
        except (ImportError, ModuleNotFoundError) as e:
            raise ModuleNotFoundError(
                "Please install plotly if you intend to use plots in Turftopic."
            ) from e
        fig = go.Figure()
        width, height = 1200, 1200
        scale_factor = 0.25
        w, h = width * scale_factor, height * scale_factor
        padding = 10
        figure_height = (h + padding) * self.n_components
        figure_width = (w + padding) * 2
        fig = fig.add_trace(
            go.Scatter(
                x=[0, figure_width],
                y=[0, figure_height],
                mode="markers",
                marker_opacity=0,
            )
        )
        vocab = self.get_vocab()
        for i, component in enumerate(self.components_):
            positive = vocab[np.argsort(-component)[:7]]
            negative = vocab[np.argsort(component)[:7]]
            pos_image = self._image_grid(
                self.top_images[i],
                (width, height),
                grid_size=(grid_size, grid_size),
            )
            neg_image = self._image_grid(
                self.negative_images[i],
                (width, height),
                grid_size=(grid_size, grid_size),
            )
            x0 = 0
            y0 = (h + padding) * (self.n_components - i)
            fig = fig.add_layout_image(
                dict(
                    x=x0,
                    sizex=w,
                    y=y0,
                    sizey=h,
                    xref="x",
                    yref="y",
                    opacity=1.0,
                    layer="below",
                    sizing="stretch",
                    source=pos_image,
                ),
            )
            fig.add_annotation(
                x=(w / 2),
                y=(h + padding) * (self.n_components - i) - (h / 2),
                text="<b> " + "<br> ".join(positive),
                font=dict(
                    size=16,
                    family="Times New Roman",
                    color="white",
                ),
                bgcolor="rgba(0,0,255, 0.5)",
            )
            x0 = (w + padding) * 1
            fig = fig.add_layout_image(
                dict(
                    x=x0,
                    sizex=w,
                    y=y0,
                    sizey=h,
                    xref="x",
                    yref="y",
                    opacity=1.0,
                    layer="below",
                    sizing="stretch",
                    source=neg_image,
                ),
            )
            fig.add_annotation(
                x=(w + padding) + (w / 2),
                y=(h + padding) * (self.n_components - i) - (h / 2),
                text="<b> " + "<br> ".join(negative),
                font=dict(
                    size=16,
                    family="Times New Roman",
                    color="white",
                ),
                bgcolor="rgba(255,0,0, 0.5)",
            )
        fig = fig.update_xaxes(visible=False, range=[0, figure_width])
        fig = fig.update_yaxes(
            visible=False,
            range=[0, figure_height],
            # the scaleanchor attribute ensures that the aspect ratio stays constant
            scaleanchor="x",
        )
        fig = fig.update_layout(
            width=figure_width,
            height=figure_height,
            margin={"l": 0, "r": 0, "t": 0, "b": 0},
        )
        return fig

    def _rename_automatic(self, analyzer: Analyzer) -> list[str]:
        """Names topics with a topic namer in the model.

        Parameters
        ----------
        analyzer: Analyzer
            A Topic namer model to name topics with.

        Returns
        -------
        list[str]
            List of topic names.
        """
        try:
            positive_documents = self.get_top_documents()
            negative_documents = self.get_top_documents(positive=False)
        except ValueError as e:
            warnings.warn(
                f"Couldn't get top documents, proceeding only with keywords: {e}"
            )
            positive_documents = None
            negative_documents = None
        positive_names = analyzer.name_topics(
            self._top_terms(), documents=positive_documents
        )
        negative_names = analyzer.name_topics(
            self._top_terms(positive=False), documents=negative_documents
        )
        names = []
        for positive, negative in zip(positive_names, negative_names):
            names.append(f"{positive}/{negative}")
        self.topic_names_ = names
        return self.topic_names_

    def refit_transform(
        self,
        raw_documents,
        y=None,
        embeddings: Optional[np.ndarray] = None,
        n_components: Optional[int] = None,
        max_iter: Optional[int] = None,
        random_state: Optional[int] = None,
    ):
        """Refits model with the given parameters.
        This is significantly faster than fitting a new model from scratch.

        Parameters
        ----------
        raw_documents
            Corpus on which the model is based.
        y
            Ignored, exists for API compatibility.
        embeddings
            Ignored, embeddings are already stored, exists for compatibility.
        n_components: int, default None
            Number of topics.
        max_iter: int, default None
            Maximum number of iterations for ICA.
        random_state: int, default None
            Random state to use so that results are exactly reproducible.

        Returns
        -------
        ndarray of shape (n_documents, n_topics)
            Document-topic matrix.
        """
        self.n_components = n_components
        self.topic_names_ = None
        n_components = (
            n_components if n_components is not None else self.n_components
        )
        max_iter = max_iter if max_iter is not None else self.max_iter
        random_state = (
            random_state if random_state is not None else self.random_state
        )
        self.decomposition = FastICA(
            n_components, max_iter=max_iter, random_state=random_state
        )
        console = Console()
        with console.status("Refitting model") as status:
            status.update("Decomposing embeddings")
            doc_topic = self.decomposition.fit_transform(self.embeddings)
            self.topic_vectors_ = self.decomposition.components_
            console.log("Decomposition done.")
            status.update("Estimating term importances")
            vocab_topic = self.decomposition.transform(self.vocab_embeddings)
            self.axial_components_ = vocab_topic.T
            if self.feature_importance == "axial":
                self.components_ = self.axial_components_
            elif self.feature_importance == "angular":
                self.components_ = self.angular_components_
            elif self.feature_importance == "combined":
                self.components_ = (
                    np.square(self.axial_components_)
                    * self.angular_components_
                )
            self.top_documents = self.get_top_documents(
                raw_documents, document_topic_matrix=doc_topic
            )
            self.negative_documents = self.get_top_documents(
                raw_documents, document_topic_matrix=doc_topic, positive=False
            )
            console.log("Model fitting done.")
        return doc_topic

    def fit_transform_dynamic(
        self,
        raw_documents,
        timestamps: list[datetime],
        embeddings: Optional[np.ndarray] = None,
        bins: Union[int, list[datetime]] = 10,
    ) -> np.ndarray:
        document_topic_matrix = self.fit_transform(
            raw_documents, embeddings=embeddings
        )
        time_labels, self.time_bin_edges = self.bin_timestamps(
            timestamps, bins
        )
        n_comp, n_vocab = self.components_.shape
        n_bins = len(self.time_bin_edges) - 1
        self.axial_temporal_components_ = np.full(
            (n_bins, n_comp, n_vocab),
            np.nan,
            dtype=self.components_.dtype,
        )
        self.temporal_importance_ = np.zeros((n_bins, n_comp))
        whitened_embeddings = np.copy(self.embeddings)
        if getattr(self.decomposition, "whiten", False):
            whitened_embeddings -= self.decomposition.mean_
        # doc_topic = np.dot(X, self.components_.T)
        for i_timebin in np.unique(time_labels):
            topic_importances = document_topic_matrix[
                time_labels == i_timebin
            ].mean(axis=0)
            self.temporal_importance_[i_timebin, :] = topic_importances
            t_doc_topic = document_topic_matrix[time_labels == i_timebin]
            t_embeddings = whitened_embeddings[time_labels == i_timebin]
            linreg = LinearRegression().fit(t_embeddings, t_doc_topic)
            self.axial_temporal_components_[i_timebin, :, :] = np.dot(
                self.vocab_embeddings, linreg.coef_.T
            ).T
        self.estimate_components(self.feature_importance)
        return document_topic_matrix

    def refit_transform_dynamic(
        self,
        raw_documents,
        timestamps: list[datetime],
        embeddings=None,
        bins: Union[int, list[datetime]] = 10,
        n_components: Optional[int] = None,
        max_iter: Optional[int] = None,
        random_state: Optional[int] = None,
    ):
        """Refits $S^3$ to be a dynamic model."""
        document_topic_matrix = self.refit_transform(
            raw_documents,
            embeddings=embeddings,
            n_components=n_components,
            max_iter=max_iter,
            random_state=random_state,
        )
        time_labels, self.time_bin_edges = self.bin_timestamps(
            timestamps, bins
        )
        n_comp, n_vocab = self.components_.shape
        n_bins = len(self.time_bin_edges) - 1
        self.axial_temporal_components_ = np.full(
            (n_bins, n_comp, n_vocab),
            np.nan,
            dtype=self.components_.dtype,
        )
        self.temporal_importance_ = np.zeros((n_bins, n_comp))
        whitened_embeddings = np.copy(self.embeddings)
        if getattr(self.decomposition, "whiten"):
            whitened_embeddings -= self.decomposition.mean_
        # doc_topic = np.dot(X, self.components_.T)
        for i_timebin in np.unique(time_labels):
            topic_importances = document_topic_matrix[
                time_labels == i_timebin
            ].mean(axis=0)
            self.temporal_importance_[i_timebin, :] = topic_importances
            t_doc_topic = document_topic_matrix[time_labels == i_timebin]
            t_embeddings = whitened_embeddings[time_labels == i_timebin]
            linreg = LinearRegression().fit(t_embeddings, t_doc_topic)
            self.axial_temporal_components_[i_timebin, :, :] = np.dot(
                self.vocab_embeddings, linreg.coef_.T
            ).T
        self.estimate_components(self.feature_importance)
        return document_topic_matrix

    def refit(
        self,
        raw_documents,
        y=None,
        embeddings=None,
        n_components: Optional[int] = None,
        max_iter: Optional[int] = None,
        random_state: Optional[int] = None,
    ):
        """Refits model with the given parameters.
        This is significantly faster than fitting a new model from scratch.

        Parameters
        ----------
        raw_documents
            Corpus on which the model is based.
        y
            Ignored, exists for API compatibility.
        embeddings
            Ignored, embeddings are already stored, exists for compatibility.
        n_components: int, default None
            Number of topics.
        max_iter: int, default None
            Maximum number of iterations for ICA.
        random_state: int, default None
            Random state to use so that results are exactly reproducible.

        Returns
        -------
        Refitted model.
        """
        self.refit_transform(
            raw_documents,
            y=y,
            embeddings=embeddings,
            n_components=n_components,
            max_iter=max_iter,
            random_state=random_state,
        )
        return self

    @property
    def angular_components_(self):
        """Reweights words based on their angle in ICA-space to the axis
        base vectors.
        """
        if not hasattr(self, "axial_components_"):
            raise NotFittedError("Model has not been fitted yet.")
        word_vectors = self.axial_components_.T
        n_topics = self.axial_components_.shape[0]
        axis_vectors = np.eye(n_topics)
        cosine_components = cosine_similarity(axis_vectors, word_vectors)
        return cosine_components

    @property
    def angular_temporal_components_(self):
        """Reweights words based on their angle in ICA-space to the axis
        base vectors in a dynamic model.
        """
        if not hasattr(self, "axial_temporal_components_"):
            raise NotFittedError("Model has not been fitted dynamically.")
        components = []
        for axial_components in self.axial_temporal_components_:
            word_vectors = axial_components.T
            n_topics = axial_components.shape[0]
            axis_vectors = np.eye(n_topics)
            cosine_components = cosine_similarity(axis_vectors, word_vectors)
            components.append(cosine_components)
        return np.stack(components)

    def transform(
        self, raw_documents, embeddings: Optional[np.ndarray] = None
    ) -> np.ndarray:
        """Infers topic importances for new documents based on a fitted model.

        Parameters
        ----------
        raw_documents: iterable of str
            Documents to fit the model on.
        embeddings: ndarray of shape (n_documents, n_dimensions), optional
            Precomputed document encodings.

        Returns
        -------
        ndarray of shape (n_dimensions, n_topics)
            Document-topic matrix.
        """
        if embeddings is None:
            embeddings = self.encode_documents(raw_documents)
        return self.decomposition.transform(embeddings)

    def merge_with(
        self,
        other: SemanticSignalSeparation,
        match_threshold=0.7,
        merge_method: str | Callable = "asymmetric_mean",
        weighted=True,
    ):
        """Updates topic model by merging it with another one trained on the new data.

        !!! warning:
            Only static properties of a model will be taken into account when merging.
            Attributes associated with top exemplars and temporal data will be **DELETED** from the model.
            The model will be modified in-place.
            We recommend that you save the original model to disk to avoid losing data.

        Parameters
        ----------
        other: SemanticSignalSeparation
            Other SemanticSignalSeparation model to merge this one with.
        match_threshold: float, default 0.7
            Cosine similarity threshold, above which topics are to be considered the same.
        merge_method: {'symmetric_mean', 'asymmetric_mean', 'keep_first'} or Callable, default 'symmetric_mean'
            Method for merging the two topic models.
             - `'symmetric_mean'` Treats the two models as equal, and takes the mean of matching topics.
               Matches are found by building a graph of topic connections based on the similarity threshold.
               Each new topic will be one graph component.
             - `'asymmetric_mean'` Merges new topics into the old topics and takes their mean.
             - `'keep_first'` Keeps all components untouched in the current model,
               and only adds new components from the new model that do not match.
        weighted: bool, default True
            Indicates whether the merge aggregation should be weighted by the number of documents the
            two models have seen.

        Returns
        -------
        Self
        """
        self.update_vocabulary(other)
        if weighted:
            try:
                weights = np.array(
                    [
                        self.n_documents_,
                        other.n_documents_,
                    ]
                )
                self.n_documents_ = self.n_documents_ + other.n_documents_
            except Exception:
                weights = None
        else:
            weights = None
        self.decomposition, self.merge_history_ = merge_fastica(
            self.decomposition,
            other.decomposition,
            merge_method,
            match_threshold,
            weights,
        )
        if hasattr(self, "top_documents"):
            delattr(self, "top_documents")
        if hasattr(self, "top_images"):
            delattr(self, "top_images")
        if hasattr(self, "temporal_components_"):
            delattr(self, "temporal_components_")
        if hasattr(self, "temporal_importance_"):
            delattr(self, "temporal_importance_")
        return self

    @classmethod
    def merge_models(
        cls,
        models: list[SemanticSignalSeparation],
        match_threshold=0.7,
        merge_method: str | Callable = "asymmetric_mean",
        weighted=True,
        return_merge_graph: bool = False,
    ):
        """Merges multiple SemanticSignalSeparation models together.
        The original models will not be modified.

        Parameters
        ----------
        models: list[SemanticSignalSeparation]
            SemanticSignalSeparation models to merge together.
        match_threshold: float, default 0.7
            Cosine similarity threshold, above which topics are to be considered the same.
        merge_method: {'symmetric_mean', 'asymmetric_mean', 'keep_first'} or Callable, default 'symmetric_mean'
            Method for merging the two topic models.
             - `'symmetric_mean'` Treats the two models as equal, and takes the mean of matching topics.
               Matches are found by building a graph of topic connections based on the similarity threshold.
               Each new topic will be one graph component.
             - `'asymmetric_mean'` Merges new topics into the old topics and takes their mean.
             - `'keep_first'` Keeps all components untouched in the current model,
               and only adds new components from the new model that do not match.
        weighted: bool, default True
            Indicates whether the merge aggregation should be weighted by the number of documents the
            two models have seen.
        return_merge_graph: bool, default = False
            Indicates whether the merge graph should be returned.

        Returns
        -------
        SemanticSignalSeparation
            Merged model.
        Graph (OPTIONAL)
            igraph directed Graph over merges.
        """
        return _merge_models(
            models,
            match_threshold,
            merge_method,
            weighted,
            return_merge_graph,
        )

    def print_topics(
        self,
        top_k: int = 10,
        show_scores: bool = False,
        show_negative: bool = False,
    ):
        show_negative = self.has_negative_side
        super().print_topics(top_k, show_scores, show_negative)

    def export_topics(
        self,
        top_k: int = 10,
        show_scores: bool = False,
        show_negative: bool = True,
        format: str = "csv",
    ) -> str:
        show_negative = self.has_negative_side
        return super().export_topics(top_k, show_scores, show_negative, format)

    def print_representative_documents(
        self,
        topic_id,
        raw_documents,
        document_topic_matrix=None,
        top_k=10,
        show_negative: bool = False,
    ):
        show_negative = self.has_negative_side
        super().print_representative_documents(
            topic_id,
            raw_documents,
            document_topic_matrix,
            top_k,
            show_negative,
        )

    def export_representative_documents(
        self,
        topic_id,
        raw_documents,
        document_topic_matrix=None,
        top_k=10,
        show_negative: bool = False,
        format: str = "csv",
    ):
        show_negative = self.has_negative_side
        return super().export_representative_documents(
            topic_id,
            raw_documents,
            document_topic_matrix,
            top_k,
            show_negative,
            format,
        )

    def concept_compass(
        self, topic_x: Union[int, str], topic_y: Union[str, int]
    ):
        """[DEPRECATED] will be removed in version 1.0.0.
        See plot_concept_compass().
        """
        warnings.warn(
            "concept_compass() is deprecated and will be removed in version 1.0.0. Use plot_concept_compass() instead."
        )
        return self.plot_concept_compass(topic_x, topic_y)

    def plot_concept_compass(
        self, topic_x: Union[int, str], topic_y: Union[str, int]
    ):
        """Display a compass of concepts along two semantic axes.
        In order for the plot to be concise and readable, terms are randomly selected on
        a grid of the two topics.

        Parameters
        ----------
        topic_x: int or str
            Index or name of the topic to display on the X axis.
        topic_y: int or str
            Index or name of the topic to display on the Y axis.

        Returns
        -------
        go.Figure
            Plotly interactive plot of the concept compass.
        """
        try:
            import plotly.express as px
        except (ImportError, ModuleNotFoundError) as e:
            raise ModuleNotFoundError(
                "Please install plotly if you intend to use plots in Turftopic."
            ) from e
        if isinstance(topic_x, str):
            try:
                topic_x = list(self.topic_names).index(topic_x)
            except ValueError as e:
                raise ValueError(
                    f"{topic_x} is not a valid topic name or index."
                ) from e
        if isinstance(topic_y, str):
            try:
                topic_y = list(self.topic_names).index(topic_y)
            except ValueError as e:
                raise ValueError(
                    f"{topic_y} is not a valid topic name or index."
                ) from e
        x = self.axial_components_[topic_x]
        y = self.axial_components_[topic_y]
        vocab = self.get_vocab()
        points = np.array(list(zip(x, y)))
        xx, yy = np.meshgrid(
            np.linspace(np.min(x), np.max(x), 20),
            np.linspace(np.min(y), np.max(y), 20),
        )
        coords = np.array(list(zip(np.ravel(xx), np.ravel(yy))))
        coords = coords + np.random.default_rng(0).normal(
            [0, 0], [0.1, 0.1], size=coords.shape
        )
        dist = euclidean_distances(coords, points)
        idxs = np.argmin(dist, axis=1)
        fig = px.scatter(
            x=x[idxs],
            y=y[idxs],
            text=vocab[idxs],
            template="plotly_white",
        )
        fig = fig.update_traces(
            mode="text", textfont_color="black", marker=dict(color="black")
        ).update_layout(
            xaxis_title=f"{self.topic_names[topic_x]}",
            yaxis_title=f"{self.topic_names[topic_y]}",
            font=dict(family="Roboto Mono"),
        )
        fig = fig.update_layout(
            font=dict(family="Roboto Mono", color="black", size=21),
            margin=dict(l=5, r=5, t=5, b=5),
        )
        fig = fig.add_hline(y=0, line_color="black", line_width=4)
        fig = fig.add_vline(x=0, line_color="black", line_width=4)
        return fig

    def plot_image_compass(
        self, topic_x: Union[str, int], topic_y: Union[str, int]
    ):
        try:
            import plotly.express as px
        except (ImportError, ModuleNotFoundError) as e:
            raise ModuleNotFoundError(
                "Please install plotly if you intend to use plots in Turftopic."
            ) from e
        top_images = getattr(self, "top_images", None)
        if top_images is None:
            raise ValueError(
                "Topic model is not multimodal. Can't plot image compass."
            )
        if isinstance(topic_x, str):
            try:
                topic_x = list(self.topic_names).index(topic_x)
            except ValueError as e:
                raise ValueError(
                    f"{topic_x} is not a valid topic name or index."
                ) from e
        if isinstance(topic_y, str):
            try:
                topic_y = list(self.topic_names).index(topic_y)
            except ValueError as e:
                raise ValueError(
                    f"{topic_y} is not a valid topic name or index."
                ) from e
        x = self.image_topic_matrix[:, topic_x]
        y = self.image_topic_matrix[:, topic_y]
        points = np.array(list(zip(x, y)))
        xx, yy = np.meshgrid(
            np.linspace(np.min(x), np.max(x), 8),
            np.linspace(np.min(y), np.max(y), 8),
        )
        coords = np.array(list(zip(np.ravel(xx), np.ravel(yy))))
        dist = euclidean_distances(coords, points)
        idxs = np.argmin(dist, axis=1)
        fig = px.scatter(
            x=x[idxs],
            y=y[idxs],
            template="plotly_white",
        )
        sizex = (max(x) - min(x)) / 10
        sizey = (max(y) - min(y)) / 10
        for i in np.unique(idxs):
            fig.add_layout_image(
                dict(
                    name=f"image{i}",
                    source=self.images[i],
                    x=x[i],
                    y=y[i],
                    xref="x",
                    yref="y",
                    xanchor="right",
                    yanchor="top",
                    layer="above",
                    sizex=sizex,
                    sizey=sizey,
                )
            )
        fig = fig.update_traces(
            mode="markers", textfont_color="black", marker=dict(opacity=0)
        ).update_layout(
            xaxis_title=f"{self.topic_names[topic_x]}",
            yaxis_title=f"{self.topic_names[topic_y]}",
            font=dict(family="Roboto Mono"),
        )
        fig = fig.update_layout(
            font=dict(family="Roboto Mono", color="black", size=21),
            margin=dict(l=5, r=5, t=5, b=5),
        )
        fig = fig.update_xaxes(range=[min(x), max(x)])
        fig = fig.update_yaxes(range=[min(y), max(y)])
        return fig

    def plot_topics_over_time(self, top_k: int = 6):
        try:
            import plotly.graph_objects as go
        except (ImportError, ModuleNotFoundError) as e:
            raise ModuleNotFoundError(
                "Please install plotly if you intend to use plots in Turftopic."
            ) from e
        fig = go.Figure()
        vocab = self.get_vocab()
        n_topics = self.temporal_components_.shape[1]
        try:
            topic_names = self.topic_names
        except AttributeError:
            topic_names = [f"Topic {i}" for i in range(n_topics)]
        for i_topic, topic_imp_t in enumerate(self.temporal_importance_.T):
            component_over_time = self.temporal_components_[:, i_topic, :]
            name_over_time = []
            for component, importance in zip(component_over_time, topic_imp_t):
                if importance < 0:
                    component = -component
                top = np.argpartition(-component, top_k)[:top_k]
                values = component[top]
                if np.all(values == 0) or np.all(np.isnan(values)):
                    name_over_time.append("<not present>")
                    continue
                top = top[np.argsort(-values)]
                name_over_time.append(", ".join(vocab[top]))
            times = self.time_bin_edges[:-1]
            fig.add_trace(
                go.Scatter(
                    x=times,
                    y=topic_imp_t,
                    mode="markers+lines",
                    text=name_over_time,
                    name=topic_names[i_topic],
                    hovertemplate="<b>%{text}</b>",
                    marker=dict(
                        line=dict(width=2, color="black"),
                        size=14,
                    ),
                    line=dict(width=3),
                )
            )
        fig.add_hline(y=0, line_dash="dash", opacity=0.5)
        fig.update_layout(
            template="plotly_white",
            hoverlabel=dict(font_size=16, bgcolor="white"),
            hovermode="x",
            font=dict(family="Roboto Mono"),
        )
        fig.update_xaxes(title="Time Slice Start")
        fig.update_yaxes(title="Topic Importance")
        return fig

    def _topics_over_time(
        self,
        top_k: int = 5,
        show_scores: bool = False,
        date_format: str = "%Y %m %d",
    ) -> list[list[str]]:
        temporal_components = self.temporal_components_
        slices = self.get_time_slices()
        slice_names = []
        for start_dt, end_dt in slices:
            start_str = start_dt.strftime(date_format)
            end_str = end_dt.strftime(date_format)
            slice_names.append(f"{start_str} - {end_str}")
        n_topics = self.temporal_components_.shape[1]
        try:
            topic_names = self.topic_names
        except AttributeError:
            topic_names = [f"Topic {i}" for i in range(n_topics)]
        columns = []
        rows = []
        columns.append("Time Slice")
        for topic in topic_names:
            columns.append(topic)
        for slice_name, components, weights in zip(
            slice_names, temporal_components, self.temporal_importance_
        ):
            fields = []
            fields.append(slice_name)
            vocab = self.get_vocab()
            for component, weight in zip(components, weights):
                if np.all(component == 0) or np.all(np.isnan(component)):
                    fields.append("Topic not present.")
                    continue
                if weight < 0:
                    component = -component
                top = np.argpartition(-component, top_k)[:top_k]
                importance = component[top]
                top = top[np.argsort(-importance)]
                top = top[importance != 0]
                scores = component[top]
                words = vocab[top]
                if show_scores:
                    concat_words = ", ".join(
                        [
                            f"{word}({importance:.2f})"
                            for word, importance in zip(words, scores)
                        ]
                    )
                else:
                    concat_words = ", ".join([word for word in words])
                fields.append(concat_words)
            rows.append(fields)
        return [columns, *rows]

angular_components_ property

Reweights words based on their angle in ICA-space to the axis base vectors.

angular_temporal_components_ property

Reweights words based on their angle in ICA-space to the axis base vectors in a dynamic model.

concept_compass(topic_x, topic_y)

[DEPRECATED] will be removed in version 1.0.0. See plot_concept_compass().

Source code in turftopic/models/decomp.py
933
934
935
936
937
938
939
940
941
942
def concept_compass(
    self, topic_x: Union[int, str], topic_y: Union[str, int]
):
    """[DEPRECATED] will be removed in version 1.0.0.
    See plot_concept_compass().
    """
    warnings.warn(
        "concept_compass() is deprecated and will be removed in version 1.0.0. Use plot_concept_compass() instead."
    )
    return self.plot_concept_compass(topic_x, topic_y)

estimate_components(feature_importance)

Reestimates components based on the chosen feature_importance method.

Source code in turftopic/models/decomp.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def estimate_components(
    self, feature_importance: Literal["axial", "angular", "combined"]
) -> np.ndarray:
    """Reestimates components based on the chosen feature_importance method."""
    if feature_importance == "axial":
        self.components_ = self.axial_components_
    elif feature_importance == "angular":
        self.components_ = self.angular_components_
    elif feature_importance == "combined":
        self.components_ = (
            np.square(self.axial_components_) * self.angular_components_
        )
    if hasattr(self, "axial_temporal_components_"):
        if feature_importance == "axial":
            self.temporal_components_ = self.axial_temporal_components_
        elif feature_importance == "angular":
            self.temporal_components_ = self.angular_temporal_components_
        elif feature_importance == "combined":
            self.temporal_components_ = (
                np.square(self.axial_temporal_components_)
                * self.angular_temporal_components_
            )
    return self.components_

merge_models(models, match_threshold=0.7, merge_method='asymmetric_mean', weighted=True, return_merge_graph=False) classmethod

Merges multiple SemanticSignalSeparation models together. The original models will not be modified.

Parameters:

Name Type Description Default
models list[SemanticSignalSeparation]

SemanticSignalSeparation models to merge together.

required
match_threshold

Cosine similarity threshold, above which topics are to be considered the same.

0.7
merge_method str | Callable

Method for merging the two topic models. - 'symmetric_mean' Treats the two models as equal, and takes the mean of matching topics. Matches are found by building a graph of topic connections based on the similarity threshold. Each new topic will be one graph component. - 'asymmetric_mean' Merges new topics into the old topics and takes their mean. - 'keep_first' Keeps all components untouched in the current model, and only adds new components from the new model that do not match.

'asymmetric_mean'
weighted

Indicates whether the merge aggregation should be weighted by the number of documents the two models have seen.

True
return_merge_graph bool

Indicates whether the merge graph should be returned.

False

Returns:

Type Description
SemanticSignalSeparation

Merged model.

Graph(OPTIONAL)

igraph directed Graph over merges.

Source code in turftopic/models/decomp.py
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
@classmethod
def merge_models(
    cls,
    models: list[SemanticSignalSeparation],
    match_threshold=0.7,
    merge_method: str | Callable = "asymmetric_mean",
    weighted=True,
    return_merge_graph: bool = False,
):
    """Merges multiple SemanticSignalSeparation models together.
    The original models will not be modified.

    Parameters
    ----------
    models: list[SemanticSignalSeparation]
        SemanticSignalSeparation models to merge together.
    match_threshold: float, default 0.7
        Cosine similarity threshold, above which topics are to be considered the same.
    merge_method: {'symmetric_mean', 'asymmetric_mean', 'keep_first'} or Callable, default 'symmetric_mean'
        Method for merging the two topic models.
         - `'symmetric_mean'` Treats the two models as equal, and takes the mean of matching topics.
           Matches are found by building a graph of topic connections based on the similarity threshold.
           Each new topic will be one graph component.
         - `'asymmetric_mean'` Merges new topics into the old topics and takes their mean.
         - `'keep_first'` Keeps all components untouched in the current model,
           and only adds new components from the new model that do not match.
    weighted: bool, default True
        Indicates whether the merge aggregation should be weighted by the number of documents the
        two models have seen.
    return_merge_graph: bool, default = False
        Indicates whether the merge graph should be returned.

    Returns
    -------
    SemanticSignalSeparation
        Merged model.
    Graph (OPTIONAL)
        igraph directed Graph over merges.
    """
    return _merge_models(
        models,
        match_threshold,
        merge_method,
        weighted,
        return_merge_graph,
    )

merge_with(other, match_threshold=0.7, merge_method='asymmetric_mean', weighted=True)

Updates topic model by merging it with another one trained on the new data.

!!! warning: Only static properties of a model will be taken into account when merging. Attributes associated with top exemplars and temporal data will be DELETED from the model. The model will be modified in-place. We recommend that you save the original model to disk to avoid losing data.

Parameters:

Name Type Description Default
other SemanticSignalSeparation

Other SemanticSignalSeparation model to merge this one with.

required
match_threshold

Cosine similarity threshold, above which topics are to be considered the same.

0.7
merge_method str | Callable

Method for merging the two topic models. - 'symmetric_mean' Treats the two models as equal, and takes the mean of matching topics. Matches are found by building a graph of topic connections based on the similarity threshold. Each new topic will be one graph component. - 'asymmetric_mean' Merges new topics into the old topics and takes their mean. - 'keep_first' Keeps all components untouched in the current model, and only adds new components from the new model that do not match.

'asymmetric_mean'
weighted

Indicates whether the merge aggregation should be weighted by the number of documents the two models have seen.

True

Returns:

Type Description
Self
Source code in turftopic/models/decomp.py
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
def merge_with(
    self,
    other: SemanticSignalSeparation,
    match_threshold=0.7,
    merge_method: str | Callable = "asymmetric_mean",
    weighted=True,
):
    """Updates topic model by merging it with another one trained on the new data.

    !!! warning:
        Only static properties of a model will be taken into account when merging.
        Attributes associated with top exemplars and temporal data will be **DELETED** from the model.
        The model will be modified in-place.
        We recommend that you save the original model to disk to avoid losing data.

    Parameters
    ----------
    other: SemanticSignalSeparation
        Other SemanticSignalSeparation model to merge this one with.
    match_threshold: float, default 0.7
        Cosine similarity threshold, above which topics are to be considered the same.
    merge_method: {'symmetric_mean', 'asymmetric_mean', 'keep_first'} or Callable, default 'symmetric_mean'
        Method for merging the two topic models.
         - `'symmetric_mean'` Treats the two models as equal, and takes the mean of matching topics.
           Matches are found by building a graph of topic connections based on the similarity threshold.
           Each new topic will be one graph component.
         - `'asymmetric_mean'` Merges new topics into the old topics and takes their mean.
         - `'keep_first'` Keeps all components untouched in the current model,
           and only adds new components from the new model that do not match.
    weighted: bool, default True
        Indicates whether the merge aggregation should be weighted by the number of documents the
        two models have seen.

    Returns
    -------
    Self
    """
    self.update_vocabulary(other)
    if weighted:
        try:
            weights = np.array(
                [
                    self.n_documents_,
                    other.n_documents_,
                ]
            )
            self.n_documents_ = self.n_documents_ + other.n_documents_
        except Exception:
            weights = None
    else:
        weights = None
    self.decomposition, self.merge_history_ = merge_fastica(
        self.decomposition,
        other.decomposition,
        merge_method,
        match_threshold,
        weights,
    )
    if hasattr(self, "top_documents"):
        delattr(self, "top_documents")
    if hasattr(self, "top_images"):
        delattr(self, "top_images")
    if hasattr(self, "temporal_components_"):
        delattr(self, "temporal_components_")
    if hasattr(self, "temporal_importance_"):
        delattr(self, "temporal_importance_")
    return self

plot_concept_compass(topic_x, topic_y)

Display a compass of concepts along two semantic axes. In order for the plot to be concise and readable, terms are randomly selected on a grid of the two topics.

Parameters:

Name Type Description Default
topic_x Union[int, str]

Index or name of the topic to display on the X axis.

required
topic_y Union[str, int]

Index or name of the topic to display on the Y axis.

required

Returns:

Type Description
Figure

Plotly interactive plot of the concept compass.

Source code in turftopic/models/decomp.py
 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
def plot_concept_compass(
    self, topic_x: Union[int, str], topic_y: Union[str, int]
):
    """Display a compass of concepts along two semantic axes.
    In order for the plot to be concise and readable, terms are randomly selected on
    a grid of the two topics.

    Parameters
    ----------
    topic_x: int or str
        Index or name of the topic to display on the X axis.
    topic_y: int or str
        Index or name of the topic to display on the Y axis.

    Returns
    -------
    go.Figure
        Plotly interactive plot of the concept compass.
    """
    try:
        import plotly.express as px
    except (ImportError, ModuleNotFoundError) as e:
        raise ModuleNotFoundError(
            "Please install plotly if you intend to use plots in Turftopic."
        ) from e
    if isinstance(topic_x, str):
        try:
            topic_x = list(self.topic_names).index(topic_x)
        except ValueError as e:
            raise ValueError(
                f"{topic_x} is not a valid topic name or index."
            ) from e
    if isinstance(topic_y, str):
        try:
            topic_y = list(self.topic_names).index(topic_y)
        except ValueError as e:
            raise ValueError(
                f"{topic_y} is not a valid topic name or index."
            ) from e
    x = self.axial_components_[topic_x]
    y = self.axial_components_[topic_y]
    vocab = self.get_vocab()
    points = np.array(list(zip(x, y)))
    xx, yy = np.meshgrid(
        np.linspace(np.min(x), np.max(x), 20),
        np.linspace(np.min(y), np.max(y), 20),
    )
    coords = np.array(list(zip(np.ravel(xx), np.ravel(yy))))
    coords = coords + np.random.default_rng(0).normal(
        [0, 0], [0.1, 0.1], size=coords.shape
    )
    dist = euclidean_distances(coords, points)
    idxs = np.argmin(dist, axis=1)
    fig = px.scatter(
        x=x[idxs],
        y=y[idxs],
        text=vocab[idxs],
        template="plotly_white",
    )
    fig = fig.update_traces(
        mode="text", textfont_color="black", marker=dict(color="black")
    ).update_layout(
        xaxis_title=f"{self.topic_names[topic_x]}",
        yaxis_title=f"{self.topic_names[topic_y]}",
        font=dict(family="Roboto Mono"),
    )
    fig = fig.update_layout(
        font=dict(family="Roboto Mono", color="black", size=21),
        margin=dict(l=5, r=5, t=5, b=5),
    )
    fig = fig.add_hline(y=0, line_color="black", line_width=4)
    fig = fig.add_vline(x=0, line_color="black", line_width=4)
    return fig

refit(raw_documents, y=None, embeddings=None, n_components=None, max_iter=None, random_state=None)

Refits model with the given parameters. This is significantly faster than fitting a new model from scratch.

Parameters:

Name Type Description Default
raw_documents

Corpus on which the model is based.

required
y

Ignored, exists for API compatibility.

None
embeddings

Ignored, embeddings are already stored, exists for compatibility.

None
n_components Optional[int]

Number of topics.

None
max_iter Optional[int]

Maximum number of iterations for ICA.

None
random_state Optional[int]

Random state to use so that results are exactly reproducible.

None

Returns:

Type Description
Refitted model.
Source code in turftopic/models/decomp.py
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
def refit(
    self,
    raw_documents,
    y=None,
    embeddings=None,
    n_components: Optional[int] = None,
    max_iter: Optional[int] = None,
    random_state: Optional[int] = None,
):
    """Refits model with the given parameters.
    This is significantly faster than fitting a new model from scratch.

    Parameters
    ----------
    raw_documents
        Corpus on which the model is based.
    y
        Ignored, exists for API compatibility.
    embeddings
        Ignored, embeddings are already stored, exists for compatibility.
    n_components: int, default None
        Number of topics.
    max_iter: int, default None
        Maximum number of iterations for ICA.
    random_state: int, default None
        Random state to use so that results are exactly reproducible.

    Returns
    -------
    Refitted model.
    """
    self.refit_transform(
        raw_documents,
        y=y,
        embeddings=embeddings,
        n_components=n_components,
        max_iter=max_iter,
        random_state=random_state,
    )
    return self

refit_transform(raw_documents, y=None, embeddings=None, n_components=None, max_iter=None, random_state=None)

Refits model with the given parameters. This is significantly faster than fitting a new model from scratch.

Parameters:

Name Type Description Default
raw_documents

Corpus on which the model is based.

required
y

Ignored, exists for API compatibility.

None
embeddings Optional[ndarray]

Ignored, embeddings are already stored, exists for compatibility.

None
n_components Optional[int]

Number of topics.

None
max_iter Optional[int]

Maximum number of iterations for ICA.

None
random_state Optional[int]

Random state to use so that results are exactly reproducible.

None

Returns:

Type Description
ndarray of shape (n_documents, n_topics)

Document-topic matrix.

Source code in turftopic/models/decomp.py
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
def refit_transform(
    self,
    raw_documents,
    y=None,
    embeddings: Optional[np.ndarray] = None,
    n_components: Optional[int] = None,
    max_iter: Optional[int] = None,
    random_state: Optional[int] = None,
):
    """Refits model with the given parameters.
    This is significantly faster than fitting a new model from scratch.

    Parameters
    ----------
    raw_documents
        Corpus on which the model is based.
    y
        Ignored, exists for API compatibility.
    embeddings
        Ignored, embeddings are already stored, exists for compatibility.
    n_components: int, default None
        Number of topics.
    max_iter: int, default None
        Maximum number of iterations for ICA.
    random_state: int, default None
        Random state to use so that results are exactly reproducible.

    Returns
    -------
    ndarray of shape (n_documents, n_topics)
        Document-topic matrix.
    """
    self.n_components = n_components
    self.topic_names_ = None
    n_components = (
        n_components if n_components is not None else self.n_components
    )
    max_iter = max_iter if max_iter is not None else self.max_iter
    random_state = (
        random_state if random_state is not None else self.random_state
    )
    self.decomposition = FastICA(
        n_components, max_iter=max_iter, random_state=random_state
    )
    console = Console()
    with console.status("Refitting model") as status:
        status.update("Decomposing embeddings")
        doc_topic = self.decomposition.fit_transform(self.embeddings)
        self.topic_vectors_ = self.decomposition.components_
        console.log("Decomposition done.")
        status.update("Estimating term importances")
        vocab_topic = self.decomposition.transform(self.vocab_embeddings)
        self.axial_components_ = vocab_topic.T
        if self.feature_importance == "axial":
            self.components_ = self.axial_components_
        elif self.feature_importance == "angular":
            self.components_ = self.angular_components_
        elif self.feature_importance == "combined":
            self.components_ = (
                np.square(self.axial_components_)
                * self.angular_components_
            )
        self.top_documents = self.get_top_documents(
            raw_documents, document_topic_matrix=doc_topic
        )
        self.negative_documents = self.get_top_documents(
            raw_documents, document_topic_matrix=doc_topic, positive=False
        )
        console.log("Model fitting done.")
    return doc_topic

refit_transform_dynamic(raw_documents, timestamps, embeddings=None, bins=10, n_components=None, max_iter=None, random_state=None)

Refits \(S^3\) to be a dynamic model.

Source code in turftopic/models/decomp.py
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
def refit_transform_dynamic(
    self,
    raw_documents,
    timestamps: list[datetime],
    embeddings=None,
    bins: Union[int, list[datetime]] = 10,
    n_components: Optional[int] = None,
    max_iter: Optional[int] = None,
    random_state: Optional[int] = None,
):
    """Refits $S^3$ to be a dynamic model."""
    document_topic_matrix = self.refit_transform(
        raw_documents,
        embeddings=embeddings,
        n_components=n_components,
        max_iter=max_iter,
        random_state=random_state,
    )
    time_labels, self.time_bin_edges = self.bin_timestamps(
        timestamps, bins
    )
    n_comp, n_vocab = self.components_.shape
    n_bins = len(self.time_bin_edges) - 1
    self.axial_temporal_components_ = np.full(
        (n_bins, n_comp, n_vocab),
        np.nan,
        dtype=self.components_.dtype,
    )
    self.temporal_importance_ = np.zeros((n_bins, n_comp))
    whitened_embeddings = np.copy(self.embeddings)
    if getattr(self.decomposition, "whiten"):
        whitened_embeddings -= self.decomposition.mean_
    # doc_topic = np.dot(X, self.components_.T)
    for i_timebin in np.unique(time_labels):
        topic_importances = document_topic_matrix[
            time_labels == i_timebin
        ].mean(axis=0)
        self.temporal_importance_[i_timebin, :] = topic_importances
        t_doc_topic = document_topic_matrix[time_labels == i_timebin]
        t_embeddings = whitened_embeddings[time_labels == i_timebin]
        linreg = LinearRegression().fit(t_embeddings, t_doc_topic)
        self.axial_temporal_components_[i_timebin, :, :] = np.dot(
            self.vocab_embeddings, linreg.coef_.T
        ).T
    self.estimate_components(self.feature_importance)
    return document_topic_matrix

transform(raw_documents, embeddings=None)

Infers topic importances for new documents based on a fitted model.

Parameters:

Name Type Description Default
raw_documents

Documents to fit the model on.

required
embeddings Optional[ndarray]

Precomputed document encodings.

None

Returns:

Type Description
ndarray of shape (n_dimensions, n_topics)

Document-topic matrix.

Source code in turftopic/models/decomp.py
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
def transform(
    self, raw_documents, embeddings: Optional[np.ndarray] = None
) -> np.ndarray:
    """Infers topic importances for new documents based on a fitted model.

    Parameters
    ----------
    raw_documents: iterable of str
        Documents to fit the model on.
    embeddings: ndarray of shape (n_documents, n_dimensions), optional
        Precomputed document encodings.

    Returns
    -------
    ndarray of shape (n_dimensions, n_topics)
        Document-topic matrix.
    """
    if embeddings is None:
        embeddings = self.encode_documents(raw_documents)
    return self.decomposition.transform(embeddings)