def _new_shear(self, start_e1, start_e2, delta_phi, delta_gamma): phi_start, gamma_start = ellipticity2phi_gamma(start_e1, start_e2) phi_min, phi_max = phi_start + delta_phi, phi_start - delta_phi gamma_min, gamma_max = max(0.0001, gamma_start - delta_gamma), gamma_start + delta_gamma e1_min, e2_min = phi_gamma_ellipticity(phi_min, gamma_min) e1_max, e2_max = phi_gamma_ellipticity(phi_max, gamma_max) return e1_min, e2_min, e1_max, e2_max
def test_solver_simplified(self): lens_model_list = ['SPEP', 'SHEAR_GAMMA_PSI'] lensModel = LensModel(lens_model_list) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0.1 sourcePos_y = -0.1 deltapix = 0.05 numPix = 150 gamma = 1.9 gamma_ext = 0.05 psi_ext = 0.4 #e1, e2 = param_util.phi_gamma_ellipticity(phi=psi_ext, gamma=gamma_ext) kwargs_lens = [{'theta_E': 1., 'gamma': gamma, 'e1': 0.1, 'e2': -0.1, 'center_x': 0.1, 'center_y': -0.1}, {'gamma_ext': gamma_ext, 'psi_ext': psi_ext}] x_pos, y_pos = lensEquationSolver.findBrightImage(sourcePos_x, sourcePos_y, kwargs_lens, numImages=4, min_distance=deltapix, search_window=numPix * deltapix) e1_new, e2_new = param_util.phi_gamma_ellipticity(phi=0., gamma=gamma_ext+0.1) kwargs_lens_init = [{'theta_E': 1.3, 'gamma': gamma, 'e1': 0., 'e2': 0., 'center_x': 0., 'center_y': 0}, {'gamma_ext': gamma_ext + 0.1, 'psi_ext': 0}] solver = Solver4Point(lensModel, solver_type='PROFILE_SHEAR') kwargs_lens_new, accuracy = solver.constraint_lensmodel(x_pos, y_pos, kwargs_lens_init) assert accuracy < 10**(-10) x_source, y_source = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens_new) x_source, y_source = np.mean(x_source), np.mean(y_source) x_pos_new, y_pos_new = lensEquationSolver.findBrightImage(x_source, y_source, kwargs_lens_new, numImages=4, min_distance=deltapix, search_window=numPix * deltapix) print(x_pos, x_pos_new) x_pos = np.sort(x_pos) x_pos_new = np.sort(x_pos_new) y_pos = np.sort(y_pos) y_pos_new = np.sort(y_pos_new) for i in range(len(x_pos)): npt.assert_almost_equal(x_pos[i], x_pos_new[i], decimal=6) npt.assert_almost_equal(y_pos[i], y_pos_new[i], decimal=6)
def test_phi_gamma_ellipticity_2(): e1, e2 = -0.04, -0.01 phi, gamma = param_util.ellipticity2phi_gamma(e1, e2) e1_out, e2_out = param_util.phi_gamma_ellipticity(phi, gamma) npt.assert_almost_equal(e1, e1_out, decimal=10) npt.assert_almost_equal(e2, e2_out, decimal=10)
def _update_kwargs(self, x, kwargs_list): """ :param x: list of parameters corresponding to the free parameter of the first lens model in the list :param kwargs_list: list of lens model kwargs :return: updated kwargs_list """ lens_model = self._lens_mode_list[0] if self._solver_type == 'CENTER': [center_x, center_y] = x kwargs_list[0]['center_x'] = center_x kwargs_list[0]['center_y'] = center_y elif self._solver_type == 'ELLIPSE': [e1, e2] = x kwargs_list[0]['e1'] = e1 kwargs_list[0]['e2'] = e2 elif self._solver_type == 'SHAPELETS': [c10, c01] = x coeffs = list(kwargs_list[0]['coeffs']) coeffs[1:3] = [c10, c01] kwargs_list[0]['coeffs'] = coeffs elif self._solver_type == 'THETA_E_PHI': [theta_E, phi_G] = x kwargs_list[0]['theta_E'] = theta_E phi_G_no_sense, gamma_ext = param_util.ellipticity2phi_gamma( kwargs_list[1]['e1'], kwargs_list[1]['e2']) e1, e2 = param_util.phi_gamma_ellipticity(phi_G, gamma_ext) kwargs_list[1]['e1'] = e1 kwargs_list[1]['e2'] = e2 else: raise ValueError( "Solver type %s not supported for 2-point solver!" % self._solver_type) return kwargs_list
def test_phi_gamma_ellipticity(): phi = -1. gamma = 0.1 e1, e2 = param_util.phi_gamma_ellipticity(phi, gamma) print(e1, e2, 'e1, e2') phi_out, gamma_out = param_util.ellipticity2phi_gamma(e1, e2) assert phi == phi_out assert gamma == gamma_out
def test_hessian(self): x = np.array([1, 3, 4]) y = np.array([2, 1, 1]) gamma, psi = 0.1, 0.5 e1, e2 = param_util.phi_gamma_ellipticity(phi=psi, gamma=gamma) values = self.shear.hessian(x, y, gamma, psi) values_e1e2 = self.shear_e1e2.hessian(x, y, e1, e2) npt.assert_almost_equal(values, values_e1e2, decimal=5)
def test_solver_profile_shear_2(self): lens_model_list = ['SPEP', 'SHEAR'] lensModel = LensModel(lens_model_list) lensEquationSolver = LensEquationSolver(lensModel) sourcePos_x = 0. sourcePos_y = 0.1 deltapix = 0.05 numPix = 150 gamma = 1.98 e1, e2 = -0.04, -0.01 kwargs_shear = {'e1': e1, 'e2': e2} # shear values to the source plane kwargs_spemd = {'theta_E': 1.66, 'gamma': gamma, 'center_x': 0.0, 'center_y': 0.0, 'e1': 0.1, 'e2': 0.05} # parameters of the deflector lens model kwargs_lens = [kwargs_spemd, kwargs_shear] x_pos, y_pos = lensEquationSolver.findBrightImage(sourcePos_x, sourcePos_y, kwargs_lens, numImages=4, min_distance=deltapix, search_window=numPix * deltapix) print(x_pos, y_pos, 'test positions') gamma_ext = np.sqrt(e1 ** 2 + e2 ** 2) e1_init, e2_init = param_util.phi_gamma_ellipticity(gamma=gamma_ext, phi=-1.3) kwargs_lens_init = [{'theta_E': 1.3, 'gamma': gamma, 'e1': 0, 'e2': 0, 'center_x': 0., 'center_y': 0}, {'e1': e1_init, 'e2': e2_init}] solver = Solver4Point(lensModel, solver_type='PROFILE_SHEAR') kwargs_lens_new, accuracy = solver.constraint_lensmodel(x_pos, y_pos, kwargs_lens_init) assert accuracy < 10**(-10) x_source, y_source = lensModel.ray_shooting(x_pos, y_pos, kwargs_lens_new) x_source, y_source = np.mean(x_source), np.mean(y_source) x_pos_new, y_pos_new = lensEquationSolver.findBrightImage(x_source, y_source, kwargs_lens_new, numImages=4, min_distance=deltapix, search_window=numPix * deltapix) print(x_pos, x_pos_new) x_pos = np.sort(x_pos) x_pos_new = np.sort(x_pos_new) y_pos = np.sort(y_pos) y_pos_new = np.sort(y_pos_new) for i in range(len(x_pos)): npt.assert_almost_equal(x_pos[i], x_pos_new[i], decimal=6) npt.assert_almost_equal(y_pos[i], y_pos_new[i], decimal=6) npt.assert_almost_equal(kwargs_lens_new[1]['e1'], kwargs_lens[1]['e1'], decimal=8) npt.assert_almost_equal(kwargs_lens_new[1]['e2'], kwargs_lens[1]['e2'], decimal=8) npt.assert_almost_equal(kwargs_lens_new[0]['e1'], kwargs_lens[0]['e1'], decimal=8) npt.assert_almost_equal(kwargs_lens_new[0]['e2'], kwargs_lens[0]['e2'], decimal=8)
def _update_kwargs(self, x, kwargs_list): """ :param x: list of parameters corresponding to the free parameter of the first lens model in the list :param kwargs_list: list of lens model kwargs :return: updated kwargs_list """ if self._solver_type == 'PROFILE_SHEAR_GAMMA_PSI': phi_G = x[5] # % (2 * np.pi) kwargs_list[1]['psi_ext'] = phi_G if self._solver_type == 'PROFILE_SHEAR': phi_G = x[5] % np.pi phi_G_no_sense, gamma_ext = param_util.ellipticity2phi_gamma( kwargs_list[1]['e1'], kwargs_list[1]['e2']) e1, e2 = param_util.phi_gamma_ellipticity(phi_G, gamma_ext) kwargs_list[1]['e1'] = e1 kwargs_list[1]['e2'] = e2 lens_model = self._lens_mode_list[0] if lens_model in ['SPEP', 'SPEMD', 'SIE', 'NIE']: [theta_E, e1, e2, center_x, center_y, no_sens_param] = x kwargs_list[0]['theta_E'] = theta_E kwargs_list[0]['e1'] = e1 kwargs_list[0]['e2'] = e2 kwargs_list[0]['center_x'] = center_x kwargs_list[0]['center_y'] = center_y elif lens_model in ['NFW_ELLIPSE']: [alpha_Rs, e1, e2, center_x, center_y, no_sens_param] = x kwargs_list[0]['alpha_Rs'] = alpha_Rs kwargs_list[0]['e1'] = e1 kwargs_list[0]['e2'] = e2 kwargs_list[0]['center_x'] = center_x kwargs_list[0]['center_y'] = center_y elif lens_model in ['SHAPELETS_CART']: [c10, c01, c20, c11, c02, no_sens_param] = x coeffs = list(kwargs_list[0]['coeffs']) coeffs[1:6] = [c10, c01, c20, c11, c02] kwargs_list[0]['coeffs'] = coeffs else: raise ValueError( "Lens model %s not supported for 4-point solver!" % lens_model) return kwargs_list
def hessian(self, x, y, gamma_ext, psi_ext, ra_0=0, dec_0=0): e1, e2 = param_util.phi_gamma_ellipticity(psi_ext, gamma_ext) return self._shear_e1e2.hessian(x, y, e1, e2, ra_0, dec_0)
def derivatives(self, x, y, gamma_ext, psi_ext, ra_0=0, dec_0=0): # rotation angle e1, e2 = param_util.phi_gamma_ellipticity(psi_ext, gamma_ext) return self._shear_e1e2.derivatives(x, y, e1, e2, ra_0, dec_0)