Skip to main content

High-performance signal processing for BCI research

Project description

zpybci -- Zerostone Python for BCI

High-performance, real-time signal processing for brain-computer interface research. Powered by Rust with zero-copy NumPy integration.

Installation

pip install zpybci

Wheels are available for Linux (x86_64, aarch64), macOS (Intel + Apple Silicon), and Windows (x86_64). Python 3.8+.

Quick Start

import numpy as np
import zpybci as zbci

# Bandpass filter for alpha band (8-12 Hz)
bpf = zbci.IirFilter.butterworth_bandpass(sample_rate=256.0, low_cutoff=8.0, high_cutoff=12.0)
signal = np.random.randn(1000).astype(np.float32)
filtered = bpf.process(signal)

# ICA for artifact removal
ica = zbci.Ica(channels=16, contrast="logcosh")
ica.fit(eeg_data, max_iter=200)
cleaned = ica.remove_components(eeg_data, exclude=[0, 2])  # remove blink/muscle artifacts

# Load an EDF file -- no MNE dependency needed
rec = zbci.read_edf("recording.edf")
ch1 = rec.get_channel("Fp1")
all_data = rec.get_all_channels()  # (n_channels, n_samples) numpy array

Validated Results

Tested on the BCI Competition IV 2a motor imagery benchmark (9 subjects, 4-class, session-to-session transfer):

Pipeline Mean Accuracy Published Baseline
CSP+LDA 40.5% ~40-50%
TS+LDA 64.4% ~60-68%
MDM 59.0% ~55-62%
xDAWN+MDM 57.4% ~55-60%

TS+LDA exceeds the original competition winner (FBCSP, ~63%). All pipelines built entirely with zpybci primitives.

Features

Filters

  • IIR -- Butterworth lowpass, highpass, bandpass (order 2/4/6/8, proper pole placement matching scipy)
  • FIR -- arbitrary-length finite impulse response
  • AC coupling -- DC removal for streaming data
  • Median -- nonlinear smoothing
  • Adaptive -- LMS and NLMS for noise cancellation
  • Notch -- narrowband rejection (e.g., 50/60 Hz line noise)

Spatial Filters

  • CAR -- common average reference
  • Surface Laplacian -- current source density approximation
  • Channel Router -- flexible channel remapping
  • xDAWN -- supervised spatial filters maximizing ERP signal-to-noise ratio

Spectral Analysis

  • FFT -- fast Fourier transform (magnitude/phase)
  • STFT -- short-time Fourier transform
  • Multi-band power -- concurrent power in multiple frequency bands
  • Welch PSD -- power spectral density estimation
  • CWT -- continuous wavelet transform (Morlet)

Independent Component Analysis

  • FastICA -- symmetric parallel extraction with LogCosh, Exp, and Cube contrast functions
  • Artifact removal -- remove blink/muscle/cardiac components and reconstruct clean signal
  • Channel counts -- 4, 8, 16, 32, or 64 channels

Kalman Filter

  • State estimation -- predict/update cycle for real-time decoder smoothing
  • Joseph form -- numerically stable covariance update (preserves positive definiteness)
  • Flexible dimensions -- 8 state/observation combinations from (2,1) to (8,8)

Linear Discriminant Analysis

  • Fisher's LDA -- binary classification with shrinkage regularization
  • Calibrated probabilities -- predict_proba() via sigmoid scaling
  • Feature dimensions -- 2, 4, 6, 8, 12, 16, 32, or 64

Spike Sorting

  • Waveform extraction -- extract spike waveforms around detected events
  • Waveform PCA -- dimensionality reduction for spike clustering
  • Template matching -- classify spikes by Euclidean distance or normalized cross-correlation
  • MAD noise estimation -- robust noise floor estimation for threshold setting
  • Batch detection -- negative-threshold crossing with refractory period
  • Online k-means -- adaptive clustering with auto-create, merge, and prune (OSort-inspired)
  • Convenience pipeline -- spike_sort() for end-to-end spike sorting with online k-means

