File size: 3,851 Bytes
326d3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
from dataclasses import asdict, dataclass, field, fields
from pathlib import Path
import json
import math

CONFIG_ROOT = Path(__file__).resolve().parents[1] / "configs"

@dataclass
class EngineConfig:
    mode: str = "automatic"
    chunk_size: int = 64
    device: str = "cpu"
    score_threshold: float = 0.35
    detector_threshold: float = 0.3
    body_pose_path: str | None = None
    person_detector_path: str | None = None
    object_detector_path: str | None = None
    annotations_path: str | None = None
    body3d_enabled: bool = False
    sam_repo: str | None = None
    sam_checkpoint: str | None = None
    sam_config: str | None = None
    mhr_path: str | None = None
    temporal3d_enabled: bool = True
    anatomy_enabled: bool = True
    fitting_config_path: str = str(CONFIG_ROOT / "fitting.yaml")
    anatomical_config_path: str = str(CONFIG_ROOT / "anatomical_model.yaml")
    mhr_limits_path: str | None = None
    endpoint_checkpoint_path: str | None = None
    endpoint_manifest_path: str | None = None
    event_checkpoint_path: str | None = None
    event_manifest_path: str | None = None
    mstcn_repo: str | None = None
    coaching_rules_path: str = str(CONFIG_ROOT.parent / "rules/coaching_rules.yaml")
    coaching_mode: str = "evidence_linked_experimental"
    quality_config_path: str = str(CONFIG_ROOT / "quality.yaml")
    stage_python: dict[str, str] = field(default_factory=dict)
    learned_events_enabled: bool = False
    bat_3d_mode: str = "disabled"
    export_video: bool = True
    event_parameters: dict = field(default_factory=lambda: {
        "version": "2.0", "ground_tolerance_px":5.0, "interaction_distance_px":8.0, "outgoing_direction_cosine_max":.5, "motion_threshold_px_per_frame": 3.0,
        "sustained_frames": 3, "min_visible_frames": 4,
    })

    def validate(self):
        if self.mode not in {"automatic", "operator_reviewed", "dataset"}:
            raise ValueError("Unknown execution mode")
        if self.chunk_size < 1 or self.device not in {"cpu", "cuda"}:
            raise ValueError("Invalid chunk size or device")
        if not 0 <= self.score_threshold <= 1 or not 0 <= self.detector_threshold <= 1:
            raise ValueError("Score thresholds must lie in [0, 1]")
        if self.mode == "automatic" and self.annotations_path:
            raise ValueError("Human annotations require operator_reviewed or dataset mode")
        for field_name in ("body3d_enabled", "temporal3d_enabled", "anatomy_enabled", "learned_events_enabled", "export_video"):
            if not isinstance(getattr(self, field_name), bool):
                raise ValueError(f"{field_name} must be a boolean")
        if not isinstance(self.bat_3d_mode, str) or not self.bat_3d_mode:
            raise ValueError("bat_3d_mode must identify a requested mode or disabled")
        if self.coaching_mode not in {"descriptive_only", "evidence_linked_experimental"}:
            raise ValueError("Unknown coaching mode")
        # Unsupported optional branch requests are reported as unavailable by workers.
        # They never execute unqualified weights and never block eligible 2D results.
        p = self.event_parameters
        if p.get("sustained_frames", 0) < 1 or p.get("min_visible_frames", 0) < 2:
            raise ValueError("Invalid event support parameters")
        if not math.isfinite(p.get("motion_threshold_px_per_frame", float("nan"))):
            raise ValueError("Invalid event motion threshold")
        return self

    def to_dict(self):
        return asdict(self)

    @classmethod
    def load(cls, path: str | Path):
        raw = json.loads(Path(path).read_text())
        unexpected = set(raw) - {f.name for f in fields(cls)}
        if unexpected:
            raise ValueError(f"Unknown configuration keys: {sorted(unexpected)}")
        return cls(**raw).validate()