def test__subtracted_image_of_plane_is_output( masked_imaging_fit_x1_plane_7x7, masked_imaging_fit_x2_plane_7x7, include_all, plot_path, plot_patch, ): aplt.FitImaging.subtracted_image_of_plane( fit=masked_imaging_fit_x1_plane_7x7, plane_index=0, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plot_path, format="png")), ) assert plot_path + "subtracted_image_of_plane_0.png" in plot_patch.paths aplt.FitImaging.subtracted_image_of_plane( fit=masked_imaging_fit_x2_plane_7x7, plane_index=0, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plot_path, format="png")), ) assert plot_path + "subtracted_image_of_plane_0.png" in plot_patch.paths aplt.FitImaging.subtracted_image_of_plane( fit=masked_imaging_fit_x2_plane_7x7, plane_index=1, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plot_path, format="png")), ) assert plot_path + "subtracted_image_of_plane_1.png" in plot_patch.paths
def test__fit_individuals__source_and_lens__depedent_on_input( masked_interferometer_fit_x1_plane_7x7, masked_interferometer_fit_x2_plane_7x7, include_all, plot_path, plot_patch, ): aplt.FitInterferometer.individuals( fit=masked_interferometer_fit_x1_plane_7x7, plot_visibilities=True, plot_noise_map=False, plot_signal_to_noise_map=False, plot_model_visibilities=True, plot_chi_squared_map=True, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plot_path, format="png")), ) assert plot_path + "visibilities.png" in plot_patch.paths assert plot_path + "noise_map.png" not in plot_patch.paths assert plot_path + "signal_to_noise_map.png" not in plot_patch.paths assert plot_path + "model_visibilities.png" in plot_patch.paths assert plot_path + "residual_map_vs_uv_distances_real.png" not in plot_patch.paths assert (plot_path + "normalized_residual_map_vs_uv_distances_real.png" not in plot_patch.paths) assert plot_path + "chi_squared_map_vs_uv_distances_real.png" in plot_patch.paths
def test__tracer_individuals__dependent_on_input(tracer_x2_plane_7x7, sub_grid_7x7, include_all, ray_tracing_plotter_path, plot_patch): aplt.Tracer.individual( tracer=tracer_x2_plane_7x7, grid=sub_grid_7x7, plot_image=True, plot_source_plane=True, plot_potential=True, plot_magnification=True, include=include_all, plotter=aplt.Plotter( output=aplt.Output(ray_tracing_plotter_path, format="png")), ) assert ray_tracing_plotter_path + "image.png" in plot_patch.paths assert ray_tracing_plotter_path + "source_plane.png" in plot_patch.paths assert ray_tracing_plotter_path + "convergence.png" not in plot_patch.paths assert ray_tracing_plotter_path + "potential.png" in plot_patch.paths assert ray_tracing_plotter_path + "deflections_y.png" not in plot_patch.paths assert ray_tracing_plotter_path + "deflections_x.png" not in plot_patch.paths assert ray_tracing_plotter_path + "magnification.png" in plot_patch.paths
def simulate_imaging_from_instrument(instrument, dataset_name, galaxies): # Simulate the imaging data, remembering that we use a special image which ensures edge-effects don't # degrade our modeling of the telescope optics (e.al. the PSF convolution). grid = instrument_util.grid_from_instrument(instrument=instrument) simulator = simulator_from_instrument(instrument=instrument) # Use the input galaxies to setup a tracer, which will generate the image for the simulated imaging data. tracer = al.Tracer.from_galaxies(galaxies=galaxies) imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid) # Now, lets output this simulated imaging-data to the test_autoarray/simulator folder. test_path = path.join("{}".format(path.dirname(path.realpath(__file__))), "..", "..") dataset_path = path.join(test_path, "dataset", "imaging", dataset_name, instrument) imaging.output_to_fits( image_path=path.join(dataset_path, "image.fits"), psf_path=path.join(dataset_path, "psf.fits"), noise_map_path=path.join(dataset_path, "noise_map.fits"), overwrite=True, ) plotter = aplt.Plotter(output=aplt.Output(path=dataset_path, format="png")) sub_plotter = aplt.SubPlotter( output=aplt.Output(path=dataset_path, format="png")) aplt.Imaging.subplot_imaging(imaging=imaging, sub_plotter=sub_plotter) aplt.Imaging.individual( imaging=imaging, plot_image=True, plot_noise_map=True, plot_psf=True, plot_signal_to_noise_map=True, plotter=plotter, ) aplt.Tracer.subplot_tracer(tracer=tracer, grid=grid, sub_plotter=sub_plotter) aplt.Tracer.individual( tracer=tracer, grid=grid, plot_image=True, plot_source_plane=True, plot_convergence=True, plot_potential=True, plot_deflections=True, plotter=plotter, )
def test__fit_quantities_are_output( masked_imaging_fit_x2_plane_7x7, include_all, plot_path, plot_patch ): aplt.FitImaging.image( fit=masked_imaging_fit_x2_plane_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "image.png" in plot_patch.paths aplt.FitImaging.noise_map( fit=masked_imaging_fit_x2_plane_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "noise_map.png" in plot_patch.paths aplt.FitImaging.signal_to_noise_map( fit=masked_imaging_fit_x2_plane_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "signal_to_noise_map.png" in plot_patch.paths aplt.FitImaging.model_image( fit=masked_imaging_fit_x2_plane_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "model_image.png" in plot_patch.paths aplt.FitImaging.residual_map( fit=masked_imaging_fit_x2_plane_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "residual_map.png" in plot_patch.paths aplt.FitImaging.normalized_residual_map( fit=masked_imaging_fit_x2_plane_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "normalized_residual_map.png" in plot_patch.paths aplt.FitImaging.chi_squared_map( fit=masked_imaging_fit_x2_plane_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "chi_squared_map.png" in plot_patch.paths
def test__plot_individual_images(hyper_galaxy_image_0_7x7, contribution_map_7x7, include_all, plot_path, plot_patch): aplt.hyper.hyper_galaxy_image( galaxy_image=hyper_galaxy_image_0_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plot_path, format="png")), ) assert plot_path + "hyper_galaxy_image.png" in plot_patch.paths aplt.hyper.contribution_map( contribution_map_in=contribution_map_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plot_path, format="png")), ) assert plot_path + "contribution_map.png" in plot_patch.paths
def simulate_interferometer_from_instrument(instrument, data_name, galaxies): # Simulate the imaging data, remembering that we use a special image which ensures edge-effects don't # degrade our modeling of the telescope optics (e.al. the PSF convolution). grid = instrument_util.grid_from_instrument(instrument=instrument) simulator = simulator_from_instrument(instrument=instrument) # Use the input galaxies to setup a tracer, which will generate the image for the simulated imaging data. tracer = al.Tracer.from_galaxies(galaxies=galaxies) interferometer = simulator.from_tracer_and_grid(tracer=tracer, grid=grid) # Now, lets output this simulated interferometer-simulator to the test_autoarray/simulator folder. test_path = "{}/../../".format(os.path.dirname(os.path.realpath(__file__))) dataset_path = af.util.create_path( path=test_path, folders=["dataset", "interferometer", data_name, instrument] ) interferometer.output_to_fits( visibilities_path=f"{dataset_path}/visibilities.fits", noise_map_path=f"{dataset_path}/noise_map.fits", uv_wavelengths_path=f"{dataset_path}/uv_wavelengths.fits", overwrite=True, ) plotter = aplt.Plotter(output=aplt.Output(path=dataset_path, format="png")) sub_plotter = aplt.SubPlotter(output=aplt.Output(path=dataset_path, format="png")) aplt.Interferometer.subplot_interferometer( interferometer=interferometer, sub_plotter=sub_plotter ) aplt.Interferometer.individual( interferometer=interferometer, plot_visibilities=True, plotter=plotter ) aplt.Tracer.subplot_tracer(tracer=tracer, grid=grid, sub_plotter=sub_plotter) aplt.Tracer.individual( tracer=tracer, grid=grid, plot_image=True, plot_source_plane=True, plot_convergence=True, plot_potential=True, plot_deflections=True, plotter=plotter, )
def test__inversion_individuals__output_dependent_on_input( rectangular_inversion_7x7_3x3, positions_7x7, plot_path, plot_patch): aplt.inversion.individuals( inversion=rectangular_inversion_7x7_3x3, plot_reconstructed_image=True, plot_errors=True, plot_chi_squared_map=True, plot_interpolated_reconstruction=True, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "reconstructed_image.png" in plot_patch.paths assert plot_path + "reconstruction.png" not in plot_patch.paths assert plot_path + "errors.png" in plot_patch.paths assert plot_path + "residual_map.png" not in plot_patch.paths assert plot_path + "normalized_residual_map.png" not in plot_patch.paths assert plot_path + "chi_squared_map.png" in plot_patch.paths assert plot_path + "interpolated_reconstruction.png" in plot_patch.paths assert plot_path + "interpolated_errors.png" not in plot_patch.paths
# Lets look at a few contribution maps, generated using hyper-galaxy's with different contribution factors. source_contribution_factor_1 = al.Galaxy( redshift=1.0, hyper_galaxy=al.HyperGalaxy(contribution_factor=1.0), hyper_galaxy_image=hyper_image, hyper_model_image=hyper_image, ) contribution_map = source_contribution_factor_1.hyper_galaxy.contribution_map_from_hyper_images( hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image) aplt.array( array=contribution_map, mask=mask, plotter=aplt.Plotter(labels=aplt.Labels(title="Contribution Map")), ) source_contribution_factor_3 = al.Galaxy( redshift=1.0, hyper_galaxy=al.HyperGalaxy(contribution_factor=3.0), hyper_galaxy_image=hyper_image, hyper_model_image=hyper_image, ) contribution_map = source_contribution_factor_3.hyper_galaxy.contribution_map_from_hyper_images( hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image) aplt.array( array=contribution_map, mask=mask,
... and use this `Grid` to setup the source-plane """ # %% source_plane = al.Plane(galaxies=[source_galaxy]) # %% """ Lets inspect our `Grid`'s - I bet our source-plane isn't the boring uniform `Grid` we plotted in the first tutorial! """ # %% aplt.Plane.plane_grid( plane=image_plane, grid=image_plane_grid, plotter=aplt.Plotter(labels=aplt.Labels(title="Image-plane Grid")), ) aplt.Plane.plane_grid( plane=source_plane, grid=source_plane_grid, plotter=aplt.Plotter(labels=aplt.Labels(title="Source-plane Grid")), ) # %% """ We can zoom in on the `centre` of the source-plane (remembering the lens galaxy was centred at (0.1", 0.1")). """ # %% aplt.Plane.plane_grid(
import autolens as al import autolens.plot as aplt plotter = aplt.Plotter() sub_plotter = aplt.SubPlotter() grid = al.Grid.uniform(shape_2d=(100, 100), pixel_scales=0.05, sub_size=2) lens_galaxy = al.Galaxy( redshift=0.5, mass=al.mp.EllipticalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0, elliptical_comps=(0.1, 0.1)), ) source_galaxy = al.Galaxy( redshift=1.0, light=al.lp.EllipticalExponential(centre=(0.02, 0.01), intensity=1.0, effective_radius=0.5), ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) aplt.Tracer.image( tracer=tracer, grid=grid, include=aplt.Include(critical_curves=True), plotter=plotter, )
reconstructed fluxes of every source-pixel pair multiplied by the regularization_coefficient. By setting the regularization coefficient to zero, we set this penalty term to zero, meaning that `Regularization`.s omitted. Why do we need to regularize our solution? Well, we just saw why - if we don't apply this smoothing, we `over-fit` the image. More specifically, we over-fit the noise in the image, which is what the large flux values located at the exteriors of the source reconstruction are doing. Think about it, if your sole aim is to maximize the log likelihood, the best way to do this is to fit *everything* accurately, including the noise. If we change the `normalization` variables of the `Plotter` such that the color-map is restricted to a narrower range of values, we can see that even without `Regularization`.e are still reconstructing the actual source galaxy. """ # %% aplt.Inversion.reconstruction( inversion=no_regularization_fit.inversion, plotter=aplt.Plotter(cmap=aplt.ColorMap(norm_max=0.5, norm_min=-0.5)), ) # %% """ Over-fitting is why `Regularization`.s necessary. Solutions like this completely ruin our attempts to model a strong lens. By smoothing our source reconstruction we ensure it doesn`t fit the noise in the image. If we set a really high regularization coefficient we completely remove over-fitting at the expense of also fitting the image less accurately. """ # %% source_galaxy = al.Galaxy( redshift=1.0, pixelization=al.pix.Rectangular(shape=(40, 40)), regularization=al.reg.Constant(coefficient=100.0), )
# source-plane locations and their light and mass profiles to perform lensing calculations. tracer = al.Tracer.from_galaxies( galaxies=[lens_galaxy_0, lens_galaxy_1, source_galaxy]) # Ray-tracing calculations are performed using a (y,x) grid of Cartesian coordinates. grid = al.Grid.uniform(shape_2d=(150, 150), pixel_scales=0.05) # The tracer and grid are used to calculate the lens system's convergence, potential, # deflection angles, image-plane image and source-plane image (see figure ?). aplt.Tracer.profile_image(tracer=tracer, grid=grid, include=aplt.Include(critical_curves=True, caustics=True)) figures_path = "{}/../figures/".format( os.path.dirname(os.path.realpath(__file__))) aplt.Tracer.profile_image( tracer=tracer, grid=grid, include=aplt.Include(critical_curves=True, caustics=True), plotter=aplt.Plotter( labels=aplt.Labels(title="Multi-Plane Strong Lens Image"), output=aplt.Output( path=figures_path + "/paper/", filename="figure_4_complex_source", format="png", ), ), )
def test__individual_attributes_are_output_for_rectangular_inversion( rectangular_inversion_7x7_3x3, positions_7x7, plot_path, plot_patch): critical_curves = [(0.0, 0.0), (0.1, 0.1)] caustics = [(0.0, 0.0), (0.1, 0.1)] aplt.inversion.reconstructed_image( inversion=rectangular_inversion_7x7_3x3, image_positions=positions_7x7, critical_curves=critical_curves, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "reconstructed_image.png" in plot_patch.paths aplt.inversion.reconstruction( inversion=rectangular_inversion_7x7_3x3, source_positions=positions_7x7, caustics=caustics, image_pixel_indexes=[0], source_pixel_indexes=[1], plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "reconstruction.png" in plot_patch.paths aplt.inversion.errors( inversion=rectangular_inversion_7x7_3x3, source_positions=positions_7x7, caustics=caustics, image_pixel_indexes=[0], source_pixel_indexes=[1], plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "errors.png" in plot_patch.paths aplt.inversion.residual_map( inversion=rectangular_inversion_7x7_3x3, source_positions=positions_7x7, caustics=caustics, image_pixel_indexes=[0], source_pixel_indexes=[1], plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "residual_map.png" in plot_patch.paths aplt.inversion.normalized_residual_map( inversion=rectangular_inversion_7x7_3x3, source_positions=positions_7x7, caustics=caustics, image_pixel_indexes=[0], source_pixel_indexes=[1], plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "normalized_residual_map.png" in plot_patch.paths aplt.inversion.chi_squared_map( inversion=rectangular_inversion_7x7_3x3, source_positions=positions_7x7, caustics=caustics, image_pixel_indexes=[0], source_pixel_indexes=[1], plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "chi_squared_map.png" in plot_patch.paths aplt.inversion.regularization_weights( inversion=rectangular_inversion_7x7_3x3, source_positions=positions_7x7, caustics=caustics, image_pixel_indexes=[0], source_pixel_indexes=[1], plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "regularization_weights.png" in plot_patch.paths aplt.inversion.interpolated_reconstruction( inversion=rectangular_inversion_7x7_3x3, source_positions=positions_7x7, caustics=caustics, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "interpolated_reconstruction.png" in plot_patch.paths aplt.inversion.interpolated_errors( inversion=rectangular_inversion_7x7_3x3, source_positions=positions_7x7, caustics=caustics, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert plot_path + "interpolated_errors.png" in plot_patch.paths
def test__fit_individuals__source_and_lens__dependent_on_input( masked_imaging_fit_x1_plane_7x7, masked_imaging_fit_x2_plane_7x7, include_all, plot_path, plot_patch, ): aplt.FitImaging.individuals( fit=masked_imaging_fit_x1_plane_7x7, plot_image=True, plot_noise_map=False, plot_signal_to_noise_map=False, plot_model_image=True, plot_chi_squared_map=True, plot_subtracted_images_of_planes=True, plot_model_images_of_planes=True, plot_plane_images_of_planes=True, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plot_path, format="png")), ) assert plot_path + "image.png" in plot_patch.paths assert plot_path + "noise_map.png" not in plot_patch.paths assert plot_path + "signal_to_noise_map.png" not in plot_patch.paths assert plot_path + "model_image.png" in plot_patch.paths assert plot_path + "residual_map.png" not in plot_patch.paths assert plot_path + "normalized_residual_map.png" not in plot_patch.paths assert plot_path + "chi_squared_map.png" in plot_patch.paths assert plot_path + "subtracted_image_of_plane_0.png" in plot_patch.paths assert plot_path + "model_image_of_plane_0.png" in plot_patch.paths assert plot_path + "plane_image_of_plane_0.png" in plot_patch.paths aplt.FitImaging.individuals( fit=masked_imaging_fit_x2_plane_7x7, plot_image=True, plot_noise_map=False, plot_signal_to_noise_map=False, plot_model_image=True, plot_chi_squared_map=True, plot_subtracted_images_of_planes=True, plot_model_images_of_planes=True, plot_plane_images_of_planes=True, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plot_path, format="png")), ) assert plot_path + "image.png" in plot_patch.paths assert plot_path + "noise_map.png" not in plot_patch.paths assert plot_path + "signal_to_noise_map.png" not in plot_patch.paths assert plot_path + "model_image.png" in plot_patch.paths assert plot_path + "residual_map.png" not in plot_patch.paths assert plot_path + "normalized_residual_map.png" not in plot_patch.paths assert plot_path + "chi_squared_map.png" in plot_patch.paths assert plot_path + "subtracted_image_of_plane_0.png" in plot_patch.paths assert plot_path + "subtracted_image_of_plane_1.png" in plot_patch.paths assert plot_path + "model_image_of_plane_0.png" in plot_patch.paths assert plot_path + "model_image_of_plane_1.png" in plot_patch.paths assert plot_path + "plane_image_of_plane_0.png" in plot_patch.paths assert plot_path + "plane_image_of_plane_1.png" in plot_patch.paths
folder_names=["dataset", dataset_label, dataset_name, pipeline_name, phase_name], ) residual_map_path = result_path + "/image/fits/fit_residual_map.fits" chi_squared_map_path = result_path + "/image/fits/fit_chi_squared_map.fits" # Now, lets load this arrays as a hyper arrays. A hyper arrays is an ordinary NumPy arrays, but it also includes a pixel # scale which allows us to convert the axes of the arrays to arc-second coordinates. image = al.array.from_fits(file_path=image_path, hdu=0, pixel_scales=0.03) # Now, lets load this image as a hyper arrays. A hyper arrays is an ordinary NumPy arrays, but it also includes a pixel # scale which allows us to convert the axes of the image to arc-second coordinates. residual_map = al.array.from_fits(file_path=residual_map_path, hdu=0, pixel_scales=0.03) # We can now use an arrays plotter to plotters the residual map. plotter = aplt.Plotter(labels=aplt.Labels(title="SLACS1430+4105 Residual Map")) aplt.array(array=residual_map, plotter=plotter) # A useful way to really dig into the residuals is to set upper and lower limits on the normalization of the colorbar. plotter = aplt.Plotter( labels=aplt.Labels(title="SLACS1430+4105 Residual Map"), cmap=aplt.ColorMap(norm_min=-0.02, norm_max=0.02), ) aplt.array(array=residual_map, plotter=plotter) # Or, alternatively, use a symmetric logarithmic colormap plotter = aplt.Plotter(
def test__fit_quantities_are_output(masked_interferometer_fit_x2_plane_7x7, plot_path, plot_patch): aplt.FitInterferometer.visibilities( fit=masked_interferometer_fit_x2_plane_7x7, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert path.join(plot_path, "visibilities.png") in plot_patch.paths aplt.FitInterferometer.noise_map( fit=masked_interferometer_fit_x2_plane_7x7, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert path.join(plot_path, "noise_map.png") in plot_patch.paths aplt.FitInterferometer.signal_to_noise_map( fit=masked_interferometer_fit_x2_plane_7x7, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert path.join(plot_path, "signal_to_noise_map.png") in plot_patch.paths aplt.FitInterferometer.model_visibilities( fit=masked_interferometer_fit_x2_plane_7x7, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert path.join(plot_path, "model_visibilities.png") in plot_patch.paths aplt.FitInterferometer.residual_map_vs_uv_distances( fit=masked_interferometer_fit_x2_plane_7x7, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert (path.join(plot_path, "residual_map_vs_uv_distances_real.png") in plot_patch.paths) aplt.FitInterferometer.residual_map_vs_uv_distances( fit=masked_interferometer_fit_x2_plane_7x7, plot_real=False, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert (path.join(plot_path, "residual_map_vs_uv_distances_imag.png") in plot_patch.paths) aplt.FitInterferometer.normalized_residual_map_vs_uv_distances( fit=masked_interferometer_fit_x2_plane_7x7, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert (path.join(plot_path, "normalized_residual_map_vs_uv_distances_real.png") in plot_patch.paths) aplt.FitInterferometer.normalized_residual_map_vs_uv_distances( fit=masked_interferometer_fit_x2_plane_7x7, plot_real=False, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert (path.join(plot_path, "normalized_residual_map_vs_uv_distances_imag.png") in plot_patch.paths) aplt.FitInterferometer.chi_squared_map_vs_uv_distances( fit=masked_interferometer_fit_x2_plane_7x7, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert (path.join(plot_path, "chi_squared_map_vs_uv_distances_real.png") in plot_patch.paths) aplt.FitInterferometer.chi_squared_map_vs_uv_distances( fit=masked_interferometer_fit_x2_plane_7x7, plot_real=False, plotter=aplt.Plotter(output=aplt.Output(path=plot_path, format="png")), ) assert (path.join(plot_path, "chi_squared_map_vs_uv_distances_imag.png") in plot_patch.paths)
def simulate_imaging_from_galaxies_and_output_to_fits( data_type, data_resolution, galaxies, sub_size=4 ): # Simulate the imaging data, remembering that we use a special image which ensures edge-effects don't # degrade our modeling of the telescope optics (e.al. the PSF convolution). simulator = simulator_from_data_resolution( data_resolution=data_resolution, sub_size=sub_size ) # Use the input galaxies to setup a tracer, which will generate the image for the simulated imaging data. tracer = al.Tracer.from_galaxies(galaxies=galaxies) imaging = simulator.from_tracer(tracer=tracer) # Now, lets output this simulated imaging-data to the test_autoarray/dataset folder. imaging_path = "{}/../".format(os.path.dirname(os.path.realpath(__file__))) dataset_path = af.path_util.make_and_return_path_from_path_and_folder_names( path=imaging_path, folder_names=["dataset", data_type, data_resolution] ) imaging.output_to_fits( image_path=dataset_path + "image.fits", psf_path=dataset_path + "psf.fits", noise_map_path=dataset_path + "noise_map.fits", overwrite=True, ) aplt.imaging.subplot_imaging( imaging=imaging, sub_plotter=aplt.SubPlotter( output=aplt.Output(filename="imaging", path=dataset_path, format="png") ), ) aplt.imaging.individual( imaging=imaging, plot_image=True, plot_noise_map=True, plot_psf=True, plot_signal_to_noise_map=True, plotter=aplt.Plotter(output=aplt.Output(path=dataset_path, format="png")), ) aplt.tracer.subplot_tracer( tracer=tracer, grid=simulator.grid, sub_plotter=aplt.SubPlotter( output=aplt.Output(filename="tracer", path=dataset_path, format="png") ), ) aplt.tracer.individual( tracer=tracer, grid=simulator.grid, plot_profile_image=True, plot_source_plane=True, plot_convergence=True, plot_potential=True, plot_deflections=True, plotter=aplt.Plotter(output=aplt.Output(path=dataset_path, format="png")), )
aplt.Galaxy.image(galaxy=fit.tracer.source_plane.galaxies[0], grid=source_plane_grid) # %% """ As our fit and ray-tracing becomes more complex, it is useful to know how to decompose their different attributes to extract different things about them. For example, we made our source-galaxy above with two `LightProfile`'s, a `bulge` and `disk. We can plot the image of each component individually, if we know how to break-up the different components of the fit and `Tracer`. """ # %% aplt.LightProfile.image( light_profile=fit.tracer.source_plane.galaxies[0].bulge, grid=source_plane_grid, plotter=aplt.Plotter(labels=aplt.Labels(title="Bulge image")), ) aplt.LightProfile.image( light_profile=fit.tracer.source_plane.galaxies[0].disk, grid=source_plane_grid, plotter=aplt.Plotter(labels=aplt.Labels(title="Disk image")), ) # %% """ And, we're done, not just with the tutorial, but the chapter! To end, I want to quickly talk about code-design and structure. Yeah, I know, as a scientist, you don't like code and certainly don't want to think about code! However, the point is, with **PyAutoLens**, you don't need to!
redshift=1.0, light=al.lp.EllipticalSersic( centre=(0.1, 0.1), axis_ratio=0.8, phi=60.0, intensity=0.3, effective_radius=1.0, sersic_index=2.5, ), ) # Use these galaxies to setup a tracer, which will generate the image for the simulated interferometer dataset. tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) plotter = aplt.Plotter( labels=aplt.Labels(title="Image Before Fourier Transform"), output=aplt.Output(path=plot_path, filename="image_pre_ft", format="png"), ) aplt.Tracer.profile_image( tracer=tracer, grid=masked_interferometer.grid, plotter=plotter ) fit = al.FitInterferometer(masked_interferometer=masked_interferometer, tracer=tracer) plotter = aplt.Plotter( labels=aplt.Labels(title="Model Visibilities"), output=aplt.Output(path=plot_path, filename="model_visibilities", format="png"), ) aplt.FitInterferometer.model_visibilities(fit=fit, plotter=plotter)
plot_path = "{}/../images/lensing/".format(os.path.dirname(os.path.realpath(__file__))) grid = al.Grid.uniform(shape_2d=(50, 50), pixel_scales=0.05) isothermal_mass_profile = al.mp.EllipticalIsothermal( centre=(0.0, 0.0), axis_ratio=0.8, phi=120.0, einstein_radius=1.6 ) convergence = isothermal_mass_profile.convergence_from_grid(grid=grid) potential = isothermal_mass_profile.potential_from_grid(grid=grid) deflections = isothermal_mass_profile.deflections_from_grid(grid=grid) plotter = aplt.Plotter( labels=aplt.Labels(title="Image of Elliptical Isothermal Mass Profile Convergence"), output=aplt.Output( path=plot_path, filename="isothermal_mass_profile_convergence", format="png" ), ) aplt.MassProfile.convergence( mass_profile=isothermal_mass_profile, grid=grid, plotter=plotter ) plotter = aplt.Plotter( labels=aplt.Labels(title="Image of Elliptical Isothermal Mass Profile Potential"), output=aplt.Output( path=plot_path, filename="isothermal_mass_profile_potential", format="png" ), ) aplt.MassProfile.potential(
tracer.save(file_path=save_path, filename="true_tracer") grid = al.Grid.uniform( shape_2d=(200, 200), pixel_scales=0.05, ) print("\nplotting observed image") print(datetime.datetime.now()) psf = al.Kernel.from_gaussian(shape_2d=(11, 11), sigma=0.1, pixel_scales=grid.pixel_scales) simulator = al.SimulatorImaging( exposure_time_map=al.Array.full(fill_value=7500.0, shape_2d=grid.shape_2d), psf=psf, background_sky_map=al.Array.full(fill_value=0.1, shape_2d=grid.shape_2d), add_noise=True, ) imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid, name=dataset_name) imaging.output_to_fits( image_path=f"{save_path}image.fits", psf_path=f"{save_path}psf.fits", noise_map_path=f"{save_path}noise_map.fits", overwrite=True, ) observed_plotter = aplt.Plotter( output=aplt.Output(path=save_path, filename="ObservedImage", format="png")) aplt.Imaging.image(imaging, plotter=observed_plotter)
import autolens as al import autolens.plot as aplt import os plot_path = "{}/../images/lensing/".format( os.path.dirname(os.path.realpath(__file__))) grid = al.Grid.uniform(shape_2d=(50, 50), pixel_scales=0.05) sersic_light_profile = al.lp.EllipticalSersic( centre=(0.0, 0.0), axis_ratio=0.9, phi=60.0, intensity=0.05, effective_radius=2.0, sersic_index=4.0, ) image = sersic_light_profile.profile_image_from_grid(grid=grid) plotter = aplt.Plotter( labels=aplt.Labels(title="Image of Elliptical Sersic Light Profile"), output=aplt.Output(path=plot_path, filename="sersic_light_profile", format="png"), ) aplt.LightProfile.profile_image(light_profile=sersic_light_profile, grid=grid, plotter=plotter)
import autolens as al import autolens.plot as aplt from test_autolens.simulators.imaging import instrument_util import numpy as np imaging = instrument_util.load_test_imaging( dataset_name="light_sersic__source_sersic", instrument="vro") array = imaging.image plotter = aplt.Plotter( figure=aplt.Figure(figsize=(10, 10)), cmap=aplt.ColorMap(cmap="gray", norm="symmetric_log", norm_min=-0.13, norm_max=20, linthresh=0.02), grid_scatterer=aplt.GridScatterer(marker="+", colors="cyan", size=450), ) grid = al.GridIrregular(grid=[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]) print(grid) vector_field = al.VectorFieldIrregular(vectors=[(1.0, 2.0), (2.0, 1.0)], grid=[(-1.0, 0.0), (-2.0, 0.0)]) aplt.Array( array=array.in_2d, grid=grid, positions=al.GridIrregularGrouped([(0.0, 1.0), (0.0, 2.0)]),
effective_radius=0.2), mass=al.mp.SphericalIsothermal(centre=(1.0, 0.0), einstein_radius=0.4), ) print(lens_satellite) # %% """ Lets have a quick look at the appearance of our lens galaxy and its satellite. """ # %% aplt.Galaxy.image( galaxy=lens_galaxy, grid=grid, plotter=aplt.Plotter(labels=aplt.Labels(title="Lens Galaxy")), ) aplt.Galaxy.image( galaxy=lens_satellite, grid=grid, plotter=aplt.Plotter(labels=aplt.Labels(title="Lens Satellite")), ) # %% """ And their deflection angles - note that the satellite doesn`t contribute as much to the deflections. """ # %% aplt.Galaxy.deflections_y(
def test__all_individual_plotters( tracer_x2_plane_7x7, sub_grid_7x7, mask_7x7, include_all, ray_tracing_plotter_path, plot_patch, ): aplt.Tracer.image( tracer=tracer_x2_plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter( output=aplt.Output(ray_tracing_plotter_path, format="png")), ) assert ray_tracing_plotter_path + "image.png" in plot_patch.paths aplt.Tracer.convergence( tracer=tracer_x2_plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter( output=aplt.Output(ray_tracing_plotter_path, format="png")), ) assert ray_tracing_plotter_path + "convergence.png" in plot_patch.paths aplt.Tracer.potential( tracer=tracer_x2_plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter( output=aplt.Output(ray_tracing_plotter_path, format="png")), ) assert ray_tracing_plotter_path + "potential.png" in plot_patch.paths aplt.Tracer.deflections_y( tracer=tracer_x2_plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter( output=aplt.Output(ray_tracing_plotter_path, format="png")), ) assert ray_tracing_plotter_path + "deflections_y.png" in plot_patch.paths aplt.Tracer.deflections_x( tracer=tracer_x2_plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter( output=aplt.Output(ray_tracing_plotter_path, format="png")), ) assert ray_tracing_plotter_path + "deflections_x.png" in plot_patch.paths aplt.Tracer.magnification( tracer=tracer_x2_plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter( output=aplt.Output(ray_tracing_plotter_path, format="png")), ) assert ray_tracing_plotter_path + "magnification.png" in plot_patch.paths tracer_x2_plane_7x7.planes[0].galaxies[0].hyper_galaxy = al.HyperGalaxy() tracer_x2_plane_7x7.planes[0].galaxies[ 0].hyper_model_image = al.Array.ones(shape_2d=(7, 7), pixel_scales=0.1) tracer_x2_plane_7x7.planes[0].galaxies[ 0].hyper_galaxy_image = al.Array.ones(shape_2d=(7, 7), pixel_scales=0.1) aplt.Tracer.contribution_map( tracer=tracer_x2_plane_7x7, mask=mask_7x7, include=include_all, plotter=aplt.Plotter( output=aplt.Output(ray_tracing_plotter_path, format="png")), ) assert ray_tracing_plotter_path + "contribution_map.png" in plot_patch.paths
import autolens.plot as aplt # In this example, we'll create grids of Cartesian (y,x) coordinates, representing the arc-second coordinate grid of # an observed dataset (e.g. imaging). # In PyAutoLens, a grid is a set of two-dimensional (y,x) coordinates (in arc-seconds) that are deflected # and traced by a strong lens system. # This coordinate grid is aligned with the image we analyze, such that each coordinate on a grid maps to the # centre of each image-pixel. Lets make a grid using 100 x 100 pixels, with a pixel scale (arcsecond-to-pixel # conversion factor) of 0.05", giving us a a 5" x 5" grid. grid = al.grid.uniform(shape_2d=(100, 100), pixel_scales=0.05) # First, lets plotters this grid, which shows that it is a fairly bland uniform grid of dots. plotter = aplt.Plotter(labels=aplt.Labels(title="Fairly Bland Uniform Grid Of Dots")) aplt.grid(grid=grid, plotter=plotter) # We can print each coordinate of this grid, revealing that it consists of a set of arc-second coordinates # (where the spacing between each coordinate corresponds to the 'pixel_scales' of 0.05" defined above) print("(y,x) pixel 0:") print(grid.in_2d[0, 0]) print("(y,x) pixel 1:") print(grid.in_2d[0, 1]) print("(y,x) pixel 2:") print(grid.in_2d[0, 2]) print("(y,x) pixel 100:") print(grid.in_2d[1, 0]) print("etc.")
# imaging dataset. imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid) # Lets plot the simulated imaging dataset before we output it to fits. aplt.Imaging.subplot_imaging(imaging=imaging) # Finally, lets output our simulated dataset to the dataset path as .fits files. imaging.output_to_fits( image_path=dataset_path + "image.fits", psf_path=dataset_path + "psf.fits", noise_map_path=dataset_path + "noise_map.fits", overwrite=True, ) plotter = aplt.Plotter( labels=aplt.Labels(title="Image"), output=aplt.Output(path=dataset_path, filename="image", format="png"), ) aplt.Imaging.image(imaging=imaging, plotter=plotter) plotter = aplt.Plotter( labels=aplt.Labels(title="Noise-Map"), output=aplt.Output(path=dataset_path, filename="noise_map", format="png"), ) aplt.Imaging.noise_map(imaging=imaging, plotter=plotter) plotter = aplt.Plotter( labels=aplt.Labels(title="PSF"), output=aplt.Output(path=dataset_path, filename="psf", format="png"), )
# %% print(type(mapper)) # %% """ By plotting our mapper, we now see our _Pixelization_. Its a fairly boring _Grid_ of rectangular pixels. (we'll cover what the 'inversion' means in a later tutorial). """ # %% aplt.MapperObj( mapper=mapper, include=aplt.Include(inversion_grid=False, inversion_pixelization_grid=False), plotter=aplt.Plotter(labels=aplt.Labels( title="Fairly Boring Grid of Rectangular Pixels")), ) # %% """ However, the _Mapper_ does contain lots of interesting information about our _Pixelization_, for example its pixelization_grid tells us where the pixel centers are located. """ # %% print("Rectangular Grid Pixel Centre 1:") print(mapper.pixelization_grid[0]) print("Rectangular Grid Pixel Centre 2:") print(mapper.pixelization_grid[1]) print("Rectangular Grid Pixel Centre 3:") print(mapper.pixelization_grid[2])
def test__all_individual_plotters__output_file_with_default_name( plane_7x7, sub_grid_7x7, mask_7x7, positions_7x7, include_all, plane_plotter_path, plot_patch, ): aplt.Plane.profile_image( plane=plane_7x7, grid=sub_grid_7x7, positions=positions_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "profile_image.png" in plot_patch.paths aplt.Plane.plane_image( plane=plane_7x7, grid=sub_grid_7x7, positions=positions_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "plane_image.png" in plot_patch.paths aplt.Plane.convergence( plane=plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "convergence.png" in plot_patch.paths aplt.Plane.potential( plane=plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "potential.png" in plot_patch.paths aplt.Plane.deflections_y( plane=plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "deflections_y.png" in plot_patch.paths aplt.Plane.deflections_x( plane=plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "deflections_x.png" in plot_patch.paths aplt.Plane.magnification( plane=plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "magnification.png" in plot_patch.paths aplt.Plane.plane_grid( plane=plane_7x7, grid=sub_grid_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "plane_grid.png" in plot_patch.paths plane_7x7.galaxies[0].hyper_galaxy = al.HyperGalaxy() plane_7x7.galaxies[0].hyper_model_image = al.Array.ones( shape_2d=(7, 7), pixel_scales=0.1 ) plane_7x7.galaxies[0].hyper_galaxy_image = al.Array.ones( shape_2d=(7, 7), pixel_scales=0.1 ) aplt.Plane.contribution_map( plane=plane_7x7, mask=mask_7x7, positions=positions_7x7, include=include_all, plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")), ) assert plane_plotter_path + "contribution_map.png" in plot_patch.paths