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
4 changes: 2 additions & 2 deletions autogalaxy/aggregator/subplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FITSFit(Enum):
The HDUs that can be extracted from the fit.fits file.
"""

model_image = "MODEL_IMAGE"
model_data = "MODEL_DATA"
residual_map = "RESIDUAL_MAP"
normalized_residual_map = "NORMALIZED_RESIDUAL_MAP"
chi_squared_map = "CHI_SQUARED_MAP"
Expand Down Expand Up @@ -38,7 +38,7 @@ class SubplotFit(Enum):

data = (0, 0)
signal_to_noise_map = (1, 0)
model_image = (2, 0)
model_data = (2, 0)
normalized_residual_map = (0, 1)
normalized_residual_map_one_sigma = (1, 1)
chi_squared_map = (2, 1)
32 changes: 28 additions & 4 deletions autogalaxy/analysis/plotter_interface.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import csv
import os
from pathlib import Path
from typing import List, Union
Expand All @@ -11,7 +12,6 @@
from autogalaxy.analysis.adapt_images.adapt_images import AdaptImages
from autogalaxy.galaxy.galaxy import Galaxy
from autogalaxy.galaxy.galaxies import Galaxies
from autogalaxy.galaxy.plot.galaxy_plotters import GalaxyPlotter
from autogalaxy.galaxy.plot.galaxies_plotters import GalaxiesPlotter
from autogalaxy.galaxy.plot.adapt_plotters import AdaptPlotter

Expand Down Expand Up @@ -182,7 +182,7 @@ def should_plot(name):
if should_plot("fits_galaxy_images"):
hdu_list = hdu_list_for_output_from(
values_list=[grid.mask.astype("float")]
+ [galaxy.image_2d_from(grid=grid) for galaxy in galaxies],
+ [galaxy.image_2d_from(grid=grid).native for galaxy in galaxies],
ext_name_list=["mask"] + [f"galaxy_{i}" for i in range(len(galaxies))],
header_dict=grid.mask.header_dict,
)
Expand Down Expand Up @@ -221,8 +221,32 @@ def should_plot(name):
if should_plot("subplot_inversion"):
mapper_list = inversion.cls_list_from(cls=aa.AbstractMapper)

for mapper_index in range(len(mapper_list)):
inversion_plotter.subplot_of_mapper(mapper_index=mapper_index)
for i in range(len(mapper_list)):
suffix = "" if len(mapper_list) == 1 else f"_{i}"

inversion_plotter.subplot_of_mapper(
mapper_index=i, auto_filename=f"subplot_inversion{suffix}"
)

if should_plot("csv_reconstruction"):
mapper_list = inversion.cls_list_from(cls=aa.AbstractMapper)

for i, mapper in enumerate(mapper_list):
y = mapper.mapper_grids.source_plane_mesh_grid[:, 0]
x = mapper.mapper_grids.source_plane_mesh_grid[:, 1]
reconstruction = inversion.reconstruction_dict[mapper]
noise_map = inversion.reconstruction_noise_map_dict[mapper]

with open(
self.image_path / f"inversion_reconstruction_{i}.csv",
mode="w",
newline="",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why pass newline="" here?

) as file:
writer = csv.writer(file)
writer.writerow(["y", "x", "reconstruction", "noise_map"]) # header

for i in range(len(x)):
writer.writerow([y[i], x[i], reconstruction[i], noise_map[i]])

def adapt_images(
self,
Expand Down
2 changes: 1 addition & 1 deletion autogalaxy/config/visualize/plots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ galaxies: # Settings for plots of galaxies (e.g
inversion: # Settings for plots of inversions (e.g. InversionPlotter).
subplot_inversion: true # Plot subplot of all quantities in each inversion (e.g. reconstrucuted image, reconstruction)?
subplot_mappings: true # Plot subplot of the image-to-source pixels mappings of each pixelization?
fits_reconstruction: false # output reconstruction.fits containing the reconstructed pixelization and noise map on the adaptive mesh?
csv_reconstruction: false # output reconstruction.csv containing the source-plane mesh y, x, reconstruction and noise map values.

adapt: # Settings for plots of adapt images used by adaptive pixelizations.
subplot_adapt_images: true # Plot subplot showing each adapt image used for adaptive pixelization?
Expand Down
8 changes: 4 additions & 4 deletions autogalaxy/imaging/model/plotter_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def fits_to_fits(
hdu_list = hdu_list_for_output_from(
values_list=[
fit.mask.astype("float"),
fit.model_data,
fit.residual_map,
fit.normalized_residual_map,
fit.chi_squared_map,
fit.model_data.native,
fit.residual_map.native,
fit.normalized_residual_map.native,
fit.chi_squared_map.native,
],
ext_name_list=[
"mask",
Expand Down
18 changes: 18 additions & 0 deletions test_autogalaxy/analysis/test_plotter_interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import csv
import shutil
import numpy as np
from os import path
import pytest

Expand Down Expand Up @@ -59,6 +61,22 @@ def test__inversion(

assert path.join(plot_path, "subplot_inversion_0.png") in plot_patch.paths

with open(path.join(plot_path, "inversion_reconstruction_0.csv"), mode="r") as file:
reader = csv.reader(file)
header_list = next(reader) # ['y', 'x', 'reconstruction', 'noise_map']

reconstruction_dict = {header: [] for header in header_list}

for row in reader:
for key, value in zip(header_list, row):
reconstruction_dict[key].append(float(value))
Comment on lines +64 to +72
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar (although not quite the same) behaviour can be obtained using csv.DictReader

reader = csv.DictReader(file)
list(reader)[0]
> {'y': 1, 'x': 2, ...}


# Convert lists to NumPy arrays
for key in reconstruction_dict:
reconstruction_dict[key] = np.array(reconstruction_dict[key])

assert reconstruction_dict["x"][0] == pytest.approx(-0.8333333333333334, rel=1.0e-2)


def test__adapt_images(
masked_imaging_7x7,
Expand Down
1 change: 1 addition & 0 deletions test_autogalaxy/config/visualize.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ plots:
subplot_adapt_images: true
inversion:
subplot_inversion: true
csv_reconstruction: true # output reconstruction_mesh.fits containing the reconstructed pixelization and noise map on the source-plane mesh?
galaxies:
subplot_galaxies: true
subplot_galaxy_images: true
Expand Down
Loading