Skip to main content

A Python toolkit for seizure detection evaluation and analysis.

Project description

SeizureKit

Python Version License: MIT PyPI version Badge Downloads

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.

References

MLstatkit

License

MIT LICENSE

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.1.tar.gz (19.8 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.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: seizurekit-0.1.1.tar.gz
  • Upload date:
  • Size: 19.8 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.1.tar.gz
Algorithm Hash digest
SHA256 d35cd19ca35b2d50cc483c3fff617ca83c9534cde114b083b6b4202eda7da9f6
MD5 446f9b092c1d6e4a32e7b50d09d31326
BLAKE2b-256 1babcfbd02c70fc315292cdbb04d3f32a8b041344898bc0f4a9f1f1d7afdcf9e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: seizurekit-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 554a873561565a48d536ef2067ab7c7dc6175282cdc60e0f0a717dae60e1fd02
MD5 421a18e3cce23380c8c6451c771c3c6d
BLAKE2b-256 dc6141cb463005a30d9ed220973ae47bbd2913e76e8cfc6cbc447f94ba7f8549

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