Repository: rhayes777/PyAutoFit
Pull request #1129 (2025-04-13 18:28:42)

fix unit tests by restoring .fits files and using MODEL_IMAGe
The following PR add a .csv visualization file for irregular source reconstructions:

https://github.com/Jammy2211/PyAutoLens/compare/feature/fits_reconstruction?expand=1

This PR adds a method to `AggregateFITS` which loads these .csv files as lists of dictionaries.

Its a bit odd it being in `AggregateFITS`, but doesn't fit with the usage of other `Aggregate` objects and I dont want to make a new one. There will be more .csv visuals in the future so I woill think about how to approach this going forward.


Patch:
diff --git a/autofit/aggregator/summary/aggregate_fits.py b/autofit/aggregator/summary/aggregate_fits.py
index 3f295f799..e7d4ea209 100644
--- a/autofit/aggregator/summary/aggregate_fits.py
+++ b/autofit/aggregator/summary/aggregate_fits.py
@@ -1,7 +1,8 @@
 import re
 from enum import Enum
-from typing import List, Union
+from typing import Dict, List, Union
 
+from astropy.table import Table
 from astropy.io import fits
 from pathlib import Path
 
@@ -27,7 +28,7 @@ class FITSFit(Enum):
     The HDUs that can be extracted from the fit.fits file.
     """
 
-    ModelImage = "MODEL_IMAGE"
+    ModelData = "MODEL_IMAGE"
     ResidualMap = "RESIDUAL_MAP"
     NormalizedResidualMap = "NORMALIZED_RESIDUAL_MAP"
     ChiSquaredMap = "CHI_SQUARED_MAP"
@@ -100,6 +101,28 @@ class AggregateFITS:
 
         return fits.HDUList(output)
 
+    def extract_csv(self, filename : str) -> List[Dict]:
+        """
+        Extract .csv files which store imaging results that are typically on irregular grids and thus don't suit
+        a .fits file.
+
+        Return the result as a list of Dicts corresponding to the fits in the aggregator.
+
+        Parameters
+        ----------
+        filename
+            The name of the .csv file to extract.
+
+        Returns
+        -------
+        The extracted HDUs.
+        """
+        output = []
+        for result in self.aggregator:
+            output.append(Table.read(result.value(filename), format="fits"))
+
+        return output
+
     def output_to_folder(
         self,
         folder: Path,
diff --git a/autofit/aggregator/summary/aggregate_images.py b/autofit/aggregator/summary/aggregate_images.py
index 8ddf411e8..ee233699f 100644
--- a/autofit/aggregator/summary/aggregate_images.py
+++ b/autofit/aggregator/summary/aggregate_images.py
@@ -34,10 +34,10 @@ class SubplotFit(Enum):
     Data = (0, 0)
     DataSourceScaled = (1, 0)
     SignalToNoiseMap = (2, 0)
-    ModelImage = (3, 0)
-    LensLightModelImage = (0, 1)
+    ModelData = (3, 0)
+    LensLightModelData = (0, 1)
     LensLightSubtractedImage = (1, 1)
-    SourceModelImage = (2, 1)
+    SourceModelData = (2, 1)
     SourcePlaneZoomed = (3, 1)
     NormalizedResidualMap = (0, 2)
     NormalizedResidualMapOneSigma = (1, 2)


Test patch:
diff --git a/test_autofit/aggregator/summary_files/test_aggregate_fits.py b/test_autofit/aggregator/summary_files/test_aggregate_fits.py
index cab751d86..7416c6c87 100644
--- a/test_autofit/aggregator/summary_files/test_aggregate_fits.py
+++ b/test_autofit/aggregator/summary_files/test_aggregate_fits.py
@@ -12,7 +12,7 @@ def make_summary(aggregator):
 def test_aggregate(summary):
     result = summary.extract_fits(
         [
-            af.FITSFit.ModelImage,
+            af.FITSFit.ModelData,
             af.FITSFit.ResidualMap,
         ],
     )
@@ -25,7 +25,7 @@ def test_output_to_file(summary, output_directory):
         folder,
         name="id",
         hdus=[
-            af.FITSFit.ModelImage,
+            af.FITSFit.ModelData,
             af.FITSFit.ResidualMap,
         ],
     )
@@ -37,7 +37,7 @@ def test_list_of_names(summary, output_directory):
         output_directory,
         ["one", "two"],
         [
-            af.FITSFit.ModelImage,
+            af.FITSFit.ModelData,
             af.FITSFit.ResidualMap,
         ],
     )
diff --git a/test_autofit/aggregator/summary_files/test_aggregate_images.py b/test_autofit/aggregator/summary_files/test_aggregate_images.py
index c3e4ceff5..d4334408b 100644
--- a/test_autofit/aggregator/summary_files/test_aggregate_images.py
+++ b/test_autofit/aggregator/summary_files/test_aggregate_images.py
@@ -49,7 +49,7 @@ def test_longer(aggregate):
         [
             SubplotFit.NormalizedResidualMap,
             SubplotFit.SourcePlaneNoZoom,
-            SubplotFit.SourceModelImage,
+            SubplotFit.SourceModelData,
         ],
     )
 
@@ -61,7 +61,7 @@ def test_subplot_width(aggregate):
         [
             SubplotFit.NormalizedResidualMap,
             SubplotFit.SourcePlaneNoZoom,
-            SubplotFit.SourceModelImage,
+            SubplotFit.SourceModelData,
         ],
         subplot_width=2,
     )
@@ -76,7 +76,7 @@ def test_output_to_folder(aggregate, output_directory):
         [
             SubplotFit.Data,
             SubplotFit.SourcePlaneZoomed,
-            SubplotFit.SourceModelImage,
+            SubplotFit.SourceModelData,
         ],
     )
     assert list(Path(output_directory).glob("*.png"))
@@ -89,7 +89,7 @@ def test_list_of_names(aggregate, output_directory):
         [
             SubplotFit.Data,
             SubplotFit.SourcePlaneZoomed,
-            SubplotFit.SourceModelImage,
+            SubplotFit.SourceModelData,
         ],
     )
     assert set([path.name for path in Path(output_directory).glob("*.png")]) == set([
@@ -109,7 +109,7 @@ def test_output_to_folder_name(
         [
             SubplotFit.Data,
             SubplotFit.SourcePlaneZoomed,
-            SubplotFit.SourceModelImage,
+            SubplotFit.SourceModelData,
         ],
     )
 
@@ -128,7 +128,7 @@ def test_custom_images(
         [
             SubplotFit.Data,
             SubplotFit.SourcePlaneZoomed,
-            SubplotFit.SourceModelImage,
+            SubplotFit.SourceModelData,
             images,
         ]
     )
@@ -144,7 +144,7 @@ def test_custom_function(aggregate):
         [
             SubplotFit.Data,
             SubplotFit.SourcePlaneZoomed,
-            SubplotFit.SourceModelImage,
+            SubplotFit.SourceModelData,
             make_image,
         ]
     )