SlowWavesPipe#
- class sleepeegpy.pipeline.SlowWavesPipe(*, prec_pipe=None, path_to_eeg=_Nothing.NOTHING, output_dir=_Nothing.NOTHING, path_to_hypno=None, hypno_freq=_Nothing.NOTHING, hypno=_Nothing.NOTHING)[source]#
Bases:
BaseEventPipe
Slow waves detection.
Methods:
Transforms the events signal to time-frequency representation.
A wrapper around
yasa.sw_detect()
with option to save.A wrapper for
mne.io.Raw.interpolate_bads()
A wrapper for
mne.io.Raw.plot()
.Plot average of the detected event.
A wrapper for
mne.viz.plot_sensors()
with a legend.Plots topomap for a sleep stage and some property of detected events.
Plots topomap collage for multiple sleep stages and event properties.
Runs YASA's automatic sleep staging.
Loads TFRs stored in hdf5 files.
A wrapper for
mne.io.Raw.save()
.A wrapper for
mne.io.Raw.set_eeg_reference()
.A wrapper for
yasa.sleep_statistics()
.Attributes:
Calculates percent of data segments annotated as BAD.
A wrapper for
raw.info["sfreq"]
.Instances of
mne.time_frequency.AverageTFR
per sleep stage.Event detection results as returned by YASA's event detection methods.
Path to hypnogram.
Sampling rate of the hypnogram in Hz.
Hypnogram with sampling frequency hypno_freq with int representing sleep stage.
Hypnogram upsampled to the sampling frequency of the raw data.
Preceding pipe that hands over mne_raw object and output_dir.
Can be any eeg file type supported by
mne.io.read_raw()
.Path to the directory where the output will be saved.
An instanse of
mne.io.Raw
.- detect(picks='eeg', reference='average', include=(1, 2, 3), freq_sw=(0.3, 1.5), dur_neg=(0.3, 1.5), dur_pos=(0.1, 1), amp_neg=(40, 200), amp_pos=(10, 150), amp_ptp=(75, 350), coupling=False, coupling_params={'freq_sp': (12, 16), 'p': 0.05, 'time': 1}, remove_outliers=False, verbose=False, save=False)[source]#
A wrapper around
yasa.sw_detect()
with option to save.
- 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
- compute_tfr(freqs, n_freqs, time_before, time_after, method='morlet', save=False, overwrite=False, **tfr_kwargs)[source]#
Transforms the events signal to time-frequency representation.
- Parameters:
freqs (
Iterable
[float
]) – Lower and upper bounds of frequencies of interest in Hz, e.g., (10,20).n_freqs (
int
) – Frequency resolution in TFR.time_before (
float
) – Seconds before the event peak to get from the real data.time_after (
float
) – Seconds after the event peak to get from the real datamethod (
str
) – TFR transform method. Defaults to “morlet”.save (
bool
) – Whether to save the TFRs to file. Defaults to False.overwrite (
bool
) – Whether to overwrite existing TFR files.**tfr_kwargs – Arguments passed to
mne.time_frequency.tfr_array_morlet()
ormne.time_frequency.tfr_array_multitaper()
.
- 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_average(save=False, **kwargs)[source]#
Plot average of the detected event.
- Parameters:
save (
bool
) – Whether to save the figure to file. Defaults to False.**kwargs – Arguments passed to the YASA’s plot_average().
- plot_sensors(legend=None, legend_args=None, **kwargs)[source]#
A wrapper for
mne.viz.plot_sensors()
with a legend.- Parameters:
legend (
Iterable
[str
]) – ch_groups names to connect to colors. Defaults to None.legend_args (
dict
) – Arguments passed tomatplotlib.axes.Axes.legend()
. Defaults to None.**kwargs – Arguments passed to
mne.viz.plot_sensors()
.
- plot_topomap(prop, stage='N2', aggfunc='mean', sleep_stages={'N1': 1, 'N2': 2, 'N3': 3, 'REM': 4, 'Wake': 0}, axis=None, save=False, topomap_args=None, cbar_args=None)[source]#
Plots topomap for a sleep stage and some property of detected events.
- Parameters:
prop (
str
) – Any event property returned by self.results.summary().stage (
str
) – One of the sleep_stages keys. Defaults to “N2”.aggfunc (
str
) – Averaging function, “mean” or “median”. Defaults to “mean”.sleep_stages (
dict
) – Mapping between sleep stages names and their integer representations. Defaults to {“Wake”: 0, “N1”: 1, “N2”: 2, “N3”: 3, “REM”: 4}.axis (
axis
) – Instance ofmatplotlib.axes.Axes
. Defaults to None.save (
bool
) – Whether to save the figure. Defaults to False.topomap_args (
dict
) – Arguments passed tomne.viz.plot_topomap()
.Defaults to None.cbar_args (
dict
) – Arguments passed tomatplotlib.pyplot.colorbar()
.Defaults to None.
- plot_topomap_collage(props, aggfunc='mean', stages_to_plot='all', sleep_stages={'N1': 1, 'N2': 2, 'N3': 3, 'REM': 4, 'Wake': 0}, low_percentile=5, high_percentile=95, fig=None, save=False, topomap_args=None, cbar_args=None)[source]#
Plots topomap collage for multiple sleep stages and event properties.
- Parameters:
props (
Iterable
[str
]) – Properties from the self.results.summary() to generate topomaps for.aggfunc (
str
) – Averaging function, “mean” or “median”. Defaults to “mean”.stages_to_plot (
tuple
) – stages_to_plot: Tuple of strings representing names from sleep_stages, e.g., (“REM”, “N1”). If set to “all” plots every stage provided in sleep_stages. Defaults to “all”.sleep_stages (
dict
) – Mapping between sleep stages names and their integer representations. Defaults to {“Wake”: 0, “N1”: 1, “N2”: 2, “N3”: 3, “REM”: 4}.low_percentile (
float
) – Set min color value by percentile of the property data. Defaults to 5.high_percentile (
float
) – Set max color value by percentile of the property data. Defaults to 95.fig (
figure
) – Instance ofmpl:matplotlib.pyplot.figure
. Defaults to None.save (
bool
) – Whether to save the figure. Defaults to False.topomap_args (
dict
) – Arguments passed tomne.viz.plot_topomap()
. Defaults to None.cbar_args (
dict
) – Arguments passed tomatplotlib.pyplot.colorbar()
. Defaults to None.
- predict_hypno(eeg_name='E183', eog_name='E252', emg_name='E247', ref_name='E26', save=True)[source]#
Runs YASA’s automatic sleep staging.
- Parameters:
eeg_name (
str
) – Preferentially a central electrode. Defaults to “E183”.eog_name (
str
) – Preferentially, the left LOC channel. Defaults to “E252”.emg_name (
str
) – Preferentially a chin electrode. Defaults to “E247”.ref_name (
str
) – Reference channel, preferentially a mastoid. Defaults to “E26”.save (
bool
) – Whether to save the hypnogram to file. Defaults to True.
- read_tfrs(dirpath=None)[source]#
Loads TFRs stored in hdf5 files.
Filenames should end with {type_of_event}_{sleep_stage}-tfr.h5
- Parameters:
dirpath (
Optional
[str
]) – Path to the directory containing hdf5 files. Defaults to None.
- 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:
ref_channels –
ref_channels
. Defaults to ‘average’.projection –
projection
. Defaults to False.**kwargs – Additional arguments passed to
mne.io.Raw.set_eeg_reference()
.
- property sf[source]#
A wrapper for
raw.info["sfreq"]
.- Returns:
sampling frequency
- Return type:
float
- sleep_stats(save=False)[source]#
A wrapper for
yasa.sleep_statistics()
.- Parameters:
save (
bool
) – Whether to save the stats to csv. Defaults to False.
- tfrs: dict#
Instances of
mne.time_frequency.AverageTFR
per sleep stage.
- results#
Event detection results as returned by YASA’s event detection methods. Depending on the child class can be instance of either
yasa.SpindlesResults
,yasa.SWResults
oryasa.REMResults
classes.
- path_to_hypno: Path#
Path to hypnogram. Must be text file with every row being int representing sleep stage for the epoch.
- hypno_freq: float#
Sampling rate of the hypnogram in Hz.
E.g., 1/30 means 1 sample per 30 secs epoch, 250 means 1 sample per 1/250 sec epoch.
- hypno: np.ndarray#
Hypnogram with sampling frequency hypno_freq with int representing sleep stage.
- hypno_up: np.array#
Hypnogram upsampled to the sampling frequency of the raw data.
- 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
.