Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ellipticity2phi_q_symmetry():
phi,q = 1.5, 0.8
e1,e2 = param_util.phi_q2_ellipticity(phi, q)
phi_new,q_new = param_util.ellipticity2phi_q(e1, e2)
assert phi == phi_new
assert q == q_new
phi,q = -1.5, 0.8
e1,e2 = param_util.phi_q2_ellipticity(phi, q)
phi_new,q_new = param_util.ellipticity2phi_q(e1, e2)
assert phi == phi_new
assert q == q_new
e1, e2 = 0.1, -0.1
phi, q = param_util.ellipticity2phi_q(e1, e2)
e1_new, e2_new = param_util.phi_q2_ellipticity(phi, q)
npt.assert_almost_equal(e1, e1_new, decimal=10)
npt.assert_almost_equal(e2, e2_new, decimal=10)
e1, e2 = 2.99, -0.0
def test_ellipticity2phi_q_symmetry():
phi,q = 1.5, 0.8
e1,e2 = param_util.phi_q2_ellipticity(phi, q)
phi_new,q_new = param_util.ellipticity2phi_q(e1, e2)
assert phi == phi_new
assert q == q_new
phi,q = -1.5, 0.8
e1,e2 = param_util.phi_q2_ellipticity(phi, q)
phi_new,q_new = param_util.ellipticity2phi_q(e1, e2)
assert phi == phi_new
assert q == q_new
e1, e2 = 0.1, -0.1
phi, q = param_util.ellipticity2phi_q(e1, e2)
e1_new, e2_new = param_util.phi_q2_ellipticity(phi, q)
npt.assert_almost_equal(e1, e1_new, decimal=10)
npt.assert_almost_equal(e2, e2_new, decimal=10)
e1, e2 = 2.99, -0.0
phi, q = param_util.ellipticity2phi_q(e1, e2)
print(phi, q)
e1_new, e2_new = param_util.phi_q2_ellipticity(phi, q)
phi_new, q_new = param_util.ellipticity2phi_q(e1_new, e2_new)
npt.assert_almost_equal(phi, phi_new, decimal=10)
npt.assert_almost_equal(q, q_new, decimal=10)
#npt.assert_almost_equal(e1, e1_new, decimal=10)
def derivatives(self, x, y, amp, sigma, e1, e2, center_x=0, center_y=0):
"""
returns df/dx and df/dy of the function
"""
phi_G, q = param_util.ellipticity2phi_q(e1, e2)
x_shift = x - center_x
y_shift = y - center_y
cos_phi = np.cos(phi_G)
sin_phi = np.sin(phi_G)
e = abs(1 - q)
x_ = (cos_phi * x_shift + sin_phi * y_shift) * np.sqrt(1 - e)
y_ = (-sin_phi * x_shift + cos_phi * y_shift) * np.sqrt(1 + e)
f_x_prim, f_y_prim = self.spherical.derivatives(x_, y_, amp=amp, sigma=sigma)
f_x_prim *= np.sqrt(1 - e)
f_y_prim *= np.sqrt(1 + e)
f_x = cos_phi * f_x_prim - sin_phi * f_y_prim
f_y = sin_phi * f_x_prim + cos_phi * f_y_prim
return f_x, f_y
def _param_conv(self, theta_E, e1, e2, t):
"""
convert parameters from R = r sqrt(1 − e*cos(2*phi)) to
R = sqrt(q^2 x^2 + y^2)
:param theta_E: Einstein radius
:param e1: eccentricity component
:param e2: eccentricity component
:param t: power law slope
:return: critical radius b, slope t, axis ratio q, orientation angle phi_G
"""
phi_G, q = param_util.ellipticity2phi_q(e1, e2)
theta_E_conv = self._theta_E_q_convert(theta_E, q)
b = theta_E_conv * np.sqrt((1 + q**2)/2)
return b, t, q, phi_G
def param_transform(self, x, y, theta_E, gamma, e1, e2, s_scale, center_x=0, center_y=0):
"""
transforms parameters in the format of fastell4py
:param x: x-coordinate (angle)
:param y: y-coordinate (angle)
:param theta_E: Einstein radius (angle), pay attention to specific definition!
:param gamma: logarithmic slope of the power-law profile. gamma=2 corresponds to isothermal
:param e1: eccentricity component
:param e2: eccentricity component
:param s_scale: smoothing scale in the center of the profile
:param center_x: x-position of lens center
:param center_y: y-position of lens center
:return: x-rotated, y-rotated, q_fastell, gam, s2, q, phi_G
"""
phi_G, q = param_util.ellipticity2phi_q(e1, e2)
x = np.array(x)
y = np.array(y)
x_shift = x - center_x
y_shift = y - center_y
q_fastell, gam, s2 = self.convert_params(theta_E, gamma, q, s_scale)
cos_phi = np.cos(phi_G)
sin_phi = np.sin(phi_G)
x1 = cos_phi * x_shift + sin_phi * y_shift
x2 = -sin_phi * x_shift + cos_phi * y_shift
return x1, x2, q_fastell, gam, s2, q, phi_G
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.shear_cartesian2polar(kwargs_list[1]['gamma1'], kwargs_list[1]['gamma2'])
gamma1, gamma2 = param_util.shear_polar2cartesian(phi_G, gamma_ext)
kwargs_list[1]['gamma1'] = gamma1
kwargs_list[1]['gamma2'] = gamma2
elif self._solver_type == 'THETA_E_ELLIPSE':
[theta_E, phi_G] = x
kwargs_list[0]['theta_E'] = theta_E
phi_G_no_sense, q = param_util.ellipticity2phi_q(kwargs_list[0]['e1'], kwargs_list[0]['e2'])
e1, e2 = param_util.phi_q2_ellipticity(phi_G, q)
kwargs_list[0]['e1'] = e1
kwargs_list[0]['e2'] = e2
else:
raise ValueError("Solver type %s not supported for 2-point solver!" % self._solver_type)
return kwargs_list
:param amp: Amplitude of Gaussian, convention: :math:`A/(2 \pi\sigma^2) \exp(-(x^2+y^2/q^2)/2\sigma^2)`
:type amp: ``float``
:param sigma: Standard deviation of Gaussian
:type sigma: ``float``
:param e1: Ellipticity parameter 1
:type e1: ``float``
:param e2: Ellipticity parameter 2
:type e2: ``float``
:param center_x: x coordinate of centroid
:type center_x: ``float``
:param center_y: y coordianate of centroid
:type center_y: ``float``
:return: Deflection angle :math:`\partial f/\partial x`, :math:`\partial f/\partial y` for elliptical Gaussian convergence.
:rtype: tuple ``(float, float)`` or ``(numpy.array, numpy.array)`` with each ``numpy.array``'s shape equal to ``x.shape``.
"""
phi_g, q = param_util.ellipticity2phi_q(e1, e2)
if q > 1 - self.min_ellipticity:
return self.spherical.derivatives(x, y, amp, sigma, center_x,
center_y)
# adjusting amplitude to make the notation compatible with the
# formulae given in Shajib (2019).
amp_ = amp / (2 * np.pi * sigma**2)
# converting ellipticity definition from x^2 + y^2/q^2 to q^2*x^2 + y^2
sigma_ = sigma * q
x_shift = x - center_x
y_shift = y - center_y
cos_phi = np.cos(phi_g)
sin_phi = np.sin(phi_g)
:param amp:
:type amp:
:param sigma:
:type sigma:
:param e1:
:type e1:
:param e2:
:type e2:
:param center_x:
:type center_x:
:param center_y:
:type center_y:
:return:
:rtype:
"""
phi_G, q = param_util.ellipticity2phi_q(e1, e2)
x_shift = x - center_x
y_shift = y - center_y
cos_phi = np.cos(phi_G)
sin_phi = np.sin(phi_G)
e = abs(1 - q)
x_ = (cos_phi * x_shift + sin_phi * y_shift) #* np.sqrt(1 - e)
y_ = (-sin_phi * x_shift + cos_phi * y_shift) #* np.sqrt(1 + e)
return amp * np.exp(-(q**2*x_**2+y_**2)/2/sigma**2)
def function(self, x, y, amp, Rs, e1, e2, center_x=0, center_y=0):
"""
:param x:
:param y:
:param amp:
:param a:
:param s:
:param center_x:
:param center_y:
:return:
"""
#TODO check ellipticity consistency with the mass profile kappa definition
#x_, y_ = param_util.transform_e1e2(x, y, e1, e2, center_x, center_y)
phi_G, q = param_util.ellipticity2phi_q(e1, e2)
x_ , y_ = self._coord_transf(x, y, q, phi_G, center_x, center_y)
return self.spherical.function(x_, y_, amp, Rs)