Matthew Ford
feat: v11.6 repair/enrichment scripts + independent-candidate prep + integrity tests
1bb570d | from __future__ import annotations | |
| import importlib.util | |
| from pathlib import Path | |
| SCRIPT = Path(__file__).parent.parent / "scripts" / "prepare_training_dataset.py" | |
| SPEC = importlib.util.spec_from_file_location("prepare_training_dataset", SCRIPT) | |
| MODULE = importlib.util.module_from_spec(SPEC) | |
| assert SPEC.loader is not None | |
| SPEC.loader.exec_module(MODULE) | |
| def component(cas: str, weight: float) -> dict: | |
| return {"cas": cas, "name": cas, "weight_fraction": weight} | |
| def test_curate_consolidates_ingredients_and_removes_exact_formula_duplicates(): | |
| rows = [ | |
| { | |
| "formula": [component("1-11-1", 0.2), component("2-22-2", 0.5), component("1-11-1", 0.3)], | |
| "metadata": {"generation_strategy": "floral"}, | |
| }, | |
| { | |
| "formula": [component("1-11-1", 0.5), component("2-22-2", 0.5)], | |
| "metadata": {"generation_strategy": "floral"}, | |
| }, | |
| ] | |
| curated, audit = MODULE.curate(rows) | |
| assert len(curated) == 1 | |
| assert curated[0]["formula"] == [component("1-11-1", 0.5), component("2-22-2", 0.5)] | |
| assert audit["rows_with_repeated_ingredients"] == 1 | |
| assert audit["ingredients_consolidated"] == 1 | |
| assert audit["exact_formula_duplicates_removed"] == 1 | |
| def test_formula_signature_is_order_independent(): | |
| first = [component("1-11-1", 0.25), component("2-22-2", 0.75)] | |
| second = list(reversed(first)) | |
| assert MODULE.formula_signature(first) == MODULE.formula_signature(second) | |