|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# /usr/bin/env python3 |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +__coding__ = "utf-8" |
| 8 | +__authors__ = ["Brian R. Pauw"] # add names to the list as appropriate |
| 9 | +__copyright__ = "Copyright 2025, The MoDaCor team" |
| 10 | +__date__ = "13/12/2025" |
| 11 | +__status__ = "Development" # "Development", "Production" |
| 12 | +__version__ = "20251213.1" |
| 13 | + |
| 14 | +__all__ = ["TraceEvent"] |
| 15 | + |
| 16 | +import json |
| 17 | +from hashlib import sha256 |
| 18 | +from typing import Any |
| 19 | + |
| 20 | +from attrs import define, field, validators |
| 21 | + |
| 22 | + |
| 23 | +def _to_jsonable(value: Any) -> Any: |
| 24 | + """ |
| 25 | + Convert arbitrary objects into a JSON-serializable structure. |
| 26 | +
|
| 27 | + Rules: |
| 28 | + - dict keys become strings |
| 29 | + - tuples/sets become lists |
| 30 | + - unknown objects become str(value) |
| 31 | + """ |
| 32 | + if value is None or isinstance(value, (str, int, float, bool)): |
| 33 | + return value |
| 34 | + |
| 35 | + if isinstance(value, dict): |
| 36 | + return {str(k): _to_jsonable(v) for k, v in value.items()} |
| 37 | + |
| 38 | + if isinstance(value, (list, tuple, set)): |
| 39 | + return [_to_jsonable(v) for v in value] |
| 40 | + |
| 41 | + # Common numpy-like scalars without importing numpy |
| 42 | + if hasattr(value, "item") and callable(getattr(value, "item")): |
| 43 | + try: |
| 44 | + return _to_jsonable(value.item()) |
| 45 | + except Exception: |
| 46 | + pass |
| 47 | + |
| 48 | + return str(value) |
| 49 | + |
| 50 | + |
| 51 | +def _stable_hash_dict(d: dict[str, Any]) -> str: |
| 52 | + """ |
| 53 | + Stable content hash of a dict (order-independent). |
| 54 | + """ |
| 55 | + canonical = json.dumps(_to_jsonable(d), sort_keys=True, separators=(",", ":"), ensure_ascii=False) |
| 56 | + return sha256(canonical.encode("utf-8")).hexdigest() |
| 57 | + |
| 58 | + |
| 59 | +@define(frozen=True, slots=True) |
| 60 | +class TraceEvent: |
| 61 | + """ |
| 62 | + A small, UI-friendly trace record for a single executed step. |
| 63 | +
|
| 64 | + Intended to be embedded into Pipeline.to_spec() so graph viewers can show: |
| 65 | + - configuration used by the step |
| 66 | + - what changed (units/dimensionality/shape/NaNs/etc.) |
| 67 | + - optional human messages (later) |
| 68 | +
|
| 69 | + Notes |
| 70 | + ----- |
| 71 | + Keep this JSON-friendly and lightweight: no arrays, no heavy objects. |
| 72 | + """ |
| 73 | + |
| 74 | + step_id: str |
| 75 | + module: str |
| 76 | + label: str = "" |
| 77 | + |
| 78 | + module_path: str = "" |
| 79 | + version: str = "" |
| 80 | + |
| 81 | + requires_steps: tuple[str, ...] = field(factory=tuple) |
| 82 | + |
| 83 | + # configuration as used for execution (JSON-friendly) |
| 84 | + config: dict[str, Any] = field(factory=dict) |
| 85 | + |
| 86 | + # computed stable hash of config |
| 87 | + config_hash: str = field(init=False) |
| 88 | + |
| 89 | + # dataset key -> { "diff": [...], "prev": {...} | None, "now": {...} } |
| 90 | + # Use a simple key like "sample.signal" or "sample_background.signal" |
| 91 | + datasets: dict[str, Any] = field(factory=dict) |
| 92 | + |
| 93 | + # reserved for later (MessageHandler, timing, etc.) |
| 94 | + messages: list[dict[str, Any]] = field(factory=list) |
| 95 | + |
| 96 | + # wall-clock runtime for this step execution (seconds) |
| 97 | + duration_s: float | None = field(default=None, validator=validators.optional(validators.instance_of(float))) |
| 98 | + |
| 99 | + def __attrs_post_init__(self) -> None: |
| 100 | + object.__setattr__(self, "config_hash", _stable_hash_dict(self.config)) |
| 101 | + |
| 102 | + def to_dict(self) -> dict[str, Any]: |
| 103 | + """ |
| 104 | + JSON-serializable representation suitable for Pipeline.to_spec(). |
| 105 | + """ |
| 106 | + return { |
| 107 | + "step_id": self.step_id, |
| 108 | + "module": self.module, |
| 109 | + "label": self.label, |
| 110 | + "module_path": self.module_path, |
| 111 | + "version": self.version, |
| 112 | + "requires_steps": list(self.requires_steps), |
| 113 | + "config": _to_jsonable(self.config), |
| 114 | + "config_hash": self.config_hash, |
| 115 | + "duration_s": self.duration_s, |
| 116 | + "datasets": _to_jsonable(self.datasets), |
| 117 | + "messages": _to_jsonable(self.messages), |
| 118 | + } |
0 commit comments