From 001bb8c45a8fc14b37d53955ecbff722c57e1fff Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 5 Nov 2026 16:40:52 +0000 Subject: [PATCH] fix: fits_fit creates its output directory (#119) With PYAUTO_FAST_PLOTS=1, subplot_save returns before creating the fit's image subfolder, so fits_fit wrote fit.fits into a directory that did not exist and every model fit with visualization crashed with FileNotFoundError. Both FITS writers (dataset_1d and charge_injection) now create the folder first. Co-Authored-By: Claude Opus 5.5 --- autocti/charge_injection/plot/fit_ci_plots.py | 3 +++ autocti/dataset_1d/plot/fit_plots.py | 3 +++ .../charge_injection/plot/test_fit_ci_plots.py | 12 ++++++++++++ test_autocti/dataset_1d/plot/test_fit_plots.py | 12 ++++++++++++ 4 files changed, 30 insertions(+) diff --git a/autocti/charge_injection/plot/fit_ci_plots.py b/autocti/charge_injection/plot/fit_ci_plots.py index 91725797..567482f3 100644 --- a/autocti/charge_injection/plot/fit_ci_plots.py +++ b/autocti/charge_injection/plot/fit_ci_plots.py @@ -332,4 +332,7 @@ def fits_fit(fit: FitImagingCI, output_path): ], header_dict=fit.dataset.mask.header_dict, ) + # The fit subfolder may not exist yet: with PYAUTO_FAST_PLOTS=1 no figure is + # saved before this, and saving a figure is what used to create it. + Path(output_path).mkdir(parents=True, exist_ok=True) hdu_list.writeto(Path(output_path) / "fit.fits", overwrite=True) diff --git a/autocti/dataset_1d/plot/fit_plots.py b/autocti/dataset_1d/plot/fit_plots.py index 9eb00552..5155f669 100644 --- a/autocti/dataset_1d/plot/fit_plots.py +++ b/autocti/dataset_1d/plot/fit_plots.py @@ -213,4 +213,7 @@ def fits_fit(fit: FitDataset1D, output_path): ], header_dict=fit.dataset.mask.header_dict, ) + # The fit subfolder may not exist yet: with PYAUTO_FAST_PLOTS=1 no figure is + # saved before this, and saving a figure is what used to create it. + Path(output_path).mkdir(parents=True, exist_ok=True) hdu_list.writeto(Path(output_path) / "fit.fits", overwrite=True) diff --git a/test_autocti/charge_injection/plot/test_fit_ci_plots.py b/test_autocti/charge_injection/plot/test_fit_ci_plots.py index fcc24d6d..eebb9922 100644 --- a/test_autocti/charge_injection/plot/test_fit_ci_plots.py +++ b/test_autocti/charge_injection/plot/test_fit_ci_plots.py @@ -90,3 +90,15 @@ def test__fits_fit(fit_ci_7x7, tmp_path): aplt.fits_fit_ci(fit=fit_ci_7x7, output_path=tmp_path) assert (Path(tmp_path) / "fit.fits").exists() + + +def test__fits_fit__creates_missing_output_directory(fit_ci_7x7, tmp_path): + # Under PYAUTO_FAST_PLOTS=1 no figure is saved first, so nothing else has + # created the fit's image subfolder when the FITS file is written. + from autocti.charge_injection.plot.fit_ci_plots import fits_fit + + output_path = tmp_path / "image" / "fit_dataset" + + fits_fit(fit=fit_ci_7x7, output_path=output_path) + + assert (output_path / "fit.fits").is_file() diff --git a/test_autocti/dataset_1d/plot/test_fit_plots.py b/test_autocti/dataset_1d/plot/test_fit_plots.py index e647783f..ab749a91 100644 --- a/test_autocti/dataset_1d/plot/test_fit_plots.py +++ b/test_autocti/dataset_1d/plot/test_fit_plots.py @@ -63,3 +63,15 @@ def test__fits_fit(fit_1d_7, plot_path, tmp_path): aplt.fits_fit_dataset_1d(fit=fit_1d_7, output_path=tmp_path) assert (Path(tmp_path) / "fit.fits").exists() + + +def test__fits_fit__creates_missing_output_directory(fit_1d_7, tmp_path): + # Under PYAUTO_FAST_PLOTS=1 no figure is saved first, so nothing else has + # created the fit's image subfolder when the FITS file is written. + from autocti.dataset_1d.plot.fit_plots import fits_fit + + output_path = tmp_path / "image" / "fit_dataset" + + fits_fit(fit=fit_1d_7, output_path=output_path) + + assert (output_path / "fit.fits").is_file()