Skip to content

Commit ce4e622

Browse files
committed
output sib/spt
Use the nonwear_utils package to fix timing for sib/spt Update orchestrator +tests to output data
1 parent ea9c613 commit ce4e622

6 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/wristpy/core/orchestrator.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ def _run_file(
361361
non_wear_algorithms=nonwear_algorithm,
362362
)
363363

364-
nonwear_epoch = nonwear_utils.nonwear_array_cleanup(
365-
nonwear_array=nonwear_array,
364+
nonwear_epoch = nonwear_utils.synchronize_measurements(
365+
data_measurement=nonwear_array,
366366
reference_measurement=activity_measurement,
367367
epoch_length=epoch_length,
368368
)
@@ -374,11 +374,24 @@ def _run_file(
374374
sleep_array = analytics.sleep_cleanup(
375375
sleep_windows=sleep_windows[0], nonwear_measurement=nonwear_epoch
376376
)
377+
spt_windows = nonwear_utils.synchronize_measurements(
378+
data_measurement=sleep_windows[1],
379+
reference_measurement=activity_measurement,
380+
epoch_length=epoch_length,
381+
)
382+
sib_periods = nonwear_utils.synchronize_measurements(
383+
data_measurement=sleep_windows[2],
384+
reference_measurement=activity_measurement,
385+
epoch_length=epoch_length,
386+
)
387+
377388
results = writers.OrchestratorResults(
378389
physical_activity_metric=activity_measurement,
379390
anglez=anglez,
380391
physical_activity_levels=physical_activity_levels,
381392
sleep_status=sleep_array,
393+
sib_periods=sib_periods,
394+
spt_periods=spt_windows,
382395
nonwear_status=nonwear_epoch,
383396
processing_params=parameters_dictionary,
384397
)

src/wristpy/io/writers/writers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class OrchestratorResults(pydantic.BaseModel):
2323
physical_activity_levels: models.Measurement
2424
nonwear_status: models.Measurement
2525
sleep_status: models.Measurement
26+
sib_periods: models.Measurement
27+
spt_periods: models.Measurement
2628
processing_params: Optional[Dict[str, Any]] = None
2729

2830
def save_results(self, output: pathlib.Path) -> None:

src/wristpy/processing/analytics.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ def run_sleep_detection(
4343
) -> Tuple[List[SleepWindow], models.Measurement, models.Measurement]:
4444
"""Sleep Detector must contain a run_sleep_detection function.
4545
46-
The function must return a list of SleepWindow objects.
46+
The function must return a tuple that contains key sleep information, namely:
47+
- a list of SleepWindow objects.
48+
- a Measurement object with the SPT windows.
49+
- a Measurement object with the SIB periods.
4750
"""
4851
pass
4952

@@ -81,8 +84,11 @@ def run_sleep_detection(
8184
the SPT windows and SIB periods.
8285
8386
Returns:
84-
A list of SleepWindow instances, each instance contains a sleep onset/wakeup
85-
time pair.
87+
A tuple that contains:
88+
- a list of SleepWindow instances, each instance contains a sleep
89+
onset/wakeup time pair.
90+
- a Measurement object with the SPT windows.
91+
- a Measurement object with the SIB periods.
8692
"""
8793
logger.debug("Beginning sleep detection.")
8894
spt_window = self._spt_window(self.anglez)

src/wristpy/processing/nonwear_utils.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,23 @@ def get_nonwear_measurements(
160160
return results[0]
161161

162162

163-
def nonwear_array_cleanup(
164-
nonwear_array: models.Measurement,
163+
def synchronize_measurements(
164+
data_measurement: models.Measurement,
165165
reference_measurement: models.Measurement,
166166
epoch_length: float = 5.0,
167167
) -> models.Measurement:
168-
"""This function is used to match the nonwear array to a reference Measurement.
168+
"""This function is used to match a Measurement object to a reference Measurement.
169169
170-
This function ensures that the nonwear array and reference Measurement times
171-
are synced up. This is accomplished by first resampling the nonwear array to
170+
This function ensures that a Measurement object and reference Measurement times
171+
are synced up. This is accomplished by first resampling a Measurement object to
172172
the specified temporal resolution.
173-
It also ensures that the nonwear array is a binary array, where 1 indicates
173+
It also ensures that a Measurement object is a binary array, where 1 indicates
174174
nonwear and 0 indicates wear.
175-
It then truncates the nonwear array to match the reference Measurement time points.
175+
It then truncates a Measurement object to match the reference
176+
Measurement time points.
176177
177178
Args:
178-
nonwear_array: The nonwear array to clean up.
179+
data_measurement: The nonwear array to clean up.
179180
reference_measurement: The reference measurement to use for resampling.
180181
epoch_length: The temporal resolution of the output, in seconds.
181182
Defaults to 5.0.
@@ -185,7 +186,7 @@ def nonwear_array_cleanup(
185186
returned as a boolean (True == nonwear).
186187
"""
187188
time_fix_nonwear = _time_fix(
188-
nonwear_array, reference_measurement.time[-1], reference_measurement.time[0]
189+
data_measurement, reference_measurement.time[-1], reference_measurement.time[0]
189190
)
190191
resampled_nonwear = computations.resample(time_fix_nonwear, epoch_length)
191192
binary_nonwear = np.where(resampled_nonwear.measurements >= 0.5, 1, 0)

tests/unit/test_orchestrator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def dummy_results() -> writers.OrchestratorResults:
2929
physical_activity_levels=dummy_measure,
3030
nonwear_status=dummy_measure,
3131
sleep_status=dummy_measure,
32+
sib_periods=dummy_measure,
33+
spt_periods=dummy_measure,
3234
)
3335

3436
return dummy_results

tests/unit/test_writers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def dummy_results() -> writers.OrchestratorResults:
2828
physical_activity_levels=dummy_measure,
2929
nonwear_status=dummy_measure,
3030
sleep_status=dummy_measure,
31+
sib_periods=dummy_measure,
32+
spt_periods=dummy_measure,
3133
)
3234

3335
return dummy_results

0 commit comments

Comments
 (0)