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
utils/__init__.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from .save_mol import MolSaver,write_pred_pos_to_conformers,save_molecule_views
|
|
2
|
+
from .util import get_arg, safe_float, _atomic_write_json
|
|
3
|
+
# utils package
|
|
4
|
+
from .device import get_device
|
|
5
|
+
from .checkpoint import CheckpointManager
|
|
6
|
+
from .helpers import to_runconfig_from_args, compute_score_by_metric, get_arg, safe_float, save_molecule_views, _atomic_write_json
|
|
7
|
+
from utils.optuna import report_optuna_and_maybe_prune, write_final_metrics, handle_training_exception
|
utils/checkpoint.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import torch
|
|
4
|
+
from typing import Any, Dict
|
|
5
|
+
|
|
6
|
+
def save_checkpoint(model, optimizer, epoch: int, best_test_score: float, path: str):
|
|
7
|
+
# keep compatibility with exputils.save_checkpoint if present;
|
|
8
|
+
# if not, fallback to simple torch.save
|
|
9
|
+
try:
|
|
10
|
+
torch.save({
|
|
11
|
+
'model_state_dict': model.state_dict(),
|
|
12
|
+
'optimizer_state_dict': optimizer.state_dict(),
|
|
13
|
+
'epoch': epoch,
|
|
14
|
+
'best_test_score': best_test_score
|
|
15
|
+
}, path)
|
|
16
|
+
except Exception:
|
|
17
|
+
# last-resort: directory ensure then save
|
|
18
|
+
p = Path(path)
|
|
19
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
20
|
+
torch.save({
|
|
21
|
+
'model_state_dict': model.state_dict(),
|
|
22
|
+
'optimizer_state_dict': optimizer.state_dict(),
|
|
23
|
+
'epoch': epoch,
|
|
24
|
+
'best_test_score': best_test_score
|
|
25
|
+
}, str(p))
|
|
26
|
+
|
|
27
|
+
class CheckpointManager:
|
|
28
|
+
def __init__(self, save_dir: str):
|
|
29
|
+
self.save_dir = Path(save_dir)
|
|
30
|
+
self.save_dir.mkdir(parents=True, exist_ok=True)
|
|
31
|
+
|
|
32
|
+
def save_best(self, model, optimizer, epoch: int, best_test_score: float):
|
|
33
|
+
p = os.path.join(str(self.save_dir), 'best.pth')
|
|
34
|
+
save_checkpoint(model, optimizer, epoch, best_test_score, p)
|
|
35
|
+
return p
|
|
36
|
+
|
|
37
|
+
def save_epoch(self, model, optimizer, epoch: int, best_test_score: float):
|
|
38
|
+
p = os.path.join(str(self.save_dir), f'epoch{epoch}.pth')
|
|
39
|
+
save_checkpoint(model, optimizer, epoch, best_test_score, p)
|
|
40
|
+
return p
|
utils/device.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
def get_device(gpu: Optional[int] = None) -> torch.device:
|
|
5
|
+
if gpu is None:
|
|
6
|
+
return torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
7
|
+
if isinstance(gpu, str):
|
|
8
|
+
gpu_text = gpu.strip().lower()
|
|
9
|
+
if gpu_text in {"", "auto"}:
|
|
10
|
+
return torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
|
11
|
+
if gpu_text in {"cpu", "none", "-1"}:
|
|
12
|
+
return torch.device('cpu')
|
|
13
|
+
try:
|
|
14
|
+
torch.cuda.set_device(int(gpu))
|
|
15
|
+
return torch.device(f'cuda:{int(gpu)}')
|
|
16
|
+
except Exception:
|
|
17
|
+
return torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
utils/helpers.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
import os
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from dataclasses import asdict
|
|
5
|
+
from .device import get_device
|
|
6
|
+
try:
|
|
7
|
+
# import project utilities if available
|
|
8
|
+
from exputils import kabsch_alignment, mae_per_atom, get_best_rmsd
|
|
9
|
+
except Exception:
|
|
10
|
+
# placeholders if exputils is not importable at write-time
|
|
11
|
+
kabsch_alignment = lambda a,b: (a,b,None,None)
|
|
12
|
+
mae_per_atom = lambda a,b: 0.0
|
|
13
|
+
get_best_rmsd = lambda a,b: 0.0
|
|
14
|
+
|
|
15
|
+
def to_runconfig_from_args(args) -> Any:
|
|
16
|
+
"""Minimal conversion from args namespace to a simple object with attributes used in trainer."""
|
|
17
|
+
class C: pass
|
|
18
|
+
c = C()
|
|
19
|
+
c.dataset = getattr(args, 'dataset', getattr(args, 'data', 'QM9'))
|
|
20
|
+
c.data_root = getattr(args, 'data_root', '.')
|
|
21
|
+
c.bs = getattr(args, 'bs', 32)
|
|
22
|
+
c.lr = getattr(args, 'lr', 1e-3)
|
|
23
|
+
c.epoch = getattr(args, 'epoch', 100)
|
|
24
|
+
c.eta_min = getattr(args, 'eta_min', 0.0)
|
|
25
|
+
c.patience = getattr(args, 'patience', 20)
|
|
26
|
+
c.gpu = getattr(args, 'gpu', None)
|
|
27
|
+
c.metric = getattr(args, 'metric', 'RMSD')
|
|
28
|
+
c.pos_w = getattr(args, 'pos_w', 1.0)
|
|
29
|
+
c.mani_w = getattr(args, 'mani_w', 1.0)
|
|
30
|
+
c.get_image = bool(getattr(args, 'get_image', False))
|
|
31
|
+
c.smiles = getattr(args, 'smiles', None)
|
|
32
|
+
c.early_stop = bool(getattr(args, 'early_stop', False))
|
|
33
|
+
c.random_seed = getattr(args, 'random_seed', 0)
|
|
34
|
+
c.checkpoint_path = getattr(args, 'checkpoint_path', None)
|
|
35
|
+
c.num_workers = getattr(args, 'num_workers', 0)
|
|
36
|
+
c.pin_memory = getattr(args, 'pin_memory', False)
|
|
37
|
+
c.prefetch_factor = getattr(args, 'prefetch_factor', None)
|
|
38
|
+
c.run_bayesian_optimization = bool(getattr(args, 'run_bayesian_optimization', False))
|
|
39
|
+
return c
|
|
40
|
+
|
|
41
|
+
def compute_score_by_metric(y_pred_list, y_gt_list, metric: str) -> float:
|
|
42
|
+
#目前还是用RMSE,不要用其他的指标
|
|
43
|
+
metric = metric.upper()
|
|
44
|
+
if metric == 'MAE':
|
|
45
|
+
y_pred_aligned, _, _, _ = kabsch_alignment(y_pred_list, y_gt_list)
|
|
46
|
+
return mae_per_atom(y_pred_aligned, y_gt_list)
|
|
47
|
+
elif metric == 'RMSD':
|
|
48
|
+
s = 0.0
|
|
49
|
+
for p, g in zip(y_pred_list, y_gt_list):
|
|
50
|
+
s += get_best_rmsd(p, g)
|
|
51
|
+
return s / max(1, len(y_pred_list))
|
|
52
|
+
elif metric == 'SCORE_ALIGNMENT':
|
|
53
|
+
try:
|
|
54
|
+
from align3D_score import score_alignment
|
|
55
|
+
return score_alignment(y_pred_list, y_gt_list)
|
|
56
|
+
except Exception:
|
|
57
|
+
raise RuntimeError('score_alignment not available')
|
|
58
|
+
elif metric == 'CE':
|
|
59
|
+
raise RuntimeError('CE metric requires mani loss externally')
|
|
60
|
+
else:
|
|
61
|
+
raise ValueError(f'Unsupported metric: {metric}')
|
|
62
|
+
|
|
63
|
+
def get_arg(args, name, default=None):
|
|
64
|
+
return getattr(args, name, default)
|
|
65
|
+
|
|
66
|
+
def safe_float(x):
|
|
67
|
+
try:
|
|
68
|
+
return float(x)
|
|
69
|
+
except Exception:
|
|
70
|
+
return None
|
|
71
|
+
|
|
72
|
+
def save_molecule_views(output_dir, mol_pred, pos_gt_data=None, smiles_target=None, args_to_save=None):
|
|
73
|
+
# minimal wrapper: attempt to save rdkit mol as mol file and metadata
|
|
74
|
+
os.makedirs(output_dir, exist_ok=True)
|
|
75
|
+
try:
|
|
76
|
+
if mol_pred is not None:
|
|
77
|
+
path = os.path.join(output_dir, 'pred.mol')
|
|
78
|
+
with open(path, 'w') as f:
|
|
79
|
+
f.write(Chem.MolToMolBlock(mol_pred))
|
|
80
|
+
except Exception:
|
|
81
|
+
pass
|
|
82
|
+
meta = {'smiles': smiles_target, 'ts': datetime.now().isoformat()}
|
|
83
|
+
if args_to_save is not None:
|
|
84
|
+
try:
|
|
85
|
+
meta['args'] = str(args_to_save)
|
|
86
|
+
except Exception:
|
|
87
|
+
pass
|
|
88
|
+
with open(os.path.join(output_dir, 'meta.txt'), 'w') as f:
|
|
89
|
+
f.write(str(meta))
|
|
90
|
+
|
|
91
|
+
def _atomic_write_json(path, obj):
|
|
92
|
+
import json, tempfile
|
|
93
|
+
d = os.path.dirname(path)
|
|
94
|
+
os.makedirs(d, exist_ok=True)
|
|
95
|
+
with tempfile.NamedTemporaryFile('w', delete=False, dir=d) as tf:
|
|
96
|
+
json.dump(obj, tf, indent=2)
|
|
97
|
+
tmpn = tf.name
|
|
98
|
+
os.replace(tmpn, path)
|
utils/kabsch.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
def kabsch_rmsd(P, Q, eps=1e-8):
|
|
4
|
+
"""
|
|
5
|
+
可微 Kabsch 对齐并计算 RMSD。
|
|
6
|
+
P, Q: (N, 3) 张量
|
|
7
|
+
"""
|
|
8
|
+
# 中心化
|
|
9
|
+
centroid_P = P.mean(dim=0, keepdim=True)
|
|
10
|
+
centroid_Q = Q.mean(dim=0, keepdim=True)
|
|
11
|
+
P_cent = P - centroid_P
|
|
12
|
+
Q_cent = Q - centroid_Q
|
|
13
|
+
# 协方差矩阵
|
|
14
|
+
H = P_cent.T @ Q_cent
|
|
15
|
+
# SVD
|
|
16
|
+
U, S, Vt = torch.linalg.svd(H)
|
|
17
|
+
V = Vt.T
|
|
18
|
+
U = U.T
|
|
19
|
+
R = V @ U.T
|
|
20
|
+
det = torch.linalg.det(R)
|
|
21
|
+
# 处理反射:避免 inplace 操作,使用克隆
|
|
22
|
+
if det < 0:
|
|
23
|
+
V_new = V.clone()
|
|
24
|
+
V_new[:, -1] *= -1
|
|
25
|
+
R = V_new @ U.T
|
|
26
|
+
# 对齐
|
|
27
|
+
P_aligned = (P_cent @ R) + centroid_Q
|
|
28
|
+
rmsd = torch.sqrt(torch.mean((P_aligned - Q) ** 2) + eps)
|
|
29
|
+
return rmsd
|
utils/lookup_table.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""
|
|
2
|
+
utils/lookup_table.py
|
|
3
|
+
|
|
4
|
+
只负责读取离线预计算好的 lookup_table.pkl,并在训练/推理时通过
|
|
5
|
+
双线性插值快速获得 UMAP 低维核参数 a、b。
|
|
6
|
+
|
|
7
|
+
注意:
|
|
8
|
+
1. 本文件不再负责构建 lookup table。
|
|
9
|
+
2. lookup_table.pkl 应由 scripts/build_lookup_table.py 离线生成。
|
|
10
|
+
3. 训练和推理阶段只查表,不做 curve_fit 实时拟合。
|
|
11
|
+
|
|
12
|
+
推荐使用:
|
|
13
|
+
from utils.lookup_table import load_lookup_table_grid, bilinear_interp
|
|
14
|
+
|
|
15
|
+
min_grid, sp_grid, ab_grid = load_lookup_table_grid("data/lookup_table.pkl")
|
|
16
|
+
a, b = bilinear_interp(min_dist, spread, min_grid, sp_grid, ab_grid)
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import os
|
|
20
|
+
import pickle
|
|
21
|
+
import numpy as np
|
|
22
|
+
import torch
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def load_lookup_table_grid(cache_file="data/lookup_table.pkl"):
|
|
26
|
+
"""
|
|
27
|
+
加载离线生成的 lookup_table.pkl,并转换为规则网格张量。
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
cache_file: lookup_table.pkl 路径
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
min_grid: Tensor, shape [M]
|
|
34
|
+
sp_grid: Tensor, shape [N]
|
|
35
|
+
ab_grid: Tensor, shape [M, N, 2]
|
|
36
|
+
ab_grid[i, j, 0] = a
|
|
37
|
+
ab_grid[i, j, 1] = b
|
|
38
|
+
"""
|
|
39
|
+
if not os.path.exists(cache_file):
|
|
40
|
+
raise FileNotFoundError(
|
|
41
|
+
f"Lookup table file not found: {cache_file}. "
|
|
42
|
+
f"Please run scripts/build_lookup_table.py first."
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
with open(cache_file, "rb") as f:
|
|
46
|
+
table = pickle.load(f)
|
|
47
|
+
|
|
48
|
+
if not isinstance(table, dict):
|
|
49
|
+
raise TypeError(
|
|
50
|
+
f"lookup_table.pkl should contain a dict, got {type(table)}"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
min_vals = sorted(set(float(k[0]) for k in table.keys()))
|
|
54
|
+
sp_vals = sorted(set(float(k[1]) for k in table.keys()))
|
|
55
|
+
|
|
56
|
+
if len(min_vals) < 2 or len(sp_vals) < 2:
|
|
57
|
+
raise ValueError(
|
|
58
|
+
"Lookup table grid is too small. "
|
|
59
|
+
"Need at least 2 min_dist values and 2 spread values."
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
ab_grid = np.zeros((len(min_vals), len(sp_vals), 2), dtype=np.float32)
|
|
63
|
+
|
|
64
|
+
for i, md in enumerate(min_vals):
|
|
65
|
+
for j, sp in enumerate(sp_vals):
|
|
66
|
+
key = (round(md, 3), round(sp, 3))
|
|
67
|
+
|
|
68
|
+
if key not in table:
|
|
69
|
+
key = _find_nearest_key(table, md, sp)
|
|
70
|
+
|
|
71
|
+
a, b = table[key]
|
|
72
|
+
ab_grid[i, j, 0] = float(a)
|
|
73
|
+
ab_grid[i, j, 1] = float(b)
|
|
74
|
+
|
|
75
|
+
min_grid = torch.tensor(min_vals, dtype=torch.float)
|
|
76
|
+
sp_grid = torch.tensor(sp_vals, dtype=torch.float)
|
|
77
|
+
ab_grid = torch.tensor(ab_grid, dtype=torch.float)
|
|
78
|
+
|
|
79
|
+
return min_grid, sp_grid, ab_grid
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _find_nearest_key(table, min_dist, spread):
|
|
83
|
+
"""
|
|
84
|
+
防止因为浮点 round 问题导致 key 缺失。
|
|
85
|
+
"""
|
|
86
|
+
keys = np.array(list(table.keys()), dtype=np.float32)
|
|
87
|
+
target = np.array([min_dist, spread], dtype=np.float32)
|
|
88
|
+
dists = np.sum((keys - target) ** 2, axis=1)
|
|
89
|
+
nearest = keys[int(np.argmin(dists))]
|
|
90
|
+
return (float(nearest[0]), float(nearest[1]))
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def bilinear_interp(min_dist, spread, min_grid, sp_grid, ab_grid):
|
|
94
|
+
"""
|
|
95
|
+
根据 min_dist、spread 从离线 lookup table 中双线性插值得到 a、b。
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
min_dist: 标量 Tensor
|
|
99
|
+
spread: 标量 Tensor
|
|
100
|
+
min_grid: Tensor, shape [M]
|
|
101
|
+
sp_grid: Tensor, shape [N]
|
|
102
|
+
ab_grid: Tensor, shape [M, N, 2]
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
a: 标量 Tensor
|
|
106
|
+
b: 标量 Tensor
|
|
107
|
+
|
|
108
|
+
注意:
|
|
109
|
+
这里不是实时拟合,只是查表插值。
|
|
110
|
+
"""
|
|
111
|
+
device = min_dist.device
|
|
112
|
+
dtype = min_dist.dtype
|
|
113
|
+
|
|
114
|
+
min_grid = min_grid.to(device=device, dtype=dtype)
|
|
115
|
+
sp_grid = sp_grid.to(device=device, dtype=dtype)
|
|
116
|
+
ab_grid = ab_grid.to(device=device, dtype=dtype)
|
|
117
|
+
|
|
118
|
+
min_dist = min_dist.clamp(min_grid[0], min_grid[-1])
|
|
119
|
+
spread = spread.clamp(sp_grid[0], sp_grid[-1])
|
|
120
|
+
|
|
121
|
+
i = torch.searchsorted(min_grid, min_dist) - 1
|
|
122
|
+
j = torch.searchsorted(sp_grid, spread) - 1
|
|
123
|
+
|
|
124
|
+
i = i.clamp(0, min_grid.numel() - 2)
|
|
125
|
+
j = j.clamp(0, sp_grid.numel() - 2)
|
|
126
|
+
|
|
127
|
+
md0 = min_grid[i]
|
|
128
|
+
md1 = min_grid[i + 1]
|
|
129
|
+
sp0 = sp_grid[j]
|
|
130
|
+
sp1 = sp_grid[j + 1]
|
|
131
|
+
|
|
132
|
+
w_md = (min_dist - md0) / (md1 - md0 + 1e-12)
|
|
133
|
+
w_sp = (spread - sp0) / (sp1 - sp0 + 1e-12)
|
|
134
|
+
|
|
135
|
+
a00 = ab_grid[i, j, 0]
|
|
136
|
+
b00 = ab_grid[i, j, 1]
|
|
137
|
+
|
|
138
|
+
a01 = ab_grid[i, j + 1, 0]
|
|
139
|
+
b01 = ab_grid[i, j + 1, 1]
|
|
140
|
+
|
|
141
|
+
a10 = ab_grid[i + 1, j, 0]
|
|
142
|
+
b10 = ab_grid[i + 1, j, 1]
|
|
143
|
+
|
|
144
|
+
a11 = ab_grid[i + 1, j + 1, 0]
|
|
145
|
+
b11 = ab_grid[i + 1, j + 1, 1]
|
|
146
|
+
|
|
147
|
+
a = (
|
|
148
|
+
(1 - w_md) * (1 - w_sp) * a00
|
|
149
|
+
+ (1 - w_md) * w_sp * a01
|
|
150
|
+
+ w_md * (1 - w_sp) * a10
|
|
151
|
+
+ w_md * w_sp * a11
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
b = (
|
|
155
|
+
(1 - w_md) * (1 - w_sp) * b00
|
|
156
|
+
+ (1 - w_md) * w_sp * b01
|
|
157
|
+
+ w_md * (1 - w_sp) * b10
|
|
158
|
+
+ w_md * w_sp * b11
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
a = torch.clamp(a, min=1e-6)
|
|
162
|
+
b = torch.clamp(b, min=1e-6)
|
|
163
|
+
|
|
164
|
+
return a, b
|
utils/metrics.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from scipy.spatial.transform import Rotation as R
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def kabsch_rmsd(P, Q):
|
|
6
|
+
"""Kabsch 对齐后计算 RMSD,P, Q 形状 (N,3)"""
|
|
7
|
+
centroid_P = np.mean(P, axis=0)
|
|
8
|
+
centroid_Q = np.mean(Q, axis=0)
|
|
9
|
+
P_centered = P - centroid_P
|
|
10
|
+
Q_centered = Q - centroid_Q
|
|
11
|
+
H = P_centered.T @ Q_centered
|
|
12
|
+
U, S, Vt = np.linalg.svd(H)
|
|
13
|
+
V = Vt.T
|
|
14
|
+
U = U.T
|
|
15
|
+
R_mat = V @ U.T
|
|
16
|
+
if np.linalg.det(R_mat) < 0:
|
|
17
|
+
V[:, -1] *= -1
|
|
18
|
+
R_mat = V @ U.T
|
|
19
|
+
P_aligned = (P_centered @ R_mat) + centroid_Q
|
|
20
|
+
return np.sqrt(np.mean((P_aligned - Q) ** 2))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def compute_rmsd(ref_coords, gen_coords, atom_mask=None):
|
|
24
|
+
"""计算两个坐标集的对齐 RMSD(单分子)"""
|
|
25
|
+
ref_coords = np.asarray(ref_coords)
|
|
26
|
+
gen_coords = np.asarray(gen_coords)
|
|
27
|
+
if atom_mask is not None:
|
|
28
|
+
atom_mask = np.asarray(atom_mask, dtype=bool)
|
|
29
|
+
if atom_mask.shape[0] == ref_coords.shape[0] == gen_coords.shape[0] and atom_mask.sum() > 1:
|
|
30
|
+
ref_coords = ref_coords[atom_mask]
|
|
31
|
+
gen_coords = gen_coords[atom_mask]
|
|
32
|
+
return kabsch_rmsd(gen_coords, ref_coords)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def compute_cov_mat(gen_confs, ref_confs, threshold=0.5, atom_mask=None):
|
|
36
|
+
"""
|
|
37
|
+
计算 Coverage 和 Matching。
|
|
38
|
+
gen_confs: list of (N,3) arrays, 生成构象集合
|
|
39
|
+
ref_confs: list of (N,3) arrays, 真实构象集合
|
|
40
|
+
threshold: RMSD 阈值(QM9 常用 0.5,Drugs 用 1.25)
|
|
41
|
+
"""
|
|
42
|
+
# 对每个真实构象,找最短 RMSD
|
|
43
|
+
min_rmsds = []
|
|
44
|
+
for ref in ref_confs:
|
|
45
|
+
best_rmsd = min(compute_rmsd(ref, gen, atom_mask=atom_mask) for gen in gen_confs)
|
|
46
|
+
min_rmsds.append(best_rmsd)
|
|
47
|
+
|
|
48
|
+
coverage = np.mean([r < threshold for r in min_rmsds])
|
|
49
|
+
matching = np.mean(min_rmsds)
|
|
50
|
+
return coverage, matching
|
utils/optuna.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# utils/optuna.py
|
|
2
|
+
"""
|
|
3
|
+
集中管理训练过程中的 metrics 写入、Optuna 报告与异常记录逻辑。
|
|
4
|
+
将复杂的 IO/异常逻辑从 Trainer 中抽离,保持 Trainer 主流程简洁。
|
|
5
|
+
"""
|
|
6
|
+
#用来处理贝叶斯优化的东西
|
|
7
|
+
import os
|
|
8
|
+
import logging
|
|
9
|
+
from datetime import datetime
|
|
10
|
+
from typing import Optional
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
# 我们复用项目里已有的原子写入函数(如果你已经在 utils.helpers 中有 _atomic_write_json)
|
|
15
|
+
try:
|
|
16
|
+
from .helpers import _atomic_write_json
|
|
17
|
+
except Exception:
|
|
18
|
+
# 如果没有 helpers 的 _atomic_write_json,则提供一个简单的备选实现
|
|
19
|
+
import json
|
|
20
|
+
import tempfile
|
|
21
|
+
|
|
22
|
+
def _atomic_write_json(path, obj):
|
|
23
|
+
d = os.path.dirname(path)
|
|
24
|
+
os.makedirs(d, exist_ok=True)
|
|
25
|
+
fd, tmp = tempfile.mkstemp(dir=d, suffix=".json")
|
|
26
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
27
|
+
json.dump(obj, f, indent=2, ensure_ascii=False)
|
|
28
|
+
os.replace(tmp, path)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def report_optuna_and_maybe_prune(optuna_trial, best_valid_score: float, epoch: int, logger_path: str):
|
|
32
|
+
"""
|
|
33
|
+
如果 optuna_trial 非空,则上报当前 best_valid_score 并检查是否需要 prune。
|
|
34
|
+
若需要 prune,会在 logger_path 写入 metrics.json 并抛出 optuna.exceptions.TrialPruned。
|
|
35
|
+
任何异常都会被捕获并记录(不阻塞训练主流程)。
|
|
36
|
+
"""
|
|
37
|
+
if optuna_trial is None:
|
|
38
|
+
return
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
# 与 Trainer 原行为保持一致:如果 best_valid_score 仍为 inf,向 optuna 上报 None
|
|
42
|
+
to_report = None if best_valid_score == float("inf") else best_valid_score
|
|
43
|
+
optuna_trial.report(to_report, epoch)
|
|
44
|
+
if optuna_trial.should_prune():
|
|
45
|
+
metrics = {
|
|
46
|
+
"status": "pruned",
|
|
47
|
+
"best_valid_score": None if best_valid_score == float("inf") else best_valid_score,
|
|
48
|
+
"best_test_score": None, # test 未必计算
|
|
49
|
+
"best_epoch": None,
|
|
50
|
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
51
|
+
}
|
|
52
|
+
try:
|
|
53
|
+
_atomic_write_json(os.path.join(logger_path, "metrics.json"), metrics)
|
|
54
|
+
except Exception:
|
|
55
|
+
logger.exception("写入 pruned metrics 失败(忽略)")
|
|
56
|
+
# 抛出 optuna 的剪枝异常(调用方应处理或让其继续向上抛)
|
|
57
|
+
raise optuna.exceptions.TrialPruned()
|
|
58
|
+
except Exception:
|
|
59
|
+
logger.exception("Optuna 报告失败(忽略)")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def write_final_metrics(logger_path: str, best_valid_score: float, best_test_score: float, best_epoch: Optional[int]):
|
|
63
|
+
"""
|
|
64
|
+
在训练正常结束时写入 final metrics(status='finished')。
|
|
65
|
+
使用 _atomic_write_json 保证原子写入;异常会记录但不抛出。
|
|
66
|
+
"""
|
|
67
|
+
metrics = {
|
|
68
|
+
"status": "finished",
|
|
69
|
+
"best_valid_score": None if best_valid_score == float("inf") else best_valid_score,
|
|
70
|
+
"best_test_score": None if best_test_score == float("inf") else best_test_score,
|
|
71
|
+
"best_epoch": best_epoch,
|
|
72
|
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
73
|
+
}
|
|
74
|
+
try:
|
|
75
|
+
_atomic_write_json(os.path.join(logger_path, "metrics.json"), metrics)
|
|
76
|
+
except Exception:
|
|
77
|
+
logger.exception("写入 final metrics 失败")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def handle_training_exception(logger_path: str, exc: Exception, best_valid_score: float, best_test_score: float, best_epoch: Optional[int]):
|
|
81
|
+
"""
|
|
82
|
+
集中化异常记录:训练过程中发生未捕获异常时调用。
|
|
83
|
+
- 会写入 status='failed' 的 metrics.json,包含错误信息与当前 best 状态。
|
|
84
|
+
- 如果写入失败,会做最小化的 fallback(写文本),以避免在异常处理器中再次抛错。
|
|
85
|
+
"""
|
|
86
|
+
try:
|
|
87
|
+
metrics = {
|
|
88
|
+
"status": "failed",
|
|
89
|
+
"error": repr(exc),
|
|
90
|
+
"best_valid_score": None if best_valid_score == float("inf") else best_valid_score,
|
|
91
|
+
"best_test_score": None if best_test_score == float("inf") else best_test_score,
|
|
92
|
+
"best_epoch": best_epoch,
|
|
93
|
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
94
|
+
}
|
|
95
|
+
try:
|
|
96
|
+
_atomic_write_json(os.path.join(logger_path, "metrics.json"), metrics)
|
|
97
|
+
except Exception:
|
|
98
|
+
logger.exception("写入 failed metrics 失败,尝试 fallback 文本写入")
|
|
99
|
+
# fallback
|
|
100
|
+
os.makedirs(logger_path, exist_ok=True)
|
|
101
|
+
with open(os.path.join(logger_path, "metrics_failed.txt"), "w", encoding="utf-8") as f:
|
|
102
|
+
f.write("Failed to write metrics.json in exception handler.\n")
|
|
103
|
+
f.write("Original exception: " + repr(exc) + "\n")
|
|
104
|
+
except Exception:
|
|
105
|
+
# 绝对兜底:不能再 raise
|
|
106
|
+
try:
|
|
107
|
+
os.makedirs(logger_path, exist_ok=True)
|
|
108
|
+
with open(os.path.join(logger_path, "metrics_failed_2.txt"), "w", encoding="utf-8") as f:
|
|
109
|
+
f.write("Exception when handling training exception. Last-resort dump.\n")
|
|
110
|
+
except Exception:
|
|
111
|
+
pass
|