dualmapref 0.2.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- args_parse.py +593 -0
- candidate_corrector.py +161 -0
- candidate_scorer.py +359 -0
- conformend/__init__.py +36 -0
- conformend/refiner.py +25 -0
- dataset/__init__.py +1 -0
- dataset/drugdataset.py +644 -0
- dataset/manifold.py +237 -0
- dataset/sharded_drugdataset.py +124 -0
- dataset/smiles2graph.py +217 -0
- dualmapref/__init__.py +24 -0
- dualmapref/refiner.py +16 -0
- dualmapref-0.2.8.dist-info/LICENSE +21 -0
- dualmapref-0.2.8.dist-info/METADATA +167 -0
- dualmapref-0.2.8.dist-info/RECORD +186 -0
- dualmapref-0.2.8.dist-info/WHEEL +5 -0
- dualmapref-0.2.8.dist-info/entry_points.txt +10 -0
- dualmapref-0.2.8.dist-info/top_level.txt +29 -0
- exputils.py +289 -0
- infer.py +23 -0
- infer_base_conformers.py +5589 -0
- infer_mean_dispersion_torsion.py +1117 -0
- infer_torsion.py +752 -0
- infer_torsional_denoiser.py +1021 -0
- manimol/__init__.py +30 -0
- manimol/__main__.py +6 -0
- manimol/cli.py +36 -0
- manimol/full_cli.py +41 -0
- manimol/legacy_imports.py +43 -0
- manimol/manifest.py +56 -0
- manimol/postprocess.py +148 -0
- manimol/priors.py +60 -0
- manimol/protocols.py +119 -0
- manimol/refiner.py +645 -0
- manimol/runtime.py +194 -0
- manimol/selection.py +85 -0
- manimol/torsion.py +22 -0
- manimol/train_cli.py +14 -0
- mixture_flow/src/__init__.py +1 -0
- mixture_flow/src/_path.py +7 -0
- mixture_flow/src/infer28_mixture_torsion_flow.py +1093 -0
- mixture_flow/src/mixture_torsion_flow.py +291 -0
- mixture_flow/src/summarize_mixture_debug.py +80 -0
- mixture_flow/src/train28_mixture_torsion_flow.py +583 -0
- mixture_torsion_flow.py +7 -0
- models/ manifold_learning.py +86 -0
- models/__init__.py +8 -0
- models/dist.py +273 -0
- models/dist2coords.py +23 -0
- models/egnn.py +107 -0
- models/experimental_backbones.py +364 -0
- models/gnnconv.py +512 -0
- models/kernel_inversion.py +141 -0
- models/kernels.py +427 -0
- models/losses.py +154 -0
- models/mean_dispersion_bridge.py +119 -0
- models/mean_dispersion_torsion_context.py +155 -0
- models/model.py +73 -0
- models/model0.py +2375 -0
- models/vis.py +128 -0
- pgraph_controls.py +107 -0
- scripts/__init__.py +2 -0
- scripts/analyze_basin_alignment.py +63 -0
- scripts/analyze_hif2a_conformer_aggregation.py +267 -0
- scripts/analyze_hif2a_contact_pharmacophore.py +1111 -0
- scripts/analyze_hif2a_fixed_budget.py +202 -0
- scripts/analyze_hif2a_pharmacophore.py +405 -0
- scripts/analyze_hif2a_pharmacophore_match_metric.py +386 -0
- scripts/analyze_p_control_candidate_flow.py +154 -0
- scripts/analyze_scaffold_basin.py +83 -0
- scripts/audit_dispersion_proposals.py +208 -0
- scripts/audit_etflow_protocol.py +258 -0
- scripts/audit_external_overlap.py +150 -0
- scripts/audit_oldbest_fulltrainval_split.py +82 -0
- scripts/audit_pdbbind_contact_pharmacophores.py +146 -0
- scripts/audit_qm9_geomol_test_split.py +94 -0
- scripts/audit_stage1_stage2_cross.py +131 -0
- scripts/build_geom_drugs_lowref_diagnostic.py +84 -0
- scripts/build_qm9_backfill_manifest.py +99 -0
- scripts/check_etflow_runtime.py +56 -0
- scripts/compare_prior_sources.py +122 -0
- scripts/compose_binding_mode_figure.py +87 -0
- scripts/compose_hif2a_contact_pharmacophore_suppfig.py +255 -0
- scripts/compose_hif2a_functional_geometry_pipeline.py +266 -0
- scripts/compose_manimol_internal_architecture.py +291 -0
- scripts/diagnose_flow_basin_intervention.py +54 -0
- scripts/diagnose_hif2a_pharmacophore_budget.py +185 -0
- scripts/diagnose_route2_p_ranking.py +123 -0
- scripts/discover_binding_mode_inputs.py +30 -0
- scripts/evaluate_functional_prior_stage1.py +230 -0
- scripts/evaluate_geomanimol.py +74 -0
- scripts/evaluate_geomanimol_flow.py +23 -0
- scripts/evaluate_geomanimol_multiscaffold.py +46 -0
- scripts/evaluate_geomanimol_residual.py +45 -0
- scripts/evaluate_geomanimol_torsion.py +40 -0
- scripts/evaluate_hif2a_bioactive_enrichment.py +91 -0
- scripts/evaluate_hif2a_pms_suite.py +440 -0
- scripts/evaluate_pdbbind_contact_pms.py +155 -0
- scripts/evaluate_platinum_external.py +114 -0
- scripts/evaluate_platinum_pool.py +82 -0
- scripts/evaluate_route2_calibrated_p.py +104 -0
- scripts/evaluate_route2_library_components.py +596 -0
- scripts/evaluate_route2_p_basin_budget.py +288 -0
- scripts/evaluate_stage1_checkpoint_only.py +30 -0
- scripts/export_dispersion_bond_audit.py +136 -0
- scripts/export_top20_shards.py +207 -0
- scripts/external_validation_metrics.py +152 -0
- scripts/fit_topology_prior.py +117 -0
- scripts/frozen_dispersion_audit.py +281 -0
- scripts/geomanimol_debug.py +50 -0
- scripts/geomanimol_smoke.py +37 -0
- scripts/infer_etflow_matched.py +524 -0
- scripts/inspect_checkpoint.py +12 -0
- scripts/inspect_conformer_capacity.py +71 -0
- scripts/inspect_pool.py +12 -0
- scripts/make_figure3_selection_loss.py +474 -0
- scripts/make_figure3_selection_summary.py +275 -0
- scripts/make_geom_debug_subset.py +62 -0
- scripts/make_low_reference_test_subset.py +70 -0
- scripts/merge_stage1_into_stage2.py +82 -0
- scripts/place_hif2a_candidates_for_evaluation.py +66 -0
- scripts/plot_hif2a_low_budget_efficiency.py +100 -0
- scripts/plot_prior_validation.py +48 -0
- scripts/precompute_stage2_assignments.py +90 -0
- scripts/prepare_bace_activity.py +98 -0
- scripts/prepare_bace_etkdg_benchmark.py +90 -0
- scripts/prepare_figure5_4x4_cases.py +470 -0
- scripts/prepare_geom_drugs_geomol_top20.py +472 -0
- scripts/prepare_pdbbind_core_raw.py +90 -0
- scripts/prepare_platinum_raw.py +74 -0
- scripts/prepare_pubchem_etkdg.py +54 -0
- scripts/rebuild_hif2a_final_sdf_and_metrics.py +55 -0
- scripts/rebuild_hif2a_fixed_pool_sweep.py +175 -0
- scripts/rebuild_hif2a_strict.py +220 -0
- scripts/recompute_hif2a_bootstrap.py +63 -0
- scripts/reference_free_geometry.py +112 -0
- scripts/render_binding_modes.py +413 -0
- scripts/render_figure5_4x4.py +330 -0
- scripts/render_figure6_pharmacophore_pymol.py +162 -0
- scripts/render_hif2a_contact_pharmacophore_pymol.py +414 -0
- scripts/render_hif2a_figure11.py +397 -0
- scripts/run_bace_2d_baseline.py +73 -0
- scripts/run_drugs_validation_matrix.py +225 -0
- scripts/run_external_reference_free.py +53 -0
- scripts/run_manimol_finalk_sensitivity.py +106 -0
- scripts/run_manimol_oversampling_sensitivity.py +109 -0
- scripts/run_pdisp_matched_intervention.py +116 -0
- scripts/run_stage1_stage2_cross20.py +83 -0
- scripts/score_hif2a_candidates_with_pgraph.py +221 -0
- scripts/summarize_dispersion_intervention.py +88 -0
- scripts/test_mean_dispersion_bridge.py +50 -0
- scripts/train_dispersion_head_diagnostic.py +320 -0
- scripts/train_dispersion_head_streaming.py +519 -0
- scripts/train_geomanimol.py +100 -0
- scripts/train_geomanimol_flow.py +26 -0
- scripts/train_geomanimol_residual.py +70 -0
- scripts/train_geomanimol_torsion.py +50 -0
- scripts/train_quantile_representation_diagnostic.py +204 -0
- scripts/validate_mean_dispersion_prior.py +173 -0
- scripts/validate_packed_shard.py +41 -0
- scripts/validate_stage1_prior.py +221 -0
- scripts/validate_top20_shards.py +36 -0
- scripts/validation/__init__.py +1 -0
- scripts/validation/hif2a_downstream_baselines.py +105 -0
- scripts/validation/hif2a_pairwise_ranker.py +218 -0
- scripts/verify_binding_mode_figure.py +47 -0
- torsion_manifold.py +281 -0
- train.py +78 -0
- train_graph_encoder.py +206 -0
- train_local_torsion_corrector.py +511 -0
- train_mean_dispersion_torsion_prior.py +329 -0
- train_probability_basin.py +235 -0
- train_torsion_generator.py +1631 -0
- train_torsion_manifold_prior.py +258 -0
- train_torsional_denoiser.py +415 -0
- trainer.py +1038 -0
- utils/__init__.py +7 -0
- utils/checkpoint.py +40 -0
- utils/device.py +17 -0
- utils/helpers.py +98 -0
- utils/kabsch.py +29 -0
- utils/lookup_table.py +164 -0
- utils/metrics.py +50 -0
- utils/optuna.py +111 -0
- utils/save_mol.py +366 -0
- utils/util.py +36 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Train the isolated ETKDG-scaffold + GeoManimol residual route."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
|
|
12
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
13
|
+
|
|
14
|
+
from geomanimol.data import load_molecule_groups
|
|
15
|
+
from geomanimol.geometry import DynamicGeometryMemory
|
|
16
|
+
from geomanimol.residual import ResidualGeoManimol, etkdg_scaffold
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def move(m, device):
|
|
20
|
+
return {**m, **{k: m[k].to(device) for k in ("atom_features", "edge_index", "edge_attr", "conformations")}}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def main():
|
|
24
|
+
p = argparse.ArgumentParser()
|
|
25
|
+
p.add_argument("--train", default="data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_train.pt")
|
|
26
|
+
p.add_argument("--val", default="data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_val.pt")
|
|
27
|
+
p.add_argument("--train-molecules", type=int, default=20)
|
|
28
|
+
p.add_argument("--val-molecules", type=int, default=20)
|
|
29
|
+
p.add_argument("--epochs", type=int, default=10)
|
|
30
|
+
p.add_argument("--node-dim", type=int, default=32)
|
|
31
|
+
p.add_argument("--n-basins", type=int, default=3)
|
|
32
|
+
p.add_argument("--manifold-dim", type=int, default=8)
|
|
33
|
+
p.add_argument("--diffusion-steps", type=int, default=1000)
|
|
34
|
+
p.add_argument("--scaffolds-per-molecule", type=int, default=4)
|
|
35
|
+
p.add_argument("--device", default="cpu")
|
|
36
|
+
p.add_argument("--output", default="outputs/geomanimol/residual_debug_checkpoint.pt")
|
|
37
|
+
p.add_argument("--resume", default="")
|
|
38
|
+
args = p.parse_args()
|
|
39
|
+
train = load_molecule_groups(args.train, args.train_molecules)
|
|
40
|
+
val = load_molecule_groups(args.val, args.val_molecules)
|
|
41
|
+
train_memory = DynamicGeometryMemory(args.n_basins, args.manifold_dim); train_memory.initialize(train)
|
|
42
|
+
val_memory = DynamicGeometryMemory(args.n_basins, args.manifold_dim); val_memory.initialize(val)
|
|
43
|
+
device = torch.device(args.device)
|
|
44
|
+
model = ResidualGeoManimol(args.node_dim, args.n_basins, args.manifold_dim, args.diffusion_steps).to(device)
|
|
45
|
+
if args.resume:
|
|
46
|
+
checkpoint = torch.load(args.resume, map_location=device, weights_only=False)
|
|
47
|
+
model.load_state_dict(checkpoint["model"])
|
|
48
|
+
print(f"loaded_residual_checkpoint={args.resume}")
|
|
49
|
+
opt = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-6)
|
|
50
|
+
for epoch in range(args.epochs):
|
|
51
|
+
model.train(); train_vals = []
|
|
52
|
+
for mol0 in train:
|
|
53
|
+
mol = move(mol0, device)
|
|
54
|
+
scaffold = etkdg_scaffold(mol["mol"], 20260808 + epoch * 10000 + mol0["mol_id"] % max(1, args.scaffolds_per_molecule))
|
|
55
|
+
out = model(mol, scaffold.to(device), mol["conformations"][0], train_memory.get(mol0["mol_id"]))
|
|
56
|
+
opt.zero_grad(set_to_none=True); out["loss"].backward(); torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0); opt.step()
|
|
57
|
+
train_vals.append(float(out["loss"].detach()))
|
|
58
|
+
model.eval(); val_vals = []
|
|
59
|
+
with torch.no_grad():
|
|
60
|
+
for mol0 in val:
|
|
61
|
+
mol = move(mol0, device); scaffold = etkdg_scaffold(mol["mol"], 30300 + mol0["mol_id"])
|
|
62
|
+
val_vals.append(float(model(mol, scaffold.to(device), mol["conformations"][0], val_memory.get(mol0["mol_id"]))["loss"]))
|
|
63
|
+
print(f"epoch={epoch} train_loss={sum(train_vals)/len(train_vals):.5f} val_loss={sum(val_vals)/len(val_vals):.5f}")
|
|
64
|
+
out = Path(args.output); out.parent.mkdir(parents=True, exist_ok=True)
|
|
65
|
+
torch.save({"model": model.state_dict(), "args": vars(args)}, out)
|
|
66
|
+
print(f"GEOMANIMOL_RESIDUAL_TRAIN_OK checkpoint={out} train={len(train)} val={len(val)}")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
if __name__ == "__main__":
|
|
70
|
+
main()
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Debug training for isolated basin-aware torsion residual diffusion."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
import argparse, sys, math
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import torch
|
|
7
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
8
|
+
from geomanimol.data import load_molecule_groups
|
|
9
|
+
from geomanimol.geometry import DynamicGeometryMemory
|
|
10
|
+
from geomanimol.residual import etkdg_scaffold
|
|
11
|
+
from geomanimol.torsion_residual import TorsionResidualGeoManimol, torsion_defs, torsions_from_coords
|
|
12
|
+
|
|
13
|
+
def move(m, d): return {**m, **{k:m[k].to(d) for k in ('atom_features','edge_index','edge_attr','conformations')}}
|
|
14
|
+
def assign_target(m, entry, scaffold, n_modes, temperature):
|
|
15
|
+
defs = torsion_defs(m['mol'])
|
|
16
|
+
if not defs:
|
|
17
|
+
return None, None
|
|
18
|
+
ref_t = torch.stack([torsions_from_coords(m['mol'], c, defs) for c in m['conformations']])
|
|
19
|
+
labels = entry['labels']; s_t = torsions_from_coords(m['mol'], scaffold, defs)
|
|
20
|
+
mode_dist = []; centers = []
|
|
21
|
+
for k in range(n_modes):
|
|
22
|
+
members = ref_t[labels == k] if (labels == k).any() else ref_t
|
|
23
|
+
centers.append(torch.atan2(torch.sin(members).mean(0), torch.cos(members).mean(0)))
|
|
24
|
+
delta = torch.atan2(torch.sin(members - s_t), torch.cos(members - s_t))
|
|
25
|
+
mode_dist.append(delta.abs().mean())
|
|
26
|
+
residual_centers = torch.stack([torch.atan2(torch.sin(c - s_t), torch.cos(c - s_t)) / torch.pi for c in centers])
|
|
27
|
+
return torch.softmax(-torch.stack(mode_dist) / temperature, dim=0), residual_centers
|
|
28
|
+
|
|
29
|
+
def main():
|
|
30
|
+
p=argparse.ArgumentParser(); p.add_argument('--train',default='data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_train.pt'); p.add_argument('--val',default='data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_val.pt'); p.add_argument('--train-molecules',type=int,default=20); p.add_argument('--val-molecules',type=int,default=20); p.add_argument('--epochs',type=int,default=20); p.add_argument('--node-dim',type=int,default=32); p.add_argument('--n-basins',type=int,default=3); p.add_argument('--manifold-dim',type=int,default=8); p.add_argument('--diffusion-steps',type=int,default=1000); p.add_argument('--assignment-weight',type=float,default=.5); p.add_argument('--basin-weight',type=float,default=.2); p.add_argument('--assignment-temperature',type=float,default=.3); p.add_argument('--output',default='outputs/geomanimol/torsion_debug_checkpoint.pt'); args=p.parse_args()
|
|
31
|
+
train=load_molecule_groups(args.train,args.train_molecules); val=load_molecule_groups(args.val,args.val_molecules); tm=DynamicGeometryMemory(args.n_basins,args.manifold_dim); tm.initialize(train); vm=DynamicGeometryMemory(args.n_basins,args.manifold_dim); vm.initialize(val)
|
|
32
|
+
model=TorsionResidualGeoManimol(args.node_dim,args.n_basins,args.manifold_dim,args.diffusion_steps,args.assignment_weight,args.basin_weight); opt=torch.optim.AdamW(model.parameters(),lr=1e-3,weight_decay=1e-6)
|
|
33
|
+
best_score = -1.0; best_state = None
|
|
34
|
+
for e in range(args.epochs):
|
|
35
|
+
model.train(); tr=[]; ta=[]
|
|
36
|
+
for m0 in train:
|
|
37
|
+
m=move(m0,'cpu'); s=etkdg_scaffold(m['mol'],20260808+e*1000+m0['mol_id'])
|
|
38
|
+
entry = tm.get(m0['mol_id'])
|
|
39
|
+
entry['assignment_target'], entry['basin_torsion_centers'] = assign_target(m, entry, s, args.n_basins, args.assignment_temperature)
|
|
40
|
+
out=model(m,s,m['conformations'][0],entry); opt.zero_grad(); out['loss'].backward(); torch.nn.utils.clip_grad_norm_(model.parameters(),1.0); opt.step(); tr.append(float(out['loss'])); ta.append(float(out['assignment_accuracy']))
|
|
41
|
+
model.eval(); va=[]; va_acc=[]
|
|
42
|
+
with torch.no_grad():
|
|
43
|
+
for m0 in val:
|
|
44
|
+
m=move(m0,'cpu'); s=etkdg_scaffold(m['mol'],30300+m0['mol_id']); entry=vm.get(m0['mol_id']); entry['assignment_target'], entry['basin_torsion_centers']=assign_target(m,entry,s,args.n_basins,args.assignment_temperature); out_val=model(m,s,m['conformations'][0],entry); va.append(float(out_val['loss'])); va_acc.append(float(out_val['assignment_accuracy']))
|
|
45
|
+
score=sum(va_acc)/len(va_acc)
|
|
46
|
+
print(f'epoch={e} train_loss={sum(tr)/len(tr):.5f} val_loss={sum(va)/len(va):.5f} val_assignment_acc={score:.3f}',flush=True)
|
|
47
|
+
if e == 0 or score > best_score:
|
|
48
|
+
best_score = score; best_state = {k:v.detach().clone() for k,v in model.state_dict().items()}
|
|
49
|
+
out=Path(args.output); out.parent.mkdir(parents=True,exist_ok=True); model.load_state_dict(best_state); torch.save({'model':model.state_dict(),'args':vars(args),'best_assignment_accuracy':best_score},out); print(f'GEOMANIMOL_TORSION_TRAIN_OK checkpoint={out} best_assignment_acc={best_score:.3f}')
|
|
50
|
+
if __name__=='__main__': main()
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Train and audit ordered pairwise Q25/Q50/Q75 relational heads.
|
|
3
|
+
|
|
4
|
+
This is the first, prediction-only stage of the quantile sufficiency study.
|
|
5
|
+
It uses the same frozen graph encoder and the same UMAP proximity target as
|
|
6
|
+
the existing mean/dispersion audit. It deliberately does not claim that
|
|
7
|
+
quantiles affect Stage-II generation until a quantile-aware decoder is trained
|
|
8
|
+
and evaluated under the matched generation protocol.
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
import csv
|
|
14
|
+
import json
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from types import SimpleNamespace
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
import torch
|
|
20
|
+
import torch.nn as nn
|
|
21
|
+
from scipy.stats import pearsonr, spearmanr
|
|
22
|
+
from torch_geometric.data import Batch
|
|
23
|
+
|
|
24
|
+
from dataset.drugdataset import QM9Dataset
|
|
25
|
+
from models.model0 import prob_low_dim_kernel
|
|
26
|
+
from train_torsional_denoiser import load_base_model
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class OrderedQuantileHead(nn.Module):
|
|
30
|
+
"""Map pair features to ordered values in [0, 1]."""
|
|
31
|
+
|
|
32
|
+
def __init__(self, input_dim: int, hidden_dim: int = 256):
|
|
33
|
+
super().__init__()
|
|
34
|
+
self.net = nn.Sequential(
|
|
35
|
+
nn.Linear(input_dim, hidden_dim), nn.SiLU(),
|
|
36
|
+
nn.Linear(hidden_dim, hidden_dim), nn.SiLU(),
|
|
37
|
+
nn.Linear(hidden_dim, 3),
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
def forward(self, pair_features: torch.Tensor) -> torch.Tensor:
|
|
41
|
+
raw = self.net(pair_features)
|
|
42
|
+
q25 = torch.sigmoid(raw[:, 0])
|
|
43
|
+
d50 = torch.sigmoid(raw[:, 1])
|
|
44
|
+
d75 = torch.sigmoid(raw[:, 2])
|
|
45
|
+
q50 = q25 + (1.0 - q25) * d50
|
|
46
|
+
q75 = q50 + (1.0 - q50) * d75
|
|
47
|
+
return torch.stack((q25, q50, q75), dim=-1)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _groups(dataset, indices, limit):
|
|
51
|
+
groups = {}
|
|
52
|
+
for idx in indices:
|
|
53
|
+
item = dataset[int(idx)]
|
|
54
|
+
mid = int(item.mol_id.reshape(-1)[0])
|
|
55
|
+
if mid not in groups and limit > 0 and len(groups) >= limit:
|
|
56
|
+
continue
|
|
57
|
+
groups.setdefault(mid, []).append(item)
|
|
58
|
+
return groups
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _pair_indices(n, heavy):
|
|
62
|
+
if heavy is None:
|
|
63
|
+
return torch.triu_indices(n, n, offset=1)
|
|
64
|
+
atoms = torch.where(heavy)[0]
|
|
65
|
+
return torch.combinations(atoms, r=2).t()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _corr(x, y):
|
|
69
|
+
x, y = np.asarray(x, dtype=float), np.asarray(y, dtype=float)
|
|
70
|
+
if len(x) < 3 or np.ptp(x) < 1e-12 or np.ptp(y) < 1e-12:
|
|
71
|
+
return None, None
|
|
72
|
+
return float(pearsonr(x, y).statistic), float(spearmanr(x, y).statistic)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def collect_split(dataset, split_indices, limit, args, model, device):
|
|
76
|
+
records = []
|
|
77
|
+
groups = _groups(dataset, split_indices, limit)
|
|
78
|
+
for mid, group in groups.items():
|
|
79
|
+
batch = Batch.from_data_list([group[0]]).to(device)
|
|
80
|
+
with torch.no_grad():
|
|
81
|
+
node = model.gnn(batch)
|
|
82
|
+
edge = model._get_local_edge_index(batch.edge_index, batch.batch == 0)
|
|
83
|
+
pair_feat = model._build_pair_features(node, edge)
|
|
84
|
+
q = torch.stack([
|
|
85
|
+
prob_low_dim_kernel(item.pos.to(device), args.a, args.b, kernel="umap")
|
|
86
|
+
for item in group[:args.max_confs]
|
|
87
|
+
])
|
|
88
|
+
target = torch.stack([
|
|
89
|
+
torch.quantile(q, level, dim=0) for level in (0.25, 0.50, 0.75)
|
|
90
|
+
], dim=-1)
|
|
91
|
+
heavy = (batch.x[:, 0] < 0.5).detach().cpu() if hasattr(batch, "x") else None
|
|
92
|
+
ij = _pair_indices(node.size(0), heavy)
|
|
93
|
+
records.append({
|
|
94
|
+
"molecule_id": mid,
|
|
95
|
+
"pair_features": pair_feat[ij[0].to(device), ij[1].to(device)].detach().cpu(),
|
|
96
|
+
"target": target[ij[0].to(device), ij[1].to(device)].detach().cpu(),
|
|
97
|
+
})
|
|
98
|
+
if not records:
|
|
99
|
+
raise ValueError("No molecules collected for split")
|
|
100
|
+
return records
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def evaluate(head, records, device):
|
|
104
|
+
pred_all, target_all = [], []
|
|
105
|
+
with torch.no_grad():
|
|
106
|
+
for record in records:
|
|
107
|
+
pred_all.append(head(record["pair_features"].to(device)).cpu())
|
|
108
|
+
target_all.append(record["target"])
|
|
109
|
+
pred, target = torch.cat(pred_all), torch.cat(target_all)
|
|
110
|
+
rows = []
|
|
111
|
+
for index, name in enumerate(("q25", "q50", "q75")):
|
|
112
|
+
p, s = _corr(target[:, index].numpy(), pred[:, index].numpy())
|
|
113
|
+
rows.append({
|
|
114
|
+
"quantile": name,
|
|
115
|
+
"n_pairs": int(target.size(0)),
|
|
116
|
+
"pearson": p,
|
|
117
|
+
"spearman": s,
|
|
118
|
+
"mae": float((pred[:, index] - target[:, index]).abs().mean()),
|
|
119
|
+
})
|
|
120
|
+
return rows
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def main():
|
|
124
|
+
parser = argparse.ArgumentParser()
|
|
125
|
+
parser.add_argument("--checkpoint", required=True)
|
|
126
|
+
parser.add_argument("--data-root", required=True)
|
|
127
|
+
parser.add_argument("--raw-prefix", default="geom_drugs")
|
|
128
|
+
parser.add_argument("--train-raw-filename", default="")
|
|
129
|
+
parser.add_argument("--val-raw-filename", default="")
|
|
130
|
+
parser.add_argument("--test-raw-filename", default="")
|
|
131
|
+
parser.add_argument("--max-confs", type=int, default=30)
|
|
132
|
+
parser.add_argument("--train-molecules", type=int, default=200)
|
|
133
|
+
parser.add_argument("--valid-molecules", type=int, default=50)
|
|
134
|
+
parser.add_argument("--test-molecules", type=int, default=50)
|
|
135
|
+
parser.add_argument("--a", type=float, default=1.6)
|
|
136
|
+
parser.add_argument("--b", type=float, default=0.8)
|
|
137
|
+
parser.add_argument("--epochs", type=int, default=30)
|
|
138
|
+
parser.add_argument("--lr", type=float, default=1e-4)
|
|
139
|
+
parser.add_argument("--hidden-dim", type=int, default=256)
|
|
140
|
+
parser.add_argument("--seed", type=int, default=20260921)
|
|
141
|
+
parser.add_argument("--device", default="cpu")
|
|
142
|
+
parser.add_argument("--output-dir", required=True)
|
|
143
|
+
args = parser.parse_args()
|
|
144
|
+
torch.manual_seed(args.seed)
|
|
145
|
+
np.random.seed(args.seed)
|
|
146
|
+
out = Path(args.output_dir)
|
|
147
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
148
|
+
device = torch.device(args.device)
|
|
149
|
+
model = load_base_model(SimpleNamespace(checkpoint=args.checkpoint, max_confs=args.max_confs), str(device))
|
|
150
|
+
model.to(device).eval()
|
|
151
|
+
for param in model.parameters():
|
|
152
|
+
param.requires_grad_(False)
|
|
153
|
+
dataset = QM9Dataset(
|
|
154
|
+
name="Drugs", root=args.data_root, max_confs=args.max_confs,
|
|
155
|
+
raw_prefix=args.raw_prefix,
|
|
156
|
+
train_raw_filename=args.train_raw_filename or None,
|
|
157
|
+
val_raw_filename=args.val_raw_filename or None,
|
|
158
|
+
test_raw_filename=args.test_raw_filename or None,
|
|
159
|
+
)
|
|
160
|
+
train = collect_split(dataset, dataset.train_index, args.train_molecules, args, model, device)
|
|
161
|
+
valid = collect_split(dataset, dataset.valid_index, args.valid_molecules, args, model, device)
|
|
162
|
+
test = collect_split(dataset, dataset.test_index, args.test_molecules, args, model, device)
|
|
163
|
+
in_dim = int(train[0]["pair_features"].size(-1))
|
|
164
|
+
head = OrderedQuantileHead(in_dim, args.hidden_dim).to(device)
|
|
165
|
+
optimizer = torch.optim.Adam(head.parameters(), lr=args.lr)
|
|
166
|
+
history = []
|
|
167
|
+
best = float("inf")
|
|
168
|
+
for epoch in range(args.epochs):
|
|
169
|
+
head.train()
|
|
170
|
+
losses = []
|
|
171
|
+
for record in train:
|
|
172
|
+
optimizer.zero_grad()
|
|
173
|
+
pred = head(record["pair_features"].to(device))
|
|
174
|
+
loss = (pred - record["target"].to(device)).square().mean()
|
|
175
|
+
loss.backward()
|
|
176
|
+
optimizer.step()
|
|
177
|
+
losses.append(float(loss.detach().cpu()))
|
|
178
|
+
head.eval()
|
|
179
|
+
with torch.no_grad():
|
|
180
|
+
valid_loss = np.mean([
|
|
181
|
+
float((head(r["pair_features"].to(device)) - r["target"].to(device)).square().mean().cpu())
|
|
182
|
+
for r in valid
|
|
183
|
+
])
|
|
184
|
+
history.append({"epoch": epoch, "train_mse": float(np.mean(losses)), "valid_mse": float(valid_loss)})
|
|
185
|
+
if valid_loss < best:
|
|
186
|
+
best = valid_loss
|
|
187
|
+
torch.save({"model_state_dict": head.state_dict(), "input_dim": in_dim,
|
|
188
|
+
"hidden_dim": args.hidden_dim, "seed": args.seed}, out / "best.pth")
|
|
189
|
+
print(f"epoch={epoch} train_mse={np.mean(losses):.6g} valid_mse={valid_loss:.6g}", flush=True)
|
|
190
|
+
head.load_state_dict(torch.load(out / "best.pth", map_location=device, weights_only=True)["model_state_dict"])
|
|
191
|
+
diagnostics = evaluate(head.eval(), test, device)
|
|
192
|
+
with (out / "quantile_prediction_diagnostics.csv").open("w", newline="") as handle:
|
|
193
|
+
writer = csv.DictWriter(handle, fieldnames=list(diagnostics[0]))
|
|
194
|
+
writer.writeheader(); writer.writerows(diagnostics)
|
|
195
|
+
summary = {"args": vars(args), "n_train_molecules": len(train), "n_valid_molecules": len(valid),
|
|
196
|
+
"n_test_molecules": len(test), "parameter_count": sum(p.numel() for p in head.parameters()),
|
|
197
|
+
"best_valid_mse": best, "diagnostics": diagnostics}
|
|
198
|
+
(out / "history.json").write_text(json.dumps(history, indent=2))
|
|
199
|
+
(out / "summary.json").write_text(json.dumps(summary, indent=2))
|
|
200
|
+
print(json.dumps(summary, indent=2))
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
if __name__ == "__main__":
|
|
204
|
+
main()
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Direct held-out validation of the mean--dispersion Stage-I heads."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import json
|
|
8
|
+
import math
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
import pandas as pd
|
|
14
|
+
import torch
|
|
15
|
+
from scipy.stats import pearsonr, spearmanr
|
|
16
|
+
from torch_geometric.data import Batch
|
|
17
|
+
|
|
18
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
19
|
+
if str(ROOT) not in sys.path:
|
|
20
|
+
sys.path.insert(0, str(ROOT))
|
|
21
|
+
|
|
22
|
+
from dataset.drugdataset import QM9Dataset
|
|
23
|
+
from models.model0 import prob_low_dim_kernel
|
|
24
|
+
from train_torsional_denoiser import load_base_model
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def teacher_kernel_parameters(model, graph, conf_id, fixed_a=0.0, fixed_b=0.0):
|
|
28
|
+
"""Use fixed UMAP parameters when requested, otherwise model-predicted ones."""
|
|
29
|
+
fixed_a = float(fixed_a)
|
|
30
|
+
fixed_b = float(fixed_b)
|
|
31
|
+
if (fixed_a > 0.0) != (fixed_b > 0.0):
|
|
32
|
+
raise ValueError("fixed_a and fixed_b must be supplied together")
|
|
33
|
+
if fixed_a > 0.0 and fixed_b > 0.0:
|
|
34
|
+
return (
|
|
35
|
+
graph.new_tensor(fixed_a).clamp(1e-4, 1e4),
|
|
36
|
+
graph.new_tensor(fixed_b).clamp(1e-4, 10.0),
|
|
37
|
+
)
|
|
38
|
+
return model.predict_ab(graph, conf_id)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def finite_corr(fn, x, y):
|
|
42
|
+
if len(x) < 2 or np.ptp(x) <= 1e-12 or np.ptp(y) <= 1e-12:
|
|
43
|
+
return float("nan")
|
|
44
|
+
result = fn(x, y)
|
|
45
|
+
return float(getattr(result, "statistic", result[0]))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def top_overlap(x, y, fraction):
|
|
49
|
+
count = max(1, int(math.ceil(fraction * len(x))))
|
|
50
|
+
a = set(np.argpartition(x, -count)[-count:].tolist())
|
|
51
|
+
b = set(np.argpartition(y, -count)[-count:].tolist())
|
|
52
|
+
return len(a & b) / len(a | b) if a | b else float("nan")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def bootstrap(values, reps, seed):
|
|
56
|
+
values = np.asarray(values, dtype=float)
|
|
57
|
+
values = values[np.isfinite(values)]
|
|
58
|
+
if not len(values):
|
|
59
|
+
return float("nan"), float("nan")
|
|
60
|
+
rng = np.random.default_rng(seed)
|
|
61
|
+
means = np.asarray([rng.choice(values, len(values), replace=True).mean() for _ in range(reps)])
|
|
62
|
+
return tuple(np.quantile(means, [0.025, 0.975]))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def torsion_count(item):
|
|
66
|
+
torsions = getattr(item, "torsion_index", None)
|
|
67
|
+
if torsions is None or torsions.numel() == 0:
|
|
68
|
+
return 0
|
|
69
|
+
rows = torsions.t() if torsions.size(0) == 4 else torsions
|
|
70
|
+
return len({tuple(sorted((int(row[1]), int(row[2])))) for row in rows})
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@torch.inference_mode()
|
|
74
|
+
def evaluate_group(model, group, device, max_confs, kernel, top_fraction, fixed_a=0.0, fixed_b=0.0):
|
|
75
|
+
conformers = group[:max_confs]
|
|
76
|
+
first = conformers[0]
|
|
77
|
+
batch = Batch.from_data_list([first]).to(device)
|
|
78
|
+
node = model.gnn(batch)
|
|
79
|
+
graph = model.pool(node, batch.batch)[0]
|
|
80
|
+
edge = model._get_local_edge_index(batch.edge_index, batch.batch == 0)
|
|
81
|
+
p_mean, p_disp, _ = model._predict_mean_dispersion(node, edge)
|
|
82
|
+
teachers = []
|
|
83
|
+
for item in conformers:
|
|
84
|
+
cid = int(torch.as_tensor(item.conf_id).reshape(-1)[0].item())
|
|
85
|
+
a, b = teacher_kernel_parameters(model, graph, cid, fixed_a, fixed_b)
|
|
86
|
+
teachers.append(prob_low_dim_kernel(item.pos.to(device=device, dtype=node.dtype), a, b, kernel=kernel))
|
|
87
|
+
stack = torch.stack(teachers, dim=0)
|
|
88
|
+
targets = {"mean": stack.mean(0), "dispersion": stack.var(0, unbiased=False)}
|
|
89
|
+
upper = torch.triu(torch.ones_like(p_mean, dtype=torch.bool), diagonal=1)
|
|
90
|
+
rows = []
|
|
91
|
+
for head, pred_tensor in (("mean", p_mean), ("dispersion", p_disp)):
|
|
92
|
+
pred = pred_tensor[upper].cpu().numpy().astype(float)
|
|
93
|
+
truth = targets[head][upper].cpu().numpy().astype(float)
|
|
94
|
+
delta = pred - truth
|
|
95
|
+
rows.append({
|
|
96
|
+
"molecule_id": int(first.mol_id.reshape(-1)[0]),
|
|
97
|
+
"n_atoms": int(first.pos.size(0)),
|
|
98
|
+
"n_rotatable_bonds": torsion_count(first),
|
|
99
|
+
"n_reference_conformers": len(group),
|
|
100
|
+
"n_teacher_conformers": len(conformers),
|
|
101
|
+
"head": head,
|
|
102
|
+
"mae": float(np.abs(delta).mean()),
|
|
103
|
+
"rmse": float(np.sqrt(np.square(delta).mean())),
|
|
104
|
+
"pearson": finite_corr(pearsonr, pred, truth),
|
|
105
|
+
"spearman": finite_corr(spearmanr, pred, truth),
|
|
106
|
+
"topk_overlap": top_overlap(pred, truth, top_fraction),
|
|
107
|
+
})
|
|
108
|
+
return rows
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def main():
|
|
112
|
+
p = argparse.ArgumentParser()
|
|
113
|
+
p.add_argument("--checkpoint", required=True)
|
|
114
|
+
p.add_argument("--data-root", default="data")
|
|
115
|
+
p.add_argument("--raw-prefix", default="geom_drugs")
|
|
116
|
+
p.add_argument("--max-confs", type=int, default=30)
|
|
117
|
+
p.add_argument("--max-molecules", type=int, default=50)
|
|
118
|
+
p.add_argument("--kernel", default="umap")
|
|
119
|
+
p.add_argument("--fixed-kernel-a", type=float, default=0.0)
|
|
120
|
+
p.add_argument("--fixed-kernel-b", type=float, default=0.0)
|
|
121
|
+
p.add_argument("--top-fraction", type=float, default=0.10)
|
|
122
|
+
p.add_argument("--bootstrap", type=int, default=2000)
|
|
123
|
+
p.add_argument("--seed", type=int, default=20260910)
|
|
124
|
+
p.add_argument("--device", default="cuda")
|
|
125
|
+
p.add_argument("--output-dir", default="results/prior_validation_mean_dispersion")
|
|
126
|
+
args = p.parse_args()
|
|
127
|
+
device = torch.device(args.device if args.device != "auto" else ("cuda" if torch.cuda.is_available() else "cpu"))
|
|
128
|
+
dataset = QM9Dataset(name="Drugs", root=args.data_root, max_confs=args.max_confs, raw_prefix=args.raw_prefix)
|
|
129
|
+
groups_by_id = {}
|
|
130
|
+
for index in dataset.test_index:
|
|
131
|
+
item = dataset[int(index)]
|
|
132
|
+
mol_id = int(item.mol_id.reshape(-1)[0])
|
|
133
|
+
groups_by_id.setdefault(mol_id, []).append(item)
|
|
134
|
+
groups = list(groups_by_id.values())[:args.max_molecules]
|
|
135
|
+
model = load_base_model(argparse.Namespace(checkpoint=args.checkpoint, max_confs=args.max_confs), device)
|
|
136
|
+
rows = []
|
|
137
|
+
for number, group in enumerate(groups, 1):
|
|
138
|
+
rows.extend(evaluate_group(
|
|
139
|
+
model, group, device, args.max_confs, args.kernel, args.top_fraction,
|
|
140
|
+
args.fixed_kernel_a, args.fixed_kernel_b,
|
|
141
|
+
))
|
|
142
|
+
if number == 1 or number % 10 == 0 or number == len(groups):
|
|
143
|
+
print(f"[{number}/{len(groups)}]", flush=True)
|
|
144
|
+
frame = pd.DataFrame(rows)
|
|
145
|
+
strata = {
|
|
146
|
+
"all": np.ones(len(frame), dtype=bool),
|
|
147
|
+
"low_0_5": frame.n_rotatable_bonds <= 5,
|
|
148
|
+
"medium_6_8": (frame.n_rotatable_bonds >= 6) & (frame.n_rotatable_bonds <= 8),
|
|
149
|
+
"high_ge_9": frame.n_rotatable_bonds >= 9,
|
|
150
|
+
}
|
|
151
|
+
summary = []
|
|
152
|
+
metrics = ["mae", "rmse", "pearson", "spearman", "topk_overlap"]
|
|
153
|
+
for stratum, mask in strata.items():
|
|
154
|
+
for head in ("mean", "dispersion"):
|
|
155
|
+
subset = frame[mask & (frame["head"] == head)]
|
|
156
|
+
for offset, metric in enumerate(metrics):
|
|
157
|
+
values = subset[metric].to_numpy(float)
|
|
158
|
+
lo, hi = bootstrap(values, args.bootstrap, args.seed + offset)
|
|
159
|
+
finite = values[np.isfinite(values)]
|
|
160
|
+
summary.append({"stratum": stratum, "head": head, "metric": metric,
|
|
161
|
+
"n_molecules": len(finite), "mean": float(np.mean(finite)) if len(finite) else np.nan,
|
|
162
|
+
"median": float(np.median(finite)) if len(finite) else np.nan,
|
|
163
|
+
"ci95_low": lo, "ci95_high": hi})
|
|
164
|
+
output = Path(args.output_dir)
|
|
165
|
+
output.mkdir(parents=True, exist_ok=True)
|
|
166
|
+
frame.to_csv(output / "prior_validation_per_molecule.csv", index=False)
|
|
167
|
+
pd.DataFrame(summary).to_csv(output / "prior_validation_summary.csv", index=False)
|
|
168
|
+
(output / "config.json").write_text(json.dumps(vars(args), indent=2), encoding="utf-8")
|
|
169
|
+
print(pd.DataFrame(summary).to_string(index=False, float_format=lambda x: f"{x:.5f}"))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
if __name__ == "__main__":
|
|
173
|
+
main()
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Repack one legacy record shard and validate packed-record equivalence."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import itertools
|
|
7
|
+
import os
|
|
8
|
+
|
|
9
|
+
import torch
|
|
10
|
+
|
|
11
|
+
from export_top20_shards import pack_molecule
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main():
|
|
15
|
+
parser = argparse.ArgumentParser()
|
|
16
|
+
parser.add_argument("--input", required=True)
|
|
17
|
+
parser.add_argument("--output", required=True)
|
|
18
|
+
args = parser.parse_args()
|
|
19
|
+
|
|
20
|
+
records = torch.load(args.input, map_location="cpu", weights_only=False)["records"]
|
|
21
|
+
groups = [list(group) for _, group in itertools.groupby(
|
|
22
|
+
records, key=lambda item: int(item.mol_id.item())
|
|
23
|
+
)]
|
|
24
|
+
molecules = [pack_molecule(group) for group in groups]
|
|
25
|
+
torch.save({"molecules": molecules}, args.output, pickle_protocol=4)
|
|
26
|
+
|
|
27
|
+
restored = torch.load(args.output, map_location="cpu", weights_only=False)["molecules"]
|
|
28
|
+
restored_count = sum(int(item["pos"].size(0)) for item in restored)
|
|
29
|
+
if restored_count != len(records):
|
|
30
|
+
raise RuntimeError("Packed record count mismatch")
|
|
31
|
+
for group, packed in zip(groups, restored):
|
|
32
|
+
if not torch.equal(torch.stack([item.pos for item in group]), packed["pos"]):
|
|
33
|
+
raise RuntimeError("Packed coordinates differ from source")
|
|
34
|
+
print("source_bytes=%d packed_bytes=%d conformers=%d molecules=%d ratio=%.4f" % (
|
|
35
|
+
os.path.getsize(args.input), os.path.getsize(args.output), len(records),
|
|
36
|
+
len(molecules), os.path.getsize(args.output) / os.path.getsize(args.input)
|
|
37
|
+
))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
if __name__ == "__main__":
|
|
41
|
+
main()
|