def test__fit_figure_of_merit__matches_correct_fit_given_galaxy_profiles( self, interferometer_7, mask_7x7, visibilities_mask_7x2): lens_galaxy = al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=0.1)) phase_interferometer_7 = al.PhaseInterferometer( real_space_mask=mask_7x7, galaxies=[lens_galaxy], cosmology=cosmo.FLRW, sub_size=2, phase_name="test_phase", ) analysis = phase_interferometer_7.make_analysis( dataset=interferometer_7, mask=visibilities_mask_7x2) instance = phase_interferometer_7.model.instance_from_unit_vector([]) fit_figure_of_merit = analysis.fit(instance=instance) real_space_mask = phase_interferometer_7.meta_interferometer_fit.mask_with_phase_sub_size_from_mask( mask=mask_7x7) masked_interferometer = al.masked.interferometer( interferometer=interferometer_7, visibilities_mask=visibilities_mask_7x2, real_space_mask=real_space_mask, ) tracer = analysis.tracer_for_instance(instance=instance) fit = al.fit(masked_dataset=masked_interferometer, tracer=tracer) assert fit.likelihood == fit_figure_of_merit
def perform_fit_x2_lenses_with_source_galaxy_mask_and_border( source_galaxy, mask, inversion_uses_border): simulate_image_x2_lenses() masked_imaging = al.masked.imaging( imaging=imaging, mask=mask, inversion_uses_border=inversion_uses_border) lens_galaxy_0 = al.Galaxy( redshift=0.5, mass=al.mp.EllipticalIsothermal(centre=(1.1, 0.51), axis_ratio=0.9, phi=110.0, einstein_radius=1.07), ) lens_galaxy_1 = al.Galaxy( redshift=0.5, mass=al.mp.EllipticalIsothermal(centre=(-0.20, -0.35), axis_ratio=0.56, phi=16.0, einstein_radius=0.71), ) tracer = al.Tracer.from_galaxies( galaxies=[lens_galaxy_0, lens_galaxy_1, source_galaxy]) return al.fit(masked_dataset=masked_imaging, tracer=tracer)
def fit_with_offset_centre(centre): mask = al.mask.elliptical( shape_2d=imaging.shape_2d, pixel_scales=imaging.pixel_scales, major_axis_radius=3.0, axis_ratio=1.0, phi=0.0, centre=centre, ) # The lines of code below do everything we're used to, that is, setup an image and its grid, mask it, trace it # via a tracer, setup the rectangular mapper, etc. lens_galaxy = al.Galaxy( redshift=0.5, mass=al.mp.EllipticalIsothermal(centre=(2.0, 2.0), einstein_radius=1.2, axis_ratio=0.7, phi=45.0), ) source_galaxy = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiMagnification(shape=(20, 20)), regularization=al.reg.Constant(coefficient=1.0), ) masked_imaging = al.masked_imaging(imaging=imaging, mask=mask) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) fit = al.fit(masked_dataset=masked_imaging, tracer=tracer) return fit
def fit_masked_imaging_with_source_galaxy(masked_imaging, source_galaxy): lens_galaxy = al.Galaxy( redshift=0.5, mass=al.mp.EllipticalIsothermal(centre=(0.0, 0.0), axis_ratio=0.8, phi=45.0, einstein_radius=1.55), ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) return al.fit(masked_dataset=masked_imaging, tracer=tracer)
def perform_fit_with_lens__source_galaxy(lens_galaxy, source_galaxy): imaging = simulate() mask = al.mask.circular_annular( shape_2d=imaging.shape_2d, pixel_scales=imaging.pixel_scales, inner_radius=0.5, outer_radius=2.2, ) masked_imaging = al.masked.imaging(imaging=imaging, mask=mask) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) return al.fit(masked_dataset=masked_imaging, tracer=tracer)
def perform_fit_with_source_galaxy_mask_and_border(source_galaxy, mask, inversion_uses_border): imaging = simulate() masked_imaging = al.masked.imaging( imaging=imaging, mask=mask, inversion_uses_border=inversion_uses_border) lens_galaxy = al.Galaxy( redshift=0.5, mass=al.mp.EllipticalIsothermal(centre=(0.0, 0.0), axis_ratio=0.8, phi=135.0, einstein_radius=1.6), ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) return al.fit(masked_dataset=masked_imaging, tracer=tracer)
def fit_imaging_with_voronoi_magnification_pixelization( imaging, mask, regularization_coefficient): masked_imaging = al.masked.imaging(imaging=imaging, mask=mask) lens_galaxy = al.Galaxy( redshift=0.5, mass=al.mp.EllipticalIsothermal(centre=(0.0, 0.0), axis_ratio=0.8, phi=45.0, einstein_radius=1.6), ) source_galaxy = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiMagnification(shape=(30, 30)), regularization=al.reg.Constant(coefficient=regularization_coefficient), ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) return al.fit(masked_dataset=masked_imaging, tracer=tracer)
def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles( self, imaging_7x7, mask_7x7): lens_galaxy = al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=0.1)) phase_imaging_7x7 = al.PhaseImaging( galaxies=[lens_galaxy], cosmology=cosmo.FLRW, sub_size=1, phase_name="test_phase", ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7) instance = phase_imaging_7x7.model.instance_from_unit_vector([]) fit_figure_of_merit = analysis.fit(instance=instance) masked_imaging = al.masked_imaging(imaging=imaging_7x7, mask=mask_7x7) tracer = analysis.tracer_for_instance(instance=instance) fit = al.fit(masked_dataset=masked_imaging, tracer=tracer) assert fit.likelihood == fit_figure_of_merit
def perform_fit_with_source_galaxy(source_galaxy): imaging = simulate() mask = al.mask.circular_annular( shape_2d=imaging.shape_2d, pixel_scales=imaging.pixel_scales, inner_radius=0.5, outer_radius=2.2, ) masked_imaging = al.masked.imaging(imaging=imaging, mask=mask) lens_galaxy = al.Galaxy( redshift=0.5, mass=al.mp.EllipticalIsothermal(centre=(0.0, 0.0), axis_ratio=0.8, phi=135.0, einstein_radius=1.6), ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) return al.fit(masked_dataset=masked_imaging, tracer=tracer)
print("Time to compute reguarization matrix = {}".format(diff / repeats)) start = time.time() for i in range(repeats): curvature_reg_matrix = np.add(curvature_matrix, regularization_matrix) diff = time.time() - start print("Time to compute curvature reguarization Matrix = {}".format( diff / repeats)) start = time.time() for i in range(repeats): reconstruction = np.linalg.solve(curvature_reg_matrix, data_vector) diff = time.time() - start print("Time to peform reconstruction = {}".format(diff / repeats)) start = time.time() for i in range(repeats): al.util.inversion.mapped_reconstructed_data_from_mapping_matrix_and_reconstruction( mapping_matrix=blurred_mapping_matrix, reconstruction=reconstruction) diff = time.time() - start print("Time to compute mapped reconstruction = {}".format(diff / repeats)) start = time.time() for i in range(repeats): al.fit(masked_dataset=masked_imaging, tracer=tracer) diff = time.time() - start print("Time to perform complete fit = {}".format(diff / repeats)) print()
for i in range(repeats): reconstruction = np.linalg.solve(curvature_reg_matrix, data_vector) diff = time.time() - start print("Time to peform reconstruction = {}".format(diff / repeats)) start = time.time() for i in range(repeats): real_mapped_visibilities = al.util.inversion.mapped_reconstructed_data_from_mapping_matrix_and_reconstruction( mapping_matrix=transformed_mapping_matrices[0], reconstruction=reconstruction) diff = time.time() - start print("Time to compute mapped real visibility reconstruction = {}".format( diff / repeats)) start = time.time() for i in range(repeats): imag_mapped_visibilities = al.util.inversion.mapped_reconstructed_data_from_mapping_matrix_and_reconstruction( mapping_matrix=transformed_mapping_matrices[1], reconstruction=reconstruction) diff = time.time() - start print("Time to compute mapped imaginary visibility reconstruction = {}".format( diff / repeats)) start = time.time() for i in range(repeats): al.fit(masked_dataset=masked_interferometer, tracer=tracer) diff = time.time() - start print("Time to perform complete fit = {}".format(diff / repeats)) print()
def test__simulate_imaging_data_and_fit__include_psf_blurring__chi_squared_is_0__noise_normalization_correct( ): psf = al.kernel.from_gaussian(shape_2d=(3, 3), pixel_scales=0.2, sigma=0.75) grid = al.grid.uniform(shape_2d=(11, 11), pixel_scales=0.2, sub_size=1) lens_galaxy = al.Galaxy( redshift=0.5, light=al.lp.EllipticalSersic(centre=(0.1, 0.1), intensity=0.1), mass=al.mp.EllipticalIsothermal(centre=(0.1, 0.1), einstein_radius=1.8), ) source_galaxy = al.Galaxy( redshift=1.0, light=al.lp.EllipticalExponential(centre=(0.1, 0.1), intensity=0.5), ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) imaging_simulator = simulator.ImagingSimulator( shape_2d=(11, 11), pixel_scales=0.2, sub_size=1, exposure_time=300.0, psf=psf, background_level=0.0, add_noise=False, ) imaging_simulated = imaging_simulator.from_image( image=tracer.padded_profile_image_from_grid_and_psf_shape( grid=grid, psf_shape_2d=psf.shape_2d)) imaging_simulated.noise_map = al.array.ones( shape_2d=imaging_simulated.image.shape_2d) path = "{}/data_temp/simulate_and_fit".format( os.path.dirname(os.path.realpath( __file__))) # Setup path so we can output the simulated image. try: shutil.rmtree(path) except FileNotFoundError: pass if os.path.exists(path) is False: os.makedirs(path) imaging_simulated.output_to_fits( image_path=path + "/image.fits", noise_map_path=path + "/noise_map.fits", psf_path=path + "/psf.fits", ) imaging = al.imaging.from_fits( image_path=path + "/image.fits", noise_map_path=path + "/noise_map.fits", psf_path=path + "/psf.fits", pixel_scales=0.2, ) mask = al.mask.circular(shape_2d=imaging.image.shape_2d, pixel_scales=0.2, sub_size=1, radius=0.8) masked_imaging = al.masked.imaging(imaging=imaging, mask=mask) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) fit = al.fit(masked_dataset=masked_imaging, tracer=tracer) assert fit.chi_squared == pytest.approx(0.0, 1e-4) path = "{}/data_temp".format(os.path.dirname(os.path.realpath( __file__))) # Setup path so we can output the simulated image. if os.path.exists(path) == True: shutil.rmtree(path)
def test__simulate_interferometer_data_and_fit__chi_squared_is_0__noise_normalization_correct( ): lens_galaxy = al.Galaxy( redshift=0.5, light=al.lp.EllipticalSersic(centre=(0.1, 0.1), intensity=0.1), mass=al.mp.EllipticalIsothermal(centre=(0.1, 0.1), einstein_radius=1.0), ) source_galaxy = al.Galaxy( redshift=1.0, light=al.lp.EllipticalExponential(centre=(0.1, 0.1), intensity=0.5), ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) interferometer_simulator = simulator.InterferometerSimulator( real_space_shape_2d=(51, 51), real_space_pixel_scales=0.1, uv_wavelengths=np.ones(shape=(7, 2)), sub_size=2, exposure_time=300.0, background_level=0.0, noise_if_add_noise_false=1.0, noise_sigma=None, ) interferometer_simulated = interferometer_simulator.from_tracer( tracer=tracer) path = "{}/data_temp/simulate_and_fit".format( os.path.dirname(os.path.realpath( __file__))) # Setup path so we can output the simulated image. try: shutil.rmtree(path) except FileNotFoundError: pass if os.path.exists(path) is False: os.makedirs(path) interferometer_simulated.output_to_fits( visibilities_path=path + "/visibilities.fits", noise_map_path=path + "/noise_map.fits", uv_wavelengths_path=path + "/uv_wavelengths.fits", ) interferometer = al.interferometer.from_fits( visibilities_path=path + "/visibilities.fits", noise_map_path=path + "/noise_map.fits", uv_wavelengths_path=path + "/uv_wavelengths.fits", ) visibilities_mask = np.full(fill_value=False, shape=(7, 2)) real_space_mask = al.mask.unmasked(shape_2d=(51, 51), pixel_scales=0.1, sub_size=2) masked_interferometer = al.masked.interferometer( interferometer=interferometer, visibilities_mask=visibilities_mask, real_space_mask=real_space_mask, inversion_uses_border=False, ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) fit = al.fit(masked_dataset=masked_interferometer, tracer=tracer) assert fit.chi_squared == 0.0 pix = al.pix.Rectangular(shape=(7, 7)) reg = al.reg.Constant(coefficient=0.0001) lens_galaxy = al.Galaxy( redshift=0.5, light=al.lp.EllipticalSersic(centre=(0.1, 0.1), intensity=0.1), mass=al.mp.EllipticalIsothermal(centre=(0.1, 0.1), einstein_radius=1.0), ) source_galaxy = al.Galaxy(redshift=1.0, pixelization=pix, regularization=reg) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) fit = al.fit(masked_dataset=masked_interferometer, tracer=tracer) assert abs(fit.chi_squared) < 1.0e-4 path = "{}/data_temp".format(os.path.dirname(os.path.realpath( __file__))) # Setup path so we can output the simulated image. if os.path.exists(path) == True: shutil.rmtree(path)
def fit_masked_imaging_with_lens_and_source_galaxy(masked_imaging, lens_galaxy, source_galaxy): tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) return al.fit(masked_dataset=masked_imaging, tracer=tracer)
# fitting it therefore increasing or decreasing the background sky level in the image .This means we can account for # an inaccurate background sky subtraction in our dataset reduction during PyAutoLens model fitting. # We can also scale the background noise in an analogous fashion, using the HyperBackgroundNoise class and the # 'noise_scale' hyper-galaxy-parameter. This value is added to every pixel in the noise-map. hyper_background_noise = al.hyper_data.HyperBackgroundNoise(noise_scale=1.0) # To use these hyper-galaxy_data parameters, we pass them to a lens-fit just like we do our tracer. tracer = al.Tracer.from_galaxies( galaxies=[lens_galaxy_hyper, source_magnification_hyper]) al.fit( masked_dataset=masked_imaging, tracer=tracer, hyper_image_sky=hyper_image_sky, hyper_background_noise=hyper_background_noise, ) aplt.fit_imaging.subplot_fit_imaging( fit=fit, include=aplt.Include(inversion_image_pixelization_grid=True, mask=True)) # Is there any reason to scale the background noise other than if the background sky subtraction has a large correction? # There is. Lots of pixels in an image do not contain the lensed source but are fitted by the inversion. As we've learnt # in this chapter, this isn't problematic when we have our adaptive regularization scheme because the regularization # coefficient will be increased to large values. # However, if you ran a full PyAutoLens analysis in hyper-galaxy-mode (which we cover in the next tutorial), you'd find the # method still dedicates a lot of source-pixels to fit these regions of the image, __even though they have no source__. # Why is this? Its because although these pixels have no source, they still have a relatively high S/N values
source_galaxy = al.Galaxy( redshift=1.0, light=al.lp.EllipticalSersic( centre=(0.0, 0.0), axis_ratio=0.5, phi=90.0, intensity=0.03, effective_radius=0.3, sersic_index=3.0, ), ) tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) # Now, lets fit the lens-data with the tracer and plotters the fit. It looks a lot better than above, doesn't it? correct_fit = al.fit(masked_dataset=masked_imaging, tracer=tracer) aplt.fit_imaging.subplot_fit_imaging(fit=correct_fit) # Finally, just to be sure, lets compare the two likelihoods. print("Likelihood of Non-linear Search:") print(results.most_likely_fit.likelihood) print("Likelihood of Correct Model:") print(correct_fit.likelihood) # Well, there we have it, the input model has a much higher likelihood than the one our non-linear search inferred. # Clearly, our non-linear search failed. So, what happened? Where did it all go wrong? # Lets think about 'complexity'. As we made our lens model more realistic, we also made it more complex. Our # non-linear parameter space went from 7 dimensions to 18. This means there was a much larger 'volume' of parameter