nirspipe.pipeline.glm#

Module Contents#

Functions#

sole_regressor_channels

Short channels that would be regressed out of themselves, leaving nothing.

instant_conditions

Conditions whose every event lasts 0 s, which the design fits as an instantaneous response.

stim_durations

--stim-dur as one duration, or a condition map: ["20", "Press=0"] -> {"*": 20.0, "Press": 0.0}.

build_design_matrix

Build a GLM design matrix, wrapping nilearn’s make_first_level_design_matrix.

fit_glm

constant_channels

Channels holding one value on every kept frame, e.g. a wholly non-finite one prep zeroed.

fit_noise_model

(results, Worsley’s channels fitted as auto, constant channels fitted by OLS).

read_contrasts

A --contrast-file as {contrast: {condition: weight}}: a TOML file, or a TSV/CSV table.

compute_contrasts

Each {condition: weight} mapping as a weight vector over the design’s columns.

run_glm_pipeline

on_residual_df

df as each channel’s own residual df, and p_value two-sided on it from stat.

blank_self_fitted

Every statistic of a channel fitted against a copy of itself set to n/a, rows kept.

glm_inputs

The tables a regression read besides the haemoglobin, for the Sources of what it writes.

Data#

API#

nirspipe.pipeline.glm.logger = 'get_logger(...)'#
nirspipe.pipeline.glm.HRFModel = None#
nirspipe.pipeline.glm.NoiseModel = None#
nirspipe.pipeline.glm.DriftModel = None#
nirspipe.pipeline.glm.SCRStrategy = None#
nirspipe.pipeline.glm.SHORT_CH_PREFIX = 'short_ch_'#
nirspipe.pipeline.glm.AR_BINS_MIN = 1000#
nirspipe.pipeline.glm.sole_regressor_channels(haemo: mne.io.Raw, strategy: SCRStrategy | None, sep_bands=None) → list[str]#

Short channels that would be regressed out of themselves, leaving nothing.

mean: [] for a montage with several good short channels a chromophore, ["S5_D6 hbo"] for one whose only surviving short channel is that one. pca: every good short channel of both chromophores.

The mean regressor is the mean of the good short channels of a chromophore. Where one of them survives screening, that mean is that channel, sample for sample, and the design matrix is applied to every channel including the ones it was built from: the channel is fitted against a copy of itself and its residual is numerically zero. With two or more the residual is the channel minus their mean, which is a real if small quantity, so the line is at one and not at some share.

pca keeps every component, so its basis spans each good short channel and all of them are fitted against themselves, however many survive.

A chromophore with no good short channel builds no regressor at all, so nothing is named then.

nirspipe.pipeline.glm.instant_conditions(events: pandas.DataFrame) → list[str]#

Conditions whose every event lasts 0 s, which the design fits as an instantaneous response.

Stops on a negative duration, and on a condition mixing 0 s events with longer ones, whose one column could not say what its beta measures.

nirspipe.pipeline.glm.EVERY_OTHER = '*'#
nirspipe.pipeline.glm.stim_durations(value) → float | dict[str, float] | None#

--stim-dur as one duration, or a condition map: ["20", "Press=0"] -> {"*": 20.0, "Press": 0.0}.

nirspipe.pipeline.glm.build_design_matrix(raw: mne.io.Raw, stim_dur: float | dict[str, float] | None, hrf_model: nirspipe.pipeline.glm.HRFModel, drift_model: nirspipe.pipeline.glm.DriftModel, high_pass: float | None, drift_order: int | None, fir_delays: tuple[int, ...] = (0,), add_regs: pandas.DataFrame | None = None, add_reg_names: list[str] | None = None, min_onset: float = -24, oversampling: int = 50, events: pandas.DataFrame | None = None) → pandas.DataFrame#

Build a GLM design matrix, wrapping nilearn’s make_first_level_design_matrix.

Modified from mne_nirs.experimental_design.make_first_level_design_matrix to accept external events and confounds, and to fall back to snirf annotations when events is None.

Conditional/non-obvious parameters:

stim_dur     required when events is None (annotation fallback)
high_pass    only used when drift_model='cosine'
drift_order  only used when drift_model='polynomial'
events       columns 'onset', 'duration', 'trial_type'; None → read from snirf
             annotations, skipping the BAD_/EDGE_ spans
