33 lines
958 B
Python
33 lines
958 B
Python
"""Diagnostics export without a plotting dependency."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import json
|
|
from dataclasses import asdict, is_dataclass
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import numpy as np
|
|
|
|
|
|
def export_json(path: str | Path, value: Any) -> None:
|
|
payload = asdict(value) if is_dataclass(value) else value
|
|
Path(path).write_text(json.dumps(payload, indent=2, default=str), encoding="utf-8")
|
|
|
|
|
|
def export_tracking_csv(path: str | Path, measurements: list[Any]) -> None:
|
|
if not measurements:
|
|
Path(path).write_text("", encoding="utf-8")
|
|
return
|
|
rows = [asdict(item) for item in measurements]
|
|
with Path(path).open("w", newline="", encoding="utf-8") as stream:
|
|
writer = csv.DictWriter(stream, fieldnames=list(rows[0]))
|
|
writer.writeheader()
|
|
writer.writerows(rows)
|
|
|
|
|
|
def export_numpy(path: str | Path, **arrays: np.ndarray) -> None:
|
|
np.savez(Path(path), **arrays)
|
|
|