Files
WebApi/webapi/orchestration/eeg.py

30 lines
1.2 KiB
Python

"""TEA's EEG analysis as the cycle runs it: the `tea` library over each stored EDF, before the silver load.
`EEG_ANALYSIS_CLUSTER` says where the batch runs: `auto` (the default: the scheduler in `DASK_SCHEDULER_ADDRESS` if
set, otherwise a local cluster), `local`, `remote`, `sync` (in the cycle's own process), or `off`, which leaves the
analysis out of the cycle (`python -m models tea analyze` and `POST /governance/eeg/run` still run it).
"""
import os
from collections.abc import Callable
from functools import partial
from typing import Any
CLUSTER_ENV = "EEG_ANALYSIS_CLUSTER"
CLUSTERS = ("auto", "local", "remote", "sync")
def cluster() -> str:
value = os.environ.get(CLUSTER_ENV, "auto")
if value not in (*CLUSTERS, "off"):
raise ValueError(f"{CLUSTER_ENV} must be one of {', '.join((*CLUSTERS, 'off'))}, not {value!r}")
return value
def live_eeg_analysis(domain: str) -> Callable[[], Any] | None:
"""The domain's EEG analysis for the cycle, or none: only TEA has one, and `off` leaves it out."""
if domain != "tea" or cluster() == "off":
return None
from models.eeg_analysis import analyze_pending
return partial(analyze_pending, cluster=cluster())