Skip to content

Commit 7e33a83

Browse files
committed
dynamic range change, literal casting.
1 parent 68c8aa1 commit 7e33a83

4 files changed

Lines changed: 25 additions & 20 deletions

File tree

src/wristpy/core/orchestrator.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,6 @@ def _run_file(
316316

317317
watch_data = readers.read_watch_data(input)
318318

319-
dynamic_range = watch_data.dynamic_range
320-
321319
if calibrator is None:
322320
logger.debug("Running without calibration")
323321
calibrated_acceleration = watch_data.acceleration
@@ -341,7 +339,7 @@ def _run_file(
341339
calibrated_acceleration,
342340
activity_metric,
343341
epoch_length,
344-
dynamic_range=dynamic_range,
342+
dynamic_range=watch_data.dynamic_range,
345343
)
346344

347345
sleep_detector = analytics.GgirSleepDetection(anglez)
@@ -425,14 +423,13 @@ def _compute_activity(
425423
elif activity_metric == "mad":
426424
return metrics.mean_amplitude_deviation(acceleration, epoch_length=epoch_length)
427425
elif activity_metric == "mims":
428-
return (
429-
metrics.monitor_independent_movement_summary_units(
426+
if dynamic_range is None:
427+
return metrics.monitor_independent_movement_summary_units(
430428
acceleration,
431429
epoch=epoch_length,
432430
)
433-
if dynamic_range is None
434-
else metrics.monitor_independent_movement_summary_units(
435-
acceleration, epoch=epoch_length, dynamic_range=dynamic_range
436-
)
431+
return metrics.monitor_independent_movement_summary_units(
432+
acceleration, epoch=epoch_length, dynamic_range=dynamic_range
437433
)
434+
438435
return metrics.euclidean_norm_minus_one(acceleration, epoch_length=epoch_length)

src/wristpy/io/readers/readers.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import os
44
import pathlib
5-
from typing import Literal, Optional, Union
5+
from typing import Literal, Union, cast
66

77
import actfast
88
import numpy as np
@@ -41,14 +41,16 @@ def read_watch_data(file_name: Union[pathlib.Path, str]) -> models.WatchData:
4141
)
4242

4343
file_type = os.path.splitext(file_name)[1]
44+
file_type = cast(Literal[".gt3x", ".bin"], file_type)
4445
idle_sleep_mode_flag = False
4546
if file_type == ".gt3x":
4647
idle_sleep_mode_flag = (
4748
data["metadata"]["device_feature_enabled"]["sleep_mode"].lower() == "true"
4849
)
4950

5051
dynamic_range = _extract_dynamic_range(
51-
metadata=data["metadata"], file_type=file_type
52+
metadata=data["metadata"],
53+
file_type=file_type,
5254
)
5355

5456
return models.WatchData(
@@ -63,8 +65,8 @@ def read_watch_data(file_name: Union[pathlib.Path, str]) -> models.WatchData:
6365

6466

6567
def _extract_dynamic_range(
66-
metadata: dict, file_type: str
67-
) -> Optional[tuple[float, float]]:
68+
metadata: dict, file_type: Literal[".gt3x", ".bin"]
69+
) -> tuple[float, float]:
6870
"""Extract the dynamic range from metadata.
6971
7072
Args:
@@ -73,9 +75,12 @@ def _extract_dynamic_range(
7375
7476
Returns:
7577
A tuple containing the accelerometer range.
78+
79+
Raises:
80+
ValueError: If file type is not supported.
7681
"""
7782
if file_type == ".gt3x":
78-
dynamic_range = (
83+
return (
7984
float(metadata.get("info", {}).get("Acceleration Min")),
8085
float(metadata.get("info", {}).get("Acceleration Max")),
8186
)
@@ -86,9 +91,9 @@ def _extract_dynamic_range(
8691
.strip()
8792
.split(" to ")
8893
)
89-
dynamic_range = (float(range_str[0]), float(range_str[1]))
94+
return (float(range_str[0]), float(range_str[1]))
9095

91-
return dynamic_range
96+
raise ValueError(f"Unsupported file type given: {file_type}")
9297

9398

9499
def unix_epoch_time_to_polars_datetime(

src/wristpy/processing/metrics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Calculate base metrics, anglez and enmo."""
22

3-
from typing import Literal, Tuple
3+
from typing import Literal, Optional, Tuple
44

55
import numpy as np
66
import polars as pl
@@ -615,7 +615,7 @@ def monitor_independent_movement_summary_units(
615615
combination_method: Literal["sum", "vector_magnitude"] = "sum",
616616
epoch: float = 60.0,
617617
interpolation_frequency: int = 100,
618-
dynamic_range: Tuple[float, float] = (-8.0, 8.0),
618+
dynamic_range: tuple[float, float] = (-8.0, 8.0),
619619
cutoffs: Tuple[float, float] = (0.2, 5.0),
620620
order: int = 4,
621621
*,

tests/unit/test_readers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test readers.py functions."""
22

33
import pathlib
4+
from typing import Literal, cast
45

56
import actfast
67
import pytest
@@ -49,9 +50,10 @@ def test_extract_dynamic_range_bin(sample_data_bin: pathlib.Path) -> None:
4950
"""Test extracting dynamic range metadata from .bin files."""
5051
expected_dynamic_range = (-8, 8)
5152
data = actfast.read(sample_data_bin)
53+
file_type = cast(Literal[".gt3x", ".bin"], sample_data_bin.suffix)
5254

5355
result = readers._extract_dynamic_range(
54-
metadata=data["metadata"], file_type=sample_data_bin.suffix
56+
metadata=data["metadata"], file_type=file_type
5557
)
5658

5759
assert (
@@ -63,9 +65,10 @@ def test_extract_dynamic_range_gt3x(sample_data_bin: pathlib.Path) -> None:
6365
"""Test extracting dynamic range metadata from .gt3x files."""
6466
expected_dynamic_range = (-8, 8)
6567
data = actfast.read(sample_data_bin)
68+
file_type = cast(Literal[".gt3x", ".bin"], sample_data_bin.suffix)
6669

6770
result = readers._extract_dynamic_range(
68-
metadata=data["metadata"], file_type=sample_data_bin.suffix
71+
metadata=data["metadata"], file_type=file_type
6972
)
7073

7174
assert (

0 commit comments

Comments
 (0)