nirspipe.qc.metrics.gvtd#

GVTD: the global motion index, its threshold, and censoring on it.

The whole of GVTD is here, from the per-sample trace to the spans a run marks as BAD_gvtd, because the parts are only meaningful together: the threshold is defined against the trace, the censoring is defined against the threshold, and the channel set the figures print is the one the censoring used.

Every GVTD trace is taken over the channels the screening kept (gvtd_kept()). It asks how much the analysed data moved, so a rejected channel’s artefact is not counted, unlike SCI, whose mean keeps rejected channels so as not to be taken over channels picked for good SCI.

The other motion measures, spikes and the correction footprint, are in motion.py.

Module Contents#

Functions#

gvtd_timetrace

GVTD time trace: RMS across channels of the temporal derivative.

gvtd_threshold

GVTD motion threshold, histogram-mode [1].

gvtd_kept

names (every channel by default) without the ones in raw.info["bads"].

with_bads

A copy of raw with names marked bad, for a GVTD read off a file that carries no marks.

gvtd_channel_picks

Channels for the GVTD trace and carpet, plus the name to label the figure with.

gvtd_channel_blocks

The GVTD panel’s rows, canonical set first, as [(set name, channel names), ...].

gvtd_above_segments

Time spans above the run’s own GVTD threshold, as (onset, duration) pairs.

gvtd_censor_spans

Spans of a recording to censor on GVTD, and what censoring them costs.

window_grid

(samples per window, first sample, last sample) of each window on the shared grid.

compute_windowed_gvtd

Mean and p95 GVTD per non-overlapping window, unfiltered.

compute_windowed_filtered_gvtd

Mean and p95 GVTD per window on the motion-band bandpassed OD.

Data#

API#

nirspipe.qc.metrics.gvtd.logger = 'get_logger(...)'#
nirspipe.qc.metrics.gvtd.GVTD_MOTION_BAND = (0.02, 0.5)#
nirspipe.qc.metrics.gvtd.GVTD_N_STD = 10.0#
nirspipe.qc.metrics.gvtd.gvtd_timetrace(data: numpy.ndarray, sfreq: float, l_freq: float | None = None, h_freq: float | None = None, standardize_channels: bool = False) → numpy.ndarray#

GVTD time trace: RMS across channels of the temporal derivative.

A global motion index from the per-sample temporal derivative of all channels [1]:

\[g_i = \sqrt{\frac{1}{N} \sum_{j=1}^{N} \left(y_{j,i} - y_{j,i-1}\right)^2},\]

where \(N\) is the number of channels and \(y_{j,i}\) the value of channel \(j\) at sample \(i\). Two optional steps, both off by default: a motion-band bandpass before differencing (l_freq/h_freq) and per-channel standardization (standardize_channels).

Parameters:
  • data (np.ndarray) -- Channel-by-time array, typically optical density.

  • sfreq (float) -- Sampling frequency in Hz.

  • l_freq (float or None, optional) -- Edges of an order-4 Butterworth bandpass applied before differencing, to isolate the motion band. An h_freq at or above Nyquist (sfreq / 2) is dropped, degrading the bandpass to a high-pass.

  • h_freq (float or None, optional) -- Edges of an order-4 Butterworth bandpass applied before differencing, to isolate the motion band. An h_freq at or above Nyquist (sfreq / 2) is dropped, degrading the bandpass to a high-pass.

  • standardize_channels (bool, optional) -- If True, divide each channel’s derivative by its own SD before the RMS (DVARS-vstd analog ), so high-dynamic-range channels do not dominate the global value.

Returns:

GVTD trace, one value per timepoint (length n_times - 1).

Return type:

np.ndarray

Notes

Input NaN/inf are zeroed before filtering and differencing. The trace is non-negative by construction, matching the paper’s \(g_i > 0\).

References

nirspipe.qc.metrics.gvtd.gvtd_threshold(gvtd: numpy.ndarray, n_std: float = GVTD_N_STD) → float | None#

GVTD motion threshold, histogram-mode [1].

\[\sigma_L = \sqrt{\frac{1}{|L|} \sum_{g_i \in L} (g_i - m)^2}, \qquad L = \{\, g_i : g_i < m \,\},\]
\[\tau = m + n_\text{std}\,\sigma_L,\]

where \(m\) is the histogram mode (center of the tallest bin, bins ~ n/5) and \(\sigma_L\) is the left-tail std, the RMS spread of the points below the mode, which are free of motion-spike contamination.

Parameters:
  • gvtd (np.ndarray) -- GVTD time trace.

  • n_std (float, optional) -- Multiplier on the left-tail std.

Returns:

Motion threshold, or None if the trace has no positive values.

Return type:

float or None

References

nirspipe.qc.metrics.gvtd.gvtd_kept(raw: mne.io.Raw, names: list[str] | None = None) → list[str]#

names (every channel by default) without the ones in raw.info["bads"].

names ["S1_D1 760", "S2_D1 760"], bads ["S2_D1 760"]  ->  ["S1_D1 760"]
nirspipe.qc.metrics.gvtd.with_bads(raw: mne.io.Raw, names) → mne.io.Raw#

