Skip to main content

CEO-MOF with expanding statistics

Code for the corrected CEO-MOF experiment (design report: "CEO-MOF With Expanding Statistics: Corrected Experiment Design"). It adds expanding-statistics context to MOW-ECG's MOF input and measures R-peak detection on MIT-BIH.

  • MOW-ECG itself is a faithful port of MOW_ECG_v5.ipynb (R-peak path). Your notebook ships inside the package (ceo_mof/reference/) and verify runs it unchanged with the real pymof to prove the port gives identical R-peaks.
  • Only the MOF input changes between rungs. Preprocessing, windows, candidate search, filtering, merging and evaluation are the same for every rung.
  • Every context feature at time i uses only samples 0..i (checked).

Install

cd ceo-mof-expanding
pip install .            # or: pip install -e .   (editable, for development)

Python 3.9+. Installs numpy, scipy, pandas, wfdb, neurokit2, joblib, numba, pymof. This also gives the command ceo-mof (same as python -m ceo_mof).

Run it: five steps

# 1. data (about 100 MB; skip if you already have the mitdb folder)
ceo-mof download --data-dir mitdb

# 2. verify the pipeline is MOW-ECG                         (~3-5 min)
ceo-mof verify --data-dir mitdb

# 3. pre-run checks on the 6 quick records                   (~3-5 min)
ceo-mof check --data-dir mitdb

#    -> read results_expanding/checks/prerun_checks_zerophase_MLII.txt before step 4

# 4. the experiment: 11 rungs x 48 records x 80 settings      (~45 min on 12 workers)
ceo-mof run --data-dir mitdb --records all --workers 12

# 5. (optional) rebuild the report from saved results
ceo-mof report --data-dir mitdb

PowerShell is the same. The results folder is results_expanding/ (change with --out). Everything is cached per record, so an interrupted run resumes where it stopped.

The rungs

Rung Features (all scaled per window with robust z, except R1) Question
R1 amplitude x (unscaled, = MOW-ECG) baseline
T x, position in window (0 to 1) control: what does a pure time marker do?
A1 x, μ⁽⁰⁾ does the signal's usual level so far help?
A2 x, μ⁽⁰⁾, σ⁽⁰⁾ + its usual spread
A3 x, μ⁽⁰⁾, σ⁽⁰⁾, skewness + the lean of its history
A4 x, μ⁽⁰⁾, σ⁽⁰⁾, skewness, kurtosis + spikiness of its history
B1 x, μ⁽¹⁾, σ⁽¹⁾ history of slopes (1st difference)
B2 x, μ⁽²⁾, σ⁽²⁾ history of curvature (2nd difference)
B3 x, μ⁽³⁾, σ⁽³⁾ history of the 3rd difference
B123 x + all six of B1-B3 all orders together
AB1 x, μ⁽⁰⁾, σ⁽⁰⁾, μ⁽¹⁾, σ⁽¹⁾ closest to Yin Jiang's vector (without time index)

Definitions (ceo_mof/features.py):

  • Backward differences: Δx_i = x_i − x_{i−1}, Δ²x_i = x_i − 2x_{i−1} + x_{i−2}, Δ³x_i = x_i − 3x_{i−1} + 3x_{i−2} − x_{i−3}. The difference values are never given to MOF.
  • μ⁽ʳ⁾_i, σ⁽ʳ⁾_i: expanding mean and population SD of Δʳx over positions r..i (Eqs. 5–6); 0 for i < r.
  • Skewness, kurtosis: standardized central moments of x over 0..i (kurtosis non-excess, normal = 3); 0 where SD = 0.
  • Computed in one streaming pass (Welford/Pébay update), numerically stable over 650,000 samples.
  • The expansion starts at the beginning of the record (open question 1 in the design report).
  • Scaling: robust z per window and per column, (v − median) / (1.4826·MAD), no additive constant; a column with MAD = 0 in a window is set to 0.

Everything else is MOW-ECG: MLII lead, full 30-min records, raw ADC values; zero-phase Butterworth 0.5 Hz + 50 Hz powerline filter; 3 s windows every 2 s; candidate search and RR filter from approx_peaks_v5 / filter_false_positives_rr_outlier_v5; merge from overlapping_window_MOF_PQRST_v5; ±50 ms greedy matching on beat annotations only.

