dualmapref 0.2.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. args_parse.py +593 -0
  2. candidate_corrector.py +161 -0
  3. candidate_scorer.py +359 -0
  4. conformend/__init__.py +36 -0
  5. conformend/refiner.py +25 -0
  6. dataset/__init__.py +1 -0
  7. dataset/drugdataset.py +644 -0
  8. dataset/manifold.py +237 -0
  9. dataset/sharded_drugdataset.py +124 -0
  10. dataset/smiles2graph.py +217 -0
  11. dualmapref/__init__.py +24 -0
  12. dualmapref/refiner.py +16 -0
  13. dualmapref-0.2.8.dist-info/LICENSE +21 -0
  14. dualmapref-0.2.8.dist-info/METADATA +167 -0
  15. dualmapref-0.2.8.dist-info/RECORD +186 -0
  16. dualmapref-0.2.8.dist-info/WHEEL +5 -0
  17. dualmapref-0.2.8.dist-info/entry_points.txt +10 -0
  18. dualmapref-0.2.8.dist-info/top_level.txt +29 -0
  19. exputils.py +289 -0
  20. infer.py +23 -0
  21. infer_base_conformers.py +5589 -0
  22. infer_mean_dispersion_torsion.py +1117 -0
  23. infer_torsion.py +752 -0
  24. infer_torsional_denoiser.py +1021 -0
  25. manimol/__init__.py +30 -0
  26. manimol/__main__.py +6 -0
  27. manimol/cli.py +36 -0
  28. manimol/full_cli.py +41 -0
  29. manimol/legacy_imports.py +43 -0
  30. manimol/manifest.py +56 -0
  31. manimol/postprocess.py +148 -0
  32. manimol/priors.py +60 -0
  33. manimol/protocols.py +119 -0
  34. manimol/refiner.py +645 -0
  35. manimol/runtime.py +194 -0
  36. manimol/selection.py +85 -0
  37. manimol/torsion.py +22 -0
  38. manimol/train_cli.py +14 -0
  39. mixture_flow/src/__init__.py +1 -0
  40. mixture_flow/src/_path.py +7 -0
  41. mixture_flow/src/infer28_mixture_torsion_flow.py +1093 -0
  42. mixture_flow/src/mixture_torsion_flow.py +291 -0
  43. mixture_flow/src/summarize_mixture_debug.py +80 -0
  44. mixture_flow/src/train28_mixture_torsion_flow.py +583 -0
  45. mixture_torsion_flow.py +7 -0
  46. models/ manifold_learning.py +86 -0
  47. models/__init__.py +8 -0
  48. models/dist.py +273 -0
  49. models/dist2coords.py +23 -0
  50. models/egnn.py +107 -0
  51. models/experimental_backbones.py +364 -0
  52. models/gnnconv.py +512 -0
  53. models/kernel_inversion.py +141 -0
  54. models/kernels.py +427 -0
  55. models/losses.py +154 -0
  56. models/mean_dispersion_bridge.py +119 -0
  57. models/mean_dispersion_torsion_context.py +155 -0
  58. models/model.py +73 -0
  59. models/model0.py +2375 -0
  60. models/vis.py +128 -0
  61. pgraph_controls.py +107 -0
  62. scripts/__init__.py +2 -0
  63. scripts/analyze_basin_alignment.py +63 -0
  64. scripts/analyze_hif2a_conformer_aggregation.py +267 -0
  65. scripts/analyze_hif2a_contact_pharmacophore.py +1111 -0
  66. scripts/analyze_hif2a_fixed_budget.py +202 -0
  67. scripts/analyze_hif2a_pharmacophore.py +405 -0
  68. scripts/analyze_hif2a_pharmacophore_match_metric.py +386 -0
  69. scripts/analyze_p_control_candidate_flow.py +154 -0
  70. scripts/analyze_scaffold_basin.py +83 -0
  71. scripts/audit_dispersion_proposals.py +208 -0
  72. scripts/audit_etflow_protocol.py +258 -0
  73. scripts/audit_external_overlap.py +150 -0
  74. scripts/audit_oldbest_fulltrainval_split.py +82 -0
  75. scripts/audit_pdbbind_contact_pharmacophores.py +146 -0
  76. scripts/audit_qm9_geomol_test_split.py +94 -0
  77. scripts/audit_stage1_stage2_cross.py +131 -0
  78. scripts/build_geom_drugs_lowref_diagnostic.py +84 -0
  79. scripts/build_qm9_backfill_manifest.py +99 -0
  80. scripts/check_etflow_runtime.py +56 -0
  81. scripts/compare_prior_sources.py +122 -0
  82. scripts/compose_binding_mode_figure.py +87 -0
  83. scripts/compose_hif2a_contact_pharmacophore_suppfig.py +255 -0
  84. scripts/compose_hif2a_functional_geometry_pipeline.py +266 -0
  85. scripts/compose_manimol_internal_architecture.py +291 -0
  86. scripts/diagnose_flow_basin_intervention.py +54 -0
  87. scripts/diagnose_hif2a_pharmacophore_budget.py +185 -0
  88. scripts/diagnose_route2_p_ranking.py +123 -0
  89. scripts/discover_binding_mode_inputs.py +30 -0
  90. scripts/evaluate_functional_prior_stage1.py +230 -0
  91. scripts/evaluate_geomanimol.py +74 -0
  92. scripts/evaluate_geomanimol_flow.py +23 -0
  93. scripts/evaluate_geomanimol_multiscaffold.py +46 -0
  94. scripts/evaluate_geomanimol_residual.py +45 -0
  95. scripts/evaluate_geomanimol_torsion.py +40 -0
  96. scripts/evaluate_hif2a_bioactive_enrichment.py +91 -0
  97. scripts/evaluate_hif2a_pms_suite.py +440 -0
  98. scripts/evaluate_pdbbind_contact_pms.py +155 -0
  99. scripts/evaluate_platinum_external.py +114 -0
  100. scripts/evaluate_platinum_pool.py +82 -0
  101. scripts/evaluate_route2_calibrated_p.py +104 -0
  102. scripts/evaluate_route2_library_components.py +596 -0
  103. scripts/evaluate_route2_p_basin_budget.py +288 -0
  104. scripts/evaluate_stage1_checkpoint_only.py +30 -0
  105. scripts/export_dispersion_bond_audit.py +136 -0
  106. scripts/export_top20_shards.py +207 -0
  107. scripts/external_validation_metrics.py +152 -0
  108. scripts/fit_topology_prior.py +117 -0
  109. scripts/frozen_dispersion_audit.py +281 -0
  110. scripts/geomanimol_debug.py +50 -0
  111. scripts/geomanimol_smoke.py +37 -0
  112. scripts/infer_etflow_matched.py +524 -0
  113. scripts/inspect_checkpoint.py +12 -0
  114. scripts/inspect_conformer_capacity.py +71 -0
  115. scripts/inspect_pool.py +12 -0
  116. scripts/make_figure3_selection_loss.py +474 -0
  117. scripts/make_figure3_selection_summary.py +275 -0
  118. scripts/make_geom_debug_subset.py +62 -0
  119. scripts/make_low_reference_test_subset.py +70 -0
  120. scripts/merge_stage1_into_stage2.py +82 -0
  121. scripts/place_hif2a_candidates_for_evaluation.py +66 -0
  122. scripts/plot_hif2a_low_budget_efficiency.py +100 -0
  123. scripts/plot_prior_validation.py +48 -0
  124. scripts/precompute_stage2_assignments.py +90 -0
  125. scripts/prepare_bace_activity.py +98 -0
  126. scripts/prepare_bace_etkdg_benchmark.py +90 -0
  127. scripts/prepare_figure5_4x4_cases.py +470 -0
  128. scripts/prepare_geom_drugs_geomol_top20.py +472 -0
  129. scripts/prepare_pdbbind_core_raw.py +90 -0
  130. scripts/prepare_platinum_raw.py +74 -0
  131. scripts/prepare_pubchem_etkdg.py +54 -0
  132. scripts/rebuild_hif2a_final_sdf_and_metrics.py +55 -0
  133. scripts/rebuild_hif2a_fixed_pool_sweep.py +175 -0
  134. scripts/rebuild_hif2a_strict.py +220 -0
  135. scripts/recompute_hif2a_bootstrap.py +63 -0
  136. scripts/reference_free_geometry.py +112 -0
  137. scripts/render_binding_modes.py +413 -0
  138. scripts/render_figure5_4x4.py +330 -0
  139. scripts/render_figure6_pharmacophore_pymol.py +162 -0
  140. scripts/render_hif2a_contact_pharmacophore_pymol.py +414 -0
  141. scripts/render_hif2a_figure11.py +397 -0
  142. scripts/run_bace_2d_baseline.py +73 -0
  143. scripts/run_drugs_validation_matrix.py +225 -0
  144. scripts/run_external_reference_free.py +53 -0
  145. scripts/run_manimol_finalk_sensitivity.py +106 -0
  146. scripts/run_manimol_oversampling_sensitivity.py +109 -0
  147. scripts/run_pdisp_matched_intervention.py +116 -0
  148. scripts/run_stage1_stage2_cross20.py +83 -0
  149. scripts/score_hif2a_candidates_with_pgraph.py +221 -0
  150. scripts/summarize_dispersion_intervention.py +88 -0
  151. scripts/test_mean_dispersion_bridge.py +50 -0
  152. scripts/train_dispersion_head_diagnostic.py +320 -0
  153. scripts/train_dispersion_head_streaming.py +519 -0
  154. scripts/train_geomanimol.py +100 -0
  155. scripts/train_geomanimol_flow.py +26 -0
  156. scripts/train_geomanimol_residual.py +70 -0
  157. scripts/train_geomanimol_torsion.py +50 -0
  158. scripts/train_quantile_representation_diagnostic.py +204 -0
  159. scripts/validate_mean_dispersion_prior.py +173 -0
  160. scripts/validate_packed_shard.py +41 -0
  161. scripts/validate_stage1_prior.py +221 -0
  162. scripts/validate_top20_shards.py +36 -0
  163. scripts/validation/__init__.py +1 -0
  164. scripts/validation/hif2a_downstream_baselines.py +105 -0
  165. scripts/validation/hif2a_pairwise_ranker.py +218 -0
  166. scripts/verify_binding_mode_figure.py +47 -0
  167. torsion_manifold.py +281 -0
  168. train.py +78 -0
  169. train_graph_encoder.py +206 -0
  170. train_local_torsion_corrector.py +511 -0
  171. train_mean_dispersion_torsion_prior.py +329 -0
  172. train_probability_basin.py +235 -0
  173. train_torsion_generator.py +1631 -0
  174. train_torsion_manifold_prior.py +258 -0
  175. train_torsional_denoiser.py +415 -0
  176. trainer.py +1038 -0
  177. utils/__init__.py +7 -0
  178. utils/checkpoint.py +40 -0
  179. utils/device.py +17 -0
  180. utils/helpers.py +98 -0
  181. utils/kabsch.py +29 -0
  182. utils/lookup_table.py +164 -0
  183. utils/metrics.py +50 -0
  184. utils/optuna.py +111 -0
  185. utils/save_mol.py +366 -0
  186. utils/util.py +36 -0
