def test__grid_classes_input__used_in_masked_imaging( self, imaging_7x7, mask_7x7): phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch(), settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( grid_inversion_class=al.Grid2D)), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert isinstance(analysis.masked_imaging.grid, al.Grid2D) assert isinstance(analysis.masked_imaging.grid_inversion, al.Grid2D) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch(), settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( grid_class=al.Grid2DIterate, grid_inversion_class=al.Grid2DIterate, fractional_accuracy=0.2, sub_steps=[2, 3], )), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert isinstance(analysis.masked_imaging.grid, al.Grid2DIterate) assert analysis.masked_imaging.grid.fractional_accuracy == 0.2 assert analysis.masked_imaging.grid.sub_steps == [2, 3] assert isinstance(analysis.masked_imaging.grid_inversion, al.Grid2DIterate) assert analysis.masked_imaging.grid_inversion.fractional_accuracy == 0.2 assert analysis.masked_imaging.grid_inversion.sub_steps == [2, 3] phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch(), settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( grid_class=al.Grid2DInterpolate, grid_inversion_class=al.Grid2DInterpolate, pixel_scales_interp=0.1, )), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert isinstance(analysis.masked_imaging.grid, al.Grid2DInterpolate) assert analysis.masked_imaging.grid.pixel_scales_interp == (0.1, 0.1) assert isinstance(analysis.masked_imaging.grid_inversion, al.Grid2DInterpolate) assert analysis.masked_imaging.grid_inversion.pixel_scales_interp == ( 0.1, 0.1)
def test__masked_imaging__settings_inputs_are_used_in_masked_imaging( self, imaging_7x7, mask_7x7): phase_imaging_7x7 = al.PhaseImaging( settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( grid_class=al.Grid, grid_inversion_class=al.Grid, sub_size=3, signal_to_noise_limit=1.0, bin_up_factor=2, psf_shape_2d=(3, 3), ), settings_pixelization=al.SettingsPixelization( use_border=False, is_stochastic=True), ), search=mock.MockSearch(), ) assert phase_imaging_7x7.settings.settings_masked_imaging.sub_size == 3 assert (phase_imaging_7x7.settings.settings_masked_imaging. signal_to_noise_limit == 1.0) assert phase_imaging_7x7.settings.settings_masked_imaging.bin_up_factor == 2 assert phase_imaging_7x7.settings.settings_masked_imaging.psf_shape_2d == ( 3, 3) assert phase_imaging_7x7.settings.settings_pixelization.use_border == False assert phase_imaging_7x7.settings.settings_pixelization.is_stochastic == True analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert isinstance(analysis.masked_dataset.grid, al.Grid) assert isinstance(analysis.masked_dataset.grid_inversion, al.Grid) phase_imaging_7x7 = al.PhaseImaging( settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( grid_class=al.GridIterate, sub_size=3, fractional_accuracy=0.99, sub_steps=[2], )), search=mock.MockSearch(), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert isinstance(analysis.masked_dataset.grid, al.GridIterate) assert analysis.masked_dataset.grid.sub_size == 1 assert analysis.masked_dataset.grid.fractional_accuracy == 0.99 assert analysis.masked_dataset.grid.sub_steps == [2]
def test__results_of_phase_include_pixelization__available_as_property( self, imaging_7x7, mask_7x7 ): lens = al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=1.0)) source = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiMagnification(shape=(2, 3)), regularization=al.reg.Constant(), ) tracer = al.Tracer.from_galaxies(galaxies=[lens, source]) samples = mock.MockSamples(max_log_likelihood_instance=tracer) phase_imaging_7x7 = al.PhaseImaging( phase_name="test_phase", settings=al.SettingsPhaseImaging(), search=mock.MockSearch(samples=samples), ) result = phase_imaging_7x7.run( dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults() ) assert isinstance(result.pixelization, al.pix.VoronoiMagnification) assert result.pixelization.shape == (2, 3) lens = al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=1.0)) source = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiBrightnessImage(pixels=6), regularization=al.reg.Constant(), ) source.hyper_galaxy_image = np.ones(9) tracer = al.Tracer.from_galaxies(galaxies=[lens, source]) samples = mock.MockSamples(max_log_likelihood_instance=tracer) phase_imaging_7x7 = al.PhaseImaging( phase_name="test_phase", settings=al.SettingsPhaseImaging(), search=mock.MockSearch(samples=samples), ) result = phase_imaging_7x7.run( dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults() ) assert isinstance(result.pixelization, al.pix.VoronoiBrightnessImage) assert result.pixelization.pixels == 6
def test__positions_are_input__are_used_in_analysis( self, image_7x7, noise_map_7x7, mask_7x7): # If position threshold is input (not None) and positions are input, make the positions part of the lens dataset. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridCoordinates([[(1.0, 1.0), (2.0, 2.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( phase_name="test_phase", search=mock.MockSearch(), settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens( positions_threshold=0.2)), ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=mock.MockResults()) phase_imaging_7x7.modify_settings(dataset=imaging_7x7, results=mock.MockResults()) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert (analysis.masked_dataset.positions.in_list[0][0] == np.array( [1.0, 1.0])).all() assert (analysis.masked_dataset.positions.in_list[0][1] == np.array( [2.0, 2.0])).all() assert analysis.settings.settings_lens.positions_threshold == 0.2 # If position threshold is input (not None) and but no positions are supplied, raise an error with pytest.raises(exc.PhaseException): imaging_7x7 = al.Imaging(image=image_7x7, noise_map=noise_map_7x7, positions=None) phase_imaging_7x7 = al.PhaseImaging( phase_name="test_phase", search=mock.MockSearch(), settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens( positions_threshold=0.2)), ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=mock.MockResults()) phase_imaging_7x7.modify_settings(dataset=imaging_7x7, results=mock.MockResults())
def test__masked_imaging_is_binned_up(self, imaging_7x7, mask_7x7_1_pix): binned_up_imaging = imaging_7x7.binned_up_from(bin_up_factor=2) binned_up_mask = mask_7x7_1_pix.binned_mask_from_bin_up_factor( bin_up_factor=2) phase_imaging_7x7 = al.PhaseImaging( settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( bin_up_factor=2)), search=mock.MockSearch(), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7_1_pix, results=mock.MockResults()) assert (analysis.masked_dataset.image.in_2d == binned_up_imaging.image.in_2d * np.invert(binned_up_mask)).all() assert (analysis.masked_dataset.psf == (1.0 / 9.0) * binned_up_imaging.psf).all() assert (analysis.masked_dataset.noise_map.in_2d == binned_up_imaging.noise_map.in_2d * np.invert(binned_up_mask)).all() assert (analysis.masked_dataset.mask == binned_up_mask).all()
def test__results_of_phase_include_positions__available_as_property( self, imaging_7x7, mask_7x7, samples_with_result): phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase", samples=samples_with_result)) result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert result.positions == None phase_imaging_7x7 = al.PhaseImaging( galaxies=dict(lens=al.Galaxy(redshift=0.5), source=al.Galaxy(redshift=1.0)), search=mock.MockSearch("test_phase", samples=samples_with_result), settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens( positions_threshold=1.0)), ) imaging_7x7.positions = al.GridIrregularGrouped([[(1.0, 1.0)]]) result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert (result.positions[0] == np.array([1.0, 1.0])).all()
def test__positions_are_input__are_used_in_analysis( self, image_7x7, noise_map_7x7, mask_7x7): # If position threshold is input (not None) and positions are input, make the positions part of the lens dataset. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 1.0), (2.0, 2.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens( positions_threshold=0.2)), ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=mock.MockResults()) phase_imaging_7x7.modify_settings(dataset=imaging_7x7, results=mock.MockResults()) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert (analysis.masked_dataset.positions.in_grouped_list[0][0] == np.array([1.0, 1.0])).all() assert (analysis.masked_dataset.positions.in_grouped_list[0][1] == np.array([2.0, 2.0])).all() assert analysis.settings.settings_lens.positions_threshold == 0.2
def test___image_dict(self, masked_imaging_7x7): galaxies = af.ModelInstance() galaxies.lens = al.Galaxy(redshift=0.5) galaxies.source = al.Galaxy(redshift=1.0) instance = af.ModelInstance() instance.galaxies = galaxies analysis = al.PhaseImaging.Analysis( masked_imaging=masked_imaging_7x7, settings=al.SettingsPhaseImaging(), results=mock.MockResults(), cosmology=cosmo.Planck15, ) result = al.PhaseImaging.Result( samples=mock.MockSamples(max_log_likelihood_instance=instance), previous_model=af.ModelMapper(), analysis=analysis, search=None, ) image_dict = result.image_galaxy_dict assert isinstance(image_dict[("galaxies", "lens")], np.ndarray) assert isinstance(image_dict[("galaxies", "source")], np.ndarray) result.instance.galaxies.lens = al.Galaxy(redshift=0.5) image_dict = result.image_galaxy_dict assert (image_dict[("galaxies", "lens")].native == np.zeros( (7, 7))).all() assert isinstance(image_dict[("galaxies", "source")], np.ndarray)
def test__masked_imaging_generator_from_aggregator(imaging_7x7, mask_7x7, samples): phase_imaging_7x7 = al.PhaseImaging( galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( grid_class=al.GridIterate, grid_inversion_class=al.GridInterpolate, fractional_accuracy=0.5, sub_steps=[2], pixel_scales_interp=0.1, ) ), search=mock.MockSearch("test_phase_aggregator", samples=samples), ) phase_imaging_7x7.run( dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults(samples=samples) ) agg = af.Aggregator(directory=phase_imaging_7x7.paths.output_path) masked_imaging_gen = al.agg.MaskedImaging(aggregator=agg) for masked_imaging in masked_imaging_gen: assert (masked_imaging.imaging.image == imaging_7x7.image).all() assert isinstance(masked_imaging.grid, al.GridIterate) assert isinstance(masked_imaging.grid_inversion, al.GridInterpolate) assert masked_imaging.grid.sub_steps == [2] assert masked_imaging.grid.fractional_accuracy == 0.5 assert masked_imaging.grid_inversion.pixel_scales_interp == (0.1, 0.1)
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=dict(lens=lens_galaxy), settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging(sub_size=1)), search=mock.MockSearch(), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) instance = phase_imaging_7x7.model.instance_from_unit_vector([]) fit_figure_of_merit = analysis.log_likelihood_function( instance=instance) masked_imaging = al.MaskedImaging( imaging=imaging_7x7, mask=mask_7x7, settings=al.SettingsMaskedImaging(sub_size=1), ) tracer = analysis.tracer_for_instance(instance=instance) fit = al.FitImaging(masked_imaging=masked_imaging, tracer=tracer) assert fit.log_likelihood == fit_figure_of_merit
def __init__(self): self.name = "name" self.paths = autofit.non_linear.paths.Paths(name=self.name, path_prefix="phase_path", tag="") self.search = mock.MockSearch(paths=self.paths) self.model = af.ModelMapper() self.settings = al.SettingsPhaseImaging(log_likelihood_cap=None) self.meta_dataset = MockMetaDataset(settings=self.settings)
def test__auto_einstein_radius_is_used__einstein_radius_used_in_analysis( self, imaging_7x7, mask_7x7): # If position threshold is input (not None) and positions are input, make the positions part of the lens dataset. phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens( auto_einstein_radius_factor=None)), ) tracer = mock.MockTracer(einstein_radius=2.0) phase_imaging_7x7.modify_settings( dataset=imaging_7x7, results=mock.MockResults(max_log_likelihood_tracer=tracer), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert analysis.settings.settings_lens.einstein_radius_estimate == None phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens( auto_einstein_radius_factor=1.0)), ) tracer = mock.MockTracer(einstein_radius=2.0) phase_imaging_7x7.modify_settings( dataset=imaging_7x7, results=mock.MockResults(max_log_likelihood_tracer=tracer), ) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults(max_log_likelihood_tracer=tracer), ) assert analysis.settings.settings_lens.einstein_radius_estimate == 2.0
def test__results_of_phase_include_pixelization_grid__available_as_property( self, imaging_7x7, mask_7x7): galaxy = al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=1.0)) tracer = al.Tracer.from_galaxies(galaxies=[galaxy]) samples = mock.MockSamples(max_log_likelihood_instance=tracer) phase_imaging_7x7 = al.PhaseImaging( galaxies=dict(lens=al.Galaxy(redshift=0.5), source=al.Galaxy(redshift=1.0)), search=mock.MockSearch("test_phase_2", samples=samples), ) result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert result.max_log_likelihood_pixelization_grids_of_planes == [None] lens = al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=1.0)) source = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiBrightnessImage(pixels=6), regularization=al.reg.Constant(), ) source.hyper_galaxy_image = np.ones(9) tracer = al.Tracer.from_galaxies(galaxies=[lens, source]) samples = mock.MockSamples(max_log_likelihood_instance=tracer) phase_imaging_7x7 = al.PhaseImaging( galaxies=dict(lens=al.Galaxy(redshift=0.5), source=al.Galaxy(redshift=1.0)), settings=al.SettingsPhaseImaging(), search=mock.MockSearch("test_phase_2", samples=samples), ) result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert result.max_log_likelihood_pixelization_grids_of_planes[ -1].shape == ( 6, 2, )
def make_pipeline(name, folders, search=af.DynestyStatic()): lens = al.GalaxyModel(redshift=0.5, mass=al.mp.EllipticalIsothermal) lens.mass.centre_0 = af.UniformPrior(lower_limit=-0.01, upper_limit=0.01) lens.mass.centre_1 = af.UniformPrior(lower_limit=-0.01, upper_limit=0.01) lens.mass.einstein_radius = af.UniformPrior(lower_limit=1.55, upper_limit=1.65) source = al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic) source.light.centre_0 = af.UniformPrior(lower_limit=-0.01, upper_limit=0.01) source.light.centre_1 = af.UniformPrior(lower_limit=-0.01, upper_limit=0.01) source.light.intensity = af.UniformPrior(lower_limit=0.35, upper_limit=0.45) source.light.effective_radius = af.UniformPrior(lower_limit=0.45, upper_limit=0.55) source.light.sersic_index = af.UniformPrior(lower_limit=0.9, upper_limit=1.1) class GridPhase( af.as_grid_search(phase_class=al.PhaseImaging, parallel=True)): @property def grid_priors(self): return [ self.model.galaxies.subhalo.mass.centre_0, self.model.galaxies.subhalo.mass.centre_1, ] subhalo = al.GalaxyModel(redshift=0.5, mass=al.mp.SphericalTruncatedNFWMCRLudlow) subhalo.mass.mass_at_200 = af.LogUniformPrior(lower_limit=1.0e6, upper_limit=1.0e11) subhalo.mass.centre_0 = af.UniformPrior(lower_limit=-2.5, upper_limit=2.5) subhalo.mass.centre_1 = af.UniformPrior(lower_limit=-2.5, upper_limit=2.5) phase1 = GridPhase( phase_name="phase_1", folders=folders, galaxies=dict(lens=lens, subhalo=subhalo, source=source), search=search, settings=al.SettingsPhaseImaging(), number_of_steps=2, ) return al.PipelineDataset(name, phase1)
def test__stochastic_histogram_for_instance(self, masked_interferometer_7): galaxies = af.ModelInstance() galaxies.lens = al.Galaxy( redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=1.2) ) galaxies.source = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiBrightnessImage(pixels=5), regularization=al.reg.Constant(), ) instance = af.ModelInstance() instance.galaxies = galaxies lens_hyper_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) lens_hyper_image[4] = 10.0 source_hyper_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) source_hyper_image[4] = 10.0 hyper_model_image = al.Array2D.full( fill_value=0.5, shape_native=(3, 3), pixel_scales=0.1 ) hyper_galaxy_image_path_dict = { ("galaxies", "lens"): lens_hyper_image, ("galaxies", "source"): source_hyper_image, } results = mock.MockResults( use_as_hyper_dataset=True, hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict, hyper_model_image=hyper_model_image, ) analysis = al.PhaseInterferometer.Analysis( masked_interferometer=masked_interferometer_7, settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens(stochastic_samples=2) ), results=results, cosmology=cosmo.Planck15, ) log_evidences = analysis.stochastic_log_evidences_for_instance( instance=instance ) assert len(log_evidences) == 2 assert log_evidences[0] != log_evidences[1]
def test__results_of_phase_include_mask__available_as_property( self, imaging_7x7, mask_7x7, samples_with_result): phase_imaging_7x7 = al.PhaseImaging( galaxies=dict(lens=al.Galaxy(redshift=0.5), source=al.Galaxy(redshift=1.0)), search=mock.MockSearch("test_phase", samples=samples_with_result), settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging(sub_size=2)), ) result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) assert (result.mask == mask_7x7).all()
def test__figure_of_merit__with_stochastic_likelihood_resamples_matches_galaxy_profiles( self, masked_imaging_7x7): galaxies = af.ModelInstance() galaxies.lens = al.Galaxy( redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=1.2)) galaxies.source = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiBrightnessImage(pixels=5), regularization=al.reg.Constant(), ) instance = af.ModelInstance() instance.galaxies = galaxies lens_hyper_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) lens_hyper_image[4] = 10.0 source_hyper_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) source_hyper_image[4] = 10.0 hyper_model_image = al.Array2D.full(fill_value=0.5, shape_native=(3, 3), pixel_scales=0.1) hyper_galaxy_image_path_dict = { ("galaxies", "lens"): lens_hyper_image, ("galaxies", "source"): source_hyper_image, } results = mock.MockResults( use_as_hyper_dataset=True, hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict, hyper_model_image=hyper_model_image, ) analysis = al.PhaseImaging.Analysis( masked_imaging=masked_imaging_7x7, settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens( stochastic_likelihood_resamples=2)), results=results, cosmology=cosmo.Planck15, ) fit_figure_of_merit = analysis.log_likelihood_function( instance=instance)
def test__paths(self): galaxy = al.Galaxy(mass=al.mp.SphericalIsothermal(), redshift=1.0) phase = al.PhaseImaging( galaxies=dict(galaxy=galaxy), search=af.DynestyStatic(name="test_phase", n_live_points=1), settings=al.SettingsPhaseImaging(), ) phase_stochastic = phase.extend_with_stochastic_phase( stochastic_search=af.DynestyStatic(n_live_points=1)) hyper_phase = phase_stochastic.make_hyper_phase() assert (path.join( "test_phase", "stochastic__settings__grid_sub_2", "dynesty_static__nlive_1", ) in hyper_phase.paths.output_path)
def test__figure_of_merit__includes_hyper_image_and_noise__matches_fit( self, imaging_7x7, mask_7x7): hyper_image_sky = al.hyper_data.HyperImageSky(sky_scale=1.0) hyper_background_noise = al.hyper_data.HyperBackgroundNoise( noise_scale=1.0) lens_galaxy = al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=0.1)) phase_imaging_7x7 = al.PhaseImaging( galaxies=dict(lens=lens_galaxy), hyper_image_sky=hyper_image_sky, hyper_background_noise=hyper_background_noise, settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging(sub_size=4)), search=mock.MockSearch(), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) instance = phase_imaging_7x7.model.instance_from_unit_vector([]) fit_figure_of_merit = analysis.log_likelihood_function( instance=instance) assert analysis.masked_dataset.mask.sub_size == 4 masked_imaging = al.MaskedImaging( imaging=imaging_7x7, mask=mask_7x7, settings=al.SettingsMaskedImaging(sub_size=4), ) tracer = analysis.tracer_for_instance(instance=instance) fit = FitImaging( masked_imaging=masked_imaging, tracer=tracer, hyper_image_sky=hyper_image_sky, hyper_background_noise=hyper_background_noise, ) assert fit.log_likelihood == fit_figure_of_merit
def test__masked_imaging__signal_to_noise_limit(self, imaging_7x7, mask_7x7_1_pix): imaging_snr_limit = imaging_7x7.signal_to_noise_limited_from( signal_to_noise_limit=1.0) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch(), settings=al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( signal_to_noise_limit=1.0)), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7_1_pix, results=mock.MockResults()) assert (analysis.masked_dataset.image.native == imaging_snr_limit.image.native * np.invert(mask_7x7_1_pix)).all() assert (analysis.masked_dataset.noise_map.native == imaging_snr_limit.noise_map.native * np.invert(mask_7x7_1_pix)).all()
def test__positions_do_not_trace_within_threshold__raises_exception( self, phase_imaging_7x7, imaging_7x7, mask_7x7): imaging_7x7.positions = al.Grid2DIrregular([(1.0, 100.0), (200.0, 2.0)]) phase_imaging_7x7 = al.PhaseImaging( galaxies=dict( lens=al.Galaxy(redshift=0.5, mass=al.mp.SphericalIsothermal()), source=al.Galaxy(redshift=1.0), ), settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens( positions_threshold=0.01)), search=mock.MockSearch(), ) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()) instance = phase_imaging_7x7.model.instance_from_unit_vector([]) with pytest.raises(exc.RayTracingException): analysis.log_likelihood_function(instance=instance)
except OSError: yay = False if not yay: raise IOError("Nope") settings_masked_imaging = al.SettingsMaskedImaging(grid_class=al.Grid) settings_lens = al.SettingsLens( positions_threshold=0.7, auto_positions_factor=3.0, auto_positions_minimum_threshold=0.2, ) settings = al.SettingsPhaseImaging( settings_masked_imaging=settings_masked_imaging, settings_lens=settings_lens) # %% """ __PIPELINE SETUP__ Transdimensional pipelines used the _SetupPipeline_ object to customize the analysis performed by the pipeline, for example if a shear was included in the mass model and the model used for the source galaxy. SLaM pipelines break the analysis down into multiple pipelines which focus on modeling a specific aspect of the strong lens, first the Source, then the (lens) Light and finally the Mass. Each of these pipelines has it own setup object which is equivalent to the _SetupPipeline_ object, customizing the analysis in that pipeline. Each pipeline therefore has its own _SetupMass_, _SetupLight_ and _SetupSource_ object. The _Setup_ used in earlier pipelines determine the model used in later pipelines. For example, if the _Source_
settings_masked_imaging = al.SettingsMaskedImaging(grid_class=al.Grid2D, sub_size=2) """ `Inversion`'s may infer unphysical solution where the source reconstruction is a demagnified reconstruction of the lensed source (see **HowToLens** chapter 4). To prevent this, auto-positioning is used, which uses the lens mass model of earlier phases to automatically set positions and a threshold that resample inaccurate mass models (see `examples/model/positions.py`). The `auto_positions_factor` is a factor that the threshold of the inferred positions using the previous mass model are multiplied by to set the threshold in the next phase. The *auto_positions_minimum_threshold* is the minimum value this threshold can go to, even after multiplication. """ settings = al.SettingsPhaseImaging( settings_masked_imaging=settings_masked_imaging, settings_lens=al.SettingsLens(stochastic_samples=1), ) """ __PIPELINE SETUP__ Transdimensional pipelines used the `SetupPipeline` object to customize the analysis performed by the pipeline, for example if a shear was included in the mass model and the model used for the source galaxy. SLaM pipelines break the analysis down into multiple pipelines which focus on modeling a specific aspect of the strong lens, first the Source, then the (lens) Light and finally the Mass. Each of these pipelines has it own setup object which is equivalent to the `SetupPipeline` object, customizing the analysis in that pipeline. Each pipeline therefore has its own `SetupMass`, `SetupLightParametric` and `SetupSourceParametric` object. The `Setup` used in earlier pipelines determine the model used in later pipelines. For example, if the `Source` pipeline is given a `Pixelization` and `Regularization`, than this `Inversion` will be used in the subsequent _SLaMPipelineLight_ and Mass pipelines. The assumptions regarding the lens light chosen by the `Light` object are
""" __Settings__ The `SettingsPhaseImaging` describe how the model is fitted to the data in the log likelihood function. We discussed these in chapter 2, and a full description of all settings can be found in the example script: `autolens_workspace/examples/model/customize/settings.py`. The settings chosen here are applied to all phases in the pipeline. """ # %% settings_masked_imaging = al.SettingsMaskedImaging(grid_class=al.Grid, sub_size=2) settings = al.SettingsPhaseImaging( settings_masked_imaging=settings_masked_imaging) # %% """ __Pipeline Creation__ To create a `Pipeline`, we call a `make_pipeline` function, which is written in its own Python script: `pipelines/tutorial_1_pipeline_lens_and_source.py`. Before we check it out, lets get the pipeline running, by importing the script, running the `make_pipeline` function to create the `Pipeline` object and calling that objects `run` function. """ # %% from pipelines import tutorial_1_pipeline_lens_and_source
def test__tag__mixture_of_values(): settings = al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( grid_class=al.Grid, grid_inversion_class=al.Grid, sub_size=2, signal_to_noise_limit=2, bin_up_factor=None, psf_shape_2d=None, ), settings_lens=al.SettingsLens(positions_threshold=2.0), ) assert settings.phase_tag_no_inversion == "settings__grid_sub_2__snr_2__pos_2.00" assert ( settings.phase_tag_with_inversion == "settings__grid_sub_2_inv_sub_2__snr_2__pos_2.00" ) settings = al.SettingsPhaseImaging( settings_masked_imaging=al.SettingsMaskedImaging( grid_class=al.GridIterate, grid_inversion_class=al.GridInterpolate, fractional_accuracy=0.5, pixel_scales_interp=0.3, bin_up_factor=3, psf_shape_2d=(2, 2), ), settings_lens=al.SettingsLens(auto_positions_factor=0.5), settings_pixelization=al.SettingsPixelization( use_border=False, is_stochastic=True ), log_likelihood_cap=200.01, ) assert ( settings.phase_tag_no_inversion == "settings__grid_facc_0.5__bin_3__psf_2x2__auto_pos_x0.50__lh_cap_200.0" ) assert ( settings.phase_tag_with_inversion == "settings__grid_facc_0.5_inv_interp_0.300__bin_3__psf_2x2__auto_pos_x0.50__no_border__stochastic__lh_cap_200.0" ) settings = al.SettingsPhaseInterferometer( masked_interferometer=al.SettingsMaskedInterferometer( grid_class=al.GridIterate, grid_inversion_class=al.GridInterpolate, fractional_accuracy=0.5, pixel_scales_interp=0.3, transformer_class=al.TransformerDFT, ), settings_pixelization=al.SettingsPixelization( use_border=False, is_stochastic=True ), log_likelihood_cap=100.01, ) assert ( settings.phase_tag_no_inversion == "settings__grid_facc_0.5__dft__lh_cap_100.0" ) assert ( settings.phase_tag_with_inversion == "settings__grid_facc_0.5_inv_interp_0.300__dft__no_border__stochastic__lh_cap_100.0" ) settings = al.SettingsPhaseInterferometer( masked_interferometer=al.SettingsMaskedInterferometer( transformer_class=al.TransformerNUFFT ), settings_inversion=al.SettingsInversion(use_linear_operators=True), ) assert settings.phase_tag_no_inversion == "settings__grid_sub_2__nufft" assert ( settings.phase_tag_with_inversion == "settings__grid_sub_2_inv_sub_2__nufft__lop" )
The `SettingsPhaseImaging` describe how the model is fitted to the data in the log likelihood function. We discussed these in chapter 2, and a full description of all settings can be found in the example script: `autolens_workspace/examples/model/customize/settings.py`. The settings chosen here are applied to all phases in the pipeline. Note how we can use the _SettingsPixelization_ object to determine whether the border is used during the model-fit. """ # %% settings_masked_imaging = al.SettingsMaskedImaging(sub_size=2) settings_pixelization = al.SettingsPixelization(use_border=True) settings = al.SettingsPhaseImaging( settings_masked_imaging=settings_masked_imaging, settings_pixelization=settings_pixelization, ) # %% """ __Pipeline_Setup_And_Tagging__: We will use the standardized `Setup` objects in this pipeline, which as discussed in chapter 3 provide us with covenient and standardized tools to compose a lens model and tags the output paths. We saw the `SetupMassTotal` object in the previous chapter, which: For this pipeline the pipeline setup customizes and tags: - The `MassProfile` fitted by the pipeline. - If there is an `ExternalShear` in the mass model or not.
sub_size=2) """ `Inversion`'s may infer unphysical solution where the source reconstruction is a demagnified reconstruction of the lensed source (see **HowToLens** chapter 4). To prevent this, auto-positioning is used, which uses the lens mass model of earlier phases to automatically set positions and a threshold that resample inaccurate mass models (see `examples/model/positions.py`). The `auto_positions_factor` is a factor that the threshold of the inferred positions using the previous mass model are multiplied by to set the threshold in the next phase. The *auto_positions_minimum_threshold* is the minimum value this threshold can go to, even after multiplication. """ settings = al.SettingsPhaseImaging( settings_masked_imaging=settings_masked_imaging, settings_lens=al.SettingsLens(auto_einstein_radius_factor=0.2, stochastic_samples=1), ) """ __PIPELINE SETUP__ Transdimensional pipelines used the `SetupPipeline` object to customize the analysis performed by the pipeline, for example if a shear was included in the mass model and the model used for the source galaxy. SLaM pipelines break the analysis down into multiple pipelines which focus on modeling a specific aspect of the strong lens, first the Source, then the (lens) Light and finally the Mass. Each of these pipelines has it own setup object which is equivalent to the `SetupPipeline` object, customizing the analysis in that pipeline. Each pipeline therefore has its own `SetupMass`, `SetupLightParametric` and `SetupSourceParametric` object. The `Setup` used in earlier pipelines determine the model used in later pipelines. For example, if the `Source` pipeline is given a `Pixelization` and `Regularization`, than this `Inversion` will be used in the subsequent
def test__auto_einstein_radius_is_used__einstein_radius_used_in_analysis( self, imaging_7x7, mask_7x7 ): # If position threshold is input (not None) and positions are input, make the positions part of the lens dataset. class MockTracer: def __init__(self, einstein_radius_via_tangential_critical_curve): self.einstein_radius_via_tangential_critical_curve = ( einstein_radius_via_tangential_critical_curve ) def einstein_radius_from_grid(self, grid): return self.einstein_radius_via_tangential_critical_curve @property def has_mass_profile(self): return True phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens(auto_einstein_radius_factor=None) ), ) tracer = MockTracer(einstein_radius_via_tangential_critical_curve=2.0) phase_imaging_7x7.modify_settings( dataset=imaging_7x7, results=mock.MockResults(max_log_likelihood_tracer=tracer), ) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults() ) assert analysis.settings.settings_lens.einstein_radius_estimate == None phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens(auto_einstein_radius_factor=1.0) ), ) tracer = MockTracer(einstein_radius_via_tangential_critical_curve=2.0) phase_imaging_7x7.modify_settings( dataset=imaging_7x7, results=mock.MockResults(max_log_likelihood_tracer=tracer), ) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults(max_log_likelihood_tracer=tracer), ) print(analysis.settings.settings_lens.einstein_radius_estimate) assert analysis.settings.settings_lens.einstein_radius_estimate == 2.0
def test__uses_auto_update_factor(self, image_7x7, noise_map_7x7, mask_7x7): tracer = al.Tracer.from_galaxies( galaxies=[al.Galaxy(redshift=0.5), al.Galaxy(redshift=1.0)] ) # Auto positioning is OFF, so use input positions + threshold. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 1.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens(positions_threshold=0.1) ), ) results = mock.MockResults(max_log_likelihood_tracer=tracer) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) phase_imaging_7x7.modify_settings(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.settings.settings_lens.positions_threshold == 0.1 # Auto positioning is ON, but there are no previous results, so use separate of postiions x positions factor.. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 0.0), (-1.0, 0.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens( positions_threshold=0.1, auto_positions_factor=1.0 ) ), ) results = mock.MockResults(max_log_likelihood_tracer=tracer) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) phase_imaging_7x7.modify_settings(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.settings.settings_lens.positions_threshold == 2.0 # Auto position is ON, and same as above but with a factor of 3.0 which increases the threshold. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 0.0), (-1.0, 0.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens( positions_threshold=0.2, auto_positions_factor=3.0 ) ), ) results = mock.MockResults( max_log_likelihood_tracer=tracer, updated_positions_threshold=0.2 ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) phase_imaging_7x7.modify_settings(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.settings.settings_lens.positions_threshold == 6.0 # Auto position is ON, and same as above but with a minimum auto positions threshold that rounds the value up. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 0.0), (-1.0, 0.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens( positions_threshold=0.2, auto_positions_factor=3.0, auto_positions_minimum_threshold=10.0, ) ), ) results = mock.MockResults( max_log_likelihood_tracer=tracer, updated_positions_threshold=0.2 ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) phase_imaging_7x7.modify_settings(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.settings.settings_lens.positions_threshold == 10.0 # Auto positioning is ON, but positions are None and it cannot find new positions so no threshold. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=None ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens(auto_positions_factor=1.0) ), ) results = mock.MockResults(max_log_likelihood_tracer=tracer) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) phase_imaging_7x7.modify_settings(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.settings.settings_lens.positions_threshold == None
def test__updates_correct_using_factor( self, imaging_7x7, image_7x7, noise_map_7x7, mask_7x7 ): tracer = al.Tracer.from_galaxies( galaxies=[al.Galaxy(redshift=0.5), al.Galaxy(redshift=1.0)] ) # Auto positioning is OFF, so use input positions + threshold. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 1.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens(positions_threshold=0.1) ), ) results = mock.MockResults(max_log_likelihood_tracer=tracer) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.masked_dataset.positions.in_grouped_list == [[(1.0, 1.0)]] # Auto positioning is ON, but there are no previous results, so use input positions. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 1.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens( positions_threshold=0.2, auto_positions_factor=2.0 ) ), ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.masked_dataset.positions.in_grouped_list == [[(1.0, 1.0)]] # Auto positioning is ON, there are previous results so use their new positions and threshold (which is # multiplied by the auto_positions_factor). However, only one set of positions is computed from the previous # result, to use input positions. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 1.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens( positions_threshold=0.2, auto_positions_factor=2.0 ) ), ) results = mock.MockResults( max_log_likelihood_tracer=tracer, updated_positions=al.GridIrregularGrouped(grid=[[(2.0, 2.0)]]), updated_positions_threshold=0.3, ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.masked_dataset.positions.in_grouped_list == [[(1.0, 1.0)]] # Auto positioning is ON, but the tracer only has a single plane and thus no lensing, so use input positions. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 1.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens( positions_threshold=0.2, auto_positions_factor=1.0 ) ), ) tracer_x1_plane = al.Tracer.from_galaxies(galaxies=[al.Galaxy(redshift=0.5)]) results = mock.MockResults( max_log_likelihood_tracer=tracer_x1_plane, updated_positions=al.GridIrregularGrouped(grid=[[(2.0, 2.0), (3.0, 3.0)]]), updated_positions_threshold=0.3, ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.masked_dataset.positions.in_grouped_list == [[(1.0, 1.0)]] # Auto positioning is ON, there are previous results so use their new positions and threshold (which is # multiplied by the auto_positions_factor). Multiple positions are available so these are now used. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(1.0, 1.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens( positions_threshold=0.2, auto_positions_factor=2.0 ) ), ) results = mock.MockResults( max_log_likelihood_tracer=tracer, updated_positions=al.GridIrregularGrouped(grid=[[(2.0, 2.0), (3.0, 3.0)]]), updated_positions_threshold=0.3, ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.masked_dataset.positions.in_grouped_list == [ [(2.0, 2.0), (3.0, 3.0)] ] # Auto positioning is Off, but there are previous results with updated positions relative to the input # positions, so use those with their positions threshold. imaging_7x7 = al.Imaging( image=image_7x7, noise_map=noise_map_7x7, positions=al.GridIrregularGrouped([[(2.0, 2.0)]]), ) phase_imaging_7x7 = al.PhaseImaging( search=mock.MockSearch("test_phase"), settings=al.SettingsPhaseImaging( settings_lens=al.SettingsLens(positions_threshold=0.1) ), ) results = mock.MockResults( max_log_likelihood_tracer=tracer, positions=al.GridIrregularGrouped(grid=[[(3.0, 3.0), (4.0, 4.0)]]), updated_positions_threshold=0.3, ) phase_imaging_7x7.modify_dataset(dataset=imaging_7x7, results=results) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=results ) assert analysis.masked_dataset.positions.in_grouped_list == [ [(3.0, 3.0), (4.0, 4.0)] ]