Skip to content

Commit f99cfc7

Browse files
authored
Update registration process for devices to be explicit (#177)
1 parent 4787d8f commit f99cfc7

File tree

29 files changed

+158
-132
lines changed

29 files changed

+158
-132
lines changed

requirements.txt

Whitespace-only changes.

wearipedia/__init__.py

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
# type: ignore[attr-defined]
22
"""wearables in development"""
3-
4-
import importlib
5-
import importlib.util
6-
from importlib import import_module
3+
import os
74

85
try:
96
from importlib import metadata as importlib_metadata
107
except ImportError: # for Python<3.8
118
import importlib_metadata as importlib_metadata
129

13-
from .constants import *
14-
from .devices import *
10+
from .devices import registry
1511

1612

1713
def get_device(device_name, **kwargs):
@@ -34,56 +30,22 @@ def get_device(device_name, **kwargs):
3430
device = wearipedia.get_device("whoop/whoop_4")
3531
...
3632
"""
37-
company, model = device_name.split("/")
38-
39-
module_path = f"{PACKAGE_PATH}/devices/{device_name}.py"
40-
41-
spec = importlib.util.spec_from_file_location(
42-
name=f"wearipedia.devices.{company}.{model}",
43-
location=module_path,
44-
)
45-
46-
module = importlib.util.module_from_spec(spec)
47-
48-
spec.loader.exec_module(module)
49-
50-
class_name = getattr(module, "class_name")
33+
try:
34+
device_class = registry.get_device_class(device_name)
35+
except KeyError:
36+
raise ValueError(f"Device '{device_name}' is not registered")
5137

52-
return getattr(module, class_name)(**kwargs)
38+
return device_class(**kwargs)
5339

5440

55-
def get_all_device_names():
41+
def get_all_device_names() -> list[str]:
5642
"""Get a list of all device names.
5743
5844
:return: a list of device names
5945
:rtype: List
6046
"""
6147

62-
return [
63-
"apple/healthkit",
64-
"biostrap/evo",
65-
"cronometer/cronometer",
66-
"whoop/whoop_4",
67-
"withings/scanwatch",
68-
"withings/bodyplus",
69-
"withings/sleepmat",
70-
"dreem/headband_2",
71-
"dexcom/pro_cgm",
72-
"garmin/fenix_7s",
73-
"google/googlefit",
74-
"polar/h10",
75-
"polar/verity_sense",
76-
"nutrisense/cgm",
77-
"fitbit/fitbit_charge_4",
78-
"fitbit/fitbit_charge_6",
79-
"fitbit/fitbit_sense",
80-
"oura/oura_ring3",
81-
"coros/coros_pace_2",
82-
"polar/vantage",
83-
"strava/strava",
84-
"myfitnesspal/myfitnesspal",
85-
"qualtrics/qualtrics",
86-
]
48+
return registry.REGISTRY.keys()
8749

8850

8951
def get_version() -> str:

wearipedia/constants.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

wearipedia/devices/__init__.py

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
1-
from .apple import *
2-
from .biostrap import *
3-
from .cronometer import *
4-
from .dexcom import *
5-
from .dreem import *
6-
from .fitbit import *
7-
from .garmin import *
8-
from .google import *
9-
from .myfitnesspal import *
10-
from .oura import *
11-
from .polar import *
12-
from .strava import *
13-
from .whoop import *
14-
from .withings import *
1+
"""Device registration module."""
2+
3+
# Import all device classes
4+
from .apple.healthkit import HealthKit
5+
from .biostrap.evo import EVO
6+
from .coros.coros_pace_2 import CorosPace2
7+
from .cronometer.cronometer import Cronometer
8+
from .dexcom.pro_cgm import DexcomProCGM
9+
from .dreem.headband_2 import DreemHeadband2
10+
from .fitbit.fitbit_charge_4 import FitbitCharge4
11+
from .fitbit.fitbit_charge_6 import FitbitCharge6
12+
from .fitbit.fitbit_sense import FitbitSense
13+
from .fitbit.google_pixel_watch import GooglePixelWatch
14+
from .garmin.fenix_7s import Fenix7S
15+
from .google.googlefit import GoogleFit
16+
from .myfitnesspal.myfitnesspal import MyFitnessPal
17+
from .nutrisense.cgm import NutrisenseCGM
18+
from .oura.oura_ring3 import OuraRing3
19+
from .polar.h10 import H10
20+
from .polar.vantage import PolarVantage
21+
from .polar.verity_sense import VeritySense
22+
from .qualtrics.qualtrics import Qualtrics
23+
from .registry import register_device
24+
from .strava.strava import Strava
25+
from .whoop.whoop_4 import Whoop4
26+
from .withings.bodyplus import BodyPlus
27+
from .withings.scanwatch import ScanWatch
28+
from .withings.sleepmat import SleepMat
29+
30+
ALL_DEVICES = [
31+
HealthKit,
32+
EVO,
33+
CorosPace2,
34+
Cronometer,
35+
DexcomProCGM,
36+
DreemHeadband2,
37+
FitbitCharge4,
38+
FitbitCharge6,
39+
FitbitSense,
40+
GooglePixelWatch,
41+
Fenix7S,
42+
GoogleFit,
43+
MyFitnessPal,
44+
NutrisenseCGM,
45+
OuraRing3,
46+
H10,
47+
PolarVantage,
48+
VeritySense,
49+
Qualtrics,
50+
Whoop4,
51+
ScanWatch,
52+
BodyPlus,
53+
SleepMat,
54+
Strava,
55+
]
56+
57+
# Register all devices
58+
for device in ALL_DEVICES:
59+
register_device(device)

wearipedia/devices/apple/healthkit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from ...utils import bin_search, seed_everything
88
from .apple_gen import *
99

10-
class_name = "HealthKit"
11-
1210

1311
class HealthKit(BaseDevice):
1412
"""This device allows you to work with data from `Apple HealthKit <https://www.apple.com/ios/health/>`_ .
@@ -30,6 +28,8 @@ class HealthKit(BaseDevice):
3028
:type use_cache: bool, optional
3129
"""
3230

31+
name = "apple/healthkit"
32+
3333
def __init__(
3434
self,
3535
seed=0,

wearipedia/devices/biostrap/evo.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
from .evo_fetch import *
1212
from .evo_gen import *
1313

14-
class_name = "EVO"
15-
1614

1715
class EVO(BaseDevice):
16+
17+
name = "biostrap/evo"
18+
1819
def __init__(self, seed=0, start_date="2023-06-05", end_date="2023-06-20"):
1920
params = {
2021
"seed": seed,

wearipedia/devices/coros/coros_pace_2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
from .coros_pace_2_fetch import *
77
from .coros_pace_2_gen import *
88

9-
class_name = "Coros_pace_2"
109

11-
12-
class Coros_pace_2(BaseDevice):
10+
class CorosPace2(BaseDevice):
1311
"""This device allows you to work with data from the `Fitbit charge <(https://www.fitbit.com/global/au/products/trackers/charge4)>`_ device.
1412
Available datatypes for this device are:
1513
@@ -28,6 +26,8 @@ class Coros_pace_2(BaseDevice):
2826
:type synthetic_end_date: str, optional
2927
"""
3028

29+
name = "coros/coros_pace_2"
30+
3131
def __init__(
3232
self,
3333
seed=0,

wearipedia/devices/cronometer/cronometer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
from .cronometer_fetch import *
1010
from .cronometer_synthetic import *
1111

12-
class_name = "Cronometer"
13-
1412
# todo: change this to better path
1513
CRED_CACHE_PATH = "/tmp/wearipedia_cronometer_data.pkl"
1614

@@ -38,6 +36,8 @@ class Cronometer(BaseDevice):
3836
:type use_cache: bool, optional
3937
"""
4038

39+
name = "cronometer/cronometer"
40+
4141
def __init__(self, seed=0, start_date="2022-03-01", end_date="2022-06-17"):
4242
params = {
4343
"seed": seed,

wearipedia/devices/dexcom/pro_cgm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
from .pro_cgm_fetch import *
1010
from .pro_cgm_gen import *
1111

12-
class_name = "DexcomProCGM"
13-
1412

1513
class DexcomProCGM(BaseDevice):
1614
"""This device allows you to work with data from the `Dexcom Pro CGM <https://provider.dexcom.com/products/dexcom-g6-pro>`_ device.
@@ -26,6 +24,8 @@ class DexcomProCGM(BaseDevice):
2624
:type synthetic_end_date: str, optional
2725
"""
2826

27+
name = "dexcom/pro_cgm"
28+
2929
def __init__(
3030
self, seed=0, synthetic_start_date="2022-02-16", synthetic_end_date="2022-05-15"
3131
):

wearipedia/devices/dreem/headband_2.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
from .dreem_fetch import *
1111
from .dreem_gen import *
1212

13-
class_name = "DreemHeadband2"
14-
1513

1614
class DreemHeadband2(BaseDevice):
15+
16+
name = "dreem/headband_2"
17+
1718
def __init__(
1819
self, seed=0, synthetic_start_date="2022-03-01", synthetic_end_date="2022-06-17"
1920
):

0 commit comments

Comments
 (0)