Files
tea/README.md

98 lines
4.5 KiB
Markdown

# tea
EEG library for the TEA project: EDF in; validation, preprocessing, processes
and biomarkers out. Every biomarker and every process is its own module, and
modules talk only through `tea.contracts`, so any module can be replaced
without touching the others. Architecture plan:
https://claude.ai/artifact/H9Rh19Lv79dVeikeVD9KFZ
## Two implementations
| | `tea` (modern) | `tea.legacy` |
|---|---|---|
| Code | the director's methods on MNE, SciPy, scikit-learn, antropy, fooof, meegkit | the director's code (biomarcadores-tea @ 7dfc641, codigo @ c7a4736) copied unchanged except imports |
| Batches | Dask (`cluster="auto"/"remote"/"local"/"sync"`) | plain single-threaded loop |
| Provenance | written against legacy (equal to float32 precision without acquisition gaps) | each file's header names its source file and commit |
## Modules
| kind | name | requires |
|---|---|---|
| validator | `validation` | — |
| preprocessor | `preprocessing` | — |
| process | `annotation_mask` | — |
| process | `window_selection` | annotation_mask |
| process | `sleep_state` | annotation_mask |
| process | `slow_wave_staging` | sleep_state |
| process | `spindle_detection` | sleep_state (+ slow_wave_staging with `phase`) |
| biomarker | `posterior_rhythm` | sleep_state |
| biomarker | `spindles` | spindle_detection |
| biomarker | `gradient` | sleep_state |
| biomarker | `slow_waves` | slow_wave_staging |
| biomarker | `aperiodic`, `mse` | sleep_state, annotation_mask |
| biomarker | `coupling` | spindle_detection (+ slow_wave_staging with `in_slow_wave`) |
| biomarker | `spindle_topography` | spindle_detection, sleep_state |
| biomarker | `spikes` | sleep_state (modern: + annotation_mask) |
## Use
```python
from tea.validation import validate
bundle, valid = validate("study.edf") # R4B FHIR Bundle, 1 valid / 0 not valid
from tea.pipeline import Pipeline
from tea.preprocessing import DIRECTOR_CLUSTER
pipe = Pipeline.default() # or Pipeline.default(impl="legacy")
study = pipe.study("study.edf", plan=DIRECTOR_CLUSTER, age_months=27.5)
pipe.biomarker("posterior_rhythm").measure(study)
table = pipe.map(paths, plan=DIRECTOR_CLUSTER, ages_months=ages,
biomarkers=("posterior_rhythm", "spindles")) # Dask: LocalCluster, or scheduler=
rhythm = pipe.biomarker("posterior_rhythm").train(table) # baseline: curves or age_blocks
rhythm.predict(table.iloc[[0]]) # outside -1/0/+1 (NaN: no reference), z
from tea.serving import log_plan, log_biomarker # MLflow models, privacy guard
```
```python
from tea.pipeline import registry
assoc = registry.association("ados2_association").fit(cohort) # or impl="legacy"
assoc.associations() # odds ratio per unit of z, interval, p, q; per pair
assoc.score(cohort) # each child's probability of the outcome event
```
Replace a module: `pipe.replace("spindle_detection", MyDetector())`, or register
it under the same name in the `tea.modules` entry-point group. Check it
with `tea.contracts.testing`.
## Associations
Cohort-level models of EEG findings against a clinical outcome run outside the per-study pipeline and register in
`tea.associations` / `tea.legacy_associations`. `ados2_association` relates each zone's z (spike and spindle
densities per scalp zone, `tea.contracts.ZONES`), the posterior rhythm's and the gradient's z with ADOS-2 items
(Firth logistic) or the CSS (ordinal), adjusted for age and module; legacy is the research code vendored unchanged.
The cohort's columns and the outputs are in `tea.associations.ados2.api` and `tea.contracts`.
## Install
Python ≥ 3.12. As a git submodule of another project:
```sh
git submodule add ssh://git@10.0.10.41:2222/alberto/tea.git libs/tea
uv add --editable ./libs/tea # or: pip install -e libs/tea
```
or as a plain dependency: `uv add "tea @ git+ssh://git@10.0.10.41:2222/alberto/tea.git"`.
Extras: `tea[s3]` reads `s3://` paths (endpoint and keys from the environment),
`tea[export]` for the KTLX → EDF exporter.
## Batches
`Pipeline.map(..., cluster=)` runs the modern pipeline as a Dask graph:
`"local"` (a LocalCluster on this machine), `"sync"` (one study at a time, in
this process), `"remote"` with `scheduler="tcp://host:8786"` (any Dask
scheduler you provide), or `"auto"` (`scheduler=` / `DASK_SCHEDULER_ADDRESS`
if set, otherwise local). Legacy always runs a plain single-threaded loop.
A study that fails becomes a row with `error` set; it never stops the batch.
Rows carry the caller's `key`, never file names, paths or dates.