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()