diff --git a/src/tea/scoring/__init__.py b/src/tea/scoring/__init__.py new file mode 100644 index 0000000..558f1b3 --- /dev/null +++ b/src/tea/scoring/__init__.py @@ -0,0 +1,4 @@ +"""Clinical scores computed from a study's recorded items, outside the per-study EEG pipeline.""" +from tea.scoring.ados2 import Ados2Score, ModuleScorer, register, score + +__all__ = ["Ados2Score", "ModuleScorer", "register", "score"] diff --git a/src/tea/scoring/ados2.py b/src/tea/scoring/ados2.py new file mode 100644 index 0000000..8662fec --- /dev/null +++ b/src/tea/scoring/ados2.py @@ -0,0 +1,152 @@ +"""ADOS-2 totals, calibrated severity score (CSS) and classification from the algorithm item scores. + +A module's scorer is a `ModuleScorer` subclass registered with `@register`; adding a module touches nothing else. +Items are named by their code without the table prefix (`sa_a2`, `rrb_d1`); a score of 3 counts as 2, as the +algorithms do. The total is the sum of the items given, so a missing item lowers it. + +The CSS cut points below are approximate values from the published calibrated severity tables (Gotham, Pickles and +Lord 2009; Hus and Lord 2014), kept as data so the licensed tables can replace them without changing the code. Each +row lists the lowest total that reaches CSS 2, 3, ... 10; a total under the first is CSS 1. +""" +from __future__ import annotations + +from abc import ABC, abstractmethod +from collections.abc import Mapping +from dataclasses import dataclass +from typing import ClassVar + +MAX_ALGORITHM_SCORE = 2 +SOCIAL_AFFECT = "sa_" +RESTRICTED_REPETITIVE = "rrb_" + +# Classification from the CSS: from 3 on the child is on the spectrum, from 5 on autism. +CSS_SPECTRUM = 3 +CSS_AUTISM = 5 +NO_SPECTRUM = "NO_ESPECTRO" +SPECTRUM = "ESPECTRO_AUTISTA" +AUTISM = "AUTISMO" +CONCERN_MILD_MODERATE = "PREOCUPACION_LEVE_MODERADA" +CONCERN_SEVERE = "PREOCUPACION_SEVERA" + +MONTHS_PER_YEAR = 12 +FIVE_YEARS = 5 * MONTHS_PER_YEAR +DAYS_PER_MONTH = 30.4375 + + +@dataclass(frozen=True) +class Ados2Score: + """What an evaluation's items give. `css` is empty where the module has none (Toddler) or the table needs an + age or a language level that wasn't given.""" + + social_affect: int + restricted_repetitive: int + total: int + css: int | None + classification: str | None + + +def classify_css(css: int) -> str: + if css < CSS_SPECTRUM: + return NO_SPECTRUM + return SPECTRUM if css < CSS_AUTISM else AUTISM + + +def totals(items: Mapping[str, int | None]) -> tuple[int, int] | None: + """Social affect and restricted-repetitive totals, or none if either domain has no item.""" + sa = [min(v, MAX_ALGORITHM_SCORE) for k, v in items.items() if k.startswith(SOCIAL_AFFECT) and v is not None] + rrb = [min(v, MAX_ALGORITHM_SCORE) for k, v in items.items() + if k.startswith(RESTRICTED_REPETITIVE) and v is not None] + if not sa or not rrb: + return None + return sum(sa), sum(rrb) + + +class ModuleScorer(ABC): + module: ClassVar[str] + + @abstractmethod + def severity(self, total: int, *, age_months: float | None, algorithm: str | None) -> tuple[int | None, str | None]: + """The CSS and the classification of a total, each empty if it can't be given.""" + + +SCORERS: dict[str, ModuleScorer] = {} + + +def register(cls: type[ModuleScorer]) -> type[ModuleScorer]: + SCORERS[cls.module] = cls() + return cls + + +def _css(total: int, cut_points: tuple[int, ...]) -> int: + return 1 + sum(total >= c for c in cut_points) + + +class TableScorer(ModuleScorer): + """A module whose CSS reads a cut-point row chosen by the language level and the age group.""" + + # (algorithm, under five years) -> cut points; `None` matches any. + tables: ClassVar[dict[tuple[str | None, bool | None], tuple[int, ...]]] + + def severity(self, total, *, age_months, algorithm): + under_five = None if age_months is None else age_months < FIVE_YEARS + for key in ((algorithm, under_five), (algorithm, None), (None, under_five), (None, None)): + if key in self.tables: + css = _css(total, self.tables[key]) + return css, classify_css(css) + return None, None + + +@register +class ModuleOne(TableScorer): + module = "MODULO_1" + tables = { + ("Pocas o ninguna palabra", True): (4, 7, 11, 14, 16, 18, 20, 22, 24), + ("Pocas o ninguna palabra", False): (6, 9, 12, 15, 17, 19, 21, 23, 25), + ("Algunas palabras", True): (3, 5, 8, 10, 12, 14, 16, 18, 20), + ("Algunas palabras", False): (4, 6, 9, 11, 13, 15, 17, 19, 21), + } + + +@register +class ModuleTwo(TableScorer): + module = "MODULO_2" + tables = { + (None, True): (3, 5, 7, 9, 10, 12, 14, 16, 18), + (None, False): (3, 6, 8, 10, 11, 13, 15, 17, 19), + } + + +@register +class ModuleThree(TableScorer): + module = "MODULO_3" + tables = {(None, None): (2, 4, 7, 8, 9, 11, 13, 15, 17)} + + +@register +class Toddler(ModuleScorer): + """Ranges of concern by age band instead of a CSS: the lowest total of the mild-to-moderate and of the severe + range, for 12 to 20 months and for 21 to 30.""" + + module = "TODDLER" + bands = ((20, (10, 16)), (30, (8, 14))) + + def severity(self, total, *, age_months, algorithm): + if age_months is None: + return None, None + mild, severe = next((cuts for limit, cuts in self.bands if age_months <= limit), self.bands[-1][1]) + if total >= severe: + return None, CONCERN_SEVERE + return None, CONCERN_MILD_MODERATE if total >= mild else NO_SPECTRUM + + +def score(module: str | None, items: Mapping[str, int | None], *, age_months: float | None = None, + algorithm: str | None = None) -> Ados2Score | None: + """The score of an evaluation, or none when its module isn't one the library knows or it has no items in a + domain. `age_months` is the child's age at the evaluation.""" + scorer = SCORERS.get(module or "") + given = totals(items) + if scorer is None or given is None: + return None + sa, rrb = given + css, classification = scorer.severity(sa + rrb, age_months=age_months, algorithm=algorithm) + return Ados2Score(sa, rrb, sa + rrb, css, classification)