LosslessPipeline Class¶
This is the docstring for the Lossless Pipeline class. The methods and attributes detailed below are used throughout the Lossless pipeline.
- class pylossless.LosslessPipeline(config_path=None, config=None)¶
Class used to handle pipeline parameters.
- Parameters:
- config_path
pathlib.Path path to config file specifying the parameters to be used in the pipeline.
- config_path
- Attributes:
- flags
dict A dictionary of detailing the flagged channels, epochs, and ICs. keys are
'ch','epoch', and'ic', and values are instances ofFlaggedChs,FlaggedEpochs, andFlaggedICs, respectively.- config_path
pathlib.Path path to the config file specifying the parameters to be used in the in the pipeline.
- config
dict A dictionary containing the pipeline parameters.
- raw
mne.io.Raw An instance of
Rawcontaining that will be processed by the pipeline.- ica1
mne.preprocessing.ICA An instance of
ICA. The result of the initial ICA run during the pipeline.- ica2
mne.preprocessing.ICA An instance of
ICA. The result of the final ICA run during the pipeline.
- flags
Methods
add_pylossless_annotations(inds, event_type, ...)Add annotations for flagged epochs.
filter()Run filter procedure based on structured config args.
Find breaks using mne.preprocessing.annotate_break.
find_outlier_chs([epochs, picks])Detect outlier Channels to leave out of rereference.
flag_bridged_channels(data_r_ch)Flag bridged channels.
flag_channels_fixed_threshold([threshold, picks])Flag channels based on the stdev value across the time dimension.
flag_epochs_fixed_threshold([threshold, picks])Flag epochs based on the stdev value across the time dimension.
flag_noisy_channels([picks])Flag channels with outlying standard deviation.
flag_noisy_epochs([picks])Flag epochs with outlying standard deviation.
flag_noisy_ics([picks])Calculate the IC standard Deviation by epoch window.
flag_rank_channel(data_r_ch)Flag the channel that is the least unique.
flag_uncorrelated_channels([picks])Check neighboring channels for too high or low of a correlation.
flag_uncorrelated_epochs([picks])Flag epochs where too many channels are uncorrelated.
Get a combined event ID dictionary from existing markers and raw annotations.
get_derivative_path(bids_path[, derivative_name])Build derivative path for file.
get_epochs([detrend, preload, rereference, ...])Create mne.Epochs according to user arguments.
Make an MNE events array of fixed length events.
get_n_nbr([picks])Calculate nearest neighbour correlation for channels.
Load the config file.
load_ll_derivative(derivatives_path)Load a completed pylossless derivative state.
run(bids_path[, save, overwrite])Run the pylossless pipeline.
run_dataset(paths)Run a full dataset.
run_ica(run[, picks])Run ICA.
Run a staging script if specified in config.
run_with_raw(raw)Execute pipeline on a raw object.
save([derivatives_path, overwrite, format, ...])Save the file at the end of the pipeline.
Set the montage.
- add_pylossless_annotations(inds, event_type, epochs)¶
Add annotations for flagged epochs.
- Parameters:
- inds
list|tuple indices corresponding to artefactual epochs
- event_type
str One of ‘ch_sd’, ‘low_r’, ‘ic_sd1’
- epochs
mne.Epochs an instance of mne.Epochs
- inds
- filter()¶
Run filter procedure based on structured config args.
- find_breaks()¶
Find breaks using mne.preprocessing.annotate_break.
- Parameters:
- kwargs
dict a dict with keys that are valid keyword arguments for mne.preprocessing.annotate_break, and with values that are valid for their respective arguments.
- kwargs
Notes
kwargs=dict(min_break_duration=15.0, t_start_after_previous=5.0)for example would unpack two keyword arguments from mne.preprocessing.annotate_break
- find_outlier_chs(epochs=None, picks='eeg')¶
Detect outlier Channels to leave out of rereference.
- Parameters:
- epochs
mne.Epochs|None An instance of
mne.Epochs, orNone. IfNone, thenpylossless.LosslessPipeline.rawshould be set, and this method will callpylossless.LosslessPipeline.get_epochs()to create epochs to use for outlier detection.- picks
str(default “eeg”) Channels to include in the outlier detection process. You can pass any argument that is valid for the
pick()method, but you should avoid passing a mix of channel types with differing units of measurement (e.g. EEG and MEG), as this would likely lead to incorrect outlier detection (e.g. all EEG channels would be flagged as outliers).
- epochs
- Returns:
lista list of channel names that are considered outliers.
Notes
This method is used to detect channels that are so noisy that they should be left out of the robust average rereference process.
Examples
>>> import mne >>> import pylossless as ll >>> config = ll.Config().load_default() >>> pipeline = ll.LosslessPipeline(config=config) >>> fname = mne.datasets.sample.data_path() / "MEG/sample/sample_audvis_raw.fif" >>> raw = mne.io.read_raw(fname) >>> epochs = mne.make_fixed_length_epochs(raw, preload=True) >>> chs_to_leave_out = pipeline.find_outlier_chs(epochs=epochs)
- flag_bridged_channels(data_r_ch)¶
Flag bridged channels.
- Parameters:
- data_r_chnumpy.array
an instance of numpy.array
- flag_channels_fixed_threshold(threshold=5e-05, picks='eeg')¶
Flag channels based on the stdev value across the time dimension.
Flags channels if the voltage-variance standard deviation is above the given threshold in n_percent of epochs (default: 20%).
- Parameters:
- threshold
float threshold, in volts. If the standard deviation across time in any channel x epoch indice is above this threshold, then the channel x epoch indices will be considered an outlier. Defaults to 5e-5, or 50 microvolts. Note that here, ‘time’ refers to the samples in an epoch. For each channel, if its std value is above the given threshold in more than 20% of the epochs, it is flagged.
- picks
str(default “eeg”) Type of channels to pick.
- threshold
- Returns:
NoneIf any channels are flagged, those channel names will be logged in the flags attribute of the LosslessPipeline object, under the key
'volt_std', e.g.my_pipeline.flags["ch"]["volt_std"].
Notes
Warning
the default threshold of 50 microvolts may not be appropriate for a particular dataset or data file, as the baseline voltage variance is affected by the impedance of the system that the data was recorded with. You may need to assess a more appropriate value for your own data. You can use the
find_bads_by_threshold()function to quickly assess a more appropriate threshold.See also
Examples
>>> import mne >>> import pylossless as ll >>> config = ll.Config().load_default() >>> config["flag_channels_fixed_threshold"] = {"threshold": 5e-5} >>> pipeline = ll.LosslessPipeline(config=config) >>> sample_fpath = mne.datasets.sample.data_path() >>> fpath = sample_fpath / "MEG" / "sample" / "sample_audvis_raw.fif" >>> raw = mne.io.read_raw(fpath).pick("eeg") >>> pipeline.raw = raw >>> pipeline.flag_channels_fixed_threshold()
- flag_epochs_fixed_threshold(threshold=5e-05, picks='eeg')¶
Flag epochs based on the stdev value across the time dimension.
Flags an epoch if the voltage-variance standard deviation is above the given threshold in n_percent of channels (default: 20%).
- Parameters:
- threshold
float threshold, in volts. If the standard deviation across time in any channel x epoch indice is above this threshold, then the channel x epoch indices will considered an outlier. Defaults to 5e-5, or 50 microvolts. Note that here, ‘time’ refers to the samples in an epoch. For each epoch, if the std value of more than 20% of channels (in that epoch) are above the given threshold, the epoch is flagged.
- picks
str(default “eeg”) Type of channels to pick.
- threshold
Notes
WARNING: the default threshold of 50 microvolts may not be appropriate for a particular dataset or data file, as the baseline voltage variance is affected by the impedance of the system that the data was recorded with. You may need to assess a more appropriate value for your own data.
- flag_noisy_channels(picks='eeg')¶
Flag channels with outlying standard deviation.
Calculates the standard deviation of the voltage-variance for each channel at each epoch (default: 1-second epochs). Then, for each epoch, creates a distribution of the stdev values of all channels. Then, for each epoch, estimates a stdev outlier threshold, where any channel that has an stdev value higher than the threshold (in the current epoch) is flagged. If a channel is flagged as an outlier in more than n_percent of epochs (default: 20%), the channel is flagged for removal.
- Parameters:
- picks
str(default “eeg”) Type of channels to pick.
- picks
- flag_noisy_epochs(picks='eeg')¶
Flag epochs with outlying standard deviation.
- Parameters:
- picks
str(default “eeg”) Type of channels to pick.
- picks
- flag_noisy_ics(picks='eeg')¶
Calculate the IC standard Deviation by epoch window.
Flags windows with too many ICs with outlying standard deviations.
- Parameters:
- picks
str(default “eeg”) Type of channels to pick.
- picks
- flag_rank_channel(data_r_ch)¶
Flag the channel that is the least unique.
Flags the channel that is the least unique, the channel to remove prior to ICA in order to account for the rereference rank deficiency.
- Parameters:
- data_r_chnumpy.array.
an instance of numpy.array.
Check neighboring channels for too high or low of a correlation.
- Parameters:
- picks
str(default “eeg”) Type of channels to pick.
- picks
- Returns:
- data arraynumpy.array
an instance of numpy.array
Flag epochs where too many channels are uncorrelated.
- Parameters:
- picks
str(default “eeg”) Type of channels to pick.
- picks
Notes
Similarly to the neighbor r calculation done between channels this section looks at the correlation, but between all channels and for epochs of time. Time segments are flagged for removal.
- get_all_event_ids()¶
Get a combined event ID dictionary from existing markers and raw annotations.
- get_derivative_path(bids_path, derivative_name='pylossless')¶
Build derivative path for file.
- get_epochs(detrend=None, preload=True, rereference=True, picks='eeg')¶
Create mne.Epochs according to user arguments.
- Parameters:
- detrend
int|None(defaultNone) If 0 or 1, the data channels (MEG and EEG) will be detrended when loaded. 0 is a constant (DC) detrend, 1 is a linear detrend.None is no detrending. Note that detrending is performed before baseline correction. If no DC offset is preferred (zeroth order detrending), either turn off baseline correction, as this may introduce a DC shift, or set baseline correction to use the entire time interval (will yield equivalent results but be slower).
- preload
bool(defaultTrue) Load epochs from disk when creating the object or wait before accessing each epoch (more memory efficient but can be slower).
- picks
str(default “eeg”) Type of channels to pick.
- detrend
- Returns:
- Epochs
mne.Epochs an instance of mne.Epochs
- Epochs
- get_events()¶
Make an MNE events array of fixed length events.
- get_n_nbr(picks='eeg')¶
Calculate nearest neighbour correlation for channels.
- Parameters:
- picks
str(default “eeg”) Type of channels to pick.
- picks
- load_config()¶
Load the config file.
- load_ll_derivative(derivatives_path)¶
Load a completed pylossless derivative state.
- Parameters:
- derivatives_path
str|mne_bids.BIDSPath Path to a saved pylossless derivatives.
- derivatives_path
- Returns:
LosslessPipelineReturns an instance of
LosslessPipelinefor the loaded pylossless derivative state.
- run(bids_path, save=True, overwrite=False)¶
Run the pylossless pipeline.
- run_dataset(paths)¶
Run a full dataset.
- run_ica(run, picks='eeg')¶
Run ICA.
- run_staging_script()¶
Run a staging script if specified in config.
- run_with_raw(raw)¶
Execute pipeline on a raw object.
- save(derivatives_path=None, overwrite=False, format='EDF', event_id=None)¶
Save the file at the end of the pipeline.
- Parameters:
- derivatives_path
None|mne_bids.BIDSPath path of the derivatives folder to save the file to.
- overwrite
bool(defaultFalse) whether to overwrite existing files with the same name.
- format
str(default “EDF”) The format to use for saving the raw data. Can be
"auto","FIF","EDF","BrainVision","EEGLAB".- event_id
dict|None(defaultNone) Dictionary mapping annotation descriptions to event codes.
- derivatives_path
- set_montage()¶
Set the montage.