PBstats
From a raw, messy recording to ML-ready features — with a diagnosis, a reproducible recipe, and a scikit-learn model at the end.
PBstats is a signal-aware analysis framework for time series and biosignals (ECG, EEG, EMG, vibration, spectroscopy, sensors). It gives you a fluent, chainable API for cleaning, filtering, transforming, testing and featurising signals. Three things make it different from a bag of SciPy wrappers:
- It tells you what's wrong with your signal.
diagnose()checks for gaps, sensor dropout, clipping, impulse spikes, baseline drift, power-line interference, broadband noise and non-stationarity. It then grades the recording 0–100 and prescribes an ordered cleaning pipeline.auto_clean()applies it. - Your exploration becomes your pipeline. Every chained call is recorded in a structured form, so
d.to_pipeline()turns what you just tried interactively into a saveable, replayablePipeline. Then you can run it on a whole dataset. - It plugs straight into machine learning.
SignalFeatureExtractoris a scikit-learn transformer: raw signals in, ~50 time, spectral, nonlinear, peak and HRV features out. Cleaning happens inside the model, so cross-validation stays honest.
Installation
git clone https://github.com/PallabBiswas3/PEstats.git
cd PEstats
pip install -e . # editable install: your edits take effect immediately
60-second tour
from PBstats import Data
d = Data(raw_ecg, fs=500, label="patient_001")
d.diagnose(verbose=True)
Signal diagnosis — 'patient_001' (15000 samples, 30.0 s @ 500 Hz)
────────────────────────────────────────────────────────────────
Quality █████░░░░░░░░░░░░░░░ 26/100 grade F
────────────────────────────────────────────────────────────────
✖ [critical] Power-line interference at 50 Hz (42 dB above the local background).
▲ [warning ] 60 missing samples (0.4%) in 1 gap(s) — filters and FFTs fail on NaN.
▲ [warning ] 6 impulse spike sample(s) (> 10× both noise level and local slope) — ...
▲ [warning ] 72% of the power is below 0.5 Hz — baseline wander/drift. ...
• [info ] Large DC offset (|mean| = 2.8× std) — dominates the 0 Hz bin of spectra.
────────────────────────────────────────────────────────────────
Prescription (apply with .auto_clean() or report.pipeline):
1. remove_missing(strategy='interpolate')
2. remove_outliers(method='spike', threshold=10.0, window=5)
3. highpass(cutoff=0.5)
4. notch(freq=50.0, harmonics=False)
d.plot_quality() # signal with problem regions highlighted + PSD
d.auto_clean() # 26/100 → 100/100
d.auto_features(include=["time", "freq", "nonlinear", "hrv"])
d.feature_vector.to_series() # ~50 named features
Explore once, replay everywhere
d = (Data(x, fs=500)
.remove_missing()
.bandpass(0.5, 40)
.notch(50)
.standardize())
pipe = d.to_pipeline("ecg_clean") # exactly what you did, with every argument
pipe.save("ecg_clean.json")
cleaned = Pipeline.load("ecg_clean.json").run_batch(
[Data(s, fs=500, label=f"rec_{i}") for i, s in enumerate(dataset)],
on_error="warn",
)
Pipelines compose (clean + features), refuse to call private methods when loaded from JSON, and can run on a copy (pipe.run(d, copy=True)) to leave the input untouched.
Machine learning
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from PBstats import Pipeline, SignalFeatureExtractor
model = make_pipeline(
SignalFeatureExtractor(
fs=250,
include=["time", "freq", "nonlinear"],
preprocess=Pipeline("clean").add("bandpass", low=1, high=40),
n_jobs=-1,
),
RandomForestClassifier(),
)
cross_val_score(model, X_signals, y, cv=5) # X: (n_signals, n_samples) or ragged list
Prefer a table? extract_features(signals, fs, labels=y) returns a DataFrame. For long recordings, d.windowed_features(window=30, step=10) turns one signal into a feature time series. That's the standard input for sleep staging, activity recognition, seizure detection and fault detection.
What's inside
| Area | Highlights |
|---|---|
| Diagnostics | diagnose, auto_clean, plot_quality — 0–100 score, issue list, prescribed Pipeline |
| Cleaning | remove_missing (interpolate / ffill / bfill / mean / median / drop), impute (incl. KNN across channels), remove_outliers (zscore / iqr / mad / hampel / spike), winsorize, clip, crop, remove_dc, detrend (linear / poly) |
| Filters | zero-phase Butterworth lowpass / highpass / bandpass / bandstop, notch (+ harmonics), linear-phase fir_filter, savgol, median_filter, polyphase resample (non-integer rates), plot_filter_response |
| Transforms | amplitude-calibrated fft, Welch psd, stft, wavelet (CWT, log-spaced), hilbert (envelope, phase, inst. frequency) |
| Statistics | describe (incl. RMS, MAD, SNR), normality & stationarity tests, Welch t-test with Cohen's d, Mann-Whitney with rank-biserial r, correlate, FFT autocorrelation, cross_correlation (time-delay estimation), coherence |
| Features | time domain + Hjorth; Welch spectral shape, edges, EEG band powers; sample entropy (KD-tree), permutation entropy, Higuchi fractal dimension, DFA α; peak statistics; HRV (SDNN, RMSSD, pNN50, Poincaré SD1/SD2, LF/HF) with RR-artefact rejection |
| Visualisation | signal, spectrum, PSD with bands, spectrogram, scalogram, envelope, stats panel, ACF, correlation matrix, quality report, 6-panel dashboard — scoped styling that never touches your global matplotlib settings |
Multichannel: pass a 2D array (n_samples, n_channels) or a DataFrame (columns = channels). Cleaning, normalisation and filters work per channel along time. Features are prefixed with the channel name (Fz__td_rms). Use d.channel("Fz") for single-channel transforms.
Design notes
- Every method returns
selffor chaining. Results land on attributes (fft_result,psd_result,quality_report,feature_vector, …). - Transforms that change the domain (
fft,hilbert,stft,wavelet) replaceself.datawith their output so a chain can continue. Analysis methods (psd,describe,diagnose, features) never modify the data. reset()restores the original data and sampling rate, and clears all history and results.copy()gives an independent branch.- Diagnosis is heuristic. Thresholds are documented constants in
PBstats/diagnostics/quality.py. They were tuned on biosignals and validated for zero false alarms on clean synthetic ECG at 250–1000 Hz. If slow content is your signal (e.g. respiration), passdrift_cutoff=None.
Demo and tests
python examples/demo.py # messy ECG → diagnosis → pipeline → features → classifier
python -m pytest # 114 tests
Project structure
PBstats/
core/ Data, Pipeline, step recording, result containers
diagnostics/ signal-quality diagnosis and auto-cleaning
preprocessing/ cleaning, imputation, outliers, normalisation, filters
transforms/ FFT, Welch PSD, STFT, CWT, Hilbert
statistics/ descriptive, hypothesis tests, regression, correlation
features/ pure feature functions + Data feature methods
visualization/ plotting
ml.py scikit-learn transformer and batch feature extraction
Developed with passion for the signal processing community.
Metadata
Release files for PBstats 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pbstats-0.2.0.tar.gz | 67.0 kB | Without provenance | Details for pbstats-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pbstats-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for pbstats-0.2.0-py3-none-any.whl |
Total release size: 137.7 kB
Release files / pbstats-0.2.0.tar.gz
| Download URL | pbstats-0.2.0.tar.gz |
|---|---|
| Size | 67.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4f9d1c090706e1146e6bd9f2e77956086be44cce26bced3dd3acce84d306ed17
|
|
BLAKE2b-256 checksum How to use checksums |
adbc18f85cfe0c5ea9bd18f58872c9aff31f85d13443788e3069f9820fb6939e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.4
|
Release files / pbstats-0.2.0-py3-none-any.whl
| Download URL | pbstats-0.2.0-py3-none-any.whl |
|---|---|
| Size | 70.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
553b55d828e7d1f27c795523f690fdb0bfaadc218cfecb27879bb3eac66e589f
|
|
BLAKE2b-256 checksum How to use checksums |
0845bf70945a9158c74a92a0ad1f95fa0c93e43b5801ce3f078a6f418bfc7ff0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.4
|
Security for PBstats 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.