added json save button and remove history button. Added logging to save process
This commit is contained in:
@@ -19,6 +19,7 @@ from python_app.storage.npz.snapshot_numpy import (
|
||||
save_trace_history_numpy,
|
||||
select_aligned_histories,
|
||||
)
|
||||
from python_app.storage.npz.vna_history_json import build_vna_history_payload
|
||||
from python_app.storage.store_api import StoreApi
|
||||
|
||||
|
||||
@@ -176,7 +177,12 @@ class NpzStore(StoreApi):
|
||||
{
|
||||
"format": "numpy-directory-v1",
|
||||
"selection_mode": selection_summary["selection_mode"],
|
||||
"anchor_stage": selection_summary.get("anchor_stage", "unknown"),
|
||||
"selected_collection_ids": selection_summary["selected_collection_ids"],
|
||||
"aligned_key_count": int(selection_summary.get("aligned_key_count", 0)),
|
||||
"raw_missing_count": int(selection_summary.get("raw_missing_count", 0)),
|
||||
"preprocessed_missing_count": int(selection_summary.get("preprocessed_missing_count", 0)),
|
||||
"result_missing_count": int(selection_summary.get("result_missing_count", 0)),
|
||||
"raw_collections": len(selected_raw),
|
||||
"preprocessed_collections": len(selected_preprocessed),
|
||||
"result_collections": len(selected_results),
|
||||
@@ -190,12 +196,77 @@ class NpzStore(StoreApi):
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
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)}"
|
||||
)
|
||||
|
||||
selection_summary["raw_count"] = len(selected_raw)
|
||||
selection_summary["preprocessed_count"] = len(selected_preprocessed)
|
||||
selection_summary["result_count"] = len(selected_results)
|
||||
selection_summary["snapshot_dir"] = str(snapshot_dir)
|
||||
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,
|
||||
primary_stage: str = "preprocessed",
|
||||
) -> 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.utcnow().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_path = output_root_dir / f"{output_stem}_vna_bscan_history.json"
|
||||
if output_path.exists():
|
||||
raise FileExistsError(f"Output JSON file already exists: {output_path}")
|
||||
|
||||
selected_raw, selected_preprocessed, selected_results, selection_summary = select_aligned_histories(
|
||||
raw_history,
|
||||
preprocessed_history,
|
||||
result_history,
|
||||
last_n,
|
||||
)
|
||||
payload = build_vna_history_payload(
|
||||
selected_raw,
|
||||
selected_preprocessed,
|
||||
selected_results,
|
||||
input_index=input_index,
|
||||
output_index=output_index,
|
||||
primary_stage=primary_stage,
|
||||
)
|
||||
output_path.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["primary_stage"] = str(primary_stage)
|
||||
summary["output_path"] = str(output_path)
|
||||
return output_path, summary
|
||||
|
||||
def _set_dir(self, kind: str, radar_key: str) -> Path:
|
||||
"""Return directory for set kind and radar key."""
|
||||
return self._root_dir / kind / radar_key
|
||||
|
||||
Reference in New Issue
Block a user