def inversion_interferometer_from( self, dataset: aa.Interferometer, visibilities: aa.Visibilities, noise_map: aa.VisibilitiesNoiseMap, w_tilde, settings_pixelization: aa.SettingsPixelization = aa. SettingsPixelization(), settings_inversion: aa.SettingsInversion = aa.SettingsInversion(), preloads: Preloads = Preloads(), ): linear_obj_galaxy_dict = self.linear_obj_galaxy_dict_from( dataset=dataset, settings_pixelization=settings_pixelization, preloads=preloads, ) linear_obj_list = list(linear_obj_galaxy_dict.keys()) inversion = inversion_interferometer_unpacked_from( visibilities=visibilities, noise_map=noise_map, transformer=dataset.transformer, w_tilde=w_tilde, linear_obj_list=linear_obj_list, regularization_list=self.tracer.regularization_list, settings=settings_inversion, profiling_dict=self.tracer.profiling_dict, ) inversion.linear_obj_galaxy_dict = linear_obj_galaxy_dict return inversion
def inversion_imaging_from( self, dataset: aa.Imaging, image: aa.Array2D, noise_map: aa.Array2D, w_tilde: aa.WTildeImaging, settings_pixelization: aa.SettingsPixelization = aa. SettingsPixelization(), settings_inversion: aa.SettingsInversion = aa.SettingsInversion(), preloads: Preloads = Preloads(), ): linear_obj_galaxy_dict = self.linear_obj_galaxy_dict_from( dataset=dataset, settings_pixelization=settings_pixelization, preloads=preloads, ) linear_obj_list = list(linear_obj_galaxy_dict.keys()) inversion = inversion_imaging_unpacked_from( image=image, noise_map=noise_map, convolver=dataset.convolver, w_tilde=w_tilde, linear_obj_list=linear_obj_list, regularization_list=self.tracer.regularization_list, settings=settings_inversion, preloads=preloads, profiling_dict=self.tracer.profiling_dict, ) inversion.linear_obj_galaxy_dict = linear_obj_galaxy_dict return inversion
def linear_obj_galaxy_dict_from( self, dataset: Union[aa.Imaging, aa.Interferometer], settings_pixelization: aa.SettingsPixelization = aa. SettingsPixelization(), preloads: Preloads = Preloads(), ) -> Dict[Union[ag.LightProfileLinearObjFunc, aa.AbstractMapper], ag.Galaxy]: lp_linear_func_galaxy_dict = self.lp_linear_func_galaxy_dict_from( grid=dataset.grid, blurring_grid=dataset.blurring_grid, convolver=dataset.convolver, ) if preloads.mapper_galaxy_dict is None: mapper_galaxy_dict = self.mapper_galaxy_dict_from( grid=dataset.grid_pixelized, settings_pixelization=settings_pixelization, preloads=preloads, ) else: mapper_galaxy_dict = preloads.mapper_galaxy_dict return {**lp_linear_func_galaxy_dict, **mapper_galaxy_dict}
def _fit_interferometer_from( fit: af.Fit, galaxies: List[ag.Galaxy], real_space_mask: Optional[aa.Mask2D] = None, settings_interferometer: aa.SettingsInterferometer = None, settings_pixelization: aa.SettingsPixelization = None, settings_inversion: aa.SettingsInversion = None, use_preloaded_grid: bool = True, ) -> FitInterferometer: """ Returns a `FitInterferometer` object from a PyAutoFit database `Fit` object and an instance of galaxies from a non-linear search model-fit. This function adds the `hyper_model_image` and `hyper_galaxy_image_path_dict` to the galaxies before performing the fit, if they were used. Parameters ---------- fit A PyAutoFit database Fit object containing the generators of the results of PyAutoGalaxy model-fits. galaxies A list of galaxies corresponding to a sample of a non-linear search and model-fit. Returns ------- FitInterferometer The fit to the interferometer dataset computed via an instance of galaxies. """ interferometer = _interferometer_from( fit=fit, real_space_mask=real_space_mask, settings_interferometer=settings_interferometer, ) tracer = _tracer_from(fit=fit, galaxies=galaxies) settings_pixelization = settings_pixelization or fit.value( name="settings_pixelization") settings_inversion = settings_inversion or fit.value( name="settings_inversion") preloads = None if use_preloaded_grid: sparse_grids_of_planes = fit.value( name="preload_sparse_grids_of_planes") if sparse_grids_of_planes is not None: preloads = Preloads( sparse_image_plane_grid_pg_list=sparse_grids_of_planes) return FitInterferometer( dataset=interferometer, tracer=tracer, settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, )
def max_log_likelihood_fit(self) -> FitImaging: """ An instance of a `FitImaging` corresponding to the maximum log likelihood model inferred by the non-linear search. """ return self.analysis.fit_imaging_via_instance_from( instance=self.instance, preload_overwrite=Preloads(use_w_tilde=False))
def mapper_galaxy_dict_from( self, grid: aa.type.Grid2DLike, settings_pixelization=aa.SettingsPixelization(), preloads=Preloads(), ) -> Dict[aa.AbstractMapper, ag.Galaxy]: mapper_galaxy_dict = {} if preloads.traced_grids_of_planes_for_inversion is None: traced_grids_of_planes_list = self.traced_grid_2d_list_of_inversion_from( grid=grid) else: traced_grids_of_planes_list = preloads.traced_grids_of_planes_for_inversion if preloads.traced_sparse_grids_list_of_planes is None: traced_sparse_grids_list_of_planes, sparse_image_plane_grid_list = self.traced_sparse_grid_pg_list_from( grid=grid, settings_pixelization=settings_pixelization, preloads=preloads, ) else: traced_sparse_grids_list_of_planes = ( preloads.traced_sparse_grids_list_of_planes) sparse_image_plane_grid_list = preloads.sparse_image_plane_grid_list for (plane_index, plane) in enumerate(self.planes): if plane.has_pixelization: galaxies_with_pixelization_list = plane.galaxies_with_pixelization for mapper_index in range( len(traced_sparse_grids_list_of_planes[plane_index])): mapper = plane.to_inversion.mapper_from( source_grid_slim=traced_grids_of_planes_list[ plane_index], source_pixelization_grid= traced_sparse_grids_list_of_planes[plane_index] [mapper_index], data_pixelization_grid=sparse_image_plane_grid_list[ plane_index][mapper_index], pixelization=self.pixelization_pg_list[plane_index] [mapper_index], hyper_galaxy_image=self. hyper_galaxy_image_pg_list[plane_index][mapper_index], settings_pixelization=settings_pixelization, preloads=preloads, ) galaxy = galaxies_with_pixelization_list[mapper_index] mapper_galaxy_dict[mapper] = galaxy return mapper_galaxy_dict
def traced_sparse_grid_pg_list_from( self, grid: aa.type.Grid2DLike, settings_pixelization=aa.SettingsPixelization(), preloads=Preloads(), ) -> Tuple[List[List], List[List]]: """ Ray-trace the sparse image plane grid used to define the source-pixel centres by calculating the deflection angles at (y,x) coordinate on the grid from the galaxy mass profiles and then ray-trace them from the image-plane to the source plane. """ if (preloads.sparse_image_plane_grid_pg_list is None or settings_pixelization.is_stochastic): sparse_image_plane_grid_pg_list = self.sparse_image_plane_grid_pg_list_from( grid=grid, settings_pixelization=settings_pixelization) else: sparse_image_plane_grid_pg_list = preloads.sparse_image_plane_grid_pg_list traced_sparse_grid_pg_list = [] for (plane_index, plane) in enumerate(self.planes): if sparse_image_plane_grid_pg_list[plane_index] is None: traced_sparse_grid_pg_list.append(None) else: traced_sparse_grids_list = [] for sparse_image_plane_grid in sparse_image_plane_grid_pg_list[ plane_index]: try: traced_sparse_grids_list.append( self.tracer.traced_grid_2d_list_from( grid=sparse_image_plane_grid)[plane_index]) except AttributeError: traced_sparse_grids_list.append(None) traced_sparse_grid_pg_list.append(traced_sparse_grids_list) return traced_sparse_grid_pg_list, sparse_image_plane_grid_pg_list
def lp_linear_func_galaxy_dict_from( self, grid: aa.type.Grid2DLike, blurring_grid: aa.type.Grid1D2DLike, convolver: Optional[aa.Convolver] = None, preloads=Preloads(), ) -> Dict[ag.LightProfileLinearObjFunc, ag.Galaxy]: if not self.tracer.has_light_profile_linear: return {} lp_linear_galaxy_dict_list = {} traced_grids_of_planes_list = self.tracer.traced_grid_2d_list_from( grid=grid) if blurring_grid is not None: traced_blurring_grids_of_planes_list = self.tracer.traced_grid_2d_list_from( grid=blurring_grid) else: traced_blurring_grids_of_planes_list = [ None ] * len(traced_grids_of_planes_list) for (plane_index, plane) in enumerate(self.planes): lp_linear_galaxy_dict_of_plane = plane.to_inversion.lp_linear_func_galaxy_dict_from( source_grid_slim=traced_grids_of_planes_list[plane_index], source_blurring_grid_slim=traced_blurring_grids_of_planes_list[ plane_index], convolver=convolver, ) lp_linear_galaxy_dict_list = { **lp_linear_galaxy_dict_list, **lp_linear_galaxy_dict_of_plane, } return lp_linear_galaxy_dict_list
def __init__( self, dataset: aa.Imaging, tracer: Tracer, hyper_image_sky: Optional[ag.hyper_data.HyperImageSky] = None, hyper_background_noise: Optional[ ag.hyper_data.HyperBackgroundNoise] = None, use_hyper_scaling: bool = True, settings_pixelization: aa.SettingsPixelization = aa. SettingsPixelization(), settings_inversion: aa.SettingsInversion = aa.SettingsInversion(), preloads: Preloads = Preloads(), profiling_dict: Optional[Dict] = None, ): """ An lens fitter, which contains the tracer's used to perform the fit and functions to manipulate \ the lens dataset's hyper_galaxies. Parameters ----------- tracer : Tracer The tracer, which describes the ray-tracing and strong lens configuration. """ super().__init__(dataset=dataset, profiling_dict=profiling_dict) super(AbstractFit).__init__() self.tracer = tracer self.hyper_image_sky = hyper_image_sky self.hyper_background_noise = hyper_background_noise self.use_hyper_scaling = use_hyper_scaling self.settings_pixelization = settings_pixelization self.settings_inversion = settings_inversion self.preloads = preloads
def _fit_imaging_from( fit: af.Fit, galaxies: List[ag.Galaxy], settings_imaging: aa.SettingsImaging = None, settings_pixelization: aa.SettingsPixelization = None, settings_inversion: aa.SettingsInversion = None, use_preloaded_grid: bool = True, use_hyper_scaling: bool = True, ) -> FitImaging: """ Returns a `FitImaging` object from a PyAutoFit database `Fit` object and an instance of galaxies from a non-linear search model-fit. This function adds the `hyper_model_image` and `hyper_galaxy_image_path_dict` to the galaxies before performing the fit, if they were used. Parameters ---------- fit A PyAutoFit database Fit object containing the generators of the results of PyAutoGalaxy model-fits. galaxies A list of galaxies corresponding to a sample of a non-linear search and model-fit. Returns ------- FitImaging The fit to the imaging dataset computed via an instance of galaxies. """ imaging = _imaging_from(fit=fit, settings_imaging=settings_imaging) tracer = _tracer_from(fit=fit, galaxies=galaxies) settings_pixelization = settings_pixelization or fit.value( name="settings_pixelization") settings_inversion = settings_inversion or fit.value( name="settings_inversion") preloads = Preloads(use_w_tilde=False) if use_preloaded_grid: sparse_grids_of_planes = fit.value( name="preload_sparse_grids_of_planes") if sparse_grids_of_planes is not None: preloads = Preloads( sparse_image_plane_grid_pg_list=sparse_grids_of_planes, use_w_tilde=False, ) if len(preloads.sparse_image_plane_grid_pg_list) == 2: if type(preloads.sparse_image_plane_grid_pg_list[1]) != list: preloads.sparse_image_plane_grid_pg_list[1] = [ preloads.sparse_image_plane_grid_pg_list[1] ] return FitImaging( dataset=imaging, tracer=tracer, settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, use_hyper_scaling=use_hyper_scaling, )
def visualize( self, paths: af.DirectoryPaths, instance: af.ModelInstance, during_analysis: bool, ): """ Output images of the maximum log likelihood model inferred by the model-fit. This function is called throughout the non-linear search at regular intervals, and therefore provides on-the-fly visualization of how well the model-fit is going. The visualization performed by this function includes: - Images of the best-fit `Tracer`, including the images of each of its galaxies. - Images of the best-fit `FitImaging`, including the model-image, residuals and chi-squared of its fit to the imaging data. - The hyper-images of the model-fit showing how the hyper galaxies are used to represent different galaxies in the dataset. - If hyper features are used to scale the noise or background sky, a `FitImaging` with these features turned off may be output, to indicate how much these features are altering the dataset. The images output by this function are customized using the file `config/visualize/plots.ini`. Parameters ---------- paths The PyAutoFit paths object which manages all paths, e.g. where the non-linear search outputs are stored, visualization, and the pickled objects used by the aggregator output by this function. instance An instance of the model that is being fitted to the data by this analysis (whose parameters have been set via a non-linear search). during_analysis If True the visualization is being performed midway through the non-linear search before it is finished, which may change which images are output. """ instance = self.instance_with_associated_hyper_images_from( instance=instance) fit = self.fit_imaging_via_instance_from(instance=instance) if self.positions_likelihood is not None: self.positions_likelihood.output_positions_info( output_path=paths.output_path, tracer=fit.tracer) if fit.inversion is not None: try: fit.inversion.reconstruction except exc.InversionException: return visualizer = VisualizerImaging(visualize_path=paths.image_path) visualizer.visualize_fit_imaging(fit=fit, during_analysis=during_analysis) visualizer.visualize_tracer(tracer=fit.tracer, grid=fit.grid, during_analysis=during_analysis) visualizer.visualize_galaxies(galaxies=fit.tracer.galaxies, grid=fit.grid, during_analysis=during_analysis) if fit.inversion is not None: visualizer.visualize_inversion(inversion=fit.inversion, during_analysis=during_analysis) visualizer.visualize_contribution_maps(tracer=fit.tracer) if visualizer.plot_fit_no_hyper: fit = self.fit_imaging_via_tracer_from( tracer=fit.tracer, hyper_image_sky=None, hyper_background_noise=None, use_hyper_scaling=False, preload_overwrite=Preloads(use_w_tilde=False), ) visualizer.visualize_fit_imaging(fit=fit, during_analysis=during_analysis, subfolders="fit_no_hyper")