Download code/src/lnpbench/registry.py from eduzrh/LNPBench: direct link, hf CLI and curl.
- Browser
- Download file 14.8 kB
-
https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/src/lnpbench/registry.py
- Command line
-
hf download hf://datasets/eduzrh/LNPBench/code/src/lnpbench/registry.py
-
curl -L -o registry.py https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/src/lnpbench/registry.py
14.8 kB
| """Model registry, including ablation variants.""" | |
| from __future__ import annotations | |
| from typing import Callable, Dict | |
| import numpy as np | |
| from .models.baselines import AGILE, LION | |
| from .models.classical import ( | |
| FingerprintMLP, | |
| SklearnModel, | |
| make_extra_trees, | |
| make_random_forest, | |
| make_xgboost, | |
| ) | |
| # v2 backbone: GINE graph + RDKit descriptors feeding the formulation set. | |
| # The v1 (fingerprint-fragment) implementations are kept in phys_causal.py / | |
| # phys_set.py for the ablation trail but are no longer the reported methods. | |
| from .models.backbone import PhyCausalLNP, PhysSetLNP | |
| from .models.probe import (ProbeFlat, ProbeFlatRank, ProbePhysSet, ProbePhysSetRank, | |
| ProbeSet, ProbeSetBias, ProbeSetBiasRank) | |
| from .models.physmode import DiffModeLNP, PhysModeLNP, RelModeLNP | |
| from .models.seq_backbone import SeqCausalLNP | |
| from .trainer import TrainConfig | |
| class MeanPredictor: | |
| name = "mean" | |
| def fit(self, store, train_idx, val_idx, cfg: TrainConfig): | |
| self.mu = float(store.y[train_idx].mean()) | |
| return {"best_val_spearman": float("nan")} | |
| def predict(self, store, idx): | |
| return np.full(len(idx), self.mu) | |
| PM_KW = dict(lam_base=0.0, lam_vq=0.02, lam_bal=0.02, lam_orth=0.005, alpha_init=1.0) | |
| DM_KW = dict(lam_base=0.0, lam_mode=0.2) | |
| REGISTRY: Dict[str, Callable[[], object]] = { | |
| # trivial | |
| "mean": lambda: MeanPredictor(), | |
| # classical | |
| "xgboost": lambda: SklearnModel(make_xgboost), | |
| "randomforest": lambda: SklearnModel(make_random_forest), | |
| "extratrees": lambda: SklearnModel(make_extra_trees), | |
| "xgboost_noctx": lambda: SklearnModel(make_xgboost, drop_ctx=True), | |
| # deep baselines | |
| "mlp_fp": lambda: FingerprintMLP(), | |
| "mlp_fp_noctx": lambda: FingerprintMLP(drop_ctx=True), | |
| "lion": lambda: LION(), | |
| "agile": lambda: AGILE(), | |
| # proposed | |
| "phcausal": lambda: PhyCausalLNP(), | |
| "physset": lambda: PhysSetLNP(), | |
| "seqcausal": lambda: SeqCausalLNP(), | |
| # CPU-sized variant of Method C: identical architecture (GINE + Bi-Mamba + | |
| # GroupDRO + causal/environment disentanglement), reduced width because the | |
| # full-width selective state space needs >3 GB per mini-batch on CPU. | |
| "seqcausal_small": lambda: SeqCausalLNP(d_model=96, mamba_layers=1, d_state=4, z_dim=64), | |
| # ---- architecture isolation ladder ---------------------------------------- | |
| "probe_flat": lambda: ProbeFlat(), | |
| "probe_set": lambda: ProbeSet(), | |
| "probe_set_bias": lambda: ProbeSetBias(), | |
| "probe_set_bias_rank": lambda: ProbeSetBiasRank(), | |
| "probe_flat_rank": lambda: ProbeFlatRank(), | |
| "probe_physset": lambda: ProbePhysSet(), | |
| "probe_physset_rank": lambda: ProbePhysSetRank(), | |
| "phcausal_phys": lambda: PhyCausalLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5), | |
| "physset_phys": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5), | |
| # ---- ablations of the final configuration -------------------------------- | |
| "physset_phys_bal": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5), | |
| "physset_phys_nophys": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, use_physics=False), | |
| "physset_phys_norank": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.0), | |
| "physset_phys_fp": lambda: PhysSetLNP(cond_mode="concat", node_mode="fp", lam_rank=0.5), | |
| "physset_phys_nocond": lambda: PhysSetLNP(cond_mode="none", node_mode="phys", lam_rank=0.5), | |
| "physset_phys_nomdaux": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, use_md_aux=False), | |
| "physset_phys_nodual": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, dual_state=False), | |
| # ------------------------------------------------------------------ | |
| # external supervision: encoder pretrained on LNP Atlas absolute | |
| # properties (size / PDI / zeta / EE) before seeing any LNPDB activity | |
| "physset_ext": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| pretrained="cache/atlas_pretrain.pt"), | |
| "physset_ext_s1": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| pretrained="cache/atlas_pretrain_s1.pt"), | |
| "physset_ext_s2": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| pretrained="cache/atlas_pretrain_s2.pt"), | |
| "physset_ext_lipid": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| pretrained="cache/atlas_pretrain_lipid.pt"), | |
| "physset_ext_lipid_shuf": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| pretrained="cache/atlas_pretrain_lipid_shuf.pt"), | |
| "physset_ext_lipid_frozen": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| pretrained="cache/atlas_pretrain_lipid.pt", freeze_gnn=True), | |
| "physset_ext_frozen": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| pretrained="cache/atlas_pretrain.pt", freeze_gnn=True), | |
| "physset_ext_aux": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| pretrained="cache/atlas_pretrain.pt", aux_dim=4, lam_aux=0.2), | |
| # ---- Method F: PhysMode-LNP (LaRIN-style discrete mode codebook) --------- | |
| # r2 configuration: the base scorer is trained only through the residual | |
| # decomposition (lam_base=0 leaves the nesting into PhysSet-LNP exact), the | |
| # codebook terms are reduced so they regularise rather than compete, and | |
| # alpha starts at unity because the base score is a standardised regression | |
| # output, not a log-odds. | |
| "physmode": lambda: PhysModeLNP(**PM_KW), | |
| "physmode_base": lambda: PhysModeLNP(**{**PM_KW, "use_residual": False}), | |
| "physmode_hard": lambda: PhysModeLNP(**{**PM_KW, "hard_mode": True}), | |
| "physmode_lin": lambda: PhysModeLNP(**{**PM_KW, "mix": "linear"}), | |
| "physmode_novq": lambda: PhysModeLNP(**{**PM_KW, "use_vq": False}), | |
| "physmode_resonly": lambda: PhysModeLNP(**{**PM_KW, "residual_only": True}), | |
| "physmode_k8": lambda: PhysModeLNP(**{**PM_KW, "n_codes": 8}), | |
| "physmode_k32": lambda: PhysModeLNP(**{**PM_KW, "n_codes": 32}), | |
| "physmode_a01": lambda: PhysModeLNP(**{**PM_KW, "alpha_init": 0.1}), | |
| # chemistry-only mode space: the discrete mode and the residual experts never | |
| # see the experiment context (see physmode.py, mode_space) | |
| "physmode_chem": lambda: PhysModeLNP(**{**PM_KW, "mode_space": "chem"}), | |
| "physmode_chem_hard": lambda: PhysModeLNP(**{**PM_KW, "mode_space": "chem", "hard_mode": True}), | |
| "physmode_chem_novq": lambda: PhysModeLNP(**{**PM_KW, "mode_space": "chem", "use_vq": False}), | |
| "physmode_chem_k8": lambda: PhysModeLNP(**{**PM_KW, "mode_space": "chem", "n_codes": 8}), | |
| "physmode_chem_k32": lambda: PhysModeLNP(**{**PM_KW, "mode_space": "chem", "n_codes": 32}), | |
| "physmode_chem_resonly": lambda: PhysModeLNP(**{**PM_KW, "mode_space": "chem", "residual_only": True}), | |
| "physmode_chem_lin": lambda: PhysModeLNP(**{**PM_KW, "mode_space": "chem", "mix": "linear"}), | |
| # preliminary configuration kept for the ablation trail | |
| "physmode_r1": lambda: PhysModeLNP(), | |
| # ---- Method G: DiffMode-LNP (CoDiffGRN-style discrete diffusion) --------- | |
| "diffmode": lambda: DiffModeLNP(**DM_KW), | |
| "diffmode_base": lambda: DiffModeLNP(**{**DM_KW, "use_residual": False}), | |
| "diffmode_lin": lambda: DiffModeLNP(**{**DM_KW, "mix": "linear"}), | |
| "diffmode_hard": lambda: DiffModeLNP(**{**DM_KW, "hard_mode": True}), | |
| "diffmode_tass": lambda: DiffModeLNP(**{**DM_KW, "tass_p": 0.3}), | |
| "diffmode_chem": lambda: DiffModeLNP(**{**DM_KW, "mode_space": "chem"}), | |
| "diffmode_chem_hard": lambda: DiffModeLNP(**{**DM_KW, "mode_space": "chem", "hard_mode": True}), | |
| "diffmode_r1": lambda: DiffModeLNP(), | |
| # ---- Method H: RelMode-LNP (LaRIN latent relations over node pairs) ------ | |
| # The discrete variable is the intermolecular contact, not the formulation: | |
| # K prototypes over pair representations, EMA codebook, residual latent- | |
| # relation odds marginalised into the attention logits. | |
| "relmode": lambda: RelModeLNP(), | |
| # expert granularity: the only place where a mode can add capacity | |
| "relmode_h": lambda: RelModeLNP(rel_expert="head"), | |
| "relmode_sc": lambda: RelModeLNP(rel_expert="scalar"), | |
| "relmode_sc_a1": lambda: RelModeLNP(rel_expert="scalar", rel_alpha=1.0), | |
| "relmode_h_a1": lambda: RelModeLNP(rel_expert="head", rel_alpha=1.0), | |
| "relmode_ln": lambda: RelModeLNP(rel_expert="linear"), | |
| # identity-free mode space: the molar-composition channel (a near-unique | |
| # study fingerprint) is removed from the pair representation | |
| "relmode_nr": lambda: RelModeLNP(rel_use_ratio=False), | |
| "relmode_nr_sc": lambda: RelModeLNP(rel_use_ratio=False, rel_expert="scalar"), | |
| "relmode_nr_w": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8), | |
| "relmode_nr_sc_w": lambda: RelModeLNP(rel_use_ratio=False, rel_expert="scalar", rel_warmup=8), | |
| "relmode_nr_sch": lambda: RelModeLNP(rel_use_ratio=False, rel_schema_only=True), | |
| "relmode_nr_w_sch": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, rel_schema_only=True), | |
| # ablation ladder around the adopted configuration | |
| "relmode_nr_w_k8": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, rel_n_codes=8), | |
| "relmode_nr_w_k32": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, rel_n_codes=32), | |
| "relmode_nr_w_novq": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, rel_use_vq=False), | |
| "relmode_nr_w_hard": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, rel_hard=True), | |
| "relmode_nr_w_nocent": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, rel_center=False), | |
| # hybrid read-out: neural head sees the identity-free 398-d chemistry block | |
| "relmode_blk": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, use_block=True), | |
| "relset_blk": lambda: RelModeLNP(use_rel_mode=False, use_block=True), | |
| # ---- evaluation 3: coarse cross-study context --------------------------- | |
| "relmode_ctx": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, ctx_mode="coarse"), | |
| "relmode_ctx_blk": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, | |
| ctx_mode="coarse", use_block=True), | |
| "relset_ctx": lambda: RelModeLNP(use_rel_mode=False, ctx_mode="coarse"), | |
| "relset_ctx_blk": lambda: RelModeLNP(use_rel_mode=False, ctx_mode="coarse", use_block=True), | |
| "relset_ctxc": lambda: RelModeLNP(use_rel_mode=False, ctx_mode="none"), | |
| # ---- evaluation 2: pure within-study ranking ---------------------------- | |
| "relmode_rank": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, lam_reg=0.1, lam_rank=1.0), | |
| "relmode_rank0": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, lam_reg=0.0, lam_rank=1.0), | |
| "relmode_rank_blk": lambda: RelModeLNP(rel_use_ratio=False, rel_warmup=8, | |
| lam_reg=0.1, lam_rank=1.0, use_block=True), | |
| "relset_rank": lambda: RelModeLNP(use_rel_mode=False, lam_reg=0.1, lam_rank=1.0), | |
| "relset_rank0": lambda: RelModeLNP(use_rel_mode=False, lam_reg=0.0, lam_rank=1.0), | |
| "relmode_base": lambda: RelModeLNP(use_rel_mode=False), | |
| "relmode_hard": lambda: RelModeLNP(rel_hard=True), | |
| "relmode_lin": lambda: RelModeLNP(rel_mix="linear"), | |
| "relmode_novq": lambda: RelModeLNP(rel_use_vq=False), | |
| "relmode_nocent": lambda: RelModeLNP(rel_center=False), | |
| "relmode_k8": lambda: RelModeLNP(rel_n_codes=8), | |
| "relmode_k32": lambda: RelModeLNP(rel_n_codes=32), | |
| "relmode_a01": lambda: RelModeLNP(rel_alpha=0.1), | |
| "relmode_a1": lambda: RelModeLNP(rel_alpha=1.0), | |
| "relmode_a2": lambda: RelModeLNP(rel_alpha=2.0), | |
| # ---- Method E: Schema-LNP ------------------------------------------------- | |
| # ontology node attributes + typed (schema) adjacency on top of PhysSet-LNP | |
| "schema_lnp": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| use_onto=True, use_edge_mask=True), | |
| "schema_lnp_global": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| use_onto=True, use_edge_mask=True, use_global=True), | |
| "schema_lnp_nomask": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| use_onto=True, use_edge_mask=False), | |
| "schema_lnp_noon": lambda: PhysSetLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, | |
| use_onto=False, use_edge_mask=True), | |
| "phcausal_phys_nograph": lambda: PhyCausalLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, use_graph=False), | |
| "phcausal_phys_nodesc": lambda: PhyCausalLNP(cond_mode="concat", node_mode="phys", lam_rank=0.5, use_desc=False), | |
| # ---- ablations (Method A) ------------------------------------------------- | |
| "phcausal_concat": lambda: PhyCausalLNP(cond_mode="concat"), | |
| "phcausal_nohelp": lambda: PhyCausalLNP(cond_mode="concat", use_helpers=False), | |
| "phcausal_cd05": lambda: PhyCausalLNP(cond_mode="concat", comp_dropout=0.5), | |
| "phcausal_cd09": lambda: PhyCausalLNP(cond_mode="concat", comp_dropout=0.9), | |
| "phcausal_nocond": lambda: PhyCausalLNP(cond_mode="none"), | |
| "physset_concat": lambda: PhysSetLNP(cond_mode="concat"), | |
| "phcausal_no_phys": lambda: PhyCausalLNP(use_physics=False), | |
| "phcausal_no_cond": lambda: PhyCausalLNP(use_condition=False), | |
| "phcausal_no_rank": lambda: PhyCausalLNP(lam_rank=0.0), | |
| "phcausal_no_graph": lambda: PhyCausalLNP(use_graph=False), | |
| "phcausal_no_desc": lambda: PhyCausalLNP(use_desc=False), | |
| "phcausal_disentangle": lambda: PhyCausalLNP(disentangle=True, lam_adv=0.1, lam_env=0.1, lam_ortho=0.05), | |
| # ---- ablations (Method B) ------------------------------------------------- | |
| "physset_no_dual": lambda: PhysSetLNP(dual_state=False), | |
| "physset_no_mdaux": lambda: PhysSetLNP(use_md_aux=False), | |
| "physset_no_rank": lambda: PhysSetLNP(lam_rank=0.0), | |
| # ---- ablations (Method C) ------------------------------------------------- | |
| "seqcausal_no_mamba": lambda: SeqCausalLNP(use_mamba=False), | |
| "seqcausal_no_gdro": lambda: SeqCausalLNP(use_gdro=False), | |
| "seqcausal_no_rank": lambda: SeqCausalLNP(lam_rank=0.0), | |
| "seqcausal_no_desc": lambda: SeqCausalLNP(use_desc=False), | |
| } | |
| def build_model(name: str): | |
| if name not in REGISTRY: | |
| raise KeyError(f"unknown model {name}; available: {sorted(REGISTRY)}") | |
| return REGISTRY[name]() |