nirspipe.qc.metrics.screening#

Which channels get rejected, and on what grounds.

One table, CRITERIA, lists every per-channel number the reports carry. A channel is rejected if it fails any criterion whose screens is set, so the screening half of the table is a union and its entries are independent: dropping a criterion is deleting its line, adding one is adding a line plus a scorer. Everything that prunes goes through screen_channels(), so the prep pipeline, the raw QC report, the per-trial scoring and the dyad path cannot end up screening on different things.

SCI and PSP are measured and reported but do not decide. good_frac counts the short windows in which a channel’s SCI clears its line, and its PSP too when the run gives PSP a line; the two whole-run numbers stay in the table because every report prints them.

The scores themselves are measured in nirspipe.qc.metrics.coupling; this module only decides. Cutoffs live in nirspipe.qc.metrics._helpers beside the other per-channel lines.

Module Contents#

Classes#

Criterion

One screening criterion: what it measures, where its line is, and how to score it.

Functions#

criterion_cutoffs

Each criterion’s line for this run: its default, overridden from config.

resolve_cutoffs

criterion_cutoffs() with loose per-criterion overrides laid on top.

screening_scores

Every criterion’s per-channel scores, measured on optical density.

unmeasured

The labels of the screening criteria that came back empty.

require_screened

Refuse a recording whose screening criteria came back unmeasured.

screening_gap

Why screening cannot judge a recording, or None when it can.

screen_channels

The rejected channels, and which criteria each one failed.

Data#

API#

nirspipe.qc.metrics.screening.logger = 'get_logger(...)'#
class nirspipe.qc.metrics.screening.Criterion#

One screening criterion: what it measures, where its line is, and how to score it.

scorer takes optical density and the cardiac band and returns {channel: score}. cutoff None is a criterion with no line unless a run gives one. config_field names the PrepConfig / CLI field a run may override cutoff with, or None for a criterion with no flag. Every criterion so far fails low, which is what fails_below records; a criterion that fails high would set it False.

screens is whether failing this criterion rejects the channel. False means measured and reported only. needs_context marks a scorer that is handed a fourth argument, the run’s screening context: the resolved cutoffs under cutoffs and the stretches of the recording that count under scope. That is how a criterion built out of other criteria’s lines gets them without reaching for a global.

name: str = None#
label: str = None#
cutoff: float | None = None#
scorer: Callable[..., dict[str, float]] = None#
config_field: str | None = None#
fails_below: bool = True#
screens: bool = True#
needs_context: bool = False#
failures(scores: dict[str, float], cutoff: float | None = None) → list[str]#
nirspipe.qc.metrics.screening.CRITERIA: tuple[nirspipe.qc.metrics.screening.Criterion, ...] = ()#
nirspipe.qc.metrics.screening.criterion_cutoffs(config: object | None = None) → dict[str, float | None]#

Each criterion’s line for this run: its default, overridden from config.

criterion_cutoffs(config_with_sci_threshold_0_9) -> {“sci”: 0.9, “psp”: None, …}

A criterion with no config_field, or a config that does not carry it, keeps the default. Passing None gives the defaults, which is what a caller with no run config has.

nirspipe.qc.metrics.screening.resolve_cutoffs(config: object | None = None, **overrides) → dict[str, float | None]#

criterion_cutoffs() with loose per-criterion overrides laid on top.

resolve_cutoffs(sci=0.9, psp=None) -> {“sci”: 0.9, “psp”: None, …}

For a caller holding the lines as separate floats rather than as a config object, which is every CLI command and every report builder. A None override is not a value, so it keeps whatever the config or the default gave; an unknown criterion name is a typo the screening would silently ignore, so it raises.

nirspipe.qc.metrics.screening.screening_scores(raw_od: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float, have: dict[str, dict[str, float]] | None = None, cutoffs: dict[str, float] | None = None, scope: list[tuple[str, float, float]] | None = None) → dict[str, dict[str, float]]#

Every criterion’s per-channel scores, measured on optical density.

have is whatever the caller measured already, keyed by criterion name; those are passed through rather than recomputed, which is what keeps a caller that needs SCI for its own reasons from paying for it twice.

cutoffs is only read by criteria built out of other criteria’s lines, and defaults to the table’s own. Pass the same dict here and to screen_channels(), or a run would count windows against one SCI line and colour its report against another.

scope is the stretches of the recording that count, [(label, tstart, tstop)], and None counts all of it. It reaches the window-counting criterion only; nothing else in the table has a time axis to restrict.

nirspipe.qc.metrics.screening.unmeasured(scores: dict[str, dict[str, float]]) → list[str]#

The labels of the screening criteria that came back empty.

nirspipe.qc.metrics.screening.require_screened(scores: dict[str, dict[str, float]], name: str) → None#

Refuse a recording whose screening criteria came back unmeasured.

screening_scores() leaves a criterion it cannot measure empty, which rejects nothing; a caller screening a whole recording must not read that as every channel passing. Per-trial scoring tolerates it on purpose and does not call this.

nirspipe.qc.metrics.screening.screening_gap(raw_od: mne.io.Raw | None, scores: dict[str, dict[str, float]], scope=None) → str | None#

Why screening cannot judge a recording, or None when it can.

The two conditions preprocessing refuses on: fewer than two counted windows, or a criterion that came back unmeasured. The raw QC reports print the reason and reject nothing on screening instead of stopping.

nirspipe.qc.metrics.screening.screen_channels(scores: dict[str, dict[str, float]], cutoffs: dict[str, float] | None = None) → tuple[list[str], dict[str, list[str]]]#

The rejected channels, and which criteria each one failed.

screen_channels({“good_frac”: {“S1_D1 760”: 0.4}}) -> ([“S1_D1 760”], {“S1_D1 760”: [“passing windows”]})

The union over the criteria that screen: one failed is enough. Criteria with screens unset are scored and reported but reject nothing, so a channel with a poor whole-run SCI that still passes enough windows is kept. The second half is what lets a report say why a channel went rather than only that it did.