nirspipe.qc.metrics.windowed#

The same quality questions asked per window instead of per recording.

SCI, PSP and GVTD binned onto one shared non-overlapping window grid, so a report can show where in a recording the quality moved rather than only its average. One grid for all three on purpose: window_s * sfreq is rarely an integer, and three independently derived grids drift apart over a long run.

Module Contents#

Functions#

compute_windowed_sci

SCI per channel in each non-overlapping window.

compute_windowed_psp

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

compute_windowed_cv

CV per channel in each non-overlapping window, and each window’s [start, end] times.

attach_windowed_series

Compute sliding-window SCI/PSP/GVTD series, attach summaries to sqm, return raw series.

windowed_key

One channel set’s key in the windowed section.

long_or_all

The long set’s entry, or every channel’s on a montage that never split, as the scalar sections fill a skipped _long from the whole.

window_centers

Mid-time of each window, from the [start, end] pairs mne_nirs returns.

windows_touching

Per window, whether it overlaps any span at all, as mne drops an epoch touching one.

task_scope_windows

[(label, tstart, tstop)] over the annotations long enough to hold screening windows.

passing_mask_from_matrices

Channel x window, True where a channel passes in that window. None if unusable.

counted_screen_windows

(centres, counted) on the screening grid, before anything is measured.

good_window_fraction

Share of windows in which a channel passes, per channel.

passing_windows

good_window_fraction(), also handing back the grid it counted over.

condition_window_means

Per-condition means of an already-computed windowed metric, by slicing it.

Data#

API#

nirspipe.qc.metrics.windowed.logger = 'get_logger(...)'#
nirspipe.qc.metrics.windowed.compute_windowed_sci(raw_od: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float, window_s: float = 10.0) → tuple[np.ndarray, np.ndarray]#

SCI per channel in each non-overlapping window.

Parameters:
  • raw_od (mne.io.Raw) -- Optical-density recording.

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

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

  • window_s (float, optional) -- Non-overlapping window length in seconds.

Returns:

(scores, times): SCI per channel per window (channel x window), and each window’s [start, end] times.

Return type:

tuple[np.ndarray, np.ndarray]

Notes

Wrapper over mne_nirs scalp_coupling_index_windowed; used to see how coupling drifts over the recording rather than as a single whole-run value.

nirspipe.qc.metrics.windowed.compute_windowed_psp(raw_od: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float, window_s: float = 10.0) → tuple[np.ndarray, np.ndarray]#

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

Parameters:
  • raw_od (mne.io.Raw) -- Optical-density recording.

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

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

  • window_s (float, optional) -- Non-overlapping window length in seconds.

Returns:

(scores, times): PSP per channel per window, and each window’s [start, end] times.

Return type:

tuple[np.ndarray, np.ndarray]

Notes

peak_spectral_power() with flat windows blanked; the windowed companion to the whole-run PSP, showing how the cardiac peak strength varies over time.

nirspipe.qc.metrics.windowed.compute_windowed_cv(raw_intensity: mne.io.Raw, window_s: float = 10.0) → tuple[np.ndarray, np.ndarray]#

CV per channel in each non-overlapping window, and each window’s [start, end] times.

Returned on the same grid and in the same shape as the SCI and PSP series, so the three line up column for column. Measured on raw intensity, which is what CV is defined on: after the optical-density conversion sigma/mu no longer means relative brightness.

nirspipe.qc.metrics.windowed.attach_windowed_series(sqm: dict, raw_od: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float, window_s: float = 10.0, *, gvtd_od: mne.io.Raw | None = None, raw_intensity: mne.io.Raw | None = None, sep_bands=None) → dict#

Compute sliding-window SCI/PSP/GVTD series, attach summaries to sqm, return raw series.

