Governance TEA EEG: reference cohorts, baselines export and import, cluster health, training and analysis jobs with progress, EDF upload, EEG queries
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
"""Add an EEG recording to TEA through the import pipeline.
|
||||
|
||||
The recording goes through the `edf` connector and the `tea.Eeg` target, the same path an import takes, so the
|
||||
validation, the natural key (`eeg_id_registro_laboratorio`) and the audit trail are the import's own.
|
||||
"""
|
||||
|
||||
import base64
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from persistance.tea import eeg_repository
|
||||
from webapi.imports import audit as import_audit
|
||||
from webapi.imports.services.import_service import ImportRequestError, ImportSource, commit_sources
|
||||
|
||||
TARGET = "tea.Eeg"
|
||||
COLUMNS = {
|
||||
"id_registro_laboratorio": "eeg_id_registro_laboratorio",
|
||||
"fecha_estudio": "eeg_fecha_estudio",
|
||||
"canales": "eeg_canales",
|
||||
"duracion_min": "eeg_duracion_min",
|
||||
"estudio": "eeg_estudio",
|
||||
}
|
||||
|
||||
|
||||
class EdfIntake:
|
||||
"""Registers one EDF file as an EEG, optionally linked to a patient."""
|
||||
|
||||
def add(self, filename: str, raw: bytes, patient_id: int | None = None) -> dict:
|
||||
"""`ImportRequestError` for a file that is not a readable EDF or that the import rejects."""
|
||||
source = {
|
||||
"source_type": "edf",
|
||||
"target": TARGET,
|
||||
"payload": {"file_base64": base64.b64encode(raw).decode("ascii"), "options": {"file_name": filename}},
|
||||
"column_mapping": COLUMNS,
|
||||
"defaults": {} if patient_id is None else {"eeg_pac_id": patient_id},
|
||||
"label": filename,
|
||||
}
|
||||
lab_id = self._lab_id(source)
|
||||
audit = import_audit.ImportAudit("carga", {"on_conflict": "upsert", "merge_duplicates": False})
|
||||
try:
|
||||
(result,) = commit_sources([source], on_conflict="upsert", audit=audit)
|
||||
except Exception as exc:
|
||||
audit.fail(exc, "solicitud_invalida" if isinstance(exc, ImportRequestError) else "fallida")
|
||||
raise
|
||||
else:
|
||||
audit.succeed()
|
||||
finally:
|
||||
import_audit.record(audit)
|
||||
if result.rejected:
|
||||
raise ImportRequestError("; ".join(error["message"] for error in result.errors) or "el EEG fue rechazado")
|
||||
return {"eeg_id": self._eeg_id(lab_id), "lab_id": lab_id, "created": bool(result.created)}
|
||||
|
||||
@staticmethod
|
||||
def _lab_id(source: dict) -> str | None:
|
||||
(row,) = ImportSource.parse(source).rows()
|
||||
return row.get("eeg_id_registro_laboratorio")
|
||||
|
||||
@staticmethod
|
||||
def _eeg_id(lab_id: str | None):
|
||||
if lab_id is None:
|
||||
return None
|
||||
model = eeg_repository.model
|
||||
session = eeg_repository.open_session()
|
||||
try:
|
||||
found = session.execute(
|
||||
select(model.eeg_id).where(model.eeg_id_registro_laboratorio == lab_id)
|
||||
).scalars().first()
|
||||
finally:
|
||||
session.close()
|
||||
return found
|
||||
Reference in New Issue
Block a user