CleaningPipe

class sleepeegpy.pipeline.CleaningPipe(*, prec_pipe=None, path_to_eeg=NOTHING, output_dir=NOTHING)[source]

Bases: BasePipe

The cleaning pipeline element.

Contains resampling function, band and notch filters, mne browser for manual selection of bad channels and bad data spans.

Methods:

auto_detect_bad_channels

Writes bad channels file automatically based on pyprep lib

auto_set_annotations

Sets annotations automatically based on MNE preprocessing library.

filter

A wrapper for mne.io.Raw.filter().

interpolate_bads

A wrapper for mne.io.Raw.interpolate_bads()

notch

A wrapper for mne.io.Raw.notch_filter().

plot

A wrapper for mne.io.Raw.plot().

plot_sensors

A wrapper for mne.viz.plot_sensors() with a legend.

read_annotations

Imports annotations from file to mne raw object

read_bad_channels

Imports bad channels from file to mne raw object.

resample

A wrapper for mne.io.Raw.resample() with an additional option to save the resampled data to file.

save_annotations

Writes annotations to "annotations.txt" file.

save_bad_channels

Adds bad channels from info["bads"] to the "bad_channels.txt" file.

save_raw

A wrapper for mne.io.Raw.save().

set_eeg_reference

A wrapper for mne.io.Raw.set_eeg_reference().

Attributes:

bad_data_percent

Calculates percent of data segments annotated as BAD.

sf

A wrapper for raw.info["sfreq"].

prec_pipe

Preceding pipe that hands over mne_raw object and output_dir.

path_to_eeg

Can be any eeg file type supported by mne.io.read_raw().

output_dir

Path to the directory where the output will be saved.

mne_raw

An instanse of mne.io.Raw.

resample(sfreq=250, **resample_kwargs)[source]

A wrapper for mne.io.Raw.resample() with an additional option to save the resampled data to file.

Parameters:
  • sfreq (float) – Desired new frequency. Defaults to 250.

  • save – Whether to save a resampled data to a fif file. Defaults to False.

  • **resample_kwargs – Arguments passed to mne.io.Raw.resample().

filter(l_freq=0.3, h_freq=None, **filter_kwargs)[source]

A wrapper for mne.io.Raw.filter().

Parameters:
  • l_freq (float | None) – Lower pass-band edge in Hz. Defaults to 0.3.

  • h_freq (float | None) – Upper pass-band edge in Hz. Defaults to None.

  • **filter_kwargs – Arguments passed to mne.io.Raw.filter().

notch(freqs='50s', **notch_kwargs)[source]

A wrapper for mne.io.Raw.notch_filter().

Parameters:
  • freqs (str | Iterable[float]) – Frequencies to notch filter in Hz. Can be either array of floats, or ’50s’ or ’60s’ to filter harmonics of 50 and 60 Hz, respectively. Defaults to ’50s’.

  • **notch_kwargs – Arguments passed to mne.io.Raw.notch_filter().

auto_set_annotations(amplitude_peak=0.0001, amplitude_min_duration=0.005)[source]

Sets annotations automatically based on MNE preprocessing library.

Parameters:
  • amplitude_peak (float) – Maximum accepted peak-to-peak (PTP) amplitude.

  • amplitude_min_duration (float) – Minimum required duration for the annotation.c

For more information about these parameters, check: https://mne.tools/dev/generated/mne.preprocessing.annotate_amplitude.html

auto_detect_bad_channels(path=None, methods=dict_keys(['ransac', 'SNR', 'hf_noise', 'correlation', 'deviation', 'dropout', 'flat']))[source]

Writes bad channels file automatically based on pyprep lib

Parameters:

path – Path to the output bad channels file. if None will be saved in default path.

Returns:

The path of the generated bad channels file.

Return type:

str

read_bad_channels(path=None)[source]

Imports bad channels from file to mne raw object.

Parameters:

path (str | None) – Path to the txt file with bad channel name per row. Defaults to None.

read_annotations(path=None)[source]

Imports annotations from file to mne raw object

Parameters:

path (str | None) – Path to txt file with mne-style annotations. Defaults to None.

save_bad_channels(overwrite=False)[source]

Adds bad channels from info[“bads”] to the “bad_channels.txt” file.

Parameters:

overwrite – Whether to overwrite the file if exists. If False will add unique new channels to the file. Defaults to False.

save_annotations(overwrite=False)[source]

Writes annotations to “annotations.txt” file.

Parameters:

overwrite – Whether to overwrite the file if exists. If False and the file exists will throw an exception. Defaults to False.

property bad_data_percent[source]

Calculates percent of data segments annotated as BAD.

Returns:

percent of bad data spans in raw data

Return type:

float

interpolate_bads(**interp_kwargs)[source]

A wrapper for mne.io.Raw.interpolate_bads()

Parameters:

**interp_kwargs – Arguments passed to mne.io.Raw.interpolate_bads().

plot(save_annotations=False, save_bad_channels=False, overwrite=False, **kwargs)[source]

A wrapper for mne.io.Raw.plot().

Parameters:
  • save_annotations (bool) – Whether to save annotations as txt. Defaults to False.

  • save_bad_channels (bool) – Whether to save bad channels as txt. Defaults to False.

  • overwrite (bool) – Whether to overwrite annotations and bad_channels files if exist. Defaults to False.

  • **kwargs – Arguments passed to mne.io.Raw.plot().

plot_sensors(legend=None, legend_args=None, **kwargs)[source]

A wrapper for mne.viz.plot_sensors() with a legend.

Parameters:
save_raw(fname, **kwargs)[source]

A wrapper for mne.io.Raw.save().

Parameters:
  • fname (str) – Filename for the fif file being saved.

  • **kwargs – Arguments passed to mne.io.Raw.save().

set_eeg_reference(ref_channels='average', projection=False, **kwargs)[source]

A wrapper for mne.io.Raw.set_eeg_reference().

Parameters:
property sf[source]

A wrapper for raw.info["sfreq"].

Returns:

sampling frequency

Return type:

float

prec_pipe: Type[BasePipeType]

Preceding pipe that hands over mne_raw object and output_dir.

path_to_eeg: Path

Can be any eeg file type supported by mne.io.read_raw().

output_dir: Path

Path to the directory where the output will be saved.

mne_raw: mne.io.Raw

An instanse of mne.io.Raw.