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
  • Convenience pipeline -- spike_sort() for end-to-end spike sorting with inline 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

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

Statistics

  • Online mean/variance -- Welford's algorithm, no buffer required
  • Online covariance -- streaming covariance matrix
  • Connectivity -- coherence and phase locking value (PLV)

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

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.4.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.4.0.tar.gz (410.2 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.4.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.4 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

zpybci-0.4.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.4 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

zpybci-0.4.0-cp38-abi3-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.8+Windows x86-64

zpybci-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

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

zpybci-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.4 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

zpybci-0.4.0-cp38-abi3-macosx_11_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

zpybci-0.4.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (2.7 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.4.0.tar.gz.

File metadata

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

File hashes

Hashes for zpybci-0.4.0.tar.gz
Algorithm Hash digest
SHA256 681183cc59501991b0a428f083c22283fda60dbddb721a2485c90057dd1adef4
MD5 656e9084b435ea9f66594983010870aa
BLAKE2b-256 f621d8f7e7d93977e7471d59db3f44a8c05a3a15ca5cf6717ff8a54007a1d088

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.4.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.4.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zpybci-0.4.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 bec9eca82e55fc169f212043ae5b7a117b42206e531e1875c94c9b4e1610a9e5
MD5 9d926069665dd4f154165a882a401bdd
BLAKE2b-256 7449a9c2eced9c70b6fd424c6456650d3143bef929dda4824b7b7d8fb7360254

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.4.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.4.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zpybci-0.4.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5055b0f2d40a35803584eab657bcc8e82979f652b178821b4a5f7db1151b2278
MD5 9626f318774f1143a16560a5c9bede94
BLAKE2b-256 6f8467e2bf799348eed1947ca491b700957d22747ef5d907a35b064414056d88

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.4.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.4.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: zpybci-0.4.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.3 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.4.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a6ea14d700591a93632381d3a523f8684c267fdfbe4e04f16e170d072a92e1f4
MD5 9896a2dd2cfd3eeb083a97f3db503f12
BLAKE2b-256 4f637ce2b903ecae424d57e69823693bdfb91f5c3775beb3b8b0f19c4ada1dea

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.4.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.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for zpybci-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 beda13236ddb717e58393f10ac1cc9f1e6f377d92432075d73651255e98c298e
MD5 71ced4e0ee4f2e383bdf23492f6e24c7
BLAKE2b-256 7c5b479cafbeda538d9d5d8e2a999e452fc3049a1f0fee7f014684cc7990e694

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.4.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.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for zpybci-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b7af0c495c03213d35b8c06fef188d7688014738f3ead282caafbb81095cdd36
MD5 53a95ccd6dbbf8314a1c0a01d206f5d8
BLAKE2b-256 e5ca92b846e7b76d0831f33b940babbfb2fe5721b978adcb653d4b347bd50278

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.4.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.4.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for zpybci-0.4.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 418d539f7471b4ee302ad1b4798bdc593d5f38814f3cccb58f3cdd115a7a99b2
MD5 1f12536113b9057759b8ca658f6d9d44
BLAKE2b-256 8682711e15b22104310c3eb222310d4015e93ecf7f8f6465e665aae1bb7cbc76

See more details on using hashes here.

File details

Details for the file zpybci-0.4.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.4.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 a628ececf2a35c1d55af95853ef744701826aaf7a4fe397848d9340006035492
MD5 4cdde8b111ef702031aad9639dd240d9
BLAKE2b-256 15e481b72a2dafb070ae1112e1df3aa6172aaa727168b78675c5888b7fb18f93

See more details on using hashes here.

Provenance

The following attestation bundles were made for zpybci-0.4.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