From a194a7dc522eddd2bb1912be6f7f2b1b4273fda5 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Wed, 16 Sep 2026 23:57:30 +0100 Subject: [PATCH] fix: AggregateFITS resolves FITS sources through result.fits, not value() SearchOutput.value searches JSON outputs before FITS outputs. A search that writes both files/.json and image/.fits (every PyAutoLens fit writes files/tracer.json beside image/tracer.fits) therefore made AggregateFITS._hdus receive the JSON object and fail with "'Tracer' object has no attribute 'index_of'", so al.agg.fits_tracer was unusable through AggregateFITS. Found while collecting per-lens mass maps for the Euclid DR1 catalogue (euclid_strong_lens_modeling_pipeline#80). New module-level fits_source(result, name) looks the FITS output up by name in result.fits and raises FileNotFoundError when absent; _hdus and extract_csv use it. SearchOutput.value keeps its JSON-first order. Regression test copies the aggregate_summary fixture, drops a files/fit.json beside image/fit.fits and asserts extraction still returns the HDUs (red on main: AttributeError 'dict' object has no attribute 'index_of'). Co-Authored-By: Claude Fable 5.1 --- autofit/aggregator/summary/aggregate_fits.py | 30 ++++++++++++++- .../summary_files/test_aggregate_fits.py | 38 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/autofit/aggregator/summary/aggregate_fits.py b/autofit/aggregator/summary/aggregate_fits.py index 56412aa4d..852649701 100644 --- a/autofit/aggregator/summary/aggregate_fits.py +++ b/autofit/aggregator/summary/aggregate_fits.py @@ -20,6 +20,32 @@ def subplot_filename(subplot: Enum) -> str: .lstrip("_") ) +def fits_source(result: SearchOutput, name: str): + """ + The opened HDUList of the FITS output called `name` for a given search. + + Resolved through `result.fits` rather than `result.value`, because `value` searches + JSON outputs first and a same-named JSON (e.g. `files/tracer.json` beside + `image/tracer.fits`) therefore shadows the FITS file. + + Parameters + ---------- + result + The search output. + name + The name of the fits file, without a suffix. + + Returns + ------- + The opened fits file. + """ + for output in result.fits: + if output.name == name: + return output.value + + raise FileNotFoundError(f"No FITS output named '{name}' in {result.directory}") + + class AggregateFITS: def __init__(self, aggregator: Union[Aggregator, List[SearchOutput]]): """ @@ -69,7 +95,7 @@ def _hdus( for hdu in hdus: source_name = subplot_filename(hdu) if source_name not in sources: - sources[source_name] = result.value(source_name) + sources[source_name] = fits_source(result, source_name) source = sources[source_name] source_hdu = source[source.index_of(hdu.value)] @@ -133,7 +159,7 @@ def extract_csv(self, filename: str) -> List[Dict]: output = [] for result in self.aggregator: - source = result.value(filename) + source = fits_source(result, filename) try: output.append(Table.read(source, format="fits").copy()) finally: diff --git a/test_autofit/aggregator/summary_files/test_aggregate_fits.py b/test_autofit/aggregator/summary_files/test_aggregate_fits.py index b3fccf8b0..b260aa563 100644 --- a/test_autofit/aggregator/summary_files/test_aggregate_fits.py +++ b/test_autofit/aggregator/summary_files/test_aggregate_fits.py @@ -87,3 +87,41 @@ def test_extract_fits_closes_files(summary): after = len(os.listdir("/proc/self/fd")) assert after <= before + 1 + + +def test_json_does_not_shadow_fits(tmp_path): + """ + A fit writes both `files/.json` and `image/.fits` (e.g. a lens fit's + `tracer`). `SearchOutput.value` searches JSONs first, so resolving the FITS source + through it returned the JSON object instead of the HDUList. + """ + import json + import shutil + + directory = tmp_path / "aggregate_summary" + shutil.copytree( + Path(__file__).parent / "aggregate_summary", + directory, + ) + for fit_directory in directory.glob("fit_*"): + with open(fit_directory / "files" / "fit.json", "w+") as f: + json.dump({"shadow": "not the fits file"}, f) + + from autofit.aggregator import Aggregator + + summary = af.AggregateFITS(Aggregator.from_directory(directory)) + + result = summary.extract_fits( + [ + FITSFit.ModelData, + FITSFit.ResidualMap, + ], + ) + assert len(result) == 5 + + +def test_fits_source_missing(aggregator): + from autofit.aggregator.summary.aggregate_fits import fits_source + + with pytest.raises(FileNotFoundError): + fits_source(list(aggregator)[0], "not_a_fits_file")