def make_pipeline(name, folders, search=af.DynestyStatic()): phase1 = al.PhaseImaging( 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), ), search=search, ) phase2 = al.PhaseImaging( 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), ), search=search, ) return al.PipelineDataset(name, phase1, phase2)
def make_pipeline(name, phase_folders, optimizer_class=af.MultiNest): sersic = af.PriorModel(al.lp.EllipticalSersic) # This will lead to pretty weird results sersic.add_assertion(sersic.axis_ratio > sersic.intensity) phase1 = al.PhaseImaging( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict(lens=al.GalaxyModel(redshift=0.5, sersic=sersic)), optimizer_class=optimizer_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 40 phase1.optimizer.sampling_efficiency = 0.8 # TODO : And even with them not causing errors above, the promise doesnt work. phase2 = al.PhaseImaging( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict(lens=phase1.result.model.galaxies.lens), 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__results_of_phase_include_positions__available_as_property( self, imaging_7x7, mask_7x7 ): clean_images() phase_imaging_7x7 = al.PhaseImaging( optimizer_class=mock_pipeline.MockNLO, galaxies=[ al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=1.0)) ], phase_name="test_phase_2", ) result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7) assert result.positions == None phase_imaging_7x7 = al.PhaseImaging( optimizer_class=mock_pipeline.MockNLO, galaxies=dict( lens=al.Galaxy( redshift=0.5, light=al.lp.EllipticalSersic(intensity=1.0) ), source=al.Galaxy(redshift=1.0), ), positions_threshold=1.0, phase_name="test_phase_2", ) result = phase_imaging_7x7.run( dataset=imaging_7x7, mask=mask_7x7, positions=[[(1.0, 1.0)]] ) assert (result.positions[0] == np.array([1.0, 1.0])).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 make_pipeline(name, phase_folders, non_linear_class=af.MultiNest): phase1 = al.PhaseImaging( 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, sersic=al.lp.EllipticalSersic), ), 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.PhaseImaging( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict( lens=phase1.result.model.galaxies.lens, source_0=phase1.result.model.galaxies.source_0, source_1=al.GalaxyModel(redshift=1.0, sersic=al.lp.EllipticalSersic), source_2=al.GalaxyModel(redshift=1.0, sersic=al.lp.EllipticalSersic), ), 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, non_linear_class=af.MultiNest): phase1 = al.PhaseImaging( 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, sersic=al.lp.EllipticalSersic), ), sub_size=1, non_linear_class=non_linear_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 20 phase1.optimizer.sampling_efficiency = 0.8 # We want to set up the source from the result, where: # If it is parametric, it is a model (thus N = 12). # If it is an inversion, it is an instance (Thus N = 5) # When we use af.last, this fails, because the promise pixelizzation attribute is another promise. source = source_with_previous_model_or_instance() phase2 = al.PhaseImaging( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict(lens=phase1.result.model.galaxies.lens, source=source), sub_size=1, non_linear_class=non_linear_class, ) return al.PipelineDataset(name, phase1, phase2)
def test__inversion_pixel_limit_computed_via_config_or_input( self, mask_function_7x7 ): phase_imaging_7x7 = al.PhaseImaging( phase_name="phase_imaging_7x7", mask_function=mask_function_7x7, inversion_pixel_limit=None, ) assert phase_imaging_7x7.meta_data_fit.inversion_pixel_limit == 3000 phase_imaging_7x7 = al.PhaseImaging( phase_name="phase_imaging_7x7", mask_function=mask_function_7x7, inversion_pixel_limit=10, ) assert phase_imaging_7x7.meta_data_fit.inversion_pixel_limit == 10 phase_imaging_7x7 = al.PhaseImaging( phase_name="phase_imaging_7x7", mask_function=mask_function_7x7, inversion_pixel_limit=2000, ) assert phase_imaging_7x7.meta_data_fit.inversion_pixel_limit == 2000
def make_pipeline(name, phase_folders, optimizer_class=af.MultiNest): phase1 = al.PhaseImaging( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic)), 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.PhaseImaging( 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, )), 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__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( signal_to_noise_limit=1.0) phase_imaging_7x7 = al.PhaseImaging(phase_name="phase_imaging_7x7", signal_to_noise_limit=1.0) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7_1_pix) assert (analysis.masked_dataset.image.in_2d == imaging_snr_limit.image.in_2d * np.invert(mask_7x7_1_pix)).all() assert (analysis.masked_dataset.noise_map.in_2d == imaging_snr_limit.noise_map.in_2d * np.invert(mask_7x7_1_pix)).all() imaging_snr_limit = imaging_7x7.signal_to_noise_limited_from_signal_to_noise_limit( signal_to_noise_limit=0.1) phase_imaging_7x7 = al.PhaseImaging(phase_name="phase_imaging_7x7", signal_to_noise_limit=0.1) analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7_1_pix) assert (analysis.masked_dataset.image.in_2d == imaging_snr_limit.image.in_2d * np.invert(mask_7x7_1_pix)).all() assert (analysis.masked_dataset.noise_map.in_2d == imaging_snr_limit.noise_map.in_2d * np.invert(mask_7x7_1_pix)).all()
def make_pipeline(name, phase_folders, optimizer_class=af.MultiNest): phase1 = al.PhaseImaging( phase_name="phase_1", phase_folders=phase_folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, light=al.light_profiles.EllipticalSersic, light_1=None)), optimizer_class=optimizer_class, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 20 phase1.optimizer.sampling_efficiency = 0.8 phase2 = al.PhaseImaging( phase_name="phase_2", phase_folders=phase_folders, galaxies=dict(lens=al.GalaxyModel( redshift=0.5, light=al.light_profiles.EllipticalSersic, light_1=phase1.result.constant.galaxies.lens.light_1, )), optimizer_class=optimizer_class, ) phase2.optimizer.const_efficiency_mode = True phase2.optimizer.n_live_points = 20 phase2.optimizer.sampling_efficiency = 0.8 return al.PipelineImaging(name, phase1, phase2)
def make_pipeline(name, phase_folders, non_linear_class=af.MultiNest): phase1 = al.PhaseImaging( 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), ), non_linear_class=non_linear_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, include_background_sky=True, include_background_noise=True) phase2 = al.PhaseImaging( 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, ), ), hyper_image_sky=phase1.result.hyper_combined.instance.hyper_image_sky, hyper_background_noise=phase1.result.hyper_combined.instance. hyper_background_noise, non_linear_class=non_linear_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, include_background_sky=True, include_background_noise=True) return al.PipelineDataset(name, phase1, phase2)
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__extended_with_hyper_and_pixelizations(self): phase_no_pixelization = al.PhaseImaging( optimizer_class=mock_pipeline.MockNLO, phase_name="test_phase" ) phase_extended = phase_no_pixelization.extend_with_multiple_hyper_phases( hyper_galaxy=False, inversion=False ) assert phase_extended == phase_no_pixelization # This phase does not have a pixelization, so even though inversion=True it will not be extended phase_extended = phase_no_pixelization.extend_with_multiple_hyper_phases( inversion=True ) assert phase_extended == phase_no_pixelization phase_with_pixelization = al.PhaseImaging( galaxies=dict( source=al.GalaxyModel( redshift=0.5, pixelization=al.pix.Rectangular, regularization=al.reg.Constant, ) ), optimizer_class=mock_pipeline.MockNLO, phase_name="test_phase", ) phase_extended = phase_with_pixelization.extend_with_multiple_hyper_phases( inversion=True ) assert type(phase_extended.hyper_phases[0]) == al.InversionPhase phase_extended = phase_with_pixelization.extend_with_multiple_hyper_phases( hyper_galaxy=True, inversion=False ) assert type(phase_extended.hyper_phases[0]) == al.HyperGalaxyPhase phase_extended = phase_with_pixelization.extend_with_multiple_hyper_phases( hyper_galaxy=False, inversion=True ) assert type(phase_extended.hyper_phases[0]) == al.InversionPhase phase_extended = phase_with_pixelization.extend_with_multiple_hyper_phases( hyper_galaxy=True, inversion=True ) assert type(phase_extended.hyper_phases[0]) == al.InversionPhase assert type(phase_extended.hyper_phases[1]) == al.HyperGalaxyPhase phase_extended = phase_with_pixelization.extend_with_multiple_hyper_phases( hyper_galaxy=True, inversion=True, hyper_galaxy_phase_first=True ) assert type(phase_extended.hyper_phases[0]) == al.HyperGalaxyPhase assert type(phase_extended.hyper_phases[1]) == al.InversionPhase
def test__duplication(self): phase_dataset_7x7 = al.PhaseImaging( phase_name="test_phase", galaxies=dict(lens=al.GalaxyModel(redshift=0.5), source=al.GalaxyModel(redshift=1.0)), ) al.PhaseImaging(phase_name="test_phase") assert phase_dataset_7x7.galaxies is not None
def test__use_border__determines_if_border_pixel_relocation_is_used( self, imaging_7x7, mask_7x7 ): # noinspection PyTypeChecker lens_galaxy = al.Galaxy( redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=100.0) ) source_galaxy = al.Galaxy( redshift=1.0, pixelization=al.pix.Rectangular(shape=(3, 3)), regularization=al.reg.Constant(coefficient=1.0), ) phase_imaging_7x7 = al.PhaseImaging( galaxies=[lens_galaxy, source_galaxy], cosmology=cosmo.Planck15, phase_name="test_phase", inversion_uses_border=True, ) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=mock_pipeline.MockResults() ) analysis.masked_dataset.grid[4] = np.array([[500.0, 0.0]]) instance = phase_imaging_7x7.model.instance_from_unit_vector([]) tracer = analysis.tracer_for_instance(instance=instance) fit = analysis.masked_imaging_fit_for_tracer( tracer=tracer, hyper_image_sky=None, hyper_background_noise=None ) assert fit.inversion.mapper.grid[4][0] == pytest.approx(97.19584, 1.0e-2) assert fit.inversion.mapper.grid[4][1] == pytest.approx(-3.699999, 1.0e-2) phase_imaging_7x7 = al.PhaseImaging( galaxies=[lens_galaxy, source_galaxy], cosmology=cosmo.Planck15, phase_name="test_phase", inversion_uses_border=False, ) analysis = phase_imaging_7x7.make_analysis( dataset=imaging_7x7, mask=mask_7x7, results=mock_pipeline.MockResults() ) analysis.masked_dataset.grid[4] = np.array([300.0, 0.0]) instance = phase_imaging_7x7.model.instance_from_unit_vector([]) tracer = analysis.tracer_for_instance(instance=instance) fit = analysis.masked_imaging_fit_for_tracer( tracer=tracer, hyper_image_sky=None, hyper_background_noise=None ) assert fit.inversion.mapper.grid[4][0] == pytest.approx(200.0, 1.0e-4)
def make_pipeline(name, folders, search=af.DynestyStatic()): phase1 = al.PhaseImaging( phase_name="phase_1", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=al.mp.EllipticalIsothermal), source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic), ), search=af.DynestyStatic(n_live_points=40, evidence_tolerance=10.0), ) pixeliation = af.PriorModel(al.pix.VoronoiBrightnessImage) pixeliation.pixels = 100 phase1 = phase1.extend_with_multiple_hyper_phases(setup=al.SetupPipeline(), include_inversion=False) phase2 = al.PhaseImaging( phase_name="phase_2", folders=folders, galaxies=dict( lens=al.GalaxyModel( redshift=0.5, mass=phase1.result.instance.galaxies.lens.mass), source=al.GalaxyModel( redshift=1.0, pixelization=pixeliation, regularization=al.reg.AdaptiveBrightness, ), ), search=af.DynestyStatic(n_live_points=40, evidence_tolerance=10.0), ) phase2 = phase2.extend_with_multiple_hyper_phases(setup=al.SetupPipeline(), include_inversion=True) phase3 = al.PhaseImaging( phase_name="phase_3", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=phase1.result.model.galaxies.lens.mass), source=al.GalaxyModel( redshift=1.0, pixelization=phase2.result.instance.galaxies.source. pixelization, regularization=phase2.result.instance.galaxies.source. regularization, ), ), search=af.DynestyStatic(n_live_points=40, evidence_tolerance=10.0), ) return al.PipelineDataset(name, phase1, phase2, phase3)
def test__tracer_for_instance__includes_cosmology( self, imaging_data_7x7, mask_function_7x7 ): lens_galaxy = al.Galaxy(redshift=0.5) source_galaxy = al.Galaxy(redshift=0.5) phase_imaging_7x7 = al.PhaseImaging( mask_function=mask_function_7x7, galaxies=[lens_galaxy], cosmology=cosmo.FLRW, phase_name="test_phase", ) analysis = phase_imaging_7x7.make_analysis(data=imaging_data_7x7) instance = phase_imaging_7x7.variable.instance_from_unit_vector([]) tracer = analysis.tracer_for_instance(instance=instance) assert tracer.image_plane.galaxies[0] == lens_galaxy assert tracer.cosmology == cosmo.FLRW phase_imaging_7x7 = al.PhaseImaging( mask_function=mask_function_7x7, galaxies=[lens_galaxy, source_galaxy], cosmology=cosmo.FLRW, phase_name="test_phase", ) analysis = phase_imaging_7x7.make_analysis(imaging_data_7x7) instance = phase_imaging_7x7.variable.instance_from_unit_vector([]) tracer = analysis.tracer_for_instance(instance) assert tracer.image_plane.galaxies[0] == lens_galaxy assert tracer.source_plane.galaxies[0] == source_galaxy assert tracer.cosmology == cosmo.FLRW galaxy_0 = al.Galaxy(redshift=0.1) galaxy_1 = al.Galaxy(redshift=0.2) galaxy_2 = al.Galaxy(redshift=0.3) phase_imaging_7x7 = al.PhaseImaging( mask_function=mask_function_7x7, galaxies=[galaxy_0, galaxy_1, galaxy_2], cosmology=cosmo.WMAP7, phase_name="test_phase", ) analysis = phase_imaging_7x7.make_analysis(data=imaging_data_7x7) instance = phase_imaging_7x7.variable.instance_from_unit_vector([]) tracer = analysis.tracer_for_instance(instance) assert tracer.planes[0].galaxies[0] == galaxy_0 assert tracer.planes[1].galaxies[0] == galaxy_1 assert tracer.planes[2].galaxies[0] == galaxy_2 assert tracer.cosmology == cosmo.WMAP7
def make_pipeline(name, phase_folders, non_linear_class=af.MultiNest): phase1 = al.PhaseImaging( phase_name="phase_1__lens_bulge_disk", 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), ), non_linear_class=af.MultiNest, ) phase1.optimizer.const_efficiency_mode = True phase1.optimizer.n_live_points = 40 phase1.optimizer.sampling_efficiency = 0.3 lens = al.setup.lens_with_light_only_from_result(result=af.last, fix_lens_light=True) # 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 lens.mass = mass lens.hyper_galaxy = ( af.last.hyper_combined.instance.optional.galaxies.lens.hyper_galaxy) phase2 = al.PhaseImaging( phase_name="phase_2__lens_power_law__source", phase_folders=phase_folders, galaxies=dict(lens=lens, source=phase1.result.instance.galaxies.source), hyper_image_sky=af.last.hyper_combined.instance.optional. hyper_image_sky, hyper_background_noise=af.last.hyper_combined.instance.optional. hyper_background_noise, non_linear_class=af.MultiNest, ) phase2.optimizer.const_efficiency_mode = True phase2.optimizer.n_live_points = 75 phase2.optimizer.sampling_efficiency = 0.2 return al.PipelineDataset(name, phase1, phase2)
def test__most_likely_tracer_source_inversion_centres_correct( self, imaging_7x7, mask_7x7): phase_dataset_7x7 = al.PhaseImaging( non_linear_class=mock_pipeline.MockNLO, galaxies=dict( lens=al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=1.0)), source=al.Galaxy( redshift=1.0, pixelization=al.pix.Rectangular((3, 3)), regularization=al.reg.Constant(coefficient=1.0), ), ), phase_name="test_phase_2", ) result = phase_dataset_7x7.run(dataset=imaging_7x7, mask=mask_7x7, results=mock_pipeline.MockResults()) assert result.most_likely_fit.inversion.reconstruction == pytest.approx( np.array([ 0.80707817, 0.80929118, 0.80707817, 0.80929118, 0.81251066, 0.80929118, 0.80707817, 0.80929118, 0.80707817, ]), 1.0e-4, ) assert result.source_plane_inversion_centres == [(0.0, 0.0)] phase_dataset_7x7 = al.PhaseImaging( non_linear_class=mock_pipeline.MockNLO, galaxies=dict( lens=al.Galaxy(redshift=0.5, light=al.lp.EllipticalSersic(intensity=1.0)), source=al.Galaxy(redshift=1.0), ), phase_name="test_phase_2", ) result = phase_dataset_7x7.run(dataset=imaging_7x7, mask=mask_7x7, results=mock_pipeline.MockResults()) assert result.source_plane_inversion_centres == []
def test_assertion_failure(self, imaging_data_7x7, mask_function_7x7): def make_analysis(*args, **kwargs): return mock_pipeline.MockAnalysis(1, 1) phase_imaging_7x7 = al.PhaseImaging( phase_name="phase_name", mask_function=mask_function_7x7, optimizer_class=mock_pipeline.MockNLO, galaxies=dict( lens=al.Galaxy( light=al.light_profiles.EllipticalLightProfile, redshift=1 ) ), ) phase_imaging_7x7.make_analysis = make_analysis result = phase_imaging_7x7.run( data=imaging_data_7x7, results=None, mask=None, positions=None ) assert result is not None phase_imaging_7x7 = al.PhaseImaging( phase_name="phase_name", mask_function=mask_function_7x7, optimizer_class=mock_pipeline.MockNLO, galaxies=dict( lens=al.Galaxy( light=al.light_profiles.EllipticalLightProfile, redshift=1 ) ), ) phase_imaging_7x7.make_analysis = make_analysis result = phase_imaging_7x7.run( data=imaging_data_7x7, results=None, mask=None, positions=None ) assert result is not None class CustomPhase(al.PhaseImaging): def customize_priors(self, results): self.galaxies.lens.light = al.light_profiles.EllipticalLightProfile() phase_imaging_7x7 = CustomPhase( phase_name="phase_name", mask_function=mask_function_7x7, optimizer_class=mock_pipeline.MockNLO, galaxies=dict( lens=al.Galaxy( light=al.light_profiles.EllipticalLightProfile, redshift=1 ) ), ) phase_imaging_7x7.make_analysis = make_analysis
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_data_7x7, mask_function_7x7 ): clean_images() phase_imaging_7x7 = al.PhaseImaging( optimizer_class=mock_pipeline.MockNLO, mask_function=mask_function_7x7, galaxies=dict( lens=al.Galaxy( redshift=0.5, light=al.light_profiles.EllipticalSersic(intensity=1.0), ), source=al.Galaxy( redshift=1.0, pixelization=al.pixelizations.VoronoiMagnification(shape=(2, 3)), regularization=al.regularization.Constant(), ), ), inversion_pixel_limit=6, phase_name="test_phase_2", ) result = phase_imaging_7x7.run(data=imaging_data_7x7) assert isinstance(result.pixelization, al.pixelizations.VoronoiMagnification) assert result.pixelization.shape == (2, 3) phase_imaging_7x7 = al.PhaseImaging( optimizer_class=mock_pipeline.MockNLO, mask_function=mask_function_7x7, galaxies=dict( lens=al.Galaxy( redshift=0.5, light=al.light_profiles.EllipticalSersic(intensity=1.0), ), source=al.Galaxy( redshift=1.0, pixelization=al.pixelizations.VoronoiBrightnessImage(pixels=6), regularization=al.regularization.Constant(), ), ), inversion_pixel_limit=6, phase_name="test_phase_2", ) phase_imaging_7x7.galaxies.source.binned_hyper_galaxy_image_1d = np.ones(9) result = phase_imaging_7x7.run(data=imaging_data_7x7) assert isinstance(result.pixelization, al.pixelizations.VoronoiBrightnessImage) assert result.pixelization.pixels == 6
def make_pipeline(name, folders, 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 phase1 = al.PhaseImaging( phase_name="phase_1", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=mass), source=al.GalaxyModel( redshift=1.0, pixelization=al.pix.Rectangular, regularization=al.reg.Constant, ), ), search=search, ) phase1.search.const_efficiency_mode = True phase1.search.n_live_points = 60 phase1.search.facc = 0.8 phase1.extend_with_multiple_hyper_phases(hyper_galaxies_search=True) phase2 = al.PhaseImaging( phase_name="phase_2", folders=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, ), ), search=search, ) phase2.search.const_efficiency_mode = True phase2.search.n_live_points = 40 phase2.search.facc = 0.8 return al.PipelineDataset(name, phase1, phase2)
def make_pipeline(name, folders, 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 phase1 = al.PhaseImaging( phase_name="phase_1", folders=folders, galaxies=dict( lens=al.GalaxyModel(redshift=0.5, mass=mass), source=al.GalaxyModel( redshift=1.0, pixelization=al.pix.VoronoiMagnification, regularization=al.reg.Constant, ), ), 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.PhaseImaging( 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, ), ), 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 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__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 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 make_pipeline(name, folders, 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.PhaseImaging( 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, ), ), 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__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__fit_figure_of_merit__includes_hyper_image_and_noise__matches_fit( self, imaging_data_7x7, mask_function_7x7): hyper_image_sky = al.HyperImageSky(sky_scale=1.0) hyper_background_noise = al.HyperBackgroundNoise(noise_scale=1.0) lens_galaxy = al.Galaxy( redshift=0.5, light=al.light_profiles.EllipticalSersic(intensity=0.1)) phase_imaging_7x7 = al.PhaseImaging( mask_function=mask_function_7x7, galaxies=[lens_galaxy], hyper_image_sky=hyper_image_sky, hyper_background_noise=hyper_background_noise, cosmology=cosmo.FLRW, phase_name="test_phase", ) analysis = phase_imaging_7x7.make_analysis(data=imaging_data_7x7) instance = phase_imaging_7x7.variable.instance_from_unit_vector([]) fit_figure_of_merit = analysis.fit(instance=instance) mask = phase_imaging_7x7.meta_data_fit.mask_function( image=imaging_data_7x7.image, sub_size=2) lens_data = al.LensImagingData(imaging_data=imaging_data_7x7, mask=mask) tracer = analysis.tracer_for_instance(instance=instance) fit = al.LensImagingFit.from_lens_data_and_tracer( lens_data=lens_data, tracer=tracer, hyper_image_sky=hyper_image_sky, hyper_background_noise=hyper_background_noise, ) assert fit.likelihood == fit_figure_of_merit