"""Import schema for `core.tea.models.RelacionAdos2`: one association of an EEG alteration in a scalp zone with an ADOS-2 outcome, per fitted model version. An item outcome is an item meaning code (`ADOS2_ITEM_MEANINGS`), the same in every module; a domain outcome is the total calibrated severity score (`ADOS2_CSS`). `rel_evento` is the outcome event as a threshold on its score, machine-readable (`>=1`); it is shown as written, never parsed.""" from typing import Annotated, Optional from pydantic import Field, model_validator from core.tea.enums import MetricaEeg, NivelRelacionAdos2 from core.tea.models import ADOS2_CSS, ADOS2_ITEM_MEANINGS from webapi.imports.schemas.base import ImportSchema from webapi.imports.schemas.coercion import Bounded, Catalog, CatalogValue, OptionalStr, ShortKey, SourceId _NIVEL_CATALOG = Catalog(NivelRelacionAdos2) _BIOMARCADOR_CATALOG = Catalog(MetricaEeg) _RESULTADOS_DOMINIO = {ADOS2_CSS} class RelacionAdos2ImportSchema(ImportSchema): rel_id: SourceId = None # The natural key the import merges model rows on: rel_version, rel_biomarcador, rel_zona, rel_resultado. rel_version: ShortKey = None rel_nivel: CatalogValue(_NIVEL_CATALOG) = None rel_validado: Bounded(int, 0, 1) = None rel_n_cohorte: Bounded(int, 1, None) = None rel_biomarcador: CatalogValue(_BIOMARCADOR_CATALOG) = None rel_zona: ShortKey = None rel_tipo: CatalogValue(_NIVEL_CATALOG) = None rel_resultado: ShortKey = None rel_evento: OptionalStr = None rel_or: Optional[Annotated[float, Field(gt=0)]] = None rel_ic_inf: Bounded(float, 0, None) = None rel_ic_sup: Bounded(float, 0, None) = None rel_q: Bounded(float, 0, 1) = None @model_validator(mode="after") def _outcome_code_and_interval_checks(self): if self.rel_ic_inf is not None and self.rel_ic_sup is not None and self.rel_ic_inf > self.rel_ic_sup: raise ValueError("rel_ic_inf no puede ser mayor que rel_ic_sup") if self.rel_tipo is not None and self.rel_resultado is not None: validos = (set(ADOS2_ITEM_MEANINGS) if self.rel_tipo == NivelRelacionAdos2.ITEM.value else _RESULTADOS_DOMINIO) if self.rel_resultado not in validos: raise ValueError(f"'{self.rel_resultado}' no es un código de {self.rel_tipo} del ADOS-2") return self