Parameters:
  • sqm (dict) -- Metric dict; per-window summaries are attached to it in place.

  • raw_od (mne.io.Raw) -- Optical-density recording the SCI and PSP series are read off.

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

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

  • window_s (float, optional) -- Non-overlapping window length in seconds.

  • gvtd_od (mne.io.Raw or None, optional) -- A second optical-density recording for the GVTD series alone; raw_od when None.

Returns:

Raw series (sci_matrix/sci_times/psp_matrix/psp_times) for callers that also plot them, and each matrix’s row names (sci_channels etc.); all None on failure.

Return type:

dict

Notes

SCI/PSP and GVTD fail independently; whichever survives is still attached. Center times collapse the mne-nirs [start, end] window pairs to their midpoint. window_s is stored as qc_window_s so a record says which grid its series were binned on; psp_mean is deliberately not on that grid, see PSP_WINDOW_S.

gvtd_od lets GVTD run on the motion-corrected file while SCI and PSP stay on the uncorrected one, where the per-channel scores were taken. Both grids are derived from window_s the same way, so the series stay time-aligned across the two files as long as neither was resampled.

nirspipe.qc.metrics.windowed.WINDOWED_SETS = None#
nirspipe.qc.metrics.windowed.windowed_key(stem: str, channel_set: str = 'all', spans: bool = False) → str#

One channel set’s key in the windowed section.

windowed_key("gvtd_filt_per_window", "long")      -> "gvtd_filt_per_window_long"
windowed_key("spike_spans", "short", spans=True)  -> "spike_spans_short_s"
nirspipe.qc.metrics.windowed.long_or_all(windowed: dict, stem: str, spans: bool = False)#

The long set’s entry, or every channel’s on a montage that never split, as the scalar sections fill a skipped _long from the whole.

nirspipe.qc.metrics.windowed.SCREEN_WINDOW_S = 10.0#
nirspipe.qc.metrics.windowed.window_centers(times) → numpy.ndarray#

Mid-time of each window, from the [start, end] pairs mne_nirs returns.

[[0, 10], [10, 20]]  ->  array([5., 15.])

A window belongs at its middle, and a scope is decided on that one time rather than on overlap, so a window straddling the edge of a task block counts for the side it mostly sits in and is never counted twice.

nirspipe.qc.metrics.windowed.windows_touching(times, spans) → numpy.ndarray#

Per window, whether it overlaps any span at all, as mne drops an epoch touching one.

windows [0, 10], [10, 20], [20, 30], a span at 18-19 s  ->  [False, True, False]

times are the [start, end] pairs mne_nirs returns; bare centres count as zero-width windows.

nirspipe.qc.metrics.windowed.task_scope_windows(raw: mne.io.Raw, min_duration: float = 2 * SCREEN_WINDOW_S) → list[tuple[str, float, float]]#

[(label, tstart, tstop)] over the annotations long enough to hold screening windows.

"rest" at 20 s for 300 s, plus a 5 s trigger  ->  [("rest", 20.0, 320.0)]

The annotation’s own duration is the window, so a recording carrying only zero-length or short triggers yields nothing here and the caller keeps whatever scope it had. A BAD_ span is never a block, however long.

Clamped to the recording, and on the data axis: a cropped Raw keeps its annotations on the original axis while its samples restart at zero.

nirspipe.qc.metrics.windowed.passing_mask_from_matrices(sci_matrix, psp_matrix, sci_cutoff: float, psp_cutoff: float | None) → numpy.ndarray | None#

Channel x window, True where a channel passes in that window. None if unusable.

sci 0.92 psp 0.04 against 0.8 / None  ->  True
sci 0.92 psp 0.04 against 0.8 / 0.1   ->  False

SCI decides alone unless a PSP line is given, and then both have to pass inside the same window. It lives here alone so the two callers cannot drift apart: the screening measures the matrices off a recording, while a per-condition view reads the ones the quality record already stored. A second copy of this comparison would let a report disagree with the verdict the run was screened by.

