Metadata-Version: 2.4
Name: rustneuro
Version: 0.3.0
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Typing :: Typed
Requires-Dist: numpy>=1.24
Requires-Dist: pytest>=7 ; extra == 'test'
Requires-Dist: scipy>=1.10 ; extra == 'test'
Provides-Extra: test
License-File: LICENSE
Summary: Fast, validated neural and physiological signal processing (EEG/MEG/iEEG, ECG/HRV, EDA, EMG, fNIRS, spikes, calcium imaging), powered by Rust.
Keywords: neuroscience,eeg,signal-processing,electrophysiology,hrv,fnirs,spikes
Author-email: Yusuf Berdan Guzel <y.guzel@unimelb.edu.au>
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/ybguzel/RustNeuro
Project-URL: Rust API docs, https://docs.rs/rustneuro
Project-URL: Rust crate, https://crates.io/crates/rustneuro

# rustneuro for Python

Fast, validated signal processing for neuroscience and physiology —
EEG/MEG/iEEG/LFP, ECG/HRV, PPG, respiration, EDA, EMG, fNIRS, spikes and
calcium imaging — as a NumPy-friendly Python package powered by the
[rustneuro](https://crates.io/crates/rustneuro) Rust library.

- **Validated.** Every module is tested against the reference outputs of
  SciPy, MNE-Python, mne-connectivity, antropy, NeuroKit2 and tensorpac —
  the same golden fixtures that validate the Rust crate.
- **Fast on multi-channel data.** Functions act along the last axis and
  process channels in parallel with the GIL released.
- **One dependency.** Only NumPy at runtime; wheels ship compiled code, so no
  Rust toolchain is needed.

```bash
pip install rustneuro
```

## Quick start

```python
import numpy as np
import rustneuro as rn

sfreq = 250.0
eeg = np.random.default_rng(0).standard_normal((32, 60 * 250))   # (channels, times)

# MNE-style FIR band-pass and line-noise removal, all channels in parallel
x = rn.filter.filter_data(eeg, sfreq, l_freq=1.0, h_freq=40.0)
x = rn.filter.notch_filter(x, sfreq, [50.0])

# Spectra and features
freqs, psd = rn.spectral.welch(x, sfreq, nperseg=500)              # psd: (32, 251)
alpha = rn.spectral.band_power(freqs, psd, 8.0, 13.0)              # (32,)
fit = rn.spectral.fit_aperiodic(freqs, psd, 2.0, 40.0)             # 1/f offset and exponent

# Complexity, one value per channel
sampen = rn.complexity.sample_entropy(x[:, :2000])                 # (32,)

# Connectivity across epochs (mne-connectivity's estimators)
epochs = x[:, : 58 * 250].reshape(32, 29, 500).transpose(1, 0, 2)  # (epochs, channels, times)
wpli = rn.connectivity.spectral_connectivity(epochs, sfreq, "wpli", fmin=8.0, fmax=13.0, faverage=True)
matrix = wpli.dense()[..., 0]                                      # (32, 32)
```

Heart-rate variability from a raw ECG:

```python
clean = rn.physio.ecg_clean(ecg, sfreq=500.0)
peaks = rn.physio.ecg_peaks(clean, sfreq=500.0)
hrv = rn.physio.hrv(peaks, sfreq=500.0)        # {"MeanNN": ..., "RMSSD": ..., "LF": ..., "SD1": ...}
```

## What's inside

| Module | Highlights | Validated against |
| --- | --- | --- |
| `rn.filter` | `filter_data`, `notch_filter` (MNE conventions); `iirfilter`/`butter`, `sosfiltfilt`, `filtfilt`, `firwin`, `resample_poly`, `resample`, `decimate`, `savgol_filter`, `medfilt` | MNE-Python, SciPy |
| `rn.signal` | `hilbert`, `envelope`, instantaneous phase/frequency, `detrend`, `find_peaks`, `peak_prominences`, `get_window` | SciPy |
| `rn.spectral` | `welch`, `periodogram`, `psd_multitaper`, `csd`, `coherence`, `spectrogram`, `tfr_morlet`, `tfr_power_itc`, `lomb_scargle`, `dpss`; band power, peak/mean/edge frequency, spectral entropy, `fit_aperiodic` | SciPy, MNE-Python, fooof |
| `rn.connectivity` | `spectral_connectivity` (coh, imcoh, plv, ciplv, ppc, pli, pli2_unbiased, dpli, wpli, wpli2_debiased), `envelope_correlation`, `pac` with surrogates | mne-connectivity, tensorpac |
| `rn.complexity` | Hjorth, permutation/spectral/SVD/approximate/sample entropy, Lempel–Ziv, Petrosian/Katz/Higuchi fractal dimension, DFA | antropy, NeuroKit2 |
| `rn.physio` | ECG, PPG, respiration, EDA and EMG cleaning and peaks; time, frequency and non-linear HRV | NeuroKit2 |
| `rn.spikes` | `detect_spikes`, `extract_waveforms`, ISI, CV/CV2/LV, Fano factor, refractory violations, correlograms, spike-triggered average | — (formula tests) |
| `rn.calcium` | `delta_f_over_f`, `oasis_ar1` deconvolution | NumPy, NNLS |
| `rn.fnirs` | `optical_density`, `beer_lambert`, `tddr` motion correction | MNE-Python |

Every function has a docstring naming the reference it reproduces, and the
public API is fully typed (`py.typed`).

## Conventions

- **Time is the last axis.** Signal functions accept any shape
  `(..., n_times)` and return arrays of matching leading shape; reductions
  (entropies, band power, ...) return a NumPy scalar for 1-D input and an
  array of the leading shape otherwise.
- **SciPy and MNE names and defaults** wherever an equivalent exists, with
  sampling frequencies in Hz (`fs`/`sfreq`). Differences are documented in
  each docstring (for example, `find_peaks` returns only the indices).
- **Errors** become `ValueError` (invalid arguments), `RuntimeError`
  (numerical failures) or `OSError`.

## Performance

64 channels × 60 s at 1 kHz on a 10-core Apple Silicon laptop (best of 5):

| Operation | rustneuro | SciPy |
| --- | --- | --- |
| `sosfiltfilt`, 4th-order band-pass | 28 ms | 124 ms |
| `welch`, `nperseg=2048` | 8 ms | 59 ms |
| `resample_poly`, 1/4 | 13 ms | 65 ms |
| `hilbert` | 34 ms | 69 ms |
| `savgol_filter`, window 51 | 20 ms | 45 ms |
| `medfilt`, one channel, kernel 101 | 11 ms | 2 ms |

Most of the gain comes from processing channels in parallel; single-channel
work runs at roughly SciPy's speed, and large-kernel median filtering is
currently slower.

## Building from source

Requires Rust ≥ 1.89 and [maturin](https://www.maturin.rs/):

```bash
cd rustneuro-py
pip install maturin
maturin develop --release
pip install pytest scipy && pytest
```

## License

MIT. Contact: Yusuf Berdan Guzel (y.guzel@unimelb.edu.au).

