BaseEventPipeο
Tip
π¨ Abstract Class - This class is abstract and cannot be used directly. Subclass it and implement the required methods.
- class sleepeegpy.base.BaseEventPipe(*, prec_pipe=None, path_to_eeg=NOTHING, output_dir=NOTHING, path_to_hypno=None, hypno_freq=NOTHING, hypno=NOTHING)[source]ο
Bases:
BaseHypnoPipe,ABCA base class for event detection.
Methods:
Transforms the events signal to time-frequency representation.
Each event class should contain the detection method
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.AverageTFRper 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.- 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.SWResultsoryasa.REMResultsclasses.
-
tfrs:
dictο Instances of
mne.time_frequency.AverageTFRper sleep stage.
- 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().
- 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:
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.
- 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.
- 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.
-
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:
ndarrayο Hypnogram with sampling frequency hypno_freq with int representing sleep stage.
-
hypno_up:
arrayο Hypnogram upsampled to the sampling frequency of the raw data.
-
prec_pipe:
Type[TypeVar(BasePipeType, bound= BasePipe)]ο 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:
Rawο An instanse of
mne.io.Raw.
- 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.
- 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().