def test__use_border__determines_if_border_pixel_relocation_is_used( self, masked_imaging_7x7 ): model = af.Collection( galaxies=af.Collection( lens=al.Galaxy( redshift=0.5, mass=al.mp.SphIsothermal(einstein_radius=100.0) ), source=al.Galaxy( redshift=1.0, pixelization=al.pix.Rectangular(shape=(3, 3)), regularization=al.reg.Constant(coefficient=1.0), ), ) ) masked_imaging_7x7 = masked_imaging_7x7.apply_settings( settings=al.SettingsImaging(sub_size_inversion=2) ) analysis = al.AnalysisImaging( dataset=masked_imaging_7x7, settings_pixelization=al.SettingsPixelization(use_border=True), ) analysis.dataset.grid_inversion[4] = np.array([[500.0, 0.0]]) instance = model.instance_from_unit_vector([]) tracer = analysis.tracer_for_instance(instance=instance) fit = analysis.fit_imaging_for_tracer( tracer=tracer, hyper_image_sky=None, hyper_background_noise=None ) assert fit.inversion.linear_obj_list[0].source_grid_slim[4][0] == pytest.approx( 97.19584, 1.0e-2 ) assert fit.inversion.linear_obj_list[0].source_grid_slim[4][1] == pytest.approx( -3.699999, 1.0e-2 ) analysis = al.AnalysisImaging( dataset=masked_imaging_7x7, settings_pixelization=al.SettingsPixelization(use_border=False), ) analysis.dataset.grid_inversion[4] = np.array([300.0, 0.0]) instance = model.instance_from_unit_vector([]) tracer = analysis.tracer_for_instance(instance=instance) fit = analysis.fit_imaging_for_tracer( tracer=tracer, hyper_image_sky=None, hyper_background_noise=None ) assert fit.inversion.linear_obj_list[0].source_grid_slim[4][0] == pytest.approx( 200.0, 1.0e-4 )
fit_imaging_plotter.subplot_fit_imaging() """ __Modification__ The `FitImagingAgg` allow us to modify the fit settings. By default, it uses the `SettingsImaging`, `SettingsPixelization` and `SettingsInversion` that were used during the model-fit. However, we can change these settings such that the fit is performed differently. For example, what if I wanted to see how the fit looks where the `Grid2D`'s `sub_size` is 4 (instead of the value of 2 that was used)? Or where the pixelization didn`t use a border? You can do this by passing the settings objects, which overwrite the ones used by the analysis. """ fit_agg = al.agg.FitImagingAgg( aggregator=agg, settings_imaging=al.SettingsImaging(sub_size=4), settings_pixelization=al.SettingsPixelization(use_border=False), ) fit_imaging_gen = fit_agg.max_log_likelihood_gen() for fit in fit_imaging_gen: fit_imaging_plotter = aplt.FitImagingPlotter(fit=fit) fit_imaging_plotter.subplot_fit_imaging() """ __Visualization Customization__ The benefit of inspecting fits using the aggregator, rather than the files outputs to the hard-disk, is that we can customize the plots using the PyAutoLens mat_plot_2d. Below, we create a new function to apply as a generator to do this. However, we use a convenience method available in the PyAutoLens aggregator package to set up the fit.
def test__simulate_imaging_data_and_fit__include_psf_blurring__chi_squared_is_0__noise_normalization_correct( ): grid = al.Grid2D.uniform(shape_native=(11, 11), pixel_scales=0.2, sub_size=1) psf = al.Kernel2D.from_gaussian(shape_native=(3, 3), pixel_scales=0.2, sigma=0.75, normalize=True) lens_galaxy = al.Galaxy( redshift=0.5, light=al.lp.EllSersic(centre=(0.1, 0.1), intensity=0.1), mass=al.mp.EllIsothermal(centre=(0.1, 0.1), einstein_radius=1.8), ) source_galaxy = al.Galaxy(redshift=1.0, light=al.lp.EllExponential(centre=(0.1, 0.1), intensity=0.5)) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) imaging = al.SimulatorImaging(exposure_time=300.0, psf=psf, add_poisson_noise=False) imaging = imaging.from_tracer_and_grid(tracer=tracer, grid=grid) imaging.noise_map = al.Array2D.ones( shape_native=imaging.image.shape_native, pixel_scales=0.2) file_path = path.join( "{}".format(path.dirname(path.realpath(__file__))), "data_temp", "simulate_and_fit", ) try: shutil.rmtree(file_path) except FileNotFoundError: pass if path.exists(file_path) is False: os.makedirs(file_path) imaging.output_to_fits( image_path=path.join(file_path, "image.fits"), noise_map_path=path.join(file_path, "noise_map.fits"), psf_path=path.join(file_path, "psf.fits"), ) imaging = al.Imaging.from_fits( image_path=path.join(file_path, "image.fits"), noise_map_path=path.join(file_path, "noise_map.fits"), psf_path=path.join(file_path, "psf.fits"), pixel_scales=0.2, ) mask = al.Mask2D.circular(shape_native=imaging.image.shape_native, pixel_scales=0.2, radius=0.8) masked_imaging = imaging.apply_mask(mask=mask) masked_imaging = masked_imaging.apply_settings(settings=al.SettingsImaging( sub_size=1)) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) fit = al.FitImaging(imaging=masked_imaging, tracer=tracer) assert fit.chi_squared == pytest.approx(0.0, 1e-4) file_path = path.join("{}".format(path.dirname(path.realpath(__file__))), "data_temp") if path.exists(file_path) is True: shutil.rmtree(file_path)
from os import path import autofit as af import autolens as al import autolens.plot as aplt """ __Settings Specific Code__ To customize the sub-grid used by the model-fit, we create a `SettingsImaging` object and specify that the `sub_size=4`. This increases the sub grid size of the `Grid2D` used to evaluate the lens galaxy and source galaxy `LightProfiles` from the default value of 2 to 4. """ settings_imaging = al.SettingsImaging(grid_class=al.Grid2D, sub_size=4) """ __Dataset + Masking__ For this sub-grid to be used in the model-fit, we must pass the `settings_imaging` to the `Imaging` object, which will be created using a `Grid2D` with a `sub-size value` of 4 (instead of the default of 2). """ dataset_name = "mass_sie__source_sersic" dataset_path = path.join("dataset", "imaging", "no_lens_light", dataset_name) imaging = al.Imaging.from_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"), pixel_scales=0.1,
from os import path import autofit as af import autolens as al import autolens.plot as aplt """ __Settings Specific Code__ To limit the signal-to-noise of the dataset used by the model-fit, we create a `SettingsImaging` object and specify that the `signal_to_noise_limit=10.0` and `signal_to_noise_limit_radii=0.5`. When the `Imaging` object is created, this will rescale the noise-map such that all image-pixels within 0.5" of the central pixel are increased so as to produce a signal-to-noise value in the data of 10.0. Image-pixels with a S/N value below 10.0 do not have their noise-map value changed. """ settings_imaging = al.SettingsImaging(signal_to_noise_limit=10.0, signal_to_noise_limit_radii=0.5) """ __Dataset__ For signal-to-noise limiting to be used in the model-fit, we must pass the `settings_imaging` to the `Imaging` object, which will rescale its noise-map accordingly. """ dataset_name = "light_sersic_exp__mass_sie__source_sersic" dataset_path = path.join("dataset", "imaging", "with_lens_light", dataset_name) imaging = al.Imaging.from_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"), pixel_scales=0.1, )
from os import path import autofit as af import autolens as al import autolens.plot as aplt """ __Settings Specific Code__ To use deflection angle interpolation, we create a `SettingsImaging` object and specify that the `grid_class=al.Grid2DInterpolate` and `pixel_scales_interp=0.05`. By using a `Grid2dInterpolate` the interpolation scheme described above is used, with the coarse grid used to compute deflection angles having a pixel-scale of 0.05". """ settings_imaging = al.SettingsImaging( grid_class=al.Grid2DInterpolate, pixel_scales_interp=0.05 ) """ __Dataset + Masking__ For this sub-grid to be used in the model-fit, we must pass the `settings_imaging` to the `Imaging` object, which will be created using a `Grid2D` with a `sub-size value` of 4 (instead of the default of 2). """ dataset_name = "mass_sie__source_sersic" dataset_path = path.join("dataset", "imaging", "no_lens_light", dataset_name) imaging = al.Imaging.from_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"),
psf_path=path.join(dataset_path, "psf.fits"), noise_map_path=path.join(dataset_path, "noise_map.fits"), pixel_scales=0.1, ) """ __Fit__ We now mask the data and fit it with a `Tracer` to create a `FitImaging` object. """ mask = al.Mask2D.circular( shape_native=imaging.shape_native, pixel_scales=imaging.pixel_scales, radius=3.5 ) imaging = imaging.apply_mask(mask=mask) imaging = imaging.apply_settings( settings=al.SettingsImaging(grid_class=al.Grid2DIterate) ) lens_galaxy = al.Galaxy( redshift=0.5, bulge=al.lp.EllSersic( centre=(0.0, 0.0), elliptical_comps=al.convert.elliptical_comps_from(axis_ratio=0.9, angle=45.0), intensity=1.0, effective_radius=0.8, sersic_index=4.0, ), mass=al.mp.EllIsothermal( centre=(0.0, 0.0), einstein_radius=1.5, elliptical_comps=al.convert.elliptical_comps_from(axis_ratio=0.9, angle=45.0),