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.
Files changed (186) hide show
  1. args_parse.py +593 -0
  2. candidate_corrector.py +161 -0
  3. candidate_scorer.py +359 -0
  4. conformend/__init__.py +36 -0
  5. conformend/refiner.py +25 -0
  6. dataset/__init__.py +1 -0
  7. dataset/drugdataset.py +644 -0
  8. dataset/manifold.py +237 -0
  9. dataset/sharded_drugdataset.py +124 -0
  10. dataset/smiles2graph.py +217 -0
  11. dualmapref/__init__.py +24 -0
  12. dualmapref/refiner.py +16 -0
  13. dualmapref-0.2.8.dist-info/LICENSE +21 -0
  14. dualmapref-0.2.8.dist-info/METADATA +167 -0
  15. dualmapref-0.2.8.dist-info/RECORD +186 -0
  16. dualmapref-0.2.8.dist-info/WHEEL +5 -0
  17. dualmapref-0.2.8.dist-info/entry_points.txt +10 -0
  18. dualmapref-0.2.8.dist-info/top_level.txt +29 -0
  19. exputils.py +289 -0
  20. infer.py +23 -0
  21. infer_base_conformers.py +5589 -0
  22. infer_mean_dispersion_torsion.py +1117 -0
  23. infer_torsion.py +752 -0
  24. infer_torsional_denoiser.py +1021 -0
  25. manimol/__init__.py +30 -0
  26. manimol/__main__.py +6 -0
  27. manimol/cli.py +36 -0
  28. manimol/full_cli.py +41 -0
  29. manimol/legacy_imports.py +43 -0
  30. manimol/manifest.py +56 -0
  31. manimol/postprocess.py +148 -0
  32. manimol/priors.py +60 -0
  33. manimol/protocols.py +119 -0
  34. manimol/refiner.py +645 -0
  35. manimol/runtime.py +194 -0
  36. manimol/selection.py +85 -0
  37. manimol/torsion.py +22 -0
  38. manimol/train_cli.py +14 -0
  39. mixture_flow/src/__init__.py +1 -0
  40. mixture_flow/src/_path.py +7 -0
  41. mixture_flow/src/infer28_mixture_torsion_flow.py +1093 -0
  42. mixture_flow/src/mixture_torsion_flow.py +291 -0
  43. mixture_flow/src/summarize_mixture_debug.py +80 -0
  44. mixture_flow/src/train28_mixture_torsion_flow.py +583 -0
  45. mixture_torsion_flow.py +7 -0
  46. models/ manifold_learning.py +86 -0
  47. models/__init__.py +8 -0
  48. models/dist.py +273 -0
  49. models/dist2coords.py +23 -0
  50. models/egnn.py +107 -0
  51. models/experimental_backbones.py +364 -0
  52. models/gnnconv.py +512 -0
  53. models/kernel_inversion.py +141 -0
  54. models/kernels.py +427 -0
  55. models/losses.py +154 -0
  56. models/mean_dispersion_bridge.py +119 -0
  57. models/mean_dispersion_torsion_context.py +155 -0
  58. models/model.py +73 -0
  59. models/model0.py +2375 -0
  60. models/vis.py +128 -0
  61. pgraph_controls.py +107 -0
  62. scripts/__init__.py +2 -0
  63. scripts/analyze_basin_alignment.py +63 -0
  64. scripts/analyze_hif2a_conformer_aggregation.py +267 -0
  65. scripts/analyze_hif2a_contact_pharmacophore.py +1111 -0
  66. scripts/analyze_hif2a_fixed_budget.py +202 -0
  67. scripts/analyze_hif2a_pharmacophore.py +405 -0
  68. scripts/analyze_hif2a_pharmacophore_match_metric.py +386 -0
  69. scripts/analyze_p_control_candidate_flow.py +154 -0
  70. scripts/analyze_scaffold_basin.py +83 -0
  71. scripts/audit_dispersion_proposals.py +208 -0
  72. scripts/audit_etflow_protocol.py +258 -0
  73. scripts/audit_external_overlap.py +150 -0
  74. scripts/audit_oldbest_fulltrainval_split.py +82 -0
  75. scripts/audit_pdbbind_contact_pharmacophores.py +146 -0
  76. scripts/audit_qm9_geomol_test_split.py +94 -0
  77. scripts/audit_stage1_stage2_cross.py +131 -0
  78. scripts/build_geom_drugs_lowref_diagnostic.py +84 -0
  79. scripts/build_qm9_backfill_manifest.py +99 -0
  80. scripts/check_etflow_runtime.py +56 -0
  81. scripts/compare_prior_sources.py +122 -0
  82. scripts/compose_binding_mode_figure.py +87 -0
  83. scripts/compose_hif2a_contact_pharmacophore_suppfig.py +255 -0
  84. scripts/compose_hif2a_functional_geometry_pipeline.py +266 -0
  85. scripts/compose_manimol_internal_architecture.py +291 -0
  86. scripts/diagnose_flow_basin_intervention.py +54 -0
  87. scripts/diagnose_hif2a_pharmacophore_budget.py +185 -0
  88. scripts/diagnose_route2_p_ranking.py +123 -0
  89. scripts/discover_binding_mode_inputs.py +30 -0
  90. scripts/evaluate_functional_prior_stage1.py +230 -0
  91. scripts/evaluate_geomanimol.py +74 -0
  92. scripts/evaluate_geomanimol_flow.py +23 -0
  93. scripts/evaluate_geomanimol_multiscaffold.py +46 -0
  94. scripts/evaluate_geomanimol_residual.py +45 -0
  95. scripts/evaluate_geomanimol_torsion.py +40 -0
  96. scripts/evaluate_hif2a_bioactive_enrichment.py +91 -0
  97. scripts/evaluate_hif2a_pms_suite.py +440 -0
  98. scripts/evaluate_pdbbind_contact_pms.py +155 -0
  99. scripts/evaluate_platinum_external.py +114 -0
  100. scripts/evaluate_platinum_pool.py +82 -0
  101. scripts/evaluate_route2_calibrated_p.py +104 -0
  102. scripts/evaluate_route2_library_components.py +596 -0
  103. scripts/evaluate_route2_p_basin_budget.py +288 -0
  104. scripts/evaluate_stage1_checkpoint_only.py +30 -0
  105. scripts/export_dispersion_bond_audit.py +136 -0
  106. scripts/export_top20_shards.py +207 -0
  107. scripts/external_validation_metrics.py +152 -0
  108. scripts/fit_topology_prior.py +117 -0
  109. scripts/frozen_dispersion_audit.py +281 -0
  110. scripts/geomanimol_debug.py +50 -0
  111. scripts/geomanimol_smoke.py +37 -0
  112. scripts/infer_etflow_matched.py +524 -0
  113. scripts/inspect_checkpoint.py +12 -0
  114. scripts/inspect_conformer_capacity.py +71 -0
  115. scripts/inspect_pool.py +12 -0
  116. scripts/make_figure3_selection_loss.py +474 -0
  117. scripts/make_figure3_selection_summary.py +275 -0
  118. scripts/make_geom_debug_subset.py +62 -0
  119. scripts/make_low_reference_test_subset.py +70 -0
  120. scripts/merge_stage1_into_stage2.py +82 -0
  121. scripts/place_hif2a_candidates_for_evaluation.py +66 -0
  122. scripts/plot_hif2a_low_budget_efficiency.py +100 -0
  123. scripts/plot_prior_validation.py +48 -0
  124. scripts/precompute_stage2_assignments.py +90 -0
  125. scripts/prepare_bace_activity.py +98 -0
  126. scripts/prepare_bace_etkdg_benchmark.py +90 -0
  127. scripts/prepare_figure5_4x4_cases.py +470 -0
  128. scripts/prepare_geom_drugs_geomol_top20.py +472 -0
  129. scripts/prepare_pdbbind_core_raw.py +90 -0
  130. scripts/prepare_platinum_raw.py +74 -0
  131. scripts/prepare_pubchem_etkdg.py +54 -0
  132. scripts/rebuild_hif2a_final_sdf_and_metrics.py +55 -0
  133. scripts/rebuild_hif2a_fixed_pool_sweep.py +175 -0
  134. scripts/rebuild_hif2a_strict.py +220 -0
  135. scripts/recompute_hif2a_bootstrap.py +63 -0
  136. scripts/reference_free_geometry.py +112 -0
  137. scripts/render_binding_modes.py +413 -0
  138. scripts/render_figure5_4x4.py +330 -0
  139. scripts/render_figure6_pharmacophore_pymol.py +162 -0
  140. scripts/render_hif2a_contact_pharmacophore_pymol.py +414 -0
  141. scripts/render_hif2a_figure11.py +397 -0
  142. scripts/run_bace_2d_baseline.py +73 -0
  143. scripts/run_drugs_validation_matrix.py +225 -0
  144. scripts/run_external_reference_free.py +53 -0
  145. scripts/run_manimol_finalk_sensitivity.py +106 -0
  146. scripts/run_manimol_oversampling_sensitivity.py +109 -0
  147. scripts/run_pdisp_matched_intervention.py +116 -0
  148. scripts/run_stage1_stage2_cross20.py +83 -0
  149. scripts/score_hif2a_candidates_with_pgraph.py +221 -0
  150. scripts/summarize_dispersion_intervention.py +88 -0
  151. scripts/test_mean_dispersion_bridge.py +50 -0
  152. scripts/train_dispersion_head_diagnostic.py +320 -0
  153. scripts/train_dispersion_head_streaming.py +519 -0
  154. scripts/train_geomanimol.py +100 -0
  155. scripts/train_geomanimol_flow.py +26 -0
  156. scripts/train_geomanimol_residual.py +70 -0
  157. scripts/train_geomanimol_torsion.py +50 -0
  158. scripts/train_quantile_representation_diagnostic.py +204 -0
  159. scripts/validate_mean_dispersion_prior.py +173 -0
  160. scripts/validate_packed_shard.py +41 -0
  161. scripts/validate_stage1_prior.py +221 -0
  162. scripts/validate_top20_shards.py +36 -0
  163. scripts/validation/__init__.py +1 -0
  164. scripts/validation/hif2a_downstream_baselines.py +105 -0
  165. scripts/validation/hif2a_pairwise_ranker.py +218 -0
  166. scripts/verify_binding_mode_figure.py +47 -0
  167. torsion_manifold.py +281 -0
  168. train.py +78 -0
  169. train_graph_encoder.py +206 -0
  170. train_local_torsion_corrector.py +511 -0
  171. train_mean_dispersion_torsion_prior.py +329 -0
  172. train_probability_basin.py +235 -0
  173. train_torsion_generator.py +1631 -0
  174. train_torsion_manifold_prior.py +258 -0
  175. train_torsional_denoiser.py +415 -0
  176. trainer.py +1038 -0
  177. utils/__init__.py +7 -0
  178. utils/checkpoint.py +40 -0
  179. utils/device.py +17 -0
  180. utils/helpers.py +98 -0
  181. utils/kabsch.py +29 -0
  182. utils/lookup_table.py +164 -0
  183. utils/metrics.py +50 -0
  184. utils/optuna.py +111 -0
  185. utils/save_mol.py +366 -0
  186. utils/util.py +36 -0
