yasa.Hypnogram.upsample#

Hypnogram.upsample(new_freq)[source]#

Upsample hypnogram to a higher frequency.

Parameters:
new_freqstr

Target frequency as a pandas frequency string (e.g. "10s" or "1min"). Must represent a higher sampling rate than the current hypnogram frequency, i.e. a shorter epoch duration (e.g. "10s" when the current frequency is "30s").

Returns:
hypyasa.Hypnogram

The upsampled Hypnogram object. This function returns a copy, i.e. the original hypnogram is not modified in place.

Examples

Create a 30-sec hypnogram

>>> from yasa import Hypnogram
>>> hyp = Hypnogram(["W", "W", "S", "S", "W"], n_stages=2, start="2022-12-23 23:00")
>>> hyp.hypno
Time
2022-12-23 23:00:00     WAKE
2022-12-23 23:00:30     WAKE
2022-12-23 23:01:00    SLEEP
2022-12-23 23:01:30    SLEEP
2022-12-23 23:02:00     WAKE
Freq: 30s, Name: Stage, dtype: category
Categories (4, str): ['WAKE', 'SLEEP', 'ART', 'UNS']

Upsample to a 15-seconds resolution

>>> hyp_up = hyp.upsample("15s")
>>> hyp_up.hypno
Time
2022-12-23 23:00:00     WAKE
2022-12-23 23:00:15     WAKE
2022-12-23 23:00:30     WAKE
2022-12-23 23:00:45     WAKE
2022-12-23 23:01:00    SLEEP
2022-12-23 23:01:15    SLEEP
2022-12-23 23:01:30    SLEEP
2022-12-23 23:01:45    SLEEP
2022-12-23 23:02:00     WAKE
2022-12-23 23:02:15     WAKE
Freq: 15s, Name: Stage, dtype: category
Categories (4, str): ['WAKE', 'SLEEP', 'ART', 'UNS']