nirspipe.qc.subject.condition_views#

One QC view per condition, sliced out of the whole-run pass.

A recording preprocessed whole holds its conditions as annotations, so a per-condition view is a column selection out of the windowed metrics the run already computed, never a cut of the recording, so its numbers stay on the run’s filter and window grid. See condition_window_means().

Each view screens on its own stretch, so a channel passing in one condition and failing in another is named in the one it failed in. That is what a per-condition page is for. It is a view, not what the run did: the recording was processed under the run’s verdict, and the run’s own page carries it, one click away through the run index.

Module Contents#

Functions#

span_share

Share of [t0, t1) covered by (onset, duration) spans, or None without a window.

slice_record

A copy of the record whose sliceable per-channel metrics come from one condition.

with_condition_corr

The record with section’s HbO-HbR correlation replaced by the condition’s own.

condition_haemo_scalars

Scalars a condition can be given by recomputing on its own cut, not by slicing.

zoom_to_condition

A time-axis figure viewing only one condition, without recomputing it.

rescale_y_to_window

Re-fit every y axis of a time-axis figure to what is inside t0-t1.

window_view_spec

What rescale_y_to_window() would do to figure, measured but not applied.

apply_carpet_window

Narrow a carpet to one condition in place: time axis, GVTD rows, and their labels.

carpet_view_table

Each condition’s view of a carpet: its window, and its GVTD rows re-fitted to it.

condition_view_table

Every condition’s view of one run-wide figure, keyed by slug for a page to pick.

figure_leaks

Whole-run figures that survived onto a condition page, by value rather than by name.

carpet_window_spec

The carpet’s GVTD and IMU rows re-fitted to one condition, its heatmaps left alone.

condition_payloads

One viewer payload per condition, read out of the quality record.

slice_time_traces

A figure dict holding only the samples inside [t0, t1], scales untouched.

condition_slices_from_record

Per-condition per-channel metrics read out of a quality record, nothing recomputed.

condition_scalars

The scalars a condition’s panel prints, averaged over the channels it has.

condition_set_scalars

The condition’s optical-density averages over each channel set, for the split table.

span_counts

span_share again, with the number of spans that touch the window.

Data#

API#

