def __init__(self, lens_data, tracer, padded_tracer): """ An abstract lens profile fitter, which generates the image-plane image of all galaxies (with light \ profiles) in the tracer and blurs it with the lens data's PSF. If a padded tracer is supplied, the blurred profile image's can be generated over the entire image and thus \ without the mask. Parameters ----------- lens_data : lens_data.LensData The lens-image that is fitted. tracer : ray_tracing.AbstractTracerNonStack The tracer, which describes the ray-tracing and strong lens configuration. padded_tracer : ray_tracing.Tracer or None A tracer with an identical strong lens configuration to the tracer above, but using the lens data's \ padded grid_stack such that unmasked model-images can be computed. """ super(AbstractLensProfileFit, self).__init__(tracer=tracer, padded_tracer=padded_tracer, psf=lens_data.psf, map_to_scaled_array=lens_data.map_to_scaled_array) self.convolver_image = lens_data.convolver_image blurred_profile_image_1d = util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.image_plane_image_1d, blurring_image_1d=tracer.image_plane_blurring_image_1d, convolver=self.convolver_image) self.blurred_profile_image = self.map_to_scaled_array( array_1d=blurred_profile_image_1d)
def blurred_images_1d_of_images_from_1d_unblurred_and_bluring_images( unblurred_images_1d, blurring_images_1d, convolvers): """For a list of 1D masked images and 1D blurring images (the regions outside the masks whose light blurs \ into the masks after PSF convolution), use both to compute the blurred image within the masks via PSF convolution. The convolution uses each image's convolver (*See ccd.convolution*). Parameters ---------- unblurred_images_1d : [ndarray] List of the 1D masked images which are blurred. blurring_images_1d : [ndarray] List of the 1D masked blurring images which are used for blurring. convolvers : [ccd.convolution.ConvolverImage] List of the image-convolvers which perform the convolutions in 1D. """ blurred_images = [] for image_index in range(len(unblurred_images_1d)): blurred_profile_image = lens_fit_util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=unblurred_images_1d[image_index], blurring_image_1d=blurring_images_1d[image_index], convolver=convolvers[image_index]) blurred_images.append(blurred_profile_image) return blurred_images
def test__tracer_and_tracer_sensitive_are_identical__added__likelihood_is_noise_term( self, lens_data_blur): g0 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0)) g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=2.0)) tracer = ray_tracing.TracerImageSourcePlanes( lens_galaxies=[g0], source_galaxies=[g1], image_plane_grid_stack=lens_data_blur.grid_stack) fit = sensitivity_fit.SensitivityProfileFit(lens_data=lens_data_blur, tracer_normal=tracer, tracer_sensitive=tracer) assert (fit.fit_normal.image == lens_data_blur.image).all() assert (fit.fit_normal.noise_map == lens_data_blur.noise_map).all() model_image_1d = util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.image_plane_image_1d, blurring_image_1d=tracer.image_plane_blurring_image_1d, convolver=lens_data_blur.convolver_image) model_image = lens_data_blur.map_to_scaled_array( array_1d=model_image_1d) assert (fit.fit_normal.model_image == model_image).all() residual_map = fit_util.residual_map_from_data_mask_and_model_data( data=lens_data_blur.image, mask=lens_data_blur.mask, model_data=model_image) assert (fit.fit_normal.residual_map == residual_map).all() chi_squared_map = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask( residual_map=residual_map, mask=lens_data_blur.mask, noise_map=lens_data_blur.noise_map) assert (fit.fit_normal.chi_squared_map == chi_squared_map).all() assert (fit.fit_sensitive.image == lens_data_blur.image).all() assert (fit.fit_sensitive.noise_map == lens_data_blur.noise_map).all() assert (fit.fit_sensitive.model_image == model_image).all() assert (fit.fit_sensitive.residual_map == residual_map).all() assert (fit.fit_sensitive.chi_squared_map == chi_squared_map).all() chi_squared = fit_util.chi_squared_from_chi_squared_map_and_mask( chi_squared_map=chi_squared_map, mask=lens_data_blur.mask) noise_normalization = fit_util.noise_normalization_from_noise_map_and_mask( mask=lens_data_blur.mask, noise_map=lens_data_blur.noise_map) assert fit.fit_normal.likelihood == -0.5 * (chi_squared + noise_normalization) assert fit.fit_sensitive.likelihood == -0.5 * (chi_squared + noise_normalization) assert fit.figure_of_merit == 0.0
def __init__(self, lens_data, noise_map_1d, tracer, padded_tracer): """ An abstract lens profile and inversion fitter, which first generates and subtracts the image-plane \ image of all galaxies (with light profiles) in the tracer, blurs it with the PSF and fits the residual image \ with an inversion using the mapper(s) and regularization(s) in the galaxy's of the tracer. This inversion use's the lens-image, its PSF and an input noise-map. If a padded tracer is supplied, the blurred profile image's can be generated over the entire image and thus \ without the mask. Parameters ----------- lens_data : lens_data.LensData The lens-image that is fitted. noise_map_1d : ndarray The 1D noise_map map that is fitted, which is an input variable so a hyper-noise_map map can be used (see \ *AbstractHyperFitter*). tracer : ray_tracing.Tracer The tracer, which describes the ray-tracing and strong lens configuration. padded_tracer : ray_tracing.AbstractTracerNonStack or None A tracer with an identical strong lens configuration to the tracer above, but using the lens data's \ padded grid_stack such that unmasked model-images can be computed. """ super(AbstractLensProfileInversionFit, self).__init__(tracer=tracer, padded_tracer=padded_tracer, psf=lens_data.psf, map_to_scaled_array=lens_data.map_to_scaled_array) self.psf = lens_data.psf self.convolver_image = lens_data.convolver_image blurred_profile_image_1d = util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.image_plane_image_1d, blurring_image_1d=tracer.image_plane_blurring_image_1d, convolver=lens_data.convolver_image) self.blurred_profile_image = self.map_to_scaled_array( array_1d=blurred_profile_image_1d) profile_subtracted_image_1d = lens_data.image_1d - blurred_profile_image_1d self.profile_subtracted_image = lens_data.image - self.blurred_profile_image self.inversion = inversions.inversion_from_image_mapper_and_regularization( image_1d=profile_subtracted_image_1d, noise_map_1d=noise_map_1d, convolver=lens_data.convolver_mapping_matrix, mapper=tracer.mappers_of_planes[-1], regularization=tracer.regularizations_of_planes[-1])
def test___manual_image_and_psf(self, lens_data_stack_manual): g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=1.0)) g1 = g.Galaxy(mass_profile=mp.SphericalIsothermal( einstein_radius=1.0)) tracer = ray_tracing_stack.TracerImageSourcePlanesStack( lens_galaxies=[g0, g1], source_galaxies=[g0], image_plane_grid_stacks=lens_data_stack_manual.grid_stacks) padded_tracer = ray_tracing_stack.TracerImageSourcePlanesStack( lens_galaxies=[g0, g1], source_galaxies=[g0], image_plane_grid_stacks=lens_data_stack_manual. padded_grid_stacks) fit = lens_fit_stack.fit_lens_image_stack_with_tracer( lens_data_stack=lens_data_stack_manual, tracer=tracer, padded_tracer=padded_tracer) assert lens_data_stack_manual.noise_maps[0] == pytest.approx( fit.noise_maps[0], 1e-4) assert lens_data_stack_manual.noise_maps[1] == pytest.approx( fit.noise_maps[1], 1e-4) model_image_1d_0 = lens_fit_util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.image_plane_images_1d[0], blurring_image_1d=tracer.image_plane_blurring_images_1d[0], convolver=lens_data_stack_manual.convolvers_image[0]) model_image_1d_1 = lens_fit_util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.image_plane_images_1d[1], blurring_image_1d=tracer.image_plane_blurring_images_1d[1], convolver=lens_data_stack_manual.convolvers_image[1]) model_image_0 = lens_data_stack_manual.map_to_scaled_arrays[0]( array_1d=model_image_1d_0) model_image_1 = lens_data_stack_manual.map_to_scaled_arrays[1]( array_1d=model_image_1d_1) assert model_image_0 == pytest.approx(fit.model_images[0], 1e-4) assert model_image_1 == pytest.approx(fit.model_images[1], 1e-4) residual_map_0 = fit_util.residual_map_from_data_mask_and_model_data( data=lens_data_stack_manual.images[0], mask=lens_data_stack_manual.masks[0], model_data=model_image_0) residual_map_1 = fit_util.residual_map_from_data_mask_and_model_data( data=lens_data_stack_manual.images[1], mask=lens_data_stack_manual.masks[1], model_data=model_image_1) assert residual_map_0 == pytest.approx(fit.residual_maps[0], 1e-4) assert residual_map_1 == pytest.approx(fit.residual_maps[1], 1e-4) chi_squared_map_0 = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask( residual_map=residual_map_0, mask=lens_data_stack_manual.masks[0], noise_map=lens_data_stack_manual.noise_maps[0]) chi_squared_map_1 = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask( residual_map=residual_map_1, mask=lens_data_stack_manual.masks[1], noise_map=lens_data_stack_manual.noise_maps[1]) assert chi_squared_map_0 == pytest.approx(fit.chi_squared_maps[0], 1e-4) assert chi_squared_map_1 == pytest.approx(fit.chi_squared_maps[1], 1e-4) chi_squared_0 = fit_util.chi_squared_from_chi_squared_map_and_mask( chi_squared_map=chi_squared_map_0, mask=lens_data_stack_manual.masks[0]) noise_normalization_0 = fit_util.noise_normalization_from_noise_map_and_mask( noise_map=lens_data_stack_manual.noise_maps[0], mask=lens_data_stack_manual.masks[0]) likelihood_0 = fit_util.likelihood_from_chi_squared_and_noise_normalization( chi_squared=chi_squared_0, noise_normalization=noise_normalization_0) chi_squared_1 = fit_util.chi_squared_from_chi_squared_map_and_mask( chi_squared_map=chi_squared_map_1, mask=lens_data_stack_manual.masks[1]) noise_normalization_1 = fit_util.noise_normalization_from_noise_map_and_mask( noise_map=lens_data_stack_manual.noise_maps[1], mask=lens_data_stack_manual.masks[1]) likelihood_1 = fit_util.likelihood_from_chi_squared_and_noise_normalization( chi_squared=chi_squared_1, noise_normalization=noise_normalization_1) assert fit.chi_squareds == [chi_squared_0, chi_squared_1] assert fit.noise_normalizations == [ noise_normalization_0, noise_normalization_1 ] assert fit.likelihoods == [likelihood_0, likelihood_1] assert likelihood_0 + likelihood_1 == pytest.approx( fit.likelihood, 1e-4) assert likelihood_0 + likelihood_1 == fit.figure_of_merit blurred_image_of_planes_0 = lens_fit_util.blurred_image_of_planes_from_1d_images_and_convolver( total_planes=tracer.total_planes, image_plane_image_1d_of_planes=tracer. image_plane_images_1d_of_planes[0], image_plane_blurring_image_1d_of_planes=tracer. image_plane_blurring_images_1d_of_planes[0], convolver=lens_data_stack_manual.convolvers_image[0], map_to_scaled_array=lens_data_stack_manual. map_to_scaled_arrays[0]) blurred_image_of_planes_1 = lens_fit_util.blurred_image_of_planes_from_1d_images_and_convolver( total_planes=tracer.total_planes, image_plane_image_1d_of_planes=tracer. image_plane_images_1d_of_planes[1], image_plane_blurring_image_1d_of_planes=tracer. image_plane_blurring_images_1d_of_planes[1], convolver=lens_data_stack_manual.convolvers_image[1], map_to_scaled_array=lens_data_stack_manual. map_to_scaled_arrays[1]) assert (blurred_image_of_planes_0[0] == fit.model_images_of_planes[0][0]).all() assert (blurred_image_of_planes_0[1] == fit.model_images_of_planes[0][1]).all() assert (blurred_image_of_planes_1[0] == fit.model_images_of_planes[1][0]).all() assert (blurred_image_of_planes_1[1] == fit.model_images_of_planes[1][1]).all() unmasked_blurred_image_0 = \ lens_fit_util.unmasked_blurred_image_from_padded_grid_stack_psf_and_unmasked_image( padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[0], psf=lens_data_stack_manual.psfs[0], unmasked_image_1d=padded_tracer.image_plane_images_1d[0]) unmasked_blurred_image_1 = \ lens_fit_util.unmasked_blurred_image_from_padded_grid_stack_psf_and_unmasked_image( padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[1], psf=lens_data_stack_manual.psfs[1], unmasked_image_1d=padded_tracer.image_plane_images_1d[1]) assert (unmasked_blurred_image_0 == fit.unmasked_model_images[0] ).all() assert (unmasked_blurred_image_1 == fit.unmasked_model_images[1] ).all() unmasked_blurred_image_of_galaxies_i0 = \ lens_fit_util.unmasked_blurred_image_of_galaxies_from_psf_and_unmasked_1d_galaxy_images( galaxies=padded_tracer.image_plane.galaxies, image_plane_image_1d_of_galaxies=padded_tracer.image_plane.image_plane_images_1d_of_galaxies[0], padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[0], psf=lens_data_stack_manual.psfs[0]) unmasked_blurred_image_of_galaxies_s0 = \ lens_fit_util.unmasked_blurred_image_of_galaxies_from_psf_and_unmasked_1d_galaxy_images( galaxies=padded_tracer.source_plane.galaxies, image_plane_image_1d_of_galaxies=padded_tracer.source_plane.image_plane_images_1d_of_galaxies[0], padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[0], psf=lens_data_stack_manual.psfs[0]) unmasked_blurred_image_of_galaxies_i1 = \ lens_fit_util.unmasked_blurred_image_of_galaxies_from_psf_and_unmasked_1d_galaxy_images( galaxies=padded_tracer.image_plane.galaxies, image_plane_image_1d_of_galaxies=padded_tracer.image_plane.image_plane_images_1d_of_galaxies[0], padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[1], psf=lens_data_stack_manual.psfs[1]) unmasked_blurred_image_of_galaxies_s1 = \ lens_fit_util.unmasked_blurred_image_of_galaxies_from_psf_and_unmasked_1d_galaxy_images( galaxies=padded_tracer.source_plane.galaxies, image_plane_image_1d_of_galaxies=padded_tracer.source_plane.image_plane_images_1d_of_galaxies[0], padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[1], psf=lens_data_stack_manual.psfs[1]) assert ( unmasked_blurred_image_of_galaxies_i0[0] == fit. unmasked_model_images_of_planes_and_galaxies[0][0][0]).all() assert ( unmasked_blurred_image_of_galaxies_s0[0] == fit. unmasked_model_images_of_planes_and_galaxies[0][1][0]).all() assert ( unmasked_blurred_image_of_galaxies_i1[0] == fit. unmasked_model_images_of_planes_and_galaxies[1][0][0]).all() assert ( unmasked_blurred_image_of_galaxies_s1[0] == fit. unmasked_model_images_of_planes_and_galaxies[1][1][0]).all()
start_overall = time.time() start = time.time() for i in range(repeats): tracer = ray_tracing.TracerImageSourcePlanes( lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy], image_plane_grid_stack=lens_data.grid_stack) diff = time.time() - start print("Time to Setup Tracer = {}".format(diff / repeats)) start = time.time() for i in range(repeats): blurred_profile_image_1d = lens_fit_util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.image_plane_image_1d, blurring_image_1d=tracer.image_plane_blurring_image_1d, convolver=lens_data.convolver_image) blurred_profile_image = lens_data.map_to_scaled_array( array_1d=blurred_profile_image_1d) diff = time.time() - start print("Time to perform PSF convolution = {}".format(diff / repeats)) start = time.time() for i in range(repeats): lens_fit.LensDataFit(image=lens_data.image_1d, noise_map=lens_data.noise_map_1d, mask=lens_data.mask_1d, model_image=blurred_profile_image_1d) diff = time.time() - start print("Time to perform fit (1D) = {}".format(diff / repeats))
"\n") print("Number of points = " + str(lens_data.grid.shape[0]) + "\n") start_overall = time.time() start = time.time() for i in range(repeats): tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy]) diff = time.time() - start print("Time to Setup Tracer = {}".format(diff / repeats)) start = time.time() for i in range(repeats): blurred_profile_image_1d = lens_fit_util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.profile_image_from_grid, blurring_image_1d=tracer.profile_blurring_image, convolver=lens_data.convolver, ) blurred_profile_image = lens_data.grid.mapping.scaled_array_2d_from_array_1d( array_1d=blurred_profile_image_1d) diff = time.time() - start print("Time to perform PSF convolution = {}".format(diff / repeats)) start = time.time() for i in range(repeats): al.LensDataFit( image_1d=lens_data.image_1d, noise_map_1d=lens_data.noise_map_1d, mask_1d=lens_data.mask_1d, model_image_1d=blurred_profile_image_1d, )