yasa.Hypnogram.upsample_to_data#
- Hypnogram.upsample_to_data(data, sf=None, meas_date_is_local=True, verbose=False)[source]#
Upsample a hypnogram to a given sampling frequency and fit the resulting hypnogram to corresponding EEG data, such that the hypnogram and EEG data have the exact same number of samples.
When
self.startis set anddatais amne.io.BaseRawwith a validmeas_date, alignment uses absolute timestamps rather than sample count. See the Working with Hypnograms tutorial for a full description of all alignment scenarios and when to usestart/tz.- Parameters:
- dataarray_like or
mne.io.BaseRaw 1D or 2D EEG data. Can also be a
mne.io.BaseRaw, in which casedataandsfwill be automatically extracted.- sffloat
The sampling frequency of
data, in Hz (e.g. 100 Hz, 256 Hz, …). Can be omitted ifdatais amne.io.BaseRaw.- meas_date_is_localbool
If
True(default),meas_dateis treated as a local absolute timestamp, consistent with the EDF+ standard, which explicitly definesstarttimeas local time at the patient’s location. Set toFalseonly if your EDF files genuinely store UTC inmeas_date, in which case passtzwhen constructing theHypnogramso the two timestamps share a common reference frame.- verbosebool or str
Verbose level. Default (False) will only print warning and error messages. The logging levels are ‘debug’, ‘info’, ‘warning’, ‘error’, and ‘critical’. For most users the choice is between ‘info’ (or
verbose=True) and warning (verbose=False).Changed in version 0.8.0: The default is now False, as documented. Previously, the default was True.
- dataarray_like or
- Returns:
- hypno
numpy.ndarray The hypnogram values as a 1D integer array, upsampled to
sfHz and cropped/padded tomax(data.shape)samples. For compatibility with most YASA functions, integer values are returned rather than ayasa.Hypnogramobject.
- hypno
- Raises:
- ValueError
Only when
meas_date_is_local=False: raised ifself.startis timezone-naive whileraw.meas_dateis timezone-aware (UTC). Fix by passingtzat construction:Hypnogram(..., tz='Europe/Paris'). This error cannot occur with the defaultmeas_date_is_local=True.
Notes
If the hypnogram is shorter or longer than the data, it is padded or cropped and a warning is logged via the
yasalogger (not a PythonUserWarning). This message can be silenced by passingverbose='error'.Examples
>>> import numpy as np >>> from yasa import Hypnogram >>> hyp = Hypnogram(["W", "W", "N1", "N2", "N2", "REM"], freq="30s") >>> data = np.zeros((1, 18000)) >>> hypno = hyp.upsample_to_data(data, sf=100) >>> hypno.shape (18000,) >>> np.unique(hypno) array([0, 1, 2, 4], dtype=int16)