nirspipe.qc.metrics.windowed.counted_screen_windows(raw: mne.io.Raw, scope=None, window_s: float = SCREEN_WINDOW_S) → tuple[np.ndarray, np.ndarray]#

(centres, counted) on the screening grid, before anything is measured.

a 100 s recording at 10 Hz, BAD_ over 0-45 s  ->  centres 5..95, counted from 55 s on

The grid is mne-nirs’ windowed SCI grid (ceil samples per window, the last window ending one sample early), so this agrees window for window with passing_windows().

nirspipe.qc.metrics.windowed.good_window_fraction(raw_od: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float, sci_cutoff: float, psp_cutoff: float | None, window_s: float = SCREEN_WINDOW_S, scope: list[tuple[str, float, float]] | None = None) → dict[str, float]#

Share of windows in which a channel passes, per channel.

a channel passing both lines in 300 of 400 windows  ->  0.75

A window counts when its SCI passes, and its PSP too when psp_cutoff is given, both inside that window. Windows are counted rather than averaged, so a channel dead for half the run cannot pass on the other half.

scope restricts the denominator to the windows whose centres fall inside those stretches, such as the blocks of a run without its lead-in and gaps. None counts the whole recording. A window touching any BAD_ span is left out under either.

The scope masks one whole-record pass rather than cutting the recording and measuring each piece: SCI and PSP filter to the cardiac band, so a cut piece is filtered against its own edges and lands on its own window grid. Masking keeps one grid and one filter.

Returns an empty dict when a metric the rule reads cannot be measured, or when the scope keeps no window, which screens nothing rather than rejecting everything.

nirspipe.qc.metrics.windowed.passing_windows(raw_od: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float, sci_cutoff: float, psp_cutoff: float | None, window_s: float = SCREEN_WINDOW_S, scope: list[tuple[str, float, float]] | None = None) → dict#

good_window_fraction(), also handing back the grid it counted over.

-> {"fractions": {ch: 0.77, ...}, "mask": (n_ch, 390), "centers": (390,),
    "sci": (n_ch, 390), "psp": (n_ch, 390), "channel_order": [...]}

The share is what screening reads and the grid is what a figure over time draws, and they have to be the same measurement: a panel that recomputed its own mask could shade a window the verdict had counted the other way. The two matrices come back beside the mask for the same reason, since a series drawn over the carpet is explaining the holes in it.

A dict rather than a tuple because this is the extension point: a metric added to the screening has to reach the figures without every caller unpacking one more slot.

The scope restricts fractions only, never the grid, because a figure draws the whole recording and says which part of it counted. fractions is empty where good_window_fraction() returns an empty dict; mask is None when nothing could be measured at all, and psp is None when PSP alone could not be.

nirspipe.qc.metrics.windowed.condition_window_means(matrix: numpy.ndarray, centers: numpy.ndarray, windows: list[tuple[str, float, float]]) → dict[str, np.ndarray]#

Per-condition means of an already-computed windowed metric, by slicing it.

matrix (n_ch, 360) on a 10 s grid, a condition running 600 to 1500 s
-> {"task1": (n_ch,) means over the 90 columns whose centres fall inside it}

matrix is reduced over its last axis, so a per-channel metric (channels x windows) gives one value per channel and a single series (windows,) gives one scalar.

A condition keeps the windows whose centre falls inside it, the same rule the screening scope uses, so every per-condition number in a report comes off one grid and one filter.

Slicing rather than cutting the recording keeps the run’s grid: a condition cut into its own file is filtered against its own two edges and lands on a grid of its own.

A condition holding no whole window is left out rather than given an empty mean, which is what an annotation shorter than one window looks like.

centers may be either the window centres or the mne-nirs [start, end] pairs that attach_windowed_series() returns alongside each matrix; a pair array is collapsed to midpoints here by the same rule that function records. Without that, an (n, 2) array would broadcast against the window bounds and mask nothing correctly.