nirspipe.qc.common.windows#

Condition windows and crop provenance, read off a recording’s annotations.

Shared by both report paths.

Module Contents#

Functions#

crop_provenance

What a cropped input’s sidecar says about the cut, or None if it is a whole recording.

markers_on_data_axis

Non-BAD annotations with their onsets moved onto the data axis, which starts at zero.

condition_windows

[(label, tstart, tend)] on the aligned clock, one window per task annotation.

refuse_colliding_labels

Raise when two condition labels would name one file, or one takes a reserved name.

split_windows

Cut each window into non-overlapping windows of one length, and say where each came from.

condition_margins

Seconds from each window back to its condition’s start and on to its condition’s end.

Data#

API#

nirspipe.qc.common.windows.logger = 'get_logger(...)'#
nirspipe.qc.common.windows.crop_provenance(raw: mne.io.Raw) → dict | None#

What a cropped input’s sidecar says about the cut, or None if it is a whole recording.

a file cut to [3555, 3927] with a 47 s margin
  -> {"window": [3555.0, 3927.0], "analysis": [3602.4, 3902.5], "margin_s": 47.1}

nirspipe-prep crop records the span it wrote and, with --margin, the narrower span the cut was made for.

The sidecar is read off disk rather than the lineage stamp, which carries only the filter keys across a SNIRF round trip.

nirspipe.qc.common.windows.markers_on_data_axis(raw: mne.io.Raw) → list[dict]#

Non-BAD annotations with their onsets moved onto the data axis, which starts at zero.

a raw cropped from 22.4 s, annotation "task" at onset 3602.4  ->  onset 3580.0

Annotations of a cropped recording still sit on the original recording’s axis, with the offset held in first_time, while the data axis and everything computed from it start at zero. align_recordings() crops every member from its first shared trigger, so raw annotation onsets are late by that trigger’s onset against any figure or window drawn on the aligned clock.

Every consumer of the aligned markers goes through this.

nirspipe.qc.common.windows.condition_windows(raw: mne.io.Raw, min_duration: float) → list[tuple[str, float, float]]#

[(label, tstart, tend)] on the aligned clock, one window per task annotation.

e.g. annotations “Rest” at 60 s for 240 s and “Task” at 300 s for 240 s give [("Rest", 60.0, 300.0), ("Task", 300.0, 540.0)].

An annotation’s own duration is the window when it has one. Many acquisition systems write triggers with a duration of zero, so a window with none runs from its onset to the next annotation’s, and the last to the end of the recording. Which rule each window came from is logged, and the tail of a zero-duration run is as long as the recording happens to be rather than as long as the block was.

A description that occurs more than once gets one window per occurrence, numbered desc#1, desc#2. They are not spliced into a single record: a wavelet transform reads a join between two non-adjacent segments as a step, which lands in the result as broadband coherence at the join.

min_duration drops windows shorter than that, in seconds. Pass 0.0 where the windows only split panels and no frequency has to fit inside one.

nirspipe.qc.common.windows.refuse_colliding_labels(labels, reserved: dict[str, str] | None = None) → None#

Raise when two condition labels would name one file, or one takes a reserved name.

reserved maps a name, as its letters and digits, to what the output uses it for:

["game 1", "game1"]                          -> StageError: both are cond-game1
["all"], reserved={"all": "cond-all is ..."} -> StageError naming what cond-all is

A label reaches a file name through a slug of its letters and digits, so two labels that differ only in the rest would write the same file, the later one replacing the earlier.

nirspipe.qc.common.windows.split_windows(windows: list[tuple[str, float, float]], length_s: float) → tuple[list[tuple[str, float, float]], dict[str, tuple[str, float]]]#

Cut each window into non-overlapping windows of one length, and say where each came from.

[("rest", 0.0, 100.0), ("task", 200.0, 500.0)], length 100.0
  -> [("rest-w1", 0.0, 100.0), ("task-w1", 200.0, 300.0),
      ("task-w2", 300.0, 400.0), ("task-w3", 400.0, 500.0)],
     {"rest-w1": ("rest", 0.0), "task-w1": ("task", 0.0),
      "task-w2": ("task", 100.0), "task-w3": ("task", 200.0)}

The remainder is dropped rather than kept short, since a window of another length would undo the split. A window shorter than length_s therefore yields nothing and is logged.

The second return value is what a null needs. A draw cuts the stand-in at its own marker for the condition, not at the real dyad’s clock, so for a window it needs the condition the window belongs to and how far into it the window starts. Returned explicitly rather than parsed back out of the label, which condition_windows may already have numbered desc#2.

nirspipe.qc.common.windows.condition_margins(conditions: list[tuple[str, float, float]], windows: list[tuple[str, float, float]], sources: dict[str, tuple[str, float]]) → dict[str, tuple[float, float]]#

Seconds from each window back to its condition’s start and on to its condition’s end.

conditions [("task", 200.0, 500.0)], windows of 100 s
  -> {"task-w1": (0.0, 200.0), "task-w2": (100.0, 100.0), "task-w3": (200.0, 0.0)}

Relative rather than on a clock, so a null that cuts each window out of a stand-in places the condition’s edges where the real run has them. A window that is a condition itself, absent from sources, is (0.0, 0.0).