ac17829147
Keep the existing age-group stratum and add AGE_SEX to M21 and M22. M24 already declares AGE_SEX. The existing StatusComposition evaluator emits the sex-by-category counts needed by M23 and M31. Backend totals retain unknown sex and the charts omit those bars. Use the existing sex colors in male-first order, sort age groups by starting month, use zero for missing counts, and leave gaps for rates without a denominator. M24 retains count heights and percentage labels using offset groups. Both charts use the previous prevalence alpha of 0.25 for transparent outcomes. M31 uses the published outcome counts and keeps sex focus through a bar outline. Use the standard chart card height and identify changed figures by measure in the page checks. Ruff check, Ruff format check, lint-imports, Prettier, seven backend tests, 23 WebApp logic tests, and focused figure type checking passed. Full application type checking was blocked by missing pdfmake dependencies. Angular and Playwright runs are reserved for test machines.
157 lines
7.2 KiB
Python
157 lines
7.2 KiB
Python
import sqlite3
|
|
from datetime import date
|
|
from pathlib import Path
|
|
|
|
from jinja2 import Environment, StrictUndefined
|
|
|
|
from core import catalog, fhir
|
|
from core.domains import Domain, measures
|
|
from core.measures import Period, evaluate, report
|
|
from core.tea import ADOS2_ITEMS, ados2, dbt_vars
|
|
from core.tea.measures import MEASURES
|
|
from persistance.config import DBT_DIR
|
|
from persistance.measures import READS
|
|
|
|
|
|
def test_ados2_items_match_catalog_order():
|
|
fields = catalog.studies()["ados2"].fields
|
|
assert ADOS2_ITEMS == [
|
|
name for name, field in fields.items()
|
|
if field.type == "number" and not name.endswith("_total")
|
|
] # fmt: skip
|
|
assert len(ADOS2_ITEMS) == 22
|
|
assert dbt_vars()["ados2_items"] == ADOS2_ITEMS
|
|
|
|
|
|
def test_ados2_requires_items_in_both_domains():
|
|
assert ados2.total({"module": "toddler", "pointing": 3}) is None
|
|
assert ados2.total({"module": "toddler", "pointing": 3, "unusual_sensory_interest": 1}) == 3
|
|
|
|
|
|
def test_ados2_toddler_score_depends_on_age():
|
|
row = {
|
|
"module": "toddler",
|
|
"evaluation_date": date(2025, 10, 1),
|
|
"pointing": 2,
|
|
"gestures": 2,
|
|
"unusual_eye_contact": 2,
|
|
"unusual_sensory_interest": 2,
|
|
}
|
|
assert ados2.score(row, date(2024, 4, 1)) == (None, "non_spectrum")
|
|
assert ados2.score(row, date(2023, 10, 1)) == (None, "mild_to_moderate_concern")
|
|
|
|
|
|
def test_tea_measure_counts_only_confirmed_children_in_their_final_period():
|
|
measure = MEASURES["M21_DIAGNOSTICADOS_CON_EEG"]
|
|
rows = [
|
|
{"person_id": "a", "mchat_date": "2024-11-01", "confirmed_date": "2025-03-01",
|
|
"eeg_date": "2025-06-30", "age_group": "0-4"},
|
|
{"person_id": "b", "mchat_date": "2025-02-01", "confirmed_date": "2025-05-01",
|
|
"eeg_date": "2025-07-01", "age_group": "0-4"},
|
|
{"person_id": "c", "mchat_date": "2025-02-01", "age_group": "0-4"},
|
|
] # fmt: skip
|
|
first = evaluate(measure, rows, Period.parse("2025-S1"), event_cutoff=date(2025, 12, 31))
|
|
second = evaluate(measure, rows, Period.parse("2025-S2"), event_cutoff=date(2025, 12, 31))
|
|
|
|
assert first.populations == {"initial-population": 2, "denominator": 2, "numerator": 1}
|
|
assert second.populations == {"initial-population": 1, "denominator": 1, "numerator": 1}
|
|
|
|
|
|
def test_eeg_measures_stratify_confirmed_children_by_age_and_sex():
|
|
rows = [
|
|
{"person_id": "a", "mchat_date": "2025-01-01", "confirmed_date": "2025-03-01",
|
|
"eeg_date": "2025-05-01", "age_group": "16-23", "sex": "male"},
|
|
{"person_id": "b", "mchat_date": "2025-01-01", "confirmed_date": "2025-03-01",
|
|
"age_group": "16-23", "sex": "female"},
|
|
{"person_id": "c", "mchat_date": "2025-01-01", "confirmed_date": "2025-03-01",
|
|
"age_group": "24-36"},
|
|
] # fmt: skip
|
|
period = Period.parse("2025-S1")
|
|
for measure_id, with_eeg in (
|
|
("M21_DIAGNOSTICADOS_CON_EEG", 1),
|
|
("M22_DIAGNOSTICADOS_SIN_EEG", 0),
|
|
):
|
|
measure = MEASURES[measure_id]
|
|
result = evaluate(measure, rows, period)
|
|
assert result.strata["age_sex"] == {
|
|
"16-23|male": {"initial-population": 1, "denominator": 1, "numerator": with_eeg},
|
|
"16-23|female": {"initial-population": 1, "denominator": 1, "numerator": 1 - with_eeg},
|
|
"24-36|no_data": {"initial-population": 1, "denominator": 1, "numerator": 1 - with_eeg},
|
|
}
|
|
|
|
|
|
def test_diagnostic_reports_preserve_sex_counts_by_result_and_confirmation():
|
|
rows = [
|
|
{"person_id": "a", "mchat_date": "2025-01-01", "confirmed_date": "2025-05-01",
|
|
"sex": "female", "age_group": "16-23"},
|
|
{"person_id": "b", "mchat_date": "2025-01-01", "sex": "male", "age_group": "16-23"},
|
|
{"person_id": "c", "mchat_date": "2025-01-01", "age_group": "16-23"},
|
|
] # fmt: skip
|
|
period = Period.parse("2025-S1")
|
|
diagnosis = evaluate(MEASURES["M23_PACIENTES_EVALUADOS"], rows, period)
|
|
assert diagnosis.crossed["sex", "category"] == {
|
|
("female", "confirmed"): {"initial-population": 1},
|
|
("male", "in_process"): {"initial-population": 1},
|
|
("no_data", "in_process"): {"initial-population": 1},
|
|
}
|
|
confirmation = evaluate(MEASURES["M24_CONFIRMACION_DIAGNOSTICO"], rows, period)
|
|
assert confirmation.strata["age_sex"] == {
|
|
"16-23|female": {"initial-population": 1, "denominator": 1, "numerator": 1},
|
|
"16-23|male": {"initial-population": 1, "denominator": 1, "numerator": 0},
|
|
"16-23|no_data": {"initial-population": 1, "denominator": 1, "numerator": 0},
|
|
}
|
|
|
|
|
|
def test_registered_prevalence_model_and_report_use_direct_confirmation():
|
|
dbt = Path(DBT_DIR)
|
|
env = Environment(undefined=StrictUndefined, autoescape=False)
|
|
macros = env.from_string(
|
|
(dbt / "macros" / "dates.sql").read_text() + (dbt / "macros" / "text.sql").read_text()
|
|
).module
|
|
sql = env.from_string((dbt / "models/gold/tea/tea_prevalence.sql").read_text()).render(
|
|
var={"tea": dbt_vars()}.__getitem__,
|
|
ref=str,
|
|
add_days=macros.sqlite__add_days,
|
|
sql_list=macros.sql_list,
|
|
)
|
|
with sqlite3.connect(":memory:") as db:
|
|
db.row_factory = sqlite3.Row
|
|
db.executescript("""
|
|
attach ':memory:' as gold;
|
|
create table person (person_id, sex, birth_date, known_at);
|
|
insert into person values ('a', 'female', '2023-02-01', null),
|
|
('b', 'male', '2023-04-01', null), ('c', null, '2023-01-02', null),
|
|
('d', null, date('2025-01-01', '-' || 1126 || ' days'), null);
|
|
create table clinical_note (person_id, note_date, specialty, icd10);
|
|
insert into clinical_note values
|
|
('a', '2025-02-03', 'pediatric_neurology', 'F84.0'),
|
|
('a', '2025-01-03', 'pediatric_neurology', 'F84.0'),
|
|
('b', '2025-01-04', 'psychology', 'F84.0'),
|
|
('b', '2025-01-05', 'pediatric_neurology', 'Z00'),
|
|
('c', '2025-02-01', 'pediatric_neurology', 'F84.0');
|
|
""")
|
|
db.execute(f"create table gold.tea_prevalence as {sql}")
|
|
rows = [dict(row) for row in db.execute(READS["tea"]["prevalence"])]
|
|
assert rows[0]["confirmed_date"] == "2025-01-03"
|
|
assert rows[1]["confirmed_date"] is None
|
|
assert rows[3]["exit_date"] == "2025-01-01"
|
|
|
|
measure = measures(Domain.TEA)["M31_PREVALENCIA"]
|
|
definition = fhir.measure(measure, "tea")
|
|
assert definition.title == "Prevalencia registrada de TEA"
|
|
assert definition.description == measure.description
|
|
assert [s.id for s in definition.group[0].stratifier] == ["category", "sex"]
|
|
period = Period.parse("2025-01")
|
|
result = evaluate(measure, rows, period)
|
|
assert result.populations["initial-population"] == 3
|
|
assert result.strata["category"] == {
|
|
"confirmed": {"initial-population": 1}, "in_population": {"initial-population": 2},
|
|
} # fmt: skip
|
|
crossed = next(s for s in report(measure, period, result, None).group[0].stratifier
|
|
if s.id == "sex-category") # fmt: skip
|
|
assert {
|
|
tuple(component.value.text for component in cell.component): cell.population[0].count
|
|
for cell in crossed.stratum
|
|
} == {("female", "confirmed"): 1, ("male", "in_population"): 1,
|
|
("no_data", "in_population"): 1} # fmt: skip
|