def inversion_interferometer_from( self, grid, visibilities, noise_map, transformer, settings_pixelization=aa.SettingsPixelization(), settings_inversion=aa.SettingsInversion(), preloads=Preloads(), ): if preloads.linear_obj_list is None: linear_obj_list = self.linear_obj_list_from( grid=grid, settings_pixelization=settings_pixelization, preloads=preloads, ) else: linear_obj_list = preloads.linear_obj_list return inversion_interferometer_unpacked_from( visibilities=visibilities, noise_map=noise_map, transformer=transformer, linear_obj_list=linear_obj_list, regularization_list=self.regularization_list, settings=settings_inversion, profiling_dict=self.profiling_dict, )
def fit_unit_instance_from( self, unit_value: float) -> Union[FitImaging, FitInterferometer]: """ Create a fit via the model using an input `unit_value`, where the `unit_value` defines unit hyper cube values of each parameter's prior in the model, used to map each value to physical values for the fit. Parameters ---------- unit_value The unit hyper cube values of each parameter's prior in the model, used to map each value to physical values for the fit. Returns ------- fit A fit object where an instance of the model has been fitted to the data. """ ignore_prior_limits = conf.instance["general"]["model"][ "ignore_prior_limits"] conf.instance["general"]["model"]["ignore_prior_limits"] = True instance = self.model.instance_from_unit_vector( unit_vector=[unit_value] * self.model.prior_count) conf.instance["general"]["model"][ "ignore_prior_limits"] = ignore_prior_limits return self.fit_func( instance=instance, preload_overwrite=Preloads(use_w_tilde=False), check_positions=False, )
def inversion_imaging_from( self, grid, image, noise_map, convolver, w_tilde, settings_pixelization=aa.SettingsPixelization(), settings_inversion=aa.SettingsInversion(), preloads=Preloads(), ): if preloads.linear_obj_list is None: linear_obj_list = self.linear_obj_list_from( grid=grid, settings_pixelization=settings_pixelization, preloads=preloads, ) else: linear_obj_list = preloads.linear_obj_list return inversion_imaging_unpacked_from( image=image, noise_map=noise_map, convolver=convolver, w_tilde=w_tilde, linear_obj_list=linear_obj_list, regularization_list=self.regularization_list, settings=settings_inversion, preloads=preloads, profiling_dict=self.profiling_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_for_instance( instance=self.instance, preload_overwrite=Preloads(use_w_tilde=False), check_positions=False, )
def linear_obj_list_from( self, grid: aa.type.Grid2DLike, settings_pixelization=aa.SettingsPixelization(), preloads=Preloads(), ): linear_obj_list = [] if preloads.traced_grids_of_planes_for_inversion is None: traced_grids_of_planes = self.traced_grid_list_of_inversion_from( grid=grid) else: traced_grids_of_planes = 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: for mapper_index in range( len(traced_sparse_grids_list_of_planes[plane_index])): mapper = plane.mapper_from( source_grid_slim=traced_grids_of_planes[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, ) linear_obj_list.append(mapper) return linear_obj_list
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.traced_grid_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 fit_random_instance_from(self) -> Union[FitImaging, FitInterferometer]: """ Create a fit via the model by guessing a a sequence of random fits until an exception is not returned. If the number of `preload_attempts` defined in the configuration files is exceeded a None is returned. Returns ------- fit A fit object where an instance of the model has been fitted to the data. """ preload_attempts = conf.instance["general"]["analysis"][ "preload_attempts"] ignore_prior_limits = conf.instance["general"]["model"][ "ignore_prior_limits"] conf.instance["general"]["model"]["ignore_prior_limits"] = True for i in range(preload_attempts): try: instance = self.model.random_instance() conf.instance["general"]["model"][ "ignore_prior_limits"] = ignore_prior_limits return self.fit_func( instance=instance, preload_overwrite=Preloads(use_w_tilde=False), check_positions=False, ) except Exception: pass if i == preload_attempts: conf.instance["general"]["model"][ "ignore_prior_limits"] = ignore_prior_limits
def __init__( self, dataset, tracer, hyper_background_noise=None, use_hyper_scaling=True, settings_pixelization=aa.SettingsPixelization(), settings_inversion=aa.SettingsInversion(), 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. """ self.tracer = tracer 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 self.profiling_dict = profiling_dict if use_hyper_scaling: if hyper_background_noise is not None: noise_map = hyper_background_noise.hyper_noise_map_complex_from( noise_map=dataset.noise_map) else: noise_map = dataset.noise_map else: noise_map = dataset.noise_map self.tracer = tracer self.profile_visibilities = self.tracer.visibilities_via_transformer_from( grid=dataset.grid, transformer=dataset.transformer) self.profile_subtracted_visibilities = (dataset.visibilities - self.profile_visibilities) if not tracer.has_pixelization: inversion = None model_visibilities = self.profile_visibilities else: inversion = tracer.inversion_interferometer_from( grid=dataset.grid_inversion, visibilities=self.profile_subtracted_visibilities, noise_map=noise_map, transformer=dataset.transformer, settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, ) model_visibilities = (self.profile_visibilities + inversion.mapped_reconstructed_data) fit = aa.FitDataComplex( data=dataset.visibilities, noise_map=noise_map, model_data=model_visibilities, inversion=inversion, use_mask_in_fit=False, profiling_dict=profiling_dict, ) super().__init__(dataset=dataset, fit=fit, profiling_dict=profiling_dict)
def test__set_sparse_grid_of_planes(): # sparse image plane of grids is None so no Preloading. tracer_0 = MockTracer(sparse_image_plane_grid_pg_list=[None, None]) tracer_1 = MockTracer(sparse_image_plane_grid_pg_list=[None, None]) fit_0 = MockFit(tracer=tracer_0) fit_1 = MockFit(tracer=tracer_1) preloads = Preloads(sparse_image_plane_grid_pg_list=1) preloads.set_sparse_image_plane_grid_pg_list(fit_0=fit_0, fit_1=fit_1) assert preloads.sparse_image_plane_grid_pg_list is None # sparse image plane of grids are different, indiciating the model parameters change the grid, so no preloading. tracer_0 = MockTracer( sparse_image_plane_grid_pg_list=[None, np.array([[1.0]])]) tracer_1 = MockTracer( sparse_image_plane_grid_pg_list=[None, np.array([[2.0]])]) fit_0 = MockFit(tracer=tracer_0) fit_1 = MockFit(tracer=tracer_1) preloads = Preloads(sparse_image_plane_grid_pg_list=1) preloads.set_sparse_image_plane_grid_pg_list(fit_0=fit_0, fit_1=fit_1) assert preloads.sparse_image_plane_grid_pg_list is None # sparse image plane of grids are the same meaning they are fixed in the model, so do preload. tracer_0 = MockTracer( sparse_image_plane_grid_pg_list=[None, np.array([[1.0]])]) tracer_1 = MockTracer( sparse_image_plane_grid_pg_list=[None, np.array([[1.0]])]) fit_0 = MockFit(tracer=tracer_0) fit_1 = MockFit(tracer=tracer_1) preloads = Preloads(sparse_image_plane_grid_pg_list=1) preloads.set_sparse_image_plane_grid_pg_list(fit_0=fit_0, fit_1=fit_1) assert preloads.sparse_image_plane_grid_pg_list[0] is None assert (preloads.sparse_image_plane_grid_pg_list[1] == np.array([[1.0] ])).all()
def test__set_traced_grids_of_planes(): # traced grids is None so no Preloading. tracer_0 = MockTracer(traced_grids_of_planes=[None, None]) tracer_1 = MockTracer(traced_grids_of_planes=[None, None]) fit_0 = MockFit(tracer=tracer_0) fit_1 = MockFit(tracer=tracer_1) preloads = Preloads(traced_grids_of_planes_for_inversion=1) preloads.set_traced_grids_of_planes_for_inversion(fit_0=fit_0, fit_1=fit_1) assert preloads.traced_grids_of_planes_for_inversion is None # traced grids are different, indiciating the model parameters change the grid, so no preloading. tracer_0 = MockTracer(traced_grids_of_planes=[None, np.array([[1.0]])]) tracer_1 = MockTracer(traced_grids_of_planes=[None, np.array([[2.0]])]) fit_0 = MockFit(tracer=tracer_0) fit_1 = MockFit(tracer=tracer_1) preloads = Preloads(traced_grids_of_planes_for_inversion=1) preloads.set_traced_grids_of_planes_for_inversion(fit_0=fit_0, fit_1=fit_1) assert preloads.traced_grids_of_planes_for_inversion is None # traced grids are the same meaning they are fixed in the model, so do preload. tracer_0 = MockTracer(traced_grids_of_planes=[None, np.array([[1.0]])]) tracer_1 = MockTracer(traced_grids_of_planes=[None, np.array([[1.0]])]) fit_0 = MockFit(tracer=tracer_0) fit_1 = MockFit(tracer=tracer_1) preloads = Preloads(traced_grids_of_planes_for_inversion=1) preloads.set_traced_grids_of_planes_for_inversion(fit_0=fit_0, fit_1=fit_1) assert preloads.traced_grids_of_planes_for_inversion[0] is None assert (preloads.traced_grids_of_planes_for_inversion[1] == np.array( [[1.0]])).all()
def test__info(): file_path = path.join("{}".format(path.dirname(path.realpath(__file__))), "files") file_preloads = path.join(file_path, "preloads.summary") preloads = Preloads( blurred_image=np.zeros(3), w_tilde=None, use_w_tilde=False, traced_grids_of_planes_for_inversion=None, sparse_image_plane_grid_pg_list=None, relocated_grid=None, linear_obj_list=None, operated_mapping_matrix=None, curvature_matrix_preload=None, ) af.formatter.output_list_of_strings_to_file(file=file_preloads, list_of_strings=preloads.info) results = open(file_preloads) lines = results.readlines() i = 0 assert lines[i] == f"W Tilde = False\n" i += 1 assert lines[i] == f"Use W Tilde = False\n" i += 1 assert lines[i] == f"\n" i += 1 assert lines[i] == f"Blurred Image = False\n" i += 1 assert lines[i] == f"Traced Grids of Planes (For LEq) = False\n" i += 1 assert lines[i] == f"Sparse Image-Plane Grids of Planes = False\n" i += 1 assert lines[i] == f"Relocated Grid = False\n" i += 1 assert lines[i] == f"Mapper = False\n" i += 1 assert lines[i] == f"Blurred Mapping Matrix = False\n" i += 1 assert lines[i] == f"Curvature Matrix Sparse = False\n" i += 1 assert lines[i] == f"Regularization Matrix = False\n" i += 1 assert lines[i] == f"Log Det Regularization Matrix Term = False\n" i += 1 preloads = Preloads( blurred_image=1, w_tilde=1, use_w_tilde=True, traced_grids_of_planes_for_inversion=1, relocated_grid=1, sparse_image_plane_grid_pg_list=1, linear_obj_list=1, operated_mapping_matrix=1, curvature_matrix_preload=1, regularization_matrix=1, log_det_regularization_matrix_term=1, ) af.formatter.output_list_of_strings_to_file(file=file_preloads, list_of_strings=preloads.info) results = open(file_preloads) lines = results.readlines() i = 0 assert lines[i] == f"W Tilde = True\n" i += 1 assert lines[i] == f"Use W Tilde = True\n" i += 1 assert lines[i] == f"\n" i += 1 assert lines[i] == f"Blurred Image = True\n" i += 1 assert lines[i] == f"Traced Grids of Planes (For LEq) = True\n" i += 1 assert lines[i] == f"Sparse Image-Plane Grids of Planes = True\n" i += 1 assert lines[i] == f"Relocated Grid = True\n" i += 1 assert lines[i] == f"Mapper = True\n" i += 1 assert lines[i] == f"Blurred Mapping Matrix = True\n" i += 1 assert lines[i] == f"Curvature Matrix Sparse = True\n" i += 1 assert lines[i] == f"Regularization Matrix = True\n" i += 1 assert lines[i] == f"Log Det Regularization Matrix Term = True\n" i += 1
def test__set_blurred_image(): # Blurred image is all zeros so preloads as zeros fit_0 = MockFit(blurred_image=np.zeros(2)) fit_1 = MockFit(blurred_image=np.zeros(2)) preloads = Preloads(blurred_image=1) preloads.set_blurred_image(fit_0=fit_0, fit_1=fit_1) assert (preloads.blurred_image == np.zeros(2)).all() # Blurred image are different, indicating the model parameters change the grid, so no preloading. fit_0 = MockFit(blurred_image=np.array([1.0])) fit_1 = MockFit(blurred_image=np.array([2.0])) preloads = Preloads(blurred_image=1) preloads.set_blurred_image(fit_0=fit_0, fit_1=fit_1) assert preloads.blurred_image is None # Blurred images are the same meaning they are fixed in the model, so do preload. fit_0 = MockFit(blurred_image=np.array([1.0])) fit_1 = MockFit(blurred_image=np.array([1.0])) preloads = Preloads(blurred_image=1) preloads.set_blurred_image(fit_0=fit_0, fit_1=fit_1) assert (preloads.blurred_image == np.array([1.0])).all()
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, ) -> 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, )
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.associate_hyper_images(instance=instance) fit = self.fit_imaging_for_instance(instance=instance) 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) 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_for_tracer( tracer=fit.tracer, hyper_image_sky=None, hyper_background_noise=None, use_hyper_scalings=False, preload_overwrite=Preloads(use_w_tilde=False), ) visualizer.visualize_fit_imaging(fit=fit, during_analysis=during_analysis, subfolders="fit_no_hyper")
def __init__( self, dataset, tracer, hyper_image_sky=None, hyper_background_noise=None, use_hyper_scaling=True, settings_pixelization=aa.SettingsPixelization(), settings_inversion=aa.SettingsInversion(), 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. """ 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 self.profiling_dict = profiling_dict if use_hyper_scaling: image = hyper_image_from(image=dataset.image, hyper_image_sky=hyper_image_sky) noise_map = hyper_noise_map_from( noise_map=dataset.noise_map, tracer=tracer, hyper_background_noise=hyper_background_noise, ) else: image = dataset.image noise_map = dataset.noise_map if preloads.blurred_image is None: self.blurred_image = self.tracer.blurred_image_2d_via_convolver_from( grid=dataset.grid, convolver=dataset.convolver, blurring_grid=dataset.blurring_grid, ) else: self.blurred_image = preloads.blurred_image self.profile_subtracted_image = image - self.blurred_image if not tracer.has_pixelization: inversion = None model_image = self.blurred_image else: inversion = tracer.inversion_imaging_from( grid=dataset.grid_inversion, image=self.profile_subtracted_image, noise_map=noise_map, convolver=dataset.convolver, w_tilde=dataset.w_tilde, settings_pixelization=settings_pixelization, settings_inversion=settings_inversion, preloads=preloads, ) model_image = self.blurred_image + inversion.mapped_reconstructed_image fit = aa.FitData( data=image, noise_map=noise_map, model_data=model_image, mask=dataset.mask, inversion=inversion, use_mask_in_fit=False, profiling_dict=profiling_dict, ) super().__init__(dataset=dataset, fit=fit, profiling_dict=profiling_dict)