Skip to content

pelinker.model

EntityPredictionRow

Bases: TypedDict

Row shape for predict entities entries from clustering (before optional KB fields).

Source code in pelinker/model.py
class EntityPredictionRow(TypedDict):
    """Row shape for ``predict`` ``entities`` entries from clustering (before optional KB fields)."""

    mention: str
    a: int | None
    b: int | None
    a_abs: int | None
    b_abs: int | None
    itext: int | None
    ichunk: int | None
    word_grouping: WordGrouping | None
    lemma: str
    entity_id_predicted: str
    score: float
    pca_residual: float
    pca_mahalanobis: float
    pca_spectral_entropy: float
    anomaly_score_max_z: float
    projection_score: float

Linker

Source code in pelinker/model.py
 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
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
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
class Linker:
    def __init__(
        self,
        transformer: EmbeddingTransformer | None = None,
        clusterer: hdbscan.HDBSCAN | None = None,
        transform_config: TransformConfig | None = None,
        embedding_metadata: EmbeddingModelMetadata | None = None,
        kb_config: KBConfig | None = None,
        **kwargs,
    ):
        self.transformer: EmbeddingTransformer | None = transformer
        self.clusterer: hdbscan.HDBSCAN | None = clusterer
        self.cluster_assignments: dict[str, int] = {}
        self.transform_config: TransformConfig | None = transform_config
        self.embedding_metadata: EmbeddingModelMetadata | None = embedding_metadata
        self.kb_config: KBConfig | None = kb_config

        self.vocabulary: list[str] = []
        self.labels_map: dict[str, str] = kwargs.pop("labels_map", dict())
        self.training_cluster_frame: pd.DataFrame | None = None
        self.training_pca_residuals: np.ndarray | None = None
        self.training_pca_mahalanobis: np.ndarray | None = None
        self.training_pca_spectral_entropy: np.ndarray | None = None
        self.training_umap_clustering: np.ndarray | None = None
        self.training_cluster_viz: np.ndarray | None = None
        self.training_pca_reduced: np.ndarray | None = None
        self.cluster_composition: ClusterCompositionSnapshot | None = None
        self.cluster_consensus_names: dict[int, str] = {}
        self.cluster_derived_labels_map: dict[str, str] = {}
        self.kb_in_labels_map: dict[str, str] = {}
        self.kb_in_entity_clusters: dict[str, int] = {}
        self.cluster_id_to_entity_id: dict[int, str] = {}
        self.kb_out_catalog: dict[str, object] | None = None
        self.screener: NegativeClassScreener | None = None
        self.screener_in_sample_metrics: NegativeScreenerInSampleMetrics | None = None
        self.clustering_fit_metrics: ClusteringFitMetrics | None = None
        self.projection: ManifoldOovScoreModel | None = kwargs.pop("projection", None)
        self._projection_cv_payload: dict[str, object] | None = None
        self.entity_head: EntityHead | None = kwargs.pop("entity_head", None)
        self.predict_mode: str = kwargs.pop("predict_mode", "legacy")
        self.min_cluster_size_provenance: MinClusterSizeProvenance | None = kwargs.pop(
            "min_cluster_size_provenance", None
        )
        self.distillation_fidelity: DistillationFidelityMetrics | None = kwargs.pop(
            "distillation_fidelity", None
        )
        self._hf_tokenizer = None
        self._hf_model = None
        self._hf_models_by_type: dict[str, tuple[object, object]] = {}
        self.nlp_model_name: str = kwargs.pop("nlp_model_name", "en_core_web_trf")
        self._nlp: object | None = None
        self._fit_clustering_report: ModelSelectionReport | None = None

    @staticmethod
    def filter_entities(
        entities: list[dict[str, object]], thr_score: float
    ) -> list[dict[str, object]]:
        return [r for r in entities if float(r.get("score", 0.0)) >= thr_score]

    @classmethod
    def filter_report(
        cls, report: dict[str, object], thr_score: float
    ) -> dict[str, object]:
        """Return a shallow copy with ``entities`` filtered by score (does not mutate ``report``)."""
        raw_entities = report.get("entities", [])
        entities = raw_entities if isinstance(raw_entities, list) else []
        filtered = cls.filter_entities(
            cast(list[dict[str, object]], entities), thr_score
        )
        return {**report, "entities": filtered}

    @staticmethod
    def _merge_prediction_fields_into_debug_mentions(
        debug_rows: list[dict[str, object]],
        predictions: list[dict[str, object]],
        *,
        include_kb_validation_fields: bool,
    ) -> None:
        keys_always: tuple[str, ...] = (
            "entity_id_predicted",
            "score",
            "kb_training_entity",
            "pca_spectral_entropy",
            "projection_score",
        )
        keys_when_validation: tuple[str, ...] = (
            "kb_training_entity_from_lemma",
            "kb_training_entity_for_prediction",
            "lemma_kb_matches_predicted_entity",
        )
        for row in predictions:
            mi = row.get("mention_source_index")
            if not isinstance(mi, int) or mi < 0 or mi >= len(debug_rows):
                continue
            target = debug_rows[mi]
            for k in keys_always:
                if k in row:
                    target[k] = row[k]
            if include_kb_validation_fields:
                for k in keys_when_validation:
                    if k in row:
                        target[k] = row[k]

    def dump(self, file_spec: str | pathlib.Path) -> None:
        self._fit_clustering_report = None
        path = _linker_artifact_gz_path(file_spec)
        path.parent.mkdir(parents=True, exist_ok=True)

        manifold = None
        if self.transformer is not None and is_parametric_umap(self.transformer.umap):
            manifold = self.transformer.umap
            # Avoid pickling Keras nets inside joblib; restore after dump.
            self.transformer.umap = None
            sidecar = parametric_umap_sidecar_dir(path)
            save_clustering_manifold(manifold, sidecar)
            logger.info("Wrote ParametricUMAP sidecar to %s", sidecar)

        try:
            joblib.dump(self, path, compress=3)
        finally:
            if manifold is not None and self.transformer is not None:
                self.transformer.umap = manifold

    @classmethod
    def load(cls, file_spec: str | pathlib.Path) -> Linker:
        path = _linker_artifact_gz_path(file_spec)
        pe_model = joblib.load(path)
        for field, default in _LINKER_LOAD_DEFAULTS.items():
            if field not in pe_model.__dict__:
                setattr(pe_model, field, default)
        sidecar = parametric_umap_sidecar_dir(path)
        if pe_model.transformer is not None and pe_model.transformer.umap is None:
            if sidecar.is_dir():
                pe_model.transformer.umap = load_clustering_manifold(sidecar)
                logger.info("Loaded ParametricUMAP sidecar from %s", sidecar)
        return pe_model

    def take_fit_clustering_report(self) -> ModelSelectionReport | None:
        """
        Consume the :class:`~pelinker.reporting.ClusteringReport` produced by the last :meth:`fit`.

        Call **before** :meth:`dump` if you need JSON or other persistence: the report is
        not serialized on the linker artifact (only prediction state is pickled).

        Returns ``None`` if :meth:`fit` has not been run, the report was already taken, or
        clustering state was incomplete.
        """
        report = self._fit_clustering_report
        self._fit_clustering_report = None
        return report

    def _strip_training_metrics_for_prediction(self) -> None:
        """Drop mention-level training tables and manifold arrays; keep predict-time fields."""
        self.training_cluster_frame = None
        self.training_pca_residuals = None
        self.training_pca_mahalanobis = None
        self.training_pca_spectral_entropy = None
        self.training_umap_clustering = None
        self.training_cluster_viz = None
        self.training_pca_reduced = None
        self._projection_cv_payload = None

    def build_clustering_report(
        self,
        *,
        training_diagnostics: LinkerFitDiagnostics | None = None,
    ) -> ModelSelectionReport | None:
        """
        Build a :class:`~pelinker.reporting.ClusteringReport` when full training rows exist.

        After a normal :meth:`fit`, heavy training payloads are removed for prediction; use
        :meth:`take_fit_clustering_report` immediately after fitting instead.

        This method remains useful for **legacy** pickled linkers that still embed training
        arrays, or for tests that skip stripping.

        Args:
            training_diagnostics: Optional stratified-sampled mention-level diagnostics
                (PCA quality + screener / manifold OOV scores) attached only to the fit report.
        """
        tcf = self.training_cluster_frame
        if (
            tcf is None
            or self.training_pca_residuals is None
            or self.training_pca_mahalanobis is None
            or self.training_pca_spectral_entropy is None
            or self.training_umap_clustering is None
            or self.training_cluster_viz is None
            or self.training_pca_reduced is None
            or self.clustering_fit_metrics is None
        ):
            return None
        n = len(tcf)
        if (
            len(self.training_pca_residuals) != n
            or len(self.training_pca_mahalanobis) != n
            or len(self.training_pca_spectral_entropy) != n
            or self.training_umap_clustering.shape[0] != n
            or self.training_cluster_viz.shape[0] != n
            or self.training_pca_reduced.shape[0] != n
        ):
            return None

        m = self.clustering_fit_metrics
        dbcv_f = float(m.dbcv) if m.dbcv is not None else float("nan")
        ari_val = float(m.ari) if m.ari is not None else float("nan")
        metrics_df = pd.DataFrame(
            [
                {
                    "min_cluster_size": m.min_cluster_size,
                    "icm": float("nan"),
                    "n_clusters": m.n_clusters_emergent,
                    "dbcv": dbcv_f,
                    "ari": ari_val,
                }
            ]
        )

        base_cols = ["entity", "cluster", "pmid", "mention"]
        optional_cols = [
            *MENTION_PROVENANCE_COLUMNS,
            "screener_score",
            "projection_score",
            "cluster_score",
            "clustering_in_sample",
            "screener_pass",
            "manifold_oov_pass",
        ]
        keep = [c for c in base_cols + optional_cols if c in tcf.columns]
        assignments = tcf[keep].copy()

        number_properties = int(tcf["entity"].nunique())

        res_f = np.asarray(self.training_pca_residuals, dtype=np.float64)
        mah_f = np.asarray(self.training_pca_mahalanobis, dtype=np.float64)
        ent_f = np.asarray(self.training_pca_spectral_entropy, dtype=np.float64)

        neg_lbl = (
            self.screener.negative_label
            if self.screener is not None
            else NEGATIVE_LABEL
        )
        y_neg = entity_negative_label_mask_01(tcf["entity"], neg_lbl)

        return ModelSelectionReport(
            hyperparameters=ClusteringHyperparameters(
                min_cluster_size=m.min_cluster_size
            ),
            best_score=dbcv_f,
            number_properties=number_properties,
            n_clusters_emergent=m.n_clusters_emergent,
            metrics_df=metrics_df,
            assignments=assignments,
            pca_residuals=res_f,
            pca_mahalanobis=mah_f,
            pca_spectral_entropy=ent_f,
            oov_label=y_neg,
            umap_clustering=np.asarray(self.training_umap_clustering, dtype=np.float64),
            cluster_viz=np.asarray(self.training_cluster_viz, dtype=np.float64),
            cluster_viz_method=(
                self.transform_config.cluster_viz_method
                if self.transform_config is not None
                else "pca"
            ),
            pca_reduced=np.asarray(self.training_pca_reduced, dtype=np.float64),
            all_screener_cv=None,
            screener_oos_datapoints=None,
            ari=m.ari,
            training_diagnostics=training_diagnostics,
            distillation_fidelity=self.distillation_fidelity,
            min_cluster_size_provenance=self.min_cluster_size_provenance,
            n_rows_realized=(
                None
                if self.min_cluster_size_provenance is None
                else self.min_cluster_size_provenance.n_rows_realized
            ),
        )

    @staticmethod
    def _normalize_embedding_paths(
        embeddings: pathlib.Path | Sequence[pathlib.Path],
    ) -> list[pathlib.Path]:
        if isinstance(embeddings, pathlib.Path):
            return [embeddings.expanduser()]
        return [pathlib.Path(p).expanduser() for p in embeddings]

    def fit(
        self,
        embeddings: pathlib.Path | Sequence[pathlib.Path] | None,
        transform_config: TransformConfig,
        min_cluster_size: int | None = None,
        *,
        fit_config: LinkerFitConfig | None = None,
        embedding_training: EmbeddingTrainingConfig | None = None,
        embedding_metadata: EmbeddingModelMetadata | None = None,
        kb_config: KBConfig | None = None,
        kb_out_naming: KbOutNamingConfig | None = None,
        kb_in_labels_map_path: str | None = None,
    ) -> Linker:
        """
        Fit the Linker model with embeddings.

        This method handles two main parts:
        a) Loading and processing embeddings (from file or direct array)
        b) Fitting the negative screener, then PCA/UMAP + HDBSCAN on non-negative rows

        Args:
            embeddings: Path or sequence of paths to parquet file(s) (mention-level rows:
                        ``pmid``, ``entity``, ``mention``, ``embed``). Multiple files are
                        fused like :func:`~pelinker.selection.load_selection_frame` (inner join
                        on keys, concat embeddings). Order must match
                        ``embedding_metadata.sources``. If None, ``embed_kb_corpus`` is run
                        (one output file per source).
            transform_config: TransformConfig instance
            min_cluster_size: HDBSCAN ``min_cluster_size`` (choose upstream, e.g. via
                ``pelinker.model_selection``). When ``None``, it is resolved from
                ``fit_config.scale_curve`` against the realized manifold row count, or
                falls back to :data:`~pelinker.scaling.DEFAULT_MIN_CLUSTER_SIZE`. An
                explicit value always wins; either way the choice and its origin land on
                ``min_cluster_size_provenance`` and in the fit report.
            fit_config: Parquet read batching, mention load filters, subsample settings, and screener config.
                Defaults to :class:`LinkerFitConfig()`.
            embedding_training: Corpus paths and embedding runtime. Required when embeddings=None.
            embedding_metadata: If provided, overrides or sets ``self.embedding_metadata`` for
                this fit (required when embeddings=None unless already set on the linker).
            kb_config: Knowledge-base metadata stored on the linker; ``entity_count`` is set
                from fitted vocabulary when omitted (None).

        Side effects:
            Sets ``cluster_composition`` (mention-weighted property mass and per-cluster
            mixtures), ``cluster_consensus_names`` (short labels from those mixtures),
            ``screener_in_sample_metrics``, and ``clustering_fit_metrics``. Mention-level
            training tables and manifold arrays used for :class:`~pelinker.reporting.ClusteringReport`
            are stripped after each fit; persist JSON with :meth:`take_fit_clustering_report` and
            :func:`~pelinker.reporting.write_clustering_report_json` at
            :func:`~pelinker.reporting.linker_fit_clustering_report_path` (same layout as
            ``pelinker-fit`` ``report_path``) before :meth:`dump`.

        Returns:
            self
        """
        if min_cluster_size is not None and min_cluster_size < 2:
            raise ValueError("min_cluster_size must be >= 2")

        is_temporary = False
        embeddings_paths: list[pathlib.Path] = []

        try:
            if embedding_metadata is not None:
                self.embedding_metadata = embedding_metadata

            fc = fit_config or LinkerFitConfig()
            load_cfg = fc.to_clustering_sample_config()

            if embeddings is None:
                if embedding_training is None:
                    raise ValueError(
                        "embedding_training is required when embeddings is None. "
                        "Provide embeddings path or EmbeddingTrainingConfig(...)."
                    )
                self.nlp_model_name = embedding_training.nlp_model
                if self.embedding_metadata is None:
                    raise ValueError(
                        "embedding_metadata is required when embeddings is None "
                        "(set on Linker(...) or pass embedding_metadata=... to fit())."
                    )
                k = len(self.embedding_metadata.sources)
                for _ in range(k):
                    tf = tempfile.NamedTemporaryFile(suffix=".parquet", delete=False)
                    embeddings_paths.append(pathlib.Path(tf.name))
                    tf.close()

                logger.info("Stage (a): Embedding corpus (%s source(s))...", k)
                embed_kb_corpus(
                    metadata=self.embedding_metadata,
                    training=embedding_training,
                    output_parquet_paths=embeddings_paths,
                )
                is_temporary = True
            else:
                embeddings_paths = self._normalize_embedding_paths(embeddings)
                if self.embedding_metadata is not None and len(embeddings_paths) != len(
                    self.embedding_metadata.sources
                ):
                    raise ValueError(
                        "Number of embedding parquet paths must match "
                        f"embedding_metadata.sources ({len(self.embedding_metadata.sources)}), "
                        f"got {len(embeddings_paths)}"
                    )
                logger.info(
                    "Stage (A): Using provided embeddings (%s file(s)): %s",
                    len(embeddings_paths),
                    embeddings_paths,
                )

            logger.info(
                "Stage (B): mention-level load from %s parquet file(s)",
                len(embeddings_paths),
            )
            prepared = load_selection_frame(
                file_paths=embeddings_paths,
                config=load_cfg,
                show_embedding_read_progress=True,
            )
            if prepared is None or len(prepared) == 0:
                raise ValueError(
                    "No mention-level embedding rows loaded from parquet (check paths "
                    "and columns pmid, entity, mention, embed)."
                )

            self._fit_clustering_on_prepared_mentions(
                prepared=prepared,
                transform_config=transform_config,
                fit_cfg=fc,
                min_cluster_size=min_cluster_size,
                kb_config=kb_config,
                kb_out_naming=kb_out_naming,
                kb_in_labels_map_path=kb_in_labels_map_path,
            )

            return self
        finally:
            if is_temporary:
                for p in embeddings_paths:
                    try:
                        p.unlink()
                        logger.debug("Removed temporary parquet file: %s", p)
                    except Exception as e:
                        logger.warning(
                            "Failed to remove temporary parquet file %s: %s", p, e
                        )

    def _fit_clustering_on_prepared_mentions(
        self,
        *,
        prepared: pd.DataFrame,
        transform_config: TransformConfig,
        fit_cfg: LinkerFitConfig,
        min_cluster_size: int | None,
        kb_config: KBConfig | None,
        kb_out_naming: KbOutNamingConfig | None = None,
        kb_in_labels_map_path: str | None = None,
    ) -> None:
        """Fit screeners, then PCA/UMAP + HDBSCAN on the clustering subsample; label full KB via predict."""
        prepared = prepared.copy()
        prepared[_ROW_ID_COL] = np.arange(len(prepared), dtype=np.int64)

        ns_cfg = fit_cfg.ambient_screener
        mo_cfg = fit_cfg.projection_screener
        neg_label = ns_cfg.negative_label

        sample_cfg = fit_cfg.to_clustering_sample_config()
        clustering_prepared = draw_selection_sample(
            prepared,
            sample_cfg,
            sample_index=fit_cfg.clustering_sample_index,
        )

        screener_prepared = _screener_training_frame(
            prepared,
            fit_cfg,
            negative_label=neg_label,
            clustering_prepared=clustering_prepared,
        )
        _, manifold_screener = split_by_negative_label(screener_prepared, neg_label)

        neg_step = _fit_ambient_screener_step(prepared, screener_prepared, ns_cfg)
        self.screener = neg_step.screener
        self.screener_in_sample_metrics = neg_step.in_sample_metrics

        _, manifold_fit = split_by_negative_label(clustering_prepared, neg_label)
        if len(manifold_fit) == 0:
            raise ValueError(
                "No rows left after excluding negative-label mentions for manifold fit"
            )

        # Resolve min_cluster_size against the rows HDBSCAN will actually see, now that
        # every load filter and the clustering subsample have been applied.
        min_cluster_size, self.min_cluster_size_provenance = resolve_min_cluster_size(
            explicit=min_cluster_size,
            n_rows_realized=len(manifold_fit),
            scale_curve=fit_cfg.scale_curve,
        )
        _log_min_cluster_size_provenance(self.min_cluster_size_provenance)

        _, manifold_full = split_by_negative_label(prepared, neg_label)
        if len(manifold_full) == 0:
            raise ValueError(
                "No rows left after excluding negative-label mentions for manifold fit"
            )

        transform_config = _align_manifold_kind_with_predict_mode(
            transform_config, predict_mode=fit_cfg.predict_mode
        )
        self.transform_config = transform_config
        self.predict_mode = fit_cfg.predict_mode

        cl_result = fit_manifold_clustering(
            manifold_fit,
            transform_config=transform_config,
            min_cluster_size=min_cluster_size,
            prediction_data=True,
        )
        self.transformer = cl_result.transformer
        self.clusterer = cl_result.clusterer
        self.clustering_fit_metrics = cl_result.fit_metrics

        full_artifacts = score_transform_artifacts(
            manifold_full,
            cl_result.transformer,
            include_umap=True,
        )
        _store_training_manifold_arrays(self, full_artifacts)

        mo_step = _fit_projection_step(
            screener_prepared,
            manifold_screener,
            self.transformer,
            ns_cfg,
            mo_cfg,
        )
        self.projection = mo_step.model
        self._projection_cv_payload = mo_step.cv_payload

        built_mo_diag: tuple[np.ndarray, np.ndarray, np.ndarray] | None = None
        if mo_cfg.enabled:
            built_mo_diag = build_projection_training_arrays(
                prepared,
                manifold_full,
                self.transformer,
                negative_label=neg_label,
            )

        full_diag = _linker_fit_diagnostics_full(
            prepared=prepared,
            negative_label=neg_label,
            screener_decision=neg_step.decision,
            transformer=self.transformer,
            built_mo=built_mo_diag,
            mo_model=self.projection,
            sample_random_state=fit_cfg.diagnostics_random_state,
        )
        sampled_diag = subsample_diagnostics_stratified(
            full_diag,
            max_rows=fit_cfg.diagnostics_sample_size,
            random_state=fit_cfg.diagnostics_random_state,
        )

        neg_mask = prepared["entity"].astype(str).values == neg_label
        manifold_mask = ~neg_mask
        cluster_labels, cluster_scores = _predict_cluster_labels_on_full_manifold(
            manifold_full,
            manifold_fit,
            cl_result.clusterer,
            full_artifacts.umap_clustering,
            cl_result.cluster_labels,
        )
        clustering_in_sample, screener_pass, manifold_oov_pass = (
            _cluster_viz_membership_masks(
                manifold_full,
                manifold_fit,
                screener=neg_step.screener,
                pca_residuals=full_artifacts.pca_residuals,
                pca_mahalanobis=full_artifacts.pca_mahalanobis,
                pca_spectral_entropy=full_artifacts.pca_spectral_entropy,
                projection_model=mo_step.model,
            )
        )
        self.training_cluster_frame = _build_training_cluster_frame(
            manifold_full,
            cluster_labels,
            cluster_scores,
            neg_step.decision,
            manifold_mask,
            full_diag.projection_score,
            clustering_in_sample=clustering_in_sample,
            screener_pass=screener_pass,
            manifold_oov_pass=manifold_oov_pass,
        )

        if fit_cfg.predict_mode == "compact":
            self.entity_head, self.distillation_fidelity = (
                _fit_entity_head_with_fidelity(
                    manifold_full=manifold_full,
                    umap_full=full_artifacts.umap_clustering,
                    teacher_labels=cluster_labels,
                    teacher_scores=cluster_scores,
                    exact_label_mask=clustering_in_sample,
                    fit_cfg=fit_cfg,
                )
            )
            # Compact artifacts do not ship HDBSCAN prediction_data.
            self.clusterer = None
        else:
            self.entity_head = None
            self.distillation_fidelity = None

        _finalize_linker_cluster_state(
            self,
            kb_config=kb_config,
            sampled_diag=sampled_diag,
            fit_provenance=KbOutFitProvenance(
                min_cluster_size=min_cluster_size,
                clustering_sample_index=fit_cfg.clustering_sample_index,
                seed=fit_cfg.base_seed,
            ),
            kb_out_naming=kb_out_naming,
            kb_in_labels_map_path=kb_in_labels_map_path,
        )

    def _load_embeddings_from_file(
        self, embeddings_path: pathlib.Path, kb_labels: set[str] | None = None
    ) -> tuple[np.ndarray, list[str]]:
        """Backward-compatible single-file loader; delegates to fused multi-file path."""
        return self._load_fused_embeddings_from_files([embeddings_path], kb_labels)

    def _load_fused_embeddings_from_files(
        self,
        embeddings_paths: Sequence[pathlib.Path],
        kb_labels: set[str] | None = None,
        *,
        read_config: ClusteringOptimizationConfig | None = None,
    ) -> tuple[np.ndarray, list[str]]:
        """
        Per-file per-property mean embeddings, intersection across sources, concat features.

        Legacy helper for property-level loads; ``Linker.fit`` uses mention-level fusion instead.
        """
        cfg = read_config or ClusteringOptimizationConfig()
        logger.info(
            "Reading %s parquet source(s) and fusing per-property vectors...",
            len(embeddings_paths),
        )
        fused = fused_property_vectors_from_paths(
            embeddings_paths,
            kb_labels,
            batch_size=cfg.batch_size,
            show_read_progress=sys.stdout.isatty(),
        )
        if not fused:
            raise ValueError("No fused property vectors (empty intersection or inputs)")

        dfr = property_fused_dataframe_for_linker_order(fused, self.labels_map)
        if len(dfr) == 0:
            raise ValueError("No valid embeddings after mapping to entity_ids")

        embeddings = np.stack([np.asarray(e, dtype=np.float64) for e in dfr["embed"]])
        entity_ids = list(dfr["entity_id"])
        logger.info(
            "Embedded %s KB properties into %s-dimensional fused vectors",
            len(embeddings),
            embeddings.shape[1],
        )
        return embeddings, entity_ids

    def _ensure_hf_models_for_sources(self, *, use_gpu: bool = False) -> None:
        """Load tokenizer+encoder once per distinct ``model_type`` in metadata sources."""
        if self.embedding_metadata is None:
            raise ValueError(
                "embedding_metadata is required for predict(); set it during fit() or on the Linker."
            )
        for src in self.embedding_metadata.sources:
            mt = src.model_type
            if mt not in self._hf_models_by_type:
                logger.info("Loading encoder for predict: %s", mt)
                self._hf_models_by_type[mt] = load_models(mt, sentence=False)
        if use_gpu and torch.cuda.is_available():
            for _mt, (_tok, model) in self._hf_models_by_type.items():
                model.to("cuda")
        elif use_gpu:
            logger.warning("CUDA is not available; predict runs on CPU")

    def _ensure_nlp(self) -> object:
        """Lazy-load the spaCy pipeline used for word tokenization (same role as training ``nlp_model``)."""
        if self._nlp is None:
            import spacy

            logger.info("Loading spaCy model %r for predict()", self.nlp_model_name)
            self._nlp = spacy.load(self.nlp_model_name)
        return self._nlp

    @staticmethod
    def _mention_tensor_lists_aligned(
        lists: list[list[torch.Tensor]],
    ) -> None:
        n0 = len(lists[0])
        for i, lst in enumerate(lists[1:], start=1):
            if len(lst) != n0:
                raise ValueError(
                    f"Mention tensor count mismatch between fused sources: "
                    f"source 0 has {n0}, source {i} has {len(lst)}. "
                    "Use the same model_type for all sources if spans must align."
                )

    @staticmethod
    def _zscore(values: np.ndarray) -> np.ndarray:
        v = np.asarray(values, dtype=np.float64)
        if v.size == 0:
            return np.array([], dtype=np.float64)
        mean = float(v.mean())
        std = float(v.std())
        if std <= 1e-12:
            return np.zeros_like(v, dtype=np.float64)
        return (v - mean) / std

    @staticmethod
    def _mention_interval_half_open(
        row: dict[str, object],
    ) -> tuple[int, int, int] | None:
        """Document character interval ``[start, end)`` for overlap tests, or ``None``."""
        it = row.get("itext")
        if it is None:
            return None
        it_i = int(it)
        aa = row.get("a_abs")
        bb = row.get("b_abs")
        if aa is not None and bb is not None:
            return (it_i, int(aa), int(bb))
        aa = row.get("a")
        bb = row.get("b")
        if aa is not None and bb is not None:
            return (it_i, int(aa), int(bb))
        return None

    @staticmethod
    def _char_intervals_overlap(
        u: tuple[int, int, int], v: tuple[int, int, int]
    ) -> bool:
        if u[0] != v[0]:
            return False
        _, a1, b1 = u
        _, a2, b2 = v
        return a1 < b2 and a2 < b1

    @staticmethod
    def _span_extent_chars(row: dict[str, object]) -> int:
        iv = Linker._mention_interval_half_open(row)
        if iv is not None:
            return max(iv[2] - iv[1], 0)
        return len(str(row.get("mention", "")))

    @staticmethod
    def _entity_prediction_row(
        item: MentionCandidate,
        *,
        entity_id_predicted: str,
        cluster_membership_prob: float,
        pca_residual: float,
        pca_mahalanobis: float,
        pca_spectral_entropy: float,
        anomaly_score_max_z: float,
        projection_score: float,
    ) -> EntityPredictionRow:
        """Merge mention span fields with clustering outputs; ``score`` is cluster soft membership."""
        base = dataclasses.asdict(item)
        out = cast(EntityPredictionRow, dict(base))
        out["entity_id_predicted"] = entity_id_predicted
        out["score"] = cluster_membership_prob
        out["pca_residual"] = pca_residual
        out["pca_mahalanobis"] = pca_mahalanobis
        out["pca_spectral_entropy"] = pca_spectral_entropy
        out["anomaly_score_max_z"] = anomaly_score_max_z
        out["projection_score"] = projection_score
        return out

    @staticmethod
    def _dedupe_overlapping_prediction_rows(
        rows: list[EntityPredictionRow],
    ) -> list[EntityPredictionRow]:
        """Drop redundant W1/W2/W3 windows that cover the same text region.

        Rows without a usable ``(itext, …)`` interval are never merged with others.

        Overlap is union of intersecting half-open character intervals on the same
        document. Within each connected component, keep the row with highest
        ``score``; ties prefer a shorter span, then lexicographic ``mention``.

        Returned rows are sorted by ``(itext, a_abs or a)`` for stable output.
        """
        n = len(rows)
        if n <= 1:
            return rows

        intervals: list[tuple[int, int, int] | None] = [
            Linker._mention_interval_half_open(r) for r in rows
        ]
        parent = list(range(n))

        def find(i: int) -> int:
            while parent[i] != i:
                parent[i] = parent[parent[i]]
                i = parent[i]
            return i

        def union(i: int, j: int) -> None:
            ri, rj = find(i), find(j)
            if ri != rj:
                parent[ri] = rj

        for i in range(n):
            for j in range(i + 1, n):
                ui, uj = intervals[i], intervals[j]
                if ui is None or uj is None:
                    continue
                if Linker._char_intervals_overlap(ui, uj):
                    union(i, j)

        comp_members: dict[int, list[int]] = defaultdict(list)
        for i in range(n):
            comp_members[find(i)].append(i)

        chosen: list[EntityPredictionRow] = []
        for members in comp_members.values():

            def rank_key(idx: int) -> tuple[float, int, str]:
                r = rows[idx]
                return (
                    -float(r["score"]),
                    Linker._span_extent_chars(r),
                    str(r["mention"]),
                )

            best = min(members, key=rank_key)
            chosen.append(rows[best])

        def sort_key(r: EntityPredictionRow) -> tuple[int, int]:
            it = r.get("itext")
            it_i = int(it) if it is not None else -1
            aa = r.get("a_abs")
            if aa is not None:
                return it_i, int(aa)
            aa = r.get("a")
            return it_i, int(aa) if aa is not None else -1

        chosen.sort(key=sort_key)
        return chosen

    def training_anomaly_metric_summary(self) -> dict[str, dict[str, float]] | None:
        """Quantile summary from stored per-mention PCA metrics (legacy pickles only after fit)."""
        if (
            self.training_pca_residuals is None
            or self.training_pca_mahalanobis is None
            or self.training_pca_spectral_entropy is None
            or len(self.training_pca_residuals) == 0
            or len(self.training_pca_mahalanobis) == 0
        ):
            return None

        residual = np.asarray(self.training_pca_residuals, dtype=np.float64)
        mahal = np.asarray(self.training_pca_mahalanobis, dtype=np.float64)
        entropy = np.asarray(self.training_pca_spectral_entropy, dtype=np.float64)
        combined = np.maximum.reduce(
            [
                self._zscore(residual),
                self._zscore(mahal),
                self._zscore(entropy),
            ]
        )
        quantiles = [0.5, 0.9, 0.95, 0.99]

        def _q(values: np.ndarray) -> dict[str, float]:
            return {
                f"q{int(q * 100):02d}": float(np.quantile(values, q)) for q in quantiles
            }

        return {
            "residual": _q(residual),
            "mahalanobis": _q(mahal),
            "spectral_entropy": _q(entropy),
            "combined_max_z": _q(combined),
        }

    def _encode_mentions(
        self,
        texts: Sequence[str],
        max_length: int | None,
        *,
        use_gpu: bool,
    ) -> tuple[torch.Tensor | None, list[MentionCandidate], object]:
        """Run encoders + spaCy and build the fused mention tensor and mention rows.

        Returns ``(fused_tensor, mentions, primary_report_batch)``. ``fused_tensor`` is
        ``None`` when no mentions were extracted. Each mention row carries
        chunk-local bounds ``a``/``b``, absolute bounds ``a_abs``/``b_abs``, ``itext``,
        ``ichunk``, ``word_grouping`` and ``lemma`` (space-joined token lemmas, used for
        KB-match lookups).

        Mentions are filtered with :func:`~pelinker.util.keep_expression_for_prediction`
        (drop windows containing punctuation; drop windows whose tokens are all stop
        words).
        """
        if self.embedding_metadata is None:
            raise ValueError(
                "embedding_metadata is required; set it during fit() or on the Linker."
            )
        self._ensure_hf_models_for_sources(use_gpu=use_gpu)
        nlp = self._ensure_nlp()
        resolved_max_length = max_length if max_length is not None else MAX_LENGTH

        word_groupings = [WordGrouping.W1, WordGrouping.W2, WordGrouping.W3]
        report_batches: list = []
        for src in self.embedding_metadata.sources:
            tok, model = self._hf_models_by_type[src.model_type]
            rb = texts_to_vrep(
                list(texts),
                tok,
                model,
                src.layers_spec,
                word_groupings,
                nlp,
                max_length=resolved_max_length,
            )
            report_batches.append(rb)

        primary = report_batches[0]
        tt_lists = [
            extract_ordered_mention_tensors(rb, keep=keep_expression_for_prediction)
            for rb in report_batches
        ]
        self._mention_tensor_lists_aligned(tt_lists)

        mentions: list[MentionCandidate] = []
        for wg in word_groupings:
            if wg not in primary.available_groupings():
                continue
            expression_container = primary[wg]
            for expr_holder in expression_container.expression_data:
                for expr, _tt in zip(expr_holder.expressions, expr_holder.tt):
                    if not keep_expression_for_prediction(expr):
                        continue
                    mention_text = ""
                    offset: int | None = None
                    if (
                        expr.itext is not None
                        and expr.itext < len(primary.texts)
                        and expr.a is not None
                        and expr.b is not None
                    ):
                        text = primary.texts[expr.itext]
                        if expr.ichunk is not None:
                            offset = primary.chunk_mapper.map_chunk_to_text(
                                expr.itext, expr.ichunk
                            )
                            mention_text = text[offset + expr.a : offset + expr.b]
                        else:
                            mention_text = text[expr.a : expr.b]
                    lemma = " ".join(t.lemma for t in expr.tokens)
                    mentions.append(
                        MentionCandidate(
                            mention=mention_text,
                            a=expr.a,
                            b=expr.b,
                            a_abs=(
                                expr.a + offset
                                if expr.a is not None and offset is not None
                                else None
                            ),
                            b_abs=(
                                expr.b + offset
                                if expr.b is not None and offset is not None
                                else None
                            ),
                            itext=expr.itext,
                            ichunk=expr.ichunk,
                            word_grouping=wg,
                            lemma=lemma,
                        )
                    )

        if not tt_lists[0]:
            return None, mentions, primary

        fused_rows: list[torch.Tensor] = []
        for i in range(len(tt_lists[0])):
            fused_rows.append(
                torch.cat([tts[i] for tts in tt_lists], dim=-1),
            )
        tt = torch.stack(fused_rows, dim=0)
        return tt, mentions, primary

    def predict(
        self,
        texts: Sequence[str],
        max_length: int | None = None,
        threshold: float = DEFAULT_CLUSTER_MEMBERSHIP_THRESHOLD,
        *,
        use_gpu: bool = False,
        include_mention_anomaly: bool = False,
        include_debug_mentions: bool = False,
        include_prediction_kb_validation: bool = False,
    ) -> LinkerPredictResult:
        """
        Predict entities for input texts.

        With multiple ``embedding_metadata.sources``, runs ``texts_to_vrep`` per source
        (cached by ``model_type``), concatenates mention tensors along the feature axis in
        source order, then applies the fitted transformer and clusterer. Mention counts
        must match across sources (typically the same ``model_type`` for all sources).

        Tokenization uses the spaCy pipeline named by ``nlp_model_name`` (set from
        ``EmbeddingTrainingConfig.nlp_model`` during corpus embedding, else default
        ``en_core_web_trf``).

        Each ``entities`` row includes ``score``: HDBSCAN approximate cluster
        membership probability from ``approximate_predict`` on UMAP coordinates.
        The ``threshold`` argument drops rows whose ``score`` is below that minimum
        (default :data:`DEFAULT_CLUSTER_MEMBERSHIP_THRESHOLD`). Cluster ``-1``
        (HDBSCAN noise) is always treated as nil and omitted.

        When ``include_mention_anomaly`` or ``include_debug_mentions`` is true,
        :attr:`LinkerPredictResult.debug_mentions` lists one diagnostic row per extracted
        mention (same single encode and PCA→UMAP pass as predictions). Use
        :meth:`LinkerPredictResult.to_dict` with ``include_debug=True`` to emit the legacy
        ``mention_anomaly`` key for JSON consumers.

        ``kb_training_entity`` (human label from ``labels_map`` for the predicted id)
        is attached only when mention-debug or KB validation is requested, not on the
        default prediction path.

        When ``include_prediction_kb_validation`` is true, each row in ``entities`` gains
        validation-only fields comparing mention lemmas to KB training ``entity`` labels
        (same index as training-time matching): ``kb_training_entity_from_lemma``,
        ``kb_training_entity_for_prediction``, ``lemma_kb_matches_predicted_entity``.
        When debug rows are also returned, those fields are copied onto the matching
        mention row via ``mention_source_index``.
        """
        want_debug = include_mention_anomaly or include_debug_mentions
        tt, mentions, primary = self._encode_mentions(
            texts, max_length, use_gpu=use_gpu
        )

        if tt is None:
            return LinkerPredictResult(
                entities=[],
                debug_mentions=[] if want_debug else None,
            )

        kb_lemma_by_wg: dict[WordGrouping, dict[str, str]] | None = None
        if want_debug or include_prediction_kb_validation:
            nlp = self._ensure_nlp()
            kb_lemma_by_wg = self._kb_lemma_index_by_wg(nlp)

        predictions, mention_anomaly = self._predict_with_clustering(
            tt,
            mentions,
            threshold=threshold,
            mention_anomaly_rows=want_debug,
            kb_lemma_by_wg=kb_lemma_by_wg,
        )

        if include_prediction_kb_validation:
            if kb_lemma_by_wg is None:
                raise ValueError(
                    "kb_lemma_by_wg missing for include_prediction_kb_validation "
                    "(internal error: index should have been built)"
                )
            enrich_entity_predictions_kb_validation(
                cast(list[dict[str, object]], predictions),
                kb_lemma_by_wg,
                self.labels_map,
            )

        preds_obj = cast(list[dict[str, object]], predictions)
        if want_debug or include_prediction_kb_validation:
            for row in preds_obj:
                eid = row.get("entity_id_predicted")
                row["kb_training_entity"] = (
                    self.labels_map.get(str(eid)) if eid is not None else None
                )

        if mention_anomaly is not None:
            self._merge_prediction_fields_into_debug_mentions(
                mention_anomaly,
                preds_obj,
                include_kb_validation_fields=include_prediction_kb_validation,
            )

        for item in preds_obj:
            item.pop("lemma", None)

        return LinkerPredictResult(
            entities=preds_obj,
            debug_mentions=mention_anomaly,
        )

    def _build_mention_anomaly_rows(
        self,
        mentions: list[MentionCandidate],
        screener_neg: np.ndarray,
        screener_margin: np.ndarray,
        residuals: np.ndarray,
        mahalanobis: np.ndarray,
        spectral_entropy: np.ndarray,
        combined: np.ndarray,
        projection_scores: np.ndarray,
        kb_lemma_by_wg: dict[WordGrouping, dict[str, str]],
    ) -> list[dict[str, object]]:
        rows: list[dict[str, object]] = []
        for i, item in enumerate(mentions):
            wg = item.word_grouping
            lemma = item.lemma
            kb_property = lookup_kb_training_entity_label(
                wg if isinstance(wg, WordGrouping) else None,
                str(lemma),
                kb_lemma_by_wg,
            )
            rows.append(
                {
                    "mention": item.mention,
                    "a": item.a,
                    "b": item.b,
                    "a_abs": item.a_abs,
                    "b_abs": item.b_abs,
                    "itext": item.itext,
                    "ichunk": item.ichunk,
                    "word_grouping": wg.name if isinstance(wg, WordGrouping) else None,
                    "lemma": lemma,
                    "is_kb_match": kb_property is not None,
                    "kb_property_match": kb_property,
                    "pca_residual": float(residuals[i]),
                    "pca_mahalanobis": float(mahalanobis[i]),
                    "pca_spectral_entropy": float(spectral_entropy[i]),
                    "anomaly_score_max_z": float(combined[i]),
                    "projection_score": float(projection_scores[i]),
                    "screener_is_negative": bool(screener_neg[i]),
                    "screener_decision": float(screener_margin[i]),
                }
            )
        return rows

    def _predict_with_clustering(
        self,
        embeddings: torch.Tensor,
        mentions: list[MentionCandidate],
        threshold: float = DEFAULT_CLUSTER_MEMBERSHIP_THRESHOLD,
        *,
        mention_anomaly_rows: bool = False,
        kb_lemma_by_wg: dict[WordGrouping, dict[str, str]] | None = None,
    ) -> tuple[list[EntityPredictionRow], list[dict[str, object]] | None]:
        """
        Predict entities using clustering approach.

        Mentions classified as negative by the screener are dropped immediately: no
        PCA/UMAP, no cluster assignment, and no anomaly metrics for them.

        Each entity row includes ``score``: MLP ``max(predict_proba)`` in compact mode,
        or HDBSCAN soft cluster membership from ``approximate_predict`` in legacy mode
        (same scale as ``threshold``).

        Args:
            embeddings: Tensor of shape (n_mentions, embedding_dim)
            mentions: Extracted mention candidates in row order with ``embeddings``.
            threshold: Minimum cluster membership probability required to return
                a prediction (compared to each row's ``score``).

        Returns:
            ``(entity_predictions, mention_anomaly_rows_or_none)``. Anomaly rows are
            returned only when ``mention_anomaly_rows`` is true (requires
            ``kb_lemma_by_wg``).
        """
        if self.transformer is None or self.screener is None:
            raise ValueError(
                "Screener and Transformer must be fitted before prediction"
            )
        use_head = self.entity_head is not None
        if not use_head and self.clusterer is None:
            raise ValueError(
                "Either entity_head (compact) or clusterer (legacy) must be fitted "
                "before prediction"
            )
        if mention_anomaly_rows and kb_lemma_by_wg is None:
            raise ValueError(
                "kb_lemma_by_wg is required when mention_anomaly_rows is true"
            )

        # Convert to numpy
        embeddings_np = embeddings.detach().cpu().numpy()

        screener_neg = self.screener.predict_is_negative(embeddings_np)
        idx_keep = np.flatnonzero(~screener_neg)
        screener_margin: np.ndarray | None
        if mention_anomaly_rows:
            screener_margin = self.screener.decision_function(embeddings_np)
        else:
            screener_margin = None

        candidates: list[EntityPredictionRow] = []
        n_mentions = len(mentions)

        if len(idx_keep) == 0:
            if mention_anomaly_rows:
                assert screener_margin is not None
                assert kb_lemma_by_wg is not None
                nan_vec = np.full(n_mentions, np.nan, dtype=np.float64)
                return [], self._build_mention_anomaly_rows(
                    mentions,
                    screener_neg,
                    screener_margin,
                    nan_vec,
                    nan_vec,
                    nan_vec,
                    nan_vec,
                    nan_vec,
                    kb_lemma_by_wg,
                )
            return [], None

        emb_k = embeddings_np[idx_keep]
        _umap_k, _, res_k, mah_k, ent_k = self.transformer.transform(emb_k)
        if use_head:
            assert self.entity_head is not None
            cl_k, cp_k = self.entity_head.predict(_umap_k)
        else:
            assert self.clusterer is not None
            cl_k, cp_k = approximate_predict(self.clusterer, _umap_k)
        cl_arr = cl_k.astype(np.int64, copy=False)
        cp_arr = np.asarray(cp_k, dtype=np.float64).ravel()
        combined_k = np.maximum.reduce(
            [
                self._zscore(res_k),
                self._zscore(mah_k),
                self._zscore(ent_k),
            ]
        )
        mo = self.projection
        if mo is not None:
            X3 = np.column_stack(
                [
                    np.asarray(res_k, dtype=np.float64),
                    np.asarray(mah_k, dtype=np.float64),
                    np.asarray(ent_k, dtype=np.float64),
                ]
            )
            oov_scores_k = mo.score(X3)
            oov_gate_k = mo.is_oov(X3)
        else:
            oov_scores_k = np.full(len(idx_keep), np.nan, dtype=np.float64)
            oov_gate_k = np.zeros(len(idx_keep), dtype=bool)

        for j, mention_i in enumerate(idx_keep):
            item = mentions[int(mention_i)]
            if bool(oov_gate_k[j]):
                continue
            cluster_id = int(cl_arr[j])
            cluster_prob = float(cp_arr[j])
            # Skip HDBSCAN outliers and low-confidence assignments.
            if cluster_id == -1 or cluster_prob < threshold:
                continue

            # Resolve KB-out entity for this HDBSCAN cluster.
            predicted_entity = self.cluster_id_to_entity_id.get(cluster_id)
            if predicted_entity is None:
                continue

            row = self._entity_prediction_row(
                item,
                entity_id_predicted=predicted_entity,
                cluster_membership_prob=cluster_prob,
                pca_residual=float(res_k[j]),
                pca_mahalanobis=float(mah_k[j]),
                pca_spectral_entropy=float(ent_k[j]),
                anomaly_score_max_z=float(combined_k[j]),
                projection_score=float(oov_scores_k[j]),
            )
            cast(dict[str, object], row)["mention_source_index"] = int(mention_i)
            candidates.append(row)

        deduped = self._dedupe_overlapping_prediction_rows(candidates)
        if mention_anomaly_rows:
            assert screener_margin is not None
            assert kb_lemma_by_wg is not None
            residuals = np.full(n_mentions, np.nan, dtype=np.float64)
            mahalanobis = np.full(n_mentions, np.nan, dtype=np.float64)
            spectral_entropy = np.full(n_mentions, np.nan, dtype=np.float64)
            combined_full = np.full(n_mentions, np.nan, dtype=np.float64)
            projection_full = np.full(n_mentions, np.nan, dtype=np.float64)
            residuals[idx_keep] = res_k
            mahalanobis[idx_keep] = mah_k
            spectral_entropy[idx_keep] = ent_k
            combined_full[idx_keep] = combined_k
            projection_full[idx_keep] = oov_scores_k
            return deduped, self._build_mention_anomaly_rows(
                mentions,
                screener_neg,
                screener_margin,
                residuals,
                mahalanobis,
                spectral_entropy,
                combined_full,
                projection_full,
                kb_lemma_by_wg,
            )
        return deduped, None

    def _kb_lemma_index_by_wg(self, nlp: object) -> dict[WordGrouping, dict[str, str]]:
        """Build lemma→KB training-entity index; see :func:`pelinker.linker_kb_lemma.build_kb_lemma_index`."""
        return build_kb_lemma_index(self.labels_map, nlp)

    def compute_mention_anomaly(
        self,
        texts: Sequence[str],
        max_length: int | None = None,
        *,
        use_gpu: bool = False,
    ) -> list[dict[str, object]]:
        """Per-mention PCA residual / Mahalanobis with KB-match and screener fields.

        Delegates to :meth:`predict` with ``include_mention_anomaly=True`` so encoding,
        screening, and ``EmbeddingTransformer.transform`` run once (no duplicate pass).

        Screened-negative rows use NaN for PCA metrics. Each row includes
        ``screener_is_negative``, ``screener_decision``, plus ``is_kb_match`` /
        ``kb_property_match`` (lemma vs KB under :class:`WordGrouping`).
        """
        out = self.predict(
            texts,
            max_length=max_length,
            threshold=0.0,
            use_gpu=use_gpu,
            include_mention_anomaly=True,
        )
        rows = out.debug_mentions
        return list(rows) if rows is not None else []