A copy of raw with names marked bad, for a GVTD read off a file that carries no marks.

nirspipe.qc.metrics.gvtd.gvtd_channel_picks(raw: mne.io.Raw, sep_bands=None, channel_set: str | None = None) → tuple[list[str], str]#

Channels for the GVTD trace and carpet, plus the name to label the figure with.

Defaults to the long channels, which is the set the analysis uses, so a run is judged on the channels it is built from. channel_set overrides that with "short" or "all" for a caller that has been told which set to use; only --gvtd-censor passes it, and the carpet draws every set and never overrides.

The fallback is about missing optode positions, not about a montage without long channels. An unregistered montage reports every separation as zero, so neither band claims anything and every channel is picked.

The returned label is the set actually used, which on the fallback differs from the one asked for.

Example: an n-channel montage with n_long long and the rest out of band returns (n_long names, "long"); the same call on an unregistered montage returns (n names, "all").

nirspipe.qc.metrics.gvtd.gvtd_channel_blocks(raw: mne.io.Raw, sep_bands=None) → list[tuple[str, list[str]]]#

The GVTD panel’s rows, canonical set first, as [(set name, channel names), ...].

The canonical block is what gvtd_channel_picks() selects and is the only one the reported scalars and the verdict come from. A second block follows when the montage has short channels the canonical set left out, so the panel can show their quality without changing the number the run is judged on:

montage of n_long long + n_short short  ->  [("long", n_long), ("short", n_short)]
montage with no short channels          ->  [("long", n_long)]
unregistered montage, no long channels  ->  [("all", n)]

The fallback set gets no second block: it already contains the short channels, and a row for a subset of the row above it would be read as a comparison between two independent sets.

nirspipe.qc.metrics.gvtd.gvtd_above_segments(raw_intensity: mne.io.Raw, sep_bands=None, picks: list[str] | None = None) → list[tuple[float, float]]#

Time spans above the run’s own GVTD threshold, as (onset, duration) pairs.

The same boolean _motion_metrics() counts for gvtd_pct_above_thresh, kept as spans so a view of part of the recording can count its own share against the threshold the whole run set. Recomputing the threshold on a piece would give each piece its own yardstick; see gvtd_threshold().

picks names the channel set; without it the canonical one. GVTD is an RMS across channels, so each set has its own trace and its own threshold and the spans of one set cannot be derived from another’s.

nirspipe.qc.metrics.gvtd.gvtd_censor_spans(raw_od: mne.io.Raw, n_std: float = 10.0, min_epoch_s: float = 30.0, sep_bands=None, channel_set: str | None = None) → tuple[list[tuple[float, float]], dict[str, Any]]#

Spans of a recording to censor on GVTD, and what censoring them costs.

Returns spans to mark, following [1], not data to replace: nothing here interpolates, zero-fills or averages over what it flags.

Measured on the channels the screening kept: an artefact on a rejected channel censors nothing. Two passes: every sample above the threshold is flagged, then every surviving stretch shorter than min_epoch_s.

The rule is about every survivor, not only the islands between artifacts. On a 100 s recording with artifacts at 20-22 s and 26-28 s and min_epoch_s=30, the 4 s island between them goes, and so does the 20 s head, which is also too short: spans [(0.0, 28.0)] and one surviving epoch of 72 s, where the threshold alone would have left three epochs.

n_std defaults to the same 10 the reports score with; changing it moves only the censoring.

Returns:

  • spans (list of (onset, duration)) -- In seconds, to attach as BAD_gvtd annotations.

  • metrics (dict) -- gvtd_censor_*: the threshold used, the censored percentage, how many spans, how many surviving epochs and how many seconds they hold.

References

nirspipe.qc.metrics.gvtd.window_grid(n_times: int, sfreq: float, window_s: float) → tuple[int, np.ndarray, np.ndarray]#

(samples per window, first sample, last sample) of each window on the shared grid.

The grid the windowed SCI/PSP use, so every series binned on it lands on the same time axis: ceil samples per window, whole windows only. Deriving it independently drifts, because window_s * sfreq is rarely an integer and the rounding difference accumulates over the recording:

n_times 100, sfreq 10, window_s 3  ->  (30, [0, 30, 60], [30, 60, 90])
nirspipe.qc.metrics.gvtd.compute_windowed_gvtd(raw_od: mne.io.Raw, window_s: float = 10.0) → tuple[np.ndarray, np.ndarray, np.ndarray]#

Mean and p95 GVTD per non-overlapping window, unfiltered.

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

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

Returns:

(mean, p95, center_times) per window: average and 95th-percentile GVTD in each window, and the window center times.

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray]

nirspipe.qc.metrics.gvtd.compute_windowed_filtered_gvtd(raw_od: mne.io.Raw, window_s: float = 10.0) → tuple[np.ndarray, np.ndarray, np.ndarray]#

Mean and p95 GVTD per window on the motion-band bandpassed OD.

Same as compute_windowed_gvtd(), but the OD is first bandpassed to the motion band (GVTD_MOTION_BAND) to isolate head-motion frequencies.

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

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

Returns:

(mean, p95, center_times) per window.

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray]