Code Monkey home page Code Monkey logo

eeg_real_time_seizure_detection's Introduction

Real-Time Seizure Detection using Electroencephalogram (EEG)

This is the repository for "Real-Time Seizure Detection using EEG: A Comprehensive Comparison of Recent Approaches under a Realistic Setting". You can checkout pdf file of our paper in our github repo.

  • If you have used our code or referred to our result in your research, please cite:
@inproceedings{lee2022real,
  title={Real-Time Seizure Detection using EEG: A Comprehensive Comparison of Recent Approaches under a Realistic Setting},
  author={Lee, Kwanhyung and Jeong, Hyewon and Kim, Seyun and Yang, Donghwa and Kang, Hoon-Chul and Choi, Edward},
  booktitle={Conference on Health, Inference, and Learning},
  pages={311--337},
  year={2022},
  organization={PMLR}
}

Real-Time EEG Detection

We downsample the EEG signal and extract features. The models detect whether ictal / non-ictal signal appears within the 4-second sliding window input. We present an example case with Raw EEG signal but other signal feature extractors can also be applied in the pipeline. concpet

Requirements

To install all the requirements of this repository in your environment, run:

pip install -r requirements.txt

Preprocessing

To construct dataset with TUH EEG dataset, you can download EEG Seizure Corpus in the website and save it to $PATH_TO_EEG and run:

python preproces.py --data_type train --cpu_num *available cpu numbers* --label_type  *tse or tse_bi* --save_directory *path to save preprocessed files* --samplerate *sample rate that you want to re-sample all files*

Before running the code, please change $PATH_TO_EEG/ to your own path to EEG raw dataset.

Model Training

Check our builder/models/detection_models or builder/models/multiclassification repository to see available models for each task. To train the model in default setting, run a command in a format as shown below :

CUDA_VISIBLE_DEVICES=*device number* python ./2_train.py --project-name *folder name to store trained model* --model *name of model to run* --task-type *task*

For sincnet settin, add --sincnet-bandnum 7

Example run for binary seizure detection:

CUDA_VISIBLE_DEVICES=7 python3 ./2_train.py --project-name alexnet_v4_raw --model alexnet_v4 --task-type binary --optim adam --window-size 4 --window-shift 1 --eeg-type bipolar --enc-model raw --binary-sampler-type 6types --binary-target-groups 2 --epoch 8 --batch-size 32 --seizure-wise-eval-for-binary True
CUDA_VISIBLE_DEVICES=7 python3 ./2_train.py --project-name cnn2d_lstm_raw --model cnn2d_lstm_v8 --task-type binary --optim adam --window-size 4 --window-shift 1 --eeg-type bipolar --enc-model raw --binary-sampler-type 6types --binary-target-groups 2 --epoch 8 --batch-size 32 --seizure-wise-eval-for-binary True

Example run for SincNet signal feature extraction :

CUDA_VISIBLE_DEVICES=7 python3 ./2_train.py --project-name alexnet_v4_raw_sincnet --model alexnet_v4 --task-type binary --optim adam --window-size 4 --window-shift 1 --eeg-type bipolar --enc-model sincnet --sincnet-bandnum 7 --binary-sampler-type 6types --binary-target-groups 2 --epoch 8 --batch-size 32 --seizure-wise-eval-for-binary True

Other arguments you can add :

  1. enc-model : preprocessing method to extract features from raw EEG data (options: raw, sincnet, LFCC, stft2, psd2, downsampled) psd2 is for Frequency bands described in our paper stft2 is for short-time fourier transform
  2. seizure-wise-eval-for-binary : perform seizure-wise evaluation for binary task at the end of training if True
  3. ignore-model-summary : does not print model summary and size information if True model summary is measured with torchinfo Please refer to /control/config.py for other arguments and brief explanations.

Model Evaluation

We provide multiple evaluation methods to measure model performance in different perspectives. This command will measure the model's inference time in seconds for one window.

python ./3_test.py --project-name *folder where model is stored* --model *name of model to test* --task-type *task*
python ./4_seiz_test.py --project-name *folder where model is stored* --model *name of model to test* --task-type *task*

Test and measure model speed

To evaluate the model and measure model speed per window using cpu, run the following command :

CUDA_VISIBLE_DEVICES="" python ./3_test.py --project-name *folder where model is stored* --model *name of model to test* --cpu 1 --batch-size 1

For sincnet setting, add --sincnet-bandnum 7 4_seiz_test.py is for evaluation metrics of OVLP, TAES, average latency, and MARGIN

Other arguments you can add :

  1. ignore-model-speed : does not calculate model's inference time per sliding window if True

eeg_real_time_seizure_detection's People

Contributors

mandiehyewon avatar rhksgud92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

eeg_real_time_seizure_detection's Issues

preprocess_info.infopkl

Trying to follow your ReadMe.
In preprocessing script preprocess_info.infopkl is required, which I couldn't find in original TUSZ dataset or your repo.
Could you help me find it?

Traceback (most recent call last):
  File "/path/to/eeg/eeg_real_time_seizure_detection/1_preprocess.py", line 1108, in <module>
    main(args)
  File "/path/to/eeg/eeg_real_time_seizure_detection/1_preprocess.py", line 1017, in main
    with open(data_file_directory + "/preprocess_info.infopkl", "wb") as pkl:
FileNotFoundError: [Errno 2] No such file or directory: '/path/to/data/dataset-tuh_task-binary_datatype-train_v6/preprocess_info.infopkl'

Encountering a problem when running 1_preprocess.py

Dear source code provider:
It seems that every subject will look for a .tse file or a .tse_bi file, but in fact most of the data in the dataset does not have a .tse file, so the following error will be reported in 1_preprocess.py

################ Preprocess begins... ################

8
  0%|                                             | 0/1832 [00:00<?, ?it/s]
multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/home/user/anaconda3/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/mnt/d/script/23-EEG_real_time_seizure_detection/1_preprocess2.py", line 312, in generate_training_data_leadwise_tuh_train_final
    label_file = open(file_name + "." + GLOBAL_DATA['label_type'], 'r') # EX) 00007235_s003_t003.tse or 00007235_s003_t003.tse_bi
FileNotFoundError: [Errno 2] No such file or directory: '/mnt/f/data/04_tuh_eeg/edf/dev/aaaaaajy/s002_2003_07_21/01_tcp_ar/aaaaaajy_s002_t001.tse_bi'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/mnt/d/script/23-EEG_real_time_seizure_detection/1_preprocess2.py", line 869, in <module>
    main(args)
  File "/mnt/d/script/23-EEG_real_time_seizure_detection/1_preprocess2.py", line 827, in main
    run_multi_process(generate_training_data_leadwise_tuh_train_final, edf_list, n_processes=cpu_num)
  File "/mnt/d/script/23-EEG_real_time_seizure_detection/builder/utils/process_util.py", line 13, in run_multi_process
    for r in tqdm(pool.imap_unordered(f, l), total=len(l), ncols=75):
  File "/home/user/anaconda3/lib/python3.8/site-packages/tqdm/std.py", line 1129, in __iter__
    for obj in iterable:
  File "/home/user/anaconda3/lib/python3.8/multiprocessing/pool.py", line 868, in next
    raise value
FileNotFoundError: [Errno 2] No such file or directory: '/mnt/f/data/04_tuh_eeg/edf/dev/aaaaaajy/s002_2003_07_21/01_tcp_ar/aaaaaajy_s002_t001.tse_bi'

Process finished with exit code 1

Why do you make the input to LSTM module has length of 1 at the time dimension?

In the code for many of the decoder models, you have self.agvpool = nn.AdaptiveAvgPool2d((1,1)) (for example, in the builder/models/detector_models/resnet_dilation_lstm.py file at Line 119), which, if I understand it correctly, averages out the output from the CNN module at channel and time dimension so that the output is 1-by-1 at those two dimensions. I can understand that for the channel dimension, but not for the time-series dimension. As this output will then be sent to the LSTM module, the whole point of which is to process time-series signals. If it is of length 1 at time dimension, then why does it need an LSTM? Am I missing anything?

Encountering a problem when running the code

Hi. I hope all is well.

I am working on this code for binary classification and so far, the following problems have caught my attention:

  1. When I execute the following command:

python 1_preprocess.py --data_type train --cpu_num 8 --label_type tse_bi --save_directory "/media/farough/ext/f/pre" --samplerate 200

I get the following error:


1_preprocess.py", line 806, in main

selected_diseases.append(str(disease_labels[i]))

KeyError: 'gnsz'


I made the following change in line 258 to fix it:

parser.add_argument('--disease_type', type=list, default=['bckg', 'seiz'])

so when preprocess start, this information shows in Terminal:

Number of EDF files: 4599

label_list: ['EEG FP1', 'EEG FP2', 'EEG F3', 'EEG F4', 'EEG F7', 'EEG F8', 'EEG C3', 'EEG C4', 'EEG CZ', 'EEG T3', 'EEG T4', 'EEG P3', 'EEG P4', 'EEG O1', 'EEG O2', 'EEG T5', 'EEG T6', 'EEG PZ', 'EEG FZ']

disease_labels: {'bckg': 0, 'seiz': 1}

disease_labels_inv: {0: 'bckg', 1: 'seiz'}

data_file_directory: /media/farough/ext/f/pre/dataset-tuh_task-binary_datatype-train_v6

label_type: tse_bi

feature_type: ['rawsignal']

feature_sample_rate: 50

sample_rate: 200

fsr_sr_ratio: 4

min_binary_slicelength: 30

min_binary_edge_seiz: 3

disease_type: ['bckg', 'seiz']

target_dictionary: {0: 1, 1: 2}

selected_diseases: ['0', '1']

binary_target1: {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1}

binary_target2: {0: 0, 1: 1, 2: 2, 3: 2, 4: 2, 5: 1, 6: 3, 7: 4, 8: 5}

  1. When I want to start train phase, I get the following error:

GitHub/EEG_real_time_seizure_detection/builder/data/data_preprocess.py", line 306, in get_data_preprocessed

patT_idx = (train_data.type_type).index("patT")

ValueError: 'patT' is not in list


And I don’t know why it is happening.

I will be very happy if we can interact with each other. I need the best model of this code, and it seems that this code has some problems with the changes it has had over time.

Best,

Farough

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.