dataset/manifold.py ADDED
@@ -0,0 +1,237 @@
1
+ import numpy as np
2
+ from typing import Optional, Dict, Any
3
+ from models import (
4
+ compute_AE_tanimoto_distance_np,
5
+ compute_augmented_graph_distance_np,
6
+ compute_embed3d_distance_np,
7
+ hop_matrix_from_mol,
8
+ )
9
+
10
+
11
+ def _extract_distance_matrix(dist_used, prefer_key: Optional[str] = "D_aug"):
12
+ """从可能的返回类型(np.ndarray / dict / list-like)中提取方阵距离矩阵。
13
+
14
+ 优先查找 prefer_key,然后查找常见键名,再回退到第一个能识别为方阵的 array-like 值。
15
+ 如果无法提取,将抛出 ValueError。
16
+ """
17
+ if isinstance(dist_used, dict):
18
+ # 优先取 prefer_key
19
+ if prefer_key is not None and prefer_key in dist_used:
20
+ return np.asarray(dist_used[prefer_key], dtype=float)
21
+
22
+ # 常见候选键
23
+ for key in ("D_aug", "D_topo", "D_edge", "D_feat", "dist", "matrix", "D"):
24
+ if key in dist_used:
25
+ return np.asarray(dist_used[key], dtype=float)
26
+
27
+ # 回退:第一个能解析成方阵的 value
28
+ for v in dist_used.values():
29
+ try:
30
+ arr = np.asarray(v, dtype=float)
31
+ except Exception:
32
+ continue
33
+ if arr.ndim == 2 and arr.shape[0] == arr.shape[1]:
34
+ return arr
35
+
36
+ raise ValueError(
37
+ "Cannot extract a square distance matrix from dict. Available keys: %s"
38
+ % (list(dist_used.keys()),)
39
+ )
40
+ else:
41
+ arr = np.asarray(dist_used, dtype=float)
42
+ if arr.ndim != 2 or arr.shape[0] != arr.shape[1]:
43
+ raise ValueError("dist must be a square (n x n) array, got shape: %s" % (arr.shape,))
44
+ return arr
45
+
46
+
47
+ def build_high_dim_probabilities(
48
+ mol=None,
49
+ dist: Optional[np.ndarray] = None,
50
+ dist_name: str = "D2",
51
+ compute_D_params: Optional[Dict[str, Any]] = None,
52
+ hop: Optional[np.ndarray] = None,
53
+ K_HOP_MAX: int = 2,
54
+ k_min: int = 3,
55
+ k_max: int = 8,
56
+ default_k_when_no_deg: float = 6.0,
57
+ sigma_search_iters: int = 32,
58
+ sigma_hi_init: float = 1.0,
59
+ verbose: bool = False,
60
+ data=None,
61
+ ):
62
+ """
63
+ 构造高维概率图(返回对称化的 P 矩阵以及中间产物)。
64
+
65
+ 返回 dict:
66
+ - "P": 对称化的概率矩阵 (n x n)
67
+ - "prob": 非对称未归一化的 prob 矩阵
68
+ - "sigma": 每行二分搜索得到的 sigma
69
+ - "dist": 使用的距离矩阵 (n x n)
70
+ - "hop": 使用的 hop 矩阵或 None
71
+ """
72
+
73
+ params = compute_D_params or {}
74
+
75
+ # ---------- 1. 准备距离矩阵 ----------
76
+ if dist is None:
77
+ if mol is None:
78
+ raise ValueError("必须提供 dist 矩阵,或提供 mol 以便根据 dist_name 计算。")
79
+
80
+ if verbose:
81
+ print(f"[build_high_dim_probabilities] computing distance {dist_name} ...")
82
+
83
+ if dist_name == "D1":
84
+ raw = compute_augmented_graph_distance_np(mol=mol, data=data, **params)
85
+ dist_mat = _extract_distance_matrix(raw, prefer_key="D_aug")
86
+ elif dist_name == "D2":
87
+ raw = compute_AE_tanimoto_distance_np(mol=mol, **params)
88
+ # AE / tanimoto 函数通常返回矩阵,但也可能返回 dict
89
+ dist_mat = _extract_distance_matrix(raw, prefer_key=None)
90
+ elif dist_name == "D3":
91
+ raw = compute_embed3d_distance_np(mol=mol, **params)
92
+ dist_mat = _extract_distance_matrix(raw, prefer_key=None)
93
+ else:
94
+ raise ValueError(f"未知 dist_name: {dist_name}")
95
+ else:
96
+ dist_mat = np.asarray(dist, dtype=float)
97
+ if dist_mat.ndim != 2 or dist_mat.shape[0] != dist_mat.shape[1]:
98
+ raise ValueError("传入的 dist 必须是方阵 (n x n). got: %s" % (dist_mat.shape,))
99
+
100
+ n = int(dist_mat.shape[0])
101
+
102
+ if verbose:
103
+ print(f"[build_high_dim_probabilities] distance matrix shape: {dist_mat.shape}")
104
+
105
+ # ---------- 2. 准备 hop 矩阵 ----------
106
+ hop_used = hop
107
+ if hop_used is None and mol is not None:
108
+ try:
109
+ hop_used = hop_matrix_from_mol(mol)
110
+ except Exception as e:
111
+ if verbose:
112
+ print(f"[build_high_dim_probabilities] hop_matrix_from_mol failed: {e}. Ignoring hop.")
113
+ hop_used = None
114
+
115
+ if hop_used is not None:
116
+ hop_used = np.asarray(hop_used, dtype=float)
117
+ if hop_used.shape != (n, n):
118
+ if verbose:
119
+ print("[build_high_dim_probabilities] Warning: hop 矩阵形状与 dist 不匹配,忽略 hop 筛选。")
120
+ hop_used = None
121
+
122
+ # ---------- 3. 计算每个节点目标 k_i ----------
123
+ try:
124
+ if mol is not None:
125
+ deg = np.array([a.GetDegree() for a in mol.GetAtoms()], dtype=int)
126
+ if deg.shape[0] != n:
127
+ # 不匹配则忽略 deg
128
+ deg = None
129
+ else:
130
+ deg = None
131
+
132
+ if deg is not None:
133
+ k_i = np.clip(deg + 2, k_min, k_max).astype(float)
134
+ else:
135
+ k_i = np.full(n, default_k_when_no_deg, dtype=float)
136
+ except Exception:
137
+ k_i = np.full(n, default_k_when_no_deg, dtype=float)
138
+
139
+ # ---------- 4. 辅助函数 ----------
140
+ def prob_row_from_sigma(sigma, d_row, rho_i):
141
+ x = d_row - rho_i
142
+ x = np.where(x < 0.0, 0.0, x)
143
+ return np.exp(- x / (sigma + 1e-12))
144
+
145
+ def k_from_prob(prob_row):
146
+ s = float(prob_row.sum())
147
+ return np.power(2.0, s) - 1.0
148
+
149
+ def sigma_binary_search_row(k_of_sigma, fixed_k, lo=1e-6, hi=sigma_hi_init, iters=sigma_search_iters):
150
+ # expand hi until >= fixed_k (robust策略)
151
+ try:
152
+ while k_of_sigma(hi) < fixed_k:
153
+ hi *= 2.0
154
+ if hi > 1e6:
155
+ break
156
+ except Exception:
157
+ hi = sigma_hi_init
158
+
159
+ for _ in range(iters):
160
+ mid = 0.5 * (lo + hi)
161
+ try:
162
+ if k_of_sigma(mid) < fixed_k:
163
+ lo = mid
164
+ else:
165
+ hi = mid
166
+ except Exception:
167
+ lo = mid
168
+ hi *= 2.0
169
+ return 0.5 * (lo + hi)
170
+
171
+ # ---------- 5. 主循环 ----------
172
+ prob = np.zeros((n, n), dtype=float)
173
+ sigma_array = np.zeros(n, dtype=float)
174
+
175
+ for i in range(n):
176
+ # 全量可选集合:非自身且距离有限
177
+ finite_mask = np.isfinite(dist_mat[i])
178
+ cand = np.where((np.arange(n) != i) & finite_mask)[0]
179
+
180
+ # 若有 hop 限制,则再筛一次
181
+ if hop_used is not None:
182
+ hop_mask = hop_used[i] <= K_HOP_MAX
183
+ cand = cand[hop_mask[cand]]
184
+
185
+ if cand.size == 0:
186
+ sigma_array[i] = 0.0
187
+ if verbose and ((i + 1) % 200 == 0):
188
+ print(f"Row {i}: no candidates (all inf or excluded by hop)")
189
+ continue
190
+
191
+ if cand.size <= 2:
192
+ prob[i, cand] = 1.0 / float(cand.size)
193
+ sigma_array[i] = 0.0
194
+ continue
195
+
196
+ d_i = dist_mat[i, cand].astype(float)
197
+
198
+ pos = d_i[d_i > 0.0]
199
+ rho_i = float(pos.min()) if pos.size > 0 else 0.0
200
+
201
+ k_target = float(min(k_i[i], cand.size))
202
+
203
+ def k_of_sigma(sig):
204
+ p = prob_row_from_sigma(sig, d_i, rho_i)
205
+ return k_from_prob(p)
206
+
207
+ sigma_i = sigma_binary_search_row(k_of_sigma, k_target, lo=1e-6, hi=sigma_hi_init, iters=sigma_search_iters)
208
+ sigma_array[i] = sigma_i
209
+
210
+ p_row = prob_row_from_sigma(sigma_i, d_i, rho_i)
211
+ prob[i, cand] = p_row
212
+
213
+ if verbose and ((i + 1) % 100 == 0 or i == n - 1):
214
+ print(f"Sigma search row {i+1}/{n}")
215
+
216
+ if verbose:
217
+ print("Mean sigma =", float(sigma_array.mean()))
218
+
219
+ # ---------- 6. 对称化(fuzzy union) ----------
220
+ P = prob + prob.T - prob * prob.T
221
+
222
+ return {
223
+ "P": P,
224
+ # "prob": prob,
225
+ # "sigma": sigma_array,
226
+ # "dist": dist_mat,
227
+ # "hop": hop_used,
228
+ }
229
+
230
+
231
+ # ---------------- 简要示例 ----------------
232
+ # result = build_high_dim_probabilities(mol=mol, dist_name="D2", compute_D_params={'radius':2,'nBits':2048})
233
+ # P = result['P']
234
+
235
+
236
+
237
+
@@ -0,0 +1,124 @@
1
+ """Lazy, shard-backed reader for the isolated full top20 experiments.
2
+
3
+ The legacy InMemoryDataset remains the default. This reader only loads the
4
+ small shard containing a requested sample and never materializes RDKit objects.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import json
9
+ import os
10
+ from bisect import bisect_right
11
+
12
+ import torch
13
+ from torch_geometric.data import Data
14
+ from torch.utils.data import Subset
15
+
16
+
17
+ class ShardedDrugDataset:
18
+ def __init__(self, root, split="train", cache_size=2, assignment_root=""):
19
+ self.root = os.fspath(root)
20
+ with open(os.path.join(self.root, "manifest.json"), "r") as handle:
21
+ self.manifest = json.load(handle)
22
+ if split not in self.manifest["splits"]:
23
+ raise ValueError("Unknown shard split: %s" % split)
24
+ info = self.manifest["splits"][split]
25
+ self.split = split
26
+ self.shards = info["shards"]
27
+ self.offsets = [int(item["start"]) for item in self.shards]
28
+ self.length = int(info["samples"])
29
+ self.molecules = int(info["molecules"])
30
+ self._cache_size = max(1, int(cache_size))
31
+ self._cache = {}
32
+ self._cache_order = []
33
+ self._packed = self.manifest.get("format") == "manimol-shards-v3-packed"
34
+ self._assignments = {}
35
+ if assignment_root:
36
+ assignment_path = os.path.join(os.fspath(assignment_root), "%s.pt" % split)
37
+ if os.path.exists(assignment_path):
38
+ assignment_payload = torch.load(
39
+ assignment_path, map_location="cpu", weights_only=False
40
+ )
41
+ source_identity = assignment_payload.get("source_identity", "")
42
+ expected_identity = self.manifest.get("identity", {}).get("sha256", "")
43
+ if source_identity and expected_identity and source_identity != expected_identity:
44
+ raise ValueError("Stage-II assignment cache does not match shard identity")
45
+ self._assignments = assignment_payload.get("molecules", {})
46
+ self.molecule_groups_by_shard = []
47
+ self._group_ends_by_shard = []
48
+ for shard in self.shards:
49
+ cursor = int(shard["start"])
50
+ groups, ends, local_total = [], [], 0
51
+ for size in shard.get("group_sizes", []):
52
+ size = int(size)
53
+ groups.append(range(cursor, cursor + size))
54
+ cursor += size
55
+ local_total += size
56
+ ends.append(local_total)
57
+ if groups:
58
+ if cursor != int(shard["start"]) + int(shard["samples"]):
59
+ raise ValueError("Invalid molecule group metadata in %s" % shard["file"])
60
+ self.molecule_groups_by_shard.append(groups)
61
+ self._group_ends_by_shard.append(ends)
62
+ if len(self.molecule_groups_by_shard) != len(self.shards):
63
+ self.molecule_groups_by_shard = []
64
+ self._group_ends_by_shard = []
65
+
66
+ def __len__(self):
67
+ return self.length
68
+
69
+ def _load_shard(self, shard_id):
70
+ shard_id = int(shard_id)
71
+ if shard_id in self._cache:
72
+ return self._cache[shard_id]
73
+ path = os.path.join(self.root, self.shards[shard_id]["file"])
74
+ payload = torch.load(path, map_location="cpu", weights_only=False)
75
+ records = payload["molecules"] if self._packed else payload["records"]
76
+ self._cache[shard_id] = records
77
+ self._cache_order.append(shard_id)
78
+ while len(self._cache_order) > self._cache_size:
79
+ old = self._cache_order.pop(0)
80
+ self._cache.pop(old, None)
81
+ return records
82
+
83
+ def __getitem__(self, index):
84
+ if isinstance(index, (list, tuple, torch.Tensor)):
85
+ if isinstance(index, torch.Tensor):
86
+ index = index.tolist()
87
+ return Subset(self, list(index))
88
+ index = int(index)
89
+ if index < 0:
90
+ index += self.length
91
+ if index < 0 or index >= self.length:
92
+ raise IndexError(index)
93
+ shard_id = bisect_right(self.offsets, index) - 1
94
+ local_index = index - self.offsets[shard_id]
95
+ records = self._load_shard(shard_id)
96
+ if not self._packed:
97
+ return records[local_index]
98
+ ends = self._group_ends_by_shard[shard_id]
99
+ molecule_index = bisect_right(ends, local_index)
100
+ start = 0 if molecule_index == 0 else ends[molecule_index - 1]
101
+ conformer_index = local_index - start
102
+ molecule = records[molecule_index]
103
+ fields = {key: value for key, value in molecule.items()
104
+ if key not in ("pos", "conf_id")}
105
+ fields["pos"] = molecule["pos"][conformer_index]
106
+ fields["conf_id"] = molecule["conf_id"][conformer_index]
107
+ mol_id = int(molecule["mol_id"].item())
108
+ assignment = self._assignments.get(mol_id)
109
+ if assignment is not None:
110
+ expected_conf = int(assignment["conf_id"][conformer_index].item())
111
+ actual_conf = int(fields["conf_id"].item())
112
+ if expected_conf != actual_conf:
113
+ raise ValueError(
114
+ "Assignment conf_id mismatch for molecule %d: %d != %d"
115
+ % (mol_id, expected_conf, actual_conf)
116
+ )
117
+ fields["stage2_cluster_id"] = assignment["cluster_id"][conformer_index]
118
+ fields["stage2_cluster_count"] = torch.tensor(
119
+ int(assignment["cluster_count"]), dtype=torch.long
120
+ )
121
+ return Data(**fields)
122
+
123
+ def split_indices(self):
124
+ return list(range(self.length))
@@ -0,0 +1,217 @@
1
+ # import dgl
2
+ import numpy as np
3
+ import rdkit
4
+ import torch
5
+ from rdkit import Chem
6
+ import os
7
+ from rdkit.Chem import AllChem
8
+ from models import (
9
+ KERNEL, UMAPLowKernel, GaussianKernel, StudentTKernel, pairwise_dist,
10
+ UMAPRowExpKernel, UMAPRowFamilyKernel, SmoothKRowExpKernel, find_ab_params,
11
+ )
12
+ from models import KERNEL, pairwise_dist
13
+
14
+
15
+ def get_atom_features(atom):
16
+ # The usage of features is along with the Attentive FP.
17
+ feature = np.zeros(39)
18
+
19
+ # Symbol
20
+ symbol = atom.GetSymbol()
21
+ symbol_list = ['H', 'B', 'C', 'N', 'O', 'F', 'Si', 'P', 'S', 'Cl', 'As', 'Se', 'Br', 'I', 'At']
22
+ if symbol in symbol_list:
23
+ loc = symbol_list.index(symbol)
24
+ feature[loc] = 1
25
+ else:
26
+ feature[15] = 1
27
+
28
+ # Degree
29
+ degree = atom.GetDegree()
30
+ if degree > 5:
31
+ print("atom degree larger than 5. Please check before featurizing.")
32
+ raise RuntimeError
33
+
34
+ feature[16 + degree] = 1
35
+
36
+ # Formal Charge
37
+ charge = atom.GetFormalCharge()
38
+ feature[22] = charge
39
+
40
+ # radical electrons
41
+ radelc = atom.GetNumRadicalElectrons()
42
+ feature[23] = radelc
43
+
44
+ # Hybridization
45
+ hyb = atom.GetHybridization()
46
+ hybridization_list = [rdkit.Chem.rdchem.HybridizationType.SP,
47
+ rdkit.Chem.rdchem.HybridizationType.SP2,
48
+ rdkit.Chem.rdchem.HybridizationType.SP3,
49
+ rdkit.Chem.rdchem.HybridizationType.SP3D,
50
+ rdkit.Chem.rdchem.HybridizationType.SP3D2]
51
+ if hyb in hybridization_list:
52
+ loc = hybridization_list.index(hyb)
53
+ feature[loc + 24] = 1
54
+ else:
55
+ feature[29] = 1
56
+
57
+ # aromaticity
58
+ if atom.GetIsAromatic():
59
+ feature[30] = 1
60
+
61
+ # hydrogens
62
+ hs = atom.GetNumImplicitHs()
63
+ feature[31 + hs] = 1
64
+
65
+ # chirality, chirality type
66
+ if atom.HasProp('_ChiralityPossible'):
67
+ # TODO what kind of error
68
+ feature[36] = 1
69
+
70
+ try:
71
+ chi = atom.GetProp('_CIPCode')
72
+ chi_list = ['R', 'S']
73
+ loc = chi_list.index(chi)
74
+ feature[37 + loc] = 1
75
+ except KeyError:
76
+ feature[37] = 0
77
+ feature[38] = 0
78
+
79
+ return feature
80
+
81
+
82
+ def get_bond_features(bond):
83
+ feature = np.zeros(10)
84
+
85
+ # bond type
86
+ type = bond.GetBondType()
87
+ bond_type_list = [rdkit.Chem.rdchem.BondType.SINGLE,
88
+ rdkit.Chem.rdchem.BondType.DOUBLE,
89
+ rdkit.Chem.rdchem.BondType.TRIPLE,
90
+ rdkit.Chem.rdchem.BondType.AROMATIC]
91
+ if type in bond_type_list:
92
+ loc = bond_type_list.index(type)
93
+ feature[0 + loc] = 1
94
+ else:
95
+ print("Wrong type of bond. Please check before feturization.")
96
+ raise RuntimeError
97
+
98
+ # conjugation
99
+ conj = bond.GetIsConjugated()
100
+ feature[4] = conj
101
+
102
+ # ring
103
+ ring = bond.IsInRing()
104
+ feature[5] = ring
105
+
106
+ # stereo
107
+ stereo = bond.GetStereo()
108
+ stereo_list = [rdkit.Chem.rdchem.BondStereo.STEREONONE,
109
+ rdkit.Chem.rdchem.BondStereo.STEREOANY,
110
+ rdkit.Chem.rdchem.BondStereo.STEREOZ,
111
+ rdkit.Chem.rdchem.BondStereo.STEREOE]
112
+ if stereo in stereo_list:
113
+ loc = stereo_list.index(stereo)
114
+ feature[6 + loc] = 1
115
+ else:
116
+ print("Wrong stereo type of bond. Please check before featurization.")
117
+ raise RuntimeError
118
+
119
+ return feature
120
+
121
+
122
+ def get_atom_vdw_radii(mol):
123
+ """计算每个原子的范德华半径"""
124
+ periodic_table = rdkit.Chem.GetPeriodicTable()
125
+ vdw_radii = []
126
+ for atom in mol.GetAtoms():
127
+ atomic_num = atom.GetAtomicNum()
128
+ # 获取范德华半径 (单位: Angstrom)
129
+ radius = periodic_table.GetRvdw(atomic_num)
130
+ vdw_radii.append(radius)
131
+ return torch.tensor(vdw_radii, dtype=torch.float).view(-1, 1)
132
+
133
+
134
+ def smile2graph4GEOM(data):
135
+ mol = data['rdmol']
136
+ # if (mol is None):
137
+ # return None
138
+ src = []
139
+ dst = []
140
+ atom_feature = []
141
+ bond_feature = []
142
+
143
+ for atom in mol.GetAtoms():
144
+ one_atom_feature = get_atom_features(atom)
145
+ atom_feature.append(one_atom_feature)
146
+ atom_feature = np.array(atom_feature)
147
+ atom_feature = torch.tensor(atom_feature).float()
148
+ vdw_radii = get_atom_vdw_radii(mol)
149
+ if len(mol.GetBonds()) > 0: # mol has bonds
150
+ for bond in mol.GetBonds():
151
+ i = bond.GetBeginAtomIdx()
152
+ j = bond.GetEndAtomIdx()
153
+ one_bond_feature = get_bond_features(bond)
154
+ src.append(i)
155
+ dst.append(j)
156
+ bond_feature.append(one_bond_feature)
157
+ src.append(j)
158
+ dst.append(i)
159
+ bond_feature.append(one_bond_feature)
160
+
161
+ src = torch.tensor(src).long()
162
+ dst = torch.tensor(dst).long()
163
+ bond_feature = np.array(bond_feature)
164
+ bond_feature = torch.tensor(bond_feature).float()
165
+ edge_index = torch.vstack([src, dst])
166
+ # graph_cur_smile = dgl.graph((src, dst), num_nodes=len(mol.GetAtoms()))
167
+ # graph_cur_smile.ndata['x'] = atom_feature
168
+ # graph_cur_smile.edata['x'] = bond_feature
169
+ else:
170
+ edge_index = torch.empty((2, 0)).long()
171
+ bond_feature = torch.empty((0, 10)).float()
172
+
173
+ return atom_feature, edge_index, bond_feature, vdw_radii
174
+
175
+
176
+ def teacher_coords_from_smiles(mol, seed=42, optimize=True):
177
+ """
178
+ 用 RDKit ETKDG 生成 3D 坐标(仅重原子,保持与你可视化的原子顺序一致)。
179
+ 如果优化=True,做一次 UFF 优化以稳定键长。
180
+ 失败时返回 (None, None)。
181
+ """
182
+ mol_copy = Chem.Mol(mol) # 避免修改原对象
183
+ # 尝试多次嵌入(有时随机种子影响成功)
184
+ for attempt in range(3):
185
+ try:
186
+ params = AllChem.ETKDGv3()
187
+ params.randomSeed = seed + attempt
188
+ params.useSmallRingTorsions = True
189
+ ok = AllChem.EmbedMolecule(mol_copy, params)
190
+ if ok == 0:
191
+ if optimize:
192
+ try:
193
+ AllChem.MMFFOptimizeMolecule(mol_copy, maxIters=1000)
194
+ except Exception:
195
+ pass
196
+ conf = mol_copy.GetConformer(0)
197
+ idx_map = [conf.GetAtomPosition(a.GetIdx()) for a in mol_copy.GetAtoms()]
198
+ Y_true = np.array([[p.x, p.y, p.z] for p in idx_map], dtype=float)
199
+ return mol_copy, Y_true
200
+ except Exception:
201
+ continue
202
+ return None, None
203
+
204
+
205
+ def center_and_rescale(Y, target_rms=1.0):
206
+ """每个 epoch:居中并把坐标 RMS 缩放到 target_rms,防止数值漂移或坍缩。"""
207
+ Y = Y - Y.mean(axis=0, keepdims=True)
208
+ rms = np.sqrt((Y ** 2).mean())
209
+ if rms < 1e-8:
210
+ return Y
211
+ return Y * (target_rms / rms)
212
+
213
+
214
+ def q_from_Y(Y: np.ndarray, KERNEL):
215
+ D = pairwise_dist(Y)
216
+ Q = KERNEL.forward(D)
217
+ return Q, D
dualmapref/__init__.py ADDED
@@ -0,0 +1,24 @@
1
+ """Public DualMapRef API, backed by the checkpoint-compatible implementation."""
2
+ from conformend import (
3
+ ConforMend as _LegacyConforMend,
4
+ circular_standard_deviation,
5
+ density_centrality_select,
6
+ ensemble_pairwise_prior,
7
+ fps_indices,
8
+ mmff94s_window_fps,
9
+ pairwise_rmsd,
10
+ run_full_inference,
11
+ select_energy_window,
12
+ wrap_angle,
13
+ )
14
+
15
+ class DualMapRef(_LegacyConforMend):
16
+ """Checkpoint-backed DualMapRef inference interface."""
17
+
18
+
19
+ __all__ = [
20
+ 'DualMapRef', 'circular_standard_deviation', 'density_centrality_select',
21
+ 'ensemble_pairwise_prior', 'fps_indices', 'mmff94s_window_fps',
22
+ 'pairwise_rmsd', 'run_full_inference', 'select_energy_window', 'wrap_angle',
23
+ ]
24
+ __version__ = '0.2.8'
dualmapref/refiner.py ADDED
@@ -0,0 +1,16 @@
1
+ """DualMapRef candidate-conditioned torsional refinement API."""
2
+ from conformend.refiner import (
3
+ AcceptanceConfig, ConforMendRefiner as _LegacyRefiner,
4
+ InterpretableLocalRefiner, RefinementResult, accept_refinement,
5
+ apply_torsion_residuals, bond_cross_fragment_context, proximity_matrix,
6
+ stable_relational_energy,
7
+ )
8
+ class DualMapRefRefiner(_LegacyRefiner):
9
+ """Checkpoint-compatible DualMapRef torsional Refiner."""
10
+
11
+
12
+ __all__ = [
13
+ 'AcceptanceConfig', 'DualMapRefRefiner', 'InterpretableLocalRefiner',
14
+ 'RefinementResult', 'accept_refinement', 'apply_torsion_residuals',
15
+ 'bond_cross_fragment_context', 'proximity_matrix', 'stable_relational_energy',
16
+ ]
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ConforMend developers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.