ADOS-2 scoring step in the indicators cycle, run after the document import commits; EEG analysis ordered before the relation steps
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""What this module covers: the cycle's `score_ados2` step (TEA's ADOS-2 CSS and classification, before the
|
||||
association and the silver load) and how the cycle gets the scoring (`webapi.orchestration.eeg.live_ados2_scoring`).
|
||||
|
||||
Fixtures: none; the graph is built from `webapi.orchestration.cycle` and the scoring is a stand-in.
|
||||
"""
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from hamilton import driver
|
||||
|
||||
from webapi.orchestration import cycle, run, steps_of
|
||||
from webapi.orchestration.context import Context
|
||||
from webapi.orchestration.eeg import SCORING_ENV, live_ados2_scoring
|
||||
|
||||
|
||||
@pytest.mark.parametrize("proyecto", ["cacu", "cama", "tea"])
|
||||
def test_the_scoring_runs_before_the_analysis_the_association_and_the_load(proyecto):
|
||||
"""Pins down: `score_ados2` needs only the migrated schema and `analyze_eeg` waits for it, so the CSS is written
|
||||
in seconds instead of after a long EEG analysis, and still before the association and the silver load read it.
|
||||
Fails if the scoring moves after the analysis, the association or the load."""
|
||||
dr = driver.Builder().with_modules(cycle).with_config({"with_cohorts": proyecto == "cacu"}).build()
|
||||
deps = {n.name: {d.name for d in n.dependencies} - {"ctx"} for n in dr.graph.get_nodes() if n.name != "ctx"}
|
||||
assert deps["score_ados2"] == {"migrate"}
|
||||
assert deps["analyze_eeg"] == {"migrate", "score_ados2"}
|
||||
order = steps_of(proyecto)
|
||||
assert order.index("score_ados2") < order.index("analyze_eeg") < order.index("relate_ados2") < order.index("load")
|
||||
|
||||
|
||||
def test_the_step_returns_the_scorings_summary_or_nothing():
|
||||
"""Pins down: without a scoring the step is a no-op, and with one it runs it once and returns its summary as a
|
||||
dict. Fails if the step scores when nobody configured it, or stops reporting what it did."""
|
||||
assert [(p.name, p.summary) for p in run(Context("cacu", 2025, 2025), only=["score_ados2"])] == [
|
||||
("score_ados2", {})]
|
||||
calls = []
|
||||
|
||||
def scoring():
|
||||
calls.append(1)
|
||||
from models.ados2_scoring import Summary
|
||||
|
||||
return Summary(evaluations=5, scored=4, unscored=1)
|
||||
|
||||
pasos = run(Context("tea", 2025, 2025, ados2_scoring=scoring, repos=SimpleNamespace()), only=["score_ados2"])
|
||||
assert calls == [1]
|
||||
assert pasos[0].summary == {"evaluations": 5, "scored": 4, "unscored": 1}
|
||||
|
||||
|
||||
def test_only_tea_has_the_scoring_and_it_can_be_turned_off(monkeypatch):
|
||||
"""Pins down: TEA's cycle gets `models.ados2_scoring.score_pending`, the other domains none, `ADOS2_SCORING=off`
|
||||
leaves it out and an unknown value is refused. Fails if another domain starts scoring, `off` stops working, or
|
||||
a mistyped value silently counts as on."""
|
||||
from models.ados2_scoring import score_pending
|
||||
|
||||
monkeypatch.delenv(SCORING_ENV, raising=False)
|
||||
assert live_ados2_scoring("tea") is score_pending
|
||||
assert live_ados2_scoring("cama") is None
|
||||
monkeypatch.setenv(SCORING_ENV, "off")
|
||||
assert live_ados2_scoring("tea") is None
|
||||
monkeypatch.setenv(SCORING_ENV, "maybe")
|
||||
with pytest.raises(ValueError, match=SCORING_ENV):
|
||||
live_ados2_scoring("tea")
|
||||
@@ -58,15 +58,15 @@ def fake_analysis(tea_base):
|
||||
|
||||
@pytest.mark.parametrize("proyecto", ["cacu", "cama", "tea"])
|
||||
def test_the_eeg_analysis_runs_before_the_silver_load(proyecto):
|
||||
"""Pins down: `load` depends on `analyze_eeg`, which needs only the migrated schema.
|
||||
"""Pins down: `load` depends on `analyze_eeg`, which needs only the migrated schema and the quick ADOS-2 scoring.
|
||||
|
||||
Fails if `load` stops waiting for the analysis (the cycle would load an EEG's old analysis and pick the new one up
|
||||
only on the next cycle), or the analysis starts waiting for steps it doesn't need.
|
||||
only on the next cycle), or the analysis starts waiting for steps other than the migration and the scoring.
|
||||
"""
|
||||
dr = driver.Builder().with_modules(cycle).with_config({"with_cohorts": proyecto == "cacu"}).build()
|
||||
deps = {n.name: {d.name for d in n.dependencies} - {"ctx"} for n in dr.graph.get_nodes() if n.name != "ctx"}
|
||||
assert "analyze_eeg" in deps["load"]
|
||||
assert deps["analyze_eeg"] == {"migrate"}
|
||||
assert deps["analyze_eeg"] == {"migrate", "score_ados2"}
|
||||
order = steps_of(proyecto)
|
||||
assert order.index("analyze_eeg") < order.index("load")
|
||||
|
||||
|
||||
@@ -200,6 +200,27 @@ def test_an_import_into_tea_queues_its_recomputation(client):
|
||||
assert body["indicadores_en_recalculo"] is True
|
||||
|
||||
|
||||
def test_an_ados2_import_scores_the_evaluations_it_wrote(client):
|
||||
"""Pins down: after a commit that wrote `tea.Ados2` rows, their CSS and classification are computed from the
|
||||
stored items, module and the child's age, without waiting for the cycle; a value the source gave stays. Fails if
|
||||
the commit stops scoring (the CSS would wait for the next cycle, behind any EEG analysis) or scores over a value
|
||||
the source supplied."""
|
||||
patient = _json_source([{"pac_id": 9100, "pac_id_hospital": "T-ADOS", "pac_fecha_nacimiento": "2022-01-01"}],
|
||||
target="tea.Pacientes", column_mapping={"pac_id": "pac_id", "pac_id_hospital": "pac_id_hospital",
|
||||
"pac_fecha_nacimiento": "pac_fecha_nacimiento"})
|
||||
client.post("/imports/commit", json={"sources": [patient], "on_conflict": "upsert"})
|
||||
items = {"ado_sa_b1": 2, "ado_sa_b2": 2, "ado_sa_b3": 2, "ado_sa_b4": 2, "ado_rrb_d1": 2, "ado_rrb_d2": 2}
|
||||
rows = [{"ado_id": "a-1", "ado_pac_id": 9100, "ado_fecha": "2026-01-01", "ado_modulo": "MODULO_3", **items},
|
||||
{"ado_id": "a-2", "ado_pac_id": 9100, "ado_fecha": "2026-01-01", "ado_modulo": "MODULO_3", "ado_css": 9,
|
||||
"ado_clasificacion": "AUTISMO", **items}]
|
||||
ados = _json_source(rows, target="tea.Ados2", column_mapping={k: k for k in rows[0] | rows[1]})
|
||||
body = client.post("/imports/commit", json={"sources": [ados], "on_conflict": "upsert"}).get_json()
|
||||
assert body["results"][0]["created"] == 2
|
||||
stored = {r["ado_id"]: r for r in client.get("/data/tea.Ados2").get_json()["rows"]}
|
||||
assert (stored["a-1"]["ado_css"], stored["a-1"]["ado_clasificacion"]) == (7, "AUTISMO")
|
||||
assert stored["a-2"]["ado_css"] == 9
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sql_source_db(tmp_path):
|
||||
db_path = tmp_path / "external_source.db"
|
||||
|
||||
Reference in New Issue
Block a user