EDF/EDF+ File Reader

  • read_edf() -- load EDF/EDF+ files without MNE or pyedflib
  • Channel access -- by index or label name
  • Physical units -- automatic digital-to-physical conversion
  • Mixed sample rates -- zero-padded multi-channel extraction

XDF File Reader (Lab Streaming Layer)

  • read_xdf() -- load XDF files from LSL recordings
  • Multi-stream -- numeric and string streams with per-stream metadata
  • Clock offsets -- collection time and offset value pairs
  • Sample formats -- float32, float64, int8, int16, int32, int64, string

Detection

  • Threshold -- fixed-threshold event detection
  • Adaptive threshold -- self-adjusting threshold based on signal statistics
  • Zero-crossing -- rate estimation

Artifact Handling

  • Amplitude-based -- flag samples exceeding a threshold
  • Z-score -- flag statistically outlying segments

Analysis

  • Envelope follower -- instantaneous amplitude via rectification + smoothing
  • Windowed RMS -- streaming root-mean-square
  • Hilbert transform -- analytic signal, instantaneous phase/frequency

Phase-Amplitude Coupling

  • Modulation index -- Tort et al. (2010), KL-divergence from uniform phase distribution
  • Mean vector length -- Canolty et al. (2006), normalized complex coupling strength
  • Comodulogram -- PAC across frequency pairs (bandpass + Hilbert + MI/MVL)
  • Phase-amplitude distribution -- binned amplitude histogram for visualization

Entropy Measures

  • Sample entropy -- template-matching complexity measure (SampEn)
  • Approximate entropy -- regularity statistic with self-matches (ApEn)
  • Spectral entropy -- Shannon entropy of normalized PSD
  • Multiscale entropy -- coarse-grained sample entropy across time scales

Event-Related Spectral Perturbation (ERSP)

  • compute_ersp() -- STFT-based time-frequency decomposition with epoch averaging
  • baseline_normalize() -- dB, z-score, percentage, and log-ratio normalization modes
  • Single-trial mode -- per-epoch time-frequency maps without averaging

Statistics

  • Online mean/variance -- Welford's algorithm, no buffer required
  • Online covariance -- streaming covariance matrix
  • Connectivity -- coherence, phase locking value (PLV), and Granger causality
  • Conditional Granger -- Granger causality conditioned on confound variables

BCI Paradigms

  • Motor imagery -- CSP with online adaptation, sklearn-compatible transformer
  • SSVEP -- CCA-based frequency detection, reference signal generation
  • P300/ERP -- epoch averaging, xDAWN spatial filters

Riemannian Geometry

  • Tangent space -- SPD manifold projection for classification
  • MDM classifier -- minimum distance to mean on SPD manifold
  • Frechet mean -- geometric mean of SPD matrices
  • Riemannian distance -- affine-invariant distance metric
  • Recentering -- domain adaptation via Riemannian transport

Sklearn Integration

  • CSPTransformer -- common spatial patterns as sklearn transformer
  • TangentSpaceTransformer -- Riemannian tangent space projection
  • BandPowerTransformer -- multi-band power feature extraction
  • CovarianceEstimator -- covariance matrix estimation
  • LdaClassifier -- Fisher's LDA with predict_proba and score
  • IcaTransformer -- ICA with component exclusion for artifact removal
  • MdmWrapper -- minimum distance to mean classifier
  • XDawnWrapper -- xDAWN spatial filter transformer
  • Works with make_pipeline(), cross_val_score(), GridSearchCV()

Advanced

  • OASIS deconvolution -- calcium transient inference from fluorescence traces

