nirspipe.qc.metrics._helpers#
Pieces more than one metric family needs, and nothing that measures anything itself.
_safe_metrics declares an output schema, _mask_to_segments turns a per-sample mask
into time spans, and long_short_channels is the package’s one definition of what a long
channel is. All three are shared, so they sit below every family and import none of them.
Module Contents#
Functions#
|
|
|
|
|
|
Refuse a set of bands that does not describe two separated ranges. |
|
The whole triple in one line, for a log message or an error. |
|
The separations in neither band: |
|
The bands as record scalars, in mm: |
|
The bands a stored record was split with, read back from its |
|
Whether a record stamps its own bands, which a record whose raw section failed does not. |
|
Split channel names by source-detector separation, returning |
|
|
|
Picks for one chromophore with short-distance reference channels dropped. |
|
Channels in neither separation band, name -> separation in mm. |
|
The recording in optical density: itself when it already is, else a converted copy. |
|
Refuse a cardiac band the recording’s sampling rate cannot carry. |
|
Events that can actually be epoched over |
|
Condition -> its trials over |
Data#
API#
- nirspipe.qc.metrics._helpers.logger = 'get_logger(...)'#
- nirspipe.qc.metrics._helpers.SHORT_MAX_DIST = 0.01#
- nirspipe.qc.metrics._helpers.LONG_MIN_DIST = 0.015#
- nirspipe.qc.metrics._helpers.LONG_MAX_DIST = None#
- nirspipe.qc.metrics._helpers.SEP_TOLERANCE = 1e-05#
- nirspipe.qc.metrics._helpers.Bands = 'tuple[float, float, float | None]'#
- nirspipe.qc.metrics._helpers.in_short_band(d: float, sep_bands: nirspipe.qc.metrics._helpers.Bands) bool#
d(m) is a short separation, withinSEP_TOLERANCEof the edge.
- nirspipe.qc.metrics._helpers.in_long_band(d: float, sep_bands: nirspipe.qc.metrics._helpers.Bands) bool#
d(m) is a long separation, withinSEP_TOLERANCEof either edge.
- nirspipe.qc.metrics._helpers.separation_bands(config: object | None = None) nirspipe.qc.metrics._helpers.Bands#
(short_max, long_min, long_max)in metres;long_maxNone means no upper bound.separation_bands(config_with_long_max_dist_0_045) -> (0.01, 0.015, 0.045)
An accessor rather than three imports, so a caller cannot pick up two of the three and silently invent a fourth band. A config that does not carry a field, or carries None for it, keeps the default; passing None gives the defaults, which is what a caller with no run config has.
long_maxis the exception on both counts, since None is a value there rather than an absence, sobands_from_record()is what reads a stored one back.
- nirspipe.qc.metrics._helpers.validate_bands(sep_bands: nirspipe.qc.metrics._helpers.Bands) nirspipe.qc.metrics._helpers.Bands#
Refuse a set of bands that does not describe two separated ranges.
validate_bands((0.01, 0.005, None)) -> ValueError
The three are independent flags, so nothing stops a caller moving one and leaving the others: overlapping bands would put a channel in both lists at once, and an upper bound under the lower one would empty the long list without saying why.
- nirspipe.qc.metrics._helpers.bands_phrase(sep_bands: Bands | None = None) str#
The whole triple in one line, for a log message or an error.
(0.01, 0.015, None) -> “short <= 10 mm, long >= 15 mm”
- nirspipe.qc.metrics._helpers.unclaimed_separations(sep_bands: Bands | None = None) str#
The separations in neither band:
"10-15 mm", or"10-15 mm, or over 45 mm".
- nirspipe.qc.metrics._helpers.BANDS_RECORD_KEYS = ('sep_short_max_mm', 'sep_long_min_mm', 'sep_long_max_mm')#
- nirspipe.qc.metrics._helpers.bands_to_record(sep_bands: nirspipe.qc.metrics._helpers.Bands) dict#
The bands as record scalars, in mm:
(0.01, 0.015, None) -> {"sep_short_max_mm": 10.0, "sep_long_min_mm": 15.0, "sep_long_max_mm": None}
Stamped so a reader can tell which bands produced the split, whatever the defaults are.
- nirspipe.qc.metrics._helpers.bands_from_record(scalars: dict) nirspipe.qc.metrics._helpers.Bands#
The bands a stored record was split with, read back from its
rawsection.A key the record does not carry, as on a stage that does not stamp them, falls back to the default. A key present and null is not missing: it is an upper bound deliberately switched off, so the two cases are told apart by presence.
- nirspipe.qc.metrics._helpers.record_has_bands(scalars: dict) bool#
Whether a record stamps its own bands, which a record whose raw section failed does not.
{“sep_short_max_mm”: 10.0, “sep_long_min_mm”: 15.0, “sep_long_max_mm”: None} -> True
bands_from_record()falls back to the defaults for a missing key, so a caller comparing two records has to be able to tell that apart from two records that genuinely agree. All three keys or none: the writer always writes the three together.
- nirspipe.qc.metrics._helpers.SCI_PASS = 0.8#
- nirspipe.qc.metrics._helpers.GOOD_FRAC_PASS = 0.75#
- nirspipe.qc.metrics._helpers.CV_PASS = 0.05#
- nirspipe.qc.metrics._helpers.SNR_PASS = None#
- nirspipe.qc.metrics._helpers.CARDIAC_TRANSITION_HZ = 0.3#
- nirspipe.qc.metrics._helpers.long_short_channels(raw: mne.io.Raw, sep_bands: Bands | None = None) tuple[list[str], list[str]]#
Split channel names by source-detector separation, returning
(long, short).One definition for the whole package, so the report, the raw QC figures and the SQM record agree on which channels are which. Because the two ranges do not meet (see SHORT_MAX_DIST / LONG_MIN_DIST), the two lists need not cover every channel:
distances 8, 12, 30, 50 mm -> long ["30mm", "50mm"], short ["8mm"]
sep_bandsis this run’s separations fromseparation_bands(); None takes the package defaults. Every caller in one run has to pass the same value, or the reports would describe a different montage than the regression used.A montage with no registered optode positions reports every distance as zero, which would make every channel short; that case is logged and yields no split at all.
A channel in neither band is named in a warning rather than passed over. It takes part in no split section, no GVTD trace and no verdict.
separation_orphans()returns those channels with their separations, which is what the reports name them by.Bad channels stay in both lists. Separation is the only thing being asked about, and pick_types drops bads by default, which would leave every metric computed from these lists averaging over channels that were selected for being good.
- nirspipe.qc.metrics._helpers.usable_split(raw: mne.io.Raw, sep_bands: Bands | None = None) tuple[list[str], list[str]]#
long_short_channels(), with a set every channel of which is bad emptied.short ["S5_D5 760", "S5_D5 850"], both in info["bads"] -> short []
For the callers that measure a set: every metric drops bads, so a set with no good channel has nothing to measure and is left out rather than raising. Counting a montage stays with
long_short_channels().
- nirspipe.qc.metrics._helpers.long_channel_picks(raw: mne.io.Raw, ch_type: str = 'hbo', exclude='bads', sep_bands=None) list[int]#
Picks for one chromophore with short-distance reference channels dropped.
- A 6-channel montage whose last pair is short ->
long_channel_picks(raw, “hbo”) == [0, 1] (the third hbo pick is gone)
Short channels sample scalp haemodynamics, so no inter-brain metric is computed on them. Montages with no short channels lose nothing.
“Long” is the package’s one separation rule,
separation_bands(), and a channel outside both bands is in neither list;long_short_channelsnames such a channel in a warning.The chromophore is read off the channel name, not the MNE type, since an HbT channel (“S1_D1 hbt”) is typed hbo;
ch_typeis “hbo”, “hbr” or “hbt”.excludeis pick_types’, so rejected channels are dropped by default, which is what a metric wants.exclude=[]keeps them, which is what the axis of a channel-by-channel matrix wants: an axis over the montage rather than over the survivors gives every subject and every dyad a matrix of one shape.
- nirspipe.qc.metrics._helpers.separation_orphans(raw: mne.io.Raw, sep_bands: Bands | None = None) dict[str, float]#
Channels in neither separation band, name -> separation in mm.
distances 8, 13, 30 mm on the default bands -> {"S2_D2 760": 13.0}The same split
long_short_channels()makes, asked the other way round, so a report can name what it left out instead of only counting it. Empty for a clean montage, and empty when nothing fell in either band, which is the positionless case the callers word differently.
- nirspipe.qc.metrics._helpers.as_optical_density(raw: mne.io.Raw) mne.io.Raw#
The recording in optical density: itself when it already is, else a converted copy.
The same metric can then be taken on a motion-corrected file, which is OD already, and on the raw intensity it came from. The input is never modified. Intensity has its dark edge frames held and marked
BAD_edgefirst, as prep does.
- nirspipe.qc.metrics._helpers.require_cardiac_below_nyquist(sfreq: float, cardiac_h_freq: float) None#
Refuse a cardiac band the recording’s sampling rate cannot carry.
The band-pass behind SCI and PSP rolls off over
CARDIAC_TRANSITION_HZabove the band, and MNE refuses a roll-off that crosses Nyquist, so the band’s top edge has to sit that far below it.
- nirspipe.qc.metrics._helpers.epochable_events(raw, tmin: float, tmax: float)#
Events that can actually be epoched over
[tmin, tmax], as(events, event_id).BAD_annotations are censoring marks rather than stimuli and never enter the event set. What is left is kept only if its whole window lies inside the recording and clears theBAD_segments, which is the same test MNE applies before dropping an epoch. Applying it up front lets a caller skip the figure instead of building an empty Epochs and drawing nothing, which is what MNE reports as “All epochs were dropped”:markers at 0 s and 300 s in a 300 s run, tmin=-5, tmax=25 -> (empty, {})
That is the block design that marks only where a condition starts and ends: neither window fits, so the run has no trials to epoch even though it carries annotations.
- nirspipe.qc.metrics._helpers.condition_epochs(raw: mne.io.Raw, tmin: float, tmax: float, picks=None) dict[str, mne.Epochs]#
Condition -> its trials over
[tmin, tmax], each less the mean of its pre-onset part.Trials whose window leaves the recording or touches a
BAD_span are not epoched, so a condition can come back with fewer trials than it has markers, or not at all. Each condition is epoched on its own, so two conditions starting on one sample both keep it.