"""Binary decoders for raw/preprocessed/result payload collections.""" from __future__ import annotations import numpy as np from python_app.models.dataset_model import ( ComboKey, ResultBlock, ResultCollection, ResultPayload, SweepCollection, TraceData, ) from python_app.orchestration.shm.binary_cursor import ByteCursor RAW_MAGIC = 0x32574152 PREPROC_MAGIC = 0x32525050 RESULT_MAGIC = 0x324C5352 # RSL2: adds processing_duration_ns after monotonic_ns def decode_trace_collection(payload: bytes, expected_magic: int) -> SweepCollection: """Decode one raw/preprocessed collection from binary payload.""" cursor = ByteCursor(payload) magic = cursor.read_u32() if magic != expected_magic: raise ValueError("Unexpected trace collection magic") collection_id = cursor.read_u64() monotonic_ns = cursor.read_u64() trace_count = cursor.read_u32() traces: list[TraceData] = [] for _ in range(trace_count): input_pos = cursor.read_u32() output_pos = cursor.read_u32() point_count = cursor.read_u32() freq_bytes = point_count * 4 freq = np.frombuffer(cursor.read_bytes(freq_bytes), dtype=" ResultCollection: """Decode one processed result collection from binary payload.""" def read_payload(cursor: ByteCursor) -> ResultPayload: """Decode one result payload from stream.""" kind = cursor.read_u8() name_size = cursor.read_u16() name = cursor.read_str(name_size) if kind == 1: point_count = cursor.read_u32() freq = np.frombuffer(cursor.read_bytes(point_count * 4), dtype="