What verify proves

  1. MOF: the vectorised MOF equals pymof (max difference 0) on random data with ties (1, 3 and 8 features) and on real windows, for R1 and for multi-feature rungs.
  2. Port = your notebook: runs overlapping_window_MOF_PQRST_v5 from the shipped notebook with the real pymof and compares R-peaks at IRP 0.6 / MRI 0.3 (the notebook's hard-coded values). Default record 100; add more with --notebook-records 100,203 (about 1 min each).
  3. R1 = the earlier experiments: TP/FP/FN at all 80 IRP/MRI settings against the R1 results of the earlier ladder runs (ceo_mof/reference/r1_previous_grid.csv). This confirms your data, library versions, lead and beat set match the earlier runs.

Tested while building this package on record 100: all three PASS (2272 peaks, TP 2186, FP 86, FN 87; all 80 settings identical).

What check reports (the five pre-run checks)

# Check Pass when
1 Causality: features from samples 0..i only = features at i from the full record (20 random i) bit-equal
2 Formulas vs numpy/scipy at 10 positions relative difference < 1e-8
3 Baseline: amplitude through the scaled path vs R1 same scores (or same detections on all 80 settings)
4 Spread: within-window MAD and range of every feature, unscaled, next to amplitude report only
5 Time link: median |Spearman| between each scaled feature and its position in the window, all windows / first 5 min / rest report only

It also states whether the preprocessing filter uses future samples. (It does: NeuroKit's Butterworth uses sosfiltfilt and the powerline filter uses filtfilt.)

On record 100, checks 4–5 already showed the risk in the design report: inside a 3 s window the expanding features vary by 1e-4 of amplitude or less, and several SD features follow the time position (|Spearman| 0.3–0.5). Rung T is there to tell this apart from real history information.

Outputs (results_expanding/)

checks/verify_zerophase_MLII.txt          verification
checks/prerun_checks_zerophase_MLII.txt   pre-run checks (+ spread_*.csv, time_link_*.csv)
report/zerophase_MLII/report.txt          the results, ready to paste into the report
report/zerophase_MLII/
    totals_global_{thesis,aami}.csv       Se/PPV/DER/F1 per rung at 0.6/0.3
    totals_bestglobal_{thesis,aami}.csv   ... at each rung's own best setting
    per_record_F1_*.csv                   F1 per record
    wilcoxon_thesis.csv                   paired tests (bar: p < 0.005)
    focus_records_F1_*.csv                117, 124, 108, 200, 203, 219, 220, 221
    early_vs_rest_F1_thesis.csv           first 5 minutes vs the rest
    grid_all_records.csv                  TP/FP/FN for every record, rung, setting
scores/, grid/, split/                    per-record cache (safe to delete; resumes)

report.txt matches the empty tables in the design report's Results section.

Options

Option Default Meaning
--records quick (verify/check), all (run) quick, all, or 100,203,...
--rungs all 11 e.g. R1,T,A2,B1
--workers CPU count − 1 parallel records
--filter zerophase causal = forward-only filters (a true data-stream variant; changes R1 too)
--lead MLII or a channel number such as 0 (the notebook uses channel 0; only 114 differs)
--out results_expanding output folder
--notebook-records 100 records for the notebook comparison in verify

Runtime

  • MOF for all 11 rungs: about 0.63 s per window, 903 windows per record, so about 10 minutes per record per core. The 80-setting grid adds about 45 s per record.
  • 48 records: about 45 minutes on 12 workers; about 4–5 hours on Colab's 2 cores.
  • Memory: about 150 MB per worker.

Colab

Open run_in_colab.ipynb. It installs the package, downloads MIT-BIH, and writes results to Google Drive so a disconnect doesn't lose finished records.

Tests

pip install pytest
pytest -q tests

Six fast tests on synthetic data: MOF vs pymof with ties, Eq. 4 differences, expanding formulas and causality, scaling with a constant column, port vs notebook, and matching.

How the code is organised

File Contents
ceo_mof/mow_ecg.py MOW-ECG port: preprocess, windows, find_candidates, filter_candidates, mow_ecg_from_scores, mow_ecg
ceo_mof/mof.py vectorised MOF, bit-identical to pymof
ceo_mof/features.py backward differences, expanding moments, window position, scaling
ceo_mof/experiment.py rungs, scoring, caching, 80-setting grid, parallel run, summaries, report
ceo_mof/checks.py verify and check
ceo_mof/notebook_ref.py runs your notebook's v5 cells as the reference
ceo_mof/evaluate.py ±50 ms matching (as in the notebook), Se/PPV/DER/F1
ceo_mof/data.py MIT-BIH loading, record lists, exclusion sets

Differences from the notebook (none change the R-peaks; verify checks this)

  • IRP and MRI are parameters (the notebook hard-codes 0.6 s and 0.3 s).
  • The notebook's CLCCP/CLCVP/SOD signals and P/Q/S/T detection are not computed: they do not affect R-peaks.
  • No print statements or global variables.
  • MOF scores are computed by the vectorised MOF and passed in, instead of calling pymof inside the window loop.

Metadata

Release files for ceo-mof 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ceo-mof 0.2.0
File Size Uploaded Provenance Actions
ceo_mof-0.2.0.tar.gz 1.9 MB Without provenance Details for ceo_mof-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ceo-mof 0.2.0
File Interpreter ABI Platform Actions
ceo_mof-0.2.0-py3-none-any.whl Python 3 none any Details for ceo_mof-0.2.0-py3-none-any.whl

Total release size: 3.7 MB

Release files / ceo_mof-0.2.0.tar.gz

Download URL ceo_mof-0.2.0.tar.gz
Size 1.9 MB
Tags Source
SHA-256 checksum
How to use checksums
49816d85b181ff7b556e42e8c1dbe438375d9d72592fd90a3a2da6d749094eb7
BLAKE2b-256 checksum
How to use checksums
ac09e04568dc94798d29e2ec6123de94f0502aab18ee5c1a2f0c0e23c92cd30a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / ceo_mof-0.2.0-py3-none-any.whl

Download URL ceo_mof-0.2.0-py3-none-any.whl
Size 1.9 MB
Tags Python 3
SHA-256 checksum
How to use checksums
e3ff69d37d7012ea524057386a1bf0c00aac728cae90b2b8a1dff1d193818528
BLAKE2b-256 checksum
How to use checksums
cd93e0763c554569475b8ba3bda1ea40372bd08cfffa51c9011c536ed0d87433
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for ceo-mof 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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page