Rework radar system

This commit is contained in:
Ayzen
2026-10-06 12:32:26 +03:00
parent 552cdc688a
commit 5bb86b7165
299 changed files with 13537 additions and 27763 deletions
+109 -315
View File
@@ -1,4 +1,4 @@
"""Concrete :class:`StoreApi` implementation backed by NPZ files."""
"""Preprocess sets and datasets backed by NPZ files."""
from __future__ import annotations
@@ -9,26 +9,24 @@ import logging
import os
from pathlib import Path
import shutil
import tempfile
from typing import Any
from uuid import uuid4
from zipfile import BadZipFile
import numpy as np
from python_app.file_io import make_directory, rename_no_replace, sync_directory, temporary_file
from python_app.models.dataset_model import ComboKey, ResultCollection, SweepCollection, TraceData
from python_app.models.run_config_model import RunConfigModel
from python_app.storage.npz.paths import radar_key_from_config, sanitize_path_component
from python_app.storage.npz.serialize import PREPROC_MAGIC, RAW_MAGIC, serialize_trace_collection, validate_trace_collection
from python_app.file_io import sanitize_path_component
from python_app.orchestration.shm.decoder import RAW_MAGIC
from python_app.storage.npz.serialize import serialize_trace_collection, validate_trace_collection
from python_app.storage.npz.snapshot_numpy import (
save_result_history_binary,
save_result_history_numpy,
save_trace_history_binary,
save_trace_history_numpy,
select_aligned_histories,
save_processing_settings,
atomic_snapshot_directory,
)
from python_app.storage.npz.vna_history_json import build_vna_history_payload
from python_app.storage.store_api import StoreApi
logger = logging.getLogger(__name__)
_SET_METADATA_KEY = "metadata_json"
@@ -44,22 +42,11 @@ def _compose_snapshot_stem(name: str, name_prefix: str) -> str:
class SnapshotStreamWriter:
"""Append received raw/preprocessed/result collections to a snapshot dir in chunks.
A streaming alternative to :meth:`NpzStore.save_runtime_snapshot_numpy` for long
recordings: the caller flushes small chunks as measurements arrive and frees them,
so memory stays flat instead of buffering every measurement. The on-disk layout is
identical (``raw``/``preprocessed``/``results`` subtrees), and per-collection
directory indices continue across chunks via a running offset per stage.
"""
__slots__ = ("_dir", "_raw_written", "_preprocessed_written", "_result_written")
"""Publish complete collections incrementally, retaining them if a later write fails."""
def __init__(self, snapshot_dir: Path) -> None:
self._dir = snapshot_dir
self._raw_written = 0
self._preprocessed_written = 0
self._result_written = 0
self._counts = {"raw": 0, "preprocessed": 0, "results": 0}
@property
def directory(self) -> Path:
@@ -67,66 +54,50 @@ class SnapshotStreamWriter:
@property
def result_count(self) -> int:
"""Number of result collections written to disk so far."""
return self._result_written
return self._counts["results"]
def append(
self,
raw: list[SweepCollection],
preprocessed: list[SweepCollection],
results: list[ResultCollection],
*, processing_settings: dict[int, dict] | None = None,
) -> None:
"""Write a chunk without requiring equal stage lengths, continuing each index."""
# Stage a whole chunk before publishing complete collection directories. A
# failed write leaves earlier chunks intact and the next retry uses the same indices.
chunk_dir = Path(tempfile.mkdtemp(prefix=".chunk-", dir=self._dir))
published: list[Path] = []
try:
save_trace_history_numpy(chunk_dir / "raw", raw, index_offset=self._raw_written)
save_trace_history_numpy(
chunk_dir / "preprocessed", preprocessed, index_offset=self._preprocessed_written
)
save_result_history_numpy(chunk_dir / "results", results, index_offset=self._result_written)
for stage in ("raw", "preprocessed", "results"):
stage_dir = self._dir / stage
stage_dir.mkdir(exist_ok=True)
for source in sorted((chunk_dir / stage).iterdir()):
destination = stage_dir / source.name
if destination.exists():
raise FileExistsError(f"Collection directory already exists: {destination}")
source.rename(destination)
published.append(destination)
except BaseException:
for destination in published:
shutil.rmtree(destination)
raise
finally:
shutil.rmtree(chunk_dir)
self._raw_written += len(raw)
self._preprocessed_written += len(preprocessed)
self._result_written += len(results)
"""Append a chunk; after failure, only the unwritten collections may be retried."""
save_processing_settings(self._dir, processing_settings)
for stage, history in (("raw", raw), ("preprocessed", preprocessed)):
stage_dir = self._dir / stage
stage_dir.mkdir(exist_ok=True)
for collection in history:
save_trace_history_numpy(stage_dir, [collection], index_offset=self._counts[stage])
self._counts[stage] += 1
stage_dir = self._dir / "results"
stage_dir.mkdir(exist_ok=True)
for result in results:
save_result_history_numpy(stage_dir, [result], index_offset=self.result_count)
self._counts["results"] += 1
class NpzStore(StoreApi):
class NpzStore:
"""Persist preprocess sets and runtime snapshots using NumPy files."""
def __init__(self, root_dir: Path) -> None:
"""Create store rooted at `root_dir`."""
self._root_dir = root_dir
self._root_dir.mkdir(parents=True, exist_ok=True)
make_directory(self._root_dir, durable=True)
logger.debug("NpzStore rooted at %s", self._root_dir)
@staticmethod
def _vna_json_output_dir(output_root_dir: Path, output_stem: str) -> Path:
"""Return per-export directory for VNA history JSON files."""
return output_root_dir / output_stem
def save_set(self, kind: str, radar_key: str, set_name: str, collection: SweepCollection) -> None:
"""Persist named preprocess set as NPZ and metadata JSON."""
def save_set(
self, kind: str, radar_key: str, set_name: str, collection: SweepCollection,
*, metadata: dict[str, Any] | None = None,
) -> None:
"""Persist a named set with optional JSON metadata embedded in the NPZ."""
self._validate_set_name(set_name)
validate_trace_collection(collection, require_finite=True)
if metadata is not None and not isinstance(metadata, dict):
raise ValueError("Set user metadata must be a JSON object")
set_dir = self._set_dir(kind, radar_key)
set_dir.mkdir(parents=True, exist_ok=True)
make_directory(set_dir, durable=True)
npz_path = set_dir / f"{set_name}.npz"
meta_path = set_dir / f"{set_name}.json"
@@ -137,10 +108,8 @@ class NpzStore(StoreApi):
for trace in collection.traces:
suffix = f"i{trace.combo.input}_o{trace.combo.output}"
freq_key = f"freq_{suffix}"
s11_key = f"s11_{suffix}"
s21_key = f"s21_{suffix}"
payload[freq_key] = np.asarray(trace.frequency_hz, dtype=np.float32)
payload[s11_key] = np.asarray(trace.s11, dtype=np.complex64)
payload[s21_key] = np.asarray(trace.s21, dtype=np.complex64)
combo_records.append(
{
@@ -149,7 +118,6 @@ class NpzStore(StoreApi):
"capture_start_ns": int(trace.capture_start_ns),
"capture_end_ns": int(trace.capture_end_ns),
"freq_key": freq_key,
"s11_key": s11_key,
"s21_key": s21_key,
}
)
@@ -160,24 +128,30 @@ class NpzStore(StoreApi):
"capture_start_ns": int(collection.capture_start_ns),
"capture_end_ns": int(collection.capture_end_ns),
"combos": combo_records,
"metadata": {} if metadata is None else metadata,
}
# Metadata in the NPZ makes a single atomic replacement authoritative. The
# JSON remains a readable sidecar and legacy NPZ/JSON pairs remain loadable.
metadata_json = json.dumps(meta, indent=2)
metadata_json = json.dumps(meta, indent=2, allow_nan=False)
payload[_SET_METADATA_KEY] = np.asarray(metadata_json)
npz_tmp = meta_tmp = backup_path = None
archive_replaced = False
try:
npz_tmp = self._temporary_file(npz_path)
meta_tmp = self._temporary_file(meta_path)
backup_path = self._temporary_file(npz_path) if npz_path.exists() else None
npz_tmp = temporary_file(npz_path)
meta_tmp = temporary_file(meta_path)
backup_path = temporary_file(npz_path) if npz_path.exists() else None
if backup_path is not None:
backup_path.unlink()
os.link(npz_path, backup_path)
with npz_tmp.open("wb") as npz_file:
np.savez(npz_file, **payload)
meta_tmp.write_text(metadata_json, encoding="utf-8")
npz_file.flush()
os.fsync(npz_file.fileno())
with meta_tmp.open("w", encoding="utf-8") as meta_file:
meta_file.write(metadata_json)
meta_file.flush()
os.fsync(meta_file.fileno())
npz_tmp.replace(npz_path)
archive_replaced = True
meta_tmp.replace(meta_path)
@@ -193,6 +167,7 @@ class NpzStore(StoreApi):
logger.exception("Set rollback failed; previous archive retained at %s", backup_path)
backup_path = None # retain the recovery copy rather than deleting it below
raise
sync_directory(set_dir)
raise
finally:
for tmp_path in (npz_tmp, meta_tmp, backup_path):
@@ -200,6 +175,7 @@ class NpzStore(StoreApi):
continue
with suppress(OSError):
tmp_path.unlink(missing_ok=True)
sync_directory(set_dir)
logger.info(
"Saved set %s/%s/%s (%d traces) to %s", kind, radar_key, set_name, len(combo_records), npz_path
)
@@ -209,7 +185,8 @@ class NpzStore(StoreApi):
self._validate_set_name(set_name)
for collection in collections.values():
validate_trace_collection(collection, require_finite=True)
backup_dir = Path(tempfile.mkdtemp(prefix=".sets-", dir=self._root_dir))
backup_dir = self._root_dir / f".sets-{uuid4().hex}"
backup_dir.mkdir()
backups: dict[Path, Path | None] = {}
retain_backups = False
try:
@@ -236,6 +213,12 @@ class NpzStore(StoreApi):
except OSError:
rollback_failed = True
logger.exception("Failed to restore previous set file %s", destination)
for directory in {path.parent for path in backups if path.parent.exists()}:
try:
sync_directory(directory)
except OSError:
rollback_failed = True
logger.exception("Failed to sync restored set directory %s", directory)
retain_backups = rollback_failed
if retain_backups:
raise RuntimeError(
@@ -250,6 +233,12 @@ class NpzStore(StoreApi):
def load_set(self, kind: str, radar_key: str, set_name: str) -> SweepCollection:
"""Load named preprocess set from NPZ representation."""
return self.load_set_with_metadata(kind, radar_key, set_name)[0]
def load_set_with_metadata(
self, kind: str, radar_key: str, set_name: str,
) -> tuple[SweepCollection, dict[str, Any]]:
"""Load a named set and its user metadata; older sets have empty metadata."""
self._validate_set_name(set_name)
set_dir = self._set_dir(kind, radar_key)
npz_path = set_dir / f"{set_name}.npz"
@@ -269,13 +258,16 @@ class NpzStore(StoreApi):
)
if not isinstance(meta, dict):
raise ValueError("Set metadata must be a JSON object")
metadata = meta.get("metadata", {})
if not isinstance(metadata, dict):
raise ValueError("Set user metadata must be a JSON object")
json.dumps(metadata, allow_nan=False)
traces: list[TraceData] = []
for combo in meta["combos"]:
traces.append(
TraceData(
combo=ComboKey(input=combo["input"], output=combo["output"]),
frequency_hz=np.asarray(arrays[combo["freq_key"]], dtype=np.float32),
s11=np.asarray(arrays[combo["s11_key"]], dtype=np.complex64),
s21=np.asarray(arrays[combo["s21_key"]], dtype=np.complex64),
capture_start_ns=combo.get("capture_start_ns", 0),
capture_end_ns=combo.get("capture_end_ns", 0),
@@ -291,7 +283,7 @@ class NpzStore(StoreApi):
)
validate_trace_collection(collection, require_finite=True)
logger.debug("Loaded set %s (%d traces)", set_label, len(traces))
return collection
return collection, metadata
except (OSError, BadZipFile, EOFError, KeyError, TypeError, ValueError) as exc:
logger.exception("Corrupted preprocess set %s", set_label)
raise RuntimeError(f"Corrupted preprocess set {set_label}: {exc}") from exc
@@ -301,34 +293,10 @@ class NpzStore(StoreApi):
set_dir = self._set_dir(kind, radar_key)
if not set_dir.exists():
return []
if kind == "s21_object_calibration":
return sorted(path.name for path in set_dir.iterdir()
if not path.name.startswith(".") and (path / "cal_meta.json").is_file())
return sorted(path.stem for path in set_dir.glob("*.npz"))
def save_object_calibration(self, radar_key: str, set_name: str, calibration: dict[str, Any]) -> None:
"""Save an object calibration in its shared archive format without overwriting a set."""
from python_app.workflows.mimo_calibration import save_calibration
self._validate_set_name(set_name)
destination = self._set_dir("s21_object_calibration", radar_key) / set_name
if destination.exists():
raise FileExistsError(f"Object calibration already exists: {set_name}")
save_calibration(calibration, destination)
def load_object_calibration(self, radar_key: str, set_name: str) -> dict[str, Any]:
"""Load and validate a saved object calibration, retaining all estimation metadata."""
from python_app.workflows.mimo_calibration import load_calibration
self._validate_set_name(set_name)
return load_calibration(self._set_dir("s21_object_calibration", radar_key) / set_name)
def has_combo_coverage(self, kind: str, radar_key: str, set_name: str, combos: list[ComboKey]) -> bool:
"""Validate that named set covers all required switch combinations."""
if kind == "s21_object_calibration":
calibration = self.load_object_calibration(radar_key, set_name)
return all(combo.input in calibration["rx_input_pos"] and combo.output in calibration["tx_output_pos"]
for combo in combos)
collection = self.load_set(kind, radar_key, set_name)
existing = {(trace.combo.input, trace.combo.output) for trace in collection.traces}
required = {(combo.input, combo.output) for combo in combos}
@@ -339,36 +307,11 @@ class NpzStore(StoreApi):
collection = self.load_set(kind, radar_key, set_name)
return self._export_collection_bundle(collection, output_path)
def export_object_calibration_bundle(
self, radar_key: str, set_name: str, output_path: Path, config: RunConfigModel,
) -> Path:
"""Resample pair multipliers on the through-calibration grids used by the acquisition."""
from python_app.workflows.mimo_calibration import calibration_on_grid, validate_for_config
calibration = self.load_object_calibration(radar_key, set_name)
validate_for_config(calibration, config.to_dict())
tx_indices = {int(port): index for index, port in enumerate(calibration["tx_output_pos"])}
rx_indices = {int(port): index for index, port in enumerate(calibration["rx_input_pos"])}
source = self.load_set("s21_calibration", radar_key, config.preprocess.s21.calibration.set_name)
by_combo = {trace.combo: trace for trace in source.traces}
grids: dict[bytes, np.ndarray] = {}
traces: list[TraceData] = []
for combo in config.combos:
key = ComboKey(input=combo.input, output=combo.output)
trace = by_combo[key]
frequency = trace.frequency_hz
grid_key = frequency.tobytes()
if grid_key not in grids:
grids[grid_key] = calibration_on_grid(calibration, frequency)
correction = grids[grid_key][tx_indices[key.output], rx_indices[key.input]]
traces.append(TraceData(key, frequency, np.zeros(frequency.size, np.complex64), correction))
return self._export_collection_bundle(SweepCollection(0, 0, traces), output_path)
def _export_collection_bundle(self, collection: SweepCollection, output_path: Path) -> Path:
"""Publish a complete binary trace bundle using one atomic replacement."""
validate_trace_collection(collection, require_finite=True)
output_path.parent.mkdir(parents=True, exist_ok=True)
temporary = self._temporary_file(output_path)
temporary = temporary_file(output_path)
try:
temporary.write_bytes(serialize_trace_collection(collection, RAW_MAGIC))
temporary.replace(output_path)
@@ -377,28 +320,6 @@ class NpzStore(StoreApi):
logger.info("Exported %d traces to bundle %s", len(collection.traces), output_path)
return output_path
def save_runtime_snapshot(
self,
output_dir: Path,
raw_history: list[SweepCollection],
preprocessed_history: list[SweepCollection],
result_history: list[ResultCollection],
last_n: int,
) -> Path:
"""Save historical runtime collections using binary on-disk format."""
if last_n <= 0:
raise ValueError("last_n must be > 0")
output_dir.mkdir(parents=True, exist_ok=True)
timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
snapshot_dir = output_dir / f"snapshot_{timestamp}"
with atomic_snapshot_directory(snapshot_dir) as staging:
save_trace_history_binary(staging / "raw", raw_history[-last_n:], RAW_MAGIC)
save_trace_history_binary(staging / "preprocessed", preprocessed_history[-last_n:], PREPROC_MAGIC)
save_result_history_binary(staging / "results", result_history[-last_n:])
logger.info("Saved binary runtime snapshot (last_n=%d) to %s", last_n, snapshot_dir)
return snapshot_dir
def snapshot_directory(
self, output_root_dir: Path, snapshot_name: str, *, name_prefix: str = ""
) -> Path:
@@ -435,126 +356,19 @@ class NpzStore(StoreApi):
raw_history: list[SweepCollection],
preprocessed_history: list[SweepCollection],
result_history: list[ResultCollection],
last_n: int,
*,
name_prefix: str = "",
include_unaligned: bool = False,
) -> tuple[Path, dict[str, Any]]:
"""Save historical runtime collections in NumPy tree format."""
if last_n <= 0:
raise ValueError("last_n must be > 0")
snapshot_stem = _compose_snapshot_stem(snapshot_name, name_prefix)
output_root_dir.mkdir(parents=True, exist_ok=True)
snapshot_dir = output_root_dir / snapshot_stem
if snapshot_dir.exists():
raise FileExistsError(f"Snapshot directory already exists: {snapshot_dir}")
selected_raw, selected_preprocessed, selected_results, selection_summary = select_aligned_histories(
raw_history,
preprocessed_history,
result_history,
last_n,
include_unaligned=include_unaligned,
)
if selection_summary.get("anchor_stage") == "results":
expected = min(int(last_n), len(result_history))
if len(selected_results) != expected:
raise RuntimeError(
"Snapshot results selection invariant failed: "
f"expected={expected}, selected={len(selected_results)}"
)
processing_settings: dict[int, dict] | None = None,
) -> Path:
"""Save exactly the supplied collections and their processing revisions."""
snapshot_dir = self.snapshot_directory(output_root_dir, snapshot_name, name_prefix=name_prefix)
with atomic_snapshot_directory(snapshot_dir) as staging:
save_trace_history_numpy(staging / "raw", selected_raw)
save_trace_history_numpy(staging / "preprocessed", selected_preprocessed)
save_result_history_numpy(staging / "results", selected_results)
selection_summary["raw_count"] = len(selected_raw)
selection_summary["preprocessed_count"] = len(selected_preprocessed)
selection_summary["result_count"] = len(selected_results)
selection_summary["snapshot_stem"] = snapshot_stem
selection_summary["snapshot_dir"] = str(snapshot_dir)
logger.info(
"Saved NumPy runtime snapshot to %s (raw=%d preprocessed=%d results=%d, anchor=%s)",
snapshot_dir,
len(selected_raw),
len(selected_preprocessed),
len(selected_results),
selection_summary.get("anchor_stage"),
)
return snapshot_dir, selection_summary
def save_runtime_vna_history_json(
self,
output_root_dir: Path,
output_name: str,
raw_history: list[SweepCollection],
preprocessed_history: list[SweepCollection],
result_history: list[ResultCollection],
last_n: int,
*,
input_index: int = 0,
output_index: int = 0,
channel: str = "s21",
primary_stage: str = "preprocessed",
include_unaligned: bool = False,
) -> tuple[Path, dict[str, Any]]:
"""Save runtime history as vna_system-compatible JSON file."""
if last_n <= 0:
raise ValueError("last_n must be > 0")
output_stem = output_name.strip() or datetime.now(timezone.utc).strftime("snapshot_%Y%m%d_%H%M%S")
output_stem = sanitize_path_component(output_stem)
output_root_dir.mkdir(parents=True, exist_ok=True)
output_dir = self._vna_json_output_dir(output_root_dir, output_stem)
output_path = output_dir / f"{output_stem}_vna_bscan_history.json"
selected_raw, selected_preprocessed, selected_results, selection_summary = select_aligned_histories(
raw_history,
preprocessed_history,
result_history,
last_n,
include_unaligned=include_unaligned,
)
payload = build_vna_history_payload(
selected_raw,
selected_preprocessed,
selected_results,
input_index=input_index,
output_index=output_index,
channel=channel,
primary_stage=primary_stage,
)
with atomic_snapshot_directory(output_dir) as staging:
(staging / output_path.name).write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
summary: dict[str, Any] = dict(selection_summary)
summary["raw_count"] = len(selected_raw)
summary["preprocessed_count"] = len(selected_preprocessed)
summary["result_count"] = len(selected_results)
summary["raw_record_count"] = int(payload.get("raw_record_count", 0))
summary["preprocessed_record_count"] = int(payload.get("preprocessed_record_count", 0))
summary["sweep_count"] = len(payload.get("sweep_history", []))
summary["input_index"] = int(input_index)
summary["output_index"] = int(output_index)
summary["channel"] = str(channel)
summary["primary_stage"] = str(primary_stage)
summary["output_stem"] = output_stem
summary["output_dir"] = str(output_dir)
summary["output_path"] = str(output_path)
logger.info(
"Saved VNA history JSON to %s (input=%d output=%d channel=%s sweeps=%d)",
output_path,
int(input_index),
int(output_index),
channel,
summary["sweep_count"],
)
return output_path, summary
save_processing_settings(staging, processing_settings)
save_trace_history_numpy(staging / "raw", raw_history)
save_trace_history_numpy(staging / "preprocessed", preprocessed_history)
save_result_history_numpy(staging / "results", result_history)
logger.info("Saved NumPy runtime snapshot to %s", snapshot_dir)
return snapshot_dir
def save_runtime_vna_history_json_batch(
self,
@@ -563,33 +377,19 @@ class NpzStore(StoreApi):
raw_history: list[SweepCollection],
preprocessed_history: list[SweepCollection],
result_history: list[ResultCollection],
last_n: int,
*,
channel: str = "s21",
primary_stage: str = "preprocessed",
name_prefix: str = "",
include_unaligned: bool = False,
) -> tuple[list[Path], dict[str, Any]]:
) -> list[Path]:
"""Save one runtime VNA-history JSON per available combo."""
if last_n <= 0:
raise ValueError("last_n must be > 0")
output_stem = _compose_snapshot_stem(output_name, name_prefix)
output_root_dir.mkdir(parents=True, exist_ok=True)
output_dir = self._vna_json_output_dir(output_root_dir, output_stem)
selected_raw, selected_preprocessed, selected_results, selection_summary = select_aligned_histories(
raw_history,
preprocessed_history,
result_history,
last_n,
include_unaligned=include_unaligned,
)
output_dir = output_root_dir / output_stem
combos = sorted(
{
(int(trace.combo.input), int(trace.combo.output))
for collection in [*selected_raw, *selected_preprocessed]
for collection in [*raw_history, *preprocessed_history]
for trace in collection.traces
}
)
@@ -601,45 +401,51 @@ class NpzStore(StoreApi):
payloads: list[dict[str, Any]] = []
for input_index, output_index in combos:
output_path = output_dir / (
f"{output_stem}_i{input_index}_o{output_index}_{channel}_vna_bscan_history.json"
f"{output_stem}_i{input_index}_o{output_index}_s21_vna_bscan_history.json"
)
if output_path.exists():
raise FileExistsError(f"Output JSON file already exists: {output_path}")
payloads.append(
build_vna_history_payload(
selected_raw,
selected_preprocessed,
selected_results,
raw_history,
preprocessed_history,
result_history,
input_index=input_index,
output_index=output_index,
channel=channel,
primary_stage=primary_stage,
)
)
output_paths.append(output_path)
with atomic_snapshot_directory(output_dir) as staging:
output_existed = output_dir.exists()
output_dir.mkdir(exist_ok=True)
published: list[Path] = []
try:
# Publish complete files without overwriting a concurrent export.
for output_path, payload in zip(output_paths, payloads, strict=True):
(staging / output_path.name).write_text(
json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8"
)
temporary = temporary_file(output_path)
try:
with temporary.open("w", encoding="utf-8") as handle:
json.dump(payload, handle, ensure_ascii=False, indent=2, allow_nan=False)
handle.flush()
os.fsync(handle.fileno())
rename_no_replace(temporary, output_path)
published.append(output_path)
finally:
temporary.unlink(missing_ok=True)
sync_directory(output_dir)
except BaseException:
for path in published:
path.unlink()
if not output_existed:
with suppress(OSError):
output_dir.rmdir()
raise
summary: dict[str, Any] = dict(selection_summary)
summary["raw_count"] = len(selected_raw)
summary["preprocessed_count"] = len(selected_preprocessed)
summary["result_count"] = len(selected_results)
summary["preprocessed_record_count"] = int(sum(int(payload.get("preprocessed_record_count", 0)) for payload in payloads))
summary["combo_count"] = len(combos)
summary["combos"] = [list(combo) for combo in combos]
summary["channel"] = str(channel)
summary["primary_stage"] = str(primary_stage)
summary["output_stem"] = output_stem
summary["output_dir"] = str(output_dir)
summary["output_paths"] = [str(path) for path in output_paths]
logger.info(
"Saved %d VNA history JSON file(s) to %s (channel=%s)", len(output_paths), output_dir, channel
"Saved %d S21 VNA history JSON file(s) to %s", len(output_paths), output_dir
)
return output_paths, summary
return output_paths
def _set_dir(self, kind: str, radar_key: str) -> Path:
"""Return directory for set kind and radar key."""
@@ -653,15 +459,6 @@ class NpzStore(StoreApi):
if not value or value in {".", ".."} or Path(value).name != value or "\\" in value:
raise ValueError(f"Invalid set path component: {value!r}")
@staticmethod
def _temporary_file(destination: Path) -> Path:
"""Create a unique temporary file beside its atomic-replacement destination."""
with tempfile.NamedTemporaryFile(
dir=destination.parent, prefix=f".{destination.name}.", suffix=".tmp", delete=False
) as handle:
path = Path(handle.name)
return path
def preview_png_dir(self, kind: str, set_name: str, radar_key: str) -> Path:
"""Return directory for preview PNGs of one set/radar variant.
@@ -671,6 +468,3 @@ class NpzStore(StoreApi):
filesystem-safe by construction.
"""
return self._root_dir / "preview_png" / kind / sanitize_path_component(set_name) / radar_key
__all__ = ["NpzStore", "radar_key_from_config"]