|
5 | 5 | from scipy.ndimage.filters import convolve1d
|
6 | 6 | from scipy.signal import detrend
|
7 | 7 | from scipy.stats import zscore
|
| 8 | +from scipy.interpolate import interp1d |
8 | 9 |
|
9 | 10 | mpl.use("TkAgg")
|
10 | 11 | import matplotlib.pyplot as plt
|
|
14 | 15 | from . import utils
|
15 | 16 |
|
16 | 17 |
|
| 18 | +def rvt(belt_ts, peaks, troughs, samplerate, lags=(0, 4, 8, 12)): |
| 19 | + """ |
| 20 | + Implement the Respiratory Variance over Time (Birn et al. 2006). |
| 21 | +
|
| 22 | + Procedural choices influenced by RetroTS |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + belt_ts: array_like |
| 27 | + respiratory belt data - samples x 1 |
| 28 | + peaks: array_like |
| 29 | + peaks found by peakdet algorithm |
| 30 | + troughs: array_like |
| 31 | + troughs found by peakdet algorithm |
| 32 | + samplerate: float |
| 33 | + sample rate in hz of respiratory belt |
| 34 | + lags: tuple |
| 35 | + lags in seconds of the RVT output. Default is 0, 4, 8, 12. |
| 36 | +
|
| 37 | + Outputs |
| 38 | + ------- |
| 39 | + rvt: array_like |
| 40 | + calculated RVT and associated lags. |
| 41 | + """ |
| 42 | + timestep = 1 / samplerate |
| 43 | + # respiration belt timing |
| 44 | + time = np.arange(0, len(belt_ts) * timestep, timestep) |
| 45 | + peak_vals = belt_ts[peaks] |
| 46 | + trough_vals = belt_ts[troughs] |
| 47 | + peak_time = time[peaks] |
| 48 | + trough_time = time[troughs] |
| 49 | + mid_peak_time = (peak_time[:-1] + peak_time[1:]) / 2 |
| 50 | + period = peak_time[1:] - peak_time[:-1] |
| 51 | + # interpolate peak values over all timepoints |
| 52 | + peak_interp = interp1d( |
| 53 | + peak_time, peak_vals, bounds_error=False, fill_value="extrapolate" |
| 54 | + )(time) |
| 55 | + # interpolate trough values over all timepoints |
| 56 | + trough_interp = interp1d( |
| 57 | + trough_time, trough_vals, bounds_error=False, fill_value="extrapolate" |
| 58 | + )(time) |
| 59 | + # interpolate period over all timepoints |
| 60 | + period_interp = interp1d( |
| 61 | + mid_peak_time, period, bounds_error=False, fill_value="extrapolate" |
| 62 | + )(time) |
| 63 | + # full_rvt is (peak-trough)/period |
| 64 | + full_rvt = (peak_interp - trough_interp) / period_interp |
| 65 | + # calculate lags for RVT |
| 66 | + rvt_lags = np.zeros((len(full_rvt), len(lags))) |
| 67 | + for ind, lag in enumerate(lags): |
| 68 | + start_index = np.argmin(np.abs(time - lag)) |
| 69 | + temp_rvt = np.concatenate( |
| 70 | + ( |
| 71 | + np.full((start_index), full_rvt[0]), |
| 72 | + full_rvt[: len(full_rvt) - start_index], |
| 73 | + ) |
| 74 | + ) |
| 75 | + rvt_lags[:, ind] = temp_rvt |
| 76 | + |
| 77 | + return rvt_lags |
| 78 | + |
| 79 | + |
17 | 80 | @due.dcite(references.POWER_2018)
|
18 | 81 | def rpv(resp, window=6):
|
19 | 82 | """Calculate respiratory pattern variability.
|
|
0 commit comments