"""Binary payload serialization for sweep/result collections.""" from __future__ import annotations import struct from numbers import Integral import numpy as np from python_app.models.dataset_model import ResultCollection, SweepCollection RAW_MAGIC = 0x32574152 PREPROC_MAGIC = 0x32525050 RESULT_MAGIC = 0x324C5352 # RSL2: adds processing_duration_ns after monotonic_ns def _write_interleaved_complex(buffer: bytearray, values: np.ndarray) -> None: """Append complex64 array as interleaved float32 real/imag pairs.""" interleaved = np.empty(values.size * 2, dtype=" None: """Reject ambiguous combinations and malformed traces before saving any files.""" for field in ("collection_id", "monotonic_ns", "capture_start_ns", "capture_end_ns"): _validate_u64(getattr(collection, field), field) if require_finite and not collection.traces: raise ValueError("Preprocess set must contain at least one trace") seen: set[tuple[int, int]] = set() for trace in collection.traces: _validate_u64(trace.capture_start_ns, "trace.capture_start_ns") _validate_u64(trace.capture_end_ns, "trace.capture_end_ns") if any( isinstance(value, bool) or not isinstance(value, Integral) or not 0 <= value <= 0xFFFFFFFF for value in (trace.combo.input, trace.combo.output) ): raise ValueError("Trace input/output positions must be unsigned 32-bit integers") combo = (int(trace.combo.input), int(trace.combo.output)) if combo in seen: raise ValueError(f"Invalid or duplicate trace combination: {combo}") seen.add(combo) arrays = ( np.asarray(trace.frequency_hz, dtype=np.float32), np.asarray(trace.s11, dtype=np.complex64), np.asarray(trace.s21, dtype=np.complex64), ) if any(array.ndim != 1 for array in arrays) or len({array.shape for array in arrays}) != 1: raise ValueError(f"Trace {combo} frequency, S11 and S21 must be equally sized 1D arrays") if require_finite and (not arrays[0].size or any(not np.isfinite(array).all() for array in arrays)): raise ValueError(f"Preprocess trace {combo} must contain finite, nonempty arrays") def _validate_u64(value: int, field: str) -> None: if isinstance(value, bool) or not isinstance(value, Integral) or not 0 <= value < 1 << 64: raise ValueError(f"{field} must be an unsigned 64-bit integer") def serialize_trace_collection(collection: SweepCollection, magic: int) -> bytes: """Serialize one raw/preprocessed trace collection into ring-compatible binary format. The trailer is appended after the trace blocks so older readers, which stop at the last block, still decode the traces: first the collection capture window, then a per-trace window table (one ``(start_ns, end_ns)`` pair per trace, in trace order). See :func:`python_app.orchestration.shm.decoder.decode_trace_collection` and ``read_trace_collection`` in ``common_cpp/ipc/src/shared_types.cpp``. """ validate_trace_collection(collection) buffer = bytearray() buffer.extend(struct.pack(" bytes: """Serialize one processed collection with result blocks/payloads.""" def serialize_payload(buffer: bytearray, payload) -> None: """Append one payload in ring-compatible result format.""" name_bytes = payload.processing_name.encode("utf-8") if len(name_bytes) > 0xFFFF: raise ValueError("processing_name is too long") buffer.extend(struct.pack("