fix: regenerate degenerate pyramid_targets as fold(objective_targets); add all-tags targets
Browse filesFound 2026-07-17 via trajectory-figure investigation: stored pyramid_targets was
degenerate (30.2% of records base-only; base tier mean mass 16.7 vs top 4.1/mid
2.5) because the OAV-decay tier classifier defaulted to base when OAV series were
empty. The model trained+validated against this base-skewed label.
- regenerate_pyramid_targets_fold.py: rebuild pyramid_targets as
fold(objective_targets) (physics-derived, balanced) -> v10_pyramidfix.jsonl.
base-only 1694->386; tier mass 4.05/2.53/16.67 -> 4.55/4.19/3.20. Non-destructive.
- build_alltags_targets.py: append OAV-weighted folded 575-dim alltags_targets
(3,575) per record -> v10_alltags.jsonl. Coverage: all 5708 records tagged.
Datasets not committed (705MB each, gitignored); live under data/ locally.
Former-commit-id: 43d4c84a7c50b6ee03463b6998ee46f82ccf323e
Former-commit-id: 8625c0094ab3aecc8025a424f8d3f3128fece8cb
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Build per-record 575-dim all-tags targets (3,575) for the pom_alltags arm.
|
| 3 |
+
|
| 4 |
+
For each record, the time-resolved all-tags target is the OAV-weighted sum of each
|
| 5 |
+
ingredient's 575-dim molequles tag distribution at each trajectory step, then
|
| 6 |
+
folded to top/mid/base (3,575) via early/mid/late max-windows — the same fold as
|
| 7 |
+
the 138-dim pyramid fix. Ingredients absent from the molequles tag matrix
|
| 8 |
+
contribute nothing (coverage is 1,187 CAS; documented).
|
| 9 |
+
|
| 10 |
+
Appends `alltags_targets` (3,575) to each record in the v10 pyramidfix dataset and
|
| 11 |
+
writes empirical_dataset_v10_alltags.jsonl. Non-destructive.
|
| 12 |
+
"""
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
import sys
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
import numpy as np
|
| 20 |
+
|
| 21 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 22 |
+
SRC = ROOT / "data/empirical_dataset_v10_pyramidfix.jsonl"
|
| 23 |
+
DST = ROOT / "data/empirical_dataset_v10_alltags.jsonl"
|
| 24 |
+
TAG_MATRIX = ROOT / "data/molequles_tag_matrix.json"
|
| 25 |
+
TAG_DIM = 575
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def fold3(a: np.ndarray) -> np.ndarray:
|
| 29 |
+
if a.shape == (3, a.shape[1]):
|
| 30 |
+
return a
|
| 31 |
+
chunks = np.array_split(a, 3, axis=0)
|
| 32 |
+
return np.stack([c.max(axis=0) for c in chunks], axis=0).astype(np.float32)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def main() -> int:
|
| 36 |
+
tag_matrix = json.loads(TAG_MATRIX.read_text())
|
| 37 |
+
n = 0
|
| 38 |
+
n_covered = 0
|
| 39 |
+
n_zero = 0
|
| 40 |
+
with SRC.open() as fin, DST.open("w") as fout:
|
| 41 |
+
for line in fin:
|
| 42 |
+
if not line.strip():
|
| 43 |
+
continue
|
| 44 |
+
r = json.loads(line)
|
| 45 |
+
if not isinstance(r, dict):
|
| 46 |
+
fout.write(line); continue
|
| 47 |
+
traj = r.get("trajectory", [])
|
| 48 |
+
T = len(traj)
|
| 49 |
+
acc = np.zeros((T, TAG_DIM), dtype=np.float32)
|
| 50 |
+
any_tag = False
|
| 51 |
+
for t, step in enumerate(traj):
|
| 52 |
+
oav = step.get("OAV", {})
|
| 53 |
+
if not oav:
|
| 54 |
+
continue
|
| 55 |
+
total = sum(oav.values()) or 1.0
|
| 56 |
+
vec = np.zeros(TAG_DIM, dtype=np.float32)
|
| 57 |
+
for cas, val in oav.items():
|
| 58 |
+
tags = tag_matrix.get(cas)
|
| 59 |
+
if tags is None:
|
| 60 |
+
continue
|
| 61 |
+
any_tag = True
|
| 62 |
+
vec += np.asarray(tags, dtype=np.float32) * (float(val) / total)
|
| 63 |
+
acc[t] = vec
|
| 64 |
+
if any_tag:
|
| 65 |
+
n_covered += 1
|
| 66 |
+
target = fold3(acc) if T else np.zeros((3, TAG_DIM), dtype=np.float32)
|
| 67 |
+
if target.sum() == 0:
|
| 68 |
+
n_zero += 1
|
| 69 |
+
r["alltags_targets"] = target.tolist()
|
| 70 |
+
n += 1
|
| 71 |
+
fout.write(json.dumps(r) + "\n")
|
| 72 |
+
print(f"records: {n} | with >=1 tagged ingredient: {n_covered} | all-zero target: {n_zero}")
|
| 73 |
+
print(f"wrote -> {DST}")
|
| 74 |
+
return 0
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
sys.exit(main())
|
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Regenerate pyramid_targets as fold(objective_targets) — non-destructive label fix.
|
| 3 |
+
|
| 4 |
+
CONTEXT (found 2026-07-17): the stored `pyramid_targets` field was generated by
|
| 5 |
+
scripts/generate_pyramid_targets.py using OAV-decay tier classification. Because
|
| 6 |
+
the OAV series is empty for most ingredients (only 1,366/2,420 CAS have perfumer
|
| 7 |
+
tiers), the classifier defaulted most ingredients to "base", producing a
|
| 8 |
+
DEGENERATE label: 30.2% of records have top=mid=0 (base-only) and the base tier
|
| 9 |
+
carries mean mass 16.7 vs top 4.1 / mid 2.5. The model trained+validated against
|
| 10 |
+
this base-skewed label.
|
| 11 |
+
|
| 12 |
+
FIX (non-destructive): derive pyramid targets from the record's OWN time-resolved
|
| 13 |
+
`objective_targets` trajectory, which is physics-derived (Dortmund-UNIFAC
|
| 14 |
+
evaporation) and already balanced (folded mean 4.54/4.19/3.18). The fold maps the
|
| 15 |
+
49-step trajectory to top/heart/base via early/mid/late max-windows — the exact
|
| 16 |
+
function the dataset loader already applies (objective_targets_to_pyramid). This
|
| 17 |
+
preserves provenance: the label is the record's existing physics target, just
|
| 18 |
+
folded, never invented.
|
| 19 |
+
|
| 20 |
+
Writes a NEW file (empirical_dataset_v10_pyramidfix.jsonl); the locked v9 corpus
|
| 21 |
+
is never modified in place. Reports tier-balance before/after.
|
| 22 |
+
"""
|
| 23 |
+
from __future__ import annotations
|
| 24 |
+
|
| 25 |
+
import json
|
| 26 |
+
import sys
|
| 27 |
+
from pathlib import Path
|
| 28 |
+
|
| 29 |
+
import numpy as np
|
| 30 |
+
|
| 31 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 32 |
+
SRC = ROOT / "data/empirical_dataset_v9_plus_wisemoor_clean.repaired.jsonl"
|
| 33 |
+
DST = ROOT / "data/empirical_dataset_v10_pyramidfix.jsonl"
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def fold_pyramid(a: np.ndarray) -> np.ndarray:
|
| 37 |
+
"""Fold a time-resolved (T,138) objective trajectory to (3,138) top/mid/base
|
| 38 |
+
via early/mid/late max-windows (mirrors pimt_model.objective_targets_to_pyramid)."""
|
| 39 |
+
a = np.asarray(a, dtype=np.float32)
|
| 40 |
+
if a.shape == (3, 138):
|
| 41 |
+
return a
|
| 42 |
+
if a.ndim != 2 or a.shape[1] != 138:
|
| 43 |
+
raise ValueError(f"cannot fold shape {a.shape}")
|
| 44 |
+
chunks = np.array_split(a, 3, axis=0)
|
| 45 |
+
return np.stack([c.max(axis=0) for c in chunks], axis=0).astype(np.float32)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def tier_mass(pyr: np.ndarray) -> tuple[float, float, float]:
|
| 49 |
+
return (float(pyr[0].sum()), float(pyr[1].sum()), float(pyr[2].sum()))
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def main() -> int:
|
| 53 |
+
if not SRC.exists():
|
| 54 |
+
print(f"ERROR: source dataset not found: {SRC}"); return 1
|
| 55 |
+
n = 0
|
| 56 |
+
n_fixed = 0
|
| 57 |
+
n_baseonly_before = 0
|
| 58 |
+
n_baseonly_after = 0
|
| 59 |
+
tm_before, tm_after = [], []
|
| 60 |
+
with SRC.open() as fin, DST.open("w") as fout:
|
| 61 |
+
for line in fin:
|
| 62 |
+
if not line.strip():
|
| 63 |
+
continue
|
| 64 |
+
try:
|
| 65 |
+
r = json.loads(line)
|
| 66 |
+
except Exception:
|
| 67 |
+
continue
|
| 68 |
+
if not isinstance(r, dict):
|
| 69 |
+
fout.write(line); continue
|
| 70 |
+
obj = r.get("objective_targets")
|
| 71 |
+
# before stats
|
| 72 |
+
pyr_old = r.get("pyramid_targets")
|
| 73 |
+
if isinstance(pyr_old, list):
|
| 74 |
+
po = np.array(pyr_old, dtype=float)
|
| 75 |
+
if po.shape == (3, 138):
|
| 76 |
+
tm_before.append(tier_mass(po))
|
| 77 |
+
if po[0].sum() == 0 and po[1].sum() == 0:
|
| 78 |
+
n_baseonly_before += 1
|
| 79 |
+
# regenerate
|
| 80 |
+
if obj is not None:
|
| 81 |
+
try:
|
| 82 |
+
new_pyr = fold_pyramid(np.array(obj, dtype=np.float32))
|
| 83 |
+
r["pyramid_targets"] = new_pyr.tolist()
|
| 84 |
+
r["pyramid_targets_provenance"] = "fold(objective_targets) v10 label fix"
|
| 85 |
+
tm_after.append(tier_mass(new_pyr))
|
| 86 |
+
if new_pyr[0].sum() == 0 and new_pyr[1].sum() == 0:
|
| 87 |
+
n_baseonly_after += 1
|
| 88 |
+
n_fixed += 1
|
| 89 |
+
except Exception:
|
| 90 |
+
pass
|
| 91 |
+
n += 1
|
| 92 |
+
fout.write(json.dumps(r) + "\n")
|
| 93 |
+
|
| 94 |
+
def mean_tier(lst):
|
| 95 |
+
a = np.array(lst) if lst else np.zeros((1, 3))
|
| 96 |
+
return a.mean(axis=0).round(2).tolist()
|
| 97 |
+
|
| 98 |
+
print(f"records processed: {n}")
|
| 99 |
+
print(f"pyramid_targets regenerated (fold of objective_targets): {n_fixed}")
|
| 100 |
+
print(f"base-only (top=mid=0) BEFORE: {n_baseonly_before} AFTER: {n_baseonly_after}")
|
| 101 |
+
print(f"mean tier mass BEFORE (old pyramid_targets): {mean_tier(tm_before)}")
|
| 102 |
+
print(f"mean tier mass AFTER (folded objective_targets): {mean_tier(tm_after)}")
|
| 103 |
+
print(f"wrote -> {DST}")
|
| 104 |
+
return 0
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
sys.exit(main())
|