round-7: morgan_2048_rp parity arm (src/pino/embeddings.py)
Browse files- src/pino/embeddings.py +70 -2
src/pino/embeddings.py
CHANGED
|
@@ -23,6 +23,14 @@ STRUCTURAL_DIM = OPENPOM_DIM # 138 (Morgan fallback structural block)
|
|
| 23 |
REAL_POM_DIM = 256 # genuine OpenPOM v1.0.0 penultimate embedding (ablation-proven)
|
| 24 |
SHAPE_DIM = SHAPE_DIM # 7
|
| 25 |
PHYSICAL_DIM = 2 # molecular_weight, logp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
FUNCTIONAL_DIM = 4 # ester, alcohol, ketone, aldehyde
|
| 27 |
COMBINED_DIM = STRUCTURAL_DIM + SHAPE_DIM + PHYSICAL_DIM + FUNCTIONAL_DIM # 151
|
| 28 |
COMBINED_DIM_REAL_POM = REAL_POM_DIM + SHAPE_DIM + PHYSICAL_DIM + FUNCTIONAL_DIM # 269
|
|
@@ -106,13 +114,19 @@ class OlfactoryEmbeddingEngine:
|
|
| 106 |
self.use_fallback = use_fallback
|
| 107 |
self.structural_source = structural_source
|
| 108 |
self.use_real_pom = structural_source == "openpom_256"
|
| 109 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
self.shape_dim = SHAPE_DIM
|
| 111 |
self.physical_dim = PHYSICAL_DIM
|
| 112 |
self.functional_dim = FUNCTIONAL_DIM
|
| 113 |
self.embedding_dim = (
|
| 114 |
-
COMBINED_DIM_REAL_POM if self.use_real_pom else COMBINED_DIM
|
| 115 |
)
|
|
|
|
| 116 |
self.registry = AromaRegistry()
|
| 117 |
self._pom_index: dict[str, int] = {}
|
| 118 |
self._pom_matrix: np.ndarray | None = None
|
|
@@ -190,6 +204,10 @@ class OlfactoryEmbeddingEngine:
|
|
| 190 |
if self.use_real_pom:
|
| 191 |
# POM arm: never reuse the registry's 138-dim Morgan block.
|
| 192 |
structural = self._compute_structural_embedding(smiles, cas)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
else:
|
| 194 |
structural = np.asarray(record.get("openpom_embedding") or [0.0] * STRUCTURAL_DIM, dtype=np.float32)
|
| 195 |
if structural.shape[0] != STRUCTURAL_DIM or (not is_natural and np.count_nonzero(structural) == 0):
|
|
@@ -200,6 +218,7 @@ class OlfactoryEmbeddingEngine:
|
|
| 200 |
physical = self._extract_physical_features(record)
|
| 201 |
functional = self._extract_functional_features(record)
|
| 202 |
combined = np.concatenate([structural, shape, physical, functional]).astype(np.float32)
|
|
|
|
| 203 |
norm = np.linalg.norm(combined)
|
| 204 |
return combined / norm if norm > 0 else combined
|
| 205 |
if is_natural:
|
|
@@ -208,6 +227,7 @@ class OlfactoryEmbeddingEngine:
|
|
| 208 |
physical = np.array([0.5, self.global_median_logp / 5.0], dtype=np.float32)
|
| 209 |
functional = _functional_group_flags(None)
|
| 210 |
combined = np.concatenate([structural, shape, physical, functional]).astype(np.float32)
|
|
|
|
| 211 |
norm = np.linalg.norm(combined)
|
| 212 |
return combined / norm if norm > 0 else combined
|
| 213 |
except Exception as exc:
|
|
@@ -219,12 +239,36 @@ class OlfactoryEmbeddingEngine:
|
|
| 219 |
physical = np.array([0.5, self.global_median_logp / 5.0], dtype=np.float32)
|
| 220 |
functional = _functional_group_flags(None)
|
| 221 |
combined = np.concatenate([structural, shape, physical, functional]).astype(np.float32)
|
|
|
|
| 222 |
norm = np.linalg.norm(combined)
|
| 223 |
return combined / norm if norm > 0 else combined
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
def _compute_structural_embedding(self, smiles: str, cas: str | None = None) -> np.ndarray:
|
| 226 |
normalized_cas = self._normalized_cas(cas)
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
# Genuine OpenPOM arm: authoritative 256-dim ensemble for known CAS.
|
| 229 |
if self.use_real_pom:
|
| 230 |
if normalized_cas and normalized_cas in self._pom_index and self._pom_matrix is not None:
|
|
@@ -258,6 +302,30 @@ class OlfactoryEmbeddingEngine:
|
|
| 258 |
|
| 259 |
return self._morgan_structural(smiles)
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
def _morgan_structural(self, smiles: str) -> np.ndarray:
|
| 262 |
"""Deterministic 128-bit Morgan + 10 physicochemical descriptors (138-D).
|
| 263 |
|
|
|
|
| 23 |
REAL_POM_DIM = 256 # genuine OpenPOM v1.0.0 penultimate embedding (ablation-proven)
|
| 24 |
SHAPE_DIM = SHAPE_DIM # 7
|
| 25 |
PHYSICAL_DIM = 2 # molecular_weight, logp
|
| 26 |
+
# --- morgan_2048_rp arm (round-7 panel response): 2048-bit radius-2 Morgan +
|
| 27 |
+
# 10 physicochemical descriptors = 2058-D structural block; full token 2071-D;
|
| 28 |
+
# fixed seeded random projection down to the OpenPOM arm's combined width so
|
| 29 |
+
# both trained arms carry an IDENTICAL input-projection parameter count
|
| 30 |
+
# (parameter-parity control requested by the evaluation reviewer).
|
| 31 |
+
MORGAN2048_STRUCTURAL_DIM = 2058
|
| 32 |
+
MORGAN2048_TOKEN_DIM = MORGAN2048_STRUCTURAL_DIM + SHAPE_DIM + PHYSICAL_DIM + 4 # 2071
|
| 33 |
+
MORGAN2048_RP_SEED = 20260725
|
| 34 |
FUNCTIONAL_DIM = 4 # ester, alcohol, ketone, aldehyde
|
| 35 |
COMBINED_DIM = STRUCTURAL_DIM + SHAPE_DIM + PHYSICAL_DIM + FUNCTIONAL_DIM # 151
|
| 36 |
COMBINED_DIM_REAL_POM = REAL_POM_DIM + SHAPE_DIM + PHYSICAL_DIM + FUNCTIONAL_DIM # 269
|
|
|
|
| 114 |
self.use_fallback = use_fallback
|
| 115 |
self.structural_source = structural_source
|
| 116 |
self.use_real_pom = structural_source == "openpom_256"
|
| 117 |
+
self.use_morgan_2048_rp = structural_source == "morgan_2048_rp"
|
| 118 |
+
self.structural_dim = (
|
| 119 |
+
REAL_POM_DIM if self.use_real_pom
|
| 120 |
+
else MORGAN2048_STRUCTURAL_DIM if self.use_morgan_2048_rp
|
| 121 |
+
else STRUCTURAL_DIM
|
| 122 |
+
)
|
| 123 |
self.shape_dim = SHAPE_DIM
|
| 124 |
self.physical_dim = PHYSICAL_DIM
|
| 125 |
self.functional_dim = FUNCTIONAL_DIM
|
| 126 |
self.embedding_dim = (
|
| 127 |
+
COMBINED_DIM_REAL_POM if (self.use_real_pom or self.use_morgan_2048_rp) else COMBINED_DIM
|
| 128 |
)
|
| 129 |
+
self._rp_matrix: np.ndarray | None = None
|
| 130 |
self.registry = AromaRegistry()
|
| 131 |
self._pom_index: dict[str, int] = {}
|
| 132 |
self._pom_matrix: np.ndarray | None = None
|
|
|
|
| 204 |
if self.use_real_pom:
|
| 205 |
# POM arm: never reuse the registry's 138-dim Morgan block.
|
| 206 |
structural = self._compute_structural_embedding(smiles, cas)
|
| 207 |
+
elif self.use_morgan_2048_rp:
|
| 208 |
+
# 2048-bit arm: always fresh-compute the wide fingerprint;
|
| 209 |
+
# the registry cache only holds the 138-D block.
|
| 210 |
+
structural = self._compute_structural_embedding(smiles, cas)
|
| 211 |
else:
|
| 212 |
structural = np.asarray(record.get("openpom_embedding") or [0.0] * STRUCTURAL_DIM, dtype=np.float32)
|
| 213 |
if structural.shape[0] != STRUCTURAL_DIM or (not is_natural and np.count_nonzero(structural) == 0):
|
|
|
|
| 218 |
physical = self._extract_physical_features(record)
|
| 219 |
functional = self._extract_functional_features(record)
|
| 220 |
combined = np.concatenate([structural, shape, physical, functional]).astype(np.float32)
|
| 221 |
+
combined = self._maybe_project(combined)
|
| 222 |
norm = np.linalg.norm(combined)
|
| 223 |
return combined / norm if norm > 0 else combined
|
| 224 |
if is_natural:
|
|
|
|
| 227 |
physical = np.array([0.5, self.global_median_logp / 5.0], dtype=np.float32)
|
| 228 |
functional = _functional_group_flags(None)
|
| 229 |
combined = np.concatenate([structural, shape, physical, functional]).astype(np.float32)
|
| 230 |
+
combined = self._maybe_project(combined)
|
| 231 |
norm = np.linalg.norm(combined)
|
| 232 |
return combined / norm if norm > 0 else combined
|
| 233 |
except Exception as exc:
|
|
|
|
| 239 |
physical = np.array([0.5, self.global_median_logp / 5.0], dtype=np.float32)
|
| 240 |
functional = _functional_group_flags(None)
|
| 241 |
combined = np.concatenate([structural, shape, physical, functional]).astype(np.float32)
|
| 242 |
+
combined = self._maybe_project(combined)
|
| 243 |
norm = np.linalg.norm(combined)
|
| 244 |
return combined / norm if norm > 0 else combined
|
| 245 |
|
| 246 |
+
def _maybe_project(self, combined: np.ndarray) -> np.ndarray:
|
| 247 |
+
"""morgan_2048_rp arm: fixed seeded random projection 2071-D -> 269-D.
|
| 248 |
+
|
| 249 |
+
The projection is generated from a fixed seed on first use (no learned
|
| 250 |
+
parameters, no checkpoint dependency), so every process computes the
|
| 251 |
+
identical map. Output width matches the OpenPOM arm's combined token,
|
| 252 |
+
giving the two trained arms an identical input-projection parameter
|
| 253 |
+
count (capacity parity for the representation comparison)."""
|
| 254 |
+
if not self.use_morgan_2048_rp:
|
| 255 |
+
return combined
|
| 256 |
+
if self._rp_matrix is None:
|
| 257 |
+
rng = np.random.default_rng(MORGAN2048_RP_SEED)
|
| 258 |
+
self._rp_matrix = (
|
| 259 |
+
rng.standard_normal((combined.shape[0], COMBINED_DIM_REAL_POM)).astype(np.float32)
|
| 260 |
+
/ np.sqrt(float(combined.shape[0]))
|
| 261 |
+
)
|
| 262 |
+
return combined @ self._rp_matrix
|
| 263 |
+
|
| 264 |
def _compute_structural_embedding(self, smiles: str, cas: str | None = None) -> np.ndarray:
|
| 265 |
normalized_cas = self._normalized_cas(cas)
|
| 266 |
|
| 267 |
+
# 2048-bit arm: always the wide fingerprint block (never the cached
|
| 268 |
+
# 138-D registry block, which is a different representation).
|
| 269 |
+
if self.use_morgan_2048_rp:
|
| 270 |
+
return self._morgan_structural_wide(smiles)
|
| 271 |
+
|
| 272 |
# Genuine OpenPOM arm: authoritative 256-dim ensemble for known CAS.
|
| 273 |
if self.use_real_pom:
|
| 274 |
if normalized_cas and normalized_cas in self._pom_index and self._pom_matrix is not None:
|
|
|
|
| 302 |
|
| 303 |
return self._morgan_structural(smiles)
|
| 304 |
|
| 305 |
+
def _morgan_structural_wide(self, smiles: str) -> np.ndarray:
|
| 306 |
+
"""Deterministic 2048-bit radius-2 Morgan + 10 physicochemical descriptors (2058-D).
|
| 307 |
+
|
| 308 |
+
The standard-width fingerprint control: identical construction to the
|
| 309 |
+
preregistered 128-bit baseline, only fpSize differs."""
|
| 310 |
+
if not self._is_valid_smiles(smiles):
|
| 311 |
+
return np.zeros(MORGAN2048_STRUCTURAL_DIM, dtype=np.float32)
|
| 312 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 313 |
+
if not mol:
|
| 314 |
+
return np.zeros(MORGAN2048_STRUCTURAL_DIM, dtype=np.float32)
|
| 315 |
+
try:
|
| 316 |
+
fpgen = AllChem.GetMorganGenerator(radius=2, fpSize=2048)
|
| 317 |
+
fp_array = np.array(fpgen.GetFingerprint(mol), dtype=np.float32)
|
| 318 |
+
except AttributeError:
|
| 319 |
+
fp = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=2048)
|
| 320 |
+
fp_array = np.array(fp, dtype=np.float32)
|
| 321 |
+
physchem = np.array([
|
| 322 |
+
Descriptors.MolWt(mol), Descriptors.ExactMolWt(mol), Descriptors.MolLogP(mol),
|
| 323 |
+
Descriptors.HeavyAtomCount(mol), Descriptors.NumHAcceptors(mol),
|
| 324 |
+
Descriptors.NumHDonors(mol), Descriptors.NumRotatableBonds(mol),
|
| 325 |
+
Descriptors.FractionCSP3(mol), Descriptors.TPSA(mol), Descriptors.LabuteASA(mol),
|
| 326 |
+
], dtype=np.float32)
|
| 327 |
+
return np.concatenate([fp_array, physchem]).astype(np.float32)
|
| 328 |
+
|
| 329 |
def _morgan_structural(self, smiles: str) -> np.ndarray:
|
| 330 |
"""Deterministic 128-bit Morgan + 10 physicochemical descriptors (138-D).
|
| 331 |
|