#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Timestamp: "2025-10-29 07:21:17 (ywatanabe)"
# File: /home/ywatanabe/proj/scitex-io/src/scitex_io/_save.py
# ----------------------------------------
from __future__ import annotations
import os
__FILE__ = "./src/scitex_io/_save.py"
__DIR__ = os.path.dirname(__FILE__)
# ----------------------------------------
__FILE__ = __file__
"""
1. Functionality:
- Provides utilities for saving various data types to different file formats.
2. Input:
- Objects to be saved (e.g., NumPy arrays, PyTorch tensors, Pandas DataFrames, etc.)
- File path or name where the object should be saved
3. Output:
- Saved files in various formats (e.g., CSV, NPY, PKL, JOBLIB, PNG, HTML, TIFF, MP4, YAML, JSON, HDF5, PTH, MAT, CBM)
4. Prerequisites:
- Python 3.x
- Required libraries: numpy, pandas, torch, matplotlib, plotly, h5py, joblib, PIL, ruamel.yaml
"""
"""Imports"""
import inspect
import os as _os
from pathlib import Path
from typing import Any, Union
from scitex_logging import getLogger as _getLogger
from ._image_csv_handler import handle_image_with_csv # noqa: F401
from ._path_modules._symlink import _symlink, _symlink_to, sh # noqa: F401
from ._registry import get_saver # noqa: F401
from ._save_dispatch import ( # noqa: F401 — re-exported dispatch layer
_IMAGE_EXTS,
_execute_save_handler,
_preflight_save_handler,
_resolve_save_handler,
_save,
)
from ._utils import clean, clean_path, color_text, getsize, readable_bytes
logger = _getLogger(__name__)
# Module-level latch for the once-per-process notebook-path warning.
_NOTEBOOK_PATH_WARNED = False
def _warn_notebook_path_unresolved_once(
fallback_sdir: str, reason: Union[str, None] = None
) -> None:
"""Emit a one-time WARNING when the kernel's notebook cannot be found.
Triggered when ``scitex_io.save`` runs inside a Jupyter kernel but
:func:`scitex_io._notebook.resolve_notebook` finds no notebook (see its
docstring for the sources tried). The relative save then falls back to
``<cwd>/notebook_out/`` -- documented, but not the canonical
``<notebook_dir>/<stem>_out/`` -- so say so, say why, and say how to fix it.
Silenced for the rest of the process by:
- ``SCITEX_IO_QUIET_NOTEBOOK_WARN=1`` env var, OR
- the latch (only the first call ever emits).
"""
global _NOTEBOOK_PATH_WARNED
if _NOTEBOOK_PATH_WARNED:
return
if _os.environ.get("SCITEX_IO_QUIET_NOTEBOOK_WARN"):
_NOTEBOOK_PATH_WARNED = True
return
_NOTEBOOK_PATH_WARNED = True
msg = (
"scitex_io: notebook path could not be auto-detected; saving to "
f"{fallback_sdir!r} instead of <notebook_dir>/<stem>_out/.\n"
f" Sources tried: {reason or 'none'}.\n"
" Canonical convention: <dir>/<stem>.ipynb -> sio.save(obj, 'name.ext') "
"-> <dir>/<stem>_out/name.ext\n"
" Fix by setting SCITEX_NOTEBOOK_PATH for the kernel, e.g.:\n"
" SCITEX_NOTEBOOK_PATH=demo.ipynb jupyter nbconvert --execute --inplace demo.ipynb\n"
" Silence this hint with SCITEX_IO_QUIET_NOTEBOOK_WARN=1.\n"
" Or pass an absolute path to bypass routing: sio.save(obj, '/abs/path.ext').\n"
" (This message prints at most once per process.)"
)
logger.warning(msg)
[docs]
def save(
obj: Any,
specified_path: Union[str, Path],
makedirs: bool = True,
verbose: bool = True,
symlink_from_cwd: bool = False,
symlink_to: Union[str, Path] = None,
dry_run: bool = False,
no_csv: bool = False,
use_caller_path: bool = True,
env_detector=None,
**kwargs,
) -> None:
"""Save ``obj`` by extension; ``specified_path`` is caller-anchored.
The file format is selected from ``specified_path``'s extension via
the plugin registry — `.csv`, `.npy`, `.pkl`, `.yaml`, `.png`,
`.h5`, ... 30+ formats are built in; custom extensions can be added
with ``register_saver``.
Path resolution rules (when ``specified_path`` is relative):
- Called from a script ``/path/to/analysis.py`` →
``/path/to/analysis_out/<specified_path>``.
- Called from a notebook ``/path/to/exp.ipynb`` →
``/path/to/exp_out/<specified_path>``.
- Called from ``python -i`` / IPython / interactive REPL →
``$SCITEX_DIR/io/runtime/cache/<specified_path>`` (default
``~/.scitex/io/runtime/cache/``). Honours the canonical scitex
local-state convention; see scitex-dev skills/general
``01_ecosystem_06_local-state-directories.md``.
- Absolute path → used as-is, no routing.
Intermediate directories are created automatically — callers do
not need ``os.makedirs()`` / ``Path.mkdir()``.
Parameters
----------
obj : Any
The object to be saved.
specified_path : Union[str, Path]
The filename or relative path under which to save ``obj``. May
contain subdirectories (``"sub/dir/file.csv"``); intermediates
are auto-created. Absolute paths bypass routing.
makedirs : bool, optional
Create parent directories on demand. Default ``True``.
verbose : bool, optional
Print a one-line success message. Default ``True``.
symlink_from_cwd : bool, optional
Drop a symlink at ``./<specified_path>`` pointing into the
auto-routed location. Default ``False``.
symlink_to : Union[str, Path], optional
Plant a symlink at this custom path pointing to the saved file.
dry_run : bool, optional
Print the resolved path without writing. Default ``False``.
no_csv : bool, optional
Skip the auto-CSV sidecar for figure saves. Default ``False``.
use_caller_path : bool, optional
Resolve the anchor from the calling script, not the immediate
caller — needed when ``save`` is wrapped by a library. Default
``False``.
track : bool, optional (keyword)
Forwarded to post-save observers only; ``False`` asks a provenance
observer (scitex-clew) not to record this file. Default ``True``.
derived_from : path or list of paths / loaded objects, optional (keyword)
Declare the inputs this output was computed from. Each item is a
file path or an object returned by :func:`scitex_io.load` (resolved
to the file it was loaded from). Forwarded to post-save observers
as a list of realpaths under ``kwargs["derived_from"]`` for the
primary file and every sidecar; scitex-clew uses it for its level-2
minimal back-trace. Omitted = undeclared (never inferred). Raises
before writing if an item cannot be resolved exactly.
**kwargs
Passed through to the per-format handler.
Returns
-------
Path or None
Path to saved file on success, ``None``/``False`` on error.
"""
# Bound before ``try`` so the failure report below can name it even
# when the error is raised before path resolution.
spath = None
try:
# ``track`` is a scitex-io / observer concept, NOT a format-handler
# argument. Pop it before any dispatch so it never reaches the
# per-format handlers (_save_yaml/_save_pickle take no extra
# kwargs and would raise on an unexpected ``track=``). It is handed
# back to the post-save hook below so clew can gate tracking on it.
track = kwargs.pop("track", True)
# ``derived_from`` is likewise an observer concept: the declared
# inputs this output was computed from (paths or objects returned by
# ``load``). Resolved to existing realpaths BEFORE anything is
# written, so a bad declaration fails the save instead of recording a
# wrong edge. ``None`` = undeclared; nothing is ever inferred.
from ._derived_from import resolve_derived_from
derived_from = resolve_derived_from(kwargs.pop("derived_from", None))
if isinstance(specified_path, Path):
specified_path = str(specified_path)
########################################
# DO NOT MODIFY THIS SECTION
########################################
spath, sfname = None, None
_cache_dir_to_create = None
# f-expression handling - safely parse f-strings
if specified_path.startswith('f"') or specified_path.startswith("f'"):
path_content = specified_path[2:-1]
frame = inspect.currentframe().f_back
try:
import re
variables = re.findall(r"\{([^}]+)\}", path_content)
format_dict = {}
for var in variables:
if re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", var):
if var in frame.f_locals:
format_dict[var] = frame.f_locals[var]
elif var in frame.f_globals:
format_dict[var] = frame.f_globals[var]
else:
raise ValueError(f"Invalid variable name in f-string: {var}")
specified_path = path_content.format(**format_dict)
finally:
del frame
if specified_path.startswith("/"):
spath = specified_path
else:
from ._utils import detect_environment
if env_detector is None:
env_detector = detect_environment
env_type = env_detector()
if env_type == "jupyter":
# Same resolution rule as scitex-session (see _notebook.py),
# so save() and @session agree on <notebook>_out/.
from ._notebook import resolve_notebook
resolved = resolve_notebook()
notebook_path = resolved.get("path")
if notebook_path:
notebook_base = _os.path.splitext(
_os.path.basename(notebook_path)
)[0]
sdir = _os.path.join(
_os.path.dirname(notebook_path), f"{notebook_base}_out"
)
else:
sdir = _os.path.join(_os.getcwd(), "notebook_out")
_warn_notebook_path_unresolved_once(
sdir, resolved.get("reason")
)
spath = _os.path.join(sdir, specified_path)
elif env_type == "script":
# use_caller_path defaults to True so a save under
# `@stx.session` (or any other scitex wrapper) walks past
# the scitex frames in inspect.stack() to the real user
# script. Direct (unwrapped) callers also work: no scitex
# frames are present so the walk falls through to
# frame[1] = the user script.
#
# We detect scitex frames by MODULE NAME (any
# ``scitex_*`` package), NOT by directory prefix —
# because scitex_io and scitex_dev may live in different
# site-packages locations (PYTHONPATH overlay, editable
# install of one but not the other, etc.) and a parent-
# path comparison would silently miss the wrapper frame.
# 2026-06-13 operator dogfood: a script saved via
# ``stx.io.save`` landed inside
# ``scitex_dev/_core/decorators_out/`` because the
# parent-path scan didn't match scitex_dev's path → the
# walker treated the wrapper frame as the user script.
if use_caller_path:
script_path = None
for frame_info in inspect.stack()[1:]:
mod_name = frame_info.frame.f_globals.get("__name__", "") or ""
if not (
mod_name == "scitex"
or mod_name.startswith("scitex.")
or mod_name.startswith("scitex_")
):
script_path = _os.path.abspath(frame_info.filename)
break
if script_path is None:
script_path = inspect.stack()[1].filename
else:
script_path = inspect.stack()[1].filename
sdir = clean_path(_os.path.splitext(script_path)[0] + "_out")
spath = _os.path.join(sdir, specified_path)
elif env_type in ("ipython", "interactive"):
# Interactive sessions (IPython terminal REPL / bare
# `python` / `python -i` / `python -c`) have no script
# path to anchor `_out/` to, so route writes into the
# canonical scitex local-state cache:
# $SCITEX_DIR/io/runtime/cache/ (default ~/.scitex)
# See scitex-dev skills/general/
# 01_ecosystem_06_local-state-directories.md
_scitex_dir = _os.environ.get(
"SCITEX_DIR",
_os.path.join(_os.path.expanduser("~"), ".scitex"),
)
sdir = _os.path.join(_scitex_dir, "io", "runtime", "cache")
_cache_dir_to_create = sdir
spath = _os.path.join(sdir, specified_path)
else:
# Fail fast, fail loud: NO silent `<cwd>/output/` fallback.
# An unrecognised env_type means detect_environment() (or
# a caller-supplied env_detector) returned something
# outside the documented vocabulary; guessing an output
# path here is exactly the quiet-wrong-place bug this
# branch used to cause. Operator directive 2026-06-13:
# "no else pattern accepted" — raise with a clear
# diagnostic + the documented vocabulary so the caller
# can fix detect_environment / their env_detector
# contract.
raise ValueError(
"scitex.io.save: unrecognised execution environment "
f"{env_type!r}. Expected one of "
"'jupyter' (Jupyter kernel), "
"'ipython' (IPython terminal REPL), "
"'script' (a `.py` run), or "
"'interactive' (bare `python` / `-i` / `-c`). "
"See scitex_io._utils.detect_environment for the "
"canonical contract. Refusing to guess an output "
"directory."
)
spath_final = clean(spath)
########################################
# A dry run resolves the path only: no handler imports, directory
# creation, anchor cleanup or destination removal.
if dry_run:
try:
rel_path = _os.path.relpath(spath, _os.getcwd())
except ValueError:
rel_path = spath
if verbose:
logger.success(
color_text(f"(dry run) Saved to: ./{rel_path}", "yellow")
)
return
# Select once, using the same registry/compound-suffix rules as direct
# dispatch. Refuse missing builtin capabilities before touching paths.
handler = _resolve_save_handler(spath_final)
_preflight_save_handler(handler, spath_final, kwargs)
if _cache_dir_to_create is not None:
# Retain the historical interactive cache even with makedirs=False.
_os.makedirs(_cache_dir_to_create, exist_ok=True)
spath_cwd = _os.getcwd() + "/" + specified_path
# scitex-io#55: spath_cwd is the LITERAL location of the cwd
# anchor symlink we're about to (re)create — it must NOT be
# collapsed to whatever the existing symlink currently points
# to. ``clean()`` did ``Path.resolve()`` here, which on a second
# save() call followed call-1's symlink, silently shifted the
# anchor onto the target file, and made the subsequent rm
# delete the just-written artefact. Use ``normpath`` so the
# anchor stays where the caller asked for it.
spath_cwd = _os.path.normpath(spath_cwd)
# Defensive cleanup: if a previous (pre-fix) run left a broken
# or self-looping symlink at the anchor, remove it now so the
# downstream ``rm -f`` / ``ln -sfr`` aren't forced to interact
# with a path that ``Path.resolve()`` would refuse to follow.
if _os.path.islink(spath_cwd):
try:
_os.stat(spath_cwd) # follow → raises on loop/broken
except OSError:
_os.unlink(spath_cwd)
should_skip_deletion = spath_final.endswith(".csv") or (
(spath_final.endswith(".hdf5") or spath_final.endswith(".h5"))
and "key" in kwargs
)
if not should_skip_deletion:
for path in [spath_final, spath_cwd]:
sh(["rm", "-f", f"{path}"], verbose=False)
if makedirs:
_os.makedirs(_os.path.dirname(spath_final), exist_ok=True)
# Capture EVERY file this call writes (primary + sidecars + links)
# so each one reaches the post-save hook — see _observers/_capture.
from ._observers._capture import capture_writes
with capture_writes(spath_final) as capture:
_execute_save_handler(
handler,
obj,
spath_final,
verbose=verbose,
symlink_from_cwd=symlink_from_cwd,
symlink_to=symlink_to,
dry_run=dry_run,
no_csv=no_csv,
**kwargs,
)
_symlink(
spath, spath_cwd, symlink_from_cwd, verbose, spath_final=spath_final
)
_symlink_to(spath_final, symlink_to, verbose)
saved_path = Path(spath)
# Evict any stale load-cache entry for the path we just wrote, so a
# subsequent load() re-reads from disk. is_cache_valid keys on
# (mtime, size) only, so a same-size overwrite within one mtime tick
# would otherwise return the previous value. Invalidate both the
# specified path and the resolved final path. Best-effort; never let
# a cache concern fail the save.
try:
from ._loading._load_cache import invalidate as _invalidate_cache
_invalidate_cache(str(saved_path))
if str(spath_final) != str(saved_path):
_invalidate_cache(str(spath_final))
except Exception: # pragma: no cover — cache eviction is best-effort
pass
# Notify any registered observers (clew, audit, …) once per written
# file. Observers SELF-REGISTER on their own import — scitex_io never
# names them. A raising hook is counted + WARNed (strict mode:
# raises ProvenanceError) — never silently swallowed.
from ._observers import fire_post_save_capture
# Re-attach ``track`` (and a ``derived_from`` declaration, when one
# was made) so observers (clew) can honour them. They were popped
# above to keep them out of the format handlers.
hook_kwargs = {**kwargs, "track": track}
if derived_from is not None:
hook_kwargs["derived_from"] = derived_from
fire_post_save_capture(capture, obj, hook_kwargs)
return saved_path
except Exception as e:
# Fail loud, fail early. Previously this branch logged the error
# and returned False, which let callers proceed as though the
# file had been written — downstream code then exploded much
# later (or, worse, kept running with stale/missing data and
# silently produced wrong results).
#
# Concrete real-world incident (paper-scitex-clew TRANSLATION
# _TEMPLATE rollout, 2026-06-01): on a WSL2 Ubuntu 22.04
# minimal container without libglib2 installed, every
# scitex_io.save() call raised
# `libgthread-2.0.so.0: cannot open shared object file` deep in
# an optional handler import. The old `return False` swallowed
# the ImportError, the agent's stage 1 logged "saved metrics.csv
# with 8 rows", and stage 2 then crashed with FileNotFoundError
# — three stages later than the actual fault. With the raise
# below the ImportError surfaces at the save call site, with
# the spath / specified_path debug trail intact.
logger.error(
f"scitex_io.save failed for {specified_path!r}: {e}\n"
f" Initial script_path = {inspect.stack()[1].filename}\n"
f" Final spath = {spath}\n"
f" specified_path type = {type(specified_path)}"
)
raise
# EOF