fix(sfm): a MiniMate download that decodes as nothing is refused, not stored as a histogram
Review of 36f4e5d (measured): with the metadata-page bug put back, 4 of 7
BE12599 non-first waveforms decoded to nothing, and the check stored them
unverified as 'a histogram' -- the worst form of the bug it was built for.
Now: no waveform decode must decode as a histogram (stored unverified) or it is
refused; a waveform's record chain must end on its END record (0x06), as the
file reader requires -- cut-short streams have none; the structure checks run
for every unit and only the peak comparison is limited to a unit confirmed in
normal range (geo_range == 0; an unread range is no longer assumed normal);
a clipped blast at full scale on both sides counts as a match. Tests pin the
one-step tolerance, both ranges, the END record and the neither case.
No DB or schema change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
ac34f80daa
commit
c32a9e7851
2 files changed
+94
-20
No files matched your search
+46
-13
@@ -102,34 +102,67 @@ _GEO_FULL_SCALE_IPS = 10.0 # normal range; one decoder step = 0.005 in/s
|
|||||||
_PEAK_TOLERANCE_IPS = 0.0051 # one 0.005 in/s step, plus the 0C's float32 slop
|
_PEAK_TOLERANCE_IPS = 0.0051 # one 0.005 in/s step, plus the 0C's float32 slop
|
||||||
|
|
||||||
|
|
||||||
def verify_s3_download(ev: Any, compliance: Any) -> tuple:
|
def verify_s3_download(ev: Any, compliance: Any, body: Optional[bytes] = None) -> tuple:
|
||||||
"""Check a downloaded waveform against the unit's own 0C peaks: (verified, why).
|
"""Check a downloaded event against itself and the unit's own 0C record: (verified, why).
|
||||||
|
|
||||||
True: all three geophone channels decode to the same length and each
|
A short or misassembled event must not reach the store -- BE9441's bench
|
||||||
channel's peak is the unit's own to one 0.005 in/s step. None: nothing to
|
(2026-10-25) stored two as successes before this existed. `body` is the
|
||||||
check against -- no waveform decode (a histogram: none of ~430 fixture
|
event's 5A body (taken from `ev._a5_frames` when not given). In order:
|
||||||
histograms decodes as a waveform) or a sensitive-range unit, whose scale is
|
|
||||||
still open (the 8x discrepancy). Raises MalformedReply ("not saved")
|
* No waveform decode: it must decode as a histogram (stored unverified --
|
||||||
otherwise: a short or misassembled event must not reach the store. BE9441's
|
its peaks are not compared yet), or it is refused. 0 of ~430 fixture
|
||||||
bench (2026-10-25) stored two such events as successes before this existed.
|
histograms decode as a waveform; a corrupted waveform decodes as neither.
|
||||||
|
* A waveform's record chain must end on its END record (0x06), as the
|
||||||
|
file reader requires; a cut-short stream does not.
|
||||||
|
* All three geophone channels must be the same length.
|
||||||
|
* Each channel's peak must be the unit's own to one 0.005 in/s step (a
|
||||||
|
clipped blast -- both at full scale -- counts as a match). Only for a
|
||||||
|
unit confirmed in normal range: a sensitive-range unit's scale is still
|
||||||
|
open (the 8x discrepancy), and an unread range is not assumed.
|
||||||
|
|
||||||
|
Raises MalformedReply ("not saved") on any failure.
|
||||||
"""
|
"""
|
||||||
|
from minimateplus.blastware_file import extract_body_bytes
|
||||||
|
from minimateplus.histogram_codec import decode_histogram_body
|
||||||
|
from minimateplus.waveform_codec import STREAM_END_ID, walk_records
|
||||||
|
|
||||||
|
if body is None:
|
||||||
|
frames = getattr(ev, "_a5_frames", None)
|
||||||
|
body = extract_body_bytes(frames)[1] if frames else b""
|
||||||
samples = getattr(ev, "raw_samples", None) or {}
|
samples = getattr(ev, "raw_samples", None) or {}
|
||||||
chans = ("Tran", "Vert", "Long")
|
chans = ("Tran", "Vert", "Long")
|
||||||
if not all(samples.get(c) for c in chans):
|
if not all(samples.get(c) for c in chans):
|
||||||
return None, "no waveform decode to check (a histogram, or no codec for it)"
|
if body and decode_histogram_body(body):
|
||||||
if getattr(compliance, "geo_range", 0) not in (0, None):
|
return None, "a histogram: stored unverified (its peaks are not compared yet)"
|
||||||
return None, "sensitive range: the sample scale is not settled, so not checked"
|
raise MalformedReply(
|
||||||
|
"the downloaded event decodes as neither a waveform nor a histogram -- the "
|
||||||
|
"download is incomplete or misassembled; not saved")
|
||||||
|
try:
|
||||||
|
recs = walk_records(body)
|
||||||
|
except Exception: # noqa: BLE001 -- treated as no chain
|
||||||
|
recs = []
|
||||||
|
tail = recs[-1]["end"] if recs else None
|
||||||
|
if tail is None or tail + 5 > len(body) or body[tail + 4] != STREAM_END_ID:
|
||||||
|
raise MalformedReply(
|
||||||
|
"the downloaded waveform's record chain has no end record -- the download is "
|
||||||
|
"cut short; not saved")
|
||||||
lengths = {c: len(samples[c]) for c in chans}
|
lengths = {c: len(samples[c]) for c in chans}
|
||||||
if len(set(lengths.values())) != 1:
|
if len(set(lengths.values())) != 1:
|
||||||
raise MalformedReply(
|
raise MalformedReply(
|
||||||
f"the downloaded waveform's channels have different lengths {lengths} -- the "
|
f"the downloaded waveform's channels have different lengths {lengths} -- the "
|
||||||
"download is incomplete or misassembled; not saved")
|
"download is incomplete or misassembled; not saved")
|
||||||
|
if getattr(compliance, "geo_range", None) != 0:
|
||||||
|
return None, ("whole, but peaks not checked: the unit is not confirmed in normal "
|
||||||
|
"range (a sensitive-range scale is not settled)")
|
||||||
pv = ev.peak_values
|
pv = ev.peak_values
|
||||||
unit = {"Tran": pv.tran, "Vert": pv.vert, "Long": pv.long} if pv else {}
|
unit = {"Tran": pv.tran, "Vert": pv.vert, "Long": pv.long} if pv else {}
|
||||||
|
clip = _GEO_FULL_SCALE_IPS - _PEAK_TOLERANCE_IPS
|
||||||
for c in chans:
|
for c in chans:
|
||||||
if unit.get(c) is None:
|
if unit.get(c) is None:
|
||||||
return None, f"the unit's record gives no {c} peak to check against"
|
return None, f"whole, but the unit's record gives no {c} peak to check against"
|
||||||
decoded = max(abs(x) for x in samples[c]) * _GEO_FULL_SCALE_IPS / _GEO_INT16_FS
|
decoded = max(abs(x) for x in samples[c]) * _GEO_FULL_SCALE_IPS / _GEO_INT16_FS
|
||||||
|
if decoded >= clip and unit[c] >= clip:
|
||||||
|
continue # both at full scale: a clipped blast
|
||||||
if abs(decoded - unit[c]) > _PEAK_TOLERANCE_IPS:
|
if abs(decoded - unit[c]) > _PEAK_TOLERANCE_IPS:
|
||||||
raise MalformedReply(
|
raise MalformedReply(
|
||||||
f"the downloaded {c} peak is {decoded:.4f} in/s but the unit's own record says "
|
f"the downloaded {c} peak is {decoded:.4f} in/s but the unit's own record says "
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
import minimateplus.blastware_file as bw # noqa: E402
|
import minimateplus.blastware_file as bw # noqa: E402
|
||||||
|
from minimateplus.framing import S3FrameParser # noqa: E402
|
||||||
|
from minimate_replay import FIX # noqa: E402
|
||||||
import minimateplus.client as mc # noqa: E402
|
import minimateplus.client as mc # noqa: E402
|
||||||
from minimate_replay import KEY, SessionReplay, fast_clock, recorded_replies # noqa: E402
|
from minimate_replay import KEY, SessionReplay, fast_clock, recorded_replies # noqa: E402
|
||||||
from minimateplus.models import ComplianceConfig, Event, PeakValues # noqa: E402
|
from minimateplus.models import ComplianceConfig, Event, PeakValues # noqa: E402
|
||||||
@@ -47,35 +49,74 @@ def flat(n, peak_ips):
|
|||||||
|
|
||||||
|
|
||||||
NORMAL = ComplianceConfig(geo_range=0)
|
NORMAL = ComplianceConfig(geo_range=0)
|
||||||
|
GOOD = bw.extract_body_bytes( # BE9441 event 1, whole
|
||||||
|
[f for f in S3FrameParser().feed(FIX.read_bytes()) if f.sub == 0xA5])[1]
|
||||||
|
_H = (FIX.parent.parent / "ts-fix" / "K441LKZU.C30H").read_bytes() # BE9441's own histogram
|
||||||
|
HISTOGRAM = _H[bw._WAVEFORM_HEADER_SIZE + 21:-26]
|
||||||
|
|
||||||
|
|
||||||
def test_matching_peaks_and_lengths_verify():
|
def test_matching_peaks_and_lengths_verify():
|
||||||
ev = event({c: flat(100, p) for c, p in zip(("Tran", "Vert", "Long"), (0.03, 0.095, 0.02))},
|
ev = event({c: flat(100, p) for c, p in zip(("Tran", "Vert", "Long"), (0.03, 0.095, 0.02))},
|
||||||
(0.02999996, 0.09499989, 0.01999998))
|
(0.02999996, 0.09499989, 0.01999998))
|
||||||
assert verify_s3_download(ev, NORMAL)[0] is True
|
assert verify_s3_download(ev, NORMAL, body=GOOD)[0] is True
|
||||||
|
|
||||||
|
|
||||||
def test_a_peak_that_disagrees_is_refused():
|
def test_a_peak_that_disagrees_is_refused():
|
||||||
ev = event({c: flat(100, p) for c, p in zip(("Tran", "Vert", "Long"), (0.01, 0.01, 0.005))},
|
ev = event({c: flat(100, p) for c, p in zip(("Tran", "Vert", "Long"), (0.01, 0.01, 0.005))},
|
||||||
(0.03, 0.095, 0.02))
|
(0.03, 0.095, 0.02))
|
||||||
with pytest.raises(MalformedReply, match="Tran peak is 0.0100.*not saved"):
|
with pytest.raises(MalformedReply, match="Tran peak is 0.0100.*not saved"):
|
||||||
verify_s3_download(ev, NORMAL)
|
verify_s3_download(ev, NORMAL, body=GOOD)
|
||||||
|
|
||||||
|
|
||||||
def test_unequal_channel_lengths_are_refused():
|
def test_unequal_channel_lengths_are_refused():
|
||||||
ev = event({"Tran": flat(100, 0.03), "Vert": flat(100, 0.095), "Long": flat(90, 0.02)},
|
ev = event({"Tran": flat(100, 0.03), "Vert": flat(100, 0.095), "Long": flat(90, 0.02)},
|
||||||
(0.03, 0.095, 0.02))
|
(0.03, 0.095, 0.02))
|
||||||
with pytest.raises(MalformedReply, match="lengths.*not saved"):
|
with pytest.raises(MalformedReply, match="lengths.*not saved"):
|
||||||
verify_s3_download(ev, NORMAL)
|
verify_s3_download(ev, NORMAL, body=GOOD)
|
||||||
|
|
||||||
|
|
||||||
def test_no_waveform_decode_is_stored_unverified():
|
def test_a_histogram_is_stored_unverified():
|
||||||
assert verify_s3_download(event(None, (0.03, 0.095, 0.02)), NORMAL)[0] is None
|
ok, why = verify_s3_download(event(None, (0.03, 0.095, 0.02)), NORMAL, body=HISTOGRAM)
|
||||||
|
assert ok is None and "histogram" in why
|
||||||
|
|
||||||
|
|
||||||
def test_a_sensitive_range_unit_is_stored_unverified():
|
def test_a_body_that_decodes_as_neither_is_refused():
|
||||||
|
with pytest.raises(MalformedReply, match="neither a waveform nor a histogram.*not saved"):
|
||||||
|
verify_s3_download(event(None, (0.03, 0.095, 0.02)), NORMAL, body=GOOD[:4000])
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_waveform_without_its_end_record_is_refused():
|
||||||
|
ev = event({c: flat(100, p) for c, p in zip(("Tran", "Vert", "Long"), (0.03, 0.095, 0.02))},
|
||||||
|
(0.03, 0.095, 0.02))
|
||||||
|
with pytest.raises(MalformedReply, match="end record.*not saved"):
|
||||||
|
verify_s3_download(ev, NORMAL, body=GOOD[:20_000])
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_sensitive_range_unit_is_structure_checked_but_not_peak_checked():
|
||||||
ev = event({c: flat(100, 0.5) for c in ("Tran", "Vert", "Long")}, (0.03, 0.095, 0.02))
|
ev = event({c: flat(100, 0.5) for c in ("Tran", "Vert", "Long")}, (0.03, 0.095, 0.02))
|
||||||
assert verify_s3_download(ev, ComplianceConfig(geo_range=1))[0] is None
|
assert verify_s3_download(ev, ComplianceConfig(geo_range=1), body=GOOD)[0] is None
|
||||||
|
short = event({"Tran": flat(100, 0.5), "Vert": flat(100, 0.5), "Long": flat(90, 0.5)},
|
||||||
|
(0.03, 0.095, 0.02))
|
||||||
|
with pytest.raises(MalformedReply, match="lengths"):
|
||||||
|
verify_s3_download(short, ComplianceConfig(geo_range=1), body=GOOD)
|
||||||
|
|
||||||
|
|
||||||
|
def test_an_unknown_range_is_not_peak_checked():
|
||||||
|
ev = event({c: flat(100, 0.5) for c in ("Tran", "Vert", "Long")}, (0.03, 0.095, 0.02))
|
||||||
|
assert verify_s3_download(ev, ComplianceConfig(), body=GOOD)[0] is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_clipped_blast_is_not_refused():
|
||||||
|
ev = event({c: [0] * 99 + [32016] for c in ("Tran", "Vert", "Long")}, (10.0, 10.0, 10.0))
|
||||||
|
assert verify_s3_download(ev, NORMAL, body=GOOD)[0] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_tolerance_is_one_step():
|
||||||
|
near = event({c: flat(100, 0.095) for c in ("Tran", "Vert", "Long")}, (0.095 + 0.005,) * 3)
|
||||||
|
assert verify_s3_download(near, NORMAL, body=GOOD)[0] is True
|
||||||
|
far = event({c: flat(100, 0.095) for c in ("Tran", "Vert", "Long")}, (0.095 + 0.0055,) * 3)
|
||||||
|
with pytest.raises(MalformedReply):
|
||||||
|
verify_s3_download(far, NORMAL, body=GOOD)
|
||||||
|
|
||||||
|
|
||||||
def adapter():
|
def adapter():
|
||||||
|
|||||||
Reference in new issue
Block a user