nirspipe.qc.metrics.coupling#

How well the optodes are coupled to the scalp, and how strong the signal is.

SCI and PSP read the cardiac oscillation the two wavelengths of a pair should share, and the heart rate where it peaks; CV, SNR and mean amplitude read the intensity itself; CP is experimental. Every one of these is a property of the recording as acquired, so they run before any rejection and their aggregates include the channels that rejection will remove.

Module Contents#

Functions#

compute_sci_scores

Scalp coupling index (SCI) per channel [1].

blank_flat_windows

NaN wherever either wavelength of a channel’s optode pair is flat inside the window.

channel_cv_windowed

Per-channel CV averaged over non-overlapping windows; whole-run if one does not fit.

channel_cv

Coefficient of variation per channel: relative noise level (lower = cleaner).

saturated_share

Per channel, the share of samples held at ceiling for two samples or more in a row.

peak_spectral_power

Peak spectral power (PSP) per channel in each non-overlapping window.

compute_psp_scores

Peak spectral power per channel, averaged over PSP_WINDOW_S windows.

heart_rate_band

Where a heart rate is looked for: from the respiration band’s top to where a cardiac band may end.

labels_look_swapped

Whether swapping the two wavelengths puts the pulse in HbO by SWAP_FACTOR more; None without both.

Data#

API#

nirspipe.qc.metrics.coupling.logger = 'get_logger(...)'#
nirspipe.qc.metrics.coupling.PSP_WINDOW_S = 10.0#
nirspipe.qc.metrics.coupling.PSP_SCALE_MAX = 0.1#
nirspipe.qc.metrics.coupling.CV_WINDOW_S = 10.0#
nirspipe.qc.metrics.coupling.SCI_WINDOW_S = 10.0#
nirspipe.qc.metrics.coupling.compute_sci_scores(raw: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float) → tuple[dict[str, float], mne.io.Raw]#

Scalp coupling index (SCI) per channel [1].

Cardiac-band cross-correlation between the two wavelengths of each channel; low SCI flags poor optode-scalp coupling.

Parameters:
  • raw (mne.io.Raw) -- Raw intensity recording, or one already in optical density.

  • cardiac_l_freq (float) -- Cardiac band edges in Hz.

  • cardiac_h_freq (float) -- Cardiac band edges in Hz.

Returns:

({channel: SCI score}, optical-density recording). SCI defaults to 1.0 for all channels on failure.

Return type:

tuple[dict[str, float], mne.io.Raw]

Notes

Thin wrapper over mne.preprocessing.nirs.scalp_coupling_index that adds what batch QC needs: it converts to optical density once and returns that OD object so later metrics can reuse it, reshapes the array into a per-channel dict, and is crash-safe: on failure every channel defaults to 1.0 (so no channel is wrongly dropped) instead of raising and aborting the whole run. The exception is a cardiac band at or above Nyquist, which is a configuration error rather than a data failure and raises ValueError: defaulting there would report every channel of every run as perfectly coupled.

Already-OD input is passed through rather than converted: recomputing scores from a tree on disk reads desc-sci or desc-od, and optical_density raises on anything that is not continuous-wave amplitude.

References

nirspipe.qc.metrics.coupling.blank_flat_windows(raw_od: mne.io.Raw, scores: numpy.ndarray, window_s: float) → numpy.ndarray#

NaN wherever either wavelength of a channel’s optode pair is flat inside the window.

a pair stuck at the detector's ceiling  ->  NaN in every window, never counted as passing

mne-nirs filters a flat pair to the same rounding residue on both wavelengths and then scales it up, which reads as a perfect correlation and a large cardiac peak. A flat channel has no pulse to couple, so its windows must not count. scores is channel x window on mne-nirs’ own grid, rows in raw_od.ch_names order.

nirspipe.qc.metrics.coupling.channel_cv_windowed(data: numpy.ndarray, sfreq: float, window_s: float = CV_WINDOW_S) → numpy.ndarray#

Per-channel CV averaged over non-overlapping windows; whole-run if one does not fit.

