""" minimateplus/decode_check_carveouts.py — the proven ways Blastware's report differs from the instrument's data. Every disagreement between SFM and Blastware is SFM's bug until shown otherwise. A carve-out is that showing: the fields it covers, the rule it applies, and evidence that does not rest on SFM's own output -- Blastware contradicting itself, the instrument's own data, a third source, or Instantel documentation. `compare` turns a covered mismatch into `accepted` only when the rule holds, and every run counts the events each carve-out covers. Pure: no I/O, and no import from `sfm/`. """ from __future__ import annotations from dataclasses import dataclass from typing import Callable from .bw_xml_report import XmlReport from .decode_check import FieldResult, SfmSide, _printed_number, series_pairs, matches_printed @dataclass(frozen=True) class Carveout: id: str fields: tuple[str, ...] # exact names, or prefixes ending in "." evidence: str applies: Callable[[SfmSide, XmlReport, FieldResult], bool] _TOL = 0.0006 def _at_ceiling(value: float, r: float) -> bool: """SFM's value sits at the range ceiling: a decoder unit or two past nominal full scale is the clipped count itself (32016 = 10.005 in/s). An 8x Sensitive-range value is far above the band and does not qualify.""" return r - _TOL <= abs(value) <= r * 1.01 def _clamped(printed: str, r: float) -> bool: if printed.strip() == "OORANGE": return True p = _printed_number(printed) return p is not None and abs(p[0]) >= r - _TOL def _saturation(sfm: SfmSide, ref: XmlReport, res: FieldResult) -> bool: try: r = float(ref.scalars["GeoRange"]) except (KeyError, ValueError): return False if res.field in ("TranPPV", "VertPPV", "LongPPV"): v = sfm.values.get(res.field) return v is not None and _clamped(res.bw or "", r) and _at_ceiling(float(v), r) if res.field == "PeakVectorSum": # A vector sum can exceed the range (11.55 on T438HGTZ.B30W); it is the # clipped channel under it that must sit at the ceiling. return (_clamped(res.bw or "", r) and any(sfm.values.get(f) is not None and _at_ceiling(float(sfm.values[f]), r) for f in ("TranPPV", "VertPPV", "LongPPV"))) got = series_pairs(sfm, ref, res.field) if got is None or got[1]: return False differing = [(cell, v) for _, cell, v in got[0] if not matches_printed(v, cell)] return bool(differing) and all( _clamped(cell, r) and _at_ceiling(v, r) and (cell.strip() == "OORANGE" or (v >= 0) == (_printed_number(cell)[0] >= 0)) for cell, v in differing) SATURATION = Carveout( id="saturation", fields=("TranPPV", "VertPPV", "LongPPV", "PeakVectorSum", "samples.Tran", "samples.Vert", "samples.Long", "intervals.Tran Peak in/s", "intervals.Vert Peak in/s", "intervals.Long Peak in/s"), evidence=("Blastware clamps a clipped channel to the range maximum and prints OORANGE; " "the file holds counts past full scale (32016 counts = 10.005 in/s observed). " "scratch/verify_against_ascii.py, 2026-08-28."), applies=_saturation, ) CARVEOUTS: tuple[Carveout, ...] = (SATURATION,)