build_clustering_report(*, training_diagnostics=None)

Build a :class:~pelinker.reporting.ClusteringReport when full training rows exist.

After a normal :meth:fit, heavy training payloads are removed for prediction; use :meth:take_fit_clustering_report immediately after fitting instead.

This method remains useful for legacy pickled linkers that still embed training arrays, or for tests that skip stripping.

Parameters:

Name Type Description Default
training_diagnostics LinkerFitDiagnostics | None

Optional stratified-sampled mention-level diagnostics (PCA quality + screener / manifold OOV scores) attached only to the fit report.

None
Source code in pelinker/model.py
def build_clustering_report(
    self,
    *,
    training_diagnostics: LinkerFitDiagnostics | None = None,
) -> ModelSelectionReport | None:
    """
    Build a :class:`~pelinker.reporting.ClusteringReport` when full training rows exist.

    After a normal :meth:`fit`, heavy training payloads are removed for prediction; use
    :meth:`take_fit_clustering_report` immediately after fitting instead.

    This method remains useful for **legacy** pickled linkers that still embed training
    arrays, or for tests that skip stripping.

    Args:
        training_diagnostics: Optional stratified-sampled mention-level diagnostics
            (PCA quality + screener / manifold OOV scores) attached only to the fit report.
    """
    tcf = self.training_cluster_frame
    if (
        tcf is None
        or self.training_pca_residuals is None
        or self.training_pca_mahalanobis is None
        or self.training_pca_spectral_entropy is None
        or self.training_umap_clustering is None
        or self.training_cluster_viz is None
        or self.training_pca_reduced is None
        or self.clustering_fit_metrics is None
    ):
        return None
    n = len(tcf)
    if (
        len(self.training_pca_residuals) != n
        or len(self.training_pca_mahalanobis) != n
        or len(self.training_pca_spectral_entropy) != n
        or self.training_umap_clustering.shape[0] != n
        or self.training_cluster_viz.shape[0] != n
        or self.training_pca_reduced.shape[0] != n
    ):
        return None

    m = self.clustering_fit_metrics
    dbcv_f = float(m.dbcv) if m.dbcv is not None else float("nan")
    ari_val = float(m.ari) if m.ari is not None else float("nan")
    metrics_df = pd.DataFrame(
        [
            {
                "min_cluster_size": m.min_cluster_size,
                "icm": float("nan"),
                "n_clusters": m.n_clusters_emergent,
                "dbcv": dbcv_f,
                "ari": ari_val,
            }
        ]
    )

    base_cols = ["entity", "cluster", "pmid", "mention"]
    optional_cols = [
        *MENTION_PROVENANCE_COLUMNS,
        "screener_score",
        "projection_score",
        "cluster_score",
        "clustering_in_sample",
        "screener_pass",
        "manifold_oov_pass",
    ]
    keep = [c for c in base_cols + optional_cols if c in tcf.columns]
    assignments = tcf[keep].copy()

    number_properties = int(tcf["entity"].nunique())

    res_f = np.asarray(self.training_pca_residuals, dtype=np.float64)
    mah_f = np.asarray(self.training_pca_mahalanobis, dtype=np.float64)
    ent_f = np.asarray(self.training_pca_spectral_entropy, dtype=np.float64)

    neg_lbl = (
        self.screener.negative_label
        if self.screener is not None
        else NEGATIVE_LABEL
    )
    y_neg = entity_negative_label_mask_01(tcf["entity"], neg_lbl)

    return ModelSelectionReport(
        hyperparameters=ClusteringHyperparameters(
            min_cluster_size=m.min_cluster_size
        ),
        best_score=dbcv_f,
        number_properties=number_properties,
        n_clusters_emergent=m.n_clusters_emergent,
        metrics_df=metrics_df,
        assignments=assignments,
        pca_residuals=res_f,
        pca_mahalanobis=mah_f,
        pca_spectral_entropy=ent_f,
        oov_label=y_neg,
        umap_clustering=np.asarray(self.training_umap_clustering, dtype=np.float64),
        cluster_viz=np.asarray(self.training_cluster_viz, dtype=np.float64),
        cluster_viz_method=(
            self.transform_config.cluster_viz_method
            if self.transform_config is not None
            else "pca"
        ),
        pca_reduced=np.asarray(self.training_pca_reduced, dtype=np.float64),
        all_screener_cv=None,
        screener_oos_datapoints=None,
        ari=m.ari,
        training_diagnostics=training_diagnostics,
        distillation_fidelity=self.distillation_fidelity,
        min_cluster_size_provenance=self.min_cluster_size_provenance,
        n_rows_realized=(
            None
            if self.min_cluster_size_provenance is None
            else self.min_cluster_size_provenance.n_rows_realized
        ),
    )

