nirspipe.qc.figures.common.motion_panel#

Motion annotation figures: carpet + GVTD combined, bad segment zoom, per-channel detail.

The OD/GVTD carpet is the pre-processing (raw) motion view; post-denoising uses carpet_compare_figure.

Module Contents#

Functions#

gvtd_y_top

The top every GVTD row on a carpet panel shares, over whatever stretch it is given.

imu_y_top

An IMU row’s top: the GVTD rows’ percentile cap, so one jolt cannot flatten the rest.

carpet_coloraxis

The coloraxis every carpet is drawn on, so no view invents its own scale.

carpet_z

Per-channel z-scored optical density in at most CARPET_MAX_PTS columns, for a Heatmap.

add_carpet

One carpet image with its channel-set side bars, on the figure’s shared coloraxis.

roi_colours

{ROI label: colour}, keyed by the whole map so a ROI keeps its colour on every carpet.

add_roi_strip

A colour per ROI down the carpet’s left edge, outside the channel-set bar.

add_roi_legend

A square per drawn ROI in the figure’s legend, in the map’s order.

carpet_gvtd_figure

Motion-band GVTD + per-channel z-scored OD carpet, on one shared time axis.

bad_segment_zoom_figure

Zoom into top-N bad segments; show before (optional) and after correction.

build_motion_detail_figure

4-row per-channel motion figure: GVTD, this channel’s derivative, before/after OD, band strip.

Data#

API#

nirspipe.qc.figures.common.motion_panel.logger = 'get_logger(...)'#
nirspipe.qc.figures.common.motion_panel.GVTD_STAT_SLOT = 'gvtd-stat-'#
nirspipe.qc.figures.common.motion_panel.gvtd_y_top(traces: list[np.ndarray], thresholds: list[float | None]) → float#

The top every GVTD row on a carpet panel shares, over whatever stretch it is given.

gvtd_y_top([long_before, long_after, short_before, short_after], [4.8e-4, 3.4e-4])
-> 0.0127

A percentile rather than a maximum, so the top 0.5% may run off the canvas. Never below a row’s threshold, since the line a row is judged against has to stay visible.

One number for every row, since long and short are the same unit.

Public because a per-condition view recomputes it over that condition’s window.

build_motion_detail_figure deliberately draws the same series against its own maximum instead, its GVTD row being read as a pair with the derivative row under it.

nirspipe.qc.figures.common.motion_panel.IMU_SLOT = 'imu-row-'#
nirspipe.qc.figures.common.motion_panel.imu_y_top(y: numpy.ndarray) → float#

An IMU row’s top: the GVTD rows’ percentile cap, so one jolt cannot flatten the rest.

nirspipe.qc.figures.common.motion_panel.CARPET_MAX_PTS = 2000#
nirspipe.qc.figures.common.motion_panel.CARPET_Z = 3.0#
nirspipe.qc.figures.common.motion_panel.carpet_coloraxis(z_threshold: float = CARPET_Z, y: float = 0.35) → dict#

The coloraxis every carpet is drawn on, so no view invents its own scale.

nirspipe.qc.figures.common.motion_panel.carpet_z(data: numpy.ndarray, times: numpy.ndarray, z_threshold: float = CARPET_Z, stats: tuple[np.ndarray, np.ndarray] | None = None)#

Per-channel z-scored optical density in at most CARPET_MAX_PTS columns, for a Heatmap.

carpet_z(od[:, :39611], times)  ->  (z (44, 1981), t (1981,), (mean, std))

Each row is detrended before it is scaled, so the grey is a fluctuation about the row’s own trend and not where on a slow drift the sample sits. The display copy only.

A column is one bin of samples, drawn at the bin’s centre as its sample of largest |z|, so a burst shorter than a column still shows, as the GVTD line above keeps its peaks.

Returns the per-channel mean and SD alongside, so a second carpet of the same recording after a correction can be z-scored by the uncorrected numbers: rescaling it by its own SD would divide out the shrinkage the comparison exists to show. Pass them back as stats to do that. That carpet is still detrended by its own fit. 2 dp because the colour scale cannot resolve more. NaN frames (a channel absent from some joined runs) stay blank and are left out of the fit, the mean and the SD.

nirspipe.qc.figures.common.motion_panel.add_carpet(fig, row: int, z: numpy.ndarray, t: numpy.ndarray, labels: list[str], spans: list) → None#

One carpet image with its channel-set side bars, on the figure’s shared coloraxis.

The channel names stay in the hover rather than on the axis, where a full montage would be an unreadable stack, and the axis is reversed so row 0 of z is the top row.

nirspipe.qc.figures.common.motion_panel.roi_colours(roi_map: dict) → dict#

{ROI label: colour}, keyed by the whole map so a ROI keeps its colour on every carpet.

nirspipe.qc.figures.common.motion_panel.add_roi_strip(fig, row: int, groups: list, n_rows: int, colours: dict) → None#

A colour per ROI down the carpet’s left edge, outside the channel-set bar.

groups is [(label, first row, last row), ...] counted over the whole stacked image.

