Skip to main content

Data loading and preprocessing of EEG scans for seizure-related ML tasks

Project description

build status

To open the road for reproducibility in seizure-related ML tasks, we implement a unified preprocessing library providing the functionality required to extract EEG clips in the format required by many ML algorithms.

The package documentation is available on readthedocs.

This package provides the following functionalities:

  1. Data fetching

  2. Pre-processing of EEG measurements

  3. Creation of clips dataframe, with relevant start and end times

  4. Dataset class, which handles:

    • Data loading

    • Data transforms

The first two steps are handled by seiz_eeg.preprocess, whose parameters can be set in a yaml configuration file, or passed as cli arguments. By default, the module looks for a config.yaml file in the working directory, but another file can be specified with the -c option. A dataset must be specified with the corresponding option, either in the .yaml file, or as follows:

python -m seiz_eeg.preprocess dataset=DATASET
# python -m seiz_eeg.preprocess -c path/to/config.yaml

The creation of clips is provided by seiz_eeg.clips and the Dataset is implemented in seiz_eeg.dataset.

More details on parameters in the Parameters section.

Installation

The code can be pip-installed directly from git, if you have proper authentication. Just run:

pip install git+https://github.com/LTS4/seizure_eeg.git

Otherwise, you can clone the repository and pip install it:

git clone https://github.com/LTS4/seizure_eeg.git
cd seizure_eeg
pip install seizure_eeg

How to use

Download and pre-processing

Data are downloaded to a subfolder of raw_path, declared in the source-specific configuration. Then, with functions which are tailored to different datasets, we pretreat the the data to give them a source agnostic structure.

Segments dataframe

EEG measurements come with little structure. To perform any data-driven task, we shall identify relevant information and organize them. This is generally provided in annotations files, which are separate for each EEG scan. By preliminary reading all of such files, we can create a tabular annotation dataframe, where entries are indexed by patient, session, segment, and channel. The following image shows a sample of such a table for the training split of the TUH Seizure corpus. Thanks to this added structure, it is easy to define clips of interest and quickly retrieve the relevant signals file, which can be read and processed.

Sample of an annotation dataframe

EEG signals

In the usual pre-processing of EEG signals we read raw signals from a .edf file and resample them to the desired rate. Then we extract one clip of interest, e.g. the first seconds of a seizure, and we optionally split it in windows. Those can then be further transformed or fed to a model. Since many clips can be extracted out of the same file, it is convenient to save the resampled signal and avoid repeating expensive operations.

Schema of preprocessing pipeline

Datasets

TUH Seizure corpus

This corpus consists in many hours of labelled EEG sessions. The seiz_eeg.preprocess.tusz module provides code specific to this dataset annotations and EEG measurements.

To download the data, you need to register (free account). You will get a password and a username, which we recommend exporting to environment variables TUSZ_USER and TUSZ_PW. The password shall be included in the config.yaml file, or passed to the command line as follows:

python -m seiz_eeg.preprocess dataset=tusz tusz.user=$TUSZ_USER tusz.password=$TUSZ_PW

If you get a "Permission denied, please try again." message it is probably because your password is wrong.

More information about the TUH seizure corpus can be found on the TUH EEG Corpus website.

Parameters

Many parameters are available for data processing and they shall provided as configuration dataclasses (specified in seiz_eeg.config.py) to our functions.

The minimal parameter needed are - dataset, which specifies the dataset to preprocess. - raw_root, which specifies the root folder where the raw data is stored.

We use OmegaConf to merge .yaml files configuration and cli options in our runnable script (seiz_eeg.preprocess). An example of configuration file for TUH Seizure corpus is provided in config.yaml. The config file, or cli options can provide the following parameters:

config (DataConf)
│
├── dataset (str):                              Abbrv. of dataset to preprocess. Currently supported:
│                                                   - tusz: TUH Seizure Corpus
│                                                   - chbmit: CHB-MIT Scalp EEG Database
│
├── raw_root (str):                             Root folder for raw data (downloads)
│
├── processed_root (str):                       Root folder for preprocessed data
│
├── labels (DataLabelsConf):                    Seizure labels specifications
│   ├── map (Dict[str, int]):                       Map from string seizure codes to integers, e.g. ``bkgd -> 0`` and ``fnsz -> 1``
│   │
│   └── binary (bool):                              Wheter to read binary labels
│
├── signals (DataSignalsConf):                  Options for signals and clips processing
│   ├── diff_channels (bool):                       Wheter to compute channels diffrerences, e.g. "T3-T5", "P4-O2", etc.
│   ├── sampling_rate (int):                        Desired sampling rate, in Hz
│   ├── clip_length (float):                        Lenght of clips to extract, in seconds
│   ├── clip_stride (Union[float, str]):       Stride to extract the start times of the clips.
│   │                                               Integer or real values give explicit stride, in seconds.
│   │                                               If string, must be one of the following:
│   │                                                   - "start": extract one clip per segment, starting at onset/termination label.
│   │
│   ├── window_len (float):                         Lenght of windows to split the clip in in seconds.
│   │                                               If negative no windowing is performed.
│   │
│   ├── fft_coeffs (Optional[List[Optional[int]]]): FFT coefficient interval: *[min_index, max_index]*.
│   │                                               Include all with ``[None]`` or switch off FFT with ``None``.
│   │
│   └── node_level (bool):                          Wheter to work with node-level or global labels
│
└── tusz (DataSourceConf):                      Dataset parameters for TUH Seizure Corpus
    ├── version (str):                              Dataset version
    ├── force_download (bool):                      Download data even if they are already present
    ├── raw (str):                                  Path where to save raw data
    ├── processed (str):                            Path where to save preprocessed data
    ├── subsets (List[str]):                        List of subsets to include in preprocessing (e.g. ``["train", "test"]``)
    └── excluded_patients (Dict[str, List[str]]):   Map from subset to list of patients to exclude from it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seiz_eeg-0.4.3.tar.gz (35.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

seiz_eeg-0.4.3-py3-none-any.whl (40.1 kB view details)

Uploaded Python 3

File details

Details for the file seiz_eeg-0.4.3.tar.gz.

File metadata

  • Download URL: seiz_eeg-0.4.3.tar.gz
  • Upload date:
  • Size: 35.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for seiz_eeg-0.4.3.tar.gz
Algorithm Hash digest
SHA256 f57156fb44b44815c62eb281e780448b93541c7fdb90cf9de72aae58d355eae2
MD5 4e8110daed1a6f7c446069180f5771fa
BLAKE2b-256 c369024eb0bd0d79a840f5338830aeffa4b306cd5ac8205d9c58f041e47449e4

See more details on using hashes here.

File details

Details for the file seiz_eeg-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: seiz_eeg-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 40.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for seiz_eeg-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 971576fecea08fb6687fe59555350f075a393a2919613034ca0f51e8f004864d
MD5 7ea2dc82738c5c8f2371dce18644dd4e
BLAKE2b-256 82d38418aedd216539fa628e9b2c8721f740d2c5f5485a4b2b3a96d11c7355ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page