3900 s at 10 Hz, 10 s windows  ->  mean of 390 CVs per channel
nirspipe.qc.metrics.coupling.channel_cv(data: numpy.ndarray) → numpy.ndarray#

Coefficient of variation per channel: relative noise level (lower = cleaner).

\[\mathrm{CV} = \frac{\sigma}{\mu}\]

NaN for channels whose mean is 0.

nirspipe.qc.metrics.coupling.saturated_share(data: numpy.ndarray, ceiling: float) → numpy.ndarray#

Per channel, the share of samples held at ceiling for two samples or more in a row.

a channel pinned at the ceiling for 30 of 100 samples, in one stretch  ->  0.30
a channel touching the ceiling at one sample                            ->  0.0

A detector that receives more light than it can record writes its limit, the same value sample after sample, so the recording’s highest intensity repeated is a clipped stretch. A lone sample at that value is where the signal peaked, which is why a stretch needs two. data is (n_channels, n_times).

nirspipe.qc.metrics.coupling.peak_spectral_power(raw_od: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float, window_s: float) → tuple[np.ndarray, list[tuple[float, float]]]#

Peak spectral power (PSP) per channel in each non-overlapping window.

In each window the two wavelengths of a pair, band-passed to the cardiac band, give \(x\) and \(y\) of \(N\) samples. Their unbiased cross-correlation, scaled so that lag 0 is the cosine similarity,

\[s(k) = \frac{N}{N - |k|} \, \frac{\sum_n x_{n+k} \, y_n}{\sqrt{\sum_n x_n^2 \, \sum_n y_n^2}}, \qquad |k| < N,\]

is Hamming-windowed, and PSP is the largest value of its one-sided power spectrum (power, not density, scaling) below cardiac_h_freq. Both channels of a pair get the pair’s value.

This is QT-NIRS’s computation, the scale its 0.1 line is defined on; mne-nirs peak_power returns a density spectrum of a biased correlation and is not on that scale. The band-pass is MNE’s, as for SCI, and windows are cut on mne-nirs’ windowed SCI grid, so the two land on the same columns.

Parameters:
  • raw_od (mne.io.Raw) -- Optical-density recording, wavelength pairs adjacent as MNE orders them.

  • cardiac_l_freq (float) -- Cardiac band edges in Hz.

  • cardiac_h_freq (float) -- Cardiac band edges in Hz.

  • window_s (float) -- Window length in seconds.

Returns:

PSP per channel per window (rows in raw_od.ch_names order), and each window’s start and end times.

Return type:

tuple[np.ndarray, list[tuple[float, float]]]

nirspipe.qc.metrics.coupling.compute_psp_scores(raw: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float) → dict[str, float]#

Peak spectral power per channel, averaged over PSP_WINDOW_S windows.

Measured on optical density: the metric cross-correlates the two wavelengths, so it has no meaning after Beer-Lambert. Converting here keeps this agreeing with the windowed PSP series, which is handed OD.

The window is pinned to PSP_WINDOW_S rather than following the QC window length; see the constant for what changes when it moves. The windowed PSP series is a separate view and does follow the QC window, so its colour scale is not on this scalar’s scale.

Separate from the metric wrapper below because channel screening needs the scores and not the record entry, and computing them twice per run would be paying twice for the same measurement.

nirspipe.qc.metrics.coupling.heart_rate_band(sfreq: float, resp_h_freq: float) → tuple[float, float]#

Where a heart rate is looked for: from the respiration band’s top to where a cardiac band may end.

heart_rate_band(10.0, 0.5) -> (0.5, 4.7)
nirspipe.qc.metrics.coupling.CARDIAC_PEAK_MIN = 10.0#
nirspipe.qc.metrics.coupling.SWAP_FACTOR = 2.0#
nirspipe.qc.metrics.coupling.labels_look_swapped(ratio: float | None, ratio_swapped: float | None) → bool | None#

Whether swapping the two wavelengths puts the pulse in HbO by SWAP_FACTOR more; None without both.