def test_wls_stability(self): A = np.array([[1, 2, 3], [3, 2, 1]]).T C_D_inv = np.array([0, 0, 0]) d = np.array([1, 2, 3]) result, cov_error, image = de_lens.get_param_WLS(A, C_D_inv, d) npt.assert_almost_equal(result[0], 0, decimal=8) npt.assert_almost_equal(result[1], 0, decimal=8) npt.assert_almost_equal(image[0], 0, decimal=8) A = np.array([[1, 2, 1], [1, 2, 1]]).T d = np.array([1, 2, 3]) result, cov_error, image = de_lens.get_param_WLS(A, C_D_inv, d, inv_bool=False) npt.assert_almost_equal(result[0], 0, decimal=8) npt.assert_almost_equal(result[1], 0, decimal=8) npt.assert_almost_equal(image[0], 0, decimal=8) C_D_inv = np.array([1, 1, 1]) A = np.array([[1., 2., 1. + 10**(-8.9)], [1., 2., 1.]]).T d = np.array([1, 2, 3]) result, cov_error, image = de_lens.get_param_WLS(A, C_D_inv, d, inv_bool=False) result, cov_error, image = de_lens.get_param_WLS(A, C_D_inv, d, inv_bool=True) npt.assert_almost_equal(result[0], 0, decimal=8) npt.assert_almost_equal(result[1], 0, decimal=8) npt.assert_almost_equal(image[0], 0, decimal=8)
def test_get_param_WLS(self): A = np.array([[1, 2, 3], [3, 2, 1]]).T C_D_inv = np.array([1, 1, 1]) d = np.array([1, 2, 3]) result, cov_error, image = de_lens.get_param_WLS(A, C_D_inv, d) npt.assert_almost_equal(result[0], 1, decimal=8) npt.assert_almost_equal(result[1], 0, decimal=8) npt.assert_almost_equal(image[0], d[0], decimal=8) result_new, cov_error_new, image_new = de_lens.get_param_WLS(A, C_D_inv, d, inv_bool=False) npt.assert_almost_equal(result_new[0], result[0], decimal=10) npt.assert_almost_equal(result_new[1], result[1], decimal=10) npt.assert_almost_equal(image_new[0], image[0], decimal=10)
def image_linear_solve(self, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, inv_bool=False): """ computes the image (lens and source surface brightness with a given lens model). The linear parameters are computed with a weighted linear least square optimization (i.e. flux normalization of the brightness profiles) :param kwargs_lens: list of keyword arguments corresponding to the superposition of different lens profiles :param kwargs_source: list of keyword arguments corresponding to the superposition of different source light profiles :param kwargs_lens_light: list of keyword arguments corresponding to different lens light surface brightness profiles :param kwargs_ps: keyword arguments corresponding to "other" parameters, such as external shear and point source image positions :param inv_bool: if True, invert the full linear solver Matrix Ax = y for the purpose of the covariance matrix. :return: 1d array of surface brightness pixels of the optimal solution of the linear parameters to match the data """ A = self.linear_response_matrix(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps) C_D_response, model_error_list = self.error_response( kwargs_lens, kwargs_ps) d = self.data_response param, cov_param, wls_model = de_lens.get_param_WLS(A.T, 1 / C_D_response, d, inv_bool=inv_bool) _, _, _, _ = self._imageModel_list[0]._update_linear_kwargs( param, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps) wls_list = self._array2image_list(wls_model) return wls_list, model_error_list, cov_param, param
def _image_linear_solve(self, kwargs_lens=None, kwargs_source=None, kwargs_lens_light=None, kwargs_ps=None, kwargs_extinction=None, kwargs_special=None, inv_bool=False): """ computes the image (lens and source surface brightness with a given lens model). By default, the linear parameters are computed with a weighted linear least square optimization (i.e. flux normalization of the brightness profiles) However in case of pixel-based modelling, pixel values are constrained by an external solver (e.g. SLITronomy). :param kwargs_lens: list of keyword arguments corresponding to the superposition of different lens profiles :param kwargs_source: list of keyword arguments corresponding to the superposition of different source light profiles :param kwargs_lens_light: list of keyword arguments corresponding to different lens light surface brightness profiles :param kwargs_ps: keyword arguments corresponding to "other" parameters, such as external shear and point source image positions :param inv_bool: if True, invert the full linear solver Matrix Ax = y for the purpose of the covariance matrix. This has no impact in case of pixel-based modelling. :return: 2d array of surface brightness pixels of the optimal solution of the linear parameters to match the data """ if self._pixelbased_bool is True: model, model_error, cov_param, param = self.image_pixelbased_solve(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, kwargs_extinction, kwargs_special) else: A = self._linear_response_matrix(kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, kwargs_extinction, kwargs_special) C_D_response, model_error = self._error_response(kwargs_lens, kwargs_ps, kwargs_special=kwargs_special) d = self.data_response param, cov_param, wls_model = de_lens.get_param_WLS(A.T, 1 / C_D_response, d, inv_bool=inv_bool) model = self.array_masked2image(wls_model) _, _, _, _ = self.update_linear_kwargs(param, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps) return model, model_error, cov_param, param
def image_linear_solve(self, kwargs_lens=None, kwargs_source=None, kwargs_lens_light=None, kwargs_ps=None, inv_bool=False): """ computes the image (lens and source surface brightness with a given lens model). The linear parameters are computed with a weighted linear least square optimization (i.e. flux normalization of the brightness profiles) :param kwargs_lens: list of keyword arguments corresponding to the superposition of different lens profiles :param kwargs_source: list of keyword arguments corresponding to the superposition of different source light profiles :param kwargs_lens_light: list of keyword arguments corresponding to different lens light surface brightness profiles :param kwargs_ps: keyword arguments corresponding to "other" parameters, such as external shear and point source image positions :param inv_bool: if True, invert the full linear solver Matrix Ax = y for the purpose of the covariance matrix. :return: 1d array of surface brightness pixels of the optimal solution of the linear parameters to match the data """ if not self.LensModel is None: x_source, y_source = self.LensModel.ray_shooting(self.ImageNumerics.ra_grid_ray_shooting, self.ImageNumerics.dec_grid_ray_shooting, kwargs_lens) else: x_source, y_source = self.ImageNumerics.ra_grid_ray_shooting, self.ImageNumerics.dec_grid_ray_shooting A = self._response_matrix(self.ImageNumerics.ra_grid_ray_shooting, self.ImageNumerics.dec_grid_ray_shooting, x_source, y_source, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, self.ImageNumerics.mask) error_map = self.error_map(kwargs_lens, kwargs_ps) error_map = self.ImageNumerics.image2array(error_map) d = self.ImageNumerics.image2array(self.Data.data*self.ImageNumerics.mask) param, cov_param, wls_model = de_lens.get_param_WLS(A.T, 1 / (self.ImageNumerics.C_D_response + error_map), d, inv_bool=inv_bool) _, _, _, _ = self._update_linear_kwargs(param, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps) model = self.ImageNumerics.array2image(wls_model) error_map = self.ImageNumerics.array2image(error_map) return model, error_map, cov_param, param
def test_marginalisation_const(self): A = np.array([[1,2,3],[3,2,1]]).T C_D_inv = np.array([1,1,1]) d = np.array([1,2,3]) result, cov_error, image = de_lens.get_param_WLS(A, C_D_inv, d) logL_marg = de_lens.marginalisation_const(cov_error) npt.assert_almost_equal(logL_marg, -2.2821740957339181, decimal=8) M_inv = np.array([[1,0],[0,1]]) marg_const = de_lens.marginalisation_const(M_inv) assert marg_const == 0