-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
75 lines (58 loc) · 2.73 KB
/
__init__.py
File metadata and controls
75 lines (58 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#
# SPDX-FileCopyrightText: 2026 Stanford University, ETH Zurich, and the project authors (see CONTRIBUTORS.md)
# SPDX-FileCopyrightText: 2026 This source file is part of the SensorTSLM open-source project.
#
# SPDX-License-Identifier: MIT
#
from __future__ import annotations
import abc
import pathlib
import re
import zlib
from dataclasses import dataclass, field
from aggregators import MetricAggregator
from detectors import StructuralDetector
from timef.schema import Annotation, Recording
DEFAULT_TEMPLATES_PATH = pathlib.Path(__file__).resolve().parent.parent / "templates" / "templates.json"
VALID_CAPTION_TYPES = ("statistical", "structural", "semantic", "cross_channel")
_ACTIVITY_RE = re.compile(r"HKWorkoutActivityType(.+)$")
@dataclass(frozen=True)
class ChannelConfig:
names: list[str] # ordered channel names
meta: dict[str, tuple[str, str, int]] # channel -> (display_name, unit, decimals)
continuous: frozenset[str] # which channels are continuous
groups: dict[str, frozenset[str]] = field(default_factory=dict) # semantic/logical channel groups
aggregators: dict[str, MetricAggregator] = field(default_factory=dict) # channel -> aggregator override
detectors: dict[str, list[StructuralDetector]] = field(default_factory=dict) # channel -> detector list
templates_path: pathlib.Path = DEFAULT_TEMPLATES_PATH
time_unit: str = "minutes" # label for the time axis (e.g. "minutes", "hours")
def display_name(self, channel: str) -> str:
if channel in self.meta:
return self.meta[channel][0]
m = _ACTIVITY_RE.search(channel)
if m:
return re.sub(r"(?<=[a-z])(?=[A-Z])", " ", m.group(1)).lower()
if ":" in channel:
return channel.split(":", 1)[1]
return channel
class CaptionExtractor(abc.ABC):
caption_type: str
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if not getattr(cls, "__abstractmethods__", None):
_validate_caption_type(cls)
def __init__(self, config: ChannelConfig):
self.config = config
@staticmethod
def _seed(key: str) -> int:
return zlib.crc32(key.encode("utf-8")) & 0xFFFFFFFF
@abc.abstractmethod
def extract(self, row: Recording) -> list[Annotation]:
"""Extract annotations for a row."""
...
def _validate_caption_type(cls):
ct = getattr(cls, "caption_type", None)
if ct is None:
raise TypeError(f"{cls.__name__} must define class attribute 'caption_type'.")
if ct not in VALID_CAPTION_TYPES:
raise TypeError(f"{cls.__name__}.caption_type must be one of {VALID_CAPTION_TYPES!r}, got {ct!r}.")