Utilities

  • Pipeline -- declarative stage chaining with a single process() call
  • Resampling -- integer decimation and interpolation
  • Streaming percentile -- approximate quantiles on unbounded streams
  • Clock sync -- offset estimation, linear drift correction, sample clock alignment
  • Cross-correlation -- full, valid, and circular modes
  • Window functions -- Hann, Hamming, Blackman, flat-top, Kaiser

Version

0.5.0

License

GPL-3.0

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

zpybci-0.6.0.tar.gz (630.5 kB view details)

Uploaded Source

Built Distributions

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

zpybci-0.6.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.6 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

zpybci-0.6.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.6 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

zpybci-0.6.0-cp38-abi3-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.8+Windows x86-64

zpybci-0.6.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

zpybci-0.6.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

zpybci-0.6.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (3.1 MB view details)

Uploaded CPython 3.8+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file zpybci-0.6.0.tar.gz.

File metadata

  • Download URL: zpybci-0.6.0.tar.gz
  • Upload date:
  • Size: 630.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zpybci-0.6.0.tar.gz
Algorithm Hash digest
SHA256 926a73ecc84fd066dd25f47967c5baabdf7a8fc3678314ecb750be1ff8d41147
MD5 721ab7d378c88781182447b87d5140b0
BLAKE2b-256 9601c8ed8ca48e4146f98211c1ecb955737acdf1ddf6b6e07a4e1102430f4f0c

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.6.0.tar.gz:

Publisher: release.yml on fredrikWHaug/zerostone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpybci-0.6.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zpybci-0.6.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c6786248136d351fb096556b84bab200b1785e3525db331a67875226b8399649
MD5 afcbba18b91ad0ef9d97916583b5166a
BLAKE2b-256 f7d9fe382e94e06898194b0da3a0bc1180825f0de7328864dc8e3a9a40464400

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.6.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on fredrikWHaug/zerostone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpybci-0.6.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zpybci-0.6.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a40a2b404c81f812bb4d8874871e7f217cb9e458c7baf6eeb5b13c46a2d5927e
MD5 da408a2cfcb3b67a29f5171a27c6dd4f
BLAKE2b-256 267aa532e7eafd1a29407dbdc21780cc0457fc8736aa002897e1f78a2cefe537

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.6.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on fredrikWHaug/zerostone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpybci-0.6.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: zpybci-0.6.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for zpybci-0.6.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4ec90cec40296fa6e6bfabdb7aa394b2925e350b8bac8e3e6d5858bb0fe254a4
MD5 a8d69b3ad57e1b9a7253e3cd0aba6faa
BLAKE2b-256 6b792754f50cf055f30ad0a2c6ea4999fecc5e436f7e9c872d31f56e26c4b35a

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.6.0-cp38-abi3-win_amd64.whl:

Publisher: release.yml on fredrikWHaug/zerostone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpybci-0.6.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zpybci-0.6.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bbd96439946165d30fa8a404d39f809f0525ad5911c503b6c3faed28eaa139ee
MD5 e8dca9bc673254174e5ae711e64f310c
BLAKE2b-256 eef63ec2a8fba8ee6194838da5ad4b8dfb1d643696386df4a518ee6791f3f159

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.6.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on fredrikWHaug/zerostone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpybci-0.6.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zpybci-0.6.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3f55a7544220df34a89aa39e48541723d0392c00f8033910e6def9285980da83
MD5 07f6a3f9757e5e5937330332539a80bb
BLAKE2b-256 c80d0f38508681bc1914b0a90d2f6a9723be9626a2bacaf8ee81f2a9e0b899c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.6.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on fredrikWHaug/zerostone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file zpybci-0.6.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for zpybci-0.6.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 3deec59044fd21952dcc3762c66fcca7bfdabdc93b197b7a3d20274b56d0fa69
MD5 d339e7ba3e9c4dc98d03067bb7fbe365
BLAKE2b-256 4d626f468907b647a03d839d7e1b605e1f68661df260a20ba6bbeb1dd60a9180

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.6.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on fredrikWHaug/zerostone

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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