def test__hyper_galaxies_names_and_tag_for_lens_and_source(self): setup = al.SetupHyper(hyper_galaxies_lens=False, hyper_galaxies_source=False) assert setup.hyper_galaxies is False assert setup.hyper_galaxies_tag == "" assert setup.hyper_galaxy_names == None setup = al.SetupHyper(hyper_galaxies_lens=True, hyper_galaxies_source=False) assert setup.hyper_galaxies is True assert setup.hyper_galaxies_tag == "galaxies_lens" assert setup.hyper_galaxy_names == ["lens"] setup = al.SetupHyper(hyper_galaxies_lens=False, hyper_galaxies_source=True) assert setup.hyper_galaxies is True assert setup.hyper_galaxies_tag == "galaxies_source" assert setup.hyper_galaxy_names == ["source"] setup = al.SetupHyper(hyper_galaxies_lens=True, hyper_galaxies_source=True) assert setup.hyper_galaxies is True assert setup.hyper_galaxies_tag == "galaxies_lens_source" assert setup.hyper_galaxy_names == ["lens", "source"]
def test__hyper_fixed_after_source(self): hyper = al.SetupHyper(hyper_fixed_after_source=False) assert hyper.hyper_fixed_after_source_tag == "" hyper = al.SetupHyper(hyper_fixed_after_source=True) assert hyper.hyper_fixed_after_source_tag == "__fixed_from_source"
def test__tag(self): setup_hyper = al.SetupHyper(hyper_image_sky=None, hyper_background_noise=None) assert setup_hyper.tag == "" setup_hyper = al.SetupHyper( hyper_image_sky=al.hyper_data.HyperImageSky) assert setup_hyper.tag == "hyper[__bg_sky]" setup_hyper = al.SetupHyper( hyper_galaxies_lens=True, hyper_galaxies_source=False, hyper_image_sky=al.hyper_data.HyperImageSky, hyper_background_noise=True, ) assert setup_hyper.tag == "hyper[galaxies_lens__bg_sky__bg_noise]" setup_hyper = al.SetupHyper( hyper_galaxies_lens=True, hyper_galaxies_source=True, hyper_background_noise=al.hyper_data.HyperBackgroundNoise, hyper_fixed_after_source=True, ) assert (setup_hyper.tag == "hyper[galaxies_lens_source__bg_noise__fixed_from_source]")
def test__tag(self): hyper = al.SetupHyper( hyper_galaxies_lens=True, hyper_background_noise=al.hyper_data.HyperBackgroundNoise, hyper_image_sky=al.hyper_data.HyperImageSky, ) setup_mass = al.SetupMassLightDark(align_bulge_dark_centre=True) setup = al.SetupPipeline(setup_hyper=hyper, setup_mass=setup_mass) assert ( setup.tag == "setup__" "hyper[galaxies_lens__bg_sky__bg_noise]__" "mass[light_dark__bulge_sersic__disk_exp__mlr_free__dark_nfw_ludlow__with_shear__align_bulge_dark_centre]" ) setup_source = al.SetupSourceInversion( pixelization_prior_model=al.pix.Rectangular, regularization_prior_model=al.reg.Constant, ) setup_light = al.SetupLightParametric(light_centre=(1.0, 2.0)) setup_mass = al.SetupMassLightDark(mass_centre=(3.0, 4.0), with_shear=False) setup = al.SetupPipeline(setup_source=setup_source, setup_light=setup_light, setup_mass=setup_mass) assert ( setup.tag == "setup__" "light[parametric__bulge_sersic__disk_exp__align_bulge_disk_centre__centre_(1.00,2.00)]__" "mass[light_dark__bulge_sersic__disk_exp__mlr_free__dark_nfw_ludlow__no_shear__centre_(3.00,4.00)]__" "source[inversion__pix_rect__reg_const]") setup_mass = al.SetupMassLightDark(align_bulge_dark_centre=True) setup = al.SetupPipeline(setup_mass=setup_mass) assert ( setup.tag == "setup__" "mass[light_dark__bulge_sersic__disk_exp__mlr_free__dark_nfw_ludlow__with_shear__align_bulge_dark_centre]" ) smbh = al.SetupSMBH(smbh_centre_fixed=True) setup_subhalo = al.SetupSubhalo( subhalo_instance=al.mp.SphericalNFWMCRLudlow(centre=(1.0, 2.0), mass_at_200=1e8)) setup = al.SetupPipeline(setup_smbh=smbh, setup_subhalo=setup_subhalo) assert ( setup.tag == "setup__" "smbh[point_mass__centre_fixed]__" "subhalo[nfw_sph_ludlow__mass_is_model__source_is_model__grid_5__centre_(1.00,2.00)__mass_1.0e+08]" )
# %% """ __HYPER SETUP__ The _SetupHyper_ determines which hyper-mode features are used during the model-fit as is used identically to the hyper pipeline examples. The _SetupHyper_ object has a new input available, 'hyper_fixed_after_source', which fixes the hyper-parameters to the values computed by the hyper-phase at the end of the Source pipeline. By fixing the hyper-parameter values in the _SLaMPipelineLight_ and _SLaMPipelineMass_ pipelines, model comparison can be performed in a consistent fashion. """ # %% hyper = al.SetupHyper( hyper_galaxies_lens=False, hyper_image_sky=False, hyper_background_noise=False, evidence_tolerance=20.0, ) # %% """ __SLaMPipelineSourceParametric__ The parametric source pipeline aims to initialize a robust model for the source galaxy using _LightProfile_ objects. _SLaMPipelineSourceParametric_ determines the source model used by the parametric source pipeline. A full description of all options can be found ? and ?. By default, this assumes an _EllipticalIsothermal_ profile for the lens galaxy's mass. Our experience with lens modeling has shown they are the simpliest models that provide a good fit to the majority of strong lenses.
__Redshifts__ The redshifts of the lens and source galaxies, which are used to perform unit converions of the model and data (e.g. from arc-seconds to kiloparsecs, masses to solar masses, etc.). """ redshift_lens = 0.5 redshift_source = 1.0 """ __HYPER SETUP__ The `SetupHyper` determines which hyper-mode features are used during the model-fit. """ setup_hyper = al.SetupHyper( hyper_galaxies_lens=False, hyper_galaxies_source=False, hyper_image_sky=None, hyper_background_noise=None, ) """ __SOURCE PARAMETRIC PIPELINE (with lens light)__ The SOURCE PARAMETRIC PIPELINE (with lens light) uses three searches to initialize a robust model for the source galaxy's light, which in this example: - Uses a parametric `EllSersic` bulge and `EllExponential` disk with centres aligned for the lens galaxy's light. - Uses an `EllIsothermal` model for the lens's total mass distribution with an `ExternalShear`. __Settings__:
""" __HYPER SETUP__ The `SetupHyper` determines which hyper-mode features are used during the model-fit and is used identically to the hyper pipeline examples. The `SetupHyper` object has a new input available, `hyper_fixed_after_source`, which fixes the hyper-parameters to the values computed by the hyper-phase at the end of the Source pipeline. By fixing the hyper-parameter values in the _SLaMPipelineLight_ and `SLaMPipelineMass` pipelines, model comparison can be performed in a consistent fashion. """ hyper = al.SetupHyper( hyper_galaxies_lens=False, hyper_galaxies_source=False, hyper_image_sky=False, hyper_background_noise=False, hyper_fixed_after_source=True, ) """ __SLaMPipelineSourceParametric__ The parametric source pipeline aims to initialize a robust model for the source galaxy using `LightProfile` objects. _SLaMPipelineSourceParametric_ determines the source model used by the parametric source pipeline. A full description of all options can be found ? and ?. By default, this assumes an `EllipticalIsothermal` profile for the lens `Galaxy`'s mass and an `EllipticalSersic` + `EllipticalExponential` model for the lens `Galaxy`'s light. Our experience with lens modeling has shown they are the simplest models that provide a good fit to the majority of strong lenses.
""" __HYPER SETUP__ The `SetupHyper` determines which hyper-mode features are used during the model-fit and is used identically to the hyper pipeline examples. The `SetupHyper` object has a new input available, `hyper_fixed_after_source`, which fixes the hyper-parameters to the values computed by the hyper-phase at the end of the Source pipeline. By fixing the hyper-parameter values in the _SLaMPipelineLight_ and `SLaMPipelineMass` pipelines, model comparison can be performed in a consistent fashion. """ hyper = al.SetupHyper( hyper_search_no_inversion=af.DynestyStatic(maxcall=1), hyper_search_with_inversion=af.DynestyStatic(maxcall=1), hyper_galaxies_lens=False, hyper_galaxies_source=False, hyper_image_sky=al.hyper_data.HyperImageSky, hyper_background_noise=None, hyper_fixed_after_source=True, ) """ __SLaMPipelineSourceParametric__ The parametric source pipeline aims to initialize a robust model for the source galaxy using `LightProfile` objects. _SLaMPipelineSourceParametric_ determines the source model used by the parametric source pipeline. A full description of all options can be found ? and ?. By default, this assumes an `EllipticalIsothermal` profile for the lens `Galaxy`'s mass and an `EllipticalSersic` + `EllipticalExponential` model for the lens `Galaxy`'s light. Our experience with lens modeling has shown they are the simplest models that provide a good fit to the majority of strong lenses.
""" """ __HYPER SETUP__ The `SetupHyper` determines which hyper-mode features are used during the model-fit and is used identically to the hyper pipeline examples. The `SetupHyper` object has a new input available, `hyper_fixed_after_source`, which fixes the hyper-parameters to the values computed by the hyper-phase at the end of the Source pipeline. By fixing the hyper-parameter values in the _SLaMPipelineLight_ and `SLaMPipelineMass` pipelines, model comparison can be performed in a consistent fashion. """ hyper = al.SetupHyper( hyper_search_no_inversion=af.DynestyStatic(maxcall=1), hyper_search_with_inversion=af.DynestyStatic(maxcall=1), hyper_galaxies_lens=False, hyper_galaxies_source=False, hyper_image_sky=None, hyper_background_noise=None, ) """ __SLaMPipelineSourceParametric__ The parametric source pipeline aims to initialize a robust model for the source galaxy using `LightProfile` objects. _SLaMPipelineSourceParametric_ determines the source model used by the parametric source pipeline. A full description of all options can be found ? and ?. By default, this assumes an `EllipticalIsothermal` profile for the lens `Galaxy`'s mass. Our experience with lens modeling has shown they are the simpliest models that provide a good fit to the majority of strong lenses. For this runner the `SLaMPipelineSourceParametric` customizes:
- `hyper_galaxies`: whether the lens and / or source galaxy are treated as a hyper-galaxy, meaning that the model-fit can increase the noise-map values in the regions of the lens or source if they are poorly fitted. - `hyper_image_sky`: The background sky subtraction may be included in the model-fitting. - `hyper_background_noise`: The background noise-level may be included in the model-fitting. The pixelization and regularization schemes which use hyper-mode to adapt to the source's properties are not passed into `SetupHyper`, but are used in this example script below. In this example, we a hyper galaxy for the lens and include the background sky subtraction in the model. """ setup_hyper = al.SetupHyper( hyper_galaxies_lens=True, hyper_galaxies_source=False, hyper_image_sky=al.hyper_data.HyperImageSky, hyper_background_noise=None, ) """ __Model-Fits via Searches 1, 2 & 3__ Searches 1, 2 and 3 initialize the lens model by fitting the lens light, then the lens mass + source, and then all simultaneously. This is identical to the pipeline `chaining/pipelines/light_parametric__mass_total__source_inversion.py` We can only use hyper-model once we have a good model for the lens and source galaxies, given that it needs hyper-model images of both of these components to effectively perform tasks like scaling their noise or adapting a pixelization or regularization pattern to the source's unlensed morphology. """ analysis = al.AnalysisImaging(dataset=imaging) bulge = af.Model(al.lp.EllSersic)
""" __HYPER SETUP__ In chapter 5, we will introduce hyper-mode, which pushes lens modeling with PyAutoLens to the limit. This model uses the `SetupHyper` to determine which hyper-mode features are used during the model-fit. In this tutorial's pipeline, you'll note we extend phase 2 with a `hyper` phase to refit and improve the parameters of the `Pixelization` and `Regularization`. This isn't using any of the advanced hyper-mode features that we'll cover in chapter 5, but gives us a simple way to better set up our `Inversion` and is used throughout all the PyAutoLens template pipelines. In the `SetupHyper` below, we specify the `DynestyStatic` non-linear search used to perform this model-fit. """ # %% hyper = al.SetupHyper(hyper_search_with_inversion=af.DynestyStatic( n_live_points=50)) # %% """ 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. """ # %% setup_mass = al.SetupMassTotal(with_shear=True) # %%
""" __HYPER SETUP__ The `SetupHyper` determines which hyper-mode features are used during the model-fit and is used identically to the hyper pipeline examples. The `SetupHyper` object has a new input available, `hyper_fixed_after_source`, which fixes the hyper-parameters to the values computed by the hyper-phase at the end of the Source pipeline. By fixing the hyper-parameter values in the _SLaMPipelineLight_ and `SLaMPipelineMass` pipelines, model comparison can be performed in a consistent fashion. """ hyper = al.SetupHyper( hyper_galaxies_lens=True, hyper_galaxies_source=False, hyper_image_sky=False, hyper_background_noise=True, hyper_fixed_after_source=True, evidence_tolerance=50.0, ) """ __SLaMPipelineSourceParametric__ The parametric source pipeline aims to initialize a robust model for the source galaxy using `LightProfile` objects. _SLaMPipelineSourceParametric_ determines the source model used by the parametric source pipeline. A full description of all options can be found ? and ?. By default, this assumes an `EllipticalIsothermal` profile for the lens `Galaxy`'s mass and an `EllipticalSersic` + `EllipticalExponential` model for the lens `Galaxy`'s light. Our experience with lens modeling has shown they are the simplest models that provide a good fit to the majority of strong lenses.
def test__phase_is_extended_with_hyper_phases__sets_up_hyper_images( self, interferometer_7, mask_7x7): galaxies = af.ModelInstance() galaxies.lens = al.Galaxy(redshift=0.5) galaxies.source = al.Galaxy(redshift=1.0) instance = af.ModelInstance() instance.galaxies = galaxies hyper_galaxy_image_path_dict = { ("galaxies", "lens"): al.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0), ("galaxies", "source"): al.Array2D.full(fill_value=2.0, shape_native=(3, 3), pixel_scales=1.0), } hyper_galaxy_visibilities_path_dict = { ("galaxies", "lens"): al.Visibilities.full(fill_value=4.0, shape_slim=(7, )), ("galaxies", "source"): al.Visibilities.full(fill_value=5.0, shape_slim=(7, )), } results = mock.MockResults( hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict, hyper_model_image=al.Array2D.full(fill_value=3.0, shape_native=(3, 3), pixel_scales=1.0), hyper_galaxy_visibilities_path_dict= hyper_galaxy_visibilities_path_dict, hyper_model_visibilities=al.Visibilities.full(fill_value=6.0, shape_slim=(7, )), mask=mask_7x7, use_as_hyper_dataset=True, ) phase_interferometer_7 = al.PhaseInterferometer( galaxies=dict(lens=al.GalaxyModel(redshift=0.5, hyper_galaxy=al.HyperGalaxy)), search=mock.MockSearch("test_phase"), real_space_mask=mask_7x7, ) phase_interferometer_7.extend_with_hyper_phase( setup_hyper=al.SetupHyper()) analysis = phase_interferometer_7.make_analysis( dataset=interferometer_7, mask=mask_7x7, results=results) assert (analysis.hyper_galaxy_image_path_dict[( "galaxies", "lens")].native == np.ones((3, 3))).all() assert (analysis.hyper_galaxy_image_path_dict[( "galaxies", "source")].native == 2.0 * np.ones((3, 3))).all() assert (analysis.hyper_model_image.native == 3.0 * np.ones( (3, 3))).all() assert (analysis.hyper_galaxy_visibilities_path_dict[( "galaxies", "lens")] == (4.0 + 4.0j) * np.ones((7, ))).all() assert (analysis.hyper_galaxy_visibilities_path_dict[( "galaxies", "source")] == (5.0 + 5.0j) * np.ones((7, ))).all() assert (analysis.hyper_model_visibilities == (6.0 + 6.0j) * np.ones( (7, ))).all()
""" __PIPELINE SETUP__ Pipelines use `Setup` objects to customize how different aspects of the model are fitted. The `SetupHyper` object controls the behaviour of hyper-mode specifically: - If hyper-galaxies are used to scale the noise in the lens and source galaxies in image (default False) - If the level of background noise is modeled throughout the pipeline (default False) - If the background sky is modeled throughout the pipeline (default False) """ setup_hyper = al.SetupHyper( hyper_galaxies_lens=False, hyper_galaxies_source=False, hyper_background_noise=None, hyper_image_sky= None, # <- By default this feature is off, as it rarely changes the lens model. ) """ Next, we create a `SetupMassTotal`, which customizes: - The `MassProfile` used to fit the lens's total mass distribution. - If there is an `ExternalShear` in the mass model or not. """ setup_mass = al.SetupMassTotal(mass_prior_model=al.mp.EllipticalPowerLaw, with_shear=True) """ In hyper-mode, we can use the `VoronoiBrightnessImage` `Pixelization` and `AdaptiveBrightness` `Regularization` scheme, which adapts the `Pixelization` and `Regularization` to the morphology of the lensed source galaxy using the