dualmapref 0.2.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- args_parse.py +593 -0
- candidate_corrector.py +161 -0
- candidate_scorer.py +359 -0
- conformend/__init__.py +36 -0
- conformend/refiner.py +25 -0
- dataset/__init__.py +1 -0
- dataset/drugdataset.py +644 -0
- dataset/manifold.py +237 -0
- dataset/sharded_drugdataset.py +124 -0
- dataset/smiles2graph.py +217 -0
- dualmapref/__init__.py +24 -0
- dualmapref/refiner.py +16 -0
- dualmapref-0.2.8.dist-info/LICENSE +21 -0
- dualmapref-0.2.8.dist-info/METADATA +167 -0
- dualmapref-0.2.8.dist-info/RECORD +186 -0
- dualmapref-0.2.8.dist-info/WHEEL +5 -0
- dualmapref-0.2.8.dist-info/entry_points.txt +10 -0
- dualmapref-0.2.8.dist-info/top_level.txt +29 -0
- exputils.py +289 -0
- infer.py +23 -0
- infer_base_conformers.py +5589 -0
- infer_mean_dispersion_torsion.py +1117 -0
- infer_torsion.py +752 -0
- infer_torsional_denoiser.py +1021 -0
- manimol/__init__.py +30 -0
- manimol/__main__.py +6 -0
- manimol/cli.py +36 -0
- manimol/full_cli.py +41 -0
- manimol/legacy_imports.py +43 -0
- manimol/manifest.py +56 -0
- manimol/postprocess.py +148 -0
- manimol/priors.py +60 -0
- manimol/protocols.py +119 -0
- manimol/refiner.py +645 -0
- manimol/runtime.py +194 -0
- manimol/selection.py +85 -0
- manimol/torsion.py +22 -0
- manimol/train_cli.py +14 -0
- mixture_flow/src/__init__.py +1 -0
- mixture_flow/src/_path.py +7 -0
- mixture_flow/src/infer28_mixture_torsion_flow.py +1093 -0
- mixture_flow/src/mixture_torsion_flow.py +291 -0
- mixture_flow/src/summarize_mixture_debug.py +80 -0
- mixture_flow/src/train28_mixture_torsion_flow.py +583 -0
- mixture_torsion_flow.py +7 -0
- models/ manifold_learning.py +86 -0
- models/__init__.py +8 -0
- models/dist.py +273 -0
- models/dist2coords.py +23 -0
- models/egnn.py +107 -0
- models/experimental_backbones.py +364 -0
- models/gnnconv.py +512 -0
- models/kernel_inversion.py +141 -0
- models/kernels.py +427 -0
- models/losses.py +154 -0
- models/mean_dispersion_bridge.py +119 -0
- models/mean_dispersion_torsion_context.py +155 -0
- models/model.py +73 -0
- models/model0.py +2375 -0
- models/vis.py +128 -0
- pgraph_controls.py +107 -0
- scripts/__init__.py +2 -0
- scripts/analyze_basin_alignment.py +63 -0
- scripts/analyze_hif2a_conformer_aggregation.py +267 -0
- scripts/analyze_hif2a_contact_pharmacophore.py +1111 -0
- scripts/analyze_hif2a_fixed_budget.py +202 -0
- scripts/analyze_hif2a_pharmacophore.py +405 -0
- scripts/analyze_hif2a_pharmacophore_match_metric.py +386 -0
- scripts/analyze_p_control_candidate_flow.py +154 -0
- scripts/analyze_scaffold_basin.py +83 -0
- scripts/audit_dispersion_proposals.py +208 -0
- scripts/audit_etflow_protocol.py +258 -0
- scripts/audit_external_overlap.py +150 -0
- scripts/audit_oldbest_fulltrainval_split.py +82 -0
- scripts/audit_pdbbind_contact_pharmacophores.py +146 -0
- scripts/audit_qm9_geomol_test_split.py +94 -0
- scripts/audit_stage1_stage2_cross.py +131 -0
- scripts/build_geom_drugs_lowref_diagnostic.py +84 -0
- scripts/build_qm9_backfill_manifest.py +99 -0
- scripts/check_etflow_runtime.py +56 -0
- scripts/compare_prior_sources.py +122 -0
- scripts/compose_binding_mode_figure.py +87 -0
- scripts/compose_hif2a_contact_pharmacophore_suppfig.py +255 -0
- scripts/compose_hif2a_functional_geometry_pipeline.py +266 -0
- scripts/compose_manimol_internal_architecture.py +291 -0
- scripts/diagnose_flow_basin_intervention.py +54 -0
- scripts/diagnose_hif2a_pharmacophore_budget.py +185 -0
- scripts/diagnose_route2_p_ranking.py +123 -0
- scripts/discover_binding_mode_inputs.py +30 -0
- scripts/evaluate_functional_prior_stage1.py +230 -0
- scripts/evaluate_geomanimol.py +74 -0
- scripts/evaluate_geomanimol_flow.py +23 -0
- scripts/evaluate_geomanimol_multiscaffold.py +46 -0
- scripts/evaluate_geomanimol_residual.py +45 -0
- scripts/evaluate_geomanimol_torsion.py +40 -0
- scripts/evaluate_hif2a_bioactive_enrichment.py +91 -0
- scripts/evaluate_hif2a_pms_suite.py +440 -0
- scripts/evaluate_pdbbind_contact_pms.py +155 -0
- scripts/evaluate_platinum_external.py +114 -0
- scripts/evaluate_platinum_pool.py +82 -0
- scripts/evaluate_route2_calibrated_p.py +104 -0
- scripts/evaluate_route2_library_components.py +596 -0
- scripts/evaluate_route2_p_basin_budget.py +288 -0
- scripts/evaluate_stage1_checkpoint_only.py +30 -0
- scripts/export_dispersion_bond_audit.py +136 -0
- scripts/export_top20_shards.py +207 -0
- scripts/external_validation_metrics.py +152 -0
- scripts/fit_topology_prior.py +117 -0
- scripts/frozen_dispersion_audit.py +281 -0
- scripts/geomanimol_debug.py +50 -0
- scripts/geomanimol_smoke.py +37 -0
- scripts/infer_etflow_matched.py +524 -0
- scripts/inspect_checkpoint.py +12 -0
- scripts/inspect_conformer_capacity.py +71 -0
- scripts/inspect_pool.py +12 -0
- scripts/make_figure3_selection_loss.py +474 -0
- scripts/make_figure3_selection_summary.py +275 -0
- scripts/make_geom_debug_subset.py +62 -0
- scripts/make_low_reference_test_subset.py +70 -0
- scripts/merge_stage1_into_stage2.py +82 -0
- scripts/place_hif2a_candidates_for_evaluation.py +66 -0
- scripts/plot_hif2a_low_budget_efficiency.py +100 -0
- scripts/plot_prior_validation.py +48 -0
- scripts/precompute_stage2_assignments.py +90 -0
- scripts/prepare_bace_activity.py +98 -0
- scripts/prepare_bace_etkdg_benchmark.py +90 -0
- scripts/prepare_figure5_4x4_cases.py +470 -0
- scripts/prepare_geom_drugs_geomol_top20.py +472 -0
- scripts/prepare_pdbbind_core_raw.py +90 -0
- scripts/prepare_platinum_raw.py +74 -0
- scripts/prepare_pubchem_etkdg.py +54 -0
- scripts/rebuild_hif2a_final_sdf_and_metrics.py +55 -0
- scripts/rebuild_hif2a_fixed_pool_sweep.py +175 -0
- scripts/rebuild_hif2a_strict.py +220 -0
- scripts/recompute_hif2a_bootstrap.py +63 -0
- scripts/reference_free_geometry.py +112 -0
- scripts/render_binding_modes.py +413 -0
- scripts/render_figure5_4x4.py +330 -0
- scripts/render_figure6_pharmacophore_pymol.py +162 -0
- scripts/render_hif2a_contact_pharmacophore_pymol.py +414 -0
- scripts/render_hif2a_figure11.py +397 -0
- scripts/run_bace_2d_baseline.py +73 -0
- scripts/run_drugs_validation_matrix.py +225 -0
- scripts/run_external_reference_free.py +53 -0
- scripts/run_manimol_finalk_sensitivity.py +106 -0
- scripts/run_manimol_oversampling_sensitivity.py +109 -0
- scripts/run_pdisp_matched_intervention.py +116 -0
- scripts/run_stage1_stage2_cross20.py +83 -0
- scripts/score_hif2a_candidates_with_pgraph.py +221 -0
- scripts/summarize_dispersion_intervention.py +88 -0
- scripts/test_mean_dispersion_bridge.py +50 -0
- scripts/train_dispersion_head_diagnostic.py +320 -0
- scripts/train_dispersion_head_streaming.py +519 -0
- scripts/train_geomanimol.py +100 -0
- scripts/train_geomanimol_flow.py +26 -0
- scripts/train_geomanimol_residual.py +70 -0
- scripts/train_geomanimol_torsion.py +50 -0
- scripts/train_quantile_representation_diagnostic.py +204 -0
- scripts/validate_mean_dispersion_prior.py +173 -0
- scripts/validate_packed_shard.py +41 -0
- scripts/validate_stage1_prior.py +221 -0
- scripts/validate_top20_shards.py +36 -0
- scripts/validation/__init__.py +1 -0
- scripts/validation/hif2a_downstream_baselines.py +105 -0
- scripts/validation/hif2a_pairwise_ranker.py +218 -0
- scripts/verify_binding_mode_figure.py +47 -0
- torsion_manifold.py +281 -0
- train.py +78 -0
- train_graph_encoder.py +206 -0
- train_local_torsion_corrector.py +511 -0
- train_mean_dispersion_torsion_prior.py +329 -0
- train_probability_basin.py +235 -0
- train_torsion_generator.py +1631 -0
- train_torsion_manifold_prior.py +258 -0
- train_torsional_denoiser.py +415 -0
- trainer.py +1038 -0
- utils/__init__.py +7 -0
- utils/checkpoint.py +40 -0
- utils/device.py +17 -0
- utils/helpers.py +98 -0
- utils/kabsch.py +29 -0
- utils/lookup_table.py +164 -0
- utils/metrics.py +50 -0
- utils/optuna.py +111 -0
- utils/save_mol.py +366 -0
- utils/util.py +36 -0
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Train the frozen dispersion head from a bounded-memory shard cache.
|
|
3
|
+
|
|
4
|
+
The source is the packed ``ShardedDrugDataset`` export. Frozen GNN features
|
|
5
|
+
and fixed-kernel pair targets are written to small derived shards once, then
|
|
6
|
+
loaded one shard at a time during optimization. The source checkpoint is
|
|
7
|
+
never overwritten.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import csv
|
|
13
|
+
import hashlib
|
|
14
|
+
import json
|
|
15
|
+
import math
|
|
16
|
+
import os
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
import random
|
|
19
|
+
import sys
|
|
20
|
+
from types import SimpleNamespace
|
|
21
|
+
|
|
22
|
+
import numpy as np
|
|
23
|
+
import torch
|
|
24
|
+
import torch.nn.functional as F
|
|
25
|
+
from scipy.stats import pearsonr, spearmanr
|
|
26
|
+
|
|
27
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def atomic_torch_save(payload, path):
|
|
31
|
+
path = Path(path)
|
|
32
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
33
|
+
temporary = path.with_suffix(path.suffix + ".tmp")
|
|
34
|
+
torch.save(payload, temporary, pickle_protocol=4)
|
|
35
|
+
os.replace(temporary, path)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def atomic_json_save(payload, path):
|
|
39
|
+
path = Path(path)
|
|
40
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
41
|
+
temporary = path.with_suffix(path.suffix + ".tmp")
|
|
42
|
+
temporary.write_text(json.dumps(payload, indent=2) + "\n")
|
|
43
|
+
os.replace(temporary, path)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class TargetStatistics:
|
|
47
|
+
"""Exact RMS with a bounded deterministic sample for target quantiles."""
|
|
48
|
+
|
|
49
|
+
def __init__(self, reservoir_size=1_000_000, seed=0):
|
|
50
|
+
self.count = 0
|
|
51
|
+
self.square_sum = 0.0
|
|
52
|
+
self._reservoir_size = max(1, int(reservoir_size))
|
|
53
|
+
self._rng = np.random.default_rng(seed)
|
|
54
|
+
self.reservoir = []
|
|
55
|
+
|
|
56
|
+
def update(self, values):
|
|
57
|
+
array = torch.as_tensor(values).detach().float().reshape(-1).cpu().numpy()
|
|
58
|
+
self.count += int(array.size)
|
|
59
|
+
self.square_sum += float(np.square(array, dtype=np.float64).sum())
|
|
60
|
+
if not array.size:
|
|
61
|
+
return
|
|
62
|
+
# At most 16 representatives per molecule are enough for a stable
|
|
63
|
+
# global quantile estimate while keeping Python work bounded.
|
|
64
|
+
if array.size > 16:
|
|
65
|
+
array = array[self._rng.choice(array.size, size=16, replace=False)]
|
|
66
|
+
for value in array.tolist():
|
|
67
|
+
if len(self.reservoir) < self._reservoir_size:
|
|
68
|
+
self.reservoir.append(float(value))
|
|
69
|
+
continue
|
|
70
|
+
index = int(self._rng.integers(0, self.count))
|
|
71
|
+
if index < self._reservoir_size:
|
|
72
|
+
self.reservoir[index] = float(value)
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def rms(self):
|
|
76
|
+
return math.sqrt(self.square_sum / max(1, self.count))
|
|
77
|
+
|
|
78
|
+
def quantile(self, probability):
|
|
79
|
+
if not self.reservoir:
|
|
80
|
+
raise ValueError("No target values were observed")
|
|
81
|
+
return float(np.quantile(np.asarray(self.reservoir), probability))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def correlations(reference, prediction):
|
|
85
|
+
reference = np.asarray(reference)
|
|
86
|
+
prediction = np.asarray(prediction)
|
|
87
|
+
if len(reference) < 3 or np.ptp(reference) < 1e-12 or np.ptp(prediction) < 1e-12:
|
|
88
|
+
return {"pearson": None, "spearman": None}
|
|
89
|
+
pearson = pearsonr(reference, prediction)
|
|
90
|
+
spearman = spearmanr(reference, prediction)
|
|
91
|
+
return {
|
|
92
|
+
"pearson": float(pearson.statistic if hasattr(pearson, "statistic") else pearson[0]),
|
|
93
|
+
"spearman": float(spearman.statistic if hasattr(spearman, "statistic") else spearman[0]),
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def pairwise_rank_loss(prediction, target, temperature, max_pairs=2048,
|
|
98
|
+
min_target_delta=0.0):
|
|
99
|
+
n = int(target.numel())
|
|
100
|
+
if n < 2 or max_pairs <= 0:
|
|
101
|
+
return prediction.sum() * 0.0
|
|
102
|
+
left = torch.randint(n, (min(max_pairs, max(n * 2, 32)),), device=target.device)
|
|
103
|
+
right = torch.randint(n, left.shape, device=target.device)
|
|
104
|
+
delta = target[left] - target[right]
|
|
105
|
+
keep = delta.abs() > float(min_target_delta)
|
|
106
|
+
if not keep.any():
|
|
107
|
+
return prediction.sum() * 0.0
|
|
108
|
+
signed = delta[keep].sign() * (prediction[left[keep]] - prediction[right[keep]])
|
|
109
|
+
return F.softplus(-signed / max(float(temperature), 1e-8)).mean()
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def selected_pair_features(model, record, device):
|
|
113
|
+
node = record["node"].to(device=device, dtype=torch.float32, non_blocking=True)
|
|
114
|
+
edge = record["edge"].to(device=device, dtype=torch.long, non_blocking=True)
|
|
115
|
+
pair_index = record["pair_index"].to(device=device, dtype=torch.long, non_blocking=True)
|
|
116
|
+
atom_i, atom_j = pair_index
|
|
117
|
+
graph = model._pair_graph_features(edge, len(node), device)
|
|
118
|
+
|
|
119
|
+
def ordered(left, right):
|
|
120
|
+
first, second = node[left], node[right]
|
|
121
|
+
return torch.cat(
|
|
122
|
+
[first, second, torch.abs(first - second), first * second, graph[left, right]],
|
|
123
|
+
dim=-1,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
return ordered(atom_i, atom_j), ordered(atom_j, atom_i)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _predict_pairs(model, records, device, head, maximum):
|
|
130
|
+
forward, reverse, lengths = [], [], []
|
|
131
|
+
for record in records:
|
|
132
|
+
left, right = selected_pair_features(model, record, device)
|
|
133
|
+
forward.append(left)
|
|
134
|
+
reverse.append(right)
|
|
135
|
+
lengths.append(int(left.shape[0]))
|
|
136
|
+
left_logits = head(torch.cat(forward, dim=0)).reshape(-1)
|
|
137
|
+
right_logits = head(torch.cat(reverse, dim=0)).reshape(-1)
|
|
138
|
+
values = maximum * 0.5 * (torch.sigmoid(left_logits) + torch.sigmoid(right_logits))
|
|
139
|
+
return list(torch.split(values, lengths))
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def predict_dispersion_pairs(model, records, device):
|
|
143
|
+
return _predict_pairs(model, records, device, model.dispersion_edge_predictor, 0.25)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def predict_mean_pairs(model, records, device):
|
|
147
|
+
return _predict_pairs(model, records, device, model.global_edge_predictor, 1.0)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def source_manifest(root):
|
|
151
|
+
with (Path(root) / "manifest.json").open() as handle:
|
|
152
|
+
return json.load(handle)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def packed_molecule_batches(root, split, molecule_limit, batch_size):
|
|
156
|
+
manifest = source_manifest(root)
|
|
157
|
+
info = manifest["splits"][split]
|
|
158
|
+
emitted = 0
|
|
159
|
+
pending = []
|
|
160
|
+
for shard in info["shards"]:
|
|
161
|
+
payload = torch.load(Path(root) / shard["file"], map_location="cpu", weights_only=False)
|
|
162
|
+
for molecule in payload["molecules"]:
|
|
163
|
+
if molecule_limit > 0 and emitted >= molecule_limit:
|
|
164
|
+
break
|
|
165
|
+
pending.append(molecule)
|
|
166
|
+
emitted += 1
|
|
167
|
+
if len(pending) >= batch_size:
|
|
168
|
+
yield pending
|
|
169
|
+
pending = []
|
|
170
|
+
if molecule_limit > 0 and emitted >= molecule_limit:
|
|
171
|
+
break
|
|
172
|
+
if pending:
|
|
173
|
+
yield pending
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def molecule_data(molecule):
|
|
177
|
+
from torch_geometric.data import Data
|
|
178
|
+
|
|
179
|
+
fields = {key: value for key, value in molecule.items() if key not in ("pos", "conf_id")}
|
|
180
|
+
fields["pos"] = molecule["pos"][0]
|
|
181
|
+
fields["conf_id"] = molecule["conf_id"][0]
|
|
182
|
+
return Data(**fields)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def feature_records(model, molecules, args, device):
|
|
186
|
+
from models.model0 import prob_low_dim_kernel
|
|
187
|
+
from torch_geometric.data import Batch
|
|
188
|
+
|
|
189
|
+
graphs = [molecule_data(molecule) for molecule in molecules]
|
|
190
|
+
batch = Batch.from_data_list(graphs).to(device)
|
|
191
|
+
with torch.no_grad():
|
|
192
|
+
embeddings = model.gnn(batch).detach()
|
|
193
|
+
records = []
|
|
194
|
+
for graph_index, molecule in enumerate(molecules):
|
|
195
|
+
mask = batch.batch == graph_index
|
|
196
|
+
node = embeddings[mask]
|
|
197
|
+
edge = model._get_local_edge_index(batch.edge_index, mask)
|
|
198
|
+
x = molecule["x"].to(device)
|
|
199
|
+
if args.heavy_only:
|
|
200
|
+
atoms = torch.where(x[:, 0] < 0.5)[0]
|
|
201
|
+
else:
|
|
202
|
+
atoms = torch.arange(len(x), device=device)
|
|
203
|
+
if len(atoms) < 2:
|
|
204
|
+
continue
|
|
205
|
+
pair_index = torch.combinations(atoms, r=2).t().contiguous()
|
|
206
|
+
positions = molecule["pos"][:args.max_confs].to(device)
|
|
207
|
+
with torch.no_grad():
|
|
208
|
+
q = torch.stack([
|
|
209
|
+
prob_low_dim_kernel(position, args.a, args.b, kernel="umap")
|
|
210
|
+
for position in positions
|
|
211
|
+
])
|
|
212
|
+
reference_mean = q.mean(dim=0)
|
|
213
|
+
reference_dispersion = q.var(dim=0, unbiased=False)
|
|
214
|
+
predicted_mean = model._predict_mean_dispersion(node, edge)[0]
|
|
215
|
+
atom_i, atom_j = pair_index
|
|
216
|
+
records.append({
|
|
217
|
+
"mid": int(molecule["mol_id"].reshape(-1)[0]),
|
|
218
|
+
"node": node.detach().cpu().to(torch.float16),
|
|
219
|
+
"edge": edge.detach().cpu().to(torch.int32),
|
|
220
|
+
"pair_index": pair_index.detach().cpu().to(torch.int16),
|
|
221
|
+
"mu_target": reference_mean[atom_i, atom_j].detach().cpu().float(),
|
|
222
|
+
"disp_target": reference_dispersion[atom_i, atom_j].detach().cpu().float(),
|
|
223
|
+
"mean_prediction": predicted_mean[atom_i, atom_j].detach().cpu().float(),
|
|
224
|
+
})
|
|
225
|
+
return records
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def build_feature_cache(args, model, signature, device):
|
|
229
|
+
cache_root = Path(args.feature_cache_root)
|
|
230
|
+
manifest_path = cache_root / "manifest.json"
|
|
231
|
+
manifest = None
|
|
232
|
+
if manifest_path.exists():
|
|
233
|
+
with manifest_path.open() as handle:
|
|
234
|
+
manifest = json.load(handle)
|
|
235
|
+
if manifest.get("signature") != signature:
|
|
236
|
+
raise ValueError("Feature-cache protocol mismatch")
|
|
237
|
+
if manifest is None:
|
|
238
|
+
manifest = {"format": "dispersion-features-v1", "signature": signature, "splits": {}}
|
|
239
|
+
split_specs = [
|
|
240
|
+
("train", "train", args.train_molecules),
|
|
241
|
+
("valid", "val", args.valid_molecules),
|
|
242
|
+
("test", "test", args.test_molecules),
|
|
243
|
+
]
|
|
244
|
+
for output_split, source_split, limit in split_specs:
|
|
245
|
+
existing = manifest["splits"].get(output_split)
|
|
246
|
+
if existing is not None and all(
|
|
247
|
+
(cache_root / relative).exists() for relative in existing.get("files", [])):
|
|
248
|
+
if existing.get("files") or (output_split == "test" and limit <= 0):
|
|
249
|
+
print("Reusing %s cache: %d molecules" %
|
|
250
|
+
(output_split, int(existing.get("molecules", 0))), flush=True)
|
|
251
|
+
continue
|
|
252
|
+
if output_split == "test" and limit <= 0:
|
|
253
|
+
manifest["splits"][output_split] = {"files": [], "molecules": 0}
|
|
254
|
+
continue
|
|
255
|
+
output_dir = cache_root / output_split
|
|
256
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
257
|
+
files, molecule_count, shard_records = [], 0, []
|
|
258
|
+
shard_index = 0
|
|
259
|
+
for molecules in packed_molecule_batches(
|
|
260
|
+
args.sharded_root, source_split, limit, args.feature_batch_size):
|
|
261
|
+
shard_records.extend(feature_records(model, molecules, args, device))
|
|
262
|
+
molecule_count += len(molecules)
|
|
263
|
+
if len(shard_records) >= args.cache_shard_molecules:
|
|
264
|
+
name = "shard_%06d.pt" % shard_index
|
|
265
|
+
atomic_torch_save({"records": shard_records}, output_dir / name)
|
|
266
|
+
files.append(str(Path(output_split) / name))
|
|
267
|
+
shard_records = []
|
|
268
|
+
shard_index += 1
|
|
269
|
+
print("Cached %s: %d molecules" % (output_split, molecule_count), flush=True)
|
|
270
|
+
if shard_records:
|
|
271
|
+
name = "shard_%06d.pt" % shard_index
|
|
272
|
+
atomic_torch_save({"records": shard_records}, output_dir / name)
|
|
273
|
+
files.append(str(Path(output_split) / name))
|
|
274
|
+
manifest["splits"][output_split] = {"files": files, "molecules": molecule_count}
|
|
275
|
+
atomic_json_save(manifest, manifest_path)
|
|
276
|
+
print("Completed %s cache: %d molecules in %d shards" %
|
|
277
|
+
(output_split, molecule_count, len(files)), flush=True)
|
|
278
|
+
return manifest
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def iter_records(cache_root, manifest, split, shuffle=False, seed=0):
|
|
282
|
+
files = list(manifest["splits"][split]["files"])
|
|
283
|
+
rng = random.Random(seed)
|
|
284
|
+
if shuffle:
|
|
285
|
+
rng.shuffle(files)
|
|
286
|
+
for relative in files:
|
|
287
|
+
records = torch.load(Path(cache_root) / relative, map_location="cpu", weights_only=False)["records"]
|
|
288
|
+
if shuffle:
|
|
289
|
+
rng.shuffle(records)
|
|
290
|
+
for record in records:
|
|
291
|
+
yield record
|
|
292
|
+
del records
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def record_batches(records, batch_size):
|
|
296
|
+
pending = []
|
|
297
|
+
for record in records:
|
|
298
|
+
pending.append(record)
|
|
299
|
+
if len(pending) >= batch_size:
|
|
300
|
+
yield pending
|
|
301
|
+
pending = []
|
|
302
|
+
if pending:
|
|
303
|
+
yield pending
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def target_statistics(cache_root, manifest, split, seed):
|
|
307
|
+
stats = TargetStatistics(seed=seed)
|
|
308
|
+
for record in iter_records(cache_root, manifest, split):
|
|
309
|
+
stats.update(record["disp_target"])
|
|
310
|
+
return stats
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def evaluate(model, cache_root, manifest, split, scale, batch_size, device):
|
|
314
|
+
losses = []
|
|
315
|
+
model.eval()
|
|
316
|
+
with torch.no_grad():
|
|
317
|
+
batches = record_batches(iter_records(cache_root, manifest, split), batch_size)
|
|
318
|
+
for records in batches:
|
|
319
|
+
predictions = predict_dispersion_pairs(model, records, device)
|
|
320
|
+
for record, prediction in zip(records, predictions):
|
|
321
|
+
target = record["disp_target"].to(device, non_blocking=True)
|
|
322
|
+
losses.append(float((((prediction - target) / scale).square().mean()).cpu()))
|
|
323
|
+
return float(np.mean(losses))
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def train_epoch(model, optimizer, cache_root, manifest, scale, high_threshold,
|
|
327
|
+
args, epoch, device):
|
|
328
|
+
losses, gradients = [], []
|
|
329
|
+
records = iter_records(cache_root, manifest, "train", shuffle=True, seed=args.seed + epoch)
|
|
330
|
+
for batch_records in record_batches(records, args.molecule_batch_size):
|
|
331
|
+
optimizer.zero_grad(set_to_none=True)
|
|
332
|
+
predictions = predict_dispersion_pairs(model, batch_records, device)
|
|
333
|
+
molecule_losses = []
|
|
334
|
+
for record, prediction in zip(batch_records, predictions):
|
|
335
|
+
target = record["disp_target"].to(device, non_blocking=True)
|
|
336
|
+
weights = 1.0 + args.high_dispersion_weight * (target >= high_threshold).to(target.dtype)
|
|
337
|
+
mse = ((((prediction - target) / scale).square() * weights).sum() /
|
|
338
|
+
weights.sum().clamp_min(1.0))
|
|
339
|
+
rank = pairwise_rank_loss(
|
|
340
|
+
prediction, target, scale, max_pairs=args.rank_pairs,
|
|
341
|
+
min_target_delta=0.1 * scale,
|
|
342
|
+
)
|
|
343
|
+
molecule_losses.append(mse + args.rank_weight * rank)
|
|
344
|
+
loss = torch.stack(molecule_losses).mean()
|
|
345
|
+
if not torch.isfinite(loss):
|
|
346
|
+
raise ValueError("Nonfinite training loss")
|
|
347
|
+
loss.backward()
|
|
348
|
+
gradient = torch.nn.utils.clip_grad_norm_(model.dispersion_edge_predictor.parameters(), 10.0)
|
|
349
|
+
if not torch.isfinite(gradient):
|
|
350
|
+
raise ValueError("Nonfinite gradient")
|
|
351
|
+
optimizer.step()
|
|
352
|
+
losses.append(float(loss.detach().cpu()))
|
|
353
|
+
gradients.append(float(gradient.detach().cpu()))
|
|
354
|
+
return float(np.mean(losses)), float(np.median(gradients))
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def audit(model, cache_root, manifest, split, batch_size, device):
|
|
358
|
+
rows, molecule_scores = [], []
|
|
359
|
+
model.eval()
|
|
360
|
+
with torch.no_grad():
|
|
361
|
+
batches = record_batches(iter_records(cache_root, manifest, split), batch_size)
|
|
362
|
+
for records in batches:
|
|
363
|
+
dispersion_predictions = predict_dispersion_pairs(model, records, device)
|
|
364
|
+
mean_predictions = predict_mean_pairs(model, records, device)
|
|
365
|
+
for record, disp_prediction, mean_prediction in zip(
|
|
366
|
+
records, dispersion_predictions, mean_predictions):
|
|
367
|
+
pair_index = record["pair_index"].long()
|
|
368
|
+
reference_disp = record["disp_target"].numpy()
|
|
369
|
+
predicted_disp = disp_prediction.cpu().numpy()
|
|
370
|
+
molecule_scores.append(correlations(reference_disp, predicted_disp))
|
|
371
|
+
for position in range(pair_index.shape[1]):
|
|
372
|
+
rows.append({
|
|
373
|
+
"molecule_id": record["mid"],
|
|
374
|
+
"atom_i": int(pair_index[0, position]),
|
|
375
|
+
"atom_j": int(pair_index[1, position]),
|
|
376
|
+
"reference_mu": float(record["mu_target"][position]),
|
|
377
|
+
"predicted_mu": float(mean_prediction[position].cpu()),
|
|
378
|
+
"reference_disp": float(record["disp_target"][position]),
|
|
379
|
+
"predicted_disp": float(disp_prediction[position].cpu()),
|
|
380
|
+
})
|
|
381
|
+
return rows, molecule_scores
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def parse_args():
|
|
385
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
386
|
+
parser.add_argument("--checkpoint", required=True)
|
|
387
|
+
parser.add_argument("--sharded-root", required=True)
|
|
388
|
+
parser.add_argument("--feature-cache-root", required=True)
|
|
389
|
+
parser.add_argument("--output-dir", required=True)
|
|
390
|
+
parser.add_argument("--a", type=float, default=1.6)
|
|
391
|
+
parser.add_argument("--b", type=float, default=0.8)
|
|
392
|
+
parser.add_argument("--max-confs", type=int, default=30)
|
|
393
|
+
parser.add_argument("--train-molecules", type=int, default=0)
|
|
394
|
+
parser.add_argument("--valid-molecules", type=int, default=0)
|
|
395
|
+
parser.add_argument("--test-molecules", type=int, default=1000)
|
|
396
|
+
parser.add_argument("--feature-batch-size", type=int, default=64)
|
|
397
|
+
parser.add_argument("--cache-shard-molecules", type=int, default=512)
|
|
398
|
+
parser.add_argument("--molecule-batch-size", type=int, default=32)
|
|
399
|
+
parser.add_argument("--epochs", type=int, default=20)
|
|
400
|
+
parser.add_argument("--patience", type=int, default=5)
|
|
401
|
+
parser.add_argument("--lr", type=float, default=1e-4)
|
|
402
|
+
parser.add_argument("--rank-weight", type=float, default=0.1)
|
|
403
|
+
parser.add_argument("--rank-pairs", type=int, default=2048)
|
|
404
|
+
parser.add_argument("--high-dispersion-weight", type=float, default=0.0)
|
|
405
|
+
parser.add_argument("--audit-split", choices=("valid", "test"), default="test")
|
|
406
|
+
parser.add_argument("--all-atoms", dest="heavy_only", action="store_false")
|
|
407
|
+
parser.set_defaults(heavy_only=True)
|
|
408
|
+
parser.add_argument("--device", default="cuda:0")
|
|
409
|
+
parser.add_argument("--threads", type=int, default=8)
|
|
410
|
+
parser.add_argument("--seed", type=int, default=20260913)
|
|
411
|
+
return parser.parse_args()
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def main():
|
|
415
|
+
from train_torsional_denoiser import load_base_model
|
|
416
|
+
|
|
417
|
+
args = parse_args()
|
|
418
|
+
torch.manual_seed(args.seed)
|
|
419
|
+
np.random.seed(args.seed)
|
|
420
|
+
random.seed(args.seed)
|
|
421
|
+
torch.set_num_threads(args.threads)
|
|
422
|
+
output = Path(args.output_dir)
|
|
423
|
+
output.mkdir(parents=True, exist_ok=True)
|
|
424
|
+
if (output / "best.pth").exists():
|
|
425
|
+
raise FileExistsError("Use a fresh output directory")
|
|
426
|
+
|
|
427
|
+
checkpoint_hash = hashlib.sha256(Path(args.checkpoint).read_bytes()).hexdigest()
|
|
428
|
+
source = source_manifest(args.sharded_root)
|
|
429
|
+
signature = {
|
|
430
|
+
"checkpoint_sha256": checkpoint_hash,
|
|
431
|
+
"source_identity": source.get("identity", {}).get("sha256", ""),
|
|
432
|
+
"a": args.a,
|
|
433
|
+
"b": args.b,
|
|
434
|
+
"max_confs": args.max_confs,
|
|
435
|
+
"train_molecules": args.train_molecules,
|
|
436
|
+
"valid_molecules": args.valid_molecules,
|
|
437
|
+
"test_molecules": args.test_molecules,
|
|
438
|
+
"heavy_only": args.heavy_only,
|
|
439
|
+
"protocol": "fixed_umap_population_variance_streaming_v1",
|
|
440
|
+
}
|
|
441
|
+
model = load_base_model(SimpleNamespace(checkpoint=args.checkpoint, max_confs=args.max_confs), "cpu")
|
|
442
|
+
device = torch.device(args.device)
|
|
443
|
+
model.to(device).eval()
|
|
444
|
+
for parameter in model.parameters():
|
|
445
|
+
parameter.requires_grad_(False)
|
|
446
|
+
for parameter in model.dispersion_edge_predictor.parameters():
|
|
447
|
+
parameter.requires_grad_(True)
|
|
448
|
+
|
|
449
|
+
manifest = build_feature_cache(args, model, signature, device)
|
|
450
|
+
stats = target_statistics(args.feature_cache_root, manifest, "train", args.seed)
|
|
451
|
+
scale = max(stats.rms, 1e-8)
|
|
452
|
+
high_threshold = stats.quantile(0.8)
|
|
453
|
+
frozen = {key: value.detach().cpu().clone() for key, value in model.state_dict().items()
|
|
454
|
+
if not key.startswith("dispersion_edge_predictor.")}
|
|
455
|
+
optimizer = torch.optim.Adam(model.dispersion_edge_predictor.parameters(), lr=args.lr)
|
|
456
|
+
best = evaluate(model, args.feature_cache_root, manifest, "valid",
|
|
457
|
+
scale, args.molecule_batch_size, device)
|
|
458
|
+
torch.save(model.state_dict(), output / "best.pth")
|
|
459
|
+
history, stale = [], 0
|
|
460
|
+
for epoch in range(args.epochs):
|
|
461
|
+
train_loss, gradient = train_epoch(
|
|
462
|
+
model, optimizer, args.feature_cache_root, manifest, scale,
|
|
463
|
+
high_threshold, args, epoch, device,
|
|
464
|
+
)
|
|
465
|
+
valid_loss = evaluate(model, args.feature_cache_root, manifest, "valid",
|
|
466
|
+
scale, args.molecule_batch_size, device)
|
|
467
|
+
improved = valid_loss < best
|
|
468
|
+
if improved:
|
|
469
|
+
best, stale = valid_loss, 0
|
|
470
|
+
torch.save(model.state_dict(), output / "best.pth")
|
|
471
|
+
else:
|
|
472
|
+
stale += 1
|
|
473
|
+
row = {"epoch": epoch, "train_normalized_loss": train_loss,
|
|
474
|
+
"valid_normalized_mse": valid_loss, "gradient_norm_median": gradient}
|
|
475
|
+
history.append(row)
|
|
476
|
+
atomic_json_save(history, output / "history.json")
|
|
477
|
+
print("Epoch %d: train=%.6g valid=%.6g best=%.6g" %
|
|
478
|
+
(epoch, train_loss, valid_loss, best), flush=True)
|
|
479
|
+
if args.patience > 0 and stale >= args.patience:
|
|
480
|
+
print("Early stopping after %d stale epochs" % stale, flush=True)
|
|
481
|
+
break
|
|
482
|
+
|
|
483
|
+
model.load_state_dict(torch.load(output / "best.pth", map_location=device, weights_only=True))
|
|
484
|
+
for key, value in frozen.items():
|
|
485
|
+
if not torch.equal(value, model.state_dict()[key].detach().cpu()):
|
|
486
|
+
raise AssertionError("Frozen state changed: %s" % key)
|
|
487
|
+
rows, per_molecule = audit(
|
|
488
|
+
model, args.feature_cache_root, manifest, args.audit_split,
|
|
489
|
+
args.molecule_batch_size, device,
|
|
490
|
+
)
|
|
491
|
+
with (output / "pair_audit.csv").open("w", newline="") as handle:
|
|
492
|
+
writer = csv.DictWriter(handle, fieldnames=list(rows[0]))
|
|
493
|
+
writer.writeheader()
|
|
494
|
+
writer.writerows(rows)
|
|
495
|
+
reference_disp = np.asarray([row["reference_disp"] for row in rows])
|
|
496
|
+
predicted_disp = np.asarray([row["predicted_disp"] for row in rows])
|
|
497
|
+
summary = {
|
|
498
|
+
"signature": signature,
|
|
499
|
+
"args": vars(args),
|
|
500
|
+
"scale": scale,
|
|
501
|
+
"high_dispersion_threshold": high_threshold,
|
|
502
|
+
"n_pairs": len(rows),
|
|
503
|
+
"n_audit_molecules": len(per_molecule),
|
|
504
|
+
"dispersion": correlations(reference_disp, predicted_disp),
|
|
505
|
+
"mean": correlations(
|
|
506
|
+
[row["reference_mu"] for row in rows],
|
|
507
|
+
[row["predicted_mu"] for row in rows],
|
|
508
|
+
),
|
|
509
|
+
"dispersion_molecule": per_molecule,
|
|
510
|
+
"mse": float(np.mean((reference_disp - predicted_disp) ** 2)),
|
|
511
|
+
"zero_baseline_mse": float(np.mean(reference_disp ** 2)),
|
|
512
|
+
"mean_unchanged": True,
|
|
513
|
+
}
|
|
514
|
+
atomic_json_save(summary, output / "summary.json")
|
|
515
|
+
print(json.dumps(summary, indent=2), flush=True)
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
if __name__ == "__main__":
|
|
519
|
+
main()
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Train the isolated, coupled GeoManimol route on a molecule list."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
|
|
12
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
13
|
+
|
|
14
|
+
from geomanimol.data import load_molecule_groups
|
|
15
|
+
from geomanimol.geometry import DynamicGeometryMemory
|
|
16
|
+
from geomanimol.model import GeoManimol
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def move_molecule(mol, device):
|
|
20
|
+
return {
|
|
21
|
+
**mol,
|
|
22
|
+
"atom_features": mol["atom_features"].to(device),
|
|
23
|
+
"edge_index": mol["edge_index"].to(device),
|
|
24
|
+
"edge_attr": mol["edge_attr"].to(device),
|
|
25
|
+
"conformations": mol["conformations"].to(device),
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def run_epoch(model, molecules, memory, optimizer=None, diffusion_steps=20):
|
|
30
|
+
training = optimizer is not None
|
|
31
|
+
model.train(training)
|
|
32
|
+
values = []
|
|
33
|
+
for molecule in molecules:
|
|
34
|
+
mol = move_molecule(molecule, next(model.parameters()).device)
|
|
35
|
+
entry = memory.get(molecule["mol_id"])
|
|
36
|
+
out = model(mol, mol["conformations"][0], entry, diffusion_steps=diffusion_steps)
|
|
37
|
+
if training:
|
|
38
|
+
optimizer.zero_grad(set_to_none=True)
|
|
39
|
+
out["loss"].backward()
|
|
40
|
+
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
|
|
41
|
+
optimizer.step()
|
|
42
|
+
values.append({k: float(out[k].detach()) for k in ("loss", "diffusion_loss", "probability_loss", "latent_alignment_loss", "weight_loss", "manifold_loss", "geometry_loss", "bond_loss")})
|
|
43
|
+
return {k: sum(v[k] for v in values) / len(values) for k in values[0]}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def main():
|
|
47
|
+
p = argparse.ArgumentParser()
|
|
48
|
+
p.add_argument("--train", default="data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_train.pt")
|
|
49
|
+
p.add_argument("--val", default="data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_val.pt")
|
|
50
|
+
p.add_argument("--train-molecules", type=int, default=5)
|
|
51
|
+
p.add_argument("--val-molecules", type=int, default=2)
|
|
52
|
+
p.add_argument("--epochs", type=int, default=1)
|
|
53
|
+
p.add_argument("--node-dim", type=int, default=32)
|
|
54
|
+
p.add_argument("--n-basins", type=int, default=3)
|
|
55
|
+
p.add_argument("--manifold-dim", type=int, default=8)
|
|
56
|
+
p.add_argument("--diffusion-steps", type=int, default=20)
|
|
57
|
+
p.add_argument("--sampling-steps", type=int, default=50)
|
|
58
|
+
p.add_argument("--memory-update-freq", type=int, default=25)
|
|
59
|
+
p.add_argument("--generated-for-update", type=int, default=4)
|
|
60
|
+
p.add_argument("--device", default="cpu")
|
|
61
|
+
p.add_argument("--output", default="outputs/geomanimol/debug_checkpoint.pt")
|
|
62
|
+
args = p.parse_args()
|
|
63
|
+
|
|
64
|
+
train = load_molecule_groups(args.train, args.train_molecules)
|
|
65
|
+
val = load_molecule_groups(args.val, args.val_molecules)
|
|
66
|
+
train_memory = DynamicGeometryMemory(n_modes=args.n_basins, manifold_dim=args.manifold_dim)
|
|
67
|
+
val_memory = DynamicGeometryMemory(n_modes=args.n_basins, manifold_dim=args.manifold_dim)
|
|
68
|
+
train_memory.initialize(train)
|
|
69
|
+
val_memory.initialize(val)
|
|
70
|
+
|
|
71
|
+
device = torch.device(args.device)
|
|
72
|
+
model = GeoManimol(args.node_dim, args.n_basins, args.manifold_dim, args.diffusion_steps).to(device)
|
|
73
|
+
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-6)
|
|
74
|
+
for epoch in range(args.epochs):
|
|
75
|
+
train_metrics = run_epoch(model, train, train_memory, optimizer, args.diffusion_steps)
|
|
76
|
+
with torch.no_grad():
|
|
77
|
+
val_metrics = run_epoch(model, val, val_memory, None, args.diffusion_steps)
|
|
78
|
+
print(f"epoch={epoch} train={train_metrics} val={val_metrics}")
|
|
79
|
+
|
|
80
|
+
if (epoch + 1) % args.memory_update_freq == 0:
|
|
81
|
+
model.eval()
|
|
82
|
+
memory_drift = []
|
|
83
|
+
for molecule in train:
|
|
84
|
+
mol = move_molecule(molecule, device)
|
|
85
|
+
generated = model.generate(mol, args.generated_for_update, args.sampling_steps).cpu()
|
|
86
|
+
memory_drift.append(train_memory.update(molecule["mol_id"], generated, molecule["conformations"]))
|
|
87
|
+
if memory_drift:
|
|
88
|
+
print("memory_update=" + str({
|
|
89
|
+
key: sum(item[key] for item in memory_drift) / len(memory_drift)
|
|
90
|
+
for key in memory_drift[0]
|
|
91
|
+
}))
|
|
92
|
+
|
|
93
|
+
output = Path(args.output)
|
|
94
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
95
|
+
torch.save({"model": model.state_dict(), "args": vars(args)}, output)
|
|
96
|
+
print(f"GEOMANIMOL_TRAIN_OK checkpoint={output} train={len(train)} val={len(val)}")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
if __name__ == "__main__":
|
|
100
|
+
main()
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Train the parallel GeoManimol torsion-flow debug route."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
import argparse, sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
import torch
|
|
7
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
8
|
+
from geomanimol.data import load_molecule_groups
|
|
9
|
+
from geomanimol.geometry import DynamicGeometryMemory
|
|
10
|
+
from geomanimol.residual import etkdg_scaffold
|
|
11
|
+
from geomanimol.torsion_flow import TorsionFlowGeoManimol
|
|
12
|
+
from scripts.train_geomanimol_torsion import move, assign_target
|
|
13
|
+
def main():
|
|
14
|
+
p=argparse.ArgumentParser(); p.add_argument('--train',default='data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_train.pt'); p.add_argument('--val',default='data/geom_drugs_geomol_top20/geom_drugs_geomol_top20_debug_val.pt'); p.add_argument('--train-molecules',type=int,default=20); p.add_argument('--val-molecules',type=int,default=20); p.add_argument('--epochs',type=int,default=20); p.add_argument('--node-dim',type=int,default=32); p.add_argument('--n-basins',type=int,default=3); p.add_argument('--manifold-dim',type=int,default=8); p.add_argument('--assignment-weight',type=float,default=.5); p.add_argument('--basin-weight',type=float,default=.2); p.add_argument('--assignment-temperature',type=float,default=.3); p.add_argument('--output',default='outputs/geomanimol/flow_expert_debug_checkpoint.pt'); a=p.parse_args(); train=load_molecule_groups(a.train,a.train_molecules); val=load_molecule_groups(a.val,a.val_molecules); tm=DynamicGeometryMemory(a.n_basins,a.manifold_dim); tm.initialize(train); vm=DynamicGeometryMemory(a.n_basins,a.manifold_dim); vm.initialize(val); model=TorsionFlowGeoManimol(a.node_dim,a.n_basins,a.manifold_dim,a.assignment_weight,a.basin_weight); opt=torch.optim.AdamW(model.parameters(),lr=1e-3,weight_decay=1e-6); best=-1; state=None
|
|
15
|
+
for e in range(a.epochs):
|
|
16
|
+
model.train(); tr=[]
|
|
17
|
+
for m0 in train:
|
|
18
|
+
m=move(m0,'cpu'); s=etkdg_scaffold(m['mol'],20260808+e*1000+m0['mol_id']); entry=tm.get(m0['mol_id']); entry['assignment_target'],entry['basin_torsion_centers']=assign_target(m,entry,s,a.n_basins,a.assignment_temperature); out=model(m,s,m['conformations'][0],entry); opt.zero_grad(); out['loss'].backward(); torch.nn.utils.clip_grad_norm_(model.parameters(),1.0); opt.step(); tr.append(float(out['loss']))
|
|
19
|
+
model.eval(); va=[]; acc=[]
|
|
20
|
+
with torch.no_grad():
|
|
21
|
+
for m0 in val:
|
|
22
|
+
m=move(m0,'cpu'); s=etkdg_scaffold(m['mol'],30300+m0['mol_id']); entry=vm.get(m0['mol_id']); entry['assignment_target'],entry['basin_torsion_centers']=assign_target(m,entry,s,a.n_basins,a.assignment_temperature); out=model(m,s,m['conformations'][0],entry); va.append(float(out['loss'])); acc.append(float(out['assignment_accuracy']))
|
|
23
|
+
score=sum(acc)/len(acc); print(f'epoch={e} train_loss={sum(tr)/len(tr):.5f} val_loss={sum(va)/len(va):.5f} val_assignment_acc={score:.3f}',flush=True)
|
|
24
|
+
if score>best: best=score; state={k:v.detach().clone() for k,v in model.state_dict().items()}
|
|
25
|
+
out=Path(a.output); out.parent.mkdir(parents=True,exist_ok=True); model.load_state_dict(state); torch.save({'model':model.state_dict(),'args':vars(a)},out); print(f'GEOMANIMOL_FLOW_TRAIN_OK checkpoint={out} best_assignment_acc={best:.3f}')
|
|
26
|
+
if __name__=='__main__': main()
|