Published September 19, 2024 | Version v1
Software Open

256-channel EEG Signal Compression Using a Convolutional Autoencoder: Binocular Rivalry Use Case

Authors/Creators

Description

This Zenodo upload contains the code used for the purposes of the article Near-lossless EEG Signal Compression Using a Convolutional Autoencoder: Case Study for 256-channel Binocular Rivalry Dataset, published in the Computers in Biology and Medicine journal and available via the link below. Please, cite the linked paper if you want to reference the software made available here.

The source code files are also available via GitHub, where they might be kept in a more up-to-date state (however, any major changes will be published here as new versions). The original raw dataset is not made publicly available, but could be shared upon reasonable request.

 

How to Use

To use this code, you need to first install Python on your computer and optionally some IDE in which you can comfortably interact with the scripts. Specifically, I have used Python version 3.11.4 and the following libraries:

LIBRARY VERSION
matplotlib 3.7.2
mne 1.6.1
numpy 1.25.2
scipy 1.11.2
seaborn 0.13.0
statsmodels 0.14.0
torch 2.1.1+cu121
tqdm 4.66.1

 

With everything prepared, the code should be ready to use. Given that we have performed a case study, the code is not fully generalized, but tailored specifically to the 256-channel binocular rivalry dataset by NIMH. If you want to apply this method to other EEG datasets, some adjustments might be necessary, e.g., changing the way in which the EEG signal is loaded and preprocessed (if it is in some other format), changing the neural network architecture (if the number of channels is different), etc. In the future, we would like to improve upon this code and make it more generally usable, which was out of the scope of the original paper.

The raw dataset used in the article is not made publicly available, but one testing tensor dataset and the pre-trained network parameters are published here on Zenodo, so the ae_compressor.py script should be usable for demonstration purposes by anyone once you download the tensors32_0.pt and params.pt files (alternativelly, you can use the paramsRE.pt with reorder.npy for the UPGMA version). In the ae_compressor.py script, you then add path to the params.pt file when initializing the Compressor class, which will subsequently allow you to perform compression using .compress_dataset(), decompression with .decompress_dataset(), and the evaluation with PRD and RMSE using .evaluate_dataset() (all three methods are parametrized by paths to relevant files). To apply additional lossless folder compression to the compressed state, you need to manually use the 7-Zip software (or any other, but this one was used in the study).

The source code files contain many comments with descriptions of individual steps and parameters used by the defined classes.

 

Description of Files

If we follow the flow of the article, the order in which the files are executed is:

  1. overview.py – loading and extracting the EEG signals using MNE, computing basic characteristics, and visualisation of the signal;
  2. stats_compute.py – computing statistics (correlation, autocorrelation) and saving the results to files;
  3. stats_visualize.py – visualization of statistical analysis results, applying UPGMA and saving the array indices used for reordering the channels during testing;
  4. preprocess.py – splitting EEG signals into chunks and shuffling them, making tensor datasets for training and testing the neural network;
  5. ae_training.py – definition of the CAE (convolutional autoencoder) class and training the network on the original order of channels;
  6. ae_trainingRE.py – same as ae_training.py, but with the reordered channels using UPGMA;
  7. ae_visualize.py – visualization of the loss curve;
  8. ae_compressor.py – definition of the Compressor class, which is used to compress and decompress tensor datasets, as well as to evaluate the compressions using PRD (percentage root mean square difference) and RMSE (root mean square error);
  9. results_calculations.py – calculations and plots of results.

In addition to the GitHub repository, the Zenodo upload contains the following for demonstration purposes:

  1. params.pt – the pre-trained parameters of the CAE;
  2. paramsRE.pt – the pre-trained parameters of the CAE (with reordered channels using UPGMA);
  3. reorder.npy – an array of indices which can be used to reorder the channels based on UPGMA;
  4. tensors32_0.pt – one testing tensor dataset in float32 (used in Table 2 and labeled as No. 1 in other tables).

 

Original Directory Tree

The code contains paths reflecting the file structure used by the author, which can be altered. For clarity, it was as follows (files and folders irrelevant to the code were excluded):

└── pyPROJECT/
    ├── data/
    |   ├── float32/
    |   |   ├── tensors32_0.pt
    |   |   ├── tensors32_1.pt
    |   |   ⋮
    |   |   └── tensors32_9.pt
    |   ├── npy_chunks/
    |   |   ├── subj_01_0.npy
    |   |   ├── subj_01_1.npy
    |   |   ⋮
    |   |   └── subj_25_1274.npy
    |   ├── test/
    |   |   ├── tensors_0.pt
    |   |   ├── tensors_2.pt
    |   |   ⋮
    |   |   └── tensors_9.pt
    |   └── train/
    |   |   ├── tensors_0.pt
    |   |   ├── tensors_2.pt
    |   |   ⋮
    |   |   └── tensors_40.pt
    ├── outputs/
    |   ├── compressed/
    |   |   ├── 0uV/
    |   |   ├── 2uV/
    |   |   ├── 4uV/
    |   |   ├── 6uV/
    |   |   ├── 8uV/
   |   |   ├── 10uV/     | | └── TEMP/ | ├── decompressed/ | ├── autocorrelations.npy | ├── correlations.npy | ├── params.pt | ├── paramsRE.pt | ├── reorder.npy | ├── testlosses.npy | ├── testlossesRE.npy | ├── trainlosses.npy | ├── trainlossesRE.npy | ├── validlosses.npy | └── validlossesRE.npy ├── raw/ | ├── subj_01.mat | ├── subj_02.mat | ⋮ | └── subj_25.mat ├── ae_compressor.py ├── ae_training.py ├── ae_trainingRE.py ├── ae_visualize.py ├── overview.py ├── preprocess.py ├── results_calculations.py ├── stats_compute.py └── stats_visualize.py

Files

Files (2.0 GB)

Name Size Download all
md5:193124784c8823cf5b5f1a8362e9bff8
14.2 kB Download
md5:3f2cecc09b25de89d3386bc01fc6d16e
7.5 kB Download
md5:06dc80aafdeb424b8e9e77169aca82d7
7.6 kB Download
md5:99edd6bd38781ad095e6a6e90ddf067b
3.1 kB Download
md5:a241a38e4f86596cd4cb8ca7ac72588d
2.1 kB Download
md5:a80fa00177da47bcf412d91c0cf74891
512.4 MB Download
md5:efa76f5029ce5fc4876fb6259a385a15
512.4 MB Download
md5:2aa41efbf26972c5382cda0adb12c366
3.6 kB Download
md5:4fa4c68451551c6947835cbbdab9205d
1.2 kB Download
md5:c32f68af169a3666ad5519330d9396c2
5.9 kB Download
md5:ab230479bf58c14f1f9420085f8c1f68
2.1 kB Download
md5:2d3b4219bf318e1c3d96e40a3b1af105
4.4 kB Download
md5:4b1dd02561fd91566e97d23e1a8c87d0
1.0 GB Download

Additional details

Related works

Is supplement to
Journal article: 10.1016/j.compbiomed.2025.109888 (DOI)

Software