Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions autofit/aggregator/summary/aggregate_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]):
"""
Expand Down Expand Up @@ -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)]

Expand Down Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions test_autofit/aggregator/summary_files/test_aggregate_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name>.json` and `image/<name>.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")
Loading