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,474 @@
|
|
|
1
|
+
# Academic Figure Skill Asset Confirmation (verified against assets/figures/)
|
|
2
|
+
# (a) scatter -> cross-type inherit -> param inherit
|
|
3
|
+
# (b) ECDF -> cross-type inherit -> param inherit
|
|
4
|
+
# (c) scatter + grouped trend -> cross-type inherit -> param inherit
|
|
5
|
+
# (d) paired boxplot -> assets/figures/PairedBoxScatter -> param inherit
|
|
6
|
+
# RULE: "native run" = load pre-rendered PNG via Image.open().ax.imshow().
|
|
7
|
+
# "param inherit" = drawing function below that copies Class A/B/C values.
|
|
8
|
+
# If a panel says "native run" and you write a drawing function, you broke the contract.
|
|
9
|
+
|
|
10
|
+
#!/usr/bin/env python3
|
|
11
|
+
"""Audit finite-budget coverage loss and render manuscript Figure 3.
|
|
12
|
+
|
|
13
|
+
The input is the raw candidate-pool cache written by ``infer_base_conformers.py``. The
|
|
14
|
+
script deliberately reuses the repository's RMSD and selector functions so
|
|
15
|
+
that the diagnostic cannot silently drift from the benchmark protocol.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import argparse
|
|
21
|
+
import csv
|
|
22
|
+
import math
|
|
23
|
+
import os
|
|
24
|
+
import random
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
import matplotlib as mpl
|
|
28
|
+
|
|
29
|
+
mpl.use("Agg")
|
|
30
|
+
import matplotlib.pyplot as plt
|
|
31
|
+
import numpy as np
|
|
32
|
+
import pandas as pd
|
|
33
|
+
import torch
|
|
34
|
+
from scipy.stats import spearmanr
|
|
35
|
+
|
|
36
|
+
from infer_base_conformers import (
|
|
37
|
+
compute_rmsd_matrix,
|
|
38
|
+
find_rotatable_bonds,
|
|
39
|
+
select_precision2r_conformers,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Academic Figure Skill Typography Baseline — COPY VERBATIM, place at TOP of script
|
|
43
|
+
mpl.rcParams.update({
|
|
44
|
+
"font.family": "sans-serif",
|
|
45
|
+
"font.sans-serif": ["Arial", "Helvetica", "Liberation Sans"],
|
|
46
|
+
"font.size": 8,
|
|
47
|
+
"axes.titlesize": 8,
|
|
48
|
+
"axes.labelsize": 8,
|
|
49
|
+
"xtick.labelsize": 7,
|
|
50
|
+
"ytick.labelsize": 7,
|
|
51
|
+
"legend.fontsize": 8,
|
|
52
|
+
"figure.titlesize": 9,
|
|
53
|
+
"axes.spines.top": False,
|
|
54
|
+
"axes.spines.right": False,
|
|
55
|
+
"axes.linewidth": 0.6,
|
|
56
|
+
"xtick.direction": "out",
|
|
57
|
+
"ytick.direction": "out",
|
|
58
|
+
"xtick.major.width": 0.6,
|
|
59
|
+
"ytick.major.width": 0.6,
|
|
60
|
+
"legend.frameon": False,
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
# Academic Figure Skill Nature/Cell/Science Color Palette -- COPY VERBATIM
|
|
64
|
+
CATEGORICAL = ["#2166AC", "#B2182B", "#1B7837", "#F1A340", "#762A83", "#666666"]
|
|
65
|
+
CATEGORICAL_EXTENDED = [
|
|
66
|
+
"#2166AC", "#B2182B", "#1B7837", "#F1A340", "#762A83", "#666666",
|
|
67
|
+
"#4393C3", "#D6604D", "#5AAE61", "#B35806", "#9970AB", "#999999",
|
|
68
|
+
]
|
|
69
|
+
DIVERGING = ["#2166AC", "#F7F7F7", "#B2182B"]
|
|
70
|
+
SEQUENTIAL = ["#F7FBFF", "#6BAED6", "#08306B"]
|
|
71
|
+
ACCENT_RED = "#B2182B"
|
|
72
|
+
GREY = "#999999"
|
|
73
|
+
BLACK = "#222222"
|
|
74
|
+
|
|
75
|
+
# Academic Figure Skill Export Baseline — COPY VERBATIM
|
|
76
|
+
mpl.rcParams.update({
|
|
77
|
+
"pdf.fonttype": 42, # TrueType font embedding
|
|
78
|
+
"svg.fonttype": "none", # editable text in SVG
|
|
79
|
+
"savefig.bbox": "tight", # trim whitespace
|
|
80
|
+
"savefig.dpi": 300,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def save_cns_figure(fig, filename):
|
|
85
|
+
"""Standard Academic Figure Skill export: vector PDF + 300dpi PNG preview."""
|
|
86
|
+
fig.savefig(f"{filename}.pdf", bbox_inches="tight", dpi=300)
|
|
87
|
+
fig.savefig(f"{filename}.png", bbox_inches="tight", dpi=300)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
THRESHOLD = 0.75
|
|
91
|
+
BOOTSTRAP_REPEATS = 10_000
|
|
92
|
+
SEED = 20260914
|
|
93
|
+
COLORS = {
|
|
94
|
+
"blue": "#4C72B0",
|
|
95
|
+
"orange": "#DD8452",
|
|
96
|
+
"green": "#55A868",
|
|
97
|
+
"purple": "#8172B3",
|
|
98
|
+
"gray": "#7A7A7A",
|
|
99
|
+
"light_gray": "#D9D9D9",
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def parse_args() -> argparse.Namespace:
|
|
104
|
+
p = argparse.ArgumentParser(description=__doc__)
|
|
105
|
+
source = p.add_mutually_exclusive_group(required=True)
|
|
106
|
+
source.add_argument("--raw-pool", help="torch raw-pool cache")
|
|
107
|
+
source.add_argument(
|
|
108
|
+
"--metrics-csv",
|
|
109
|
+
help="precomputed per-molecule metrics exported by this script on another machine",
|
|
110
|
+
)
|
|
111
|
+
p.add_argument("--output-dir", default="results/figure3_selection_loss")
|
|
112
|
+
p.add_argument("--figure-dir", default="NCS_ManiMol/FIG")
|
|
113
|
+
p.add_argument("--threshold", type=float, default=THRESHOLD)
|
|
114
|
+
p.add_argument("--rmsd-backend", default="rdkit_best", choices=["rdkit_best", "kabsch"])
|
|
115
|
+
p.add_argument("--rdkit-max-matches", type=int, default=1_000_000)
|
|
116
|
+
p.add_argument("--selector-metric", default="centered_rmsd")
|
|
117
|
+
p.add_argument("--density-k", type=int, default=20)
|
|
118
|
+
p.add_argument("--prefilter-quantile", type=float, default=0.45)
|
|
119
|
+
p.add_argument("--density-weight", type=float, default=1.0)
|
|
120
|
+
p.add_argument("--centrality-weight", type=float, default=0.45)
|
|
121
|
+
p.add_argument("--duplicate-radius", type=float, default=0.04)
|
|
122
|
+
p.add_argument("--energy-prefilter-quantile", type=float, default=1.0)
|
|
123
|
+
p.add_argument("--energy-weight", type=float, default=0.0)
|
|
124
|
+
p.add_argument("--no-show", action="store_true")
|
|
125
|
+
return p.parse_args()
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
REQUIRED_METRIC_COLUMNS = {
|
|
129
|
+
"molecule_id",
|
|
130
|
+
"n_rotatable_bonds",
|
|
131
|
+
"cov_r_pool",
|
|
132
|
+
"cov_r_final",
|
|
133
|
+
"cov_r_oracle",
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def load_metrics_csv(path: str) -> pd.DataFrame:
|
|
138
|
+
"""Load an auditable per-molecule export without reconstructing distributions."""
|
|
139
|
+
df = pd.read_csv(path)
|
|
140
|
+
missing = sorted(REQUIRED_METRIC_COLUMNS.difference(df.columns))
|
|
141
|
+
if missing:
|
|
142
|
+
raise ValueError(f"Metrics CSV is missing required columns: {missing}")
|
|
143
|
+
if len(df) != 1000:
|
|
144
|
+
raise ValueError(f"Expected the complete 1,000-molecule test set, found {len(df)} rows")
|
|
145
|
+
if df["molecule_id"].duplicated().any():
|
|
146
|
+
raise ValueError("Metrics CSV contains duplicated molecule_id values")
|
|
147
|
+
|
|
148
|
+
for column in ("cov_r_pool", "cov_r_final", "cov_r_oracle"):
|
|
149
|
+
values = pd.to_numeric(df[column], errors="coerce")
|
|
150
|
+
if values.isna().any() or ((values < 0) | (values > 100)).any():
|
|
151
|
+
raise ValueError(f"{column} contains missing or out-of-range values")
|
|
152
|
+
df[column] = values
|
|
153
|
+
n_rot = pd.to_numeric(df["n_rotatable_bonds"], errors="coerce")
|
|
154
|
+
if n_rot.isna().any() or (n_rot < 0).any():
|
|
155
|
+
raise ValueError("n_rotatable_bonds contains missing or negative values")
|
|
156
|
+
df["n_rotatable_bonds"] = n_rot.astype(int)
|
|
157
|
+
|
|
158
|
+
derived = {
|
|
159
|
+
"selection_loss_pp": df["cov_r_pool"] - df["cov_r_final"],
|
|
160
|
+
"oracle_gain_pp": df["cov_r_oracle"] - df["cov_r_final"],
|
|
161
|
+
"generation_unrecovered_pp": 100.0 - df["cov_r_pool"],
|
|
162
|
+
"pool_oracle_gap_pp": df["cov_r_pool"] - df["cov_r_oracle"],
|
|
163
|
+
}
|
|
164
|
+
for column, expected in derived.items():
|
|
165
|
+
if column in df.columns and not np.allclose(
|
|
166
|
+
pd.to_numeric(df[column], errors="coerce"), expected, atol=1e-6, equal_nan=False
|
|
167
|
+
):
|
|
168
|
+
raise ValueError(f"{column} is inconsistent with its source COV-R columns")
|
|
169
|
+
df[column] = expected
|
|
170
|
+
|
|
171
|
+
if (df["cov_r_oracle"] > df["cov_r_pool"] + 1e-6).any():
|
|
172
|
+
raise ValueError("Oracle COV-R exceeds candidate-pool COV-R for at least one molecule")
|
|
173
|
+
return df
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def seed_everything(seed: int) -> None:
|
|
177
|
+
random.seed(seed)
|
|
178
|
+
np.random.seed(seed)
|
|
179
|
+
torch.manual_seed(seed)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def coverage_from_rmsd(rmsd: torch.Tensor, threshold: float) -> float:
|
|
183
|
+
if rmsd.numel() == 0:
|
|
184
|
+
return float("nan")
|
|
185
|
+
return float((rmsd.min(dim=0).values <= float(threshold)).float().mean().item() * 100.0)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def greedy_coverage_oracle(rmsd: torch.Tensor, budget: int, threshold: float) -> torch.Tensor:
|
|
189
|
+
"""Greedily maximize newly covered reference conformers at fixed budget."""
|
|
190
|
+
n_candidates, n_refs = rmsd.shape
|
|
191
|
+
budget = min(max(1, int(budget)), n_candidates)
|
|
192
|
+
covered = torch.zeros(n_refs, dtype=torch.bool)
|
|
193
|
+
selected: list[int] = []
|
|
194
|
+
hit = rmsd <= float(threshold)
|
|
195
|
+
min_rmsd = rmsd.min(dim=1).values
|
|
196
|
+
for _ in range(budget):
|
|
197
|
+
gains = (hit & ~covered.unsqueeze(0)).sum(dim=1)
|
|
198
|
+
if selected:
|
|
199
|
+
gains[torch.tensor(selected, dtype=torch.long)] = -1
|
|
200
|
+
best_gain = int(gains.max().item())
|
|
201
|
+
if best_gain <= 0:
|
|
202
|
+
remaining = torch.argsort(min_rmsd)
|
|
203
|
+
for idx in remaining.tolist():
|
|
204
|
+
if idx not in selected:
|
|
205
|
+
selected.append(int(idx))
|
|
206
|
+
break
|
|
207
|
+
else:
|
|
208
|
+
candidates = torch.nonzero(gains == best_gain, as_tuple=False).view(-1)
|
|
209
|
+
# Deterministic tie-break: lower current best RMSD, then index.
|
|
210
|
+
idx = min(candidates.tolist(), key=lambda x: (float(min_rmsd[x]), int(x)))
|
|
211
|
+
selected.append(int(idx))
|
|
212
|
+
covered |= hit[selected[-1]]
|
|
213
|
+
return torch.tensor(selected, dtype=torch.long)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def select_current_pool(record: dict, args: argparse.Namespace) -> torch.Tensor:
|
|
217
|
+
pool = record["raw_pool"].float().cpu()
|
|
218
|
+
budget = int(record.get("target_count", 0))
|
|
219
|
+
if budget <= 0:
|
|
220
|
+
raise ValueError("raw-pool record has no positive target_count")
|
|
221
|
+
if pool.size(0) <= budget:
|
|
222
|
+
return pool
|
|
223
|
+
return select_precision2r_conformers(
|
|
224
|
+
pool,
|
|
225
|
+
target_count=budget,
|
|
226
|
+
mol=record.get("mol"),
|
|
227
|
+
heavy_mask=record.get("heavy_mask"),
|
|
228
|
+
metric=args.selector_metric,
|
|
229
|
+
density_k=args.density_k,
|
|
230
|
+
prefilter_quantile=args.prefilter_quantile,
|
|
231
|
+
density_weight=args.density_weight,
|
|
232
|
+
centrality_weight=args.centrality_weight,
|
|
233
|
+
duplicate_radius=args.duplicate_radius,
|
|
234
|
+
energy_prefilter_quantile=args.energy_prefilter_quantile,
|
|
235
|
+
energy_weight=args.energy_weight,
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def analyze(args: argparse.Namespace) -> pd.DataFrame:
|
|
240
|
+
payload = torch.load(args.raw_pool, map_location="cpu", weights_only=False)
|
|
241
|
+
records = payload.get("records", [])
|
|
242
|
+
if not records:
|
|
243
|
+
raise ValueError(f"No records found in {args.raw_pool}")
|
|
244
|
+
rows = []
|
|
245
|
+
too_small = 0
|
|
246
|
+
for record_no, record in enumerate(records):
|
|
247
|
+
pool = record["raw_pool"].float().cpu()
|
|
248
|
+
refs = record["ref_confs"].float().cpu()
|
|
249
|
+
heavy_mask = record.get("heavy_mask")
|
|
250
|
+
if torch.is_tensor(heavy_mask):
|
|
251
|
+
heavy_mask = heavy_mask.bool().cpu()
|
|
252
|
+
budget = int(record.get("target_count", 0))
|
|
253
|
+
if pool.size(0) <= budget:
|
|
254
|
+
too_small += 1
|
|
255
|
+
rmsd_pool = compute_rmsd_matrix(
|
|
256
|
+
pool, refs, heavy_mask=heavy_mask, mol=record.get("mol"),
|
|
257
|
+
backend=args.rmsd_backend, rdkit_max_matches=args.rdkit_max_matches,
|
|
258
|
+
)
|
|
259
|
+
final_pool = select_current_pool(record, args)
|
|
260
|
+
rmsd_final = compute_rmsd_matrix(
|
|
261
|
+
final_pool, refs, heavy_mask=heavy_mask, mol=record.get("mol"),
|
|
262
|
+
backend=args.rmsd_backend, rdkit_max_matches=args.rdkit_max_matches,
|
|
263
|
+
)
|
|
264
|
+
oracle_idx = greedy_coverage_oracle(rmsd_pool, budget, args.threshold)
|
|
265
|
+
rmsd_oracle = rmsd_pool[oracle_idx]
|
|
266
|
+
cov_pool = coverage_from_rmsd(rmsd_pool, args.threshold)
|
|
267
|
+
cov_final = coverage_from_rmsd(rmsd_final, args.threshold)
|
|
268
|
+
cov_oracle = coverage_from_rmsd(rmsd_oracle, args.threshold)
|
|
269
|
+
mol = record.get("mol")
|
|
270
|
+
n_rot = len(find_rotatable_bonds(mol)) if mol is not None else -1
|
|
271
|
+
rows.append({
|
|
272
|
+
"molecule_id": int(record.get("mol_id", record_no)),
|
|
273
|
+
"molecule_key": str(record.get("mol_key", record_no)),
|
|
274
|
+
"n_rotatable_bonds": int(n_rot),
|
|
275
|
+
"candidate_count": int(pool.size(0)),
|
|
276
|
+
"retained_budget": int(budget),
|
|
277
|
+
"n_reference_conformers": int(refs.size(0)),
|
|
278
|
+
"cov_r_pool": cov_pool,
|
|
279
|
+
"cov_r_final": cov_final,
|
|
280
|
+
"selection_loss_pp": cov_pool - cov_final,
|
|
281
|
+
"cov_r_oracle": cov_oracle,
|
|
282
|
+
"oracle_gain_pp": cov_oracle - cov_final,
|
|
283
|
+
"generation_unrecovered_pp": 100.0 - cov_pool,
|
|
284
|
+
})
|
|
285
|
+
if (record_no + 1) % 50 == 0:
|
|
286
|
+
print(f"[audit] {record_no + 1}/{len(records)} molecules")
|
|
287
|
+
if too_small:
|
|
288
|
+
print(f"[audit] WARNING: {too_small} records have candidate_count <= retained_budget")
|
|
289
|
+
df = pd.DataFrame(rows)
|
|
290
|
+
if len(df) < 1000:
|
|
291
|
+
print(f"[audit] WARNING: only {len(df)} molecules found in raw pool")
|
|
292
|
+
return df
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def bootstrap_ci(values: np.ndarray, repeats: int, seed: int, statistic=np.mean) -> tuple[float, float]:
|
|
296
|
+
values = np.asarray(values, dtype=float)
|
|
297
|
+
values = values[np.isfinite(values)]
|
|
298
|
+
if values.size == 0:
|
|
299
|
+
return float("nan"), float("nan")
|
|
300
|
+
rng = np.random.default_rng(seed)
|
|
301
|
+
sample_idx = rng.integers(0, values.size, size=(int(repeats), values.size))
|
|
302
|
+
estimates = statistic(values[sample_idx], axis=1)
|
|
303
|
+
return tuple(np.percentile(estimates, [2.5, 97.5]).tolist())
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def summarize(df: pd.DataFrame, args: argparse.Namespace, output_dir: Path) -> None:
|
|
307
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
308
|
+
df.to_csv(output_dir / "per_molecule_metrics.csv", index=False)
|
|
309
|
+
rows = []
|
|
310
|
+
for value_name, column in [
|
|
311
|
+
("candidate_pool_cov_r", "cov_r_pool"),
|
|
312
|
+
("final_library_cov_r", "cov_r_final"),
|
|
313
|
+
("selection_loss", "selection_loss_pp"),
|
|
314
|
+
("oracle_cov_r", "cov_r_oracle"),
|
|
315
|
+
("oracle_gain", "oracle_gain_pp"),
|
|
316
|
+
]:
|
|
317
|
+
values = df[column].to_numpy(float)
|
|
318
|
+
finite = values[np.isfinite(values)]
|
|
319
|
+
lo, hi = bootstrap_ci(finite, BOOTSTRAP_REPEATS, SEED + len(rows))
|
|
320
|
+
rows.append({
|
|
321
|
+
"metric": value_name,
|
|
322
|
+
"mean": float(np.mean(finite)),
|
|
323
|
+
"median": float(np.median(finite)),
|
|
324
|
+
"std": float(np.std(finite, ddof=1)),
|
|
325
|
+
"p25": float(np.percentile(finite, 25)),
|
|
326
|
+
"p75": float(np.percentile(finite, 75)),
|
|
327
|
+
"p90": float(np.percentile(finite, 90)),
|
|
328
|
+
"bootstrap_ci_low": lo,
|
|
329
|
+
"bootstrap_ci_high": hi,
|
|
330
|
+
"n": int(finite.size),
|
|
331
|
+
})
|
|
332
|
+
summary = pd.DataFrame(rows)
|
|
333
|
+
summary.to_csv(output_dir / "summary_metrics.csv", index=False)
|
|
334
|
+
df["loss_le_5pp"] = df.selection_loss_pp <= 5
|
|
335
|
+
df["loss_le_10pp"] = df.selection_loss_pp <= 10
|
|
336
|
+
df["loss_le_20pp"] = df.selection_loss_pp <= 20
|
|
337
|
+
df["loss_gt_20pp"] = df.selection_loss_pp > 20
|
|
338
|
+
loss = df.selection_loss_pp.to_numpy(float)
|
|
339
|
+
rho, pvalue = spearmanr(df.n_rotatable_bonds, loss, nan_policy="omit")
|
|
340
|
+
flexibility_rows = []
|
|
341
|
+
for n_rot, group in df.groupby("n_rotatable_bonds", sort=True):
|
|
342
|
+
values = group.selection_loss_pp.to_numpy(float)
|
|
343
|
+
lo, hi = (float("nan"), float("nan"))
|
|
344
|
+
if len(values) >= 5:
|
|
345
|
+
lo, hi = bootstrap_ci(values, BOOTSTRAP_REPEATS, SEED + int(n_rot) + 100)
|
|
346
|
+
flexibility_rows.append({
|
|
347
|
+
"n_rotatable_bonds": int(n_rot), "n_molecules": int(len(values)),
|
|
348
|
+
"median_selection_loss_pp": float(np.median(values)),
|
|
349
|
+
"mean_selection_loss_pp": float(np.mean(values)),
|
|
350
|
+
"bootstrap_ci_low": lo, "bootstrap_ci_high": hi,
|
|
351
|
+
})
|
|
352
|
+
flex = pd.DataFrame(flexibility_rows)
|
|
353
|
+
flex.to_csv(output_dir / "flexibility_summary.csv", index=False)
|
|
354
|
+
stats = {row.metric: row for row in summary.itertuples(index=False)}
|
|
355
|
+
text = [
|
|
356
|
+
"Figure 3 finite-budget selection-loss audit",
|
|
357
|
+
f"n_molecules={len(df)} threshold_A={args.threshold:.6g} seed={SEED}",
|
|
358
|
+
f"rmsd_backend={args.rmsd_backend} selector=precision2r",
|
|
359
|
+
f"selector_params=density_k:{args.density_k},prefilter:{args.prefilter_quantile},density_weight:{args.density_weight},centrality_weight:{args.centrality_weight},duplicate_radius:{args.duplicate_radius}",
|
|
360
|
+
f"selection_loss_mean_pp={stats['selection_loss'].mean:.6f}",
|
|
361
|
+
f"selection_loss_median_pp={stats['selection_loss'].median:.6f}",
|
|
362
|
+
f"selection_loss_iqr_pp={stats['selection_loss'].p25:.6f},{stats['selection_loss'].p75:.6f}",
|
|
363
|
+
f"selection_loss_p90_pp={stats['selection_loss'].p90:.6f}",
|
|
364
|
+
f"selection_loss_ci95_mean_pp={stats['selection_loss'].bootstrap_ci_low:.6f},{stats['selection_loss'].bootstrap_ci_high:.6f}",
|
|
365
|
+
f"fraction_loss_le_5pp={float(df.loss_le_5pp.mean()):.6f}",
|
|
366
|
+
f"fraction_loss_le_10pp={float(df.loss_le_10pp.mean()):.6f}",
|
|
367
|
+
f"fraction_loss_le_20pp={float(df.loss_le_20pp.mean()):.6f}",
|
|
368
|
+
f"fraction_loss_gt_20pp={float(df.loss_gt_20pp.mean()):.6f}",
|
|
369
|
+
f"flexibility_spearman_rho={float(rho):.6f} p={float(pvalue):.6g}",
|
|
370
|
+
f"oracle_gain_mean_pp={stats['oracle_gain'].mean:.6f}",
|
|
371
|
+
f"oracle_gain_median_pp={stats['oracle_gain'].median:.6f}",
|
|
372
|
+
f"pool_oracle_gap_mean_pp={stats['candidate_pool_cov_r'].mean - stats['oracle_cov_r'].mean:.6f}",
|
|
373
|
+
f"pool_oracle_gap_median_pp={stats['candidate_pool_cov_r'].median - stats['oracle_cov_r'].median:.6f}",
|
|
374
|
+
]
|
|
375
|
+
(output_dir / "figure3_results_summary.txt").write_text("\n".join(text) + "\n")
|
|
376
|
+
write_si_table(summary, flex, args, output_dir / "figure3_selection_loss_table.tex")
|
|
377
|
+
print("\n".join(text))
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def write_si_table(summary: pd.DataFrame, flex: pd.DataFrame, args: argparse.Namespace, path: Path) -> None:
|
|
381
|
+
lookup = {row.metric: row for row in summary.itertuples(index=False)}
|
|
382
|
+
lines = [
|
|
383
|
+
r"\begin{table*}[!htbp]",
|
|
384
|
+
r"\centering",
|
|
385
|
+
r"\caption{\textbf{Finite-budget selection-loss diagnostic on GEOM-Drugs.} Values are computed on the complete test partition at $\delta=" + f"{args.threshold:g}" + r"$~\AA{} using the same saved candidate pool, retained budget, heavy-atom symmetry-aware RMSD backend, and reference-free selector. The oracle is a post hoc reference-dependent greedy diagnostic and is not used during inference.}",
|
|
386
|
+
r"\label{tab:figure3-selection-loss}",
|
|
387
|
+
r"\small",
|
|
388
|
+
r"\begin{tabular}{lrrrrrr}",
|
|
389
|
+
r"\toprule",
|
|
390
|
+
r"Metric & Mean & Median & 25th pct. & 75th pct. & 90th pct. & 95\% bootstrap CI \\",
|
|
391
|
+
r"\midrule",
|
|
392
|
+
]
|
|
393
|
+
labels = [
|
|
394
|
+
("Candidate-pool COV-R", "candidate_pool_cov_r"),
|
|
395
|
+
("Final-library COV-R", "final_library_cov_r"),
|
|
396
|
+
("Selection loss (pp)", "selection_loss"),
|
|
397
|
+
("Oracle COV-R", "oracle_cov_r"),
|
|
398
|
+
("Oracle gain (pp)", "oracle_gain"),
|
|
399
|
+
]
|
|
400
|
+
for label, key in labels:
|
|
401
|
+
row = lookup[key]
|
|
402
|
+
lines.append(f"{label} & {row.mean:.2f} & {row.median:.2f} & {row.p25:.2f} & {row.p75:.2f} & {row.p90:.2f} & [{row.bootstrap_ci_low:.2f}, {row.bootstrap_ci_high:.2f}] \\")
|
|
403
|
+
lines += [r"\bottomrule", r"\end{tabular}", r"\end{table*}", ""]
|
|
404
|
+
lines += [r"\begin{table}[!htbp]", r"\centering", r"\caption{\textbf{Selection loss by rotatable-bond count.} Confidence intervals are molecule-level percentile bootstrap intervals with 10,000 repeats; groups with fewer than five molecules are reported without an interval.}", r"\label{tab:figure3-flexibility}", r"\small", r"\begin{tabular}{rrrrr}", r"\toprule", r"$n_{\mathrm{rot}}$ & $n$ & Median loss (pp) & Mean loss (pp) & 95\% CI \\", r"\midrule"]
|
|
405
|
+
for row in flex.itertuples(index=False):
|
|
406
|
+
ci = "--" if not np.isfinite(row.bootstrap_ci_low) else f"[{row.bootstrap_ci_low:.2f}, {row.bootstrap_ci_high:.2f}]"
|
|
407
|
+
lines.append(f"{int(row.n_rotatable_bonds)} & {int(row.n_molecules)} & {row.median_selection_loss_pp:.2f} & {row.mean_selection_loss_pp:.2f} & {ci} \\")
|
|
408
|
+
lines += [r"\bottomrule", r"\end{tabular}", r"\end{table}", ""]
|
|
409
|
+
path.write_text("\n".join(lines))
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def plot_figure(df: pd.DataFrame, args: argparse.Namespace, figure_dir: Path) -> None:
|
|
413
|
+
figure_dir.mkdir(parents=True, exist_ok=True)
|
|
414
|
+
fig, axes = plt.subplots(2, 2, figsize=(7.1, 4.9), constrained_layout=True)
|
|
415
|
+
ax = axes[0, 0]
|
|
416
|
+
ax.scatter(df.cov_r_pool, df.cov_r_final, s=10, alpha=0.35, color=COLORS["blue"], edgecolors="none", rasterized=True)
|
|
417
|
+
ax.plot([0, 100], [0, 100], "--", color=COLORS["gray"], lw=1.0)
|
|
418
|
+
ax.set(xlim=(0, 100), ylim=(0, 100), xlabel="Candidate-pool COV-R (%)", ylabel="Final-library COV-R (%)", title="a Candidate pool vs. retained library")
|
|
419
|
+
ax.text(0.04, 0.06, "Limited candidate-pool coverage", transform=ax.transAxes, fontsize=7, color=COLORS["gray"])
|
|
420
|
+
ax.text(0.54, 0.16, "Coverage available in pool\nbut not retained", transform=ax.transAxes, fontsize=7, color=COLORS["gray"])
|
|
421
|
+
|
|
422
|
+
ax = axes[0, 1]
|
|
423
|
+
losses = np.sort(df.selection_loss_pp.to_numpy(float))
|
|
424
|
+
ax.step(losses, np.arange(1, len(losses) + 1) / len(losses), where="post", color=COLORS["orange"], lw=2.0)
|
|
425
|
+
q25, med, q75, q90 = np.percentile(losses, [25, 50, 75, 90])
|
|
426
|
+
ax.text(0.97, 0.08, f"Median = {med:.2f} pp\nIQR = {q25:.2f}–{q75:.2f} pp\n90th percentile = {q90:.2f} pp", transform=ax.transAxes, ha="right", va="bottom", fontsize=7)
|
|
427
|
+
ax.set(xlabel="Selection loss ΔCOV-R (percentage points)", ylabel="Cumulative fraction of molecules", title="b Distribution of selection loss", ylim=(0, 1.02))
|
|
428
|
+
|
|
429
|
+
ax = axes[1, 0]
|
|
430
|
+
ax.scatter(df.n_rotatable_bonds, df.selection_loss_pp, s=5, alpha=0.15, color=COLORS["green"], edgecolors="none", rasterized=True)
|
|
431
|
+
flex = pd.read_csv(Path(args.output_dir) / "flexibility_summary.csv")
|
|
432
|
+
valid = flex[flex.n_molecules >= 5]
|
|
433
|
+
ax.fill_between(valid.n_rotatable_bonds, valid.bootstrap_ci_low, valid.bootstrap_ci_high, color=COLORS["green"], alpha=0.20, linewidth=0)
|
|
434
|
+
ax.plot(valid.n_rotatable_bonds, valid.median_selection_loss_pp, "o-", color=COLORS["green"], lw=1.8, ms=5)
|
|
435
|
+
rho, pvalue = spearmanr(df.n_rotatable_bonds, df.selection_loss_pp, nan_policy="omit")
|
|
436
|
+
ax.text(0.97, 0.96, f"Spearman ρ = {rho:.3f}\np = {pvalue:.3g}", transform=ax.transAxes, ha="right", va="top", fontsize=7)
|
|
437
|
+
ax.set(xlabel="Number of rotatable bonds", ylabel="Selection loss ΔCOV-R (percentage points)", title="c Selection loss vs. molecular flexibility")
|
|
438
|
+
|
|
439
|
+
ax = axes[1, 1]
|
|
440
|
+
values = [df.cov_r_pool, df.cov_r_final, df.cov_r_oracle]
|
|
441
|
+
labels = ["Candidate\npool", "Current\nselector", "Oracle\nselector"]
|
|
442
|
+
colors = [COLORS["blue"], COLORS["orange"], COLORS["purple"]]
|
|
443
|
+
bp = ax.boxplot([v.to_numpy(float) for v in values], positions=[1, 2, 3], widths=0.48, patch_artist=True, showfliers=False, medianprops={"color": "black", "lw": 1.2})
|
|
444
|
+
for patch, color in zip(bp["boxes"], colors):
|
|
445
|
+
patch.set_facecolor(color); patch.set_alpha(0.55); patch.set_edgecolor(color)
|
|
446
|
+
rng = np.random.default_rng(SEED)
|
|
447
|
+
sample = rng.choice(len(df), size=min(150, len(df)), replace=False)
|
|
448
|
+
for i in range(len(sample)):
|
|
449
|
+
ax.plot([2, 3], [df.cov_r_final.iloc[sample[i]], df.cov_r_oracle.iloc[sample[i]]], color=COLORS["light_gray"], lw=0.35, alpha=0.35, zorder=0)
|
|
450
|
+
ax.set_xticks([1, 2, 3], labels); ax.set_ylabel("COV-R (%)"); ax.set_title("d Current selector vs. reference-dependent oracle")
|
|
451
|
+
ax.set_ylim(0, 100)
|
|
452
|
+
ax.text(0.97, 0.06, f"Median pool = {df.cov_r_pool.median():.2f}\nMedian current = {df.cov_r_final.median():.2f}\nMedian oracle = {df.cov_r_oracle.median():.2f}\nOracle gain = {df.oracle_gain_pp.median():+.2f} pp", transform=ax.transAxes, ha="right", va="bottom", fontsize=7)
|
|
453
|
+
|
|
454
|
+
for axis in axes.flat:
|
|
455
|
+
axis.spines["top"].set_visible(False); axis.spines["right"].set_visible(False)
|
|
456
|
+
axis.tick_params(width=0.7, length=3)
|
|
457
|
+
for fmt in ("pdf", "svg"):
|
|
458
|
+
fig.savefig(figure_dir / f"figure3_selection_loss.{fmt}", bbox_inches="tight", pad_inches=0.03)
|
|
459
|
+
fig.savefig(figure_dir / "figure3_selection_loss.png", dpi=600, bbox_inches="tight", pad_inches=0.03)
|
|
460
|
+
plt.close(fig)
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def main() -> None:
|
|
464
|
+
args = parse_args()
|
|
465
|
+
seed_everything(SEED)
|
|
466
|
+
output_dir = Path(args.output_dir)
|
|
467
|
+
df = load_metrics_csv(args.metrics_csv) if args.metrics_csv else analyze(args)
|
|
468
|
+
summarize(df, args, output_dir)
|
|
469
|
+
plot_figure(df, args, Path(args.figure_dir))
|
|
470
|
+
print(f"Wrote Figure 3 outputs to {args.figure_dir}")
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
if __name__ == "__main__":
|
|
474
|
+
main()
|