Determine output directory
Use run_python to build a unique timestamped output directory. First try plots/ relative to the current working directory; fall back to $MATCLAW_WORKSPACE/plots/ if cwd is not writable:
import os, datetime, random
tag = datetime.datetime.now().strftime("%Y%m%d%H%M%S") + f"_{random.randint(100000,999999)}"
cwd_plots = os.path.join(os.getcwd(), "plots", tag)
try:
os.makedirs(cwd_plots, exist_ok=True)
output_dir = cwd_plots
except OSError:
ws_root = os.environ.get("MATCLAW_WORKSPACE") or os.path.expanduser("~/.workspace")
output_dir = os.path.join(ws_root, "plots", tag)
os.makedirs(output_dir, exist_ok=True)
print(output_dir)
Capture the printed path — use it as OUTPUT_DIR in all subsequent steps.