Matthew Ford commited on
Commit
a150abf
·
1 Parent(s): 2ff3288

feat: link molequles material-profiles odor text into training descriptor matrix

Browse files

Add Tier 3.5 to build_master_matrix that reads odor_description /
odor_descriptions from material_profiles (molequles-enriched) and maps it
through the existing Tier-2 tokenizer. Ingredients absent from the registry
(absolutes, natural oils with real CAS) previously fell through every tier to
a zero 138-D vector; 71 of the 120 uncovered CAS already carried usable odor
text in the molequles layer that the build never read.

Ingredient-instance coverage on the repaired empirical corpus: 90.7% -> 98.5%
(531 uncovered instances remain, down from 3353).


Former-commit-id: d6969e81f352267de7efd0e1510f3f17852334d3
Former-commit-id: 319ae38dfaa7809ce4b724562df74fe5932a9766

Files changed (1) hide show
  1. scripts/generate_targets_v3.py +44 -0
scripts/generate_targets_v3.py CHANGED
@@ -1614,6 +1614,36 @@ def load_perfumer_tiers(path: Path) -> dict[str, str]:
1614
  return tiers
1615
 
1616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1617
  def load_alias_descriptions() -> dict[str, str]:
1618
  descriptions: dict[str, list[str]] = {}
1619
 
@@ -1738,10 +1768,12 @@ def build_master_matrix(
1738
  pyrfume_path: Path,
1739
  registry_path: Path,
1740
  perfumer_tier_path: Path | None = None,
 
1741
  ) -> dict[str, np.ndarray]:
1742
  """Compile the (CAS -> 138-D) ingredient descriptor matrix."""
1743
  top_vec, heart_vec, base_vec = build_tier3_vectors(vocab)
1744
  alias_descriptions = load_alias_descriptions()
 
1745
  perfumer_tiers = load_perfumer_tiers(perfumer_tier_path) if perfumer_tier_path else {}
1746
 
1747
  # Tier 1: Pyrfume exact 138-D vectors.
@@ -1778,6 +1810,16 @@ def build_master_matrix(
1778
  else:
1779
  matrix[cas] = heart_vec
1780
 
 
 
 
 
 
 
 
 
 
 
1781
  # Tier 4: material-name text from resolver/natural-profile aliases. These
1782
  # cover literature-only naturals and trade materials absent from the registry.
1783
  for cas, text in alias_descriptions.items():
@@ -1836,6 +1878,7 @@ def main() -> None:
1836
  parser.add_argument("--pyrfume", default="data/pyrfume_descriptors_by_cas.json")
1837
  parser.add_argument("--registry", default="data/aroma_chemicals.jsonl")
1838
  parser.add_argument("--perfumer-tiers", default="data/perfumer_tier_lookup.json")
 
1839
  parser.add_argument("--matrix-out", default="data/ingredient_master_matrix_v3.json")
1840
  args = parser.parse_args()
1841
 
@@ -1847,6 +1890,7 @@ def main() -> None:
1847
  Path(args.pyrfume),
1848
  Path(args.registry),
1849
  Path(args.perfumer_tiers),
 
1850
  )
1851
  print(f"Compiled ingredient master matrix: {len(matrix)} CAS")
1852
 
 
1614
  return tiers
1615
 
1616
 
1617
+ def load_material_profiles_descriptions(path: Path | None) -> dict[str, str]:
1618
+ """CAS -> odor-description text from the molequles-enriched material profiles.
1619
+
1620
+ This is the molequles -> training link: 71 of the 120 training ingredients
1621
+ that fall through every other tier already carry odor text here, but the
1622
+ master-matrix build never read it. Returns text keyed by raw CAS so it can
1623
+ feed Tier-2 tokenization. Only CAS with non-empty text are included."""
1624
+ if not path or not path.exists():
1625
+ return {}
1626
+ out: dict[str, str] = {}
1627
+ with path.open() as fh:
1628
+ for line in fh:
1629
+ try:
1630
+ rec = json.loads(line)
1631
+ except Exception:
1632
+ continue
1633
+ cas = rec.get("cas")
1634
+ if not cas:
1635
+ continue
1636
+ od = rec.get("odor_description") or rec.get("odor_descriptions")
1637
+ if not od:
1638
+ continue
1639
+ if isinstance(od, list):
1640
+ od = " ".join(str(x) for x in od)
1641
+ od = str(od).strip()
1642
+ if od:
1643
+ out[cas] = od
1644
+ return out
1645
+
1646
+
1647
  def load_alias_descriptions() -> dict[str, str]:
1648
  descriptions: dict[str, list[str]] = {}
1649
 
 
1768
  pyrfume_path: Path,
1769
  registry_path: Path,
1770
  perfumer_tier_path: Path | None = None,
1771
+ material_profiles_path: Path | None = None,
1772
  ) -> dict[str, np.ndarray]:
1773
  """Compile the (CAS -> 138-D) ingredient descriptor matrix."""
1774
  top_vec, heart_vec, base_vec = build_tier3_vectors(vocab)
1775
  alias_descriptions = load_alias_descriptions()
1776
+ material_profiles = load_material_profiles_descriptions(material_profiles_path)
1777
  perfumer_tiers = load_perfumer_tiers(perfumer_tier_path) if perfumer_tier_path else {}
1778
 
1779
  # Tier 1: Pyrfume exact 138-D vectors.
 
1810
  else:
1811
  matrix[cas] = heart_vec
1812
 
1813
+ # Tier 3.5: molequles-enriched material-profiles odor text. This is the
1814
+ # molequles -> training link for ingredients absent from the registry (e.g.
1815
+ # absolutes and natural oils that carry a real CAS but no registry row).
1816
+ for cas, text in material_profiles.items():
1817
+ if cas in matrix:
1818
+ continue
1819
+ vec = description_to_vector(text, vocab)
1820
+ if vec.sum() > 0:
1821
+ matrix[cas] = vec
1822
+
1823
  # Tier 4: material-name text from resolver/natural-profile aliases. These
1824
  # cover literature-only naturals and trade materials absent from the registry.
1825
  for cas, text in alias_descriptions.items():
 
1878
  parser.add_argument("--pyrfume", default="data/pyrfume_descriptors_by_cas.json")
1879
  parser.add_argument("--registry", default="data/aroma_chemicals.jsonl")
1880
  parser.add_argument("--perfumer-tiers", default="data/perfumer_tier_lookup.json")
1881
+ parser.add_argument("--material-profiles", default="data/material_profiles_v11_6.jsonl")
1882
  parser.add_argument("--matrix-out", default="data/ingredient_master_matrix_v3.json")
1883
  args = parser.parse_args()
1884
 
 
1890
  Path(args.pyrfume),
1891
  Path(args.registry),
1892
  Path(args.perfumer_tiers),
1893
+ Path(args.material_profiles),
1894
  )
1895
  print(f"Compiled ingredient master matrix: {len(matrix)} CAS")
1896