Files
WebApi/webapi/eeg.py
T
alberto ec5d945f71 Cleanup of the TEA parity work: no prose in code, no hacks, everything in its place
1. No prose in code
- Remove parity-added Python comments and docstrings across the EEG, FHIR,
  execution, preview, training and run code and tests. Preserve endpoint
  descriptions as explicit FastAPI schema metadata.

2. No hacks, no glued code
- Read the declared TEA patient filters and training inputs directly and
  use the same TEA check for exposing and validating reference options.
- Give the Dagster GraphQL request one helper. Keep lock backoff and
  cancellation polling because their timing and error behavior are
  covered by run tests and they handle different Dagster states.
- Use the renamed FHIR report ID helper in the hospital test. Keep
  TEA_GATE_SLOW local to that test and preserve the test assertions.

3. Everything in its place
- Keep endpoint and run handling in WebApi and reuse Core and Persistance
  for domain rules and reads. Leave tests in the existing testpaths.
2026-11-30 13:41:49 -06:00

74 lines
1.8 KiB
Python

"""TEA's EEGs for model governance: every study with its analysis verdict, and one study's
metrics with their age curves' scores."""
from typing import Any
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from persistance import eeg
from persistance.cluster import Execution
from persistance.eeg import Scope
from webapi.runs import dagster, execution_config, submit
router = APIRouter(prefix="/tea/eegs", tags=["eeg"])
class Study(BaseModel):
id: str # person and study date
person_id: str
study_date: str
lab_record_id: str | None
analysed: bool
valid: bool | None
level: str | None
class Metric(BaseModel):
code: str
value: float
z: float | None
outside: float | None
model: str | None
class Analysis(BaseModel):
id: str
valid: bool | None
level: str | None
age_months: float | None
metrics: list[Metric]
checks: list[dict[str, Any]]
regions: list[dict[str, Any]]
class AnalyzeRequest(BaseModel):
scope: Scope = Scope()
execution: Execution | None = None
@router.get("")
def studies() -> list[Study]:
return [Study(**s) for s in eeg.studies()]
@router.post(
"/analyze",
description="Starts `tea_eeg`: analyse the EEGs in scope, score the curves, rebuild gold.",
)
def analyze_eegs(request: AnalyzeRequest, client=Depends(dagster)) -> dict[str, str]:
config = {"scope": request.scope.model_dump(), "execution": execution_config(request.execution)}
return submit(client, "tea_eeg", "tea_analyze_studies", config, domain="tea")
@router.get("/summary")
def eeg_studies_summary() -> dict[str, int]:
return eeg.summary()
@router.get("/{eeg_id}")
def study(eeg_id: str) -> Analysis:
if (found := eeg.study(eeg_id)) is None:
raise HTTPException(404, detail=f"no analysed EEG {eeg_id}")
return Analysis(**found)