meeg_utils.Pipeline#

class meeg_utils.Pipeline(steps, *, copy=True)[source]#

A sequence of named processing steps.

Follows scikit-learn conventions: fit(), transform() and fit_transform(); nested parameters (pipe.set_params(ica__threshold=0.9)); sklearn.base.clone(); slicing (pipe[:3]).

Unlike sklearn.pipeline.Pipeline, every step is fitted and transformed in sequence (each step learns from the output of the previous one), steps may change the data type (e.g. Raw -> Epochs), and side outputs are collected on the fitted pipeline.

Parameters:
  • steps (list of (str, Step)) – The steps, in order. Names must be unique and must not contain "__".

  • copy (bool) – If True (default), the input data are never modified. If False, the input may be modified in place to save memory.

system_#

Acquisition system detected when fitting.

Type:

str

qc_#

Quality-control metrics of each step, keyed by step name.

Type:

dict

provenance_#

Software versions, detected system and, for each step, its class, parameters, run time and the warnings it raised.

Type:

dict

__init__(steps, *, copy=True)[source]#

Methods

__init__(steps, *[, copy])

fit(inst[, y])

Fit every step in sequence.

fit_transform(inst[, y])

Fit every step in sequence and return the fully processed data.

from_dict(config)

Build a pipeline from to_dict() output.

from_yaml(fname)

Read a pipeline configuration from a YAML file.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters, including those of each step as <step>__<param>.

insert_after(after, name, step)

Insert a step after the step called after.

insert_before(before, name, step)

Insert a step before the step called before.

plot(*[, inst])

Draw the quality-control figures of every fitted step.

preset(name, **kwargs)

Build a ready-made pipeline, see meeg_utils.presets.

remove(name)

Remove the step called name.

replace(name, step)

Replace the step called name.

set_fit_request(*[, inst])

Configure whether metadata should be requested to be passed to the fit method.

set_params(**params)

Set parameters; <step>=new_step replaces a step.

set_transform_request(*[, inst])

Configure whether metadata should be requested to be passed to the transform method.

to_dict()

Return the pipeline configuration as plain, YAML-safe data.

to_yaml(fname)

Write the pipeline configuration to a YAML file.

transform(inst)

Apply every fitted step in sequence, without refitting.

Attributes

named_steps

The steps, keyed by name.

fit(inst, y=None)[source]#

Fit every step in sequence.

Each step except the last is fitted and then transforms the data passed to the next step; the last step is only fitted.

Parameters:
  • inst (Raw | Epochs | Evoked | str | Path | BIDSPath) – The data, or a recording to read with meeg_utils.io.read().

  • y (None) – Ignored; present for scikit-learn API compatibility.

Returns:

self – The fitted pipeline.

Return type:

Pipeline

fit_transform(inst, y=None)[source]#

Fit every step in sequence and return the fully processed data.

Parameters:
  • inst (Raw | Epochs | Evoked | str | Path | BIDSPath) – The data, or a recording to read with meeg_utils.io.read().

  • y (None) – Ignored; present for scikit-learn API compatibility.

Returns:

The processed data.

Return type:

Raw | Epochs | Evoked

transform(inst)[source]#

Apply every fitted step in sequence, without refitting.

Parameters:

inst (Raw | Epochs | Evoked | str | Path | BIDSPath) – The data, or a recording to read with meeg_utils.io.read(). Must come from the same acquisition system as the data the pipeline was fitted on.

Returns:

The processed data.

Return type:

Raw | Epochs | Evoked

plot(*, inst=None)[source]#

Draw the quality-control figures of every fitted step.

Parameters:

inst (Raw | Epochs | Evoked | str | Path | BIDSPath | None) – Data for the figures that need it. It is run through the fitted steps (transform, no refitting), so each step plots the data as that step received it. Without it, only figures that need no data are drawn, from what the steps recorded when they last transformed data (after fit, the last step has not).

Returns:

Step name mapped to that step’s {kind: Figure}; steps without figures are left out.

Return type:

dict

get_params(deep=True)[source]#

Get parameters, including those of each step as <step>__<param>.

Parameters:

deep (bool) – If True, also return each step and its parameters.

Returns:

Parameter names mapped to their values.

Return type:

dict

set_params(**params)[source]#

Set parameters; <step>=new_step replaces a step.

Parameters:

**params (Any) – steps, copy, <step> or <step>__<param>.

Returns:

self – The pipeline.

Return type:

Pipeline

property named_steps: dict[str, Step]#

The steps, keyed by name.

__len__()[source]#

Return the number of steps.

__getitem__(key)[source]#

Return a step by position or name, or a sub-pipeline for a slice.

A sub-pipeline shares its step objects (and their fitted state) with this pipeline.

replace(name, step)[source]#

Replace the step called name.

Parameters:
  • name (str) – Name of the step to replace.

  • step (Step) – The new step.

Returns:

self – The pipeline.

Return type:

Pipeline

insert_after(after, name, step)[source]#

Insert a step after the step called after.

Parameters:
  • after (str) – Name of the existing step.

  • name (str) – Name of the new step.

  • step (Step) – The new step.

Returns:

self – The pipeline.

Return type:

Pipeline

insert_before(before, name, step)[source]#

Insert a step before the step called before.

Parameters:
  • before (str) – Name of the existing step.

  • name (str) – Name of the new step.

  • step (Step) – The new step.

Returns:

self – The pipeline.

Return type:

Pipeline

remove(name)[source]#

Remove the step called name.

Parameters:

name (str) – Name of the step to remove.

Returns:

self – The pipeline.

Return type:

Pipeline

to_dict()[source]#

Return the pipeline configuration as plain, YAML-safe data.

Returns:

The configuration; see from_dict().

Return type:

dict

classmethod preset(name, **kwargs)[source]#

Build a ready-made pipeline, see meeg_utils.presets.

Parameters:
Returns:

A new, unfitted pipeline; change parameters with set_params.

Return type:

Pipeline

Examples

>>> pipe = Pipeline.preset("had-meeg", datatype="eeg")
>>> pipe.set_params(ica__threshold=0.9)
Pipeline(...)
classmethod from_dict(config)[source]#

Build a pipeline from to_dict() output.

Loading a configuration imports the modules that define its steps, so only load configurations you trust.

Parameters:

config (dict) – The configuration.

Returns:

An unfitted pipeline.

Return type:

Pipeline

to_yaml(fname)[source]#

Write the pipeline configuration to a YAML file.

Parameters:

fname (str | Path) – Output file.

classmethod from_yaml(fname)[source]#

Read a pipeline configuration from a YAML file.

Loading a configuration imports the modules that define its steps, so only load configurations you trust.

Parameters:

fname (str | Path) – Input file.

Returns:

An unfitted pipeline.

Return type:

Pipeline