def make_pipeline( name, phase_folders, real_space_shape_2d=(100, 100), real_space_pixel_scales=(0.1, 0.1), optimizer_class=af.MultiNest, ): phase1 = al.PhaseInterferometer( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.SphericalDevVaucouleurs, mass=al.mp.EllipticalIsothermal, ), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 60 phase1.optimizer.sampling_efficiency = 0.8 phase1 = phase1.extend_with_multiple_hyper_phases(hyper_galaxy=True) phase2 = al.PhaseInterferometer( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=phase1.result.model.galaxies.lens.light, mass=phase1.result.model.galaxies.lens.mass, hyper_galaxy=phase1.result.hyper_combined.instance.galaxies. lens.hyper_galaxy, ), source=al.GalaxyModel( redshift=1.0, light=phase1.result.model.galaxies.source.light, hyper_galaxy=phase1.result.hyper_combined.instance.galaxies. source.hyper_galaxy, ), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase2.optimizer.const_efficiency_mode = True phase2.optimizer.n_live_points = 40 phase2.optimizer.sampling_efficiency = 0.8 phase2 = phase2.extend_with_multiple_hyper_phases(hyper_galaxy=True) return al.PipelineDataset(name, phase1, phase2)
def make_pipeline(name, folders, real_space_mask, search=af.DynestyStatic()): mass = af.PriorModel(al.mp.EllipticalIsothermal) mass.centre.centre_0 = 2.0 mass.centre.centre_1 = 2.0 mass.einstein_radius = 1.6 pixelization = af.PriorModel(al.pix.VoronoiMagnification) pixelization.shape_0 = 20.0 pixelization.shape_1 = 20.0 phase1 = al.PhaseInterferometer( phase_name="phase_1", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=mass), source=al.GalaxyModel(redshift=1.0, pixelization=pixelization, regularization=al.reg.Constant), ), real_space_mask=real_space_mask, search=search, ) phase1.search.const_efficiency_mode = True phase1.search.n_live_points = 60 phase1.search.facc = 0.8 phase1 = phase1.extend_with_inversion_phase() phase2 = al.PhaseInterferometer( phase_name="phase_2", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=phase1.result.model.galaxies.lens.mass), source=al.GalaxyModel( redshift=1.0, pixelization=phase1.results.settings_inversion.instance. galaxies.source.pixelization, regularization=phase1.results.settings_inversion.instance. galaxies.source.regularization, ), ), real_space_mask=real_space_mask, search=search, ) phase2.search.const_efficiency_mode = True phase2.search.n_live_points = 60 phase2.search.facc = 0.8 phase2 = phase2.extend_with_inversion_phase() return al.PipelineDataset(name, phase1, phase2)
def make_pipeline( name, phase_folders, real_space_shape_2d=(100, 100), real_space_pixel_scales=(0.1, 0.1), optimizer_class=af.MultiNest, ): phase1 = al.PhaseInterferometer( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic)), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 40 phase1.optimizer.sampling_efficiency = 0.8 phase1 = phase1.extend_with_multiple_hyper_phases( hyper_galaxy=True, include_background_sky=True, include_background_noise=True) phase2 = al.PhaseInterferometer( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict(lens=al.GalaxyModel( redshift=0.5, light=phase1.result.model.galaxies.lens.light, hyper_galaxy=phase1.result.hyper_combined.instance.galaxies.lens. hyper_galaxy, )), hyper_image_sky=phase1.result.hyper_combined.instance.hyper_image_sky, hyper_background_noise=phase1.result.hyper_combined.instance. hyper_background_noise, real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase2.optimizer.const_efficiency_mode = True phase2.optimizer.n_live_points = 40 phase2.optimizer.sampling_efficiency = 0.8 return al.PipelineDataset(name, phase1, phase2)
def test__positions_do_not_trace_within_threshold__raises_exception( self, phase_interferometer_7, interferometer_7, mask_7x7, visibilities_mask_7): interferometer_7.positions = al.GridIrregularGrouped([[(1.0, 100.0), (200.0, 2.0)]]) phase_interferometer_7 = al.PhaseInterferometer( real_space_mask=mask_7x7, galaxies=dict( lens=al.Galaxy(redshift=0.5, mass=al.mp.SphericalIsothermal()), source=al.Galaxy(redshift=1.0), ), settings=al.SettingsPhaseInterferometer( settings_lens=al.SettingsLens(positions_threshold=0.01)), search=mock.MockSearch("test_phase"), ) analysis = phase_interferometer_7.make_analysis( dataset=interferometer_7, mask=visibilities_mask_7, results=mock.MockResults(), ) instance = phase_interferometer_7.model.instance_from_unit_vector([]) with pytest.raises(exc.RayTracingException): analysis.log_likelihood_function(instance=instance)
def make_pipeline( name, phase_folders, real_space_shape_2d=(100, 100), real_space_pixel_scales=(0.1, 0.1), optimizer_class=af.MultiNest, ): phase1 = al.PhaseInterferometer( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.SphericalDevVaucouleurs, mass=al.mp.EllipticalIsothermal, ), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 60 phase1.optimizer.sampling_efficiency = 0.8 return al.PipelineDataset(name, phase1)
def make_pipeline(name, folders, real_space_mask, search=af.DynestyStatic()): mass = af.PriorModel(al.mp.EllipticalIsothermal) mass.centre.centre_0 = 0.0 mass.centre.centre_1 = 0.0 mass.einstein_radius = 1.6 pixelization = af.PriorModel(al.pix.Rectangular) pixelization.shape_0 = 20.0 pixelization.shape_1 = 20.0 phase1 = al.PhaseInterferometer( phase_name="phase_1", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, bulge=al.lp.SphericalDevVaucouleurs, mass=mass), source=al.GalaxyModel(redshift=1.0, pixelization=pixelization, regularization=al.reg.Constant), ), real_space_mask=real_space_mask, search=search, ) phase1.search.const_efficiency_mode = True phase1.search.n_live_points = 60 phase1.search.facc = 0.8 return al.PipelineDataset(name, phase1)
def test__fit_interferometer_generator_from_aggregator( interferometer_7, visibilities_mask_7, mask_7x7, samples ): phase_interferometer_7x7 = al.PhaseInterferometer( galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), search=mock.MockSearch("test_phase_aggregator", samples=samples), real_space_mask=mask_7x7, ) phase_interferometer_7x7.run( dataset=interferometer_7, mask=visibilities_mask_7, results=mock.MockResults(samples=samples), ) agg = af.Aggregator(directory=phase_interferometer_7x7.paths.output_path) fit_interferometer_gen = al.agg.FitInterferometer(aggregator=agg) for fit_interferometer in fit_interferometer_gen: assert ( fit_interferometer.masked_interferometer.interferometer.visibilities == interferometer_7.visibilities ).all() assert ( fit_interferometer.masked_interferometer.real_space_mask == mask_7x7 ).all()
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 make_pipeline( name, phase_folders, real_space_shape_2d=(100, 100), real_space_pixel_scales=(0.1, 0.1), non_linear_class=af.MultiNest, ): phase1 = al.PhaseInterferometer( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=al.mp.EllipticalIsothermal), source_0=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, non_linear_class=non_linear_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 60 phase1.optimizer.sampling_efficiency = 0.7 phase2 = al.PhaseInterferometer( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=phase1.result.model.galaxies.lens.mass), source_0=al.GalaxyModel( redshift=1.0, light=phase1.result.model.galaxies.source_0.light), source_1=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, non_linear_class=non_linear_class, ) phase2.optimizer.const_efficiency_mode = True phase2.optimizer.n_live_points = 60 phase2.optimizer.sampling_efficiency = 0.7 return al.PipelineDataset(name, phase1, phase2)
def make_pipeline( name, phase_folders, real_space_shape_2d=(100, 100), real_space_pixel_scales=(0.1, 0.1), optimizer_class=af.MultiNest, ): class LensPlaneGalaxyX2Phase(al.PhaseInterferometer): def customize_priors(self, results): self.galaxies.lens_0.light.centre_0 = -1.0 self.galaxies.lens_0.light.centre_1 = -1.0 self.galaxies.lens_1.light.centre_0 = 1.0 self.galaxies.lens_1.light.centre_1 = 1.0 phase1 = LensPlaneGalaxyX2Phase( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict( lens_0=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic), lens_1=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 40 phase1.optimizer.sampling_efficiency = 0.8 phase1 = phase1.extend_with_multiple_hyper_phases(hyper_galaxy=True) phase2 = al.PhaseInterferometer( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict( lens_0=al.GalaxyModel( redshift=0.5, light=phase1.result.model.galaxies.lens_0.light, hyper_galaxy=phase1.result.hyper_combined.instance.galaxies.lens_0.hyper_galaxy, ), lens_1=al.GalaxyModel( redshift=0.5, light=phase1.result.model.galaxies.lens_1.light, hyper_galaxy=phase1.result.hyper_combined.instance.galaxies.lens_1.hyper_galaxy, ), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase2.optimizer.const_efficiency_mode = True phase2.optimizer.n_live_points = 40 phase2.optimizer.sampling_efficiency = 0.8 return al.PipelineDataset(name, phase1, phase2)
def make_pipeline(name, folders, real_space_mask, search=af.DynestyStatic()): phase1 = al.PhaseInterferometer( phase_name="phase_1", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=al.mp.EllipticalIsothermal), source_0=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_mask=real_space_mask, search=search, ) phase1.search.const_efficiency_mode = True phase1.search.n_live_points = 60 phase1.search.facc = 0.7 phase2 = al.PhaseInterferometer( phase_name="phase_2", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=phase1.result.model.galaxies.lens.mass), source_0=al.GalaxyModel( redshift=1.0, light=phase1.result.model.galaxies.source_0.light), source_1=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_mask=real_space_mask, search=search, ) phase2.search.const_efficiency_mode = True phase2.search.n_live_points = 60 phase2.search.facc = 0.7 return al.PipelineDataset(name, phase1, phase2)
def test__phase_can_receive_hyper_image_and_noise_maps(self, mask_7x7): phase_interferometer_7 = al.PhaseInterferometer( galaxies=dict( lens=al.GalaxyModel(redshift=al.Redshift), lens1=al.GalaxyModel(redshift=al.Redshift), ), hyper_background_noise=al.hyper_data.HyperBackgroundNoise, search=mock.MockSearch("test_phase"), real_space_mask=mask_7x7, ) instance = phase_interferometer_7.model.instance_from_vector( [0.1, 0.2, 0.3]) assert instance.galaxies[0].redshift == 0.1 assert instance.galaxies[1].redshift == 0.2 assert instance.hyper_background_noise.noise_scale == 0.3
def make_pipeline(slam, settings, real_space_mask): pipeline_name = "pipeline_source[parametric]" """ This pipeline is tagged according to whether: 1) Hyper-fitting settings (galaxies, sky, background noise) are used. 2) The lens galaxy mass model includes an `ExternalShear`. 3) The source model determined from `SetupSourceParametric` (e.g. `bulge_prior_model`, `disk_prior_model`, etc.) """ path_prefix = slam.path_prefix_from( slam.path_prefix, pipeline_name, slam.source_parametric_tag ) """ Phase 1: Fit the lens`s `MassProfile`'s and source galaxy. """ phase1 = al.PhaseInterferometer( search=af.DynestyStatic( name="phase[1]_mass[total]_source[parametric]", n_live_points=200, walks=10 ), galaxies=af.CollectionPriorModel( lens=al.GalaxyModel( redshift=slam.redshift_lens, mass=slam.pipeline_source_parametric.setup_mass.mass_prior_model, shear=slam.pipeline_source_parametric.setup_mass.shear_prior_model, ), source=al.GalaxyModel( redshift=slam.redshift_source, bulge=slam.pipeline_source_parametric.setup_source.bulge_prior_model, disk=slam.pipeline_source_parametric.setup_source.disk_prior_model, envelope=slam.pipeline_source_parametric.setup_source.envelope_prior_model, ), ), settings=settings, real_space_mask=real_space_mask, ) phase1 = phase1.extend_with_hyper_phase(setup_hyper=slam.setup_hyper) return al.PipelineDataset(pipeline_name, path_prefix, None, phase1)
def test__fit_figure_of_merit__includes_hyper_image_and_noise__matches_fit( self, interferometer_7, mask_7x7, visibilities_mask_7 ): 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_interferometer_7 = al.PhaseInterferometer( galaxies=dict(lens=lens_galaxy), hyper_background_noise=hyper_background_noise, settings=al.SettingsPhaseInterferometer( settings_masked_interferometer=al.SettingsMaskedInterferometer( sub_size=4 ) ), search=mock.MockSearch("test_phase"), real_space_mask=mask_7x7, ) analysis = phase_interferometer_7.make_analysis( dataset=interferometer_7, mask=visibilities_mask_7, results=mock.MockResults(), ) instance = phase_interferometer_7.model.instance_from_unit_vector([]) fit_figure_of_merit = analysis.log_likelihood_function(instance=instance) assert analysis.masked_interferometer.real_space_mask.sub_size == 4 masked_interferometer = al.MaskedInterferometer( interferometer=interferometer_7, visibilities_mask=visibilities_mask_7, real_space_mask=mask_7x7, settings=al.SettingsMaskedInterferometer(sub_size=4), ) tracer = analysis.tracer_for_instance(instance=instance) fit = FitInterferometer( masked_interferometer=masked_interferometer, tracer=tracer, hyper_background_noise=hyper_background_noise, ) assert fit.log_likelihood == fit_figure_of_merit
def test__masked_interferometer_generator_from_aggregator( interferometer_7, visibilities_mask_7, mask_7x7, samples ): phase_interferometer_7x7 = al.PhaseInterferometer( galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), settings=al.SettingsPhaseInterferometer( settings_masked_interferometer=al.SettingsMaskedInterferometer( transformer_class=al.TransformerDFT, 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), real_space_mask=mask_7x7, ) phase_interferometer_7x7.run( dataset=interferometer_7, mask=visibilities_mask_7, results=mock.MockResults(samples=samples), ) agg = af.Aggregator(directory=phase_interferometer_7x7.paths.output_path) masked_interferometer_gen = al.agg.MaskedInterferometer(aggregator=agg) for masked_interferometer in masked_interferometer_gen: assert ( masked_interferometer.interferometer.visibilities == interferometer_7.visibilities ).all() assert (masked_interferometer.real_space_mask == mask_7x7).all() assert isinstance(masked_interferometer.grid, al.GridIterate) assert isinstance(masked_interferometer.grid_inversion, al.GridInterpolate) assert masked_interferometer.grid.sub_steps == [2] assert masked_interferometer.grid.fractional_accuracy == 0.5 assert masked_interferometer.grid_inversion.pixel_scales_interp == (0.1, 0.1) assert isinstance(masked_interferometer.transformer, al.TransformerDFT)
def test__fit_using_interferometer(self, interferometer_7, mask_7x7, visibilities_mask_7x2): phase_interferometer_7 = al.PhaseInterferometer( optimizer_class=mock_pipeline.MockNLO, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_mask=mask_7x7, phase_name="test_phase_test_fit", ) result = phase_interferometer_7.run(dataset=interferometer_7, mask=visibilities_mask_7x2) assert isinstance(result.instance.galaxies[0], al.Galaxy) assert isinstance(result.instance.galaxies[0], al.Galaxy)
def test__fit_using_interferometer( self, interferometer_7, mask_7x7, visibilities_mask_7, samples_with_result ): phase_interferometer_7 = al.PhaseInterferometer( galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), search=mock.MockSearch("test_phase", samples=samples_with_result), real_space_mask=mask_7x7, ) result = phase_interferometer_7.run( dataset=interferometer_7, mask=visibilities_mask_7, results=mock.MockResults(), ) assert isinstance(result.instance.galaxies[0], al.Galaxy) assert isinstance(result.instance.galaxies[0], al.Galaxy)
def test__fit_figure_of_merit__includes_hyper_image_and_noise__matches_fit( self, interferometer_7, mask_7x7, visibilities_mask_7x2): 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_interferometer_7 = al.PhaseInterferometer( real_space_mask=mask_7x7, galaxies=[lens_galaxy], hyper_background_noise=hyper_background_noise, cosmology=cosmo.FLRW, sub_size=4, phase_name="test_phase", ) analysis = phase_interferometer_7.make_analysis( dataset=interferometer_7, mask=visibilities_mask_7x2, results=mock_pipeline.MockResults(), ) instance = phase_interferometer_7.model.instance_from_unit_vector([]) fit_figure_of_merit = analysis.fit(instance=instance) real_space_mask = phase_interferometer_7.meta_dataset.mask_with_phase_sub_size_from_mask( mask=mask_7x7) assert real_space_mask.sub_size == 4 masked_interferometer = al.MaskedInterferometer( interferometer=interferometer_7, visibilities_mask=visibilities_mask_7x2, real_space_mask=real_space_mask, ) tracer = analysis.tracer_for_instance(instance=instance) fit = FitInterferometer( masked_interferometer=masked_interferometer, tracer=tracer, hyper_background_noise=hyper_background_noise, ) assert fit.likelihood == fit_figure_of_merit
def make_pipeline( name, phase_folders, real_space_shape_2d=(100, 100), real_space_pixel_scales=(0.1, 0.1), non_linear_class=af.MultiNest, ): phase1 = al.PhaseInterferometer( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict(lens=al.GalaxyModel(redshift=0.5, sersic=al.lp.EllipticalSersic)), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, non_linear_class=non_linear_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 40 phase1.optimizer.sampling_efficiency = 0.8 return al.PipelineDataset(name, phase1)
def test__fit_figure_of_merit__matches_correct_fit_given_galaxy_profiles( self, interferometer_7, mask_7x7, visibilities_mask_7 ): lens_galaxy = al.Galaxy( redshift=0.5, light=al.lp.EllipticalSersic(intensity=0.1) ) phase_interferometer_7 = al.PhaseInterferometer( galaxies=dict(lens=lens_galaxy), cosmology=cosmo.FLRW, settings=al.SettingsPhaseInterferometer( settings_masked_interferometer=al.SettingsMaskedInterferometer( sub_size=2 ) ), search=mock.MockSearch("test_phase"), real_space_mask=mask_7x7, ) analysis = phase_interferometer_7.make_analysis( dataset=interferometer_7, mask=visibilities_mask_7, results=mock.MockResults(), ) instance = phase_interferometer_7.model.instance_from_unit_vector([]) fit_figure_of_merit = analysis.log_likelihood_function(instance=instance) masked_interferometer = al.MaskedInterferometer( interferometer=interferometer_7, visibilities_mask=visibilities_mask_7, real_space_mask=mask_7x7, settings=al.SettingsMaskedInterferometer(sub_size=2), ) tracer = analysis.tracer_for_instance(instance=instance) fit = al.FitInterferometer( masked_interferometer=masked_interferometer, tracer=tracer ) assert fit.log_likelihood == fit_figure_of_merit
def test__log_likelihood_cap(self, interferometer_7, mask_7x7): lens_galaxy = al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=0.1)) phase_imaging_7x7 = al.PhaseInterferometer( phase_name="test_phase", galaxies=dict(lens=lens_galaxy), settings=al.SettingsPhaseInterferometer( masked_interferometer=al.SettingsMaskedInterferometer( sub_size=1), log_likelihood_cap=100.0, ), search=mock.MockSearch(), real_space_mask=mask_7x7, ) analysis = phase_imaging_7x7.make_analysis(dataset=interferometer_7, mask=mask_7x7, results=mock.MockResults()) assert analysis.log_likelihood_cap == 100.0
def make_pipeline(name, folders, real_space_mask, search=af.DynestyStatic()): phase1 = al.PhaseInterferometer( phase_name="phase_1", folders=folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.SphericalDevVaucouleurs, mass=al.mp.EllipticalIsothermal, ), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_mask=real_space_mask, search=search, ) phase1.search.const_efficiency_mode = True phase1.search.n_live_points = 60 phase1.search.facc = 0.8 return al.PipelineDataset(name, phase1)
def test__extend_with_stochastic_phase__sets_up_model_correctly(self, mask_7x7): galaxies = af.ModelInstance() galaxies.lens = al.Galaxy( redshift=0.5, light=al.lp.SphericalSersic(), mass=al.mp.SphericalIsothermal(), ) galaxies.source = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiBrightnessImage(), regularization=al.reg.AdaptiveBrightness(), ) phase = al.PhaseImaging(search=mock.MockSearch()) phase_extended = phase.extend_with_stochastic_phase() model = phase_extended.make_model(instance=galaxies) assert isinstance(model.lens.mass.centre, TuplePrior) assert isinstance(model.lens.light.intensity, float) assert isinstance(model.source.pixelization.pixels, int) assert isinstance(model.source.regularization.inner_coefficient, float) phase_extended = phase.extend_with_stochastic_phase(include_lens_light=True) model = phase_extended.make_model(instance=galaxies) assert isinstance(model.lens.mass.centre, TuplePrior) assert isinstance(model.lens.light.intensity, af.UniformPrior) assert isinstance(model.source.pixelization.pixels, int) assert isinstance(model.source.regularization.inner_coefficient, float) phase_extended = phase.extend_with_stochastic_phase(include_pixelization=True) model = phase_extended.make_model(instance=galaxies) assert isinstance(model.lens.mass.centre, TuplePrior) assert isinstance(model.lens.light.intensity, float) assert isinstance(model.source.pixelization.pixels, af.UniformPrior) assert not isinstance( model.source.regularization.inner_coefficient, af.UniformPrior ) phase_extended = phase.extend_with_stochastic_phase(include_regularization=True) model = phase_extended.make_model(instance=galaxies) assert isinstance(model.lens.mass.centre, TuplePrior) assert isinstance(model.lens.light.intensity, float) assert isinstance(model.source.pixelization.pixels, int) assert isinstance( model.source.regularization.inner_coefficient, af.UniformPrior ) phase = al.PhaseInterferometer( search=mock.MockSearch(), real_space_mask=mask_7x7 ) phase_extended = phase.extend_with_stochastic_phase() model = phase_extended.make_model(instance=galaxies) assert isinstance(model.lens.mass.centre, TuplePrior) assert isinstance(model.lens.light.intensity, float) assert isinstance(model.source.pixelization.pixels, int) assert isinstance(model.source.regularization.inner_coefficient, float)
def make_pipeline( setup, real_space_mask, phase_folders=None, redshift_lens=0.5, redshift_source=1.0, positions_threshold=None, sub_size=2, pixel_scale_interpolation_grid=None, inversion_uses_border=True, inversion_pixel_limit=None, ): ### SETUP PIPELINE & PHASE NAMES, TAGS AND PATHS ### # A source tag distinguishes if the previous pipeline models used a parametric or inversion model for the source. pipeline_name = "pipeline_mass__power_law" # For pipeline tagging we need to set the mass type setup.set_mass_type(mass_type="power_law") # This pipeline is tagged according to whether: # 1) Hyper-fitting setup (galaxies, sky, background noise) are used. # 2) The lens galaxy mass model includes an external shear. phase_folders.append(pipeline_name) phase_folders.append(setup.general.tag) phase_folders.append(setup.source.tag) phase_folders.append(setup.mass.tag) ### SETUP SHEAR ### # Include the shear in the mass model if not switched off in the pipeline setup. if not setup.mass.no_shear: if af.last.model.galaxies.lens.shear is not None: shear = af.last.model.galaxies.lens.shear else: shear = al.mp.ExternalShear else: shear = None ### PHASE 1 ### # In phase 1, we fit the lens galaxy's mass and source, where we: # 1) Use the source galaxy of the 'source' pipeline. # 2) Set priors on the lens galaxy mass using the EllipticalIsothermal and ExternalShear of previous pipelines. # Setup the power-law mass profile and initialize its priors from the SIE. mass = af.PriorModel(al.mp.EllipticalPowerLaw) mass.centre = af.last.model.galaxies.lens.mass.centre mass.axis_ratio = af.last.model.galaxies.lens.mass.axis_ratio mass.phi = af.last.model.galaxies.lens.mass.phi mass.einstein_radius = af.last.model_absolute( a=0.3).galaxies.lens.mass.einstein_radius # Setup the source model, which uses a variable parametric profile or fixed inversion model depending on the # previous pipeline. source = source_with_previous_model_or_instance(setup=setup) phase1 = al.PhaseInterferometer( phase_name="phase_1__lens_power_law__source", phase_folders=phase_folders, real_space_mask=real_space_mask, galaxies=dict( lens=al.GalaxyModel(redshift=redshift_lens, mass=mass, shear=shear), source=source, ), hyper_background_noise=af.last.hyper_combined.instance.optional. hyper_background_noise, positions_threshold=positions_threshold, sub_size=sub_size, pixel_scale_interpolation_grid=pixel_scale_interpolation_grid, inversion_uses_border=inversion_uses_border, inversion_pixel_limit=inversion_pixel_limit, optimizer_class=af.MultiNest, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 75 phase1.optimizer.sampling_efficiency = 0.2 phase1.optimizer.evidence_tolerance = 0.8 # If the source is parametric, the inversion hyper phase below will be skipped. phase1 = phase1.extend_with_multiple_hyper_phases(inversion=True) return al.PipelineDataset(pipeline_name, phase1)
def make_phase_interferometer_7(mask_7x7): return al.PhaseInterferometer( optimizer_class=mock_pipeline.MockNLO, real_space_mask=mask_7x7, phase_name="test_phase", )
def make_pipeline( name, phase_folders, real_space_shape_2d=(100, 100), real_space_pixel_scales=(0.1, 0.1), optimizer_class=af.MultiNest, ): class SourcePix(al.PhaseInterferometer): def customize_priors(self, results): self.galaxies.lens.mass.centre.centre_0 = 0.0 self.galaxies.lens.mass.centre.centre_1 = 0.0 self.galaxies.lens.mass.einstein_radius = 1.6 self.galaxies.source.pixelization.shape.shape_0 = 20.0 self.galaxies.source.pixelization.shape.shape_1 = 20.0 phase1 = SourcePix( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=al.mp.EllipticalIsothermal), source=al.GalaxyModel( redshift=1.0, pixelization=al.pix.VoronoiMagnification, regularization=al.reg.Constant, ), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 60 phase1.optimizer.sampling_efficiency = 0.8 phase1.extend_with_multiple_hyper_phases(hyper_galaxy=True) phase2 = al.PhaseInterferometer( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, mass=phase1.result.model.galaxies.lens.mass, hyper_galaxy=al.HyperGalaxy, ), source=al.GalaxyModel( redshift=1.0, pixelization=phase1.result.model.galaxies.source.pixelization, regularization=phase1.result.model.galaxies.source. regularization, hyper_galaxy=phase1.result.hyper_combined.instance.galaxies. source.hyper_galaxy, ), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase2.optimizer.const_efficiency_mode = True phase2.optimizer.n_live_points = 40 phase2.optimizer.sampling_efficiency = 0.8 return al.PipelineDataset(name, phase1, phase2)
def make_pipeline( setup, phase_folders, real_space_mask, lens_redshift, source_redshift, instance, priors=None, pipeline_name="pipeline_source__inversion", transformer_class=al.TransformerNUFFT, auto_positions_factor=None, positions_threshold=None, sub_size=1, inversion_uses_border=True, inversion_pixel_limit=None, evidence_tolerance=10.0, ): phase_folders.append(pipeline_name) for type in ["general", "source"]: if hasattr(setup, type): if type == "general": phase_folders.append(setup.general.source_tag) if type == "source": setup.set_source_type(source_type=setup.source.inversion_tag) phase_folders.append(setup.source.tag) if "lens" in instance.keys(): lens = instance["lens"] else: raise ValueError("...") source = al.GalaxyModel( redshift=source_redshift, pixelization=al.pix.VoronoiMagnification, regularization=al.reg.Constant, ) source.pixelization.shape.shape_0 = af.UniformPrior(lower_limit=5, upper_limit=100) source.pixelization.shape.shape_1 = af.UniformPrior(lower_limit=5, upper_limit=100) source.regularization.coefficient = af.LogUniformPrior( lower_limit=10**-5.0, upper_limit=10**+5.0) phase1 = al.PhaseInterferometer( phase_name="phase_1__lens_instance__source_inversion", phase_folders=phase_folders, real_space_mask=real_space_mask, galaxies=dict( lens=lens, source=source, ), transformer_class=transformer_class, positions_threshold=positions_threshold, auto_positions_factor=auto_positions_factor, sub_size=sub_size, inversion_uses_border=inversion_uses_border, inversion_pixel_limit=inversion_pixel_limit, non_linear_class=af.MultiNest, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 20 phase1.optimizer.sampling_efficiency = 0.8 phase1.optimizer.evidence_tolerance = 0.1 return al.PipelineDataset(pipeline_name, phase1)
def make_pipeline( setup, phase_folders, real_space_mask, lens_redshift, source_redshift, priors=None, pipeline_name="pipeline_mass__source_parametric", mass_type="PowerLaw", transformer_class=al.TransformerNUFFT, auto_positions_factor=None, positions_threshold=None, sub_size=1, pixel_scale_interpolation_grid=None, inversion_uses_border=True, inversion_pixel_limit=None, ): phase_folders.append(pipeline_name) setup.set_mass_type(mass_type=mass_type) update_phase_folders_from_setup(phase_folders=phase_folders, setup=setup) # ... NOTE: Make it more elegant mass = af.PriorModel(al.mp.EllipticalPowerLaw) # ... NOTE : Make is more elegant? mass.centre = af.last.model.galaxies.lens.mass.centre mass.axis_ratio = af.last.model.galaxies.lens.mass.axis_ratio mass.phi = af.last.model.galaxies.lens.mass.phi mass.einstein_radius = af.last.model_absolute( a=0.3).galaxies.lens.mass.einstein_radius # for attr, value in af.last.model.galaxies.lens.mass.__dict__.items(): # print(attr, value) if not setup.mass.no_shear: if af.last.model.galaxies.lens.shear is not None: shear = af.last.model.galaxies.lens.shear else: shear = al.mp.ExternalShear else: shear = None # ... lens = al.GalaxyModel(redshift=lens_redshift, mass=mass, shear=shear) phase1 = al.PhaseInterferometer( phase_name="phase_1__lens_powerlaw{}__source".format( "_and_shear" if not setup.mass.no_shear else ''), phase_folders=phase_folders, real_space_mask=real_space_mask, galaxies=dict( lens=lens, source=af.last.model.galaxies.source, ), transformer_class=transformer_class, auto_positions_factor=auto_positions_factor, positions_threshold=positions_threshold, sub_size=sub_size, pixel_scale_interpolation_grid=pixel_scale_interpolation_grid, inversion_uses_border=inversion_uses_border, inversion_pixel_limit=inversion_pixel_limit, non_linear_class=af.MultiNest, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 100 phase1.optimizer.sampling_efficiency = 0.2 phase1.optimizer.evidence_tolerance = 0.8 # phase1 = phase1.extend_with_multiple_hyper_phases(inversion=True) return al.PipelineDataset(pipeline_name, phase1)
def make_phase_interferometer_7(): return al.PhaseInterferometer(search=MockSearch(name="test_phase"), real_space_mask=make_mask_7x7())
def make_pipeline( name, phase_folders, pipeline_pixelization=al.pix.VoronoiBrightnessImage, pipeline_regularization=al.reg.AdaptiveBrightness, optimizer_class=af.MultiNest, ): phase1 = al.PhaseInterferometer( phase_name="phase_1__lens_sersic", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic)), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 30 phase1.optimizer.sampling_efficiency = 0.3 phase1 = phase1.extend_with_multiple_hyper_phases(hyper_galaxy=True) class LensSubtractedPhase(al.PhaseInterferometer): def customize_priors(self, results): ## Lens Light Sersic -> Sersic ## self.galaxies.lens.light = results.from_phase( "phase_1__lens_sersic").instance.galaxies.lens.light ## Lens Mass, Move centre priors to centre of lens light ### self.galaxies.lens.mass.centre = ( results.from_phase("phase_1__lens_sersic").model_absolute( a=0.1).galaxies.lens.light.centre) ## Set all hyper-galaxies if feature is turned on ## self.galaxies.lens.hyper_galaxy = ( results.last.hyper_combined.instance.galaxies.lens.hyper_galaxy ) phase2 = LensSubtractedPhase( phase_name="phase_2__lens_sie__source_sersic", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.EllipticalSersic, mass=al.mp.EllipticalIsothermal, shear=al.mp.ExternalShear, ), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase2.optimizer.const_efficiency_mode = False phase2.optimizer.n_live_points = 50 phase2.optimizer.sampling_efficiency = 0.3 phase2 = phase2.extend_with_multiple_hyper_phases( hyper_galaxy=True, include_background_sky=True, include_background_noise=True) class LensSourcePhase(al.PhaseInterferometer): def customize_priors(self, results): ## Lens Light, Sersic -> Sersic ### self.galaxies.lens.light = results.from_phase( "phase_1__lens_sersic").model.galaxies.lens.light ## Lens Mass, SIE -> SIE, Shear -> Shear ### self.galaxies.lens.mass = results.from_phase( "phase_2__lens_sie__source_sersic").model.galaxies.lens.mass self.galaxies.lens.shear = results.from_phase( "phase_2__lens_sie__source_sersic").model.galaxies.lens.shear ### Source Light, Sersic -> Sersic ### self.galaxies.source = results.from_phase( "phase_2__lens_sie__source_sersic").model.galaxies.source ## Set all hyper-galaxies if feature is turned on ## self.galaxies.lens.hyper_galaxy = ( results.last.hyper_combined.instance.galaxies.lens.hyper_galaxy ) phase3 = LensSourcePhase( phase_name="phase_3__lens_sersic_sie__source_sersic", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.EllipticalSersic, mass=al.mp.EllipticalIsothermal, shear=al.mp.ExternalShear, ), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase3.optimizer.const_efficiency_mode = True phase3.optimizer.n_live_points = 75 phase3.optimizer.sampling_efficiency = 0.3 phase3 = phase3.extend_with_multiple_hyper_phases(hyper_galaxy=True) class InversionPhase(al.PhaseInterferometer): def customize_priors(self, results): ## Lens Light & Mass, Sersic -> Sersic, SIE -> SIE, Shear -> Shear ### self.galaxies.lens.light = results.from_phase( "phase_3__lens_sersic_sie__source_sersic" ).instance.galaxies.lens.light self.galaxies.lens.mass = results.from_phase( "phase_3__lens_sersic_sie__source_sersic" ).instance.galaxies.lens.mass self.galaxies.lens.shear = results.from_phase( "phase_3__lens_sersic_sie__source_sersic" ).instance.galaxies.lens.shear ## Set all hyper-galaxies if feature is turned on ## self.galaxies.lens.hyper_galaxy = ( results.last.hyper_combined.instance.galaxies.lens.hyper_galaxy ) phase4 = InversionPhase( phase_name="phase_4__initialize_magnification_inversion", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.EllipticalSersic, mass=al.mp.EllipticalIsothermal, shear=al.mp.ExternalShear, ), source=al.GalaxyModel( redshift=1.0, pixelization=al.pix.VoronoiMagnification, regularization=al.reg.Constant, ), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase4.optimizer.const_efficiency_mode = True phase4.optimizer.n_live_points = 20 phase4.optimizer.sampling_efficiency = 0.8 phase4 = phase4.extend_with_multiple_hyper_phases(hyper_galaxy=True, inversion=False) class InversionPhase(al.PhaseInterferometer): def customize_priors(self, results): ## Lens Light & Mass, Sersic -> Sersic, SIE -> SIE, Shear -> Shear ### self.galaxies.lens = results.from_phase( "phase_3__lens_sersic_sie__source_sersic").model.galaxies.lens ### Source Inversion, Inv -> Inv ### self.galaxies.source.pixelization = results.from_phase( "phase_4__initialize_magnification_inversion" ).instance.galaxies.source.pixelization self.galaxies.source.regularization = results.from_phase( "phase_4__initialize_magnification_inversion" ).instance.galaxies.source.regularization ## Set all hyper-galaxies if feature is turned on ## self.galaxies.lens.hyper_galaxy = ( results.last.hyper_combined.instance.galaxies.lens.hyper_galaxy ) phase5 = InversionPhase( phase_name="phase_5__lens_sersic_sie__source_magnification_inversion", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.EllipticalSersic, mass=al.mp.EllipticalIsothermal, shear=al.mp.ExternalShear, ), source=al.GalaxyModel( redshift=1.0, pixelization=pipeline_pixelization, regularization=pipeline_regularization, ), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase5.optimizer.const_efficiency_mode = True phase5.optimizer.n_live_points = 75 phase5.optimizer.sampling_efficiency = 0.2 phase5 = phase5.extend_with_multiple_hyper_phases( hyper_galaxy=True, include_background_sky=True, include_background_noise=True, inversion=False, ) class InversionPhase(al.PhaseInterferometer): def customize_priors(self, results): ## Lens Light & Mass, Sersic -> Sersic, SIE -> SIE, Shear -> Shear ### self.galaxies.lens.light = results.from_phase( "phase_5__lens_sersic_sie__source_magnification_inversion" ).instance.galaxies.lens.light self.galaxies.lens.mass = results.from_phase( "phase_5__lens_sersic_sie__source_magnification_inversion" ).instance.galaxies.lens.mass self.galaxies.lens.shear = results.from_phase( "phase_5__lens_sersic_sie__source_magnification_inversion" ).instance.galaxies.lens.shear ## Set all hyper-galaxies if feature is turned on ## self.galaxies.lens.hyper_galaxy = ( results.last.hyper_combined.instance.galaxies.lens.hyper_galaxy ) phase6 = InversionPhase( phase_name="phase_6_initialize_inversion", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.EllipticalSersic, mass=al.mp.EllipticalIsothermal, shear=al.mp.ExternalShear, ), source=al.GalaxyModel( redshift=1.0, pixelization=pipeline_pixelization, regularization=pipeline_regularization, ), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase6.optimizer.const_efficiency_mode = True phase6.optimizer.n_live_points = 20 phase6.optimizer.sampling_efficiency = 0.8 phase6 = phase6.extend_with_multiple_hyper_phases( hyper_galaxy=True, include_background_sky=True, include_background_noise=True, inversion=True, ) class InversionPhase(al.PhaseInterferometer): def customize_priors(self, results): ## Lens Light & Mass, Sersic -> Sersic, SIE -> SIE, Shear -> Shear ### self.galaxies.lens = results.from_phase( "phase_5__lens_sersic_sie__source_magnification_inversion" ).model.galaxies.lens ### Source Inversion, Inv -> Inv ### self.galaxies.source.pixelization = results.from_phase( "phase_6_initialize_inversion" ).hyper_combined.instance.galaxies.source.pixelization self.galaxies.source.regularization = results.from_phase( "phase_6_initialize_inversion" ).hyper_combined.instance.galaxies.source.regularization ## Set all hyper-galaxies if feature is turned on ## self.galaxies.lens.hyper_galaxy = ( results.last.hyper_combined.instance.galaxies.lens.hyper_galaxy ) self.galaxies.source.hyper_galaxy = ( results.last.hyper_combined.instance.galaxies.source. hyper_galaxy) phase7 = InversionPhase( phase_name="phase_7__lens_sersic_sie__source_inversion", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, light=al.lp.EllipticalSersic, mass=al.mp.EllipticalIsothermal, shear=al.mp.ExternalShear, ), source=al.GalaxyModel( redshift=1.0, pixelization=pipeline_pixelization, regularization=pipeline_regularization, ), ), real_space_shape_2d=real_space_shape_2d, real_space_pixel_scales=real_space_pixel_scales, optimizer_class=optimizer_class, ) phase7.optimizer.const_efficiency_mode = True phase7.optimizer.n_live_points = 75 phase7.optimizer.sampling_efficiency = 0.2 phase7 = phase7.extend_with_multiple_hyper_phases( hyper_galaxy=True, include_background_sky=True, include_background_noise=True, inversion=True, ) return al.PipelineDataset(name, phase1, phase2, phase3, phase4, phase5, phase6, phase7)