neuroresp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- neuroresp-0.1.0/LICENSE +21 -0
- neuroresp-0.1.0/PKG-INFO +276 -0
- neuroresp-0.1.0/README.md +242 -0
- neuroresp-0.1.0/pyproject.toml +54 -0
- neuroresp-0.1.0/setup.cfg +4 -0
- neuroresp-0.1.0/src/neuroresp/__init__.py +55 -0
- neuroresp-0.1.0/src/neuroresp/_mechanics.py +724 -0
- neuroresp-0.1.0/src/neuroresp/_sensors.py +268 -0
- neuroresp-0.1.0/src/neuroresp/airflow.py +565 -0
- neuroresp-0.1.0/src/neuroresp/backends/__init__.py +105 -0
- neuroresp-0.1.0/src/neuroresp/backends/afni.py +71 -0
- neuroresp-0.1.0/src/neuroresp/backends/python2d.py +148 -0
- neuroresp-0.1.0/src/neuroresp/batch.py +545 -0
- neuroresp-0.1.0/src/neuroresp/belt.py +327 -0
- neuroresp-0.1.0/src/neuroresp/belt_states.py +268 -0
- neuroresp-0.1.0/src/neuroresp/bids.py +198 -0
- neuroresp-0.1.0/src/neuroresp/breaths.py +222 -0
- neuroresp-0.1.0/src/neuroresp/chpi.py +493 -0
- neuroresp-0.1.0/src/neuroresp/cli.py +185 -0
- neuroresp-0.1.0/src/neuroresp/ctf.py +220 -0
- neuroresp-0.1.0/src/neuroresp/denoise.py +481 -0
- neuroresp-0.1.0/src/neuroresp/fuse.py +277 -0
- neuroresp-0.1.0/src/neuroresp/io.py +154 -0
- neuroresp-0.1.0/src/neuroresp/meg.py +932 -0
- neuroresp-0.1.0/src/neuroresp/motion.py +504 -0
- neuroresp-0.1.0/src/neuroresp/qc.py +145 -0
- neuroresp-0.1.0/src/neuroresp/report.py +838 -0
- neuroresp-0.1.0/src/neuroresp/signal.py +259 -0
- neuroresp-0.1.0/src/neuroresp/simulate.py +829 -0
- neuroresp-0.1.0/src/neuroresp/types.py +90 -0
- neuroresp-0.1.0/src/neuroresp.egg-info/PKG-INFO +276 -0
- neuroresp-0.1.0/src/neuroresp.egg-info/SOURCES.txt +48 -0
- neuroresp-0.1.0/src/neuroresp.egg-info/dependency_links.txt +1 -0
- neuroresp-0.1.0/src/neuroresp.egg-info/entry_points.txt +2 -0
- neuroresp-0.1.0/src/neuroresp.egg-info/requires.txt +12 -0
- neuroresp-0.1.0/src/neuroresp.egg-info/top_level.txt +1 -0
- neuroresp-0.1.0/tests/test_airflow.py +456 -0
- neuroresp-0.1.0/tests/test_backends.py +434 -0
- neuroresp-0.1.0/tests/test_batch.py +333 -0
- neuroresp-0.1.0/tests/test_belt_states.py +289 -0
- neuroresp-0.1.0/tests/test_breaths_belt.py +349 -0
- neuroresp-0.1.0/tests/test_chpi.py +717 -0
- neuroresp-0.1.0/tests/test_denoise_cli.py +572 -0
- neuroresp-0.1.0/tests/test_fuse.py +313 -0
- neuroresp-0.1.0/tests/test_meg.py +376 -0
- neuroresp-0.1.0/tests/test_motion.py +348 -0
- neuroresp-0.1.0/tests/test_realdata_local.py +170 -0
- neuroresp-0.1.0/tests/test_reports.py +209 -0
- neuroresp-0.1.0/tests/test_signal_qc.py +266 -0
- neuroresp-0.1.0/tests/test_simulate.py +649 -0
neuroresp-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shane Lindsay
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
neuroresp-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: neuroresp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Open-source Python toolkit for measuring and modelling human breathing in neuroimaging and physiology
|
|
5
|
+
Author: Shane Lindsay
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/shanelindsay/neuroresp
|
|
8
|
+
Project-URL: Repository, https://github.com/shanelindsay/neuroresp
|
|
9
|
+
Project-URL: Issues, https://github.com/shanelindsay/neuroresp/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/shanelindsay/neuroresp/blob/main/docs/index.md
|
|
11
|
+
Keywords: respiration,breathing,fMRI,MEG,EEG,RETROICOR,tedana,BIDS,neuroimaging,psychophysiology
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: numpy>=1.26
|
|
24
|
+
Requires-Dist: scipy>=1.11
|
|
25
|
+
Requires-Dist: nibabel>=5.0
|
|
26
|
+
Requires-Dist: pandas>=2.0
|
|
27
|
+
Requires-Dist: matplotlib>=3.8
|
|
28
|
+
Provides-Extra: meg
|
|
29
|
+
Requires-Dist: mne>=1.6; extra == "meg"
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
32
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
<p align="center">
|
|
36
|
+
<img src="https://raw.githubusercontent.com/shanelindsay/neuroresp/main/docs/assets/logo.png" alt="neuroresp logo" width="460">
|
|
37
|
+
</p>
|
|
38
|
+
|
|
39
|
+
<p align="center">
|
|
40
|
+
<strong>Open-source Python toolkit for measuring and modelling human breathing in neuroimaging and physiology.</strong>
|
|
41
|
+
</p>
|
|
42
|
+
|
|
43
|
+
<p align="center">
|
|
44
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-0d9488.svg" alt="License: MIT"></a>
|
|
45
|
+
<img src="https://img.shields.io/badge/python-3.11%2B-1e293b.svg" alt="Python 3.11+">
|
|
46
|
+
<img src="https://img.shields.io/badge/BIDS-Physio-2563eb.svg" alt="BIDS Physio">
|
|
47
|
+
</p>
|
|
48
|
+
|
|
49
|
+
<p align="center">
|
|
50
|
+
<a href="docs/index.md">Documentation</a> ·
|
|
51
|
+
<a href="docs/approach.md">Scientific Approach</a> ·
|
|
52
|
+
<a href="docs/simulation.md">Simulation Model</a> ·
|
|
53
|
+
<a href="https://shanelindsay.github.io/explainers/breathing-simulator/">Interactive Simulator</a> ·
|
|
54
|
+
<a href="docs/comparisons.md">Comparisons & Limits</a> ·
|
|
55
|
+
<a href="docs/applications.md">Applications</a> ·
|
|
56
|
+
<a href="docs/usage.md">Usage</a> ·
|
|
57
|
+
<a href="docs/tutorials/index.md">Tutorials</a> ·
|
|
58
|
+
<a href="docs/outputs.md">Outputs & QC</a> ·
|
|
59
|
+
<a href="docs/faq.md">FAQ</a>
|
|
60
|
+
</p>
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
`neuroresp` is a Python toolkit for measuring and modelling human breathing in neuroimaging and physiology studies.
|
|
65
|
+
|
|
66
|
+
- **Measure breathing from peripheral sensors:** Clean pneumatic and dual-band RIP belts (with clipping and loose-strap detection and optional four-state chest-wall segmentation), and extract breath timing, relative pauses, motor shape, and calibrated ventilation from spirometers and nasal cannulas.
|
|
67
|
+
- **Recover breathing from brain scans when belts are missing or clipped:** Extract breathing directly from raw multiband or single-band fMRI slices, fast-TR motion logs, chest ECG in MEG/EEG, and continuous MEG head-position coils, or bridge clipped belt stretches with fMRI breaths from the same scan.
|
|
68
|
+
- **Remove respiratory artefacts from fMRI:** Build fast phase and slow vascular nuisance regressors for `fMRIPrep`, `nilearn`, and post-`tedana` multi-echo pipelines, and strip respiratory field shifts from framewise displacement before volume censoring.
|
|
69
|
+
- **Simulate coupled breathing across sensors:** Generate paired airway, gas, belt, cardiac, MEG, and fMRI recordings from exact analytic cycles, a quiet-rest lung and chest-wall mechanics model, or real calibrated spirometry ([Simulation Model](docs/simulation.md)).
|
|
70
|
+
|
|
71
|
+
Every method relies on signal physics with no training data, writing breath tables, BIDS-style physiology streams, and visual quality reports in one shared format.
|
|
72
|
+
|
|
73
|
+

