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 _extract_array(self, kwargs_list): """ inverse of _update_kwargs :param kwargs_list: :return: """ lens_model = self._lens_mode_list[0] if self._solver_type == 'CENTER': center_x = kwargs_list[0]['center_x'] center_y = kwargs_list[0]['center_y'] x = [center_x, center_y] elif self._solver_type == 'ELLIPSE': e1 = kwargs_list[0]['e1'] e2 = kwargs_list[0]['e2'] x = [e1, e2] elif self._solver_type == 'SHAPELETS': coeffs = list(kwargs_list[0]['coeffs']) [c10, c01] = coeffs[1:3] x = [c10, c01] elif self._solver_type == 'THETA_E_PHI': theta_E = kwargs_list[0]['theta_E'] e1 = kwargs_list[1]['e1'] e2 = kwargs_list[1]['e2'] phi_ext, gamma_ext = param_util.ellipticity2phi_gamma(e1, e2) x = [theta_E, phi_ext] else: raise ValueError( "Solver type %s not supported for 2-point solver!" % self._solver_type) return x
def _extract_array(self, kwargs_list): """ inverse of _update_kwargs :param kwargs_list: :return: """ if self._solver_type == 'PROFILE_SHEAR': e1 = kwargs_list[1]['e1'] e2 = kwargs_list[1]['e2'] phi_ext, gamma_ext = param_util.ellipticity2phi_gamma(e1, e2) else: phi_ext = 0 lens_model = self._lens_mode_list[0] if lens_model in ['SPEP', 'SPEMD', 'SIE', 'NIE']: e1 = kwargs_list[0]['e1'] e2 = kwargs_list[0]['e2'] center_x = kwargs_list[0]['center_x'] center_y = kwargs_list[0]['center_y'] theta_E = kwargs_list[0]['theta_E'] x = [theta_E, e1, e2, center_x, center_y, phi_ext] elif lens_model in ['NFW_ELLIPSE']: e1 = kwargs_list[0]['e1'] e2 = kwargs_list[0]['e2'] center_x = kwargs_list[0]['center_x'] center_y = kwargs_list[0]['center_y'] theta_Rs = kwargs_list[0]['theta_Rs'] x = [theta_Rs, e1, e2, center_x, center_y, phi_ext] elif lens_model in ['SHAPELETS_CART']: coeffs = list(kwargs_list[0]['coeffs']) [c10, c01, c20, c11, c02] = coeffs[1: 6] x = [c10, c01, c20, c11, c02, phi_ext] else: raise ValueError("Lens model %s not supported for 4-point solver!" % lens_model) return x
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 function(self, x, y, e1, e2, ra_0=0, dec_0=0): # change to polar coordinates psi_ext, gamma_ext = param_util.ellipticity2phi_gamma(e1, e2) r, phi = param_util.cart2polar(x - ra_0, y - dec_0) f_ = 1. / 2 * gamma_ext * r**2 * np.cos(2 * (phi - psi_ext)) #x_ = x - ra_0 #y_ = y - dec_0 #f_ = 1/2. * (e1 * x_ * x_ + 2 * e2 * x_ * y_ - e1 * y_ * y_) return f_
def external_shear(self, kwargs_lens_list): """ :param kwargs_lens_list: :return: """ for i, model in enumerate(self.lens_model_list): if model == 'SHEAR': e1 = kwargs_lens_list[i]['e1'] e2 = kwargs_lens_list[i]['e2'] phi, gamma = param_util.ellipticity2phi_gamma(e1, e2) return phi, gamma return 0, 0
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 setParams(self, kwargs_list): """ :param kwargs: :return: """ args = [] for k, model in enumerate(self.model_list): kwargs = kwargs_list[k] kwargs_fixed = self.kwargs_fixed[k] param_names = self._param_name_list[k] for name in param_names: if not name in kwargs_fixed: if model in ['SHAPELETS_POLAR', 'SHAPELETS_CART' ] and name == 'coeffs': coeffs = kwargs['coeffs'] if self._solver_type == 'SHAPELETS' and k == 0: if self._num_images == 4: coeffs = coeffs[6:] elif self._num_images == 2: coeffs = coeffs[3:] args += list(coeffs) elif model in [ 'MULTI_GAUSSIAN_KAPPA', 'MULTI_GAUSSIAN_KAPPA_ELLIPSE' ] and name == 'amp': amp = kwargs['amp'] args += list(amp) elif model in [ 'MULTI_GAUSSIAN_KAPPA', 'MULTI_GAUSSIAN_KAPPA_ELLIPSE' ] and name == 'sigma': raise ValueError("%s must have fixed 'sigma' list!" % model) elif model in ['INTERPOL', 'INTERPOL_SCALED'] and name in [ 'f_', 'f_xx', 'f_xy', 'f_yy' ]: pass elif self._solver_type == 'PROFILE_SHEAR' and k == 1: if name == 'e1': _, gamma_ext = param_util.ellipticity2phi_gamma( kwargs['e1'], kwargs['e2']) args.append(gamma_ext) else: pass else: args.append(kwargs[name]) return args
def external_shear(self, kwargs_lens_list, foreground=False): """ :param kwargs_lens_list: :return: """ for i, model in enumerate(self._lensModel.lens_model_list): if foreground is True: shear_model = 'FOREGROUND_SHEAR' else: shear_model = 'SHEAR' if model == shear_model: e1 = kwargs_lens_list[i]['e1'] e2 = kwargs_lens_list[i]['e2'] phi, gamma = param_util.ellipticity2phi_gamma(e1, e2) return phi, gamma return 0, 0
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 test_solver_simplified_2(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.96 e1, e2 = -0.01, -0.01 psi_ext, gamma_ext = param_util.ellipticity2phi_gamma(e1, e2) kwargs_shear = {'gamma_ext': gamma_ext, 'psi_ext': psi_ext} # gamma_ext: shear strength, psi_ext: shear angel (in radian) kwargs_spemd = {'theta_E': 1., 'gamma': gamma, 'center_x': 0, 'center_y': 0, 'e1': -0.2, 'e2': -0.03} 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) kwargs_lens_init = [{'theta_E': 1.3, 'gamma': gamma, 'e1': 0, 'e2': 0, 'center_x': 0., 'center_y': 0}, {'gamma_ext': gamma_ext, 'psi_ext': psi_ext}] 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]['psi_ext'], kwargs_lens[1]['psi_ext'], decimal=8) npt.assert_almost_equal(kwargs_lens_new[1]['gamma_ext'], kwargs_lens[1]['gamma_ext'], decimal=8)
def function(self, x, y, e1, e2, ra_0=0, dec_0=0): # change to polar coordinates psi_ext, gamma_ext = param_util.ellipticity2phi_gamma(e1, e2) theta, phi = param_util.cart2polar(x - ra_0, y - dec_0) f_ = 1. / 2 * gamma_ext * theta**2 * np.cos(2 * (phi - psi_ext)) return f_
def derivatives(self, x, y, n_sersic, R_sersic, k_eff, e1, e2, center_x=0, center_y=0): phi_G, gam = param_util.ellipticity2phi_gamma(e1, e2) q = max(1 - gam, 0.00001) x, y = self._coord_rotate(x, y, phi_G, center_x, center_y) if isinstance(x, float) and isinstance(y, float): alpha_x, alpha_y = self._compute_derivative_atcoord( x, y, n_sersic, R_sersic, k_eff, phi_G, q, center_x=center_x, center_y=center_y) else: assert isinstance(x, np.ndarray) or isinstance(x, list) assert isinstance(y, np.ndarray) or isinstance(y, list) x = np.array(x) y = np.array(y) shape0 = x.shape assert shape0 == y.shape alpha_x, alpha_y = np.empty_like(x).ravel(), np.empty_like( y).ravel() if isinstance(phi_G, float) or isinstance(phi_G, int): phiG = np.ones_like(alpha_x) * float(phi_G) q = np.ones_like(alpha_x) * float(q) for i, (x_i, y_i, phi_i, q_i) in \ enumerate(zip(x.ravel(), y.ravel(), phiG.ravel(), q.ravel())): fxi, fyi = self._compute_derivative_atcoord(x_i, y_i, n_sersic, R_sersic, k_eff, phi_i, q_i, center_x=center_x, center_y=center_y) alpha_x[i], alpha_y[i] = fxi, fyi alpha_x = alpha_x.reshape(shape0) alpha_y = alpha_y.reshape(shape0) alpha_x, alpha_y = self._coord_rotate(alpha_x, alpha_y, -phi_G, 0, 0) return alpha_x, alpha_y