nirspipe.qc.figures.common.motion_panel.add_roi_legend(fig, drawn: set, colours: dict) → None#

A square per drawn ROI in the figure’s legend, in the map’s order.

nirspipe.qc.figures.common.motion_panel.carpet_gvtd_figure(raw: mne.io.Raw, ch_names: list[str], z_threshold: float = 3.0, corrected_segments: list[tuple[float, float]] | None = None, spike_segments: dict[str, list] | list[tuple[float, float]] | None = None, raw_after: mne.io.Raw | None = None, channel_set: str | None = None, blocks: list[tuple[str, list[str]]] | None = None, imu: dict[str, ImuTrace] | None = None, roi_map: dict | None = None) → plotly.graph_objects.Figure#

Motion-band GVTD + per-channel z-scored OD carpet, on one shared time axis.

roi_map ({ROI label: [channel names]}) groups the canonical block’s rows by ROI, marked by a coloured strip named in the legend; the other blocks keep montage order.

imu is {"gyro": ImuTrace, "accel": ImuTrace} at the aux rate, from imu_traces(). Each sensor present is a row above the GVTD rows, the one record of movement that does not come from the optical data; a sensor the recording lacks gets no row rather than an empty one.

Only the motion-band GVTD is drawn, since the unfiltered trace is dominated by the cardiac component; gvtd_mean and gvtd_p95 still report it. corrected_segments (motion-correction footprint) is a bar on a thin strip at the very top, over the IMU row where there is one and otherwise directly over the trace. spike_segments shades the GVTD panel behind the trace, and is either one span list for the canonical row or {set name: spans} so each row shades what was found on its own channels. The test behind a span is “>= 10% of these channels spiking”, so a long-set span is not a statement about the short ones and the two are never shared. Both are drawn span by span with no merging, since the gap between two spans is the claim that nothing happened there. Neither is derived from the trace: spikes are a per-channel robust outlier test on the same band-limited derivative, so shading without a peak over the threshold, or the reverse, is a real disagreement between a per-channel and a cross-channel reading rather than a drawing error.

Given raw_after, the motion-corrected recording, every GVTD row carries a second trace and a second carpet is stacked under the first, so the figure answers whether the correction removed what it was there to remove.

Everything the comparison is read against stays fixed to the uncorrected side: the threshold line, and the per-channel mean and SD both carpets are z-scored by. Re-deriving either from the corrected data would hide a correction that shrank the signal. A raw_after that does not cover the same channels for the same duration at the same rate is dropped rather than drawn (see _matched_od_after).

blocks is [(set name, channel names), ...] with the canonical set first, normally [("long", ...), ("short", ...)] from gvtd_channel_blocks(). Each gets its own GVTD row and its own block of carpet rows, so short-channel quality can be read off the same panel without entering the number the verdict is taken from. The sets stay separate traces rather than one trace over the union, and the rows share a y range (see gvtd_y_top()). Only the canonical row carries the segments labels, since it is the one the reported scalars come from. Omitting blocks draws the single row channel_set names, over ch_names.

nirspipe.qc.figures.common.motion_panel.bad_segment_zoom_figure(raw_after: mne.io.Raw, bad_segments: list[tuple[float, float]], ch_names: list[str], raw_before: mne.io.Raw | None = None, n_top: int = 10, padding: float = 15.0) → str | None#

Zoom into top-N bad segments; show before (optional) and after correction.

Returns a base64-encoded PNG string, or None if there are no segments. Each column = one segment; rows = before (if raw_before given) / after.

nirspipe.qc.figures.common.motion_panel.build_motion_detail_figure(raw_od_before: mne.io.Raw, raw_od_after: mne.io.Raw, ch_name: str, segments: dict | None = None, max_pts: int = _LINE_MAX_PTS, corrected_segments: list[tuple[float, float]] | None = None, spike_segments: list[tuple[float, float]] | None = None, gvtd_picks: list[str] | None = None, gvtd_set: str | None = None, imu: dict[str, ImuTrace] | None = None) → plotly.graph_objects.Figure#

4-row per-channel motion figure: GVTD, this channel’s derivative, before/after OD, band strip.

imu adds the IMU rows above the GVTD row, under the strip, as on the carpet panel.

Both inputs must be in OD space (output of optical_density()). The bottom strip shows the motion-correction footprint and spike timepoints, never overlapping the traces.

Row 2 is this channel’s band-limited |dOD/dt|, the signal the spike marks in the strip are detected on, so a peak there and a mark below it refer to the same event. It is a per-channel view of the spike detector, not a per-channel GVTD: GVTD is defined across channels [1] and has no single-channel form. Both rows are band-limited to GVTD_MOTION_BAND before differencing, since the unfiltered derivative is dominated by the ~1 Hz cardiac component and shows pulse rather than movement.

gvtd_picks is the channel set row 1 averages and gvtd_set names it on the panel. The caller passes the separation class ch_name itself belongs to, because rows 1 and 2 are read as a pair and the two classes are not on one scale. spike_segments has to come from the same class for the same reason. Left unset, row 1 covers every channel, which matches no other panel in the report.

References