compute_mention_anomaly(texts, max_length=None, *, use_gpu=False)

Per-mention PCA residual / Mahalanobis with KB-match and screener fields.

Delegates to :meth:predict with include_mention_anomaly=True so encoding, screening, and EmbeddingTransformer.transform run once (no duplicate pass).

Screened-negative rows use NaN for PCA metrics. Each row includes screener_is_negative, screener_decision, plus is_kb_match / kb_property_match (lemma vs KB under :class:WordGrouping).

Source code in pelinker/model.py
def compute_mention_anomaly(
    self,
    texts: Sequence[str],
    max_length: int | None = None,
    *,
    use_gpu: bool = False,
) -> list[dict[str, object]]:
    """Per-mention PCA residual / Mahalanobis with KB-match and screener fields.

    Delegates to :meth:`predict` with ``include_mention_anomaly=True`` so encoding,
    screening, and ``EmbeddingTransformer.transform`` run once (no duplicate pass).

    Screened-negative rows use NaN for PCA metrics. Each row includes
    ``screener_is_negative``, ``screener_decision``, plus ``is_kb_match`` /
    ``kb_property_match`` (lemma vs KB under :class:`WordGrouping`).
    """
    out = self.predict(
        texts,
        max_length=max_length,
        threshold=0.0,
        use_gpu=use_gpu,
        include_mention_anomaly=True,
    )
    rows = out.debug_mentions
    return list(rows) if rows is not None else []

