nirspipe.pipeline.prep_pipeline#

Preprocessing pipeline: all prep steps in a single module.

Step outputs written to output_dir/sub-XX/[ses-YY/]nirs/:

desc-od raw intensity → ΔOD desc-sci SCI-pruned OD desc-motcorrected motion-corrected OD desc-preproc final HbO/HbR (Beer-Lambert output) desc-aux the recording’s auxiliary channels, if it has any (tsv.gz)

Each snirf is accompanied by a JSON provenance sidecar.

motion correction lives in pipeline/motion.py.

Module Contents#

Classes#

PrepResult

Outputs from run_prep() needed for reporting and downstream use.

PrepConfig

Functions#

intensity_to_od

Convert raw intensity signal to optical density.

compute_sci

Return SCI score per channel name.

mark_bad_channels

Screen channels into raw.info[‘bads’].

mark_what_can_be_judged

mark_bad_channels() for a recording that must be shown whatever its state.

release_kept

Screening’s rejections minus the pairs a reviewer kept, as (still bad, kept).

join_marks

Screening’s rejections with a reviewer’s keeps released and every mark added, as (bad, kept).

od_to_haemo

Convert OD to haemoglobin concentration via Beer-Lambert law; age is what an age DPF came from.

run_prep

Run the full preprocessing pipeline in locked step order.

Data#

API#

nirspipe.pipeline.prep_pipeline.logger = 'get_logger(...)'#
nirspipe.pipeline.prep_pipeline.intensity_to_od(raw: mne.io.Raw) → mne.io.Raw#

Convert raw intensity signal to optical density.

nirspipe.pipeline.prep_pipeline.compute_sci(raw_od: mne.io.Raw, cardiac_l_freq: float, cardiac_h_freq: float) → dict[str, float]#

Return SCI score per channel name.

nirspipe.pipeline.prep_pipeline.mark_bad_channels(raw_od: mne.io.Raw, threshold: float, cardiac_l_freq: float, cardiac_h_freq: float, psp_threshold: float | None = None, min_good_frac: float | None = None, screen_scope: str = 'run', keep: list[str] | tuple = ()) → tuple[mne.io.Raw, list[str], dict[str, float], dict[str, float]]#

Screen channels into raw.info[‘bads’].

The criteria are nirspipe.qc.metrics.screening.CRITERIA and a channel is rejected if it fails any criterion that screens, so what gets pruned is decided by that table rather than here. threshold is the SCI line a window has to clear, and psp_threshold a PSP line it has to clear as well, None for none; min_good_frac is the share of windows that has to pass, and it is the line that rejects, None keeping the criterion’s own default. screen_scope is “run” or “task” and decides which windows are counted; see resolve_screen_scope(). Windows touching a BAD_ span are never counted, and fewer than two left stops the run.

Returns raw (modified in-place), the rejected channel names, the SCI scores, and the passing-window shares, the last two because the report and the quality record both want them on their own. keep (pairs a reviewer kept) does not change the rejections returned; it only stops a run whose every channel failed from being refused here. Raises StageError if the criteria leave no usable channel and nothing is kept.

nirspipe.pipeline.prep_pipeline.mark_what_can_be_judged(raw_od: mne.io.Raw, threshold: float, cardiac_l_freq: float, cardiac_h_freq: float, psp_threshold: float | None = None, min_good_frac: float | None = None, screen_scope: str = 'run', keep: list[str] | tuple = ()) → tuple[mne.io.Raw, list[str], dict[str, float], dict[str, float]]#

mark_bad_channels() for a recording that must be shown whatever its state.

What screening cannot judge rejects nothing and is stamped as screening_gap, and every channel may fail, so keep, which only stops that refusal, has nothing to do here.

nirspipe.pipeline.prep_pipeline.release_kept(screened: list[str], keep: list[str]) → tuple[list[str], list[str]]#

Screening’s rejections minus the pairs a reviewer kept, as (still bad, kept).

(["S1_D1 760", "S1_D1 850", "S2_D2 760", "S2_D2 850"], ["S2_D2"])

