From 9cfa82117b77f5701bb477603f0f3c59819b749e Mon Sep 17 00:00:00 2001 From: serversdown Date: Tue, 3 Nov 2026 21:35:54 -0500 Subject: [PATCH] feat(decode): histogram interval times and peak times (#51 group C) With the start right, the rest of Blastware's histogram timing follows, measured on a 2,000-histogram DL2 sample: - each interval's printed time is its end, start + (i + 1) x interval, capped at the stop: 2,308,524 / 2,308,524 rows (event_file_io.interval_times) - each of Tran / Vert / Long / PVS / MicL's peak date and time is that of the interval holding its first maximum: 2,000 / 2,000 each. The checker compares intervals.Time and the ten peak date/time fields. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9 --- minimateplus/decode_check.py | 32 +++++++++++++++++++++++++++++--- minimateplus/event_file_io.py | 12 ++++++++++++ tests/test_decode_check.py | 18 ++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/minimateplus/decode_check.py b/minimateplus/decode_check.py index 5904375..0eda17a 100644 --- a/minimateplus/decode_check.py +++ b/minimateplus/decode_check.py @@ -13,13 +13,14 @@ Pure: no I/O, and no import from `sfm/`. """ from __future__ import annotations +import datetime import math import re from dataclasses import dataclass, field from typing import Callable, Optional, Sequence from .bw_xml_report import XmlReport -from .event_file_io import (_GEO_FS_BY_RANGE, _GEO_INT16_FS, _GEO_NORMAL_FS_INS, _MIC_PSI_PER_COUNT, +from .event_file_io import (interval_times, _GEO_FS_BY_RANGE, _GEO_INT16_FS, _GEO_NORMAL_FS_INS, _MIC_PSI_PER_COUNT, decode_blastware_bytes) from .histogram_codec import half_period_to_hz from .models import Event @@ -88,6 +89,7 @@ class SfmSide: geo_ips_per_count: float = GEO_IPS_PER_COUNT # at the event's range, as SFM read it # interval column -> SFM's value per interval (a number, or Blastware's word) interval_values: dict[str, list[object]] = field(default_factory=dict) + interval_times: Optional[list[str]] = None # each interval's printed time def interval_frequency(halfp: int, sample_rate: int) -> object: @@ -150,6 +152,20 @@ def sfm_side(raw: bytes) -> SfmSide: interval_values[f"{ch} Freq Hz"] = [interval_frequency(h, ev.sample_rate) for h in hps] if ev.interval_pvs: interval_values["Geo PVS in/s"] = list(ev.interval_pvs) + times = None + n = len(counts["Tran"]) + if ev.record_type == "Histogram" and ts is not None and ev.histogram_interval_sec and n: + start = datetime.datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second) + ends = interval_times(start, ev.histogram_interval_sec, ev.stop_time, n) + times = [t.strftime("%Y-%m-%d %H:%M:%S") for t in ends] + # A peak's date and time are those of the interval holding it (first). + for tag, series in (("TranPeak", counts["Tran"]), ("VertPeak", counts["Vert"]), + ("LongPeak", counts["Long"]), ("MicL", counts["MicL"]), + ("PeakVectorSum", ev.interval_pvs)): + if series and len(series) == n: + j = max(range(n), key=lambda q: abs(series[q])) + values[f"{tag}Date"] = ends[j].strftime("%Y-%m-%d") + values[f"{tag}Time"] = ends[j].strftime("%H:%M:%S") return SfmSide( event=ev, @@ -161,6 +177,7 @@ def sfm_side(raw: bytes) -> SfmSide: counts=counts, geo_ips_per_count=_GEO_FS_BY_RANGE.get(ev.geo_range, _GEO_NORMAL_FS_INS) / _GEO_INT16_FS, interval_values=interval_values, + interval_times=times, ) @@ -355,12 +372,21 @@ def _compare_samples(sfm: SfmSide, ref: XmlReport) -> list[FieldResult]: def _compare_intervals(sfm: SfmSide, ref: XmlReport) -> list[FieldResult]: - """One result per column, plus the interval times, which SFM does not keep.""" + """One result per column, plus the interval times.""" n = len(ref.intervals or []) - return ([FieldResult("intervals.Time", "sfm_blank", f"{n} rows", None)] + return ([_compare_interval_times(sfm, ref, n)] + [_compare_series(f"intervals.{t}", n, sfm, ref) for t in ref.interval_columns or []]) +def _compare_interval_times(sfm: SfmSide, ref: XmlReport, n: int) -> FieldResult: + if sfm.interval_times is None: + return FieldResult("intervals.Time", "sfm_blank", f"{n} rows", None) + diffs = [(j, float("inf")) for j, ((t, _), s) in enumerate(zip(ref.intervals or [], sfm.interval_times)) + if not _same_text(s, t)] + return _series_result("intervals.Time", n, len(sfm.interval_times), diffs, + abs(n - len(sfm.interval_times))) + + def compare(sfm: SfmSide, ref: XmlReport, carveouts: Sequence["Carveout"] = ()) -> list[FieldResult]: """Every field Blastware's report carries, set against what SFM produced. A mismatch a carve-out covers and applies to becomes `accepted`.""" diff --git a/minimateplus/event_file_io.py b/minimateplus/event_file_io.py index e692e68..f1d0262 100644 --- a/minimateplus/event_file_io.py +++ b/minimateplus/event_file_io.py @@ -947,6 +947,18 @@ _HISTOGRAM_RECORD_MARKER = b"\xfe\xf5" _HISTOGRAM_RECORD_SOURCE = b"\x04\x00" +def interval_times(start: datetime.datetime, interval_sec: int, + stop: Optional[datetime.datetime], n: int) -> list[datetime.datetime]: + """Each histogram interval's time as Blastware prints it: the interval's + end, start + (i + 1) x interval, capped at the stop. 2,308,524 / 2,308,524 + DL2 rows; and an event's peak date and time are its interval's (2026-11-03).""" + out = [] + for i in range(n): + t = start + datetime.timedelta(seconds=interval_sec * (i + 1)) + out.append(min(t, stop) if stop is not None else t) + return out + + def _histogram_event_record( raw: bytes, end: int, start: Optional[datetime.datetime], stop: Optional[datetime.datetime], diff --git a/tests/test_decode_check.py b/tests/test_decode_check.py index 8edaca6..ac01ebd 100644 --- a/tests/test_decode_check.py +++ b/tests/test_decode_check.py @@ -325,3 +325,21 @@ def test_an_oorange_interval_vector_sum_is_saturation(): def test_an_oorange_vector_sum_needs_a_clamped_channel_in_that_interval(): assert _pvs_oorange(unclamp=True).verdict == "mismatch" + + +_PEAK_TIMES = ("TranPeakDate", "TranPeakTime", "VertPeakDate", "VertPeakTime", "LongPeakDate", + "LongPeakTime", "PeakVectorSumDate", "PeakVectorSumTime", "MicLDate", "MicLTime") + + +@pytest.mark.parametrize("path", [FIX / "T004JTRF.ME0H", # 1 interval + FIX.parent / "footer-11-03-26" / "T003LCYV.JD0H", # 145 + FIX.parent / "histogram-decode-11-02-26" / "K440HA3C.JM0H"]) # 8 x 5 s +def test_histogram_times_match_blastware(path): + # #51: an interval's printed time is its end -- start + (n + 1) x interval, + # capped at the stop -- and each peak's date and time are its interval's: + # 2,308,524 / 2,308,524 rows and 2,000 / 2,000 peaks each on a DL2 sample. + ref = parse_xml_report_file(path.with_name(path.name.replace(".", "_", 1) + "_XML.XML")) + res = {r.field: r for r in compare(sfm_side(path.read_bytes()), ref, CARVEOUTS)} + for f in (("HistogramStartDate", "HistogramStartTime", "HistogramStopTime", "intervals.Time") + + tuple(t for t in _PEAK_TIMES if t in ref.scalars)): + assert res[f].verdict == "match", (f, res[f])