filter_report(report, thr_score) classmethod

Return a shallow copy with entities filtered by score (does not mutate report).

Source code in pelinker/model.py
@classmethod
def filter_report(
    cls, report: dict[str, object], thr_score: float
) -> dict[str, object]:
    """Return a shallow copy with ``entities`` filtered by score (does not mutate ``report``)."""
    raw_entities = report.get("entities", [])
    entities = raw_entities if isinstance(raw_entities, list) else []
    filtered = cls.filter_entities(
        cast(list[dict[str, object]], entities), thr_score
    )
    return {**report, "entities": filtered}

fit(embeddings, transform_config, min_cluster_size=None, *, fit_config=None, embedding_training=None, embedding_metadata=None, kb_config=None, kb_out_naming=None, kb_in_labels_map_path=None)

Fit the Linker model with embeddings.

This method handles two main parts: a) Loading and processing embeddings (from file or direct array) b) Fitting the negative screener, then PCA/UMAP + HDBSCAN on non-negative rows

Parameters:

Name Type Description Default
embeddings Path | Sequence[Path] | None

Path or sequence of paths to parquet file(s) (mention-level rows: pmid, entity, mention, embed). Multiple files are fused like :func:~pelinker.selection.load_selection_frame (inner join on keys, concat embeddings). Order must match embedding_metadata.sources. If None, embed_kb_corpus is run (one output file per source).

required
transform_config TransformConfig

TransformConfig instance

required
min_cluster_size int | None

HDBSCAN min_cluster_size (choose upstream, e.g. via pelinker.model_selection). When None, it is resolved from fit_config.scale_curve against the realized manifold row count, or falls back to :data:~pelinker.scaling.DEFAULT_MIN_CLUSTER_SIZE. An explicit value always wins; either way the choice and its origin land on min_cluster_size_provenance and in the fit report.

None
fit_config LinkerFitConfig | None

Parquet read batching, mention load filters, subsample settings, and screener config. Defaults to :class:LinkerFitConfig().

None
embedding_training EmbeddingTrainingConfig | None

Corpus paths and embedding runtime. Required when embeddings=None.

None
embedding_metadata EmbeddingModelMetadata | None

If provided, overrides or sets self.embedding_metadata for this fit (required when embeddings=None unless already set on the linker).

None
kb_config KBConfig | None

Knowledge-base metadata stored on the linker; entity_count is set from fitted vocabulary when omitted (None).

None
Side effects

Sets cluster_composition (mention-weighted property mass and per-cluster mixtures), cluster_consensus_names (short labels from those mixtures), screener_in_sample_metrics, and clustering_fit_metrics. Mention-level training tables and manifold arrays used for :class:~pelinker.reporting.ClusteringReport are stripped after each fit; persist JSON with :meth:take_fit_clustering_report and :func:~pelinker.reporting.write_clustering_report_json at :func:~pelinker.reporting.linker_fit_clustering_report_path (same layout as pelinker-fit report_path) before :meth:dump.

