Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
We use absolute and relative values for different parameters, depending on their properties. For example, using the
relative value of a parameter like the _Profile_ centre makes no sense. If our lens galaxy is centred at (0.0, 0.0),
the relative error will always be tiny and thus poorly defined. Therefore, the default configs in __PyAutoLens__ use
absolute errors on the centre.
However, there are parameters where using an absolute value does not make sense. Intensity is a good example of this.
The intensity of an image depends on its unit_label, S/N, galaxy brightness, etc. There is no single absolute value
that one can use to generically link the intensity of any two proflies. Thus, it makes more sense to link them using
the relative value from a previous phase.
We can customize how priors are passed from the results of a phase and non-linear search by inputting to the search
a PriorPasser object:
"""
search = af.DynestyStatic(
prior_passer=af.PriorPasser(sigma=2.0, use_widths=False, use_errors=True)
)
# %%
"""
The PriorPasser allows us to customize at what sigma the error values the model results are computed at to compute
__Pipeline_Setup_And_Tagging__:
The setup module customizes the behaviour of a pipeline. Hyper-fitting brings with it the following setup:
- If hyper-galaxies are used to scale the noise in each component of the image (default True)
- If the level of background noise is modeled throughout the pipeline (default True)
- If the background sky is modeled throughout the pipeline (default False)
Each of these features uses their own non-linear search in extended 'hyper phases', which are also specified in the
_PipelineSetup-.
"""
# %%
hyper_galaxies_search = af.DynestyStatic(n_live_points=100, evidence_tolerance=0.8)
inversion_search = af.DynestyStatic(n_live_points=30, evidence_tolerance=0.8)
hyper_combined_search = af.DynestyStatic(n_live_points=50, evidence_tolerance=0.8)
setup = al.PipelineSetup(
hyper_galaxies=True,
hyper_background_noise=True,
hyper_image_sky=False, # <- By default this feature is off, as it rarely changes the lens model.
hyper_galaxies_search=hyper_galaxies_search,
inversion_search=inversion_search,
hyper_combined_search=hyper_combined_search,
pixelization=al.pix.VoronoiBrightnessImage,
regularization=al.reg.AdaptiveBrightness,
folders=["c5_t6_hyper"],
)
# %%
"""
Lets import the pipeline and run it.
"""
# %%
"""
__Pipeline_Setup_And_Tagging__:
The setup module customizes the behaviour of a pipeline. Hyper-fitting brings with it the following setup:
- If hyper-galaxies are used to scale the noise in each component of the image (default True)
- If the level of background noise is modeled throughout the pipeline (default True)
- If the background sky is modeled throughout the pipeline (default False)
Each of these features uses their own non-linear search in extended 'hyper phases', which are also specified in the
_PipelineSetup-.
"""
# %%
hyper_galaxies_search = af.DynestyStatic(n_live_points=100, evidence_tolerance=0.8)
inversion_search = af.DynestyStatic(n_live_points=30, evidence_tolerance=0.8)
hyper_combined_search = af.DynestyStatic(n_live_points=50, evidence_tolerance=0.8)
setup = al.PipelineSetup(
hyper_galaxies=True,
hyper_background_noise=True,
hyper_image_sky=False, # <- By default this feature is off, as it rarely changes the lens model.
hyper_galaxies_search=hyper_galaxies_search,
inversion_search=inversion_search,
hyper_combined_search=hyper_combined_search,
pixelization=al.pix.VoronoiBrightnessImage,
regularization=al.reg.AdaptiveBrightness,
folders=["c5_t6_hyper"],
)
# %%
"""
psf_path=f"{dataset_path}/psf.fits",
pixel_scales=0.1,
)
mask = al.Mask.circular(
shape_2d=imaging.shape_2d, pixel_scales=imaging.pixel_scales, radius=3.0
)
phase = al.PhaseImaging(
phase_name="phase_t1_non_linear_search",
settings=al.PhaseSettingsImaging(grid_class=al.Grid, sub_size=2),
galaxies=dict(
lens_galaxy=al.GalaxyModel(redshift=0.5, mass=al.mp.SphericalIsothermal),
source_galaxy=al.GalaxyModel(redshift=1.0, light=al.lp.SphericalExponential),
),
search=af.DynestyStatic(n_live_points=40),
)
# result = phase.run(dataset=imaging, mask=mask)
# %%
"""
In the previous tutorials, we saw that this result contains the maximum log likelihood tracer and fit, which provide
a fast way to visualize the result.
(Uncomment the line below to pllot the tracer).
"""
# aplt.Tracer.subplot_tracer(
# tracer=result.max_log_likelihood_tracer, grid=mask.geometry.unmasked_grid
# )
# aplt.FitImaging.subplot_fit_imaging(fit=result.max_log_likelihood_fit)
"""
# %%
lens.mass.elliptical_comps = lens.light.elliptical_comps
# %%
"""
Again, we create this phase and run it. The non-linear search now has a less complex parameter space to search.
"""
# %%
phase_light_traces_mass = al.PhaseImaging(
phase_name="phase_t4_light_traces_mass",
settings=settings,
galaxies=dict(lens=lens, source=source),
search=af.DynestyStatic(n_live_points=40),
)
print(
"Dynesty has begun running - checkout the workspace/output/4_dealing_with_failure"
"folder for live output of the results, images and lens model."
"This Jupyter notebook cell with progress once Dynesty has completed - this could take some time!"
)
# result_light_trace_mass = phase_light_traces_mass.run(dataset=imaging, mask=mask)
print("Dynesty has finished run - you may now continue the notebook.")
# aplt.FitImaging.subplot_fit_imaging(fit=result_light_trace_mass.max_log_likelihood_fit)
# %%
"""
"""
Phase 1: Fit only the lens galaxy's light, where we:
1) Set priors on the lens galaxy (y,x) centre such that we assume the image is centred around the lens galaxy.
We create the phase using the same notation as in chapter 2. Note how we are using the 'fast' _Dynesty_ settings
covered in chapter 2.
"""
phase1 = al.PhaseImaging(
phase_name="phase_1__lens_sersic",
folders=setup.folders,
galaxies=dict(lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic)),
settings=settings,
search=af.DynestyStatic(n_live_points=30, evidence_tolerance=5.0),
)
"""
Phase 2: Fit the lens galaxy's mass and source galaxy's light, where we:
1) Fix the foreground lens light subtraction to the lens galaxy light model from phase 1.
2) Set priors on the centre of the lens galaxy's _MassProfile_ by linking them to those inferred for
the _LightProfile_ in phase 1.
In phase 2, we fit the source-galaxy's light. Thus, we want to fix the lens light model to the model inferred
in phase 1, ensuring the image we fit is lens subtracted. We do this below by passing the lens light as an
'instance' object, a trick we use in nearly all pipelines!
By passing an 'instance', we are telling __PyAutoLens__ that we want it to pass the maximum log likelihood result of
that phase and use those parameters as fixed values in the model. The model parameters passed as an 'instance' are
not free parameters fitted for by the non-linear search, thus this reduces the dimensionality of the non-linear
"""
__Pipeline_Setup_And_Tagging__:
The setup module customizes the behaviour of a pipeline. Hyper-fitting brings with it the following setup:
- If hyper-galaxies are used to scale the noise in each component of the image (default True)
- If the level of background noise is modeled throughout the pipeline (default True)
- If the background sky is modeled throughout the pipeline (default False)
Each of these features uses their own non-linear search in extended 'hyper phases', which are also specified in the
_PipelineSetup-.
"""
# %%
hyper_galaxies_search = af.DynestyStatic(n_live_points=100, evidence_tolerance=0.8)
inversion_search = af.DynestyStatic(n_live_points=30, evidence_tolerance=0.8)
hyper_combined_search = af.DynestyStatic(n_live_points=50, evidence_tolerance=0.8)
setup = al.PipelineSetup(
hyper_galaxies=True,
hyper_background_noise=True,
hyper_image_sky=False, # <- By default this feature is off, as it rarely changes the lens model.
hyper_galaxies_search=hyper_galaxies_search,
inversion_search=inversion_search,
hyper_combined_search=hyper_combined_search,
pixelization=al.pix.VoronoiBrightnessImage,
regularization=al.reg.AdaptiveBrightness,
folders=["c5_t6_hyper"],
)
# %%
"""
Lets import the pipeline and run it.