Skip to main content

A toolkit for EEG phase estimation

Project description

EEGPhasePy

Tests codecov PyPI version Python versions License: BSD-3-Clause

A toolkit for developing and analyzing real-time and pseudo-real-time EEG phase estimation.

EEGPhasePy includes implementations of various EEG phase estimation algorithms — including Educated Temporal Prediction (ETP) and PHASTIMATE (autoregressive) — alongside offline analysis helpers for evaluating phase estimation experiments. For background on EEG phase estimation and its applications, see Zrenner & Ziemann (2024). More phase estimators will continue to be added in the future.

Features

  • ETP — Educated Temporal Prediction (Shirinpour et al., 2020): uses average interpeak-interval to determine time to next target phase. Similar accuracy to AR with added bonus of being useful for amplifiers that have low packet send rates.
  • PHASTIMATE — Autoregressive phase estimator (Zrenner et al., 2020): uses an AR model to compensate for filter edge effects and extracts instantaneous phase via the Hilbert transform. Estimates ongoing phase rather than predicting.
  • Parameter optimization — Bayesian and genetic optimization (via bayesian-optimization and PyGAD) over AR order and window edge for PHASTIMATE. Improve accuracy of AR model on a participant-by-participant basis.
  • Visualization — Polar phase histograms and average ± std pre/post-trigger waveforms via matplotlib.
  • Supports Python 3.9–3.13.

Installation

pip install eegphasepy

Quick Start

ETP

import numpy as np
import scipy.signal as signal
from EEGPhasePy.estimators import ETP

fs = 5000  # Hz
# Build bandpass filter for the target frequency band (e.g. alpha, 8-12 Hz)
rt_filter = signal.firwin(101, [8, 12], pass_zero=False, fs=fs)
gt_filter = signal.firwin(201, [8, 12], pass_zero=False, fs=fs)

estimator = ETP(
    real_time_filter=rt_filter,
    ground_truth_filter=gt_filter,
    sampling_rate=fs,
    window_len=500,   # ms
    window_edge=40,   # ms
)

# Fit on training data (1D array, at least ~180 s at fs)
estimator.fit(training_data, min_ipi=int(fs / 12))

# Predict next sample at which the target phase (e.g. peak, 0 rad) will occur
next_sample = estimator.predict(current_window, target_phase=0.0)

PHASTIMATE

from EEGPhasePy.estimators import PHASTIMATE

estimator = PHASTIMATE(
    real_time_filter=rt_filter,
    ground_truth_filter=gt_filter,
    sampling_rate=fs,
    window_len=500,
    window_edge=40,
    ar_order=30,
)

# Optionally optimize window_edge and ar_order on training data
estimator.optimize_parameters(training_data, method="bayesian")  # or "genetic"

# At each time step: returns True if current phase matches target within tolerance
is_target = estimator.predict(current_window, target_phase=0, tolerance=5)

Visualization

from EEGPhasePy.viz import plot_polar_histogram, plot_waveform_average

# Polar histogram of triggered phases (radians)
fig = plot_polar_histogram(triggered_phases)
fig.show()

# Average ± std waveform around trigger events
fig = plot_waveform_average(waveform_segments, fs=fs, t_trigger=trigger_sample)
fig.show()

Documentation

Full documentation including API reference, usage guides, and examples is available at eegphasepy.readthedocs.io.

Contributing

Contributions are welcome. Please open an issue or pull request on GitHub.

Citation

If you use EEGPhasePy in your research, please cite the underlying algorithm(s) you end up using:

  • ETP: Shirinpour et al. (2020). Experimental Evaluation of Methods for Real-Time EEG Phase-Specific Transcranial Magnetic Stimulation. PMC8293904
  • PHASTIMATE: Zrenner et al. (2020). The shaky ground truth of real-time phase estimation. PMID 29191438

License

BSD 3-Clause License. See LICENSE for details.

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

eegphasepy-0.0.5.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

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

eegphasepy-0.0.5-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file eegphasepy-0.0.5.tar.gz.

File metadata

  • Download URL: eegphasepy-0.0.5.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for eegphasepy-0.0.5.tar.gz
Algorithm Hash digest
SHA256 d4aa981debf36ec5a89c854514d28c0ad4316ac56529cb9c9f20f8c8ca6bb3f0
MD5 0fb04034efb6c36f842aeb1bed9dda5d
BLAKE2b-256 83d11197a1d228c74cd78e8bd23464ed3d07a923ba5b226121c0eb55eda61de2

See more details on using hashes here.

File details

Details for the file eegphasepy-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: eegphasepy-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for eegphasepy-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0aeeab29ac907ad82f2058eb96c5a7384bb532cb3a4aac8bd66fb1e341854107
MD5 6dd556a1cfdbe1e0d8d1801735c24b7c
BLAKE2b-256 ff7b7cbd12008397c2197298ac9a17cb0a9a8b118af116578b7c422697a7ee3f

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