Returns:

Type Description
Linker

self

Source code in pelinker/model.py
def fit(
    self,
    embeddings: pathlib.Path | Sequence[pathlib.Path] | None,
    transform_config: TransformConfig,
    min_cluster_size: int | None = None,
    *,
    fit_config: LinkerFitConfig | None = None,
    embedding_training: EmbeddingTrainingConfig | None = None,
    embedding_metadata: EmbeddingModelMetadata | None = None,
    kb_config: KBConfig | None = None,
    kb_out_naming: KbOutNamingConfig | None = None,
    kb_in_labels_map_path: str | None = None,
) -> Linker:
    """
    Fit the Linker model with embeddings.

    This method handles two main parts:
    a) Loading and processing embeddings (from file or direct array)
    b) Fitting the negative screener, then PCA/UMAP + HDBSCAN on non-negative rows

    Args:
        embeddings: Path or sequence of paths to parquet file(s) (mention-level rows:
                    ``pmid``, ``entity``, ``mention``, ``embed``). Multiple files are
                    fused like :func:`~pelinker.selection.load_selection_frame` (inner join
                    on keys, concat embeddings). Order must match
                    ``embedding_metadata.sources``. If None, ``embed_kb_corpus`` is run
                    (one output file per source).
        transform_config: TransformConfig instance
        min_cluster_size: HDBSCAN ``min_cluster_size`` (choose upstream, e.g. via
            ``pelinker.model_selection``). When ``None``, it is resolved from
            ``fit_config.scale_curve`` against the realized manifold row count, or
            falls back to :data:`~pelinker.scaling.DEFAULT_MIN_CLUSTER_SIZE`. An
            explicit value always wins; either way the choice and its origin land on
            ``min_cluster_size_provenance`` and in the fit report.
        fit_config: Parquet read batching, mention load filters, subsample settings, and screener config.
            Defaults to :class:`LinkerFitConfig()`.
        embedding_training: Corpus paths and embedding runtime. Required when embeddings=None.
        embedding_metadata: If provided, overrides or sets ``self.embedding_metadata`` for
            this fit (required when embeddings=None unless already set on the linker).
        kb_config: Knowledge-base metadata stored on the linker; ``entity_count`` is set
            from fitted vocabulary when omitted (None).

    Side effects:
        Sets ``cluster_composition`` (mention-weighted property mass and per-cluster
        mixtures), ``cluster_consensus_names`` (short labels from those mixtures),
        ``screener_in_sample_metrics``, and ``clustering_fit_metrics``. Mention-level
        training tables and manifold arrays used for :class:`~pelinker.reporting.ClusteringReport`
        are stripped after each fit; persist JSON with :meth:`take_fit_clustering_report` and
        :func:`~pelinker.reporting.write_clustering_report_json` at
        :func:`~pelinker.reporting.linker_fit_clustering_report_path` (same layout as
        ``pelinker-fit`` ``report_path``) before :meth:`dump`.

    Returns:
        self
    """
    if min_cluster_size is not None and min_cluster_size < 2:
        raise ValueError("min_cluster_size must be >= 2")

    is_temporary = False
    embeddings_paths: list[pathlib.Path] = []

    try:
        if embedding_metadata is not None:
            self.embedding_metadata = embedding_metadata

        fc = fit_config or LinkerFitConfig()
        load_cfg = fc.to_clustering_sample_config()

        if embeddings is None:
            if embedding_training is None:
                raise ValueError(
                    "embedding_training is required when embeddings is None. "
                    "Provide embeddings path or EmbeddingTrainingConfig(...)."
                )
            self.nlp_model_name = embedding_training.nlp_model
            if self.embedding_metadata is None:
                raise ValueError(
                    "embedding_metadata is required when embeddings is None "
                    "(set on Linker(...) or pass embedding_metadata=... to fit())."
                )
            k = len(self.embedding_metadata.sources)
            for _ in range(k):
                tf = tempfile.NamedTemporaryFile(suffix=".parquet", delete=False)
                embeddings_paths.append(pathlib.Path(tf.name))
                tf.close()

            logger.info("Stage (a): Embedding corpus (%s source(s))...", k)
            embed_kb_corpus(
                metadata=self.embedding_metadata,
                training=embedding_training,
                output_parquet_paths=embeddings_paths,
            )
            is_temporary = True
        else:
            embeddings_paths = self._normalize_embedding_paths(embeddings)
            if self.embedding_metadata is not None and len(embeddings_paths) != len(
                self.embedding_metadata.sources
            ):
                raise ValueError(
                    "Number of embedding parquet paths must match "
                    f"embedding_metadata.sources ({len(self.embedding_metadata.sources)}), "
                    f"got {len(embeddings_paths)}"
                )
            logger.info(
                "Stage (A): Using provided embeddings (%s file(s)): %s",
                len(embeddings_paths),
                embeddings_paths,
            )

        logger.info(
            "Stage (B): mention-level load from %s parquet file(s)",
            len(embeddings_paths),
        )
        prepared = load_selection_frame(
            file_paths=embeddings_paths,
            config=load_cfg,
            show_embedding_read_progress=True,
        )
        if prepared is None or len(prepared) == 0:
            raise ValueError(
                "No mention-level embedding rows loaded from parquet (check paths "
                "and columns pmid, entity, mention, embed)."
            )

        self._fit_clustering_on_prepared_mentions(
            prepared=prepared,
            transform_config=transform_config,
            fit_cfg=fc,
            min_cluster_size=min_cluster_size,
            kb_config=kb_config,
            kb_out_naming=kb_out_naming,
            kb_in_labels_map_path=kb_in_labels_map_path,
        )

        return self
    finally:
        if is_temporary:
            for p in embeddings_paths:
                try:
                    p.unlink()
                    logger.debug("Removed temporary parquet file: %s", p)
                except Exception as e:
                    logger.warning(
                        "Failed to remove temporary parquet file %s: %s", p, e
                    )

predict(texts, max_length=None, threshold=DEFAULT_CLUSTER_MEMBERSHIP_THRESHOLD, *, use_gpu=False, include_mention_anomaly=False, include_debug_mentions=False, include_prediction_kb_validation=False)

Predict entities for input texts.

With multiple embedding_metadata.sources, runs texts_to_vrep per source (cached by model_type), concatenates mention tensors along the feature axis in source order, then applies the fitted transformer and clusterer. Mention counts must match across sources (typically the same model_type for all sources).

Tokenization uses the spaCy pipeline named by nlp_model_name (set from EmbeddingTrainingConfig.nlp_model during corpus embedding, else default en_core_web_trf).

Each entities row includes score: HDBSCAN approximate cluster membership probability from approximate_predict on UMAP coordinates. The threshold argument drops rows whose score is below that minimum (default :data:DEFAULT_CLUSTER_MEMBERSHIP_THRESHOLD). Cluster -1 (HDBSCAN noise) is always treated as nil and omitted.

When include_mention_anomaly or include_debug_mentions is true, :attr:LinkerPredictResult.debug_mentions lists one diagnostic row per extracted mention (same single encode and PCA→UMAP pass as predictions). Use :meth:LinkerPredictResult.to_dict with include_debug=True to emit the legacy mention_anomaly key for JSON consumers.

kb_training_entity (human label from labels_map for the predicted id) is attached only when mention-debug or KB validation is requested, not on the default prediction path.

When include_prediction_kb_validation is true, each row in entities gains validation-only fields comparing mention lemmas to KB training entity labels (same index as training-time matching): kb_training_entity_from_lemma, kb_training_entity_for_prediction, lemma_kb_matches_predicted_entity. When debug rows are also returned, those fields are copied onto the matching mention row via mention_source_index.

Source code in pelinker/model.py
def predict(
    self,
    texts: Sequence[str],
    max_length: int | None = None,
    threshold: float = DEFAULT_CLUSTER_MEMBERSHIP_THRESHOLD,
    *,
    use_gpu: bool = False,
    include_mention_anomaly: bool = False,
    include_debug_mentions: bool = False,
    include_prediction_kb_validation: bool = False,
) -> LinkerPredictResult:
    """
    Predict entities for input texts.

    With multiple ``embedding_metadata.sources``, runs ``texts_to_vrep`` per source
    (cached by ``model_type``), concatenates mention tensors along the feature axis in
    source order, then applies the fitted transformer and clusterer. Mention counts
    must match across sources (typically the same ``model_type`` for all sources).

    Tokenization uses the spaCy pipeline named by ``nlp_model_name`` (set from
    ``EmbeddingTrainingConfig.nlp_model`` during corpus embedding, else default
    ``en_core_web_trf``).

    Each ``entities`` row includes ``score``: HDBSCAN approximate cluster
    membership probability from ``approximate_predict`` on UMAP coordinates.
    The ``threshold`` argument drops rows whose ``score`` is below that minimum
    (default :data:`DEFAULT_CLUSTER_MEMBERSHIP_THRESHOLD`). Cluster ``-1``
    (HDBSCAN noise) is always treated as nil and omitted.

    When ``include_mention_anomaly`` or ``include_debug_mentions`` is true,
    :attr:`LinkerPredictResult.debug_mentions` lists one diagnostic row per extracted
    mention (same single encode and PCA→UMAP pass as predictions). Use
    :meth:`LinkerPredictResult.to_dict` with ``include_debug=True`` to emit the legacy
    ``mention_anomaly`` key for JSON consumers.

    ``kb_training_entity`` (human label from ``labels_map`` for the predicted id)
    is attached only when mention-debug or KB validation is requested, not on the
    default prediction path.

    When ``include_prediction_kb_validation`` is true, each row in ``entities`` gains
    validation-only fields comparing mention lemmas to KB training ``entity`` labels
    (same index as training-time matching): ``kb_training_entity_from_lemma``,
    ``kb_training_entity_for_prediction``, ``lemma_kb_matches_predicted_entity``.
    When debug rows are also returned, those fields are copied onto the matching
    mention row via ``mention_source_index``.
    """
    want_debug = include_mention_anomaly or include_debug_mentions
    tt, mentions, primary = self._encode_mentions(
        texts, max_length, use_gpu=use_gpu
    )

    if tt is None:
        return LinkerPredictResult(
            entities=[],
            debug_mentions=[] if want_debug else None,
        )

    kb_lemma_by_wg: dict[WordGrouping, dict[str, str]] | None = None
    if want_debug or include_prediction_kb_validation:
        nlp = self._ensure_nlp()
        kb_lemma_by_wg = self._kb_lemma_index_by_wg(nlp)

    predictions, mention_anomaly = self._predict_with_clustering(
        tt,
        mentions,
        threshold=threshold,
        mention_anomaly_rows=want_debug,
        kb_lemma_by_wg=kb_lemma_by_wg,
    )

    if include_prediction_kb_validation:
        if kb_lemma_by_wg is None:
            raise ValueError(
                "kb_lemma_by_wg missing for include_prediction_kb_validation "
                "(internal error: index should have been built)"
            )
        enrich_entity_predictions_kb_validation(
            cast(list[dict[str, object]], predictions),
            kb_lemma_by_wg,
            self.labels_map,
        )

    preds_obj = cast(list[dict[str, object]], predictions)
    if want_debug or include_prediction_kb_validation:
        for row in preds_obj:
            eid = row.get("entity_id_predicted")
            row["kb_training_entity"] = (
                self.labels_map.get(str(eid)) if eid is not None else None
            )

    if mention_anomaly is not None:
        self._merge_prediction_fields_into_debug_mentions(
            mention_anomaly,
            preds_obj,
            include_kb_validation_fields=include_prediction_kb_validation,
        )

    for item in preds_obj:
        item.pop("lemma", None)

    return LinkerPredictResult(
        entities=preds_obj,
        debug_mentions=mention_anomaly,
    )