-> (["S1_D1 760", "S1_D1 850"], ["S2_D2 760", "S2_D2 850"])

nirspipe.pipeline.prep_pipeline.join_marks(raw_od: mne.io.Raw, screened: list[str], *, keep=(), manual=(), listed=(), forced=(), label: str = 'run') → tuple[list[str], list[str]]#

Screening’s rejections with a reviewer’s keeps released and every mark added, as (bad, kept).

keep holds the pairs a reviewer kept, manual the --bad-channels labels, listed the channels.tsv rejections and forced channels marked for another reason. A mark outranks a keep; a pair --bad-channels both marks and keeps is refused. One function for the pipeline and raw QC, so the same inputs give both the same verdict.

nirspipe.pipeline.prep_pipeline.od_to_haemo(raw_od: mne.io.Raw, dpf: list[float], age: float | None = None) → mne.io.Raw#

Convert OD to haemoglobin concentration via Beer-Lambert law; age is what an age DPF came from.

class nirspipe.pipeline.prep_pipeline.PrepResult#

Outputs from run_prep() needed for reporting and downstream use.

raw_haemo: mne.io.Raw | None = None#
sci_scores: dict[str, float] = None#
bad_channels: list[str] = None#
censor_spans: list[tuple[float, float]] = 'field(...)'#
skipped: list[tuple[str, str]] = 'field(...)'#
class nirspipe.pipeline.prep_pipeline.PrepConfig#
subject: str = None#
dpf: list = None#
sci_threshold: float = None#
cardiac_l_freq: float = None#
cardiac_h_freq: float = None#
resp_l_freq: float = None#
resp_h_freq: float = None#
session: str | None = None#
age: float | None = None#
template: str = None#
population: str | None = None#
from_population: tuple[str, ...] = ()#
psp_threshold: float | None = None#
min_good_frac: float = None#
screen_scope: str = 'run'#
qc_window_s: float = 10.0#
epoch_tmin: float | None = None#
epoch_tmax: float | None = None#
epoch_chunk_duration: float | None = None#
gvtd_censor: str | None = None#
gvtd_censor_n_std: float = 10.0#
gvtd_min_epoch_s: float = 30.0#
motion_correction: str | None = None#
motion_std_thresh: float | None = None#
wavelet_iqr: float | None = None#
short_max_dist: float | None = None#
long_min_dist: float | None = None#
long_max_dist: float | None = None#
bad_channels: list[str] = 'field(...)'#
keep_channels: list[str] = 'field(...)'#
channels_tsv_bads: list[str] = 'field(...)'#
ignore: list[str] = 'field(...)'#
keep_spans: list[tuple[float, float]] = 'field(...)'#
keep_spans_from: str | None = None#
bad_spans: list[tuple[float, float]] = 'field(...)'#
lenient: bool = False#
nirspipe.pipeline.prep_pipeline.run_prep(raw: mne.io.Raw, config: nirspipe.pipeline.prep_pipeline.PrepConfig, output_dir: pathlib.Path, source_entities: dict[str, str] | None = None, source_path: pathlib.Path | None = None, source_metadata: dict | None = None) → nirspipe.pipeline.prep_pipeline.PrepResult#

Run the full preprocessing pipeline in locked step order.

Steps (order is fixed; BIDS validation handled upstream before this call):
  1. OD conversion -> desc-od_nirs.snirf

  2. SCI channel marking -> desc-sci_nirs.snirf

  3. Motion correction -> desc-motcorrected_nirs.snirf

  4. Beer-Lambert -> desc-preproc_nirs.snirf

  5. Aux extraction -> desc-aux_timeseries.tsv.gz (only if the recording has aux)

source_path is the BIDS file raw was read from; it becomes the Sources entry of the first output, and step 5 reads the aux channels back out of it. source_metadata is that file’s sidecar, inheritance applied; every output carries its REQUIRED _nirs.json fields on.

Under config.lenient a recording screening cannot judge goes on with nothing rejected, and a step that fails is left out, named in PrepResult.skipped, with what follows it taking what it can.