|
22 | 22 | from datetime import UTC |
23 | 23 | from datetime import datetime |
24 | 24 | from pathlib import Path |
| 25 | +from typing import TYPE_CHECKING |
25 | 26 | from typing import Any |
26 | 27 |
|
27 | 28 | import numpy as np |
28 | 29 | import scipy |
29 | 30 |
|
| 31 | +if TYPE_CHECKING: |
| 32 | + import pandas as pd |
| 33 | + |
30 | 34 | _PACKAGE_ROOT = Path(__file__).resolve().parent |
31 | 35 |
|
32 | 36 |
|
@@ -269,6 +273,129 @@ def save_json( |
269 | 273 | return path |
270 | 274 |
|
271 | 275 |
|
| 276 | +def _metadata_json( |
| 277 | + prov: Provenance, params: Mapping[str, Any] | None |
| 278 | +) -> tuple[str, str]: |
| 279 | + """Serialize the provenance record and params as JSON strings.""" |
| 280 | + return ( |
| 281 | + json.dumps(asdict(prov), default=_json_default), |
| 282 | + json.dumps(dict(params or {}), default=_json_default), |
| 283 | + ) |
| 284 | + |
| 285 | + |
| 286 | +def save_npz( |
| 287 | + arrays: Mapping[str, np.ndarray], |
| 288 | + directory: Path | str, |
| 289 | + name: str, |
| 290 | + *, |
| 291 | + params: Mapping[str, Any] | None = None, |
| 292 | + run_label: str | None = None, |
| 293 | + seed: int | None = None, |
| 294 | + note: str | None = None, |
| 295 | +) -> Path: |
| 296 | + """Write ``arrays`` to a self-describing, non-clobbering ``.npz`` file. |
| 297 | +
|
| 298 | + The arrays are stored with :func:`numpy.savez_compressed`, plus two |
| 299 | + reserved entries ``_provenance`` and ``_params`` holding JSON strings |
| 300 | + (read back with :func:`read_metadata`). Array names beginning with |
| 301 | + ``_`` raise :class:`ValueError`. Filename, versioning, and seed |
| 302 | + resolution follow :func:`save_json`. |
| 303 | +
|
| 304 | + Returns the written path. |
| 305 | + """ |
| 306 | + reserved = [key for key in arrays if key.startswith("_")] |
| 307 | + if reserved: |
| 308 | + raise ValueError(f"array names beginning with '_' are reserved: {reserved}") |
| 309 | + prov = _capture_metadata(params, seed, note) |
| 310 | + prov_json, params_json = _metadata_json(prov, params) |
| 311 | + path = unique_path(directory, format_stem(name, params, run_label), ".npz") |
| 312 | + entries = { |
| 313 | + **arrays, |
| 314 | + "_provenance": np.array(prov_json), |
| 315 | + "_params": np.array(params_json), |
| 316 | + } |
| 317 | + np.savez_compressed(path, allow_pickle=False, **entries) |
| 318 | + return path |
| 319 | + |
| 320 | + |
| 321 | +def save_dataframe( |
| 322 | + df: pd.DataFrame, |
| 323 | + directory: Path | str, |
| 324 | + name: str, |
| 325 | + *, |
| 326 | + params: Mapping[str, Any] | None = None, |
| 327 | + run_label: str | None = None, |
| 328 | + seed: int | None = None, |
| 329 | + note: str | None = None, |
| 330 | +) -> Path: |
| 331 | + """Write ``df`` to a self-describing, non-clobbering parquet file. |
| 332 | +
|
| 333 | + The frame is written with its index preserved; ``pyphi_provenance`` |
| 334 | + and ``pyphi_params`` entries (JSON strings) are merged into the |
| 335 | + parquet schema metadata, so :func:`pandas.read_parquet` reads the |
| 336 | + data normally and :func:`read_metadata` recovers the metadata. |
| 337 | + Filename, versioning, and seed resolution follow :func:`save_json`. |
| 338 | + DataFrame fidelity follows parquet semantics. |
| 339 | +
|
| 340 | + Returns the written path. |
| 341 | + """ |
| 342 | + import pyarrow as pa |
| 343 | + import pyarrow.parquet as pq |
| 344 | + |
| 345 | + prov = _capture_metadata(params, seed, note) |
| 346 | + prov_json, params_json = _metadata_json(prov, params) |
| 347 | + path = unique_path(directory, format_stem(name, params, run_label), ".parquet") |
| 348 | + table = pa.Table.from_pandas(df, preserve_index=True) |
| 349 | + metadata = dict(table.schema.metadata or {}) |
| 350 | + metadata[b"pyphi_provenance"] = prov_json.encode() |
| 351 | + metadata[b"pyphi_params"] = params_json.encode() |
| 352 | + pq.write_table(table.replace_schema_metadata(metadata), path) |
| 353 | + return path |
| 354 | + |
| 355 | + |
| 356 | +def read_metadata(path: Path | str) -> dict[str, Any]: |
| 357 | + """Read the provenance and params embedded in a writer's output file. |
| 358 | +
|
| 359 | + Dispatches on the file suffix (``.json``, ``.npz``, or ``.parquet``) |
| 360 | + and returns ``{"provenance": dict, "params": dict}``. A file without |
| 361 | + the expected metadata (not produced by :func:`save_json`, |
| 362 | + :func:`save_npz`, or :func:`save_dataframe`) raises |
| 363 | + :class:`ValueError`, as does an unrecognized suffix. |
| 364 | + """ |
| 365 | + path = Path(path) |
| 366 | + missing = ValueError(f"no pyphi provenance metadata in {path}") |
| 367 | + if path.suffix == ".json": |
| 368 | + document = json.loads(path.read_text()) |
| 369 | + try: |
| 370 | + return { |
| 371 | + "provenance": document["provenance"], |
| 372 | + "params": document["params"], |
| 373 | + } |
| 374 | + except (KeyError, TypeError): |
| 375 | + raise missing from None |
| 376 | + if path.suffix == ".npz": |
| 377 | + with np.load(path) as npz: |
| 378 | + try: |
| 379 | + return { |
| 380 | + "provenance": json.loads(str(npz["_provenance"][()])), |
| 381 | + "params": json.loads(str(npz["_params"][()])), |
| 382 | + } |
| 383 | + except KeyError: |
| 384 | + raise missing from None |
| 385 | + if path.suffix == ".parquet": |
| 386 | + import pyarrow.parquet as pq |
| 387 | + |
| 388 | + metadata = pq.read_schema(path).metadata or {} |
| 389 | + try: |
| 390 | + return { |
| 391 | + "provenance": json.loads(metadata[b"pyphi_provenance"]), |
| 392 | + "params": json.loads(metadata[b"pyphi_params"]), |
| 393 | + } |
| 394 | + except KeyError: |
| 395 | + raise missing from None |
| 396 | + raise ValueError(f"unrecognized suffix {path.suffix!r} for {path}") |
| 397 | + |
| 398 | + |
272 | 399 | def _set_provenance(result: Any, prov: Provenance) -> None: |
273 | 400 | """Assign ``prov`` to ``result.provenance``, working around frozen results.""" |
274 | 401 | try: |
@@ -319,7 +446,10 @@ def with_provenance(self, **fields: Any) -> HasProvenance: |
319 | 446 | "HasProvenance", |
320 | 447 | "Provenance", |
321 | 448 | "format_stem", |
| 449 | + "read_metadata", |
| 450 | + "save_dataframe", |
322 | 451 | "save_json", |
| 452 | + "save_npz", |
323 | 453 | "stamp_wall_time", |
324 | 454 | "unique_path", |
325 | 455 | ] |
0 commit comments