take_fit_clustering_report()

Consume the :class:~pelinker.reporting.ClusteringReport produced by the last :meth:fit.

Call before :meth:dump if you need JSON or other persistence: the report is not serialized on the linker artifact (only prediction state is pickled).

Returns None if :meth:fit has not been run, the report was already taken, or clustering state was incomplete.

Source code in pelinker/model.py
def take_fit_clustering_report(self) -> ModelSelectionReport | None:
    """
    Consume the :class:`~pelinker.reporting.ClusteringReport` produced by the last :meth:`fit`.

    Call **before** :meth:`dump` if you need JSON or other persistence: the report is
    not serialized on the linker artifact (only prediction state is pickled).

    Returns ``None`` if :meth:`fit` has not been run, the report was already taken, or
    clustering state was incomplete.
    """
    report = self._fit_clustering_report
    self._fit_clustering_report = None
    return report

training_anomaly_metric_summary()

Quantile summary from stored per-mention PCA metrics (legacy pickles only after fit).

Source code in pelinker/model.py
def training_anomaly_metric_summary(self) -> dict[str, dict[str, float]] | None:
    """Quantile summary from stored per-mention PCA metrics (legacy pickles only after fit)."""
    if (
        self.training_pca_residuals is None
        or self.training_pca_mahalanobis is None
        or self.training_pca_spectral_entropy is None
        or len(self.training_pca_residuals) == 0
        or len(self.training_pca_mahalanobis) == 0
    ):
        return None

    residual = np.asarray(self.training_pca_residuals, dtype=np.float64)
    mahal = np.asarray(self.training_pca_mahalanobis, dtype=np.float64)
    entropy = np.asarray(self.training_pca_spectral_entropy, dtype=np.float64)
    combined = np.maximum.reduce(
        [
            self._zscore(residual),
            self._zscore(mahal),
            self._zscore(entropy),
        ]
    )
    quantiles = [0.5, 0.9, 0.95, 0.99]

    def _q(values: np.ndarray) -> dict[str, float]:
        return {
            f"q{int(q * 100):02d}": float(np.quantile(values, q)) for q in quantiles
        }

    return {
        "residual": _q(residual),
        "mahalanobis": _q(mahal),
        "spectral_entropy": _q(entropy),
        "combined_max_z": _q(combined),
    }

LinkerPredictResult dataclass

Structured return value from :meth:Linker.predict.

debug_mentions is one row per extracted mention (including screener negatives) when debug was requested; it is not filtered by cluster score.

Source code in pelinker/model.py
@dataclass(frozen=True, slots=True)
class LinkerPredictResult:
    """Structured return value from :meth:`Linker.predict`.

    ``debug_mentions`` is one row per extracted mention (including screener negatives)
    when debug was requested; it is not filtered by cluster score.
    """

    entities: list[dict[str, object]]
    debug_mentions: list[dict[str, object]] | None = None

    def filter_by_score(self, thr_score: float) -> LinkerPredictResult:
        filtered: list[dict[str, object]] = [
            r for r in self.entities if float(r.get("score", 0.0)) >= thr_score
        ]
        return LinkerPredictResult(
            entities=filtered,
            debug_mentions=self.debug_mentions,
        )

    def to_dict(
        self,
        *,
        include_debug: bool = False,
        include_entity_anomaly_metrics: bool = True,
        strip_mention_source_index: bool = True,
        public_entity_fields: bool = False,
    ) -> dict[str, object]:
        """Serialize for JSON APIs. Debug rows use the legacy key ``mention_anomaly``.

        When ``public_entity_fields`` is true (used by ``/link`` and the link-files CLI
        in default mode), entity rows omit anomaly metrics, KB validation labels, and
        ``word_grouping``; character spans use document-global ``a`` / ``b`` (from
        internal ``a_abs`` / ``b_abs``), not chunk-local coordinates.
        """
        entities_out: list[dict[str, object]] = []
        for r in self.entities:
            e = dict(r)
            if strip_mention_source_index:
                e.pop("mention_source_index", None)
            if public_entity_fields:
                e.pop("pca_residual", None)
                e.pop("pca_mahalanobis", None)
                e.pop("pca_spectral_entropy", None)
                e.pop("anomaly_score_max_z", None)
                e.pop("projection_score", None)
                e.pop("word_grouping", None)
                for k in (
                    "kb_training_entity",
                    "kb_training_entity_from_lemma",
                    "kb_training_entity_for_prediction",
                    "lemma_kb_matches_predicted_entity",
                ):
                    e.pop(k, None)
                chunk_a = e.pop("a", None)
                chunk_b = e.pop("b", None)
                abs_a = e.pop("a_abs", None)
                abs_b = e.pop("b_abs", None)
                e["a"] = abs_a if abs_a is not None else chunk_a
                e["b"] = abs_b if abs_b is not None else chunk_b
            elif not include_entity_anomaly_metrics:
                e.pop("pca_residual", None)
                e.pop("pca_mahalanobis", None)
                e.pop("pca_spectral_entropy", None)
                e.pop("anomaly_score_max_z", None)
                e.pop("projection_score", None)
            entities_out.append(e)
        payload: dict[str, object] = {"entities": entities_out}
        if include_debug and self.debug_mentions is not None:
            payload["mention_anomaly"] = [dict(row) for row in self.debug_mentions]
        return payload

to_dict(*, include_debug=False, include_entity_anomaly_metrics=True, strip_mention_source_index=True, public_entity_fields=False)

Serialize for JSON APIs. Debug rows use the legacy key mention_anomaly.

When public_entity_fields is true (used by /link and the link-files CLI in default mode), entity rows omit anomaly metrics, KB validation labels, and word_grouping; character spans use document-global a / b (from internal a_abs / b_abs), not chunk-local coordinates.

Source code in pelinker/model.py
def to_dict(
    self,
    *,
    include_debug: bool = False,
    include_entity_anomaly_metrics: bool = True,
    strip_mention_source_index: bool = True,
    public_entity_fields: bool = False,
) -> dict[str, object]:
    """Serialize for JSON APIs. Debug rows use the legacy key ``mention_anomaly``.

    When ``public_entity_fields`` is true (used by ``/link`` and the link-files CLI
    in default mode), entity rows omit anomaly metrics, KB validation labels, and
    ``word_grouping``; character spans use document-global ``a`` / ``b`` (from
    internal ``a_abs`` / ``b_abs``), not chunk-local coordinates.
    """
    entities_out: list[dict[str, object]] = []
    for r in self.entities:
        e = dict(r)
        if strip_mention_source_index:
            e.pop("mention_source_index", None)
        if public_entity_fields:
            e.pop("pca_residual", None)
            e.pop("pca_mahalanobis", None)
            e.pop("pca_spectral_entropy", None)
            e.pop("anomaly_score_max_z", None)
            e.pop("projection_score", None)
            e.pop("word_grouping", None)
            for k in (
                "kb_training_entity",
                "kb_training_entity_from_lemma",
                "kb_training_entity_for_prediction",
                "lemma_kb_matches_predicted_entity",
            ):
                e.pop(k, None)
            chunk_a = e.pop("a", None)
            chunk_b = e.pop("b", None)
            abs_a = e.pop("a_abs", None)
            abs_b = e.pop("b_abs", None)
            e["a"] = abs_a if abs_a is not None else chunk_a
            e["b"] = abs_b if abs_b is not None else chunk_b
        elif not include_entity_anomaly_metrics:
            e.pop("pca_residual", None)
            e.pop("pca_mahalanobis", None)
            e.pop("pca_spectral_entropy", None)
            e.pop("anomaly_score_max_z", None)
            e.pop("projection_score", None)
        entities_out.append(e)
    payload: dict[str, object] = {"entities": entities_out}
    if include_debug and self.debug_mentions is not None:
        payload["mention_anomaly"] = [dict(row) for row in self.debug_mentions]
    return payload