Files
WebApi/webapi/eeg.py
T

72 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")
def analyze_eegs(request: AnalyzeRequest, client=Depends(dagster)) -> dict[str, str]:
"""Starts `tea_eeg`: analyse the EEGs in scope, score the curves, rebuild gold."""
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)