def make_analysis(self, dataset, mask, results=af.ResultsCollection(), positions=None): """ Create an lens object. Also calls the prior passing and masked_imaging modifying functions to allow child classes to change the behaviour of the phase. Parameters ---------- positions mask: Mask The default masks passed in by the pipeline dataset: im.Imaging An masked_imaging that has been masked results: autofit.tools.pipeline.ResultsCollection The result from the previous phase Returns ------- lens : Analysis An lens object that the non-linear optimizer calls to determine the fit of a set of values """ raise NotImplementedError()
def make_results_collection(): results = af.ResultsCollection() results.add("first", "one") results.add("second", "two") return results
def run(self, dataset: Dataset, results: af.ResultsCollection = None, **kwargs) -> af.Result: """ Run the hyper phase and then the hyper_galaxies phase. Parameters ---------- dataset Data results Results from previous phases. kwargs Returns ------- result The result of the phase, with a hyper_galaxies result attached as an attribute with the hyper_name of this phase. """ self.save_dataset(dataset=dataset) results = (copy.deepcopy(results) if results is not None else af.ResultsCollection()) result = self.phase.run(dataset, results=results, **kwargs) results.add(self.phase.paths.phase_name, result) hyper_result = self.run_hyper(dataset=dataset, results=results, **kwargs) setattr(result, self.hyper_name, hyper_result) return result
def test__associate_images(self, instance, result, masked_imaging_7x7): results_collection = af.ResultsCollection() results_collection.add("phase", result) results_collection[0].use_as_hyper_dataset = True analysis = al.PhaseImaging.Analysis( masked_imaging=masked_imaging_7x7, cosmology=None, results=results_collection, image_path="", ) instance = analysis.associate_hyper_images(instance=instance) lens_hyper_image = result.image_galaxy_dict[("galaxies", "lens")] source_hyper_image = result.image_galaxy_dict[("galaxies", "source")] hyper_model_image = lens_hyper_image + source_hyper_image assert instance.galaxies.lens.hyper_galaxy_image.in_2d == pytest.approx( lens_hyper_image.in_2d, 1.0e-4 ) assert instance.galaxies.source.hyper_galaxy_image.in_2d == pytest.approx( source_hyper_image.in_2d, 1.0e-4 ) assert instance.galaxies.lens.hyper_model_image.in_2d == pytest.approx( hyper_model_image.in_2d, 1.0e-4 ) assert instance.galaxies.source.hyper_model_image.in_2d == pytest.approx( hyper_model_image.in_2d, 1.0e-4 )
def run_hyper(self, dataset, info=None, results=af.ResultsCollection(), **kwargs): """ Run the phase, overriding the optimizer's model instance with one created to only fit pixelization hyperparameters. """ phase = self.make_hyper_phase() phase.model = self.make_model(results.last.instance) return phase.run( dataset, mask=results.last.mask, results=results, positions=results.last.positions, )
def run(self, dataset, mask, results: af.ResultsCollection = None, positions=None, info=None, **kwargs) -> af.Result: """ Run the phase followed by the hyper_galaxies phases. Each result of a hyper_galaxies phase is attached to the overall result object by the hyper_name of that phase. Finally, a phase in run with all of the model results from all the individual hyper_galaxies phases. Parameters ---------- positions mask data the dataset results Results from previous phases kwargs Returns ------- result The result of the phase, with hyper_galaxies results attached by associated hyper_galaxies names """ results = results.copy( ) if results is not None else af.ResultsCollection() result = self.phase.run(dataset=dataset, mask=mask, results=results, positions=positions, info=info, **kwargs) results.add(self.phase.paths.phase_name, result) for phase in self.hyper_phases: hyper_result = phase.run_hyper(dataset=dataset, results=results, info=info, **kwargs) setattr(result, phase.hyper_name, hyper_result) setattr(result, self.hyper_name, self.run_hyper(dataset=dataset, results=results)) return result
def run( self, dataset: Dataset, mask, results=af.ResultsCollection(), positions=None, info=None, ): """ Run this phase. Parameters ---------- positions mask: Mask The default masks passed in by the pipeline results: autofit.tools.pipeline.ResultsCollection An object describing the results of the last phase or None if no phase has been executed dataset: scaled_array.ScaledSquarePixelArray An masked_imaging that has been masked Returns ------- result: AbstractPhase.Result A result object comprising the best fit model and other hyper_galaxies. """ self.save_metadata(dataset=dataset) self.save_dataset(dataset=dataset) self.save_mask(mask) self.save_meta_dataset(meta_dataset=self.meta_dataset) self.save_info(info=info) self.model = self.model.populate(results) analysis = self.make_analysis(dataset=dataset, mask=mask, results=results, positions=positions) phase_attributes = self.make_phase_attributes(analysis=analysis) self.save_phase_attributes(phase_attributes=phase_attributes) self.customize_priors(results) self.assert_and_save_pickle() result = self.run_analysis(analysis) return self.make_result(result=result, analysis=analysis)
def run( self, positions, positions_noise_map, fluxes=None, fluxes_noise_map=None, imaging=None, results=None, info=None, pickle_files=None, ): """ Run this phase. Parameters ---------- mask: Mask2D The default masks passed in by the pipeline results: autofit.tools.pipeline.ResultsCollection An object describing the results of the last phase or None if no phase has been executed dataset: scaled_array.ScaledSquarePixelArray An masked_imaging that has been masked Returns ------- result: AbstractPhase.Result A result object comprising the best fit model and other hyper_galaxies. """ self.model = self.model.populate(results) results = results or af.ResultsCollection() analysis = self.make_analysis( positions=positions, positions_noise_map=positions_noise_map, fluxes=fluxes, fluxes_noise_map=fluxes_noise_map, imaging=imaging, results=results, ) result = self.run_analysis(analysis=analysis, info=info, pickle_files=pickle_files) return self.make_result(result=result, analysis=analysis)
def make_analysis(self, dataset, mask, results=af.ResultsCollection(), positions=None): """ Create an lens object. Also calls the prior passing and masked_interferometer modifying functions to allow child classes to change the behaviour of the phase. Parameters ---------- positions mask: Mask The default masks passed in by the pipeline dataset: im.Interferometer An masked_interferometer that has been masked results: autofit.tools.pipeline.ResultsCollection The result from the previous phase Returns ------- lens : Analysis An lens object that the non-linear optimizer calls to determine the fit of a set of values """ self.meta_dataset.model = self.model modified_visibilities = self.modify_visibilities( visibilities=dataset.visibilities, results=results) masked_interferometer = self.meta_dataset.masked_dataset_from( dataset=dataset, mask=mask, positions=positions, results=results, modified_visibilities=modified_visibilities, ) self.output_phase_info() analysis = self.Analysis( masked_interferometer=masked_interferometer, cosmology=self.cosmology, image_path=self.optimizer.paths.image_path, results=results, ) return analysis
def test__uses_hyper_fit_correctly(self, instance, result, masked_imaging_7x7): results_collection = af.ResultsCollection() results_collection.add("phase", result) results_collection[0].use_as_hyper_dataset = True analysis = al.PhaseImaging.Analysis( masked_imaging=masked_imaging_7x7, cosmology=cosmo.Planck15, results=results_collection, image_path="", ) hyper_galaxy = al.HyperGalaxy(contribution_factor=1.0, noise_factor=1.0, noise_power=1.0) instance.galaxies.lens.hyper_galaxy = hyper_galaxy fit_likelihood = analysis.fit(instance=instance) lens_hyper_image = result.image_galaxy_dict[("galaxies", "lens")] source_hyper_image = result.image_galaxy_dict[("galaxies", "source")] hyper_model_image = lens_hyper_image + source_hyper_image g0 = al.Galaxy( redshift=0.5, light_profile=instance.galaxies.lens.light, mass_profile=instance.galaxies.lens.mass, hyper_galaxy=hyper_galaxy, hyper_model_image=hyper_model_image, hyper_galaxy_image=lens_hyper_image, hyper_minimum_value=0.0, ) g1 = al.Galaxy(redshift=1.0, light_profile=instance.galaxies.source.light) tracer = al.Tracer.from_galaxies(galaxies=[g0, g1]) fit = ImagingFit(masked_imaging=masked_imaging_7x7, tracer=tracer) assert (fit_likelihood == fit.likelihood).all()
def make_collection(): collection = af.ResultsCollection() model = af.ModelMapper() model.one = af.PriorModel(mock.MockComponents, component=mock.MockClassx2) instance = af.ModelInstance() instance.one = mock.MockComponents(component=mock.MockClassx2()) result = af.MockResult(model=model, instance=instance) model = af.ModelMapper() instance = af.ModelInstance() model.hyper_galaxy = mock.HyperGalaxy instance.hyper_galaxy = mock.HyperGalaxy() hyper_result = af.MockResult(model=model, instance=instance) result.hyper_result = hyper_result collection.add("search name", result) return collection
def run(self, dataset: Dataset, mask, info=None, results=None): """ Pass a dataset to the phase, running the phase and non-linear search. Parameters ---------- dataset: aa.Dataset The dataset fitted by the phase, as defined in the 'dataset.py' module. mask: Mask The mask used for the analysis. Returns ------- result: AbstractPhase.Result A result object comprising information on the non-linear search and the maximum likelihood model. """ # These functions save the objects we will later access using the aggregator. They are saved via the 'pickle' # module in Python, which serializes the data on to the hard-disk. # See the 'dataset.py' module for a description of what the metadata is. self.save_metadata(dataset=dataset) self.save_dataset(dataset=dataset) self.save_mask(mask=mask) self.save_meta_dataset(meta_dataset=self.meta_dataset) self.model = self.model.populate(results) results = results or af.ResultsCollection() # This saves the search information of the phase, meaning that we can use the search instance # (e.g. Emcee) to interpret our results in the aggregator. analysis = self.make_analysis(dataset=dataset, mask=mask, results=results) result = self.run_analysis(analysis=analysis, info=info) return self.make_result(result=result, analysis=analysis)
def no_lens_light( settings_autofit: slam_util.SettingsAutoFit, analysis: Union[al.AnalysisImaging, al.AnalysisInterferometer], setup_hyper: al.SetupHyper, source_parametric_results: af.ResultsCollection, pixelization: af.Model(al.pix.Pixelization) = af.Model( al.pix.VoronoiBrightnessImage), regularization: af.Model(al.reg.Regularization) = af.Model( al.reg.Constant), ) -> af.ResultsCollection: """ The S:aM SOURCE INVERSION PIPELINE for fitting imaging data without a lens light component. Parameters ---------- analysis The analysis class which includes the `log_likelihood_function` and can be customized for the SLaM model-fit. setup_hyper The setup of the hyper analysis if used (e.g. hyper-galaxy noise scaling). source_parametric_results The results of the SLaM SOURCE PARAMETRIC PIPELINE which ran before this pipeline. pixelization The pixelization used by the `Inversion` which fits the source light. regularization The regularization used by the `Inversion` which fits the source light. """ """ __Model + Search + Analysis + Model-Fit (Search 1)__ In search 1 of the SOURCE INVERSION PIPELINE we fit a lens model where: - The lens galaxy mass is modeled using a total mass distribution [parameters fixed to result of SOURCE PARAMETRIC PIPELINE]. - The source galaxy's light is a `VoronoiMagnification` pixelization and `Constant` regularization scheme. This search aims to quickly estimate values for the pixelization resolution and regularization coefficient. """ model = af.Collection( galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=source_parametric_results.last.instance.galaxies.lens. redshift, mass=source_parametric_results.last.instance.galaxies.lens. mass, shear=source_parametric_results.last.instance.galaxies.lens. shear, ), source=af.Model( al.Galaxy, redshift=source_parametric_results.last.instance.galaxies. source.redshift, pixelization=al.pix.VoronoiMagnification, regularization=al.reg.Constant, hyper_galaxy=setup_hyper.hyper_galaxy_source_from_result( result=source_parametric_results.last), ), ), hyper_image_sky=setup_hyper.hyper_image_sky_from_result( result=source_parametric_results.last, as_model=False), hyper_background_noise=setup_hyper.hyper_background_noise_from_result( result=source_parametric_results.last), ) search = af.DynestyStatic( path_prefix=settings_autofit.path_prefix, name= "source_inversion[1]_mass[fixed]_source[inversion_magnification_initialization]", unique_tag=settings_autofit.unique_tag, number_of_cores=settings_autofit.number_of_cores, session=settings_autofit.session, nlive=30, ) result_1 = search.fit(model=model, analysis=analysis.no_positions, info=settings_autofit.info) """ __Model + Search + Analysis + Model-Fit (Search 2)__ In search 2 of the SOURCE INVERSION PIPELINE we fit a lens model where: - The lens galaxy mass is modeled using a total mass distribution [parameters initialized from the results of the SOURCE PARAMETRIC PIPELINE]. - The source galaxy's light is a `VoronoiMagnification` pixelization and `Constant` regularization scheme [parameters are fixed to the result of search 1]. This search aims to improve the lens mass model using the search 1 `Inversion`. """ model = af.Collection( galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=result_1.instance.galaxies.lens.redshift, mass=source_parametric_results.last.model.galaxies.lens.mass, shear=source_parametric_results.last.model.galaxies.lens.shear, ), source=af.Model( al.Galaxy, redshift=result_1.instance.galaxies.source.redshift, pixelization=result_1.instance.galaxies.source.pixelization, regularization=result_1.instance.galaxies.source. regularization, hyper_galaxy=result_1.instance.galaxies.source.hyper_galaxy, ), ), hyper_image_sky=result_1.instance.hyper_image_sky, hyper_background_noise=result_1.instance.hyper_background_noise, ) search = af.DynestyStatic( path_prefix=settings_autofit.path_prefix, name="source_inversion[2]_mass[total]_source[fixed]", unique_tag=settings_autofit.unique_tag, number_of_cores=settings_autofit.number_of_cores, session=settings_autofit.session, nlive=50, ) result_2 = search.fit(model=model, analysis=analysis, info=settings_autofit.info) """ __Model + Search + Analysis + Model-Fit (Search 3)__ In search 3 of the SOURCE INVERSION PIPELINE we fit a lens model where: - The lens galaxy mass is modeled using a total mass distribution [parameters fixed to result of search 2]. - The source galaxy's light is the input pixelization and regularization. This search aims to estimate values for the pixelization and regularization scheme. """ model = af.Collection( galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=result_2.instance.galaxies.lens.redshift, mass=result_2.instance.galaxies.lens.mass, shear=result_2.instance.galaxies.lens.shear, ), source=af.Model( al.Galaxy, redshift=result_2.instance.galaxies.source.redshift, pixelization=pixelization, regularization=regularization, hyper_galaxy=result_2.instance.galaxies.source.hyper_galaxy, ), ), hyper_image_sky=result_2.instance.hyper_image_sky, hyper_background_noise=result_2.instance.hyper_background_noise, ) search = af.DynestyStatic( path_prefix=settings_autofit.path_prefix, name="source_inversion[3]_mass[fixed]_source[inversion_initialization]", unique_tag=settings_autofit.unique_tag, number_of_cores=settings_autofit.number_of_cores, session=settings_autofit.session, nlive=30, dlogz=10.0, sample="rstagger", ) analysis.set_hyper_dataset(result=result_2) result_3 = search.fit(model=model, analysis=analysis.no_positions, info=settings_autofit.info) result_3.use_as_hyper_dataset = True """ __Model + Search + Analysis + Model-Fit (Search 4)__ In search 4 of the SOURCE INVERSION PIPELINE we fit a lens model where: - The lens galaxy mass is modeled using a total mass distribution [parameters initialized from the results of the search 2]. - The source galaxy's light is the input pixelization and regularization scheme [parameters fixed to the result of search 3]. This search aims to improve the lens mass model using the input `Inversion`. """ mass = slam_util.mass__from_result( mass=result_2.model.galaxies.lens.mass, result=source_parametric_results.last, unfix_mass_centre=True, ) model = af.Collection( galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=result_3.instance.galaxies.lens.redshift, mass=mass, shear=result_2.model.galaxies.lens.shear, ), source=af.Model( al.Galaxy, redshift=result_3.instance.galaxies.source.redshift, pixelization=result_3.instance.galaxies.source.pixelization, regularization=result_3.instance.galaxies.source. regularization, hyper_galaxy=result_3.instance.galaxies.source.hyper_galaxy, ), ), hyper_image_sky=result_3.instance.hyper_image_sky, hyper_background_noise=result_3.instance.hyper_background_noise, ) analysis.preloads = al.Preloads.setup(result=result_3, model=model, pixelization=True) search = af.DynestyStatic( path_prefix=settings_autofit.path_prefix, name="source_inversion[4]_mass[total]_source[fixed]", unique_tag=settings_autofit.unique_tag, number_of_cores=settings_autofit.number_of_cores, session=settings_autofit.session, nlive=50, ) result_4 = search.fit(model=model, analysis=analysis, info=settings_autofit.info) """ __Hyper Extension__ The above search is extended with a hyper-search if the SetupHyper has one or more of the following inputs: - The source is using an `Inversion`. - One or more `HyperGalaxy`'s are included. - The background sky is included via `hyper_image_sky` input. - The background noise is included via the `hyper_background_noise`. """ result_4 = extensions.hyper_fit( setup_hyper=setup_hyper, result=result_4, analysis=analysis, include_hyper_image_sky=True, ) return af.ResultsCollection([result_1, result_2, result_3, result_4])
def detection_single_plane( path_prefix: str, analysis: Union[al.AnalysisImaging, al.AnalysisInterferometer], setup_hyper: al.SetupHyper, mass_results: af.ResultsCollection, subhalo_mass: af.Model(al.mp.MassProfile) = af.Model( al.mp.SphNFWMCRLudlow), grid_dimension_arcsec: float = 3.0, number_of_steps: Union[Tuple[int], int] = 5, number_of_cores: int = 1, unique_tag: Optional[str] = None, session: Optional[bool] = None, ) -> af.ResultsCollection: """ The SLaM SUBHALO PIPELINE for fitting imaging data with or without a lens light component, where it is assumed that the subhalo is at the same redshift as the lens galaxy. Parameters ---------- path_prefix The prefix of folders between the output path and the search folders. analysis The analysis class which includes the `log_likelihood_function` and can be customized for the SLaM model-fit. setup_hyper The setup of the hyper analysis if used (e.g. hyper-galaxy noise scaling). mass_results The results of the SLaM MASS PIPELINE which ran before this pipeline. subhalo_mass The `MassProfile` used to fit the subhalo in this pipeline. grid_dimension_arcsec the arc-second dimensions of the grid in the y and x directions. An input value of 3.0" means the grid in all four directions extends to 3.0" giving it dimensions 6.0" x 6.0". number_of_steps The 2D dimensions of the grid (e.g. number_of_steps x number_of_steps) that the subhalo search is performed for. number_of_cores The number of cores used to perform the non-linear search grid search. If 1, each model-fit on the grid is performed in serial, if > 1 fits are distributed in parallel using the Python multiprocessing module. unique_tag The unique tag for this model-fit, which will be given a unique entry in the sqlite database and also acts as the folder after the path prefix and before the search name. This is typically the name of the dataset. """ """ __Model + Search + Analysis + Model-Fit (Search 1)__ In search 1 of the SUBHALO PIPELINE we fit a lens model where: - The lens galaxy mass is modeled using MASS PIPELINE's mass distribution [Priors initialized from MASS PIPELINE]. - The source galaxy's light is parametric or an inversion depending on the previous MASS PIPELINE [Model and priors initialized from MASS PIPELINE]. This search aims to accurately estimate the lens mass model, using the improved mass model priors and source model of the MASS PIPELINE. This model will be used to perform Bayesian model comparison with models that include a subhalo, to determine if a subhalo is detected. """ source = slam_util.source__from_result_model_if_parametric( result=mass_results.last, setup_hyper=setup_hyper) model = af.Collection( galaxies=af.Collection(lens=mass_results.last.model.galaxies.lens, source=source), hyper_image_sky=setup_hyper.hyper_image_sky_from_result( result=mass_results.last, as_model=True), hyper_background_noise=setup_hyper.hyper_background_noise_from_result( result=mass_results.last), ) search = af.DynestyStatic( path_prefix=path_prefix, name="subhalo[1]_mass[total_refine]", unique_tag=unique_tag, session=session, nlive=100, ) result_1 = search.fit(model=model, analysis=analysis) """ __Model + Search + Analysis + Model-Fit (Search 2)__ In search 2 of the SUBHALO PIPELINE we perform a [number_of_steps x number_of_steps] grid search of non-linear searches where: - The lens galaxy mass is modeled using MASS PIPELINE's mass distribution [Priors initialized from MASS PIPELINE]. - The source galaxy's light is parametric or an inversion depending on the previous MASS PIPELINE [Model and priors initialized from MASS PIPELINE]. - The subhalo redshift is fixed to that of the lens galaxy. - Each grid search varies the subhalo (y,x) coordinates and mass as free parameters. - The priors on these (y,x) coordinates are UniformPriors, with limits corresponding to the grid-cells. This search aims to detect a dark matter subhalo. """ subhalo = af.Model(al.Galaxy, redshift=result_1.instance.galaxies.lens.redshift, mass=subhalo_mass) subhalo.mass.mass_at_200 = af.LogUniformPrior(lower_limit=1.0e6, upper_limit=1.0e11) subhalo.mass.centre_0 = af.UniformPrior(lower_limit=-grid_dimension_arcsec, upper_limit=grid_dimension_arcsec) subhalo.mass.centre_1 = af.UniformPrior(lower_limit=-grid_dimension_arcsec, upper_limit=grid_dimension_arcsec) subhalo.mass.redshift_object = result_1.instance.galaxies.lens.redshift subhalo.mass.redshift_source = result_1.instance.galaxies.source.redshift source = slam_util.source__from_result_model_if_parametric( result=mass_results.last, setup_hyper=setup_hyper) model = af.Collection( galaxies=af.Collection(lens=mass_results.last.model.galaxies.lens, subhalo=subhalo, source=source), hyper_image_sky=setup_hyper.hyper_image_sky_from_result( result=mass_results.last, as_model=True), hyper_background_noise=setup_hyper.hyper_background_noise_from_result( result=mass_results.last), ) search = af.DynestyStatic( path_prefix=path_prefix, name="subhalo[2]_mass[total]_source_subhalo[search_lens_plane]", unique_tag=unique_tag, session=session, nlive=50, walks=5, facc=0.2, ) subhalo_grid_search = af.SearchGridSearch(search=search, number_of_steps=number_of_steps, number_of_cores=number_of_cores) grid_search_result = subhalo_grid_search.fit( model=model, analysis=analysis, grid_priors=[ model.galaxies.subhalo.mass.centre_0, model.galaxies.subhalo.mass.centre_1, ], ) """ __Model + Search + Analysis + Model-Fit (Search 3)__ In search 3 of the SUBHALO PIPELINE we refit the lens and source models above but now including a subhalo, where the subhalo model is initalized from the highest evidence model of the subhalo grid search. - The lens galaxy mass is modeled using MASS PIPELINE's mass distribution [Priors initialized from MASS PIPELINE]. - The source galaxy's light is parametric or an inversion depending on the previous MASS PIPELINE [Model and priors initialized from MASS PIPELINE]. - The subhalo redshift is fixed to that of the lens galaxy. - Each grid search varies the subhalo (y,x) coordinates and mass as free parameters. - The priors on these (y,x) coordinates are UniformPriors, with limits corresponding to the grid-cells. This search aims to refine the parameter estimates and errors of a dark matter subhalo detected in the grid search above. """ subhalo = af.Model(al.Galaxy, redshift=result_1.instance.galaxies.lens.redshift, mass=subhalo_mass) subhalo.mass.mass_at_200 = ( grid_search_result.model.galaxies.subhalo.mass.mass_at_200) subhalo.mass.centre = grid_search_result.model.galaxies.subhalo.mass.centre subhalo.mass.redshift_object = grid_search_result.instance.galaxies.lens.redshift subhalo.mass.redshift_source = grid_search_result.instance.galaxies.source.redshift model = af.Collection( galaxies=af.Collection( lens=grid_search_result.model.galaxies.lens, subhalo=subhalo, source=grid_search_result.model.galaxies.source, ), hyper_image_sky=grid_search_result.instance.hyper_image_sky, hyper_background_noise=grid_search_result.instance. hyper_background_noise, ) search = af.DynestyStatic( name="subhalo[3]_subhalo[single_plane_refine]", unique_tag=unique_tag, session=session, path_prefix=path_prefix, nlive=100, ) result_3 = search.fit(model=model, analysis=analysis) return af.ResultsCollection([result_1, grid_search_result, result_3])
def no_lens_light( settings_autofit: af.SettingsSearch, analysis: Union[al.AnalysisImaging, al.AnalysisInterferometer], setup_hyper: al.SetupHyper, source_results: af.ResultsCollection, mass: af.Model = af.Model(al.mp.EllIsothermal), smbh: Optional[af.Model] = None, mass_centre: Optional[Tuple[float, float]] = None, end_with_hyper_extension: bool = False, ) -> af.ResultsCollection: """ The SLaM MASS TOTAL PIPELINE for fitting imaging data without a lens light component. Parameters ---------- analysis The analysis class which includes the `log_likelihood_function` and can be customized for the SLaM model-fit. setup_hyper The setup of the hyper analysis if used (e.g. hyper-galaxy noise scaling). source_results The results of the SLaM SOURCE PARAMETRIC PIPELINE or SOURCE INVERSION PIPELINE which ran before this pipeline. mass The `MassProfile` used to fit the lens galaxy mass in this pipeline. smbh The `MassProfile` used to fit the a super massive black hole in the lens galaxy. mass_centre If input, a fixed (y,x) centre of the mass profile is used which is not treated as a free parameter by the non-linear search. end_with_hyper_extension If `True` a hyper extension is performed at the end of the pipeline. If this feature is used, you must be certain you have manually passed the new hyper images geneted in this search to the next pipelines. """ """ __Model + Search + Analysis + Model-Fit (Search 1)__ In search 1 of the MASS TOTAL PIPELINE we fit a lens model where: - The lens galaxy mass is modeled using a total mass distribution [Priors initialized from SOURCE PIPELINE]. - The source galaxy's light is parametric or an inversion depending on the previous pipeline [Model and priors initialized from SOURCE PIPELINE]. This search aims to accurately estimate the lens mass model, using the improved mass model priors and source model of the SOURCE PIPELINE """ mass = slam_util.mass__from(mass=mass, result=source_results.last, unfix_mass_centre=True) if mass_centre is not None: mass.centre = mass_centre if smbh is not None: smbh.centre = mass.centre source = slam_util.source__from_result_model_if_parametric( result=source_results.last, setup_hyper=setup_hyper) model = af.Collection( galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=source_results.last.instance.galaxies.lens.redshift, mass=mass, smbh=smbh, shear=source_results.last.model.galaxies.lens.shear, ), source=source, ), hyper_image_sky=setup_hyper.hyper_image_sky_from( result=source_results.last, as_model=True), hyper_background_noise=setup_hyper.hyper_background_noise_from( result=source_results.last), ) search = af.DynestyStatic( name="mass_total[1]_mass[total]_source", **settings_autofit.search_dict, nlive=100, ) result_1 = search.fit(model=model, analysis=analysis, **settings_autofit.fit_dict) """ __Hyper Extension__ The above search may be extended with a hyper-search, if the SetupHyper has one or more of the following inputs: - The source is using an `Inversion`. - One or more `HyperGalaxy`'s are included. - The background sky is included via `hyper_image_sky` input. - The background noise is included via the `hyper_background_noise`. """ if end_with_hyper_extension: result_1 = extensions.hyper_fit( setup_hyper=setup_hyper, result=result_1, analysis=analysis, include_hyper_image_sky=True, ) return af.ResultsCollection([result_1])
def with_lens_light( settings_autofit: slam_util.SettingsAutoFit, analysis: Union[al.AnalysisImaging, al.AnalysisInterferometer], setup_hyper: al.SetupHyper, source_results: af.ResultsCollection, light_results: af.ResultsCollection, lens_bulge: af.Model(al.lp.LightProfile) = af.Model(al.lp.EllSersic), lens_disk: af.Model(al.lp.LightProfile) = None, lens_envelope: af.Model(al.lp.LightProfile) = None, dark: af.Model(al.mp.MassProfile) = af.Model(al.mp.EllNFWMCRLudlow), smbh: af.Model(al.mp.MassProfile) = None, einstein_mass_range: Optional[Tuple[float, float]] = (0.01, 5.0), end_with_hyper_extension: bool = False, ) -> af.ResultsCollection: """ The SLaM MASS LIGHT DARK PIPELINE for fitting imaging data with a lens light component. Parameters ---------- analysis The analysis class which includes the `log_likelihood_function` and can be customized for the SLaM model-fit. setup_hyper The setup of the hyper analysis if used (e.g. hyper-galaxy noise scaling). source_results The results of the SLaM SOURCE PARAMETRIC PIPELINE or SOURCE INVERSION PIPELINE which ran before this pipeline. light_results The results of the SLaM LIGHT PARAMETRIC PIPELINE which ran before this pipeline. mass The `MassProfile` used to fit the lens galaxy mass in this pipeline. smbh The `MassProfile` used to fit the a super massive black hole in the lens galaxy. lens_bulge The `LightMassProfile` `Model` used to represent the light and stellar mass distribution of the lens galaxy's bulge (set to None to omit a bulge). lens_disk The `LightMassProfile` `Model` used to represent the light and stellar mass distribution of the lens galaxy's disk (set to None to omit a disk). lens_envelope The `LightMassProfile` `Model` used to represent the light and stellar mass distribution of the lens galaxy's envelope (set to None to omit an envelope). dark The `MassProfile` `Model` used to represent the dark matter distribution of the lens galaxy's (set to None to omit dark matter). einstein_mass_range The values a the estimate of the Einstein Mass in the LIGHT PIPELINE is multiplied by to set the lower and upper limits of the profile's mass-to-light ratio. end_with_hyper_extension If `True` a hyper extension is performed at the end of the pipeline. If this feature is used, you must be certain you have manually passed the new hyper images geneted in this search to the next pipelines. """ """ __Model + Search + Analysis + Model-Fit (Search 1)__ In search 1 of the MASS LIGHT DARK PIPELINE we fit a lens model where: - The lens galaxy light and stellar mass is modeled using light and mass profiles [Priors on light model parameters initialized from LIGHT PIPELINE]. - The lens galaxy dark mass is modeled using a dark mass distribution [No prior initialization]. - The source galaxy's light is parametric or an inversion depending on the previous pipeline [Model and priors initialized from SOURCE PIPELINE]. This search aims to accurately estimate the lens mass model, using the improved mass model priors and source model of the SOURCE PIPELINE and LIGHT PIPELINE. The `mass_to_light_ratio` prior of each light and stellar profile is set using the Einstein Mass estimate of the SOURCE PIPELINE, specifically using values which are 1% and 500% this estimate. The dark matter mass profile has the lens and source redshifts added to it, which are used to determine its mass from the mass-to-concentration relation of Ludlow et al. """ lens_bulge = slam_util.pass_light_and_mass_profile_priors( model=lens_bulge, result_light_component=light_results.last.model.galaxies.lens.bulge, result=light_results.last, einstein_mass_range=einstein_mass_range, ) lens_disk = slam_util.pass_light_and_mass_profile_priors( model=lens_disk, result_light_component=light_results.last.model.galaxies.lens.disk, result=light_results.last, einstein_mass_range=einstein_mass_range, ) lens_envelope = slam_util.pass_light_and_mass_profile_priors( model=lens_envelope, result_light_component=light_results.last.model.galaxies.lens.envelope, result=light_results.last, einstein_mass_range=einstein_mass_range, ) dark.mass_at_200 = af.LogUniformPrior(lower_limit=1e10, upper_limit=1e15) dark.redshift_object = light_results.last.instance.galaxies.lens.redshift dark.redshift_source = light_results.last.instance.galaxies.source.redshift if smbh is not None: smbh.centre = lens_bulge.centre source = slam_util.source__from_result_model_if_parametric( result=source_results.last, setup_hyper=setup_hyper ) model = af.Collection( galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=light_results.last.instance.galaxies.lens.redshift, bulge=lens_bulge, disk=lens_disk, envelope=lens_envelope, dark=dark, shear=source_results.last.model.galaxies.lens.shear, smbh=smbh, hyper_galaxy=setup_hyper.hyper_galaxy_lens_from_result( result=light_results.last ), ), source=source, ), hyper_image_sky=setup_hyper.hyper_image_sky_from_result( result=light_results.last, as_model=True ), hyper_background_noise=setup_hyper.hyper_background_noise_from_result( result=light_results.last ), ) search = af.DynestyStatic( path_prefix=settings_autofit.path_prefix, name="mass_light_dark[1]_light[parametric]_mass[light_dark]_source", unique_tag=settings_autofit.unique_tag, number_of_cores=settings_autofit.number_of_cores, session=settings_autofit.session, nlive=100, ) result_1 = search.fit(model=model, analysis=analysis, info=settings_autofit.info) """ __Hyper Extension__ The above search may be extended with a hyper-search, if the SetupHyper has one or more of the following inputs: - The source is using an `Inversion`. - One or more `HyperGalaxy`'s are included. - The background sky is included via `hyper_image_sky` input. - The background noise is included via the `hyper_background_noise`. """ if end_with_hyper_extension: result_1 = extensions.hyper_fit( setup_hyper=setup_hyper, result=result_1, analysis=analysis, include_hyper_image_sky=True, ) return af.ResultsCollection([result_1])
def with_lens_light( path_prefix: str, analysis: Union[al.AnalysisImaging, al.AnalysisInterferometer], setup_hyper: al.SetupHyper, source_results: af.ResultsCollection, lens_bulge: af.Model(al.lp.LightProfile) = af.Model(al.lp.EllSersic), lens_disk: af.Model(al.lp.LightProfile) = None, lens_envelope: af.Model(al.lp.LightProfile) = None, end_with_hyper_extension: bool = False, unique_tag: Optional[str] = None, session: Optional[bool] = None, ) -> af.ResultsCollection: """ The SlaM LIGHT PARAMETRIC PIPELINE for fitting imaging data with a lens light component. Parameters ---------- path_prefix The prefix of folders between the output path and the search folders. analysis The analysis class which includes the `log_likelihood_function` and can be customized for the SLaM model-fit. setup_hyper The setup of the hyper analysis if used (e.g. hyper-galaxy noise scaling). source_results The results of the SLaM SOURCE PARAMETRIC PIPELINE or SOURCE INVERSION PIPELINE which ran before this pipeline. lens_bulge The `LightProfile` `Model` used to represent the light distribution of the lens galaxy's bulge (set to None to omit a bulge). lens_disk The `LightProfile` `Model` used to represent the light distribution of the lens galaxy's disk (set to None to omit a disk). lens_envelope The `LightProfile` `Model` used to represent the light distribution of the lens galaxy's envelope (set to None to omit an envelope). end_with_hyper_extension If `True` a hyper extension is performed at the end of the pipeline. If this feature is used, you must be certain you have manually passed the new hyper images geneted in this search to the next pipelines. unique_tag The unique tag for this model-fit, which will be given a unique entry in the sqlite database and also acts as the folder after the path prefix and before the search name. This is typically the name of the dataset. """ """ __Model + Search + Analysis + Model-Fit (Search 1)__ In search 1 of the LIGHT PARAMETRIC PIPELINE we fit a lens model where: - The lens galaxy light is modeled using parametric bulge + disk + envelope [no prior initialization]. - The lens galaxy mass is modeled using SOURCE PIPELINE's mass distribution [Parameters fixed from MASS PIPELINE]. - The source galaxy's light is modeled using SOURCE PIPELINE's model [Parameters fixed from SOURCE PIPELINE]. This search aims to produce an accurate model of the lens galaxy's light, which may not have been possible in the SOURCE PIPELINE as the mass and source models were not properly initialized. """ """ If hyper-galaxy noise scaling for the lens is on, it may have scaled the noise to high values in the SOURCE PIPELINE (which fitted a simpler lens light model than this pipeline). The new lens light model fitted in this pipeline may fit the data better, requiring a reducing level of noise scaling. For this reason, the noise scaling normalization is included as a free parameter. """ hyper_galaxy = setup_hyper.hyper_galaxy_lens_from_result( result=source_results.last, noise_factor_is_model=True) source = slam_util.source__from_result(result=source_results.last, setup_hyper=setup_hyper, source_is_model=False) model = af.Collection( galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=source_results.last.instance.galaxies.lens.redshift, bulge=lens_bulge, disk=lens_disk, envelope=lens_envelope, mass=source_results.last.instance.galaxies.lens.mass, shear=source_results.last.instance.galaxies.lens.shear, hyper_galaxy=hyper_galaxy, ), source=source, ), hyper_image_sky=setup_hyper.hyper_image_sky_from_result( result=source_results.last, as_model=True), hyper_background_noise=setup_hyper.hyper_background_noise_from_result( result=source_results.last), ) search = af.DynestyStatic( path_prefix=path_prefix, name="light[1]_light[parametric]", unique_tag=unique_tag, session=session, nlive=75, ) result_1 = search.fit(model=model, analysis=analysis.no_positions) """ __Hyper Extension__ The above search is extended with a hyper-search if the SetupHyper has one or more of the following inputs: - The source is using an `Inversion`. - One or more `HyperGalaxy`'s are included. - The background sky is included via `hyper_image_sky` input. - The background noise is included via the `hyper_background_noise`. """ if end_with_hyper_extension: result_1 = extensions.hyper_fit( setup_hyper=setup_hyper, result=result_1, analysis=analysis, include_hyper_image_sky=True, ) return af.ResultsCollection([result_1])
def make_results_collection(results_7x7): results_collection = af.ResultsCollection() results_collection.add("phase", results_7x7) return results_collection
def with_lens_light( path_prefix: str, analysis: Union[al.AnalysisImaging, al.AnalysisInterferometer], setup_hyper: al.SetupHyper, lens_bulge: af.Model(al.lp.LightProfile) = af.Model(al.lp.EllSersic), lens_disk: af.Model(al.lp.LightProfile) = af.Model(al.lp.EllExponential), lens_envelope: af.Model(al.lp.LightProfile) = None, mass: af.Model(al.mp.MassProfile) = af.Model(al.mp.EllIsothermal), shear: af.Model(al.mp.ExternalShear) = af.Model(al.mp.ExternalShear), source_bulge: af.Model(al.lp.LightProfile) = af.Model(al.lp.EllSersic), source_disk: af.Model(al.lp.LightProfile) = None, source_envelope: af.Model(al.lp.LightProfile) = None, redshift_lens: float = 0.5, redshift_source: float = 1.0, mass_centre: Optional[Tuple[float, float]] = None, unique_tag: Optional[str] = None, session: Optional[bool] = None, ) -> af.ResultsCollection: """ The SlaM SOURCE PARAMETRIC PIPELINE for fitting imaging data with a lens light component. Parameters ---------- path_prefix The prefix of folders between the output path and the search folders. analysis The analysis class which includes the `log_likelihood_function` and can be customized for the SLaM model-fit. setup_hyper The setup of the hyper analysis if used (e.g. hyper-galaxy noise scaling). lens_bulge The `LightProfile` `Model` used to represent the light distribution of the lens galaxy's bulge (set to None to omit a bulge). lens_disk The `LightProfile` `Model` used to represent the light distribution of the lens galaxy's disk (set to None to omit a disk). lens_envelope The `LightProfile` `Model` used to represent the light distribution of the lens galaxy's envelope (set to None to omit an envelope). mass The `MassProfile` fitted by this pipeline. shear The model used to represent the external shear in the mass model (set to None to turn off shear). bulge_prior_model : af.Model(lp.LightProfile) source_bulge The `LightProfile` `Model` used to represent the light distribution of the source galaxy's bulge (set to None to omit a bulge). source_disk The `LightProfile` `Model` used to represent the light distribution of the source galaxy's disk (set to None to omit a disk). source_envelope The `LightProfile` `Model` used to represent the light distribution of the source galaxy's envelope (set to None to omit an envelope). redshift_lens The redshift of the lens galaxy fitted, used by the pipeline for converting arc-seconds to kpc, masses to solMass, etc. redshift_source The redshift of the source galaxy fitted, used by the pipeline for converting arc-seconds to kpc, masses to solMass, etc. mass_centre : (float, float) If input, a fixed (y,x) centre of the mass profile is used which is not treated as a free parameter by the non-linear search. unique_tag The unique tag for this model-fit, which will be given a unique entry in the sqlite database and also acts as the folder after the path prefix and before the search name. This is typically the name of the dataset. """ """ __Model + Search + Analysis + Model-Fit (Search 1)__ In search 1 of the SOURCE PARAMETRIC PIPELINE we fit a lens model where: - The lens galaxy light is modeled using parametric bulge + disk + envelope [no prior initialization]. - The lens's mass and source galaxy are omitted from the fit. This search aims to produce a somewhat accurate lens light subtracted image for the next search which fits the the lens mass model and source model. """ lens = af.Model( al.Galaxy, redshift=redshift_lens, bulge=lens_bulge, disk=lens_disk, envelope=lens_envelope, ) model = af.Collection(galaxies=af.Collection(lens=lens)) search = af.DynestyStatic( path_prefix=path_prefix, name="source_parametric[1]_light[parametric]", unique_tag=unique_tag, session=session, nlive=75, ) result_1 = search.fit(model=model, analysis=analysis) """ __Model + Search + Analysis + Model-Fit (Search 2)__ In search 2 of the SOURCE PARAMETRIC PIPELINE we fit a lens model where: - The lens galaxy light is modeled using a parametric bulge + disk + envelope [fixed to result of Search 1]. - The lens galaxy mass is modeled using a total mass distribution [no prior initialization]. - The source galaxy's light is a parametric bulge + disk + envelope [no prior initialization]. This search aims to accurately estimate the lens mass model and source model. """ if mass_centre is not None: mass.centre = mass_centre model = af.Collection(galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=redshift_lens, bulge=result_1.instance.galaxies.lens.bulge, disk=result_1.instance.galaxies.lens.disk, envelope=result_1.instance.galaxies.lens.envelope, mass=mass, shear=shear, ), source=af.Model( al.Galaxy, redshift=redshift_source, bulge=source_bulge, disk=source_disk, envelope=source_envelope, ), )) search = af.DynestyStatic( path_prefix=path_prefix, name="source_parametric[2]_light[fixed]_mass[total]_source[parametric]", unique_tag=unique_tag, session=session, nlive=200, walks=10, ) result_2 = search.fit(model=model, analysis=analysis) """ __Model + Search + Analysis + Model-Fit (Search 3)__ In search 2 of the SOURCE PARAMETRIC PIPELINE we fit a lens model where: - The lens galaxy light is modeled using a parametric bulge + disk + envelope [priors are not initialized from previous searches]. - The lens galaxy mass is modeled using a total mass distribution [priors initialized from search 2]. - The source galaxy's light is a parametric bulge + disk + envelope [priors initialized from search 2]. This search aims to accurately estimate the lens light model, mass model and source model. """ model = af.Collection(galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=redshift_lens, bulge=lens_bulge, disk=lens_disk, envelope=lens_envelope, mass=result_2.model.galaxies.lens.mass, shear=result_2.model.galaxies.lens.shear, ), source=af.Model( al.Galaxy, redshift=redshift_source, bulge=result_2.model.galaxies.source.bulge, disk=result_2.model.galaxies.source.disk, envelope=result_2.model.galaxies.source.envelope, ), )) search = af.DynestyStatic( path_prefix=path_prefix, name= "source_parametric[3]_light[parametric]_mass[total]_source[parametric]", unique_tag=unique_tag, session=session, nlive=100, ) result_3 = search.fit(model=model, analysis=analysis) result_3.use_as_hyper_dataset = True """ __Hyper Extension__ The above search is extended with a hyper-search if the SetupHyper has one or more of the following inputs: - The background sky is included via `hyper_image_sky`. - The background noise is included via the `hyper_background_noise`. - The source galaxy includes a `HyperGalaxy` for scaling the noise. """ result_3 = extensions.hyper_fit( setup_hyper=setup_hyper, result=result_3, analysis=analysis, include_hyper_image_sky=True, ) return af.ResultsCollection([result_1, result_2, result_3])
def no_lens_light( settings_autofit: slam_util.SettingsAutoFit, analysis: Union[al.AnalysisImaging, al.AnalysisInterferometer], setup_hyper: al.SetupHyper, mass: af.Model(al.mp.MassProfile) = af.Model(al.mp.EllIsothermal), shear: af.Model(al.mp.ExternalShear) = af.Model(al.mp.ExternalShear), source_bulge: af.Model(al.lp.LightProfile) = af.Model(al.lp.EllSersic), source_disk: af.Model(al.lp.LightProfile) = None, source_envelope: af.Model(al.lp.LightProfile) = None, redshift_lens: float = 0.5, redshift_source: float = 1.0, mass_centre: Optional[Tuple[float, float]] = None, ) -> af.ResultsCollection: """ The SlaM SOURCE PARAMETRIC PIPELINE for fitting imaging data without a lens light component. Parameters ---------- analysis The analysis class which includes the `log_likelihood_function` and can be customized for the SLaM model-fit. setup_hyper The setup of the hyper analysis if used (e.g. hyper-galaxy noise scaling). mass The `MassProfile` fitted by this pipeline. shear The model used to represent the external shear in the mass model (set to None to turn off shear). source_bulge The `LightProfile` `Model` used to represent the light distribution of the source galaxy's bulge (set to None to omit a bulge). source_disk The `LightProfile` `Model` used to represent the light distribution of the source galaxy's disk (set to None to omit a disk). source_envelope The `LightProfile` `Model` used to represent the light distribution of the source galaxy's envelope (set to None to omit an envelope). redshift_lens The redshift of the lens galaxy fitted, used by the pipeline for converting arc-seconds to kpc, masses to solMass, etc. redshift_source The redshift of the source galaxy fitted, used by the pipeline for converting arc-seconds to kpc, masses to solMass, etc. mass_centre If input, a fixed (y,x) centre of the mass profile is used which is not treated as a free parameter by the non-linear search. """ """ __Model + Search + Analysis + Model-Fit (Search 1)__ In search 1 of the SOURCE PARAMETRIC PIPELINE we fit a lens model where: - The lens galaxy mass is modeled using a total mass distribution [no prior initialization]. - The source galaxy's light is a parametric bulge + disk + envelope [no prior initialization]. This search aims to accurately estimate the lens mass model and source model. """ if mass_centre is not None: mass.centre = mass_centre model = af.Collection(galaxies=af.Collection( lens=af.Model( al.Galaxy, redshift=redshift_lens, mass=mass, shear=shear), source=af.Model( al.Galaxy, redshift=redshift_source, bulge=source_bulge, disk=source_disk, envelope=source_envelope, ), )) search = af.DynestyStatic( path_prefix=settings_autofit.path_prefix, name="source_parametric[1]_mass[total]_source[parametric]", unique_tag=settings_autofit.unique_tag, number_of_cores=settings_autofit.number_of_cores, session=settings_autofit.session, nlive=200, walks=10, ) result_1 = search.fit(model=model, analysis=analysis, info=settings_autofit.info) """ __Hyper Extension__ The above search is extended with a hyper-search if the SetupHyper has one or more of the following inputs: - The background sky is included via `hyper_image_sky`. - The background noise is included via the `hyper_background_noise`. - The source galaxy includes a `HyperGalaxy` for scaling the noise. """ result_1 = extensions.hyper_fit( setup_hyper=setup_hyper, result=result_1, analysis=analysis, include_hyper_image_sky=True, ) return af.ResultsCollection([result_1])
def run(self, dataset, mask, results=af.ResultsCollection(), positions=None): raise NotImplementedError()