Skip to main content

A Python toolkit for seizure detection evaluation and analysis.

Project description

SeizureKit

Python Version License: MIT PyPI version Build Status

A Python toolkit for the robust evaluation and analysis of seizure detection models.

Overview

SeizureKit provides a suite of tools designed for researchers and developers working on seizure detection algorithms. It simplifies the process of evaluating model performance at both the epoch and event levels, offering standardized metrics, statistical tests, and post-processing utilities.

Key Features

  • Epoch-Level Metrics: Calculate standard classification metrics like F1-score, precision, recall, and specificity on a per-window (epoch) basis.
  • Event-Level Metrics: Evaluate performance based on seizure events, measuring detection latency, F1-score, and other event-based statistics.
  • Post-Processing: Includes temporal smoothing and hysteresis thresholding to improve the stability and accuracy of predictions.
  • Statistical Analysis: Perform statistical significance testing with tools like Bootstrapping and DeLong's test for comparing ROC curves.
  • Visualization (Upcoming): Tools for plotting ground truth, model probabilities, and predictions.

Installation

You can install SeizureKit directly from PyPI (once published):

pip install seizurekit

Alternatively, for the latest development version, you can install it from the source:

git clone https://github.com/Mr-TalhaIlyas/seizurekit.git
cd seizurekit
pip install -e .

Usage

SeizureKit can be used both as a command-line tool for quick evaluations and as a library in your Python projects for more complex workflows.

As a Command-Line Tool

After installation, you can use the seizurekit command to evaluate your model's predictions. You need a ground truth file and a model probabilities file (either .npy or .csv).

# Display help and all available options
seizurekit --help

# Example: Run a full evaluation
seizurekit path/to/ground_truth.npy path/to/model_probabilities.npy --smoothing_window 5 --hyst_high 0.7 --hyst_low 0.3

As a Python Library

Integrate SeizureKit's functions directly into your analysis scripts.

import numpy as np
from seizurekit.eval_epochs import calculate_classification_metrics
from seizurekit.eval_events import calculate_event_level_metrics

# 1. Load or create your data
y_true = np.array([0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1])
y_prob_1 = np.array([0.1, 0.2, 0.8, 0.9, 0.4, 0.3, 0.1, 0.4, 0.85, 0.95, 0.2, 0.1, 0.3, 0.99, 0.98, 0.97, 0.5])
y_prob_2 = np.array([0.05, 0.15, 0.75, 0.85, 0.35, 0.25, 0.05, 0.35, 0.8, 0.9, 0.15, 0.05, 0.25, 0.95, 0.9, 0.92, 0.45])

all_targets = y_true
all_probs = {'my_model': y_prob_1,
              'my_model_2': y_prob_2}
eval_config = {'window_duration': 10, 'detection_tolerance': 30}

# 2. Run epoch-level evaluation
print("--- Epoch-Level Metrics ---")
epoch_results = calculate_classification_metrics(
    all_probs,
    all_targets,
    eval_config,
    smoothing_window=3,
    hysteresis_high=0.5,
    hysteresis_low=0.5
)

# 3. Run event-level evaluation
print("\n--- Event-Level Metrics ---")
event_results = calculate_event_level_metrics(
    all_probs,
    all_targets,
    eval_config,
    smoothing_window=3,
    hysteresis_high=0.5,
    hysteresis_low=0.5
)

Running above code produces output below;

--- Epoch-Level Metrics ---
========================================================================================================================
Modality             Recall (CI)          Precision (CI)       F1-Score (CI)        Specificity NPV        AUROC (CI)           AP (CI)             
------------------------------------------------------------------------------------------------------------------------
my_model             0.941 [0.824,1.000]     0.948 [0.872,1.000]     0.941 [0.820,1.000]   1.000      0.889      1.000 [1.000,1.000]     1.000 [1.000,1.000]
my_model_2           0.882 [0.706,1.000]     0.906 [0.837,1.000]     0.882 [0.706,1.000]   1.000      0.800      1.000 [1.000,1.000]     1.000 [1.000,1.000]
========================================================================================================================

Modality             TN         FP         FN         TP        
my_model             8          0          1          8         
my_model_2           8          0          2          7         
================================================================================

Total Duration: 0.05 hours
Modality             False Alarms/h 
my_model             0.00            FPR/h
my_model_2           0.00            FPR/h
================================================================================

--- Event-Level Metrics ---
========================================================================================================================
Event-Level Detection Metrics:
------------------------------------------------------------------------------------------------------------------------
Modality             | #Events  | TP    | FN    | FP    | Sens   | Lat (s)  | FA/h   | Cov.   | Burden (min/h) 
------------------------------------------------------------------------------------------------------------------------
my_model             | 3        | 3     | 0     | 0     | 1.0000 | 0.0000   | 0.0000 | 0.8889 | 28.2353        
my_model_2           | 3        | 3     | 0     | 0     | 1.0000 | 0.0000   | 0.0000 | 0.7778 | 24.7059        
========================================================================================================================
Coverage(Cov.): Fraction of true seizure time detected by AI.
Burden: Time AI believes seizure is occurring, scaled to minutes/hour.
========================================================================================================================

Contributing

Contributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request.

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/your-feature-name).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add some feature').
  5. Push to the branch (git push origin feature/your-feature-name).
  6. Open a pull request.

License

This project is licensed under the MIT License. You should create a LICENSE file in your project root with the MIT

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

seizurekit-0.1.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

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

seizurekit-0.1.0-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file seizurekit-0.1.0.tar.gz.

File metadata

  • Download URL: seizurekit-0.1.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.20

File hashes

Hashes for seizurekit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4df4e10521902f718559000e6ab3a2ce4d71d814420a140fd4383019b24da8cc
MD5 2fb3655fb96af0c2fe01b7f7c284527f
BLAKE2b-256 b228e153b5fcd31a88c326771ad882ec5ef67aa963fdd8eb062f3d959bb80ccb

See more details on using hashes here.

File details

Details for the file seizurekit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: seizurekit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.20

File hashes

Hashes for seizurekit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52e7e16f75f2735af4ba098fad903e10fe96e9c3a098831fb6d2f54da6661acd
MD5 0d62fc7a38e8205ad7242e965a705801
BLAKE2b-256 0bd3a179d170aaafa405af5a01868039c6d65f2ce392039ac04192d5b3ac2681

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