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,524 @@
1
+ #!/usr/bin/env python3
2
+ """Evaluate ET-Flow on a local GEOM-style split.
3
+
4
+ This script is intentionally separate from MANIMOL inference. It loads the
5
+ same GEOM-style raw split, groups reference conformers by SMILES, samples
6
+ ET-Flow conformers under either a capped or exact-2K budget, and evaluates
7
+ COV/AMR with symmetry-aware heavy-atom RDKit RMSD.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+ import csv
14
+ import json
15
+ import os
16
+ import shutil
17
+ import subprocess
18
+ import tempfile
19
+ import time
20
+ from collections import OrderedDict
21
+ from pathlib import Path
22
+ from typing import Dict, Iterable, List, Tuple
23
+
24
+ import numpy as np
25
+ import pandas as pd
26
+ import torch
27
+ from rdkit import Chem, RDLogger
28
+ from rdkit.Chem import rdMolAlign
29
+ from rdkit.Geometry import Point3D
30
+
31
+ RDLogger.DisableLog("rdApp.warning")
32
+
33
+
34
+ def parse_args() -> argparse.Namespace:
35
+ parser = argparse.ArgumentParser(description=__doc__)
36
+ parser.add_argument("--data_root", default="data")
37
+ parser.add_argument("--raw_prefix", default="geom_drugs")
38
+ parser.add_argument("--split", default="test")
39
+ parser.add_argument("--dataset", default="Drugs")
40
+ parser.add_argument(
41
+ "--reference_template",
42
+ default="raw",
43
+ choices=["raw", "etflow"],
44
+ help="raw preserves legacy evaluation; etflow remaps reference coordinates onto ET-Flow's SMILES template.",
45
+ )
46
+ parser.add_argument("--model", "--etflow_model", dest="model", default="drugs-o3")
47
+ parser.add_argument("--device", default="cpu")
48
+ parser.add_argument("--cache", "--etflow_cache", dest="cache", default=str(Path.home() / ".cache" / "etflow"))
49
+ parser.add_argument("--steps", "--etflow_steps", dest="steps", type=int, default=50)
50
+ parser.add_argument("--batch_size", "--etflow_batch_size", dest="batch_size", type=int, default=1)
51
+ parser.add_argument("--seed", type=int, default=20260730)
52
+ parser.add_argument("--limit_molecules", type=int, default=0)
53
+ parser.add_argument("--start_molecule", type=int, default=0)
54
+ parser.add_argument("--target_cap", type=int, default=32)
55
+ parser.add_argument("--sample_multiplier", type=float, default=2.0)
56
+ parser.add_argument("--num_samples", type=int, default=0)
57
+ parser.add_argument(
58
+ "--budget_mode",
59
+ default="exact_2k",
60
+ choices=["capped_2k", "exact_2k", "fixed"],
61
+ help="exact_2k=multiplier*K (default); capped_2k=min(multiplier*K,target_cap); fixed=--num_samples.",
62
+ )
63
+ parser.add_argument("--sampler_type", default="ode", choices=["ode", "stochastic"])
64
+ parser.add_argument("--s_churn", type=float, default=1.0)
65
+ parser.add_argument("--t_min", type=float, default=1.0)
66
+ parser.add_argument("--t_max", type=float, default=1.0)
67
+ parser.add_argument("--noise_std", type=float, default=1.0)
68
+ parser.add_argument("--disable_posthoc_chirality", action="store_true",
69
+ help="Audit only: disable ET-Flow's built-in post-hoc parity correction.")
70
+ parser.add_argument("--include_smiles_csv", default="", help="Optional CSV whose smiles column defines a fixed evaluation subset.")
71
+ parser.add_argument("--xtb_relax", action="store_true", help="Apply GFN2-xTB tight relaxation to final ET-Flow conformers before RMSD evaluation.")
72
+ parser.add_argument("--xtb_command", default="xtb")
73
+ parser.add_argument("--xtb_max_confs", type=int, default=0, help="0 relaxes every final conformer.")
74
+ parser.add_argument("--thresholds", "--cov_thresholds", dest="thresholds", default="0.25,0.5,0.75,1.0,1.25,1.5,1.75,2.0")
75
+ parser.add_argument("--rmsd_max_matches", type=int, default=100000)
76
+ parser.add_argument("--output_prefix", default="outputs/visualization/etflow_drugs_matched")
77
+ parser.add_argument("--save_rmsd_npz", action="store_true")
78
+ args, _unknown = parser.parse_known_args()
79
+ return args
80
+
81
+
82
+ def load_processed_split(path: Path):
83
+ try:
84
+ return torch.load(str(path), map_location="cpu", weights_only=False)
85
+ except TypeError:
86
+ return torch.load(str(path), map_location="cpu")
87
+
88
+
89
+ def heavy_copy(mol: Chem.Mol) -> Chem.Mol:
90
+ out = Chem.RemoveHs(Chem.Mol(mol), sanitize=False)
91
+ Chem.SanitizeMol(out)
92
+ return out
93
+
94
+
95
+ def mol_single_conf(source: Chem.Mol, conf_id: int = 0) -> Chem.Mol:
96
+ out = Chem.Mol(source)
97
+ conf = source.GetConformer(int(conf_id))
98
+ out.RemoveAllConformers()
99
+ new_conf = Chem.Conformer(out.GetNumAtoms())
100
+ for atom_idx in range(out.GetNumAtoms()):
101
+ new_conf.SetAtomPosition(atom_idx, conf.GetAtomPosition(atom_idx))
102
+ out.AddConformer(new_conf, assignId=True)
103
+ return out
104
+
105
+
106
+ def mol_from_positions(template: Chem.Mol, positions: np.ndarray) -> Chem.Mol:
107
+ if int(positions.shape[0]) != int(template.GetNumAtoms()):
108
+ raise ValueError(
109
+ f"ET-Flow coordinate atom count {positions.shape[0]} does not match template {template.GetNumAtoms()}"
110
+ )
111
+ out = Chem.Mol(template)
112
+ out.RemoveAllConformers()
113
+ conf = Chem.Conformer(out.GetNumAtoms())
114
+ for atom_idx, xyz in enumerate(np.asarray(positions, dtype=float)):
115
+ conf.SetAtomPosition(int(atom_idx), Point3D(float(xyz[0]), float(xyz[1]), float(xyz[2])))
116
+ out.AddConformer(conf, assignId=True)
117
+ return out
118
+
119
+
120
+ def _reference_on_etflow_template(raw_mol: Chem.Mol, smiles: str) -> Chem.Mol:
121
+ """Map raw reference coordinates into the exact ET-Flow SMILES template."""
122
+ from etflow.commons.featurization import get_mol_from_smiles
123
+
124
+ raw = heavy_copy(raw_mol)
125
+ template = heavy_copy(get_mol_from_smiles(smiles))
126
+ matches = template.GetSubstructMatches(raw, uniquify=False, useChirality=False, maxMatches=100000)
127
+ if not matches:
128
+ raise ValueError("ET-Flow template and raw reference are not graph-isomorphic")
129
+ # A tuple maps template atom indices to raw atom indices. The first
130
+ # valid mapping is deterministic and preserves ET-Flow coordinate order.
131
+ mapping = matches[0]
132
+ raw_xyz = np.asarray(raw.GetConformer().GetPositions(), dtype=float)
133
+ return heavy_copy(mol_from_positions(template, raw_xyz[list(mapping)]))
134
+
135
+
136
+ def group_references(records: Iterable, reference_template: str = "raw") -> "OrderedDict[str, List[Chem.Mol]]":
137
+ groups: "OrderedDict[str, List[Chem.Mol]]" = OrderedDict()
138
+ for item in records:
139
+ smiles = str(getattr(item, "smiles", "")).strip()
140
+ mol = getattr(item, "rdmol", None)
141
+ if not smiles or mol is None:
142
+ continue
143
+ try:
144
+ raw = mol_single_conf(mol, 0)
145
+ reference = (
146
+ _reference_on_etflow_template(raw, smiles)
147
+ if reference_template == "etflow"
148
+ else heavy_copy(raw)
149
+ )
150
+ groups.setdefault(smiles, []).append(reference)
151
+ except Exception:
152
+ continue
153
+ return groups
154
+
155
+
156
+ def split_thresholds(text: str) -> List[float]:
157
+ out = []
158
+ for value in str(text).split(","):
159
+ value = value.strip()
160
+ if value:
161
+ out.append(float(value))
162
+ return out or [0.75]
163
+
164
+
165
+ def resolve_device(requested: str) -> str:
166
+ requested = str(requested or "cpu")
167
+ if requested.startswith("cuda") and not torch.cuda.is_available():
168
+ print(f"Requested device={requested}, but this PyTorch build has no CUDA. Falling back to cpu.")
169
+ return "cpu"
170
+ return requested
171
+
172
+
173
+ def predict_molecules(model, smiles: str, args: argparse.Namespace, num_samples: int) -> List[Chem.Mol]:
174
+ result = model.predict(
175
+ [smiles],
176
+ max_batch_size=int(args.batch_size),
177
+ num_samples=int(num_samples),
178
+ n_timesteps=int(args.steps),
179
+ seed=int(args.seed),
180
+ device=str(args.device),
181
+ sampler_type=str(args.sampler_type),
182
+ s_churn=float(args.s_churn),
183
+ t_min=float(args.t_min),
184
+ t_max=float(args.t_max),
185
+ std=float(args.noise_std),
186
+ as_mol=False,
187
+ )
188
+ item = result[smiles] if isinstance(result, dict) else result
189
+ mols: List[Chem.Mol] = []
190
+ if isinstance(item, np.ndarray):
191
+ from etflow.commons.featurization import get_mol_from_smiles
192
+
193
+ template = get_mol_from_smiles(smiles)
194
+ for coords in item[: int(num_samples)]:
195
+ try:
196
+ mols.append(heavy_copy(mol_from_positions(template, coords)))
197
+ except Exception:
198
+ continue
199
+ else:
200
+ for mol in list(item):
201
+ if mol is None:
202
+ continue
203
+ try:
204
+ mols.append(heavy_copy(mol))
205
+ except Exception:
206
+ continue
207
+ return mols[: int(num_samples)]
208
+
209
+
210
+ def write_xtb_xyz(mol: Chem.Mol, path: Path) -> None:
211
+ conf = mol.GetConformer()
212
+ lines = [str(mol.GetNumAtoms()), "ET-Flow conformer"]
213
+ for atom in mol.GetAtoms():
214
+ point = conf.GetAtomPosition(atom.GetIdx())
215
+ lines.append(f"{atom.GetSymbol()} {point.x:.10f} {point.y:.10f} {point.z:.10f}")
216
+ path.write_text("\n".join(lines) + "\n", encoding="utf-8")
217
+
218
+
219
+ def read_xtb_xyz(path: Path, n_atoms: int) -> np.ndarray:
220
+ lines = path.read_text(errors="ignore").splitlines()[2 : 2 + n_atoms]
221
+ if len(lines) != n_atoms:
222
+ raise ValueError("xTB output has an unexpected atom count")
223
+ return np.asarray([[float(x) for x in line.split()[1:4]] for line in lines], dtype=float)
224
+
225
+
226
+ def relax_with_xtb(mol: Chem.Mol, command: str) -> Chem.Mol:
227
+ """Apply the same GFN2-xTB tight coordinate relaxation used for MANIMOL's final pool."""
228
+ with tempfile.TemporaryDirectory(prefix="etflow_xtb_") as tmp_name:
229
+ tmp = Path(tmp_name)
230
+ inp = tmp / "input.xyz"
231
+ write_xtb_xyz(mol, inp)
232
+ charge = Chem.GetFormalCharge(mol)
233
+ env = os.environ.copy()
234
+ env.setdefault("OMP_NUM_THREADS", "1")
235
+ result = subprocess.run(
236
+ [command, str(inp), "--gfn", "2", "--opt", "tight", "--chrg", str(int(charge))],
237
+ cwd=tmp,
238
+ env=env,
239
+ stdout=subprocess.PIPE,
240
+ stderr=subprocess.PIPE,
241
+ text=True,
242
+ check=False,
243
+ )
244
+ output = tmp / "xtbopt.xyz"
245
+ if result.returncode != 0 or not output.exists():
246
+ return mol
247
+ try:
248
+ coords = read_xtb_xyz(output, mol.GetNumAtoms())
249
+ except Exception:
250
+ return mol
251
+ return mol_from_positions(mol, coords)
252
+
253
+
254
+ def relax_final_predictions(preds: List[Chem.Mol], args: argparse.Namespace) -> List[Chem.Mol]:
255
+ if not args.xtb_relax:
256
+ return preds
257
+ command = shutil.which(str(args.xtb_command))
258
+ if command is None:
259
+ raise RuntimeError("xTB relaxation requested but xtb executable was not found on PATH")
260
+ limit = len(preds) if int(args.xtb_max_confs) <= 0 else min(len(preds), int(args.xtb_max_confs))
261
+ return [relax_with_xtb(mol, command) if idx < limit else mol for idx, mol in enumerate(preds)]
262
+
263
+
264
+ def chirality_error_rate(preds: List[Chem.Mol], smiles: str) -> Tuple[float, int]:
265
+ """Fraction of ET-Flow tetrahedral centers with the wrong signed parity."""
266
+ try:
267
+ from etflow.commons.featurization import MoleculeFeaturizer
268
+ from etflow.commons.utils import signed_volume
269
+
270
+ data = MoleculeFeaturizer().get_data_from_smiles(smiles)
271
+ centers = data["chiral_index"].view(-1).long()
272
+ neighbors = data["chiral_nbr_index"].view(-1, 4).long()
273
+ tags = data["chiral_tag"].view(-1).float()
274
+ if centers.numel() == 0:
275
+ return float("nan"), 0
276
+ total = 0
277
+ incorrect = 0
278
+ for mol in preds:
279
+ xyz = np.asarray(mol.GetConformer().GetPositions(), dtype=np.float32)
280
+ if xyz.shape[0] <= int(neighbors.max()):
281
+ continue
282
+ volume = signed_volume(torch.from_numpy(xyz)[neighbors]).view(-1)
283
+ valid = tags != 0
284
+ signs = volume[valid] * tags[valid]
285
+ incorrect += int((signs < 0).sum().item())
286
+ total += int(valid.sum().item())
287
+ return (float(incorrect) / total if total else float("nan")), total
288
+ except Exception:
289
+ return float("nan"), 0
290
+
291
+
292
+ def rmsd_matrix(preds: List[Chem.Mol], refs: List[Chem.Mol], max_matches: int) -> torch.Tensor:
293
+ mat = torch.full((len(preds), len(refs)), float("nan"), dtype=torch.float32)
294
+ for i, pred in enumerate(preds):
295
+ for j, ref in enumerate(refs):
296
+ if pred.GetNumAtoms() != ref.GetNumAtoms():
297
+ continue
298
+ try:
299
+ mat[i, j] = float(
300
+ rdMolAlign.GetBestRMS(
301
+ Chem.Mol(pred),
302
+ Chem.Mol(ref),
303
+ 0,
304
+ 0,
305
+ maxMatches=max(1, int(max_matches)),
306
+ symmetrizeConjugatedTerminalGroups=True,
307
+ )
308
+ )
309
+ except Exception:
310
+ continue
311
+ if torch.isnan(mat).all():
312
+ raise ValueError("all RMSD entries failed")
313
+ return torch.nan_to_num(mat, nan=1.0e6, posinf=1.0e6, neginf=1.0e6)
314
+
315
+
316
+ def metrics_from_rmsd(rmsd: torch.Tensor, threshold: float) -> Tuple[float, float, float, float]:
317
+ ref_min = rmsd.min(dim=0).values
318
+ pred_min = rmsd.min(dim=1).values
319
+ cov_r = (ref_min < float(threshold)).float().mean().item()
320
+ amr_r = ref_min.mean().item()
321
+ cov_p = (pred_min < float(threshold)).float().mean().item()
322
+ amr_p = pred_min.mean().item()
323
+ return float(cov_r), float(amr_r), float(cov_p), float(amr_p)
324
+
325
+
326
+ def mean_median(values: List[float]) -> Tuple[float, float]:
327
+ arr = np.asarray(values, dtype=float)
328
+ return float(np.mean(arr)), float(np.median(arr))
329
+
330
+
331
+ def write_csv(path: Path, rows: List[dict]) -> None:
332
+ path.parent.mkdir(parents=True, exist_ok=True)
333
+ if not rows:
334
+ path.write_text("", encoding="utf-8")
335
+ return
336
+ keys: List[str] = []
337
+ for row in rows:
338
+ for key in row:
339
+ if key not in keys:
340
+ keys.append(key)
341
+ with path.open("w", newline="", encoding="utf-8") as handle:
342
+ writer = csv.DictWriter(handle, fieldnames=keys)
343
+ writer.writeheader()
344
+ writer.writerows(rows)
345
+
346
+
347
+ def main() -> None:
348
+ args = parse_args()
349
+ args.device = resolve_device(args.device)
350
+ thresholds = split_thresholds(args.thresholds)
351
+ split_path = Path(args.data_root) / f"{args.raw_prefix}_{args.split}.pt"
352
+ if not split_path.exists():
353
+ raise FileNotFoundError(split_path)
354
+
355
+ print(f"Loading processed split: {split_path}")
356
+ records = load_processed_split(split_path)
357
+ groups = group_references(records, reference_template=args.reference_template)
358
+ smiles_items = list(groups.items())[int(args.start_molecule) :]
359
+ if args.include_smiles_csv:
360
+ include = pd.read_csv(args.include_smiles_csv)
361
+ if "smiles" not in include.columns:
362
+ raise ValueError(f"{args.include_smiles_csv} must contain a smiles column")
363
+ requested = set(include["smiles"].dropna().astype(str))
364
+ smiles_items = [(smiles, refs) for smiles, refs in smiles_items if smiles in requested]
365
+ if int(args.limit_molecules) > 0:
366
+ smiles_items = smiles_items[: int(args.limit_molecules)]
367
+ print(f"Reference molecules: {len(smiles_items)}")
368
+ ref_counts = np.asarray([len(refs) for _, refs in smiles_items], dtype=int)
369
+ print(
370
+ f"ET-Flow model={args.model}, steps={args.steps}, device={args.device}, "
371
+ f"budget_mode={args.budget_mode}, cap={args.target_cap}, xtb_relax={args.xtb_relax}"
372
+ )
373
+ if ref_counts.size:
374
+ print(
375
+ "Reference budget audit: "
376
+ f"K min/median/max={ref_counts.min()}/{int(np.median(ref_counts))}/{ref_counts.max()}, "
377
+ f"K>16={float(np.mean(ref_counts > 16)):.3f}"
378
+ )
379
+
380
+ from etflow import BaseFlow
381
+
382
+ cache_dir = str(Path(args.cache).expanduser())
383
+ model = BaseFlow.from_default(model=args.model, device=args.device, cache=cache_dir)
384
+ model.eval()
385
+ if args.disable_posthoc_chirality:
386
+ model.parity_switch = "disabled_for_audit"
387
+ checkpoint_path = Path(cache_dir) / f"{args.model}.ckpt"
388
+ parameter_count = int(sum(parameter.numel() for parameter in model.parameters()))
389
+ print(f"ET-Flow checkpoint: {checkpoint_path}")
390
+ print(f"ET-Flow parameter count: {parameter_count}")
391
+ print(
392
+ "Sampling protocol: "
393
+ f"solver={args.sampler_type}, n_timesteps={args.steps}, s_churn={args.s_churn}, "
394
+ f"t_min={args.t_min}, t_max={args.t_max}, std={args.noise_std}, "
395
+ f"parity_switch={getattr(model, 'parity_switch', None)}"
396
+ )
397
+
398
+ metric_store: Dict[float, Dict[str, List[float]]] = {
399
+ t: {"covr": [], "amrr": [], "covp": [], "amrp": []} for t in thresholds
400
+ }
401
+ per_rows: List[dict] = []
402
+ runtimes: List[float] = []
403
+ rmsd_cache = {}
404
+
405
+ for mol_idx, (smiles, refs) in enumerate(smiles_items, start=1):
406
+ ref_count = len(refs)
407
+ if str(args.budget_mode) == "fixed":
408
+ if int(args.num_samples) < 1:
409
+ raise ValueError("--budget_mode fixed requires --num_samples >= 1")
410
+ target_count = int(args.num_samples)
411
+ else:
412
+ target_count = int(round(float(args.sample_multiplier) * ref_count))
413
+ if str(args.budget_mode) == "capped_2k":
414
+ target_count = min(int(args.target_cap), target_count)
415
+ target_count = max(1, target_count)
416
+ t0 = time.time()
417
+ try:
418
+ preds = predict_molecules(model, smiles, args, target_count)
419
+ if not preds:
420
+ raise ValueError("ET-Flow returned no valid molecules")
421
+ preds = relax_final_predictions(preds, args)
422
+ rmsd = rmsd_matrix(preds, refs, max_matches=int(args.rmsd_max_matches))
423
+ except Exception as exc:
424
+ print(f"[{mol_idx}/{len(smiles_items)}] SKIP {smiles}: {exc}")
425
+ continue
426
+ elapsed = time.time() - t0
427
+ runtimes.append(elapsed)
428
+ row = {
429
+ "smiles": smiles,
430
+ "ref_count": int(ref_count),
431
+ "target_count": int(target_count),
432
+ "generated_count": int(len(preds)),
433
+ "runtime_sec": float(elapsed),
434
+ }
435
+ chiral_error, chiral_centers = chirality_error_rate(preds, smiles)
436
+ row["chiral_error_rate"] = chiral_error
437
+ row["chiral_centers_evaluated"] = int(chiral_centers)
438
+ print(f"[{mol_idx}/{len(smiles_items)}] refs={ref_count}, generated={len(preds)}, time={elapsed:.1f}s")
439
+ for threshold in thresholds:
440
+ cov_r, amr_r, cov_p, amr_p = metrics_from_rmsd(rmsd, threshold)
441
+ metric_store[threshold]["covr"].append(cov_r)
442
+ metric_store[threshold]["amrr"].append(amr_r)
443
+ metric_store[threshold]["covp"].append(cov_p)
444
+ metric_store[threshold]["amrp"].append(amr_p)
445
+ tag = str(threshold).replace(".", "p")
446
+ row[f"cov_r_t{tag}"] = cov_r
447
+ row[f"amr_r_t{tag}"] = amr_r
448
+ row[f"cov_p_t{tag}"] = cov_p
449
+ row[f"amr_p_t{tag}"] = amr_p
450
+ per_rows.append(row)
451
+ if args.save_rmsd_npz:
452
+ rmsd_cache[smiles] = rmsd.numpy()
453
+
454
+ output_prefix = Path(args.output_prefix)
455
+ per_path = output_prefix.with_name(output_prefix.name + "_per_molecule.csv")
456
+ curve_path = output_prefix.with_name(output_prefix.name + "_curve.csv")
457
+ summary_path = output_prefix.with_name(output_prefix.name + "_summary.json")
458
+ write_csv(per_path, per_rows)
459
+
460
+ curve_rows: List[dict] = []
461
+ for threshold in thresholds:
462
+ vals = metric_store[threshold]
463
+ covr_mean, covr_med = mean_median(vals["covr"])
464
+ amrr_mean, amrr_med = mean_median(vals["amrr"])
465
+ covp_mean, covp_med = mean_median(vals["covp"])
466
+ amrp_mean, amrp_med = mean_median(vals["amrp"])
467
+ print(f"threshold={threshold:.2f} COV-R={covr_mean:.4f} AMR-R={amrr_mean:.4f} COV-P={covp_mean:.4f} AMR-P={amrp_mean:.4f}")
468
+ curve_rows.append(
469
+ {
470
+ "dataset": args.dataset,
471
+ "method": f"ET-Flow {args.model}",
472
+ "threshold": threshold,
473
+ "cov_r": covr_mean,
474
+ "cov_r_median": covr_med,
475
+ "amr_r": amrr_mean,
476
+ "amr_r_median": amrr_med,
477
+ "cov_p": covp_mean,
478
+ "cov_p_median": covp_med,
479
+ "amr_p": amrp_mean,
480
+ "amr_p_median": amrp_med,
481
+ "molecules": len(per_rows),
482
+ }
483
+ )
484
+ write_csv(curve_path, curve_rows)
485
+ summary = {
486
+ "model": args.model,
487
+ "split": str(split_path),
488
+ "molecules": len(per_rows),
489
+ "steps": int(args.steps),
490
+ "target_cap": int(args.target_cap),
491
+ "sample_multiplier": float(args.sample_multiplier),
492
+ "budget_mode": str(args.budget_mode),
493
+ "reference_template": str(args.reference_template),
494
+ "reference_count_min": int(ref_counts.min()) if ref_counts.size else None,
495
+ "reference_count_median": float(np.median(ref_counts)) if ref_counts.size else None,
496
+ "reference_count_max": int(ref_counts.max()) if ref_counts.size else None,
497
+ "fraction_reference_count_gt_16": float(np.mean(ref_counts > 16)) if ref_counts.size else None,
498
+ "checkpoint_path": str(checkpoint_path),
499
+ "parameter_count": parameter_count,
500
+ "parity_switch": str(getattr(model, "parity_switch", None)),
501
+ "sampler_type": str(args.sampler_type),
502
+ "s_churn": float(args.s_churn),
503
+ "t_min": float(args.t_min),
504
+ "t_max": float(args.t_max),
505
+ "noise_std": float(args.noise_std),
506
+ "disable_posthoc_chirality": bool(args.disable_posthoc_chirality),
507
+ "include_smiles_csv": args.include_smiles_csv or None,
508
+ "xtb_relax": bool(args.xtb_relax),
509
+ "xtb_max_confs": int(args.xtb_max_confs),
510
+ "mean_runtime_sec_per_molecule": float(np.mean(runtimes)) if runtimes else None,
511
+ "median_runtime_sec_per_molecule": float(np.median(runtimes)) if runtimes else None,
512
+ "thresholds": thresholds,
513
+ }
514
+ summary_path.parent.mkdir(parents=True, exist_ok=True)
515
+ summary_path.write_text(json.dumps(summary, indent=2), encoding="utf-8")
516
+ if args.save_rmsd_npz and rmsd_cache:
517
+ np.savez_compressed(output_prefix.with_name(output_prefix.name + "_rmsd.npz"), **rmsd_cache)
518
+ print(f"Saved: {per_path}")
519
+ print(f"Saved: {curve_path}")
520
+ print(f"Saved: {summary_path}")
521
+
522
+
523
+ if __name__ == "__main__":
524
+ main()
@@ -0,0 +1,12 @@
1
+ import sys
2
+ import torch
3
+
4
+ p = sys.argv[1]
5
+ x = torch.load(p, map_location="cpu", weights_only=False)
6
+ print("keys", list(x.keys()))
7
+ for key in ("epoch", "step", "global_step", "best_metric", "metric", "args"):
8
+ if key in x:
9
+ value = x[key]
10
+ if key == "args" and isinstance(value, dict):
11
+ value = {k: value.get(k) for k in ("epoch", "metric", "max_confs", "exp_id", "train_stage") if k in value}
12
+ print(key, value)
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env python3
2
+ """Report molecule-wise conformer counts in raw GEOM-style split files.
3
+
4
+ This script is read-only. It does not create processed PyG caches and is
5
+ intended to make the selected ``max_confs`` protocol explicit before training.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import argparse
11
+ import os
12
+ from collections import Counter
13
+ from pathlib import Path
14
+ import sys
15
+
16
+ import torch
17
+ from rdkit import RDLogger
18
+
19
+ ROOT = Path(__file__).resolve().parents[1]
20
+ if str(ROOT) not in sys.path:
21
+ sys.path.insert(0, str(ROOT))
22
+
23
+ from dataset.drugdataset import QM9Dataset
24
+
25
+
26
+ def valid_key(item):
27
+ try:
28
+ mol = item["rdmol"]
29
+ pos = torch.as_tensor(item["pos"])
30
+ if mol is None or pos.dim() != 2 or pos.size(0) != mol.GetNumAtoms():
31
+ return None
32
+ return str(item["smiles"]), int(mol.GetNumAtoms())
33
+ except (KeyError, TypeError, AttributeError):
34
+ return None
35
+
36
+
37
+ def main():
38
+ parser = argparse.ArgumentParser(description="Inspect raw molecule-wise conformer capacities.")
39
+ parser.add_argument("--data_root", default="data")
40
+ parser.add_argument("--raw_prefix", default="geom_drugs")
41
+ args, _ = parser.parse_known_args()
42
+ RDLogger.DisableLog("rdApp.warning")
43
+
44
+ probe = QM9Dataset.__new__(QM9Dataset)
45
+ probe.root = args.data_root
46
+ probe.raw_prefix = args.raw_prefix
47
+ global_max = 0
48
+ for filename in probe.raw_file_names:
49
+ path = os.path.join(args.data_root, filename)
50
+ records = torch.load(path, map_location="cpu", weights_only=False)
51
+ counts = Counter(key for item in records if (key := valid_key(item)) is not None)
52
+ values = sorted(counts.values())
53
+ if not values:
54
+ print(f"{filename}: no valid conformers")
55
+ continue
56
+ global_max = max(global_max, values[-1])
57
+ q = lambda p: values[min(len(values) - 1, int(round((len(values) - 1) * p)))]
58
+ print(
59
+ f"{filename}: molecules={len(values)}, conformers={sum(values)}, "
60
+ f"min/median/p90/max={values[0]}/{q(0.50)}/{q(0.90)}/{values[-1]}"
61
+ )
62
+ print(f"Global raw maximum: {global_max}")
63
+ print(
64
+ "Auto mode uses the train/validation maximum because it defines the "
65
+ "trained conformer-ID embedding capacity. Test references can remain "
66
+ "larger for raw-reference evaluation."
67
+ )
68
+
69
+
70
+ if __name__ == "__main__":
71
+ main()
@@ -0,0 +1,12 @@
1
+ import sys
2
+ import torch
3
+
4
+ p = sys.argv[1]
5
+ x = torch.load(p, map_location="cpu", weights_only=False)
6
+ print("payload", list(x.keys()), "records", len(x.get("records", [])))
7
+ r = x["records"][0]
8
+ print("record", sorted(r.keys()))
9
+ for key in ("raw_pool", "final_pool", "ref_confs", "pool", "selected", "generated", "conformers"):
10
+ if key in r:
11
+ value = r[key]
12
+ print(key, tuple(value.shape) if hasattr(value, "shape") else type(value).__name__)