Skip to content

Commit a1aaa1e

Browse files
committed
Update analytics.py
Fix docstrings Fix fill_false_block 15 min cleanup
1 parent 4fb3cd2 commit a1aaa1e

1 file changed

Lines changed: 21 additions & 16 deletions

File tree

src/wristpy/processing/analytics.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -402,50 +402,55 @@ def sleep_cleanup(
402402
timestamps as the reference measurement. Then any overlap with nonwear
403403
is removed, and finally any blocks of sleep that are less than 15 minutes
404404
long are removed.
405+
406+
Args:
407+
sleep_windows: List of the sleep windows (Onset/Wake pairs).
408+
nonwear_measurement: The nonwear measurement data used for reference time
409+
stamps and to remove overlaps with periods of sleep.
410+
411+
Returns:
412+
A Measurement instance with the cleaned sleep data.
405413
"""
406414
logger.debug("Starting the sleep Window cleanup.")
407-
sleep = _sleep_windows_as_measurement(nonwear_measurement, sleep_windows)
415+
temporal_resolution = nonwear_measurement.time[1] - nonwear_measurement.time[0]
416+
num_samples_15min = int(15 * 60 / temporal_resolution.total_seconds())
417+
418+
sleep = _sleep_windows_as_measurement(nonwear_measurement.time, sleep_windows)
408419

409420
filtered_sleep = sleep.measurements - nonwear_measurement.measurements
410421
cleaned_sleep = np.logical_not(
411-
_fill_false_blocks(np.logical_not(filtered_sleep), 15)
422+
_fill_false_blocks(np.logical_not(filtered_sleep), num_samples_15min)
412423
)
413424

414425
return models.Measurement(time=sleep.time, measurements=cleaned_sleep.astype(int))
415426

416427

417428
def _sleep_windows_as_measurement(
418-
ref_measurement: models.Measurement, sleep_windows: List[SleepWindow]
429+
ref_measurement_time: pl.Series, sleep_windows: List[SleepWindow]
419430
) -> models.Measurement:
420431
"""Helper function to convert list of sleep windows to a Measurement instance.
421432
422433
The temporal resolution of the output Measurement instance matches the
423434
reference measurement.
424435
425436
Args:
426-
ref_measurement: Reference measurement data to match time stamps to.
437+
ref_measurement_time: Reference polars Series with time stamps from a
438+
reference Measurement.
427439
sleep_windows: The list of sleep windows, where the entries are
428440
instances of the SleepWindow class.
429441
430442
Returns:
431443
A new Measurement instance with the sleep values.
432444
"""
433445
logger.debug("Converting sleep windows to measurement.")
434-
temporal_resolution = ref_measurement.time[1] - ref_measurement.time[0]
435-
436-
time_range = pl.datetime_range(
437-
ref_measurement.time[0],
438-
ref_measurement.time[-1],
439-
interval=temporal_resolution,
440-
eager=True,
441-
time_unit="ns",
442-
).alias("time")
443446

444-
sleep_value = np.zeros(len(time_range), dtype=int)
447+
sleep_value = np.zeros(len(ref_measurement_time), dtype=int)
445448

446449
for sw in sleep_windows:
447450
if sw.onset is not None and sw.wakeup is not None:
448-
time_mask = (time_range >= sw.onset) & (time_range <= sw.wakeup)
451+
time_mask = (ref_measurement_time >= sw.onset) & (
452+
ref_measurement_time <= sw.wakeup
453+
)
449454
sleep_value[time_mask] = 1
450455

451-
return models.Measurement(time=time_range, measurements=sleep_value)
456+
return models.Measurement(time=ref_measurement_time, measurements=sleep_value)

0 commit comments

Comments
 (0)