nirspipe.pipeline.glm.fit_glm(haemo: mne.io.Raw, design_matrix: pandas.DataFrame, noise_model: nirspipe.pipeline.glm.NoiseModel = 'ar1', bins: int | None = None) → Any#
nirspipe.pipeline.glm.constant_channels(haemo: mne.io.Raw, keep: numpy.ndarray) → list[str]#

Channels holding one value on every kept frame, e.g. a wholly non-finite one prep zeroed.

nirspipe.pipeline.glm.fit_noise_model(haemo: mne.io.Raw, design_matrix: pandas.DataFrame, noise_model: str, keep: numpy.ndarray) → tuple[Any, list[str], list[str]]#

(results, Worsley’s channels fitted as auto, constant channels fitted by OLS).

nirspipe.pipeline.glm.read_contrasts(path: str | pathlib.Path) → dict[str, dict[str, float]]#

A --contrast-file as {contrast: {condition: weight}}: a TOML file, or a TSV/CSV table.

A table holds one row per contrast, its name in a contrast column and a weight under each condition’s column; an empty cell leaves the condition out, as an absent TOML key does:

contrast            Tapping_Left  Tapping_Right  Control
tapping_vs_control  0.5           0.5            -1
left_minus_right    1             -1

gives {"tapping_vs_control": {"Tapping_Left": 0.5, "Tapping_Right": 0.5, "Control": -1.0}, "left_minus_right": {"Tapping_Left": 1.0, "Tapping_Right": -1.0}}. ValueError names the line of a cell that is not a number, a name given twice, or a row that weights nothing.

nirspipe.pipeline.glm.compute_contrasts(glm_est: Any, contrast_def: dict[str, Any], design_matrix: pandas.DataFrame) → dict[str, Any]#

Each {condition: weight} mapping as a weight vector over the design’s columns.

{"left_minus_right": {"Tapping_Left": 1, "Tapping_Right": -1}} on a design carrying [Tapping_Left, Tapping_Right, constant] -> [1, -1, 0], then the fit’s own contrast of that vector.

A name the design does not carry raises rather than weighting nothing: a contrast file is written by hand against condition labels, and a typo there would otherwise be a silently empty contrast that still lands in the desc-contrast_nirsmap.tsv table.

nirspipe.pipeline.glm.run_glm_pipeline(haemo: mne.io.Raw, stim_dur: float | dict[str, float] | None, hrf_model: nirspipe.pipeline.glm.HRFModel, noise_model: nirspipe.pipeline.glm.NoiseModel, drift_model: nirspipe.pipeline.glm.DriftModel, high_pass: float | None, drift_order: int | None, fir_delays: tuple[int, ...] | None, events_path: str | None = None, drift_high_pass_auto: dict | None = None, events: pandas.DataFrame | None = None, short_channel: bool | nirspipe.pipeline.glm.SCRStrategy | None = None, short_channel_auto: bool = False, aux_path: str | pathlib.Path | None = None, aux_channels: list[str] | None = None, data_band: tuple[float | None, float | None] | None = None, data_filter_method: str = DEFAULT_FILTER_METHOD, data_filter_order: int = DEFAULT_FILTER_ORDER, contrast_def: dict[str, Any] | None = None, output_dir: str | None = None, source_path: str | None = None, sep_bands=None, chromophores: tuple[str, ...] = DEFAULT_CHROMOPHORES, p_correction: str = 'none') → tuple#
nirspipe.pipeline.glm.GLM_COLUMNS = None#
nirspipe.pipeline.glm.CONTRAST_COLUMNS = None#
nirspipe.pipeline.glm.GLM_UNITS = None#
nirspipe.pipeline.glm.P_CORRECTED = "p_value corrected by {method} over the row's family; n/a for a row outside it, such as a short or re..."#
nirspipe.pipeline.glm.on_residual_df(table: pandas.DataFrame, stat: str, dof: dict[str, float]) → pandas.DataFrame#

df as each channel’s own residual df, and p_value two-sided on it from stat.

nirspipe.pipeline.glm.blank_self_fitted(df: pandas.DataFrame, channels: list[str]) → pandas.DataFrame#

Every statistic of a channel fitted against a copy of itself set to n/a, rows kept.

nirspipe.pipeline.glm.glm_inputs(events_path: str | Path | None, aux_path: str | Path | None) → list[pathlib.Path]#

The tables a regression read besides the haemoglobin, for the Sources of what it writes.