mattbitzesty commited on
Commit
9135880
·
verified ·
1 Parent(s): 136412d

round-7 follow-through: disjoint_256 arm (src/pino/embeddings.py)

Browse files
Files changed (1) hide show
  1. src/pino/embeddings.py +33 -7
src/pino/embeddings.py CHANGED
@@ -37,6 +37,7 @@ COMBINED_DIM_REAL_POM = REAL_POM_DIM + SHAPE_DIM + PHYSICAL_DIM + FUNCTIONAL_DIM
37
 
38
  _REPO_ROOT = Path(__file__).resolve().parents[2]
39
  _POM_DIR = _REPO_ROOT / "artifacts" / "pom"
 
40
 
41
  # Mute RDKit's internal C++ warning streams globally.
42
  rd_logger = RDLogger.logger()
@@ -115,8 +116,9 @@ class OlfactoryEmbeddingEngine:
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
  )
@@ -124,7 +126,7 @@ class OlfactoryEmbeddingEngine:
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()
@@ -132,6 +134,8 @@ class OlfactoryEmbeddingEngine:
132
  self._pom_matrix: np.ndarray | None = None
133
  if self.use_real_pom:
134
  self._load_real_pom()
 
 
135
  self.global_median_logp = self._compute_median_logp() if global_median_logp is None else global_median_logp
136
  if not self.use_fallback:
137
  self._initialize_real_openpom()
@@ -153,6 +157,26 @@ class OlfactoryEmbeddingEngine:
153
  ids = idx.get("ids", [])
154
  self._pom_index = {self._normalized_cas(k): i for i, k in enumerate(ids)}
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  def _compute_median_logp(self) -> float:
157
  rows = self.registry._conn.execute(
158
  "SELECT logp FROM aroma_chemicals WHERE logp IS NOT NULL AND logp != 0"
@@ -201,8 +225,8 @@ class OlfactoryEmbeddingEngine:
201
  try:
202
  record = self.registry.get(normalized_cas)
203
  if record:
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;
@@ -269,11 +293,13 @@ class OlfactoryEmbeddingEngine:
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:
275
  return self._pom_matrix[self._pom_index[normalized_cas]]
276
- # CAS absent from the genuine-POM asset: real Morgan block, zero-padded
277
  # to the 256-dim structural slot so the combined dim stays constant.
278
  morgan = self._morgan_structural(smiles)
279
  padded = np.zeros(self.structural_dim, dtype=np.float32)
 
37
 
38
  _REPO_ROOT = Path(__file__).resolve().parents[2]
39
  _POM_DIR = _REPO_ROOT / "artifacts" / "pom"
40
+ _DISJOINT_DIR = _REPO_ROOT / "artifacts" / "pom_disjoint"
41
 
42
  # Mute RDKit's internal C++ warning streams globally.
43
  rd_logger = RDLogger.logger()
 
116
  self.structural_source = structural_source
117
  self.use_real_pom = structural_source == "openpom_256"
118
  self.use_morgan_2048_rp = structural_source == "morgan_2048_rp"
119
+ self.use_disjoint_pom = structural_source == "disjoint_256"
120
  self.structural_dim = (
121
+ REAL_POM_DIM if (self.use_real_pom or self.use_disjoint_pom)
122
  else MORGAN2048_STRUCTURAL_DIM if self.use_morgan_2048_rp
123
  else STRUCTURAL_DIM
124
  )
 
126
  self.physical_dim = PHYSICAL_DIM
127
  self.functional_dim = FUNCTIONAL_DIM
128
  self.embedding_dim = (
129
+ COMBINED_DIM_REAL_POM if (self.use_real_pom or self.use_morgan_2048_rp or self.use_disjoint_pom) else COMBINED_DIM
130
  )
131
  self._rp_matrix: np.ndarray | None = None
132
  self.registry = AromaRegistry()
 
134
  self._pom_matrix: np.ndarray | None = None
135
  if self.use_real_pom:
136
  self._load_real_pom()
137
+ if self.use_disjoint_pom:
138
+ self._load_disjoint_pom()
139
  self.global_median_logp = self._compute_median_logp() if global_median_logp is None else global_median_logp
140
  if not self.use_fallback:
141
  self._initialize_real_openpom()
 
157
  ids = idx.get("ids", [])
158
  self._pom_index = {self._normalized_cas(k): i for i, k in enumerate(ids)}
159
 
160
+ def _load_disjoint_pom(self) -> None:
161
+ """Load the benchmark-disjoint MPNN encoder embeddings (256-dim).
162
+
163
+ Same CAS-indexed layout as the genuine-POM asset, produced by
164
+ scripts/build_disjoint_asset_and_gate.py from the round-7 disjoint
165
+ encoder (mattbitzesty/pino-disjoint-encoder). Fail closed if absent:
166
+ a Morgan feature must never be silently substituted for the disjoint
167
+ arm's representation. Reuses _pom_index/_pom_matrix slots (the two
168
+ arms are never active simultaneously)."""
169
+ emb_path = _DISJOINT_DIR / "disjoint_embeddings.npy"
170
+ idx_path = _DISJOINT_DIR / "disjoint_index.json"
171
+ if not (emb_path.exists() and idx_path.exists()):
172
+ raise RealOpenPOMUnavailableError(
173
+ f"Disjoint-encoder asset missing under {_DISJOINT_DIR}; cannot honour structural_source='disjoint_256'"
174
+ )
175
+ self._pom_matrix = np.load(emb_path).astype(np.float32)
176
+ idx = json.loads(idx_path.read_text())
177
+ ids = idx.get("ids", [])
178
+ self._pom_index = {self._normalized_cas(k): i for i, k in enumerate(ids)}
179
+
180
  def _compute_median_logp(self) -> float:
181
  rows = self.registry._conn.execute(
182
  "SELECT logp FROM aroma_chemicals WHERE logp IS NOT NULL AND logp != 0"
 
225
  try:
226
  record = self.registry.get(normalized_cas)
227
  if record:
228
+ if self.use_real_pom or self.use_disjoint_pom:
229
+ # POM/disjoint arms: never reuse the registry's 138-dim Morgan block.
230
  structural = self._compute_structural_embedding(smiles, cas)
231
  elif self.use_morgan_2048_rp:
232
  # 2048-bit arm: always fresh-compute the wide fingerprint;
 
293
  if self.use_morgan_2048_rp:
294
  return self._morgan_structural_wide(smiles)
295
 
296
+ # Genuine OpenPOM arm / disjoint-encoder arm: authoritative 256-dim
297
+ # embedding for known CAS (both assets share the CAS-indexed layout and
298
+ # are loaded into the same slots; exactly one arm is active at a time).
299
+ if self.use_real_pom or self.use_disjoint_pom:
300
  if normalized_cas and normalized_cas in self._pom_index and self._pom_matrix is not None:
301
  return self._pom_matrix[self._pom_index[normalized_cas]]
302
+ # CAS absent from the asset: real Morgan block, zero-padded
303
  # to the 256-dim structural slot so the combined dim stays constant.
304
  morgan = self._morgan_structural(smiles)
305
  padded = np.zeros(self.structural_dim, dtype=np.float32)