|
|
74
|
+
|
|
75
|
+
*Figure 1. Sixty-second real-data overlays comparing breathing recovered by `neuroresp` (orange) against physical belts (teal) and exhaled CO₂ (dashed blue) across 7T multiband fMRI (WAND; McNabb et al., 2025), legacy single-band 3T fMRI above the volume Nyquist limit, and single-lead chest ECG (Autonomic Aging dataset; Schumann & Bär, 2022). See [Comparisons & Limits](docs/comparisons.md) for cohort benchmarks and [Outputs & QC](docs/outputs.md#example-qc-and-alignment-figures) for four-panel quality reports.*
|
|
76
|
+
|
|
77
|
+
## Methodological contributions
|
|
78
|
+
|
|
79
|
+
- **Using MRI field shifts as a breathing sensor (`motion`, `fuse`):** Breathing shifts echo-planar images along the phase-encoding axis (Raj et al., 2001; Van de Moortele et al., 2002; Power et al., 2019; Fair et al., 2020). `neuroresp` tracks breathing from that shift at short TRs, removes it from framewise displacement before censoring, and uses it to patch clipped belt stretches from the same scan.
|
|
80
|
+
- **Breath-by-breath sub-TR fMRI extraction without model training (`extract`):** Most fMRI respiratory estimators use neural networks trained on cortical BOLD to predict only slow respiratory variation (RV; Addeh et al., 2025, 2026). Building on multiband slice-stack registration (Hocke & Frederick, 2021), `neuroresp` runs out of the box in pure NumPy and SciPy with no training data or BOLD circularity, recovering individual breath timing and phase alongside slow RV and RVT regressors across both multiband and legacy two-pass single-band scans.
|
|
81
|
+
- **Echo-1 multi-echo extension for `tedana` cleanup (`denoise`):** Multi-echo ICA removes focal motion while leaving fast and slow respiratory T2* variance inside accepted components (Power et al., 2018; DuPre et al., 2021). Applying sub-TR extraction to the nearly BOLD-free first echo (`TE ≈ 10–14 ms`) yields a respiratory trace directly from the scan, which `neuroresp` projects orthogonal to rejected components (Lindquist et al., 2019).
|
|
82
|
+
- **Belt QC, chest-wall states, and direct airflow (`belt`, `airflow`):** Routine belt pipelines often bridge dropouts and clipped sighs, distorting multi-run samples (Power et al., 2020). `neuroresp` separates rail clipping from loose straps, withholds sigh rates when clipping occurs, adds four-state chest-wall segmentation and dual-band RIP summing for DC belts, and defines airflow pauses relative to each breath's peak flow.
|
|
83
|
+
- **ECG and head-coil breathing in MEG and EEG (`meg`):** Heart-rate estimators lose accuracy as respiratory sinus arrhythmia fades with age. `neuroresp` combines heartbeat QRS shape (Moody et al., 1985) with whitened spectral fusion, a physical modulation check, and RSA sign orientation (Yasuma & Hayano, 2004) to keep belt-level rate accuracy in older adults. It also recovers breathing from Elekta `cHPI` and CTF `HLC` head-position coils.
|
|
84
|
+
- **Multi-sensor simulation from one ground truth (`simulate`):** Clinical physiology engines (Pulse, BioGears) use heavy C++ circuits for ventilator modelling, while psychophysiology packages generate simple periodic waves without lung mechanics or scanner physics. In roughly 200 ms of pure NumPy and SciPy, `neuroresp simulate` couples quiet-rest lung and Konno-Mead chest-wall mechanics (or exact analytic cycles, or recorded spirometry via `simulate_sensors_from_flow`) to airflow, nasal pressure, CO2 capnography, sleep thermistors, dual-band RIP, clipped AC belts, thoracic impedance, ECG, PPG, MEG head coils, MRI field shifts, and delayed BOLD CO2 waves ([Simulation Model](docs/simulation.md)).
|
|
85
|
+
|
|
86
|
+
## Commands
|
|
87
|
+
|
|
88
|
+
Choose the command that matches your recording (each workflow is also available from Python):
|
|
89
|
+
|
|
90
|
+
| Input | Command | Purpose |
|
|
91
|
+
| :--- | :--- | :--- |
|
|
92
|
+
| **Synthetic recordings with known breath timing** | [neuroresp simulate](docs/usage.md#simulate-coupled-multi-sensor-breathing) | Generates coupled airway, paired RIP, AC belt, MRI, ECG, and PPG signals (`analytic` or `mechanics` lung model) |
|
|
93
|
+
| **Raw 4D fMRI (multiband or single-band)** | [neuroresp extract](docs/usage.md#extract-breathing-from-raw-fmri) | Recovers sub-TR breathing from slice-group magnetic field shifts |
|
|
94
|
+
| **fMRI or `tedana` multi-echo outputs** | [neuroresp denoise](docs/usage.md#build-confounds-for-fmriprep-and-nilearn) | Builds fast phase and slow vascular regressors, orthogonal to rejected `tedana` components |
|
|
95
|
+
| **Six-parameter motion file** | [neuroresp motion](docs/usage.md#motion-tracking-and-fd-decensoring) | Tracks breathing at short TRs and strips respiratory shifts from framewise displacement |
|
|
96
|
+
| **Respiratory belt or dual-band RIP** | [neuroresp belt](docs/usage.md#hardware-belts) | Flags clipping and loose straps, sums paired RIP bands, and optionally segments four chest-wall states |
|
|
97
|
+
| **Spirometer, pneumotachograph, or nasal cannula** | [neuroresp airflow](docs/usage.md#measure-direct-airway-flow) | Extracts breath landmarks, relative pauses, motor shape, and calibrated ventilation |
|
|
98
|
+
| **Clipped belt + fMRI from the same scan** | [neuroresp fuse](docs/usage.md#hybrid-belt-and-mri-cycle-fusion) | Fills clipped or dropped belt intervals with MRI breaths from the same scan |
|
|
99
|
+
| **MEG or EEG with ECG or head coils (Elekta cHPI, CTF HLC)** | [neuroresp meg](docs/usage.md#recover-breathing-from-meg-and-eeg) | Recovers breathing from QRS morphology, RSA, and continuous head-coil motion |
|
|
100
|
+
| **Full BIDS study** | [neuroresp batch](docs/usage.md#batch-processing-and-slurm-arrays) | Discovers runs, executes locally or on Slurm, and builds cohort QC tables and plots |
|
|
101
|
+
|
|
102
|
+
## Install
|
|
103
|
+
|
|
104
|
+
Requires Python 3.11 or newer. The default fMRI solver uses only NumPy and SciPy with no external binaries.
|
|
105
|
+
|
|
106
|
+
```sh
|
|
107
|
+
python -m pip install neuroresp
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Install `python -m pip install "neuroresp[meg]"` to read raw MEG and EEG files with MNE, or pass `--backend afni` if AFNI `3dvolreg` is installed.
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## Quickstart
|
|
115
|
+
|
|
116
|
+
Explore the lung mechanics and sensor artefacts directly in your browser with the [interactive breathing simulator](https://shanelindsay.github.io/explainers/breathing-simulator/), or generate a coupled synthetic recording locally and analyse the same breaths across airflow, chest expansion, and ECG:
|
|
117
|
+
|
|
118
|
+
```sh
|
|
119
|
+
neuroresp simulate --out demo --duration-s 90 --sigh-times 18 --hold 42:6 --hold-phase peak \
|
|
120
|
+
--belt-clip-quantile 0.92 --loose 68:5 --mri-tr-s 2.0
|
|
121
|
+
neuroresp airflow demo/flow.csv --fs 100 --calibrated --out demo/airflow
|
|
122
|
+
neuroresp belt demo/sub-sim_task-rest_desc-rip_physio.tsv.gz --dc-coupled --states \
|
|
123
|
+
--loose-min-s 0 --out demo/belt
|
|
124
|
+
neuroresp meg demo/ecg.npz --source ecg --out demo/meg
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+

|
|
128
|
+
|
|
129
|
+
*Figure 2. Five coupled sensor views of the same simulated breaths. At the sigh, the AC scanner belt flattens while DC RIP continues rising; during the 6 s peak hold, flow sits at zero and the AC belt decays toward baseline; and during the loose-strap window, only the scanner belt freezes. The lower rows show nonlinear nasal pressure, sub-TR vs. whole-volume MRI sampling, and respiratory modulation of ECG amplitude and beat spacing.*
|
|
130
|
+
|
|
131
|
+
See [Simulation Model](docs/simulation.md) for how the lung core and sensor adapters work, and [Usage](docs/usage.md#simulate-coupled-multi-sensor-breathing) for mechanics presets, real-airflow replay, and additional failure-mode examples.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Recover breathing from raw fMRI (`neuroresp extract`)
|
|
136
|
+
|
|
137
|
+
Every breath changes the amount of air in the lungs and shifts the magnetic field at the head, nudging echo-planar fMRI images back and forth along the phase-encoding axis (Raj et al., 2001; Van de Moortele et al., 2002). Standard motion correction samples that shift only once per volume, which aliases faster breathing whenever TR is 1.4 seconds or longer.
|
|
138
|
+
|
|
139
|
+
`neuroresp extract` aligns slice groups at their native acquisition times instead, sampling the same field shift 8 to 20 times per second in multiband scans (Hocke & Frederick, 2021) or across interleaved passes and slices in legacy single-band scans. For multi-run BIDS cohorts, `neuroresp batch` plans and runs extractions locally or as Slurm job arrays.
|
|
140
|
+
|
|
141
|
+
```sh
|
|
142
|
+
neuroresp extract sub-01_task-rest_echo-1_bold.nii.gz --out derivatives/neuroresp/sub-01/rest
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
This writes a BIDS physiology file (`*_desc-mri_physio.tsv.gz`), a processed waveform (`respiration.csv`), a breath table (`breaths.tsv`), run summaries (`summary.json`), and a four-panel quality report (`qc.png`). Because the sign of the shift depends on scan polarity, distinguishing inhalation from exhalation requires an external reference. Large head movements and unusually regular breathing locked to the repetition rate can also distort the trace. See [Scientific Approach](docs/approach.md#sub-tr-sampling-from-multiband-slice-stacks), [Comparisons & Limits](docs/comparisons.md#measurement-limits), and [Usage](docs/usage.md#extract-breathing-from-raw-fmri).
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Remove breathing noise and motion artefacts (`neuroresp denoise`, `neuroresp motion`)
|
|
150
|
+
|
|
151
|
+
Breathing enters fMRI at two timescales: fast ripples with every breath, and slow cortical blood-flow waves driven by arterial CO₂ 8 to 15 seconds later (Glover et al., 2000; Birn et al., 2008). Multi-echo ICA (`tedana`) removes focal motion cleanly but keeps much of this respiratory variance because CO₂ alters T2* oxygenation much like neural activity (Power et al., 2018; DuPre et al., 2021).
|
|
152
|
+
|
|
153
|
+
The first echo in a multi-echo scan (`TE ≈ 10–14 ms`) carries minimal BOLD contrast while still capturing the magnetic field shift of every breath. Extracting breathing from Echo 1 lets `neuroresp denoise` clean both fast and slow respiratory variance, while projecting motion and breathing regressors orthogonal to rejected `tedana` components so sequential regression cannot reintroduce removed noise (Lindquist et al., 2019).
|
|
154
|
+
|
|
155
|
+

|
|
156
|
+
|
|
157
|
+
*Figure 3. Left: Echo-1 slice stacking tracks direct nasal CO₂ across a TR 2.0 s multi-echo scan where whole-volume motion aliases (OpenNeuro `ds004662`). Right: orthogonal post-`tedana` Echo-1 regression cuts surviving fast breath-phase and slow respiratory variance across 30 resting scans (OpenNeuro `ds006926`).*
|
|
158
|
+
|
|
159
|
+
```sh
|
|
160
|
+
# fMRIPrep / nilearn confounds
|
|
161
|
+
neuroresp denoise derivatives/neuroresp/sub-01/rest/respiration.csv \
|
|
162
|
+
--nifti-in sub-01_task-rest_desc-preproc_bold.nii.gz \
|
|
163
|
+
--out-confounds sub-01_task-rest_desc-resp_confounds.tsv
|
|
164
|
+
|
|
165
|
+
# Post-tedana multi-echo cleanup with joint motion and respiratory orthogonalisation
|
|
166
|
+
neuroresp denoise derivatives/neuroresp/sub-01/rest/respiration.csv \
|
|
167
|
+
--nifti-in desc-denoised_bold.nii.gz --nifti-out desc-respclean_bold.nii.gz \
|
|
168
|
+
--motion motion.1D --tedana-mixing desc-ICA_mixing.tsv --tedana-metrics desc-tedana_metrics.tsv \
|
|
169
|
+
--out-confounds desc-resp_confounds.tsv
|
|
170
|
+
|
|
171
|
+
# Strip respiratory field shifts from framewise displacement before volume censoring
|
|
172
|
+
neuroresp motion decensor Movement_Regressors.txt --tr 0.72 --method bandstop --out fd-rest
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
`neuroresp motion` also tracks breathing directly from fast-TR motion tables (`track`) and separates field shifts from physical nodding across opposite phase-encoding scans (`separate`), supporting AFNI, HCP, fMRIPrep, and SPM motion files. See [Multi-echo ICA and Echo 1](docs/approach.md#multi-echo-ica-and-echo-1), [Motion censoring and head nodding](docs/approach.md#motion-censoring-and-head-nodding), and [Usage](docs/usage.md#clean-tedana-outputs-with-joint-orthogonalisation).
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## Measure breathing from belts and airway sensors (`neuroresp belt`, `neuroresp airflow`)
|
|
180
|
+
|
|
181
|
+
Chest-wall belts and direct airway sensors measure different physical quantities, so `neuroresp` gives each its own command while keeping their outputs compatible with `neuroresp denoise`.
|
|
182
|
+
|
|
183
|
+
- **`neuroresp belt`** cleans pneumatic and RIP belts, separating rail saturation from loose straps before writing breath tables and `respiration.csv`. On calibrated DC-coupled bands, `--states --dc-coupled` segments chest-wall expansion, recoil, and peak or trough dwell, and `--second-column` sums paired chest and abdomen RIP bands for within-person contour and hold comparisons.
|
|
184
|
+
- **`neuroresp airflow`** analyses spirometry, pneumotachography, and nasal-cannula recordings using per-breath relative flow thresholds, keeping pauses and flow shape comparable across changes in gain. Adding `--calibrated` computes tidal volume and both accepted-cycle and observed-time minute ventilation in L/s, while exporting an integrated lung-volume proxy in `respiration.csv` for denoising.
|
|
185
|
+
|
|
186
|
+
```sh
|
|
187
|
+
# Scanner belt QC and cycle timing
|
|
188
|
+
neuroresp belt sub-01_task-rest_physio.tsv.gz --out belt-rest
|
|
189
|
+
|
|
190
|
+
# Calibrated dual-band RIP with four-state chest-wall segmentation
|
|
191
|
+
neuroresp belt rip.csv --fs 100 --column chest --second-column abdomen --states --dc-coupled --out rip-states
|
|
192
|
+
|
|
193
|
+
# Direct airway flow and nasal pressure
|
|
194
|
+
neuroresp airflow cannula.csv --column pressure --fs 6 --sensor nasal_pressure --out nasal-rest
|
|
195
|
+
neuroresp airflow spirometry.csv --column flow --fs 100 --calibrated --out spirometry-rest
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
See [Hardware belts](docs/usage.md#hardware-belts), [Measure direct airway flow](docs/usage.md#measure-direct-airway-flow), and [Chest-wall movement versus airway timing](docs/comparisons.md#chest-wall-movement-versus-airway-timing).
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## Repair clipped scanner belts with fMRI (`neuroresp fuse`)
|
|
203
|
+
|
|
204
|
+
Scanner belts often hit their digital ceiling during deep sighs or slip loose mid-scan. `neuroresp fuse` keeps clean belt cycles inside the scan window and fills saturated or dropped stretches with complete MRI cycles from the same scan. Each sensor switch starts a new segment so breath-to-breath variability stays within one sensor, and sigh counts are withheld whenever belt clipping occurs.
|
|
205
|
+
|
|
206
|
+
```sh
|
|
207
|
+
neuroresp fuse belt-rest derivatives/neuroresp/sub-01/rest --out fused-rest
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+

|
|
211
|
+
|
|
212
|
+
*Figure 4. Raw 400 Hz abdominal belt (teal, left axis) alongside 3T fMRI phase-encoding shift (orange, right axis, TR 0.72 s) from a Human Connectome Project resting scan (Van Essen et al., 2013). During the sigh, the belt flattens at its 4095 upper rail (shaded) and then hits its 0 lower rail while the fMRI shift continues tracking the full excursion.*
|
|
213
|
+
|
|
214
|
+
See [Hybrid belt and MRI cycle fusion](docs/usage.md#hybrid-belt-and-mri-cycle-fusion) and [Outputs](docs/outputs.md#command-specific-outputs).
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## Recover breathing in MEG and EEG (`neuroresp meg`)
|
|
219
|
+
|
|
220
|
+
Most MEG and EEG studies record chest ECG without a breathing belt. Heart-rate variability estimators weaken with age, whereas chest expansion keeps tilting the heart relative to the electrodes on every beat (Moody et al., 1985). `neuroresp meg` recovers breathing from beat-by-beat QRS amplitude and area, strips slow vasomotor drift, leaves beat dropouts as gaps, and combines morphology with respiratory sinus arrhythmia (RSA; Yasuma & Hayano, 2004) for rate and approximate inspiratory sign. Built-in checks reject unmodulated ECG traces and flag runs where breathing rate shifts substantially mid-recording.
|
|
221
|
+
|
|
222
|
+

|
|
223
|
+
|
|
224
|
+
*Figure 5. Left: ECG-derived breathing vs. a physical chest belt and heart-rate-only estimation in a 68-year-old adult. Right: rate concordance against physical chest belts across young adults, older adults, and one-week retest sessions in the PhysioNet Autonomic Aging dataset (51 adults, 98 resting runs; Schumann & Bär, 2022).*
|
|
225
|
+
|
|
226
|
+
```sh
|
|
227
|
+
neuroresp meg sub-01_task-rest_meg.fif --ecg-ch ECG001 --out derivatives/neuroresp/sub-01/meg --prefix sub-01_task-rest
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Raw MEG recordings with continuous head localisation also capture breathing through small nodding and front-to-back head movements. Elekta/MEGIN `.fif` files supply active head-position indicator coils (`cHPI`), and CTF `.ds` datasets supply the nine `HLC` fiducial channels, which `neuroresp` converts to the same head poses. Pass `--source chpi` for head-motion extraction, or `--source both` to export both trackers and orient the mechanical trace using ECG.
|
|
231
|
+
|
|
232
|
+

|
|
233
|
+
|
|
234
|
+
*Figure 6. Mechanical head-coil motion (`cHPI`) aligned against independent ECG-derived respiration in a real human MEG recording (OpenNeuro `ds008861`).*
|
|
235
|
+
|
|
236
|
+
See [ECG shape and heart rate in MEG and EEG](docs/approach.md#ecg-shape-and-heart-rate-in-meg-and-eeg), [Comparisons & Limits](docs/comparisons.md#benchmarks), and [Usage](docs/usage.md#recover-breathing-from-meg-and-eeg) for validation tables, `.npz` inputs, and polarity flags.
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Documentation
|
|
241
|
+
|
|
242
|
+
- [Overview and Workflow Selector](docs/index.md)
|
|
243
|
+
- [Installation](docs/installation.md)
|
|
244
|
+
- [Scientific Approach](docs/approach.md)
|
|
245
|
+
- [Simulation Model](docs/simulation.md)
|
|
246
|
+
- [Interactive Breathing Simulator (Web)](https://shanelindsay.github.io/explainers/breathing-simulator/)
|
|
247
|
+
- [Method Comparisons and Measurement Limits](docs/comparisons.md)
|
|
248
|
+
- [Research Applications](docs/applications.md)
|
|
249
|
+
- [Usage (CLI & Python API)](docs/usage.md)
|
|
250
|
+
- [Tutorials (Runnable Quarto Walkthroughs)](docs/tutorials/index.md)
|
|
251
|
+
- [Outputs and Quality Control](docs/outputs.md)
|
|
252
|
+
- [FAQ](docs/faq.md)
|
|
253
|
+
|
|
254
|
+
## Key References
|
|
255
|
+
|
|
256
|
+
- **Sub-TR fMRI extraction (`extract`)**: Raj, Anderson, & Gore (2001, *Phys. Med. Biol.*); Van de Moortele et al. (2002, *Magn. Reson. Med.*); Hocke & Frederick (2021, *Magn. Reson. Med.*, 85(1), 309–315).
|
|
257
|
+
- **BOLD denoising and multi-echo ICA (`denoise`)**: Glover, Li, & Ress (2000, *Magn. Reson. Med.*); Birn et al. (2006, 2008, *NeuroImage*); Chang & Glover (2009, *NeuroImage*); Kundu et al. (2012, *NeuroImage*); Power et al. (2018, *PNAS*); Lindquist et al. (2019, *Hum. Brain Mapp.*); Lynch et al. (2020, *Cell Rep.*); DuPre et al. (2021, *JOSS*); Moia et al. (2021, *NeuroImage*); Kovářová & Mikl (2025, *Hum. Brain Mapp.*).
|
|
258
|
+
- **Respiratory motion and FD decensoring (`motion`, `fuse`)**: Power et al. (2019, *NeuroImage*); Fair et al. (2020, *NeuroImage*); Gratton et al. (2020, *NeuroImage*); Pipoly et al. (2026, *Hum. Brain Mapp.*); Sadil et al. (2026, *bioRxiv*).
|
|
259
|
+
- **ECG-derived respiration (`meg`)**: Moody et al. (1985, *Comput. Cardiol.*); Yasuma & Hayano (2004, *Chest*).
|
|
260
|
+
- **Respiratory mechanics and multi-sensor simulation (`simulate`)**: Otis et al. (1956, *J. Appl. Physiol.*); Konno & Mead (1967, *J. Appl. Physiol.*); Bates (2009, *Lung Mechanics*); Albanese et al. (2016, *Am. J. Physiol. Heart Circ. Physiol.*); Charlton et al. (2017, *Physiol. Meas.*).
|
|
261
|
+
- **Related image-based comparators**: Aslan, Hocke, Schwarz, & Frederick (2019, *NeuroImage*, `happy`); Jaffray et al. (2024, *Imaging Neurosci.*); Addeh et al. (2025, *Magn. Reson. Med.*; 2026, *Imaging Neurosci.*).
|
|
262
|
+
|
|
263
|
+
## Validation Datasets & Acknowledgements
|
|
264
|
+
|
|
265
|
+
Computational work was supported by the Viper and Vardent high-performance computing clusters at the University of Hull. Benchmark figures and validation tables were evaluated on open-access recordings from the following datasets:
|
|
266
|
+
|
|
267
|
+
- McNabb, C., Driver, I., Hyde, V., et al. (2025). WAND: A multi-modal dataset integrating advanced MRI, MEG, and TMS for multi-scale brain analysis. *Scientific Data*. https://doi.org/10.1038/s41597-024-04154-7
|
|
268
|
+
- Nooner, K. B., Colcombe, S. J., Tobe, R. H., et al. (2012). The NKI-Rockland Sample: A model for accelerating the pace of discovery science in psychiatry. *Frontiers in Neuroscience*. https://doi.org/10.3389/fnins.2012.00152
|
|
269
|
+
- Schumann, A., & Bär, K.-J. (2022). Autonomic aging: A dataset to quantify changes of cardiovascular autonomic function during healthy aging. *Scientific Data*. https://doi.org/10.1038/s41597-022-01202-y
|
|
270
|
+
- Van Essen, D. C., Smith, S. M., Barch, D. M., et al. (2013). The WU-Minn Human Connectome Project: An overview. *NeuroImage*. https://doi.org/10.1016/j.neuroimage.2013.05.041 *(Data were provided in part by the Human Connectome Project, WU-Minn Consortium, Principal Investigators David Van Essen and Kamil Ugurbil, 1U54MH091657, funded by the 16 NIH Institutes and Centers supporting the NIH Blueprint for Neuroscience Research and by the McDonnell Center for Systems Neuroscience at Washington University.)*
|
|
271
|
+
- Wakeman, D. G., & Henson, R. N. (2015). A multi-subject, multi-modal human neuroimaging dataset. *Scientific Data*. https://doi.org/10.1038/sdata.2015.1
|
|
272
|
+
- Additional OpenNeuro multi-echo, cortico-spinal, and MEG recordings (`ds004662`, `ds006926`, `ds008861`, `ds005883`) and calibrated spirometry/RIP recordings used for airflow and belt-state benchmarks (see [Comparisons & Limits](docs/comparisons.md)).
|
|
273
|
+
|
|
274
|
+
## Citation & License
|
|
275
|
+
|
|
276
|
+
Please cite [CITATION.cff](CITATION.cff) alongside the methodological papers above when using `neuroresp` in published work. Released under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/shanelindsay/neuroresp/main/docs/assets/logo.png" alt="neuroresp logo" width="460">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<strong>Open-source Python toolkit for measuring and modelling human breathing in neuroimaging and physiology.</strong>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-0d9488.svg" alt="License: MIT"></a>
|
|
11
|
+
<img src="https://img.shields.io/badge/python-3.11%2B-1e293b.svg" alt="Python 3.11+">
|
|
12
|
+
<img src="https://img.shields.io/badge/BIDS-Physio-2563eb.svg" alt="BIDS Physio">
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<a href="docs/index.md">Documentation</a> ·
|
|
17
|
+
<a href="docs/approach.md">Scientific Approach</a> ·
|
|
18
|
+
<a href="docs/simulation.md">Simulation Model</a> ·
|
|
19
|
+
<a href="https://shanelindsay.github.io/explainers/breathing-simulator/">Interactive Simulator</a> ·
|
|
20
|
+
<a href="docs/comparisons.md">Comparisons & Limits</a> ·
|
|
21
|
+
<a href="docs/applications.md">Applications</a> ·
|
|
22
|
+
<a href="docs/usage.md">Usage</a> ·
|
|
23
|
+
<a href="docs/tutorials/index.md">Tutorials</a> ·
|
|
24
|
+
<a href="docs/outputs.md">Outputs & QC</a> ·
|
|
25
|
+
<a href="docs/faq.md">FAQ</a>
|
|
26
|
+
</p>
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
`neuroresp` is a Python toolkit for measuring and modelling human breathing in neuroimaging and physiology studies.
|
|
31
|
+
|
|
32
|
+
- **Measure breathing from peripheral sensors:** Clean pneumatic and dual-band RIP belts (with clipping and loose-strap detection and optional four-state chest-wall segmentation), and extract breath timing, relative pauses, motor shape, and calibrated ventilation from spirometers and nasal cannulas.
|
|
33
|
+
- **Recover breathing from brain scans when belts are missing or clipped:** Extract breathing directly from raw multiband or single-band fMRI slices, fast-TR motion logs, chest ECG in MEG/EEG, and continuous MEG head-position coils, or bridge clipped belt stretches with fMRI breaths from the same scan.
|
|
34
|
+
- **Remove respiratory artefacts from fMRI:** Build fast phase and slow vascular nuisance regressors for `fMRIPrep`, `nilearn`, and post-`tedana` multi-echo pipelines, and strip respiratory field shifts from framewise displacement before volume censoring.
|
|
35
|
+
- **Simulate coupled breathing across sensors:** Generate paired airway, gas, belt, cardiac, MEG, and fMRI recordings from exact analytic cycles, a quiet-rest lung and chest-wall mechanics model, or real calibrated spirometry ([Simulation Model](docs/simulation.md)).
|
|
36
|
+
|
|
37
|
+
Every method relies on signal physics with no training data, writing breath tables, BIDS-style physiology streams, and visual quality reports in one shared format.
|
|
38
|
+
|
|
39
|
+

|
|
40
|
+
|
|
41
|
+
*Figure 1. Sixty-second real-data overlays comparing breathing recovered by `neuroresp` (orange) against physical belts (teal) and exhaled CO₂ (dashed blue) across 7T multiband fMRI (WAND; McNabb et al., 2025), legacy single-band 3T fMRI above the volume Nyquist limit, and single-lead chest ECG (Autonomic Aging dataset; Schumann & Bär, 2022). See [Comparisons & Limits](docs/comparisons.md) for cohort benchmarks and [Outputs & QC](docs/outputs.md#example-qc-and-alignment-figures) for four-panel quality reports.*
|
|
42
|
+
|
|
43
|
+
## Methodological contributions
|
|
44
|
+
|
|
45
|
+
- **Using MRI field shifts as a breathing sensor (`motion`, `fuse`):** Breathing shifts echo-planar images along the phase-encoding axis (Raj et al., 2001; Van de Moortele et al., 2002; Power et al., 2019; Fair et al., 2020). `neuroresp` tracks breathing from that shift at short TRs, removes it from framewise displacement before censoring, and uses it to patch clipped belt stretches from the same scan.
|
|
46
|
+
- **Breath-by-breath sub-TR fMRI extraction without model training (`extract`):** Most fMRI respiratory estimators use neural networks trained on cortical BOLD to predict only slow respiratory variation (RV; Addeh et al., 2025, 2026). Building on multiband slice-stack registration (Hocke & Frederick, 2021), `neuroresp` runs out of the box in pure NumPy and SciPy with no training data or BOLD circularity, recovering individual breath timing and phase alongside slow RV and RVT regressors across both multiband and legacy two-pass single-band scans.
|
|
47
|
+
- **Echo-1 multi-echo extension for `tedana` cleanup (`denoise`):** Multi-echo ICA removes focal motion while leaving fast and slow respiratory T2* variance inside accepted components (Power et al., 2018; DuPre et al., 2021). Applying sub-TR extraction to the nearly BOLD-free first echo (`TE ≈ 10–14 ms`) yields a respiratory trace directly from the scan, which `neuroresp` projects orthogonal to rejected components (Lindquist et al., 2019).
|
|
48
|
+
- **Belt QC, chest-wall states, and direct airflow (`belt`, `airflow`):** Routine belt pipelines often bridge dropouts and clipped sighs, distorting multi-run samples (Power et al., 2020). `neuroresp` separates rail clipping from loose straps, withholds sigh rates when clipping occurs, adds four-state chest-wall segmentation and dual-band RIP summing for DC belts, and defines airflow pauses relative to each breath's peak flow.
|
|
49
|
+
- **ECG and head-coil breathing in MEG and EEG (`meg`):** Heart-rate estimators lose accuracy as respiratory sinus arrhythmia fades with age. `neuroresp` combines heartbeat QRS shape (Moody et al., 1985) with whitened spectral fusion, a physical modulation check, and RSA sign orientation (Yasuma & Hayano, 2004) to keep belt-level rate accuracy in older adults. It also recovers breathing from Elekta `cHPI` and CTF `HLC` head-position coils.
|
|
50
|
+
- **Multi-sensor simulation from one ground truth (`simulate`):** Clinical physiology engines (Pulse, BioGears) use heavy C++ circuits for ventilator modelling, while psychophysiology packages generate simple periodic waves without lung mechanics or scanner physics. In roughly 200 ms of pure NumPy and SciPy, `neuroresp simulate` couples quiet-rest lung and Konno-Mead chest-wall mechanics (or exact analytic cycles, or recorded spirometry via `simulate_sensors_from_flow`) to airflow, nasal pressure, CO2 capnography, sleep thermistors, dual-band RIP, clipped AC belts, thoracic impedance, ECG, PPG, MEG head coils, MRI field shifts, and delayed BOLD CO2 waves ([Simulation Model](docs/simulation.md)).
|
|
51
|
+
|
|
52
|
+
## Commands
|
|
53
|
+
|
|
54
|
+
Choose the command that matches your recording (each workflow is also available from Python):
|
|
55
|
+
|
|
56
|
+
| Input | Command | Purpose |
|
|
57
|
+
| :--- | :--- | :--- |
|
|
58
|
+
| **Synthetic recordings with known breath timing** | [neuroresp simulate](docs/usage.md#simulate-coupled-multi-sensor-breathing) | Generates coupled airway, paired RIP, AC belt, MRI, ECG, and PPG signals (`analytic` or `mechanics` lung model) |
|
|
59
|
+
| **Raw 4D fMRI (multiband or single-band)** | [neuroresp extract](docs/usage.md#extract-breathing-from-raw-fmri) | Recovers sub-TR breathing from slice-group magnetic field shifts |
|
|
60
|
+
| **fMRI or `tedana` multi-echo outputs** | [neuroresp denoise](docs/usage.md#build-confounds-for-fmriprep-and-nilearn) | Builds fast phase and slow vascular regressors, orthogonal to rejected `tedana` components |
|
|
61
|
+
| **Six-parameter motion file** | [neuroresp motion](docs/usage.md#motion-tracking-and-fd-decensoring) | Tracks breathing at short TRs and strips respiratory shifts from framewise displacement |
|
|
62
|
+
| **Respiratory belt or dual-band RIP** | [neuroresp belt](docs/usage.md#hardware-belts) | Flags clipping and loose straps, sums paired RIP bands, and optionally segments four chest-wall states |
|
|
63
|
+
| **Spirometer, pneumotachograph, or nasal cannula** | [neuroresp airflow](docs/usage.md#measure-direct-airway-flow) | Extracts breath landmarks, relative pauses, motor shape, and calibrated ventilation |
|
|
64
|
+
| **Clipped belt + fMRI from the same scan** | [neuroresp fuse](docs/usage.md#hybrid-belt-and-mri-cycle-fusion) | Fills clipped or dropped belt intervals with MRI breaths from the same scan |
|
|
65
|
+
| **MEG or EEG with ECG or head coils (Elekta cHPI, CTF HLC)** | [neuroresp meg](docs/usage.md#recover-breathing-from-meg-and-eeg) | Recovers breathing from QRS morphology, RSA, and continuous head-coil motion |
|
|
66
|
+
| **Full BIDS study** | [neuroresp batch](docs/usage.md#batch-processing-and-slurm-arrays) | Discovers runs, executes locally or on Slurm, and builds cohort QC tables and plots |
|
|
67
|
+
|
|
68
|
+
## Install
|
|
69
|
+
|
|
70
|
+
Requires Python 3.11 or newer. The default fMRI solver uses only NumPy and SciPy with no external binaries.
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
python -m pip install neuroresp
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Install `python -m pip install "neuroresp[meg]"` to read raw MEG and EEG files with MNE, or pass `--backend afni` if AFNI `3dvolreg` is installed.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Quickstart
|
|
81
|
+
|
|
82
|
+
Explore the lung mechanics and sensor artefacts directly in your browser with the [interactive breathing simulator](https://shanelindsay.github.io/explainers/breathing-simulator/), or generate a coupled synthetic recording locally and analyse the same breaths across airflow, chest expansion, and ECG:
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
neuroresp simulate --out demo --duration-s 90 --sigh-times 18 --hold 42:6 --hold-phase peak \
|
|
86
|
+
--belt-clip-quantile 0.92 --loose 68:5 --mri-tr-s 2.0
|
|
87
|
+
neuroresp airflow demo/flow.csv --fs 100 --calibrated --out demo/airflow
|
|
88
|
+
neuroresp belt demo/sub-sim_task-rest_desc-rip_physio.tsv.gz --dc-coupled --states \
|
|
89
|
+
--loose-min-s 0 --out demo/belt
|
|
90
|
+
neuroresp meg demo/ecg.npz --source ecg --out demo/meg
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+

|
|
94
|
+
|
|
95
|
+
*Figure 2. Five coupled sensor views of the same simulated breaths. At the sigh, the AC scanner belt flattens while DC RIP continues rising; during the 6 s peak hold, flow sits at zero and the AC belt decays toward baseline; and during the loose-strap window, only the scanner belt freezes. The lower rows show nonlinear nasal pressure, sub-TR vs. whole-volume MRI sampling, and respiratory modulation of ECG amplitude and beat spacing.*
|
|
96
|
+
|
|
97
|
+
See [Simulation Model](docs/simulation.md) for how the lung core and sensor adapters work, and [Usage](docs/usage.md#simulate-coupled-multi-sensor-breathing) for mechanics presets, real-airflow replay, and additional failure-mode examples.
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Recover breathing from raw fMRI (`neuroresp extract`)
|
|
102
|
+
|
|
103
|
+
Every breath changes the amount of air in the lungs and shifts the magnetic field at the head, nudging echo-planar fMRI images back and forth along the phase-encoding axis (Raj et al., 2001; Van de Moortele et al., 2002). Standard motion correction samples that shift only once per volume, which aliases faster breathing whenever TR is 1.4 seconds or longer.
|
|
104
|
+
|
|
105
|
+
`neuroresp extract` aligns slice groups at their native acquisition times instead, sampling the same field shift 8 to 20 times per second in multiband scans (Hocke & Frederick, 2021) or across interleaved passes and slices in legacy single-band scans. For multi-run BIDS cohorts, `neuroresp batch` plans and runs extractions locally or as Slurm job arrays.
|
|
106
|
+
|
|
107
|
+
```sh
|
|
108
|
+
neuroresp extract sub-01_task-rest_echo-1_bold.nii.gz --out derivatives/neuroresp/sub-01/rest
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
This writes a BIDS physiology file (`*_desc-mri_physio.tsv.gz`), a processed waveform (`respiration.csv`), a breath table (`breaths.tsv`), run summaries (`summary.json`), and a four-panel quality report (`qc.png`). Because the sign of the shift depends on scan polarity, distinguishing inhalation from exhalation requires an external reference. Large head movements and unusually regular breathing locked to the repetition rate can also distort the trace. See [Scientific Approach](docs/approach.md#sub-tr-sampling-from-multiband-slice-stacks), [Comparisons & Limits](docs/comparisons.md#measurement-limits), and [Usage](docs/usage.md#extract-breathing-from-raw-fmri).
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Remove breathing noise and motion artefacts (`neuroresp denoise`, `neuroresp motion`)
|
|
116
|
+
|
|
117
|
+
Breathing enters fMRI at two timescales: fast ripples with every breath, and slow cortical blood-flow waves driven by arterial CO₂ 8 to 15 seconds later (Glover et al., 2000; Birn et al., 2008). Multi-echo ICA (`tedana`) removes focal motion cleanly but keeps much of this respiratory variance because CO₂ alters T2* oxygenation much like neural activity (Power et al., 2018; DuPre et al., 2021).
|
|
118
|
+
|
|
119
|
+
The first echo in a multi-echo scan (`TE ≈ 10–14 ms`) carries minimal BOLD contrast while still capturing the magnetic field shift of every breath. Extracting breathing from Echo 1 lets `neuroresp denoise` clean both fast and slow respiratory variance, while projecting motion and breathing regressors orthogonal to rejected `tedana` components so sequential regression cannot reintroduce removed noise (Lindquist et al., 2019).
|
|
120
|
+
|
|
121
|
+

|
|
122
|
+
|
|
123
|
+
*Figure 3. Left: Echo-1 slice stacking tracks direct nasal CO₂ across a TR 2.0 s multi-echo scan where whole-volume motion aliases (OpenNeuro `ds004662`). Right: orthogonal post-`tedana` Echo-1 regression cuts surviving fast breath-phase and slow respiratory variance across 30 resting scans (OpenNeuro `ds006926`).*
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
# fMRIPrep / nilearn confounds
|
|
127
|
+
neuroresp denoise derivatives/neuroresp/sub-01/rest/respiration.csv \
|
|
128
|
+
--nifti-in sub-01_task-rest_desc-preproc_bold.nii.gz \
|
|
129
|
+
--out-confounds sub-01_task-rest_desc-resp_confounds.tsv
|
|
130
|
+
|
|
131
|
+
# Post-tedana multi-echo cleanup with joint motion and respiratory orthogonalisation
|
|
132
|
+
neuroresp denoise derivatives/neuroresp/sub-01/rest/respiration.csv \
|
|
133
|
+
--nifti-in desc-denoised_bold.nii.gz --nifti-out desc-respclean_bold.nii.gz \
|
|
134
|
+
--motion motion.1D --tedana-mixing desc-ICA_mixing.tsv --tedana-metrics desc-tedana_metrics.tsv \
|
|
135
|
+
--out-confounds desc-resp_confounds.tsv
|
|
136
|
+
|
|
137
|
+
# Strip respiratory field shifts from framewise displacement before volume censoring
|
|
138
|
+
neuroresp motion decensor Movement_Regressors.txt --tr 0.72 --method bandstop --out fd-rest
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`neuroresp motion` also tracks breathing directly from fast-TR motion tables (`track`) and separates field shifts from physical nodding across opposite phase-encoding scans (`separate`), supporting AFNI, HCP, fMRIPrep, and SPM motion files. See [Multi-echo ICA and Echo 1](docs/approach.md#multi-echo-ica-and-echo-1), [Motion censoring and head nodding](docs/approach.md#motion-censoring-and-head-nodding), and [Usage](docs/usage.md#clean-tedana-outputs-with-joint-orthogonalisation).
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Measure breathing from belts and airway sensors (`neuroresp belt`, `neuroresp airflow`)
|
|
146
|
+
|
|
147
|
+
Chest-wall belts and direct airway sensors measure different physical quantities, so `neuroresp` gives each its own command while keeping their outputs compatible with `neuroresp denoise`.
|
|
148
|
+
|
|
149
|
+
- **`neuroresp belt`** cleans pneumatic and RIP belts, separating rail saturation from loose straps before writing breath tables and `respiration.csv`. On calibrated DC-coupled bands, `--states --dc-coupled` segments chest-wall expansion, recoil, and peak or trough dwell, and `--second-column` sums paired chest and abdomen RIP bands for within-person contour and hold comparisons.
|
|
150
|
+
- **`neuroresp airflow`** analyses spirometry, pneumotachography, and nasal-cannula recordings using per-breath relative flow thresholds, keeping pauses and flow shape comparable across changes in gain. Adding `--calibrated` computes tidal volume and both accepted-cycle and observed-time minute ventilation in L/s, while exporting an integrated lung-volume proxy in `respiration.csv` for denoising.
|
|
151
|
+
|
|
152
|
+
```sh
|
|
153
|
+
# Scanner belt QC and cycle timing
|
|
154
|
+
neuroresp belt sub-01_task-rest_physio.tsv.gz --out belt-rest
|
|
155
|
+
|
|
156
|
+
# Calibrated dual-band RIP with four-state chest-wall segmentation
|
|
157
|
+
neuroresp belt rip.csv --fs 100 --column chest --second-column abdomen --states --dc-coupled --out rip-states
|
|
158
|
+
|
|
159
|
+
# Direct airway flow and nasal pressure
|
|
160
|
+
neuroresp airflow cannula.csv --column pressure --fs 6 --sensor nasal_pressure --out nasal-rest
|
|
161
|
+
neuroresp airflow spirometry.csv --column flow --fs 100 --calibrated --out spirometry-rest
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
See [Hardware belts](docs/usage.md#hardware-belts), [Measure direct airway flow](docs/usage.md#measure-direct-airway-flow), and [Chest-wall movement versus airway timing](docs/comparisons.md#chest-wall-movement-versus-airway-timing).
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
## Repair clipped scanner belts with fMRI (`neuroresp fuse`)
|
|
169
|
+
|
|
170
|
+
Scanner belts often hit their digital ceiling during deep sighs or slip loose mid-scan. `neuroresp fuse` keeps clean belt cycles inside the scan window and fills saturated or dropped stretches with complete MRI cycles from the same scan. Each sensor switch starts a new segment so breath-to-breath variability stays within one sensor, and sigh counts are withheld whenever belt clipping occurs.
|
|
171
|
+
|
|
172
|
+
```sh
|
|
173
|
+
neuroresp fuse belt-rest derivatives/neuroresp/sub-01/rest --out fused-rest
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+

|
|
177
|
+
|
|
178
|
+
*Figure 4. Raw 400 Hz abdominal belt (teal, left axis) alongside 3T fMRI phase-encoding shift (orange, right axis, TR 0.72 s) from a Human Connectome Project resting scan (Van Essen et al., 2013). During the sigh, the belt flattens at its 4095 upper rail (shaded) and then hits its 0 lower rail while the fMRI shift continues tracking the full excursion.*
|
|
179
|
+
|
|
180
|
+
See [Hybrid belt and MRI cycle fusion](docs/usage.md#hybrid-belt-and-mri-cycle-fusion) and [Outputs](docs/outputs.md#command-specific-outputs).
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
## Recover breathing in MEG and EEG (`neuroresp meg`)
|
|
185
|
+
|
|
186
|
+
Most MEG and EEG studies record chest ECG without a breathing belt. Heart-rate variability estimators weaken with age, whereas chest expansion keeps tilting the heart relative to the electrodes on every beat (Moody et al., 1985). `neuroresp meg` recovers breathing from beat-by-beat QRS amplitude and area, strips slow vasomotor drift, leaves beat dropouts as gaps, and combines morphology with respiratory sinus arrhythmia (RSA; Yasuma & Hayano, 2004) for rate and approximate inspiratory sign. Built-in checks reject unmodulated ECG traces and flag runs where breathing rate shifts substantially mid-recording.
|
|
187
|
+
|
|
188
|
+

|
|
189
|
+
|
|
190
|
+
*Figure 5. Left: ECG-derived breathing vs. a physical chest belt and heart-rate-only estimation in a 68-year-old adult. Right: rate concordance against physical chest belts across young adults, older adults, and one-week retest sessions in the PhysioNet Autonomic Aging dataset (51 adults, 98 resting runs; Schumann & Bär, 2022).*
|
|
191
|
+
|
|
192
|
+
```sh
|
|
193
|
+
neuroresp meg sub-01_task-rest_meg.fif --ecg-ch ECG001 --out derivatives/neuroresp/sub-01/meg --prefix sub-01_task-rest
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Raw MEG recordings with continuous head localisation also capture breathing through small nodding and front-to-back head movements. Elekta/MEGIN `.fif` files supply active head-position indicator coils (`cHPI`), and CTF `.ds` datasets supply the nine `HLC` fiducial channels, which `neuroresp` converts to the same head poses. Pass `--source chpi` for head-motion extraction, or `--source both` to export both trackers and orient the mechanical trace using ECG.
|
|
197
|
+
|
|
198
|
+

|
|
199
|
+
|
|
200
|
+
*Figure 6. Mechanical head-coil motion (`cHPI`) aligned against independent ECG-derived respiration in a real human MEG recording (OpenNeuro `ds008861`).*
|
|
201
|
+
|
|
202
|
+
See [ECG shape and heart rate in MEG and EEG](docs/approach.md#ecg-shape-and-heart-rate-in-meg-and-eeg), [Comparisons & Limits](docs/comparisons.md#benchmarks), and [Usage](docs/usage.md#recover-breathing-from-meg-and-eeg) for validation tables, `.npz` inputs, and polarity flags.
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
## Documentation
|
|
207
|
+
|
|
208
|
+
- [Overview and Workflow Selector](docs/index.md)
|
|
209
|
+
- [Installation](docs/installation.md)
|
|
210
|
+
- [Scientific Approach](docs/approach.md)
|
|
211
|
+
- [Simulation Model](docs/simulation.md)
|
|
212
|
+
- [Interactive Breathing Simulator (Web)](https://shanelindsay.github.io/explainers/breathing-simulator/)
|
|
213
|
+
- [Method Comparisons and Measurement Limits](docs/comparisons.md)
|
|
214
|
+
- [Research Applications](docs/applications.md)
|
|
215
|
+
- [Usage (CLI & Python API)](docs/usage.md)
|
|
216
|
+
- [Tutorials (Runnable Quarto Walkthroughs)](docs/tutorials/index.md)
|
|
217
|
+
- [Outputs and Quality Control](docs/outputs.md)
|
|
218
|
+
- [FAQ](docs/faq.md)
|
|
219
|
+
|
|
220
|
+
## Key References
|
|
221
|
+
|
|
222
|
+
- **Sub-TR fMRI extraction (`extract`)**: Raj, Anderson, & Gore (2001, *Phys. Med. Biol.*); Van de Moortele et al. (2002, *Magn. Reson. Med.*); Hocke & Frederick (2021, *Magn. Reson. Med.*, 85(1), 309–315).
|
|
223
|
+
- **BOLD denoising and multi-echo ICA (`denoise`)**: Glover, Li, & Ress (2000, *Magn. Reson. Med.*); Birn et al. (2006, 2008, *NeuroImage*); Chang & Glover (2009, *NeuroImage*); Kundu et al. (2012, *NeuroImage*); Power et al. (2018, *PNAS*); Lindquist et al. (2019, *Hum. Brain Mapp.*); Lynch et al. (2020, *Cell Rep.*); DuPre et al. (2021, *JOSS*); Moia et al. (2021, *NeuroImage*); Kovářová & Mikl (2025, *Hum. Brain Mapp.*).
|
|
224
|
+
- **Respiratory motion and FD decensoring (`motion`, `fuse`)**: Power et al. (2019, *NeuroImage*); Fair et al. (2020, *NeuroImage*); Gratton et al. (2020, *NeuroImage*); Pipoly et al. (2026, *Hum. Brain Mapp.*); Sadil et al. (2026, *bioRxiv*).
|
|
225
|
+
- **ECG-derived respiration (`meg`)**: Moody et al. (1985, *Comput. Cardiol.*); Yasuma & Hayano (2004, *Chest*).
|
|
226
|
+
- **Respiratory mechanics and multi-sensor simulation (`simulate`)**: Otis et al. (1956, *J. Appl. Physiol.*); Konno & Mead (1967, *J. Appl. Physiol.*); Bates (2009, *Lung Mechanics*); Albanese et al. (2016, *Am. J. Physiol. Heart Circ. Physiol.*); Charlton et al. (2017, *Physiol. Meas.*).
|
|
227
|
+
- **Related image-based comparators**: Aslan, Hocke, Schwarz, & Frederick (2019, *NeuroImage*, `happy`); Jaffray et al. (2024, *Imaging Neurosci.*); Addeh et al. (2025, *Magn. Reson. Med.*; 2026, *Imaging Neurosci.*).
|
|
228
|
+
|
|
229
|
+
## Validation Datasets & Acknowledgements
|
|
230
|
+
|
|
231
|
+
Computational work was supported by the Viper and Vardent high-performance computing clusters at the University of Hull. Benchmark figures and validation tables were evaluated on open-access recordings from the following datasets:
|
|
232
|
+
|
|
233
|
+
- McNabb, C., Driver, I., Hyde, V., et al. (2025). WAND: A multi-modal dataset integrating advanced MRI, MEG, and TMS for multi-scale brain analysis. *Scientific Data*. https://doi.org/10.1038/s41597-024-04154-7
|
|
234
|
+
- Nooner, K. B., Colcombe, S. J., Tobe, R. H., et al. (2012). The NKI-Rockland Sample: A model for accelerating the pace of discovery science in psychiatry. *Frontiers in Neuroscience*. https://doi.org/10.3389/fnins.2012.00152
|
|
235
|
+
- Schumann, A., & Bär, K.-J. (2022). Autonomic aging: A dataset to quantify changes of cardiovascular autonomic function during healthy aging. *Scientific Data*. https://doi.org/10.1038/s41597-022-01202-y
|
|
236
|
+
- Van Essen, D. C., Smith, S. M., Barch, D. M., et al. (2013). The WU-Minn Human Connectome Project: An overview. *NeuroImage*. https://doi.org/10.1016/j.neuroimage.2013.05.041 *(Data were provided in part by the Human Connectome Project, WU-Minn Consortium, Principal Investigators David Van Essen and Kamil Ugurbil, 1U54MH091657, funded by the 16 NIH Institutes and Centers supporting the NIH Blueprint for Neuroscience Research and by the McDonnell Center for Systems Neuroscience at Washington University.)*
|
|
237
|
+
- Wakeman, D. G., & Henson, R. N. (2015). A multi-subject, multi-modal human neuroimaging dataset. *Scientific Data*. https://doi.org/10.1038/sdata.2015.1
|
|
238
|
+
- Additional OpenNeuro multi-echo, cortico-spinal, and MEG recordings (`ds004662`, `ds006926`, `ds008861`, `ds005883`) and calibrated spirometry/RIP recordings used for airflow and belt-state benchmarks (see [Comparisons & Limits](docs/comparisons.md)).
|
|
239
|
+
|
|
240
|
+
## Citation & License
|
|
241
|
+
|
|
242
|
+
Please cite [CITATION.cff](CITATION.cff) alongside the methodological papers above when using `neuroresp` in published work. Released under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "neuroresp"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Open-source Python toolkit for measuring and modelling human breathing in neuroimaging and physiology"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
authors = [{ name = "Shane Lindsay" }]
|
|
14
|
+
keywords = ["respiration", "breathing", "fMRI", "MEG", "EEG", "RETROICOR", "tedana", "BIDS", "neuroimaging", "psychophysiology"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Science/Research",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Topic :: Scientific/Engineering :: Medical Science Apps.",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"numpy>=1.26",
|
|
27
|
+
"scipy>=1.11",
|
|
28
|
+
"nibabel>=5.0",
|
|
29
|
+
"pandas>=2.0",
|
|
30
|
+
"matplotlib>=3.8",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.urls]
|
|
34
|
+
Homepage = "https://github.com/shanelindsay/neuroresp"
|
|
35
|
+
Repository = "https://github.com/shanelindsay/neuroresp"
|
|
36
|
+
Issues = "https://github.com/shanelindsay/neuroresp/issues"
|
|
37
|
+
Documentation = "https://github.com/shanelindsay/neuroresp/blob/main/docs/index.md"
|
|
38
|
+
|
|
39
|
+
[project.optional-dependencies]
|
|
40
|
+
meg = ["mne>=1.6"]
|
|
41
|
+
dev = ["pytest>=8.0", "ruff>=0.5"]
|
|
42
|
+
|
|
43
|
+
[project.scripts]
|
|
44
|
+
neuroresp = "neuroresp.cli:main"
|
|
45
|
+
|
|
46
|
+
[tool.setuptools]
|
|
47
|
+
package-dir = { "" = "src" }
|
|
48
|
+
|
|
49
|
+
[tool.setuptools.packages.find]
|
|
50
|
+
where = ["src"]
|
|
51
|
+
|
|
52
|
+
[tool.pytest.ini_options]
|
|
53
|
+
testpaths = ["tests"]
|
|
54
|
+
markers = ["realdata: integration checks using locally staged human recordings"]
|