@@ -0,0 +1,152 @@
1
+ """Shared metrics for MANIMOL external conformer-library validation."""
2
+ from __future__ import annotations
3
+
4
+ from collections.abc import Iterable
5
+
6
+ import numpy as np
7
+ from rdkit import Chem, DataStructs
8
+ from rdkit.Chem import rdFingerprintGenerator, rdMolAlign
9
+ from rdkit.Chem.Scaffolds import MurckoScaffold
10
+ from rdkit.Geometry import Point3D
11
+
12
+
13
+ SIMILARITY_BINS = (0.0, 0.4, 0.6, 0.8, 1.000001)
14
+ _MORGAN_GENERATORS = {}
15
+
16
+
17
+ def threshold_label(value: float) -> str:
18
+ text = f"{float(value):g}".replace(".", "p")
19
+ return f"recovery_le_{text}A"
20
+
21
+
22
+ def _mol_with_coords(mol: Chem.Mol, xyz) -> Chem.Mol:
23
+ arr = np.asarray(xyz, dtype=float)
24
+ if arr.shape != (mol.GetNumAtoms(), 3):
25
+ raise ValueError(f"coordinate shape {arr.shape} does not match {mol.GetNumAtoms()} atoms")
26
+ out = Chem.Mol(mol)
27
+ out.RemoveAllConformers()
28
+ conf = Chem.Conformer(out.GetNumAtoms())
29
+ for atom_idx, point in enumerate(arr):
30
+ conf.SetAtomPosition(atom_idx, Point3D(*map(float, point)))
31
+ out.AddConformer(conf, assignId=True)
32
+ return out
33
+
34
+
35
+ def symmetry_corrected_heavy_rmsd(mol: Chem.Mol, candidate, reference, max_matches: int = 10000) -> float:
36
+ """Return RDKit's automorphism-aware best RMSD over heavy atoms."""
37
+ probe = Chem.RemoveHs(_mol_with_coords(mol, candidate), sanitize=False)
38
+ target = Chem.RemoveHs(_mol_with_coords(mol, reference), sanitize=False)
39
+ if probe.GetNumAtoms() == 0 or probe.GetNumAtoms() != target.GetNumAtoms():
40
+ raise ValueError("invalid heavy-atom mapping")
41
+ return float(rdMolAlign.GetBestRMS(probe, target, maxMatches=int(max_matches)))
42
+
43
+
44
+ def evaluate_bound_pose_library(
45
+ mol: Chem.Mol,
46
+ candidates: Iterable,
47
+ reference,
48
+ budgets=(1, 5, 10, 20, 50),
49
+ thresholds=(0.5, 1.0, 1.5, 2.0),
50
+ max_matches: int = 10000,
51
+ ):
52
+ candidates = list(candidates)
53
+ distances = [
54
+ symmetry_corrected_heavy_rmsd(mol, candidate, reference, max_matches=max_matches)
55
+ for candidate in candidates
56
+ ]
57
+ rows = []
58
+ for budget in budgets:
59
+ used = min(int(budget), len(distances))
60
+ best = float(min(distances[:used])) if used else np.nan
61
+ row = {
62
+ "K": int(budget),
63
+ "n_retained_available": used,
64
+ "generation_success": int(used >= int(budget)),
65
+ "min_rmsd_A": best,
66
+ }
67
+ for threshold in thresholds:
68
+ row[threshold_label(threshold)] = int(np.isfinite(best) and best <= float(threshold))
69
+ rows.append(row)
70
+ return rows
71
+
72
+
73
+ def summarize_threshold_success(rows, metric: str):
74
+ rows = list(rows)
75
+ n_total = len(rows)
76
+ n_generated = sum(int(row.get("generation_success", 0)) for row in rows)
77
+ hits = sum(int(row.get(metric, 0)) for row in rows)
78
+ return {
79
+ "n_total": n_total,
80
+ "n_generated": n_generated,
81
+ "generation_success_rate": n_generated / n_total if n_total else np.nan,
82
+ "success_rate": hits / n_total if n_total else np.nan,
83
+ }
84
+
85
+
86
+ def complete_expected_units(rows, expected_ids, budgets, thresholds, defaults=None):
87
+ """Add explicit failure rows for eligible units absent from generator output."""
88
+ completed = [dict(row) for row in rows]
89
+ present = {(str(row["molecule_id"]), int(row["K"])) for row in completed}
90
+ defaults = dict(defaults or {})
91
+ for molecule_id in map(str, expected_ids):
92
+ for budget in map(int, budgets):
93
+ if (molecule_id, budget) in present:
94
+ continue
95
+ row = {
96
+ **defaults,
97
+ "molecule_id": molecule_id,
98
+ "K": budget,
99
+ "n_retained_available": 0,
100
+ "generation_success": 0,
101
+ "min_rmsd_A": np.nan,
102
+ "status": "missing_generation_record",
103
+ }
104
+ row.update({threshold_label(value): 0 for value in thresholds})
105
+ completed.append(row)
106
+ return completed
107
+
108
+
109
+ def molecular_identifiers(mol: Chem.Mol):
110
+ if mol is None:
111
+ raise ValueError("molecule is missing")
112
+ clean = Chem.RemoveHs(Chem.Mol(mol))
113
+ Chem.SanitizeMol(clean)
114
+ smiles = Chem.MolToSmiles(clean, canonical=True, isomericSmiles=True)
115
+ inchikey = Chem.MolToInchiKey(clean)
116
+ scaffold = MurckoScaffold.MurckoScaffoldSmiles(mol=clean, includeChirality=True)
117
+ return {
118
+ "canonical_isomeric_smiles": smiles,
119
+ "standard_inchikey": inchikey,
120
+ "bemis_murcko_smiles": scaffold,
121
+ }
122
+
123
+
124
+ def morgan_fingerprint(mol: Chem.Mol, radius: int = 2, n_bits: int = 2048):
125
+ key = (int(radius), int(n_bits))
126
+ generator = _MORGAN_GENERATORS.get(key)
127
+ if generator is None:
128
+ generator = rdFingerprintGenerator.GetMorganGenerator(
129
+ radius=key[0], fpSize=key[1]
130
+ )
131
+ _MORGAN_GENERATORS[key] = generator
132
+ return generator.GetFingerprint(Chem.RemoveHs(mol))
133
+
134
+
135
+ def maximum_tanimoto(query_fp, reference_fps) -> float:
136
+ reference_fps = list(reference_fps)
137
+ if not reference_fps:
138
+ return np.nan
139
+ return float(max(DataStructs.BulkTanimotoSimilarity(query_fp, reference_fps)))
140
+
141
+
142
+ def assign_similarity_bin(value: float) -> str:
143
+ value = float(value)
144
+ if not 0.0 <= value <= 1.0:
145
+ raise ValueError(f"similarity outside [0,1]: {value}")
146
+ if value < 0.4:
147
+ return "[0.0,0.4)"
148
+ if value < 0.6:
149
+ return "[0.4,0.6)"
150
+ if value < 0.8:
151
+ return "[0.6,0.8)"
152
+ return "[0.8,1.0]"
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env python3
2
+ """Fit a train-only graph-distance topology prior for the P pathway."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import json
8
+ from pathlib import Path
9
+ import sys
10
+
11
+ import numpy as np
12
+ import torch
13
+ from torch_geometric.data import Batch
14
+
15
+ ROOT = Path(__file__).resolve().parents[1]
16
+ if str(ROOT) not in sys.path:
17
+ sys.path.insert(0, str(ROOT))
18
+
19
+ from dataset.sharded_drugdataset import ShardedDrugDataset
20
+ from infer_base_conformers import checkpoint_max_confs
21
+ from models.model0 import prob_low_dim_kernel
22
+ from train_torsional_denoiser import load_base_model
23
+
24
+
25
+ def args_parser():
26
+ p = argparse.ArgumentParser(description=__doc__)
27
+ p.add_argument("--checkpoint", required=True)
28
+ p.add_argument("--sharded-root", required=True)
29
+ p.add_argument("--max-molecules", type=int, default=0)
30
+ p.add_argument("--max-conformers", type=int, default=0)
31
+ p.add_argument("--kernel", default="umap")
32
+ p.add_argument("--device", default="cuda")
33
+ p.add_argument("--output", default="results/topology_prior/train_fit_mapping.json")
34
+ return p.parse_args()
35
+
36
+
37
+ def groups_for(dataset, limit):
38
+ groups = [group for block in dataset.molecule_groups_by_shard for group in block]
39
+ return groups if limit <= 0 else groups[:limit]
40
+
41
+
42
+ def distances(edge_index, n, device):
43
+ out = torch.full((n, n), n + 1, dtype=torch.long, device=device)
44
+ out.fill_diagonal_(0)
45
+ if edge_index is not None and edge_index.numel():
46
+ out[edge_index[0], edge_index[1]] = 1
47
+ for pivot in range(n):
48
+ out = torch.minimum(out, out[:, pivot:pivot + 1] + out[pivot:pivot + 1, :])
49
+ return out
50
+
51
+
52
+ @torch.inference_mode()
53
+ def fit_one(model, dataset, group, device, max_confs, kernel):
54
+ records = [dataset[index] for index in list(group)[:max_confs]]
55
+ first = records[0]
56
+ batch = Batch.from_data_list([first]).to(device)
57
+ node = model.gnn(batch)
58
+ graph = model.pool(node, batch.batch)[0]
59
+ local_mask = batch.batch == 0
60
+ edge = model._get_local_edge_index(batch.edge_index, local_mask)
61
+ dist = distances(edge, int(first.pos.size(0)), device)
62
+ teacher = []
63
+ for item in records:
64
+ conf_id = int(torch.as_tensor(item.conf_id).reshape(-1)[0].item())
65
+ a, b = model.predict_ab(graph, conf_id)
66
+ teacher.append(prob_low_dim_kernel(item.pos.to(device=device, dtype=node.dtype), a, b, kernel=kernel))
67
+ target = torch.stack(teacher, dim=0).mean(dim=0)
68
+ upper = torch.triu(torch.ones_like(target, dtype=torch.bool), diagonal=1)
69
+ d = dist[upper].detach().cpu().numpy().astype(np.int64)
70
+ q = target[upper].detach().cpu().numpy().astype(np.float64)
71
+ sums = np.zeros(6, dtype=np.float64)
72
+ counts = np.zeros(6, dtype=np.int64)
73
+ for distance, value in zip(d.tolist(), q.tolist()):
74
+ bin_index = min(max(int(distance), 1), 6) - 1
75
+ sums[bin_index] += float(value)
76
+ counts[bin_index] += 1
77
+ return sums, counts
78
+
79
+
80
+ def main():
81
+ args = args_parser()
82
+ device = torch.device(args.device if args.device != "auto" else ("cuda" if torch.cuda.is_available() else "cpu"))
83
+ capacity = checkpoint_max_confs(args.checkpoint)
84
+ max_confs = int(args.max_conformers or capacity)
85
+ if max_confs > capacity:
86
+ raise ValueError(f"max-conformers={max_confs} exceeds checkpoint capacity={capacity}")
87
+ model = load_base_model(argparse.Namespace(checkpoint=args.checkpoint, max_confs=capacity), device)
88
+ dataset = ShardedDrugDataset(args.sharded_root, "train", cache_size=1)
89
+ groups = groups_for(dataset, int(args.max_molecules))
90
+ sums = np.zeros(6, dtype=np.float64)
91
+ counts = np.zeros(6, dtype=np.int64)
92
+ for number, group in enumerate(groups, start=1):
93
+ one_sum, one_count = fit_one(model, dataset, group, device, max_confs, args.kernel)
94
+ sums += one_sum
95
+ counts += one_count
96
+ if number == 1 or number % 1000 == 0 or number == len(groups):
97
+ print(f"[{number}/{len(groups)}] counts={counts.tolist()}", flush=True)
98
+ mapping = np.divide(sums, np.maximum(counts, 1), dtype=np.float64)
99
+ output = Path(args.output)
100
+ output.parent.mkdir(parents=True, exist_ok=True)
101
+ output.write_text(json.dumps({
102
+ "source": "training split only",
103
+ "checkpoint": str(args.checkpoint),
104
+ "sharded_root": str(args.sharded_root),
105
+ "molecules": len(groups),
106
+ "max_conformers_per_molecule": max_confs,
107
+ "bins": ["graph_distance_1", "graph_distance_2", "graph_distance_3", "graph_distance_4", "graph_distance_5", "graph_distance_ge_6"],
108
+ "mean_q_by_bin": mapping.tolist(),
109
+ "pair_counts_by_bin": counts.tolist(),
110
+ "fit_is_test_independent": True,
111
+ }, indent=2), encoding="utf-8")
112
+ print(f"Saved {output}")
113
+ print("mapping", mapping.tolist())
114
+
115
+
116
+ if __name__ == "__main__":
117
+ main()
@@ -0,0 +1,281 @@
1
+ #!/usr/bin/env python3
2
+ """Evaluate mean and dispersion heads against one frozen anchor target.
3
+
4
+ The anchor checkpoint is used only to construct the reference ensemble target
5
+ once. The evaluation checkpoint is then compared with that same target. This
6
+ separates checkpoint selection effects from target reconstruction effects.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import argparse
12
+ import csv
13
+ import json
14
+ from pathlib import Path
15
+ import sys
16
+ from typing import Iterable
17
+
18
+ import numpy as np
19
+ import torch
20
+ from scipy.stats import spearmanr
21
+ ROOT = Path(__file__).resolve().parents[1]
22
+ if str(ROOT) not in sys.path:
23
+ sys.path.insert(0, str(ROOT))
24
+
25
+ def upper_pair_indices(n_atoms: int) -> list[tuple[int, int]]:
26
+ if n_atoms < 0:
27
+ raise ValueError("n_atoms must be non-negative")
28
+ return [(i, j) for i in range(n_atoms) for j in range(i + 1, n_atoms)]
29
+
30
+
31
+ def validate_pair_record(atom_i, atom_j, reference_mu, predicted_mu, reference_disp, predicted_disp):
32
+ if int(atom_i) >= int(atom_j):
33
+ raise ValueError("pair must be strict upper triangle; diagonal pairs are invalid")
34
+ values = (reference_mu, predicted_mu, reference_disp, predicted_disp)
35
+ if not all(np.isfinite(float(value)) for value in values):
36
+ raise ValueError("pair values must be finite")
37
+ if float(reference_disp) < -1e-8 or float(predicted_disp) < -1e-8:
38
+ raise ValueError("dispersion values must be non-negative")
39
+
40
+
41
+ def molecule_summary(rows: Iterable[dict]) -> dict:
42
+ rows = list(rows)
43
+ if not rows:
44
+ raise ValueError("cannot summarize an empty molecule")
45
+ mean_errors = [abs(float(row["predicted_mu"]) - float(row["reference_mu"])) for row in rows]
46
+ disp_errors = [abs(float(row["predicted_disp"]) - float(row["reference_disp"])) for row in rows]
47
+ return {
48
+ "n_pairs": len(rows),
49
+ "mean_mae": float(np.mean(mean_errors)),
50
+ "mean_rmse": float(np.sqrt(np.mean(np.square(mean_errors)))),
51
+ "dispersion_mae": float(np.mean(disp_errors)),
52
+ "dispersion_rmse": float(np.sqrt(np.mean(np.square(disp_errors)))),
53
+ }
54
+
55
+
56
+ def _pearson(x, y):
57
+ x = np.asarray(x, dtype=float)
58
+ y = np.asarray(y, dtype=float)
59
+ if x.size < 3 or np.ptp(x) <= 1e-12 or np.ptp(y) <= 1e-12:
60
+ return float("nan")
61
+ return float(np.corrcoef(x, y)[0, 1])
62
+
63
+
64
+ def _spearman(x, y):
65
+ x = np.asarray(x, dtype=float)
66
+ y = np.asarray(y, dtype=float)
67
+ if x.size < 3 or np.ptp(x) <= 1e-12 or np.ptp(y) <= 1e-12:
68
+ return float("nan")
69
+ return float(spearmanr(x, y).statistic)
70
+
71
+
72
+ def _finite_summary(values):
73
+ values = np.asarray(values, dtype=float)
74
+ values = values[np.isfinite(values)]
75
+ return {
76
+ "mean": float(values.mean()) if values.size else float("nan"),
77
+ "median": float(np.median(values)) if values.size else float("nan"),
78
+ "n": int(values.size),
79
+ }
80
+
81
+
82
+ def _torsion_count(item):
83
+ torsions = getattr(item, "torsion_index", None)
84
+ if torsions is None or torsions.numel() == 0:
85
+ return 0
86
+ rows = torsions.t() if torsions.size(0) == 4 else torsions
87
+ return len({tuple(sorted((int(row[1]), int(row[2])))) for row in rows})
88
+
89
+
90
+ def teacher_kernel_parameters(model, graph, conf_id, fixed_a=0.0, fixed_b=0.0):
91
+ """Return the exact kernel parameters used by the audit target.
92
+
93
+ A fixed-target audit must not silently reconstruct the reference target
94
+ with a checkpoint's learned ``a,b`` values. Supplying both fixed values
95
+ therefore bypasses the model's parameter head and makes the target fully
96
+ reproducible from the stated protocol.
97
+ """
98
+ fixed_a = float(fixed_a)
99
+ fixed_b = float(fixed_b)
100
+ if (fixed_a > 0.0) != (fixed_b > 0.0):
101
+ raise ValueError("fixed_a and fixed_b must be supplied together")
102
+ if fixed_a > 0.0 and fixed_b > 0.0:
103
+ return (
104
+ graph.new_tensor(fixed_a).clamp(1e-4, 1e4),
105
+ graph.new_tensor(fixed_b).clamp(1e-4, 10.0),
106
+ )
107
+ return model.predict_ab(graph, conf_id)
108
+
109
+
110
+ @torch.inference_mode()
111
+ def export_group(anchor_model, eval_model, group, device, max_confs, kernel,
112
+ pair_writer, mol_writer, fixed_a=0.0, fixed_b=0.0):
113
+ import torch
114
+ from torch_geometric.data import Batch
115
+ from models.model0 import prob_low_dim_kernel
116
+
117
+ conformers = group[:max_confs]
118
+ first = conformers[0]
119
+ batch = Batch.from_data_list([first]).to(device)
120
+
121
+ anchor_node = anchor_model.gnn(batch)
122
+ anchor_graph = anchor_model.pool(anchor_node, batch.batch)[0]
123
+ anchor_edge = anchor_model._get_local_edge_index(batch.edge_index, batch.batch == 0)
124
+ anchor_q = []
125
+ for item in conformers:
126
+ cid = int(torch.as_tensor(item.conf_id).reshape(-1)[0].item())
127
+ a, b = teacher_kernel_parameters(anchor_model, anchor_graph, cid, fixed_a, fixed_b)
128
+ anchor_q.append(prob_low_dim_kernel(
129
+ item.pos.to(device=device, dtype=anchor_node.dtype), a, b, kernel=kernel
130
+ ))
131
+ target_stack = torch.stack(anchor_q, dim=0)
132
+ reference_mu = target_stack.mean(dim=0)
133
+ reference_disp = target_stack.var(dim=0, unbiased=False)
134
+
135
+ eval_node = eval_model.gnn(batch)
136
+ eval_edge = eval_model._get_local_edge_index(batch.edge_index, batch.batch == 0)
137
+ predicted_mu, predicted_disp, _ = eval_model._predict_mean_dispersion(eval_node, eval_edge)
138
+
139
+ pairs = upper_pair_indices(int(first.pos.size(0)))
140
+ rows = []
141
+ for i, j in pairs:
142
+ rmu = float(reference_mu[i, j].cpu())
143
+ pmu = float(predicted_mu[i, j].cpu())
144
+ rdisp = float(reference_disp[i, j].cpu())
145
+ pdisp = float(predicted_disp[i, j].cpu())
146
+ validate_pair_record(i, j, rmu, pmu, rdisp, pdisp)
147
+ row = {
148
+ "molecule_id": int(first.mol_id.reshape(-1)[0]),
149
+ "atom_i": i,
150
+ "atom_j": j,
151
+ "reference_mu": rmu,
152
+ "predicted_mu": pmu,
153
+ "reference_disp": rdisp,
154
+ "predicted_disp": pdisp,
155
+ }
156
+ pair_writer.writerow(row.values())
157
+ rows.append(row)
158
+
159
+ summary = molecule_summary(rows)
160
+ mean_r = _pearson([r["reference_mu"] for r in rows], [r["predicted_mu"] for r in rows])
161
+ disp_r = _pearson([r["reference_disp"] for r in rows], [r["predicted_disp"] for r in rows])
162
+ mean_rho = _spearman([r["reference_mu"] for r in rows], [r["predicted_mu"] for r in rows])
163
+ disp_rho = _spearman([r["reference_disp"] for r in rows], [r["predicted_disp"] for r in rows])
164
+ mol_writer.writerow([
165
+ int(first.mol_id.reshape(-1)[0]), int(first.pos.size(0)), _torsion_count(first),
166
+ len(group), len(conformers), summary["n_pairs"],
167
+ summary["mean_mae"], summary["mean_rmse"], summary["dispersion_mae"],
168
+ summary["dispersion_rmse"], mean_r, mean_rho, disp_r, disp_rho,
169
+ ])
170
+ return rows, {
171
+ **summary,
172
+ "mean_pearson": mean_r,
173
+ "mean_spearman": mean_rho,
174
+ "dispersion_pearson": disp_r,
175
+ "dispersion_spearman": disp_rho,
176
+ }
177
+
178
+
179
+ def load_groups(args):
180
+ import torch
181
+ from dataset.drugdataset import QM9Dataset
182
+
183
+ cache_path = Path(args.group_cache) if args.group_cache else None
184
+ if cache_path is not None and cache_path.exists():
185
+ return torch.load(cache_path, map_location="cpu")
186
+ dataset = QM9Dataset(name="Drugs", root=args.data_root, max_confs=args.max_confs, raw_prefix=args.raw_prefix)
187
+ groups_by_id = {}
188
+ for index in dataset.test_index:
189
+ item = dataset[int(index)]
190
+ mol_id = int(item.mol_id.reshape(-1)[0])
191
+ groups_by_id.setdefault(mol_id, []).append(item)
192
+ groups = list(groups_by_id.values())
193
+ if cache_path is not None:
194
+ cache_path.parent.mkdir(parents=True, exist_ok=True)
195
+ torch.save(groups, cache_path)
196
+ return groups
197
+
198
+
199
+ def main():
200
+ import torch
201
+ from train_torsional_denoiser import load_base_model
202
+
203
+ parser = argparse.ArgumentParser()
204
+ parser.add_argument("--anchor-checkpoint", required=True)
205
+ parser.add_argument("--eval-checkpoint", required=True)
206
+ parser.add_argument("--data-root", required=True)
207
+ parser.add_argument("--raw-prefix", default="geom_drugs")
208
+ parser.add_argument("--max-confs", type=int, default=30)
209
+ parser.add_argument("--max-molecules", type=int, default=0)
210
+ parser.add_argument("--kernel", default="umap")
211
+ parser.add_argument("--fixed-kernel-a", type=float, default=0.0,
212
+ help="If positive, reconstruct the reference target with this fixed UMAP a.")
213
+ parser.add_argument("--fixed-kernel-b", type=float, default=0.0,
214
+ help="If positive, reconstruct the reference target with this fixed UMAP b.")
215
+ parser.add_argument("--device", default="cuda")
216
+ parser.add_argument("--group-cache", default="")
217
+ parser.add_argument("--output-dir", required=True)
218
+ args = parser.parse_args()
219
+
220
+ out = Path(args.output_dir)
221
+ out.mkdir(parents=True, exist_ok=True)
222
+ device = torch.device(args.device if args.device != "auto" else ("cuda" if torch.cuda.is_available() else "cpu"))
223
+ groups = load_groups(args)
224
+ if args.max_molecules > 0:
225
+ groups = groups[:args.max_molecules]
226
+
227
+ model_args = argparse.Namespace(checkpoint=args.anchor_checkpoint, max_confs=args.max_confs)
228
+ anchor_model = load_base_model(model_args, device).eval()
229
+ model_args = argparse.Namespace(checkpoint=args.eval_checkpoint, max_confs=args.max_confs)
230
+ eval_model = load_base_model(model_args, device).eval()
231
+
232
+ pair_path = out / "frozen_target_pair_audit.csv"
233
+ mol_path = out / "frozen_target_per_molecule.csv"
234
+ pair_fields = ["molecule_id", "atom_i", "atom_j", "reference_mu", "predicted_mu", "reference_disp", "predicted_disp"]
235
+ mol_fields = ["molecule_id", "n_atoms", "n_rotatable_bonds", "n_reference_conformers", "n_teacher_conformers", "n_pairs", "mean_mae", "mean_rmse", "dispersion_mae", "dispersion_rmse", "mean_pearson", "mean_spearman", "dispersion_pearson", "dispersion_spearman"]
236
+ pair_values = []
237
+ mol_values = []
238
+ with pair_path.open("w", newline="", encoding="utf-8") as pair_file, mol_path.open("w", newline="", encoding="utf-8") as mol_file:
239
+ pair_writer = csv.writer(pair_file)
240
+ mol_writer = csv.writer(mol_file)
241
+ pair_writer.writerow(pair_fields)
242
+ mol_writer.writerow(mol_fields)
243
+ for number, group in enumerate(groups, 1):
244
+ rows, mol_summary = export_group(
245
+ anchor_model, eval_model, group, device, args.max_confs, args.kernel,
246
+ pair_writer, mol_writer, args.fixed_kernel_a, args.fixed_kernel_b,
247
+ )
248
+ pair_values.extend(rows)
249
+ mol_values.append(mol_summary)
250
+ if number == 1 or number % 25 == 0 or number == len(groups):
251
+ print(f"[{number}/{len(groups)}]", flush=True)
252
+
253
+ fixed_target = args.fixed_kernel_a > 0.0 and args.fixed_kernel_b > 0.0
254
+ summary = {
255
+ "n_molecules": len(groups),
256
+ "n_valid_pairs": len(pair_values),
257
+ "anchor_checkpoint": str(args.anchor_checkpoint),
258
+ "eval_checkpoint": str(args.eval_checkpoint),
259
+ "target_protocol": (
260
+ "Reference mean/dispersion reconstructed directly from conformer coordinates with fixed kernel parameters."
261
+ if fixed_target else
262
+ "Reference mean/dispersion reconstructed with the frozen anchor checkpoint kernel parameters."
263
+ ),
264
+ "fixed_kernel_a": args.fixed_kernel_a,
265
+ "fixed_kernel_b": args.fixed_kernel_b,
266
+ "pair_mean_pearson": _pearson([r["reference_mu"] for r in pair_values], [r["predicted_mu"] for r in pair_values]),
267
+ "pair_mean_spearman": _spearman([r["reference_mu"] for r in pair_values], [r["predicted_mu"] for r in pair_values]),
268
+ "pair_dispersion_pearson": _pearson([r["reference_disp"] for r in pair_values], [r["predicted_disp"] for r in pair_values]),
269
+ "pair_dispersion_spearman": _spearman([r["reference_disp"] for r in pair_values], [r["predicted_disp"] for r in pair_values]),
270
+ "molecule_mean_pearson": _finite_summary([r["mean_pearson"] for r in mol_values]),
271
+ "molecule_mean_spearman": _finite_summary([r["mean_spearman"] for r in mol_values]),
272
+ "molecule_dispersion_pearson": _finite_summary([r["dispersion_pearson"] for r in mol_values]),
273
+ "molecule_dispersion_spearman": _finite_summary([r["dispersion_spearman"] for r in mol_values]),
274
+ }
275
+ (out / "frozen_target_audit_summary.json").write_text(json.dumps(summary, indent=2), encoding="utf-8")
276
+ (out / "frozen_target_audit_config.json").write_text(json.dumps(vars(args), indent=2), encoding="utf-8")
277
+ print(json.dumps(summary, indent=2), flush=True)
278
+
279
+
280
+ if __name__ == "__main__":
281
+ main()
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env python
2
+ """Run a small molecule-complete GeoManimol debug pass."""
3
+
4
+ import argparse
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ import torch
9
+
10
+ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
11
+
12
+ from geomanimol.data import load_molecule_groups
13
+ from geomanimol.geometry import DynamicGeometryMemory
14
+ from geomanimol.model import GeoManimol
15
+
16
+
17
+ def main():
18
+ p = argparse.ArgumentParser()
19
+ p.add_argument("--train", default="data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_train.pt")
20
+ p.add_argument("--val", default="data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_val.pt")
21
+ p.add_argument("--molecules", type=int, default=5)
22
+ p.add_argument("--epochs", type=int, default=1)
23
+ args = p.parse_args()
24
+ train = load_molecule_groups(args.train, args.molecules)
25
+ val = load_molecule_groups(args.val, min(args.molecules, 2))
26
+ train_memory = DynamicGeometryMemory(n_modes=3, manifold_dim=8)
27
+ val_memory = DynamicGeometryMemory(n_modes=3, manifold_dim=8)
28
+ train_memory.initialize(train)
29
+ val_memory.initialize(val)
30
+ model = GeoManimol(node_dim=32, n_basins=3, manifold_dim=8)
31
+ optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
32
+ for epoch in range(args.epochs):
33
+ losses = []
34
+ for mol in train:
35
+ out = model(mol, mol["conformations"][0], train_memory.get(mol["mol_id"]))
36
+ optimizer.zero_grad(); out["loss"].backward(); optimizer.step()
37
+ losses.append(out["loss"].item())
38
+ print(f"epoch={epoch} train_loss={sum(losses)/len(losses):.4f}")
39
+ with torch.no_grad():
40
+ val_losses = []
41
+ for mol in val:
42
+ val_losses.append(model(mol, mol["conformations"][0], val_memory.get(mol["mol_id"]))["loss"].item())
43
+ generated = model.generate(train[0], n_samples=3, steps=4)
44
+ assert generated.shape == (3, train[0]["conformations"].shape[1], 3)
45
+ print(f"val_loss={sum(val_losses)/len(val_losses):.4f}")
46
+ print(f"GEOMANIMOL_DEBUG_OK train_molecules={len(train)} val_molecules={len(val)}")
47
+
48
+
49
+ if __name__ == "__main__":
50
+ main()
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env python
2
+ """Tiny synthetic smoke test for the isolated GeoManimol route."""
3
+
4
+ import sys
5
+ from pathlib import Path
6
+
7
+ import torch
8
+
9
+ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
10
+
11
+ from geomanimol.geometry import DynamicGeometryMemory
12
+ from geomanimol.model import GeoManimol
13
+
14
+
15
+ def main():
16
+ torch.manual_seed(7)
17
+ n = 6
18
+ atom_features = torch.randn(n, 6)
19
+ edge_index = torch.tensor([[0, 1, 1, 2, 2, 3, 3, 4, 4, 5], [1, 0, 2, 1, 3, 2, 4, 3, 5, 4]])
20
+ edge_attr = torch.randn(edge_index.shape[1], 3)
21
+ conformers = torch.randn(5, n, 3) * 0.5
22
+ mol = {"atom_features": atom_features, "edge_index": edge_index, "edge_attr": edge_attr}
23
+ memory = DynamicGeometryMemory(n_modes=3, manifold_dim=8)
24
+ memory.initialize([{"mol_id": 0, "conformations": conformers}])
25
+ model = GeoManimol(node_dim=32, n_basins=3, manifold_dim=8)
26
+ optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
27
+ for step in range(3):
28
+ out = model(mol, conformers[step % len(conformers)], memory.get(0))
29
+ optimizer.zero_grad(); out["loss"].backward(); optimizer.step()
30
+ print(f"step={step} loss={out['loss'].item():.4f} prob={out['probability_loss'].item():.4f}")
31
+ assert out["P"].shape == (n, n)
32
+ assert torch.isfinite(out["loss"])
33
+ print("GEOMANIMOL_SMOKE_OK")
34
+
35
+
36
+ if __name__ == "__main__":
37
+ main()