nirspipe.qc.subject.condition_views.logger = 'get_logger(...)'#
nirspipe.qc.subject.condition_views.SLICEABLE = ('sci_win_per_channel', 'psp_per_channel', 'good_frac_per_channel', 'cv_per_channel', 'snr_per_chann...#
nirspipe.qc.subject.condition_views.UNSLICEABLE = ('cp_per_channel', 'heart_rate_per_channel', 'temporal_derivative_variance', 'sci_per_channel', 'hbo...#
nirspipe.qc.subject.condition_views.TIME_INVARIANT = ('ch_dist_per_channel',)#
nirspipe.qc.subject.condition_views.COND_SCALAR_KEYS = ('sci_win_mean', 'good_frac_mean', 'psp_mean', 'gvtd_filt_mean', 'gvtd_filt_p95', 'cv_mean', 'snr_me...#
nirspipe.qc.subject.condition_views.PSD_NFFT_CAP = 2048#
nirspipe.qc.subject.condition_views.span_share(spans, t0: float, t1: float) → float | None#

Share of [t0, t1) covered by (onset, duration) spans, or None without a window.

[(5.0, 2.0), (20.0, 4.0)], 0.0, 10.0  ->  0.2

The spans come from a boolean the whole run set, so restricting them to a window counts the run’s own verdict over that stretch rather than re-deciding it there.

nirspipe.qc.subject.condition_views.slice_record(record_view: dict, sliced: dict[str, dict[str, float]]) → dict#

A copy of the record whose sliceable per-channel metrics come from one condition.

sliced is {"sci_win_per_channel": {ch: v}, ...} for this condition. Every section’s per-channel dict is rebuilt from it, restricted to the channels that section already described, so a short channel’s row stays in the short section and a long channel’s in the long one; channel_rows() reads the sections and would otherwise put every channel in both.

The unsliceable keys are dropped rather than carried over. Scalars are left alone: the caller replaces those it has condition values for.

nirspipe.qc.subject.condition_views.with_condition_corr(record: dict, corr_per_channel: dict[str, float], section: str = 'preproc') → dict#

The record with section’s HbO-HbR correlation replaced by the condition’s own.

channel_rows reads that column off the whole-file preproc section, or rawhaemo on a raw-only record, so that is where a value measured on the cut has to land. An empty dict leaves the column dashed.

nirspipe.qc.subject.condition_views.condition_haemo_scalars(haemo, errts, n_fft_floor: int, bands: dict, filtered=None) → dict#

Scalars a condition can be given by recomputing on its own cut, not by slicing.

haemo and errts are already cropped to the condition. None of these filters, so a cut carries no edge the whole run would not have had; compute_psd is Welch, which segments and tapers. What a cut does change is the spectrum’s resolution, and only once the cut is shorter than the transform: below n_fft_floor samples the band metrics are computed over a coarser frequency grid than the run’s and are left out instead.

3053 samples, floor 2048  ->  band metrics included
1200 samples, floor 2048  ->  band metrics absent

Returns the scalars plus hbo_hbr_corr_per_channel, measured on the same cut so the caller does not pay for it twice.

nirspipe.qc.subject.condition_views.zoom_to_condition(figure, t0: float, t1: float)#

A time-axis figure viewing only one condition, without recomputing it.

zoom_to_condition(carpet_figure_dict, 600.0, 1500.0)

This is how the carpet, the GVTD trace and the raw timeseries go per condition. A cropped recording would make the figure builders re-derive what they compute run-wide (the GVTD filter, each channel’s z-scale, the threshold), so the run is measured once and the view is narrowed. Every x axis in the layout is set, because the carpet is stacked subplots sharing a time axis and leaving one unset would show a panel at a different span from the one above it.

Takes either a plotly figure or its dict form, since the two report paths hold different ones: the raw viewer inlines figure dicts, and the subject report keeps plotly objects to save as files. One function for both, so the two paths cannot drift. A figure object is narrowed in place and returned; a dict is returned narrowed as a copy, because the run’s own dict is written out as well.

nirspipe.qc.subject.condition_views.rescale_y_to_window(figure, t0: float, t1: float)#

Re-fit every y axis of a time-axis figure to what is inside t0-t1.

rescale_y_to_window(motion_detail_fig, 22.4, 322.4)

The companion of zoom_to_condition(), which narrows the view along time and leaves the y axes pinned to the whole run, where a quiet condition’s row reads as a flat line. The row labels carry this window’s maximum and the run’s beside it, so a reader is told which scale they are on.

Shaded spans follow the new range instead of setting it: they are frames drawn to the row’s height, not measurements. A row with no trace inside the window is left alone.

nirspipe.qc.subject.condition_views.window_view_spec(figure, t0: float, t1: float) → dict#

What rescale_y_to_window() would do to figure, measured but not applied.

window_view_spec(motion_detail_fig, 22.4, 322.4)
-> {"x": [22.4, 322.4],
    "y": {"yaxis2": (0.0, 0.0031)},
    "bands": [{"i": 0, "lo": 0.0, "hi": 0.0031}],
    "notes": [{"yref": "y2", "xref": "x", "text": "max 0.0031 · run 0.017"}]}

A figure written once per channel rather than once per condition carries a table of these and lets the page pick one by URL fragment, so the numbers have to come out separately from the figure they were measured on. rescale_y_to_window applies one, which is what keeps the saved-per-condition and the picked-at-load paths from drifting: there is one measurement and two ways of spending it.

nirspipe.qc.subject.condition_views.SCALE_SLOT = 'qc-scale-'#
nirspipe.qc.subject.condition_views.apply_carpet_window(figure, t0: float, t1: float)#

Narrow a carpet to one condition in place: time axis, GVTD rows, and their labels.

The companion of carpet_window_spec(), which measures the same thing and hands it over instead. Both exist for the reason rescale_y_to_window and window_view_spec both do: the saved file is written once and a page picks a view out of it at load, so the applied and the picked path have to be one measurement or they drift. Nothing in the report calls this; the browser test that proves the two paths render alike does.

nirspipe.qc.subject.condition_views.carpet_view_table(fig, spans: list[tuple[str, float, float]]) → dict | None#

Each condition’s view of a carpet: its window, and its GVTD rows re-fitted to it.

The heatmaps keep the run’s colour scale and only the line rows move; carpet_window_spec() says why, and computes each view.

nirspipe.qc.subject.condition_views.condition_view_table(fig, spans: list[tuple[str, float, float]]) → dict | None#

Every condition’s view of one run-wide figure, keyed by slug for a page to pick.

condition_view_table(detail_fig, [("video", 60.0, 300.0)])
-> {"video": {"x": [60.0, 300.0], "y": {...}, "bands": [...], "ann": {...}}}

The result is what _HASH_VIEW_JS spends: the file is written once and a condition page addresses it as …/figure.html#video, so the run’s figure and a condition’s view of it cannot disagree.

The scale notes go on as empty annotations first, one per row any condition writes one on, because a relayout cannot append an annotation. A row no condition annotates gets no slot, and a condition reaching the run’s own maximum leaves its slot empty.

spans must be on the same axis the figure was drawn on. Most figures here are drawn on the data axis; a caller whose figure is on the original recording’s axis shifts the spans by first_time before calling.

nirspipe.qc.subject.condition_views.CONDITION_PAGE_FIGURES = ('trigger',)#
nirspipe.qc.subject.condition_views.figure_leaks(figure_paths: dict, slug: str) → list[str]#

Whole-run figures that survived onto a condition page, by value rather than by name.

{"psd": {"src": "figures/sub-01_desc-rawpsd_nirs.html"}}, "video"  ->  ["psd"]

A figure reaches a condition page one of three ways, and each leaves a mark: rewritten for the condition (its cond- entity is the slug), addressed at the condition’s window (…#<slug>), or kept whole on purpose (CONDITION_PAGE_FIGURES). Anything else is the run’s figure sitting under this condition’s numbers with nothing to say so.

Checking the assembled values rather than a list of keys also catches a figure added later.

nirspipe.qc.subject.condition_views.carpet_window_spec(figure, t0: float, t1: float) → dict#

The carpet’s GVTD and IMU rows re-fitted to one condition, its heatmaps left alone.

carpet_window_spec(carpet_fig, 22.4, 322.4)
-> {"x": [22.4, 322.4],
    "y": {"yaxis2": (0.0, 0.0061), "yaxis3": (0.0, 0.0061)},
    "bands": [{"i": 1, "lo": 0.0, "hi": 0.0061}, ...],
    "notes": [{"name": "gvtd-stat-long", "text": "max 2.36e-03 ..."}, ...]}

Two things differ from window_view_spec().

One top for every GVTD row, by the figure’s own rule. gvtd_y_top is called on this window’s samples, so the view is drawn the way the panel is drawn: a 99.5th percentile rather than a maximum, and never below the threshold rule. Long and short share the number, so the difference between the two rows stays visible.

The heatmaps are not touched. Their colour is a z-score against a per-channel mean and SD taken over the whole run from the uncorrected side, and both carpets use those same two numbers, so one colour means one deviation on every page. The lines print the scale they are on in the rewritten stat labels below.

nirspipe.qc.subject.condition_views.condition_payloads(payload: dict, *, record: dict, by_condition: dict, sci_scores: dict[str, float], bad_channels: set[str], channel_pairs: list[str] | None, series: dict, sci_threshold: float, cutoffs: dict[str, float], trial_rows: list | None = None, save_figure=None, remake_psd=None, remake_epoch=None, trial_images=None, save_stack=None, remake_hbo_hbr_corr=None, sep_bands=None, has_spans: bool = False, remake_brain=None, remake_head_slider=None) → list[tuple[str, dict]]#

One viewer payload per condition, read out of the quality record.

Every number here comes from by_condition, which condition_sections() wrote; nothing is measured. A record with no such section gets no pages rather than a second copy of the numbers free to disagree with the first.

payload is the run’s payload, whose parts that do not vary by condition (the separation notes) are carried over untouched. A figure gets here one of three ways, and which one is a property of the figure:

  • rewritten for the condition: the SCI/PSP panel and the channel-quality grid. Both are handed their data and derive nothing from a recording, so a real slice is correct and save_figure writes each under a name carrying the condition’s slug. The brain figure too, through remake_brain, graded by the condition’s own SCI; and the head slider, through remake_head_slider, on the matrices cut to the condition’s windows.

  • addressed at the condition’s window: the carpet, the per-channel detail and the per-channel motion figure. Each derives something run-wide from what it is handed, a filtered GVTD, a per-channel z-scale, a colour range, so a cut would give every condition a scale no other one can be read against. The run’s file carries every window and this page asks for one by URL fragment.

  • taken out of the run’s own pass: the trial images. One run-wide pass drew every condition’s, so the pages share a colour scale.

  • rebuilt on a cropped copy: the spectrum and the grand mean, through remake_psd and remake_epoch. There is nothing to slice, it being one spectrum rather than a time-by-frequency matrix, and nothing in a Welch estimate reads outside the samples it is handed. A condition too short for the transform gets no spectrum rather than one on a coarser grid than the run’s. The HbO-HbR correlation panel too, through remake_hbo_hbr_corr, for the reason the record measures its numbers on the cut.

The event timeline is the run’s own figure, left whole: it is the schedule the whole recording ran to. The per-trial table is cut to the trials whose onset falls inside the window; nothing is rescored, a trial’s SQM reading nothing outside its own crop.

Each payload carries three notes, because each is a way a reader could be misled: what the view is, that its rejected channels are the run’s while the header counts the ones failing on this condition’s windows alone, and that the columns with no windowed series behind them are absent rather than zero.

has_spans says the recording carries BAD_ spans; each summary then states the condition’s kept seconds, as the subject report’s condition pages do. A condition with no counted window, which only spans leave, is marked unassessed.

nirspipe.qc.subject.condition_views.slice_time_traces(figure: dict, t0: float, t1: float) → dict#

A figure dict holding only the samples inside [t0, t1], scales untouched.

n traces of 4427 points, 3602.4 to 3902.5 s  ->  n traces of ~1300

The companion of zoom_to_condition(). Narrowing sets the axis and leaves the run’s samples in the trace, which is what a figure deriving something internally needs; it also means Plotly’s own double-click, which autoranges over the data it holds, opens the whole run. Slicing removes that, so it is right wherever the y values do not depend on which samples are present. The raw-signal panel qualifies: each channel was z-scored and offset over the whole recording before the figure was built, so both numbers are already baked in and dropping samples moves nothing.

Marker rectangles drawn against the data axis are dropped when they fall outside, since a shape at 22 s would pull an autorange back to the start of the recording. The striped channel bands are on paper and stay.

nirspipe.qc.subject.condition_views.condition_slices_from_record(record: dict, ch_names: list[str], windows: list[tuple[str, float, float]], sci_cutoff: float, psp_cutoff: float | None, spans=()) → dict[str, dict[str, dict[str, float]]]#

Per-condition per-channel metrics read out of a quality record, nothing recomputed.

{"game1": {"sci_win_per_channel": {...}, "psp_per_channel": {...},
           "good_frac_per_channel": {...}}, ...}

The record’s windowed section already stores the channel-by-window SCI and PSP matrices and their window bounds, so a condition is a column selection out of what the run measured once.

The passing-window share is rebuilt here rather than read, because the record stores only its whole-run value. It goes through passing_mask_from_matrices(), the same AND the screening applied, so a condition’s share cannot disagree with the verdict the run was screened by. Both cutoffs have to be the run’s own; passing anything else produces a number no channel was judged against.

spans are the run’s BAD_ spans as (start, stop): a window touching one is left out of the share, as the screening leaves it out, while the SCI, PSP and CV means keep every window, as the run’s own means do.

Returns an empty dict when the record carries no stored matrices: the values cannot be recovered from the whole-run scalars.

nirspipe.qc.subject.condition_views.condition_scalars(sliced: dict[str, dict[str, float]], gvtd_means: dict[str, float] | None = None, *, shares: dict[str, float | None] | None = None, n_frames: dict[str, int | None] | None = None, n_segments: dict[str, int] | None = None, retention: float | None = None) → dict[str, float | None]#

The scalars a condition’s panel prints, averaged over the channels it has.

Only those with a windowed series or a stored span list behind them, see COND_SCALAR_KEYS. A key the condition has no values for comes back None, which the metric registry prints as a dash rather than as a zero.

gvtd_means are per-window series already averaged over the condition’s columns; shares, n_frames and n_segments are the same span lists counted three ways, because the report prints a fraction, a frame count and a segment count off one boolean.

nirspipe.qc.subject.condition_views.condition_set_scalars(sliced: dict[str, dict[str, float]], bad_channels: set[str], long_names: list[str], short_names: list[str]) → dict[str, dict[str, float | None]]#

The condition’s optical-density averages over each channel set, for the split table.

sliced["sci_win_per_channel"] over a montage of long and short channels
-> {"all": {...}, "long": {...}, "short": {...}}

Every metric here is a mean over the row’s channels, which is the whole of what a set means for them. GVTD is not one of them and does not belong here: it is an RMS across channels, so its sets are three separate measurements rather than three groupings of one, and it is reported in the motion panel where its before and after sit side by side.

A set with no channels comes back with every value None rather than being left out, so the table keeps its three rows on a montage that has only long channels.

nirspipe.qc.subject.condition_views.span_counts(spans, t0: float, t1: float) → tuple[float | None, int]#

span_share again, with the number of spans that touch the window.

[(5.0, 2.0), (20.0, 4.0)], 0.0, 10.0  ->  (0.2, 1)

The report prints a fraction and a segment count off the same boolean, and counting the spans here keeps the two from being derived in two places.