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,208 @@
1
+ #!/usr/bin/env python3
2
+ """Audit whether P_disp changes the breadth of actual torsion proposals.
3
+
4
+ This script deliberately evaluates the same u/v denoising path used by
5
+ ``infer_mean_dispersion_torsion.py``. It records proposal angles rather than
6
+ only the learned pairwise map, so the resulting CSV can support a mechanism
7
+ plot without synthetic examples.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+ import csv
14
+ import json
15
+ import math
16
+ import os
17
+ import sys
18
+ from types import SimpleNamespace
19
+
20
+ PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
21
+ if PROJECT_ROOT not in sys.path:
22
+ sys.path.insert(0, PROJECT_ROOT)
23
+
24
+ import torch
25
+ from torch_geometric.loader import DataLoader
26
+ from tqdm import tqdm
27
+
28
+ from dataset import QM9Dataset
29
+ from infer_base_conformers import get_split_indices
30
+ from models.model0 import _torsion_rows, torsion_angles
31
+ from train_torsional_denoiser import load_base_model
32
+ import infer_base_conformers
33
+ import infer_mean_dispersion_torsion as uv
34
+
35
+
36
+ def circular_std(values):
37
+ if not values:
38
+ return float("nan")
39
+ x = torch.tensor(values, dtype=torch.float64)
40
+ r = torch.sqrt(torch.mean(torch.cos(x)) ** 2 + torch.mean(torch.sin(x)) ** 2).clamp_min(1e-12)
41
+ return float(torch.sqrt(-2.0 * torch.log(r)).item())
42
+
43
+
44
+ def make_args(cli):
45
+ # Reuse infer_base_conformers's established defaults for candidate generation and replace
46
+ # only the u/v-specific fields needed by sample_denoise_conformers.
47
+ old_argv = sys.argv
48
+ sys.argv = [old_argv[0], "--checkpoint", cli.base_checkpoint, "--dataset", cli.dataset,
49
+ "--data_root", cli.data_root, "--raw_prefix", cli.raw_prefix,
50
+ "--max_confs", str(cli.max_confs), "--device", cli.device]
51
+ try:
52
+ args = infer_base_conformers.parse_args()
53
+ finally:
54
+ sys.argv = old_argv
55
+ args.base_checkpoint = cli.base_checkpoint
56
+ args.denoiser_checkpoint = cli.denoiser_checkpoint
57
+ args.denoise_steps = cli.denoise_steps
58
+ args.denoise_sigma_max = cli.denoise_sigma_max
59
+ args.denoise_sigma_min = cli.denoise_sigma_min
60
+ args.denoise_step_scale = cli.denoise_step_scale
61
+ args.denoise_initial_noise = cli.denoise_initial_noise
62
+ args.denoise_polish_steps = 0
63
+ args.post_denoise_steps = 0
64
+ args.final_snap_steps = 0
65
+ args.torsion_mode_max = cli.torsion_mode_max
66
+ args.torsion_base_confs = cli.torsion_base_confs
67
+ args.torsion_base_method = "etkdg"
68
+ args.torsion_manifold_prior_path = ""
69
+ args.torsion_manifold_proposal_fraction = 0.0
70
+ args.pbasin_checkpoint = ""
71
+ args.pbasin_fraction = 0.0
72
+ args.pbudget_adaptive = False
73
+ args.pbasin_only = False
74
+ args.use_dispersion_proposal = not cli.disable_dispersion_proposal
75
+ args.dispersion_width_scale = cli.dispersion_width_scale
76
+ args.dispersion_width_min = cli.dispersion_width_min
77
+ args.dispersion_width_max = cli.dispersion_width_max
78
+ args.inference_seed = cli.seed
79
+ args.use_etkdg = False
80
+ args.etkdg_candidate_pool = False
81
+ args.etkdg_init_ratio = 1.0
82
+ args.use_egnn_refine = False
83
+ args.use_mmff = False
84
+ args.use_torsion_refine = False
85
+ args.disable_stage1_pglobal = False
86
+ args.disable_probability_guidance = False
87
+ args.pglobal_control = "normal"
88
+ args.torsion_manifold_p_context_control = "normal"
89
+ args.manifold_use_mode_p = False
90
+ args.candidate_corrector_path = ""
91
+ return args
92
+
93
+
94
+ def main():
95
+ parser = argparse.ArgumentParser()
96
+ parser.add_argument("--base-checkpoint", required=True)
97
+ parser.add_argument("--denoiser-checkpoint", required=True)
98
+ parser.add_argument("--data-root", required=True)
99
+ parser.add_argument("--raw-prefix", default="geom_drugs")
100
+ parser.add_argument("--test-raw-filename", required=True)
101
+ parser.add_argument("--dataset", default="Drugs")
102
+ parser.add_argument("--max-confs", type=int, default=30)
103
+ parser.add_argument("--max-molecules", type=int, default=0)
104
+ parser.add_argument("--num-proposals", type=int, default=20)
105
+ parser.add_argument("--torsion-mode-max", type=int, default=64)
106
+ parser.add_argument("--torsion-base-confs", type=int, default=0)
107
+ parser.add_argument("--denoise-steps", type=int, default=10)
108
+ parser.add_argument("--denoise-sigma-max", type=float, default=0.75)
109
+ parser.add_argument("--denoise-sigma-min", type=float, default=0.05)
110
+ parser.add_argument("--denoise-step-scale", type=float, default=1.0)
111
+ parser.add_argument("--denoise-initial-noise", type=float, default=0.75)
112
+ parser.add_argument("--dispersion-width-scale", type=float, default=0.35)
113
+ parser.add_argument("--dispersion-width-min", type=float, default=0.5)
114
+ parser.add_argument("--dispersion-width-max", type=float, default=2.0)
115
+ parser.add_argument("--device", default="cpu")
116
+ parser.add_argument("--seed", type=int, default=20260913)
117
+ parser.add_argument("--disable-dispersion-proposal", action="store_true")
118
+ parser.add_argument("--output-csv", required=True)
119
+ cli = parser.parse_args()
120
+
121
+ torch.manual_seed(cli.seed)
122
+ device = torch.device(cli.device)
123
+ base_args = SimpleNamespace(max_confs=cli.max_confs, checkpoint=cli.base_checkpoint)
124
+ base = load_base_model(base_args, device)
125
+ denoiser = uv.load_denoiser(cli.denoiser_checkpoint, device)
126
+ model = uv.DenoiseWrapper(base, denoiser)
127
+ args = make_args(cli)
128
+
129
+ dataset = QM9Dataset(
130
+ name=cli.dataset,
131
+ root=cli.data_root,
132
+ max_confs=cli.max_confs,
133
+ raw_prefix=cli.raw_prefix,
134
+ test_raw_filename=cli.test_raw_filename,
135
+ )
136
+ subset = dataset[get_split_indices(dataset, "test")]
137
+ records, seen = [], set()
138
+ for i in range(len(subset)):
139
+ data = subset[i]
140
+ mol_id = int(data.mol_id.item())
141
+ if mol_id in seen:
142
+ continue
143
+ seen.add(mol_id)
144
+ records.append(data)
145
+ if cli.max_molecules > 0 and len(records) >= cli.max_molecules:
146
+ break
147
+ loader = DataLoader(records, batch_size=1, shuffle=False)
148
+ rows_out = []
149
+ skipped = 0
150
+ for batch in tqdm(loader, total=len(records), desc="dispersion proposal audit"):
151
+ batch = batch.to(device)
152
+ mol_id = int(batch.mol_id[0].item())
153
+ torsions = base._get_local_torsion_index(getattr(batch, "torsion_index", None), batch.batch == 0)
154
+ torsions = _torsion_rows(torsions)
155
+ if torsions is None or torsions.numel() == 0:
156
+ skipped += 1
157
+ continue
158
+ result = uv.sample_denoise_conformers(model, batch, 0, args, model_count=cli.num_proposals, etkdg_count=0)
159
+ if result is None:
160
+ skipped += 1
161
+ continue
162
+ confs = result[0]
163
+ widths = getattr(model, "last_dispersion_torsion_widths", None)
164
+ scores = getattr(model, "last_dispersion_torsion_scores", None)
165
+ valid = getattr(model, "last_dispersion_torsion_valid", None)
166
+ if widths is None or scores is None or valid is None:
167
+ raise RuntimeError("u/v inference did not expose dispersion torsion audit metadata")
168
+ angle_matrix = []
169
+ for conf in confs:
170
+ angle_matrix.append(torsion_angles(conf.to(device), torsions.to(device)).detach().cpu())
171
+ angle_matrix = torch.stack(angle_matrix, dim=0)
172
+ for bond_idx in range(min(torsions.size(0), cli.torsion_mode_max)):
173
+ if not bool(valid[bond_idx]):
174
+ continue
175
+ angles = angle_matrix[:, bond_idx].tolist()
176
+ circ = circular_std(angles)
177
+ row = {
178
+ "molecule_id": mol_id,
179
+ "bond_id": bond_idx,
180
+ "atom_indices": json.dumps([int(x) for x in torsions[bond_idx].tolist()]),
181
+ "dispersion_score": float(scores[bond_idx]),
182
+ "proposal_width": float(widths[bond_idx]),
183
+ "n_samples": len(angles),
184
+ "circular_sd_rad": circ,
185
+ "circular_sd_deg": math.degrees(circ),
186
+ "sample_angles_rad_json": json.dumps(angles),
187
+ }
188
+ rows_out.append(row)
189
+
190
+ os.makedirs(os.path.dirname(os.path.abspath(cli.output_csv)), exist_ok=True)
191
+ fieldnames = [
192
+ "molecule_id", "bond_id", "atom_indices", "dispersion_score", "proposal_width",
193
+ "n_samples", "circular_sd_rad", "circular_sd_deg", "sample_angles_rad_json",
194
+ ]
195
+ with open(cli.output_csv, "w", newline="") as handle:
196
+ writer = csv.DictWriter(handle, fieldnames=fieldnames)
197
+ writer.writeheader()
198
+ writer.writerows(rows_out)
199
+ print(f"molecules={len(records)} torsion_rows={len(rows_out)} skipped={skipped}")
200
+ if rows_out:
201
+ print(f"dispersion_score_range={min(r['dispersion_score'] for r in rows_out):.6f},"
202
+ f"{max(r['dispersion_score'] for r in rows_out):.6f}")
203
+ print(f"circular_sd_deg_range={min(r['circular_sd_deg'] for r in rows_out):.3f},"
204
+ f"{max(r['circular_sd_deg'] for r in rows_out):.3f}")
205
+
206
+
207
+ if __name__ == "__main__":
208
+ main()
@@ -0,0 +1,258 @@
1
+ #!/usr/bin/env python3
2
+ """Audit ET-Flow reproduction protocol without changing model weights.
3
+
4
+ The audit is intentionally separate from MANIMOL and writes only under an
5
+ explicit output prefix. It checks dataset identity, atom signatures, model
6
+ metadata, and aggregates condition-specific ET-Flow result files.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import argparse
12
+ import json
13
+ import sys
14
+ from collections import Counter, OrderedDict
15
+ from pathlib import Path
16
+ from types import SimpleNamespace
17
+
18
+ import numpy as np
19
+ import pandas as pd
20
+ import torch
21
+ from rdkit import Chem, RDLogger
22
+
23
+ ROOT = Path(__file__).resolve().parents[1]
24
+ if str(ROOT) not in sys.path:
25
+ sys.path.insert(0, str(ROOT))
26
+
27
+ from scripts.infer_etflow_matched import (
28
+ heavy_copy,
29
+ load_processed_split,
30
+ group_references,
31
+ predict_molecules,
32
+ rmsd_matrix,
33
+ )
34
+
35
+ RDLogger.DisableLog("rdApp.warning")
36
+
37
+
38
+ def parse_conditions(text: str):
39
+ result = OrderedDict()
40
+ for item in str(text).split(","):
41
+ if not item.strip():
42
+ continue
43
+ if "=" not in item:
44
+ raise ValueError("--conditions expects Label=outputs/..._curve.csv pairs")
45
+ label, path = item.split("=", 1)
46
+ result[label.strip()] = Path(path.strip())
47
+ return result
48
+
49
+
50
+ def atom_signature(mol: Chem.Mol):
51
+ heavy = heavy_copy(mol)
52
+ return {
53
+ "atom_count": int(heavy.GetNumAtoms()),
54
+ "atomic_numbers": [int(atom.GetAtomicNum()) for atom in heavy.GetAtoms()],
55
+ "formal_charges": [int(atom.GetFormalCharge()) for atom in heavy.GetAtoms()],
56
+ "explicit_hydrogens": [int(atom.GetNumExplicitHs()) for atom in heavy.GetAtoms()],
57
+ }
58
+
59
+
60
+ def mapping_audit(groups, max_molecules: int):
61
+ from etflow.commons.featurization import get_mol_from_smiles
62
+
63
+ rows = []
64
+ for index, (smiles, references) in enumerate(groups.items()):
65
+ if max_molecules > 0 and index >= max_molecules:
66
+ break
67
+ ref_sig = atom_signature(references[0])
68
+ try:
69
+ pred_sig = atom_signature(get_mol_from_smiles(smiles))
70
+ graph_match = bool(pred_sig["atom_count"] == ref_sig["atom_count"] and pred_sig["atomic_numbers"].count(6) == ref_sig["atomic_numbers"].count(6))
71
+ atom_order_match = ref_sig == pred_sig
72
+ # Graph isomorphism, rather than atom-index equality, decides
73
+ # whether a coordinate remapping is possible.
74
+ matches = bool(get_mol_from_smiles(smiles).GetSubstructMatch(references[0], useChirality=False))
75
+ reason = "" if matches else "template/reference are not graph-isomorphic"
76
+ except Exception as exc:
77
+ pred_sig = {key: None for key in ref_sig}
78
+ matches = False
79
+ reason = f"template construction failed: {exc}"
80
+ rows.append(
81
+ {
82
+ "smiles": smiles,
83
+ "reference_conformers": len(references),
84
+ "mapping_ok": bool(matches),
85
+ "atom_order_match": bool(atom_order_match),
86
+ "basic_element_count_match": bool(graph_match),
87
+ "reason": reason,
88
+ "reference_atom_count": ref_sig["atom_count"],
89
+ "etflow_atom_count": pred_sig["atom_count"],
90
+ "atomic_numbers_match": ref_sig["atomic_numbers"] == pred_sig["atomic_numbers"],
91
+ "formal_charges_match": ref_sig["formal_charges"] == pred_sig["formal_charges"],
92
+ "explicit_hydrogens_match": ref_sig["explicit_hydrogens"] == pred_sig["explicit_hydrogens"],
93
+ }
94
+ )
95
+ return pd.DataFrame(rows)
96
+
97
+
98
+ def condition_metrics(path: Path):
99
+ curve = pd.read_csv(path)
100
+ hit = curve[np.isclose(curve["threshold"].astype(float), 0.75)]
101
+ if hit.empty:
102
+ raise ValueError(f"No threshold=0.75 row in {path}")
103
+ row = hit.iloc[0]
104
+ return {
105
+ "cov_r_0p75": float(row["cov_r"]),
106
+ "cov_p_0p75": float(row["cov_p"]),
107
+ "amr_r": float(row["amr_r"]),
108
+ "amr_p": float(row["amr_p"]),
109
+ "molecules": int(row.get("molecules", np.nan)),
110
+ }
111
+
112
+
113
+ def main():
114
+ parser = argparse.ArgumentParser(description=__doc__)
115
+ parser.add_argument("--data_root", default="data")
116
+ parser.add_argument("--raw_prefix", default="geom_drugs")
117
+ parser.add_argument("--split", default="test")
118
+ parser.add_argument("--expected_test_molecules", type=int, default=1000)
119
+ parser.add_argument("--mapping_molecules", type=int, default=0,
120
+ help="0 audits all local test molecules.")
121
+ parser.add_argument("--model", default="drugs-o3")
122
+ parser.add_argument("--cache", default=str(Path.home() / ".cache" / "etflow"))
123
+ parser.add_argument("--reference_template", default="etflow", choices=["raw", "etflow"])
124
+ parser.add_argument("--device", default="cpu")
125
+ parser.add_argument("--crosscheck_molecules", type=int, default=0,
126
+ help="Generate this many molecules and compare current vs official RMSD matrices.")
127
+ parser.add_argument("--crosscheck_samples", type=int, default=32)
128
+ parser.add_argument("--crosscheck_steps", type=int, default=50)
129
+ parser.add_argument("--batch_size", type=int, default=8)
130
+ parser.add_argument("--seed", type=int, default=20260730)
131
+ parser.add_argument("--conditions", default="",
132
+ help="Comma-separated Label=curve.csv pairs from ET-Flow runs.")
133
+ parser.add_argument("--output_prefix", default="outputs/etflow_audit/protocol_audit")
134
+ args, _ = parser.parse_known_args()
135
+
136
+ split_path = Path(args.data_root) / f"{args.raw_prefix}_{args.split}.pt"
137
+ groups = group_references(load_processed_split(split_path), reference_template=args.reference_template)
138
+ counts = np.asarray([len(items) for items in groups.values()], dtype=int)
139
+ mapping = mapping_audit(groups, args.mapping_molecules)
140
+
141
+ from etflow import BaseFlow
142
+
143
+ # Keep ET-Flow's internal device state consistent with predict(). BaseFlow
144
+ # gracefully falls back to CPU when CUDA is unavailable, which is useful for
145
+ # local audit runs while preserving the requested device in the report.
146
+ model = BaseFlow.from_default(model=args.model, device=args.device, cache=str(Path(args.cache).expanduser()))
147
+ parameter_count = int(sum(param.numel() for param in model.parameters()))
148
+ checkpoint_path = Path(args.cache).expanduser() / f"{args.model}.ckpt"
149
+
150
+ output = Path(args.output_prefix)
151
+ output.parent.mkdir(parents=True, exist_ok=True)
152
+ mapping.to_csv(output.with_name(output.name + "_atom_mapping.csv"), index=False)
153
+
154
+ condition_rows = []
155
+ for label, path in parse_conditions(args.conditions).items():
156
+ if not path.exists():
157
+ condition_rows.append({"condition": label, "curve_path": str(path), "status": "missing"})
158
+ continue
159
+ row = {"condition": label, "curve_path": str(path), "status": "ok"}
160
+ row.update(condition_metrics(path))
161
+ summary_path = path.with_name(path.name.replace("_curve.csv", "_summary.json"))
162
+ if summary_path.exists():
163
+ summary = json.loads(summary_path.read_text())
164
+ for key in ["budget_mode", "steps", "model", "parameter_count", "parity_switch", "sampler_type", "disable_posthoc_chirality"]:
165
+ row[key] = summary.get(key)
166
+ per_path = path.with_name(path.name.replace("_curve.csv", "_per_molecule.csv"))
167
+ if per_path.exists():
168
+ per = pd.read_csv(per_path)
169
+ if "chiral_error_rate" in per:
170
+ row["mean_chiral_error_rate"] = float(per["chiral_error_rate"].mean())
171
+ condition_rows.append(row)
172
+ conditions = pd.DataFrame(condition_rows)
173
+ conditions.to_csv(output.with_name(output.name + "_conditions.csv"), index=False)
174
+
175
+ rmsd_crosscheck = []
176
+ if int(args.crosscheck_molecules) > 0:
177
+ from etflow.commons.covmat import get_best_rmsd
178
+
179
+ audit_model = model.eval()
180
+ prediction_args = SimpleNamespace(
181
+ batch_size=args.batch_size,
182
+ steps=args.crosscheck_steps,
183
+ seed=args.seed,
184
+ device=args.device,
185
+ sampler_type="ode",
186
+ s_churn=1.0,
187
+ t_min=1.0,
188
+ t_max=1.0,
189
+ noise_std=1.0,
190
+ )
191
+ for smiles, refs in list(groups.items())[: int(args.crosscheck_molecules)]:
192
+ preds = predict_molecules(audit_model, smiles, prediction_args, int(args.crosscheck_samples))
193
+ current = rmsd_matrix(preds, refs, max_matches=100000).numpy()
194
+ official = np.asarray(
195
+ [[get_best_rmsd(pred, ref) for ref in refs] for pred in preds], dtype=float
196
+ )
197
+ diff = np.abs(current - official)
198
+ rmsd_crosscheck.append(
199
+ {
200
+ "smiles": smiles,
201
+ "predictions": len(preds),
202
+ "references": len(refs),
203
+ "max_abs_difference": float(np.nanmax(diff)),
204
+ "mean_abs_difference": float(np.nanmean(diff)),
205
+ }
206
+ )
207
+ pd.DataFrame(rmsd_crosscheck).to_csv(output.with_name(output.name + "_rmsd_crosscheck.csv"), index=False)
208
+
209
+ report = {
210
+ "dataset": {
211
+ "split_path": str(split_path),
212
+ "local_test_molecules": int(len(groups)),
213
+ "expected_test_molecules": int(args.expected_test_molecules),
214
+ "matches_expected_split_size": bool(len(groups) == int(args.expected_test_molecules)),
215
+ "K_min": int(counts.min()) if counts.size else None,
216
+ "K_median": float(np.median(counts)) if counts.size else None,
217
+ "K_max": int(counts.max()) if counts.size else None,
218
+ "fraction_K_gt_16": float(np.mean(counts > 16)) if counts.size else None,
219
+ "mean_2K": float(np.mean(2 * counts)) if counts.size else None,
220
+ },
221
+ "checkpoint": {
222
+ "requested_model": args.model,
223
+ "checkpoint_path": str(checkpoint_path),
224
+ "checkpoint_exists": checkpoint_path.exists(),
225
+ "parameter_count": parameter_count,
226
+ "parity_switch": str(getattr(model, "parity_switch", None)),
227
+ },
228
+ "atom_mapping": {
229
+ "audited_molecules": int(len(mapping)),
230
+ "mismatches": int((~mapping["mapping_ok"]).sum()) if not mapping.empty else 0,
231
+ },
232
+ "rmsd_crosscheck": {
233
+ "molecules": len(rmsd_crosscheck),
234
+ "max_abs_difference": max((row["max_abs_difference"] for row in rmsd_crosscheck), default=None),
235
+ },
236
+ "official_protocol_notes": {
237
+ "sampling": "BaseFlow.predict forwards n_timesteps directly to BaseFlow.sample; default sampler_type is ode.",
238
+ "chirality": "BaseFlow.sample applies switch_parity_of_pos when parity_switch == post_hoc before returning coordinates.",
239
+ "rmsd": "ET-Flow's official covmat evaluator uses RDKit GetBestRMS after hydrogen removal.",
240
+ "warning": "A local split-size mismatch means published benchmark values cannot be treated as a matched reproduction.",
241
+ },
242
+ }
243
+ output.with_name(output.name + "_report.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
244
+ lines = ["# ET-Flow Protocol Audit", "", "## Dataset", ""]
245
+ lines.append(f"- Local test molecules: {len(groups)}; expected official split: {args.expected_test_molecules}.")
246
+ lines.append(f"- K min/median/max: {report['dataset']['K_min']}/{report['dataset']['K_median']}/{report['dataset']['K_max']}; K > 16: {report['dataset']['fraction_K_gt_16']:.3f}.")
247
+ lines += ["", "## Checkpoint", "", f"- Requested model: `{args.model}`", f"- Cached checkpoint: `{checkpoint_path}`", f"- Parameter count: {parameter_count}", f"- Parity switch: `{getattr(model, 'parity_switch', None)}`", "", "## Mapping", "", f"- Atom-signature mismatches: {report['atom_mapping']['mismatches']} / {len(mapping)}."]
248
+ if not conditions.empty:
249
+ lines += ["", "## Conditions", "", conditions.to_markdown(index=False)]
250
+ output.with_name(output.name + "_report.md").write_text("\n".join(lines) + "\n", encoding="utf-8")
251
+ print(f"Saved: {output.with_name(output.name + '_report.md')}")
252
+ print(f"Saved: {output.with_name(output.name + '_report.json')}")
253
+ print(f"Saved: {output.with_name(output.name + '_atom_mapping.csv')}")
254
+ print(f"Saved: {output.with_name(output.name + '_conditions.csv')}")
255
+
256
+
257
+ if __name__ == "__main__":
258
+ main()
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env python3
2
+ """Audit external ligands against the GEOM-Drugs training molecules."""
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import json
7
+ from pathlib import Path
8
+
9
+ import pandas as pd
10
+ import torch
11
+ from rdkit import Chem, DataStructs
12
+
13
+ try:
14
+ from .external_validation_metrics import (
15
+ assign_similarity_bin,
16
+ molecular_identifiers,
17
+ morgan_fingerprint,
18
+ )
19
+ except ImportError:
20
+ from external_validation_metrics import (
21
+ assign_similarity_bin,
22
+ molecular_identifiers,
23
+ morgan_fingerprint,
24
+ )
25
+
26
+
27
+ def audit_external_molecules(train_molecules, external_molecules):
28
+ train_rows = []
29
+ seen_train = set()
30
+ for molecule in train_molecules:
31
+ identifiers = molecular_identifiers(molecule)
32
+ identity = (
33
+ identifiers["canonical_isomeric_smiles"],
34
+ identifiers["standard_inchikey"],
35
+ )
36
+ if identity in seen_train:
37
+ continue
38
+ seen_train.add(identity)
39
+ train_rows.append({**identifiers, "fingerprint": morgan_fingerprint(molecule)})
40
+ train_smiles = {row["canonical_isomeric_smiles"] for row in train_rows}
41
+ train_inchikeys = {row["standard_inchikey"] for row in train_rows}
42
+ train_scaffolds = {row["bemis_murcko_smiles"] for row in train_rows if row["bemis_murcko_smiles"]}
43
+ train_fingerprints = [row["fingerprint"] for row in train_rows]
44
+
45
+ audited = []
46
+ for source_id, molecule in external_molecules:
47
+ identifiers = molecular_identifiers(molecule)
48
+ similarities = DataStructs.BulkTanimotoSimilarity(
49
+ morgan_fingerprint(molecule), train_fingerprints
50
+ )
51
+ nearest_index = int(max(range(len(similarities)), key=similarities.__getitem__))
52
+ exact = (
53
+ identifiers["canonical_isomeric_smiles"] in train_smiles
54
+ or identifiers["standard_inchikey"] in train_inchikeys
55
+ )
56
+ similarity = float(similarities[nearest_index])
57
+ audited.append(
58
+ {
59
+ "source_id": str(source_id),
60
+ **identifiers,
61
+ "exact_identity": int(exact),
62
+ "eligible_after_exact_exclusion": int(not exact),
63
+ "scaffold_overlap": int(
64
+ bool(identifiers["bemis_murcko_smiles"])
65
+ and identifiers["bemis_murcko_smiles"] in train_scaffolds
66
+ ),
67
+ "max_ecfp4_tanimoto": similarity,
68
+ "similarity_bin": assign_similarity_bin(similarity),
69
+ "nearest_train_smiles": train_rows[nearest_index]["canonical_isomeric_smiles"],
70
+ }
71
+ )
72
+ return audited
73
+
74
+
75
+ def _molecule_from_record(record):
76
+ if isinstance(record, dict):
77
+ molecule = record.get("rdmol")
78
+ smiles = record.get("smiles")
79
+ else:
80
+ molecule = getattr(record, "rdmol", None)
81
+ smiles = getattr(record, "smiles", None)
82
+ return Chem.Mol(molecule) if molecule is not None else Chem.MolFromSmiles(str(smiles))
83
+
84
+
85
+ def _source_id(record, fallback):
86
+ if isinstance(record, dict):
87
+ return record.get("source_id", record.get("mol_key", fallback))
88
+ return getattr(record, "source_id", getattr(record, "mol_key", fallback))
89
+
90
+
91
+ def load_torch_molecules(path, include_ids=False):
92
+ records = torch.load(path, map_location="cpu", weights_only=False)
93
+ molecules = []
94
+ for index, record in enumerate(records):
95
+ molecule = _molecule_from_record(record)
96
+ if molecule is not None:
97
+ molecules.append((_source_id(record, index), molecule) if include_ids else molecule)
98
+ return molecules
99
+
100
+
101
+ def load_sdf_molecules(path):
102
+ molecules = []
103
+ for index, molecule in enumerate(Chem.SDMolSupplier(str(path), removeHs=False)):
104
+ if molecule is not None:
105
+ source_id = molecule.GetProp("_Name") if molecule.HasProp("_Name") else index
106
+ molecules.append((source_id, molecule))
107
+ return molecules
108
+
109
+
110
+ def parse_args():
111
+ parser = argparse.ArgumentParser()
112
+ parser.add_argument("--train-pt", required=True)
113
+ source = parser.add_mutually_exclusive_group(required=True)
114
+ source.add_argument("--external-pt")
115
+ source.add_argument("--external-sdf")
116
+ parser.add_argument("--output", required=True)
117
+ return parser.parse_args()
118
+
119
+
120
+ def main():
121
+ args = parse_args()
122
+ train = load_torch_molecules(args.train_pt)
123
+ external = (
124
+ load_torch_molecules(args.external_pt, include_ids=True)
125
+ if args.external_pt
126
+ else load_sdf_molecules(args.external_sdf)
127
+ )
128
+ rows = audit_external_molecules(train, external)
129
+ output = Path(args.output)
130
+ output.parent.mkdir(parents=True, exist_ok=True)
131
+ pd.DataFrame(rows).to_csv(output, index=False)
132
+ summary = {
133
+ "n_train_records": len(train),
134
+ "n_train_unique_smiles": len(
135
+ {molecular_identifiers(mol)["canonical_isomeric_smiles"] for mol in train}
136
+ ),
137
+ "n_external": len(rows),
138
+ "n_exact_identity_excluded": sum(row["exact_identity"] for row in rows),
139
+ "n_scaffold_overlap": sum(row["scaffold_overlap"] for row in rows),
140
+ "similarity_bin_counts": pd.Series(
141
+ [row["similarity_bin"] for row in rows]
142
+ ).value_counts().sort_index().to_dict(),
143
+ }
144
+ summary_path = output.with_name(output.stem + "_summary.json")
145
+ summary_path.write_text(json.dumps(summary, indent=2) + "\n")
146
+ print(json.dumps(summary, indent=2))
147
+
148
+
149
+ if __name__ == "__main__":
150
+ main()