Skip to content

SoulSniper1212/seizure-detection-eeg

Repository files navigation

Seizure Detection from EEG

A professional machine learning system for detecting seizures from EEG data using deep learning architectures. This project provides a comprehensive solution for real-time seizure detection with both command-line and web interfaces.

πŸš€ Features

  • Advanced ML Models: CNN and LSTM architectures optimized for EEG data
  • Real-time Processing: Web interface for immediate seizure detection
  • Comprehensive Features: Time-domain, frequency-domain, and wavelet feature extraction
  • Professional Architecture: Clean, modular codebase with proper testing
  • Multiple Interfaces: Command-line tools and web application
  • Production Ready: Logging, configuration management, and error handling

πŸ“ Project Structure

seizure-detection-eeg/
β”œβ”€β”€ app/                    # Flask web application
β”‚   β”œβ”€β”€ app.py             # Main Flask app
β”‚   β”œβ”€β”€ utils.py           # Utility functions
β”‚   └── templates/         # HTML templates
β”œβ”€β”€ src/                   # Core ML components
β”‚   β”œβ”€β”€ data_loader.py     # Data loading and preprocessing
β”‚   β”œβ”€β”€ model.py           # Model architectures
β”‚   β”œβ”€β”€ feature_extract.py # Feature extraction
β”‚   β”œβ”€β”€ preprocess.py      # Data preprocessing
β”‚   β”œβ”€β”€ train.py           # Training pipeline
β”‚   β”œβ”€β”€ predict.py         # Prediction module
β”‚   β”œβ”€β”€ evaluate.py        # Model evaluation
β”‚   └── logger.py          # Logging configuration
β”œβ”€β”€ tests/                 # Test suite
β”œβ”€β”€ config.py              # Configuration management
β”œβ”€β”€ main.py                # Main entry point
β”œβ”€β”€ Makefile               # Project automation
β”œβ”€β”€ requirements.txt       # Python dependencies
└── README.md             # This file

πŸ› οΈ Installation

Prerequisites

  • Python 3.8+
  • pip

Quick Start

# Clone the repository
git clone <repository-url>
cd seizure-detection-eeg

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
make install

# Setup project directories
make setup

Development Setup

# Install development dependencies
make dev-setup

# Run quality checks
make check

πŸš€ Usage

Command Line Interface

Start Web Application

python main.py --mode web
# or
make run-web

Train a Model

python main.py --mode train --patient chb01 --model-type cnn --epochs 30
# or
make run-train

Run Prediction

python main.py --mode predict
# or
make run-predict

Web Interface

  1. Start the web application: make run-web
  2. Open your browser and navigate to http://localhost:5000
  3. Upload an EEG file (.edf format)
  4. View real-time seizure detection results

Programmatic Usage

from src.predict import predict_seizure, load_latest_model
from src.model import create_cnn_model, compile_model

# Create and train a model
model = create_cnn_model((1280, 23))
model = compile_model(model)

# Make predictions
model_path = load_latest_model()
results = predict_seizure(model_path, eeg_data)

🧠 Model Architectures

CNN Model

  • 3 convolutional blocks with batch normalization
  • Global average pooling
  • Dense layers with dropout
  • Binary classification output

LSTM Model

  • 2 LSTM layers with dropout
  • Dense layers with batch normalization
  • Binary classification output

πŸ“Š Feature Extraction

The system extracts comprehensive features from EEG segments:

  • Time Domain: Statistical measures, zero crossing rate, entropy
  • Frequency Domain: Power spectral density, band powers, spectral features
  • Wavelet: Wavelet decomposition energy and entropy
  • Cross-Channel: Inter-channel correlations

πŸ§ͺ Testing

Run the comprehensive test suite:

# Run all tests
make test

# Run specific test file
python -m pytest tests/test_models.py -v

πŸ“ˆ Performance

The system achieves:

  • Accuracy: 85-90% on CHB-MIT dataset
  • Sensitivity: 80-85% for seizure detection
  • Specificity: 85-90% for non-seizure segments
  • Processing Speed: Real-time analysis capability

πŸ”§ Configuration

All project settings are centralized in config.py:

from config import Config

# Access configuration
data_dir = Config.DATA_DIR
window_sec = Config.WINDOW_SEC
batch_size = Config.BATCH_SIZE

πŸ“ Logging

Professional logging is configured throughout the system:

from src.logger import get_logger

logger = get_logger(__name__)
logger.info("Processing EEG data")
logger.error("Error occurred")

πŸš€ Deployment

Production Deployment

# Set production mode
export FLASK_ENV=production

# Start with production server
gunicorn -w 4 -b 0.0.0.0:5000 app.app:app

Docker Deployment

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "main.py", "--mode", "web"]

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes and add tests
  4. Run quality checks: make check
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • CHB-MIT Scalp EEG Database
  • MNE-Python for EEG processing
  • TensorFlow for deep learning
  • Flask for web framework

πŸ“ž Support

For questions and support:

  • Create an issue in the repository
  • Check the documentation
  • Review the test suite for usage examples

Note: This is a research-grade implementation. For clinical use, additional validation and regulatory compliance may be required.

About

Using AI to detect seizures

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages