def test_mask_ellipse(): x, y = util.make_grid(numPix=100, deltapix=1) mask = mask_util.mask_ellipse(x, y, center_x=0, center_y=0, a=10, b=20, angle=0) assert mask[0] == 0
def kernel_gaussian(kernel_numPix, deltaPix, fwhm): sigma = util.fwhm2sigma(fwhm) #if kernel_numPix % 2 == 0: # kernel_numPix += 1 x_grid, y_grid = util.make_grid(kernel_numPix, deltaPix) gaussian = Gaussian() kernel = gaussian.function(x_grid, y_grid, amp=1., sigma=sigma, center_x=0, center_y=0) kernel /= np.sum(kernel) kernel = util.array2image(kernel) return kernel
def test_fwhm_kernel(): x_grid, y_gird = Util.make_grid(101, 1) sigma = 20 flux = gaussian.function(x_grid, y_gird, amp=1, sigma=sigma) kernel = Util.array2image(flux) kernel = kernel_util.kernel_norm(kernel) fwhm_kernel = kernel_util.fwhm_kernel(kernel) fwhm = Util.sigma2fwhm(sigma) npt.assert_almost_equal(fwhm / fwhm_kernel, 1, 2)
def critical_curve_tiling(self, kwargs_lens, compute_window=5, start_scale=0.5, max_order=10): """ :param kwargs_lens: :param compute_window: :param tiling_scale: :return: """ numPix = int(compute_window / start_scale) x_grid_init, y_grid_init = util.make_grid(numPix, deltapix=start_scale, subgrid_res=1) mag_init = util.array2image( self._lensModel.magnification(x_grid_init, y_grid_init, kwargs_lens)) x_grid_init = util.array2image(x_grid_init) y_grid_init = util.array2image(y_grid_init) ra_crit_list = [] dec_crit_list = [] # iterate through original triangles and return ra_crit, dec_crit list for i in range(numPix - 1): for j in range(numPix - 1): edge1 = [x_grid_init[i, j], y_grid_init[i, j], mag_init[i, j]] edge2 = [ x_grid_init[i + 1, j + 1], y_grid_init[i + 1, j + 1], mag_init[i + 1, j + 1] ] edge_90_1 = [ x_grid_init[i, j + 1], y_grid_init[i, j + 1], mag_init[i, j + 1] ] edge_90_2 = [ x_grid_init[i + 1, j], y_grid_init[i + 1, j], mag_init[i + 1, j] ] ra_crit, dec_crit = self._tiling_crit(edge1, edge2, edge_90_1, max_order=max_order, kwargs_lens=kwargs_lens) ra_crit_list += ra_crit # list addition dec_crit_list += dec_crit # list addition ra_crit, dec_crit = self._tiling_crit(edge1, edge2, edge_90_2, max_order=max_order, kwargs_lens=kwargs_lens) ra_crit_list += ra_crit # list addition dec_crit_list += dec_crit # list addition return np.array(ra_crit_list), np.array(dec_crit_list)
def test_update_iterative(self): fwhm = 0.5 sigma = util.fwhm2sigma(fwhm) x_grid, y_grid = util.make_grid(numPix=31, deltapix=0.05) from lenstronomy.LightModel.Profiles.gaussian import Gaussian gaussian = Gaussian() kernel_point_source = gaussian.function(x_grid, y_grid, amp=1., sigma=sigma, center_x=0, center_y=0) kernel_point_source /= np.sum(kernel_point_source) kernel_point_source = util.array2image(kernel_point_source) kwargs_psf = { 'psf_type': 'PIXEL', 'kernel_point_source': kernel_point_source } kwargs_psf_iter = { 'stacking_method': 'median', 'psf_symmetry': 2, 'psf_iter_factor': 0.2, 'block_center_neighbour': 0.1, 'error_map_radius': 0.5, 'new_procedure': True } kwargs_params = copy.deepcopy(self.kwargs_params) kwargs_ps = kwargs_params['kwargs_ps'] del kwargs_ps[0]['source_amp'] print(kwargs_params['kwargs_ps']) kwargs_psf_new = self.psf_fitting.update_iterative( kwargs_psf, kwargs_params, **kwargs_psf_iter) kernel_new = kwargs_psf_new['kernel_point_source'] kernel_true = self.kwargs_psf['kernel_point_source'] kernel_old = kwargs_psf['kernel_point_source'] diff_old = np.sum((kernel_old - kernel_true)**2) diff_new = np.sum((kernel_new - kernel_true)**2) assert diff_old > diff_new assert diff_new < 0.01 assert 'psf_error_map' in kwargs_psf_new kwargs_psf_new = self.psf_fitting.update_iterative( kwargs_psf, kwargs_params, num_iter=3, no_break=True, keep_psf_error_map=True) kernel_new = kwargs_psf_new['kernel_point_source'] kernel_true = self.kwargs_psf['kernel_point_source'] kernel_old = kwargs_psf['kernel_point_source'] diff_old = np.sum((kernel_old - kernel_true)**2) diff_new = np.sum((kernel_new - kernel_true)**2) assert diff_old > diff_new assert diff_new < 0.01
def test_function(self): x, y = util.make_grid(10, 0.1, 1) amp = 1. beta = 1. n = 2 m = 0 complex_bool = False flux = self.shapelets.function(x, y, amp, beta, n, m, complex_bool, center_x=0, center_y=0) npt.assert_almost_equal(np.sum(flux), 4.704663416542942, decimal=6) complex_bool = True flux = self.shapelets.function(x, y, amp, beta, n, m, complex_bool, center_x=0, center_y=0) npt.assert_almost_equal(np.sum(flux), 0, decimal=6) n = 5 m = 3 complex_bool = False flux = self.shapelets.function(x, y, amp, beta, n, m, complex_bool, center_x=0, center_y=0) npt.assert_almost_equal(np.sum(flux), 0, decimal=6) complex_bool = True flux = self.shapelets.function(x, y, amp, beta, n, m, complex_bool, center_x=0, center_y=0) npt.assert_almost_equal(np.sum(flux), 0, decimal=6)
def test_differentials(self): x, y = util.make_grid(numPix=10, deltapix=0.5) f_xx, f_xy, f_yx, f_yy = self.lensModel.hessian(x, y, self.kwargs) f_xx_num, f_xy_num, f_yx_num, f_yy_num = self.lensModel.hessian( x, y, self.kwargs, diff=0.00001) npt.assert_almost_equal(f_xy_num, f_yx_num, decimal=5) npt.assert_almost_equal(f_xx_num, f_xx, decimal=5) npt.assert_almost_equal(f_xy_num, f_xy, decimal=5) npt.assert_almost_equal(f_yx_num, f_yx, decimal=5) npt.assert_almost_equal(f_yy_num, f_yy, decimal=5)
def test_make_subgrid(): numPix = 101 deltapix = 1 x_grid, y_grid = util.make_grid(numPix, deltapix, subgrid_res=1) x_sub_grid, y_sub_grid = util.make_subgrid(x_grid, y_grid, subgrid_res=2) assert np.sum(x_grid) == 0 assert x_sub_grid[0] == -50.25 assert y_sub_grid[17] == -50.25 x_sub_grid_new, y_sub_grid_new = util.make_subgrid(x_grid, y_grid, subgrid_res=4) assert x_sub_grid_new[0] == -50.375
def test_arcs_at_image_position(self): # lensing quantities kwargs_spp = { 'theta_E': 1.26, 'gamma': 2., 'e1': 0.1, 'e2': -0.1, 'center_x': 0.0, 'center_y': 0.0 } # parameters of the deflector lens model # the lens model is a supperposition of an elliptical lens model with external shear lens_model_list = ['SPEP'] #, 'SHEAR'] kwargs_lens = [kwargs_spp] #, kwargs_shear] lens_model_class = LensModel(lens_model_list=lens_model_list) lensEquationSolver = LensEquationSolver(lens_model_class) source_x = 0. source_y = 0.05 x_image, y_image = lensEquationSolver.findBrightImage( source_x, source_y, kwargs_lens, numImages=4, min_distance=0.05, search_window=5) arc_model = LensModel(lens_model_list=['CURVED_ARC_SPP', 'SHIFT']) for i in range(len(x_image)): x0, y0 = x_image[i], y_image[i] print(x0, y0, i) ext = LensModelExtensions(lensModel=lens_model_class) kwargs_arc_i = ext.curved_arc_estimate(x0, y0, kwargs_lens) alpha_x, alpha_y = lens_model_class.alpha(x0, y0, kwargs_lens) kwargs_arc = [ kwargs_arc_i, { 'alpha_x': alpha_x, 'alpha_y': alpha_y } ] print(kwargs_arc_i) direction = kwargs_arc_i['direction'] print(np.cos(direction), np.sin(direction)) x, y = util.make_grid(numPix=5, deltapix=0.01) x = x0 y = y0 gamma1_arc, gamma2_arc = arc_model.gamma(x, y, kwargs_arc) gamma1, gamma2 = lens_model_class.gamma(x, y, kwargs_lens) print(gamma1, gamma2) npt.assert_almost_equal(gamma1_arc, gamma1, decimal=3) npt.assert_almost_equal(gamma2_arc, gamma2, decimal=3) theta_E_arc, gamma_arc, center_x_spp_arc, center_y_spp_arc = CurvedArcSPP.stretch2spp( **kwargs_arc_i) print(theta_E_arc, gamma_arc, center_x_spp_arc, center_y_spp_arc) npt.assert_almost_equal(center_x_spp_arc, 0, decimal=3) npt.assert_almost_equal(center_y_spp_arc, 0, decimal=3)
def setup(self): self.supersampling_factor = 3 lightModel = LightModel(light_model_list=['GAUSSIAN']) self.delta_pix = 1. x, y = util.make_grid(20, deltapix=self.delta_pix) x_sub, y_sub = util.make_grid(20*self.supersampling_factor, deltapix=self.delta_pix/self.supersampling_factor) kwargs = [{'amp': 1, 'sigma': 2, 'center_x': 0, 'center_y': 0}] flux = lightModel.surface_brightness(x, y, kwargs) self.model = util.array2image(flux) flux_sub = lightModel.surface_brightness(x_sub, y_sub, kwargs) self.model_sub = util.array2image(flux_sub) x, y = util.make_grid(5, deltapix=self.delta_pix) kwargs_kernel = [{'amp': 1, 'sigma': 1, 'center_x': 0, 'center_y': 0}] kernel = lightModel.surface_brightness(x, y, kwargs_kernel) self.kernel = util.array2image(kernel) / np.sum(kernel) x_sub, y_sub = util.make_grid(5*self.supersampling_factor, deltapix=self.delta_pix/self.supersampling_factor) kernel_sub = lightModel.surface_brightness(x_sub, y_sub, kwargs_kernel) self.kernel_sub = util.array2image(kernel_sub) / np.sum(kernel_sub)
def test_function(self): """ :return: """ x, y = util.make_grid(numPix=20, deltapix=1.) gauss = Gaussian() flux = gauss.function(x, y, amp=1., center_x=0., center_y=0., sigma=1.) image = util.array2image(flux) interp = Interpol() kwargs_interp = { 'image': image, 'scale': 1., 'phi_G': 0., 'center_x': 0., 'center_y': 0. } output = interp.function(x, y, **kwargs_interp) npt.assert_almost_equal(output, flux, decimal=0) flux = gauss.function(x - 1., y, amp=1., center_x=0., center_y=0., sigma=1.) kwargs_interp = { 'image': image, 'scale': 1., 'phi_G': 0., 'center_x': 1., 'center_y': 0. } output = interp.function(x, y, **kwargs_interp) npt.assert_almost_equal(output, flux, decimal=0) flux = gauss.function(x - 1., y - 1., amp=1, center_x=0., center_y=0., sigma=1.) kwargs_interp = { 'image': image, 'scale': 1., 'phi_G': 0., 'center_x': 1., 'center_y': 1. } output = interp.function(x, y, **kwargs_interp) npt.assert_almost_equal(output, flux, decimal=0) out = interp.function(x=1000, y=0, **kwargs_interp) assert out == 0
def test_convergence2surface_brightness(self): from lenstronomy.LightModel.Profiles.nie import NIE as NIE_Light nie_light = NIE_Light() kwargs = {'e1': 0.3, 'e2': -0.05, 's_scale': 0.5} x, y = util.make_grid(numPix=10, deltapix=0.1) f_xx, f_xy, f_yx, f_yy = self.nie.hessian(x, y, theta_E=1, **kwargs) kappa = 1 / 2. * (f_xx + f_yy) flux = nie_light.function(x, y, amp=1, **kwargs) npt.assert_almost_equal(kappa / np.sum(kappa), flux / np.sum(flux), decimal=5)
def test_update_iterative(self): fwhm = 0.3 sigma = util.fwhm2sigma(fwhm) x_grid, y_grid = util.make_grid(numPix=31, deltapix=0.05) from lenstronomy.LightModel.Profiles.gaussian import Gaussian gaussian = Gaussian() kernel_point_source = gaussian.function(x_grid, y_grid, amp=1., sigma_x=sigma, sigma_y=sigma, center_x=0, center_y=0) kernel_point_source /= np.sum(kernel_point_source) kernel_point_source = util.array2image(kernel_point_source) kwargs_psf = { 'psf_type': 'PIXEL', 'kernel_point_source': kernel_point_source } kwargs_psf_new = self.psf_fitting.update_iterative( kwargs_psf, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, factor=0.2, num_iter=3, symmetry=1) kernel_new = kwargs_psf_new['kernel_point_source'] kernel_true = self.kwargs_psf['kernel_point_source'] kernel_old = kwargs_psf['kernel_point_source'] diff_old = np.sum((kernel_old - kernel_true)**2) diff_new = np.sum((kernel_new - kernel_true)**2) assert diff_old > diff_new assert diff_new < 0.01 kwargs_psf_new = self.psf_fitting.update_iterative( kwargs_psf, self.kwargs_lens, self.kwargs_source, self.kwargs_lens_light, self.kwargs_ps, factor=0.2, num_iter=3, symmetry=1, no_break=True) kernel_new = kwargs_psf_new['kernel_point_source'] kernel_true = self.kwargs_psf['kernel_point_source'] kernel_old = kwargs_psf['kernel_point_source'] diff_old = np.sum((kernel_old - kernel_true)**2) diff_new = np.sum((kernel_new - kernel_true)**2) assert diff_old > diff_new assert diff_new < 0.01
def test_radial_profile(self): x_grid, y_grid = util.make_grid(numPix=20, deltapix=1) profile = Gaussian() light_grid = profile.function(x_grid, y_grid, amp=1., sigma=5) I_r, r = analysis_util.radial_profile(light_grid, x_grid, y_grid, center_x=0, center_y=0, n=None) assert I_r[0] == 0
def test_mask_half_moon(): x, y = util.make_grid(numPix=100, deltapix=1) mask = mask_util.mask_half_moon(x, y, center_x=0, center_y=0, r_in=5, r_out=10, phi0=0, delta_phi=np.pi) assert mask[0] == 0
def test_degrade_kernel(): x_grid, y_gird = Util.make_grid(19 * 5, 1., 1) sigma = 1.5 amp = 2 flux = gaussian.function(x_grid, y_gird, amp=2, sigma=sigma) kernel_super = Util.array2image(flux) / np.sum(flux) * amp for degrading_factor in range(7): kernel_degraded = kernel_util.degrade_kernel( kernel_super, degrading_factor=degrading_factor + 1) npt.assert_almost_equal(np.sum(kernel_degraded), amp, decimal=8)
def test_derivatives(self): x, y = util.make_grid(numPix=100, deltapix=0.1) dx = 0.000001 values = self.model.function(x, y, **self.kwargs_lens) alpha_x, alpha_y = self.model.derivatives(x, y, **self.kwargs_lens) values_dx = self.model.function(x + dx, y, **self.kwargs_lens) f_x_num = (values_dx - values) / dx npt.assert_almost_equal(f_x_num, alpha_x, decimal=3) dy = 0.000001 values_dy = self.model.function(x, y + dy, **self.kwargs_lens) f_y_num = (values_dy - values) / dy npt.assert_almost_equal(f_y_num, alpha_y, decimal=3)
def test_radial_profile(): from lenstronomy.LightModel.Profiles.gaussian import Gaussian gauss = Gaussian() x, y = util.make_grid(11, 1) flux = gauss.function(x, y, sigma=10, amp=1) data = util.array2image(flux) profile_r = image_util.radial_profile(data, center=[5, 5]) profile_r_true = gauss.function(np.linspace(0, stop=7, num=8), 0, sigma=10, amp=1) npt.assert_almost_equal(profile_r, profile_r_true, decimal=3)
def test_d_r_dx(self): x = 1 y = 0 out = calc_util.d_r_dx(x, y) assert out == 1 x, y = util.make_grid(numPix=10, deltapix=0.1) dx = 0.000001 out = calc_util.d_r_dx(x, y) r, phi = param_util.cart2polar(x, y) r_dx, phi_dx = param_util.cart2polar(x + dx, y) dr_dx = (r_dx - r) / dx npt.assert_almost_equal(dr_dx, out, decimal=5)
def test_d_r_dy(self): x = 1 y = 0 out = calc_util.d_r_dy(x, y) assert out == 0 x, y = util.make_grid(numPix=10, deltapix=0.1) dy = 0.000001 out = calc_util.d_r_dy(x, y) r, phi = param_util.cart2polar(x, y) r_dy, phi_dy = param_util.cart2polar(x, y + dy) dr_dy = (r_dy - r) / dy npt.assert_almost_equal(dr_dy, out, decimal=5)
def magnification_finite(self, x_pos, y_pos, kwargs_lens, source_sigma=0.003, window_size=0.1, grid_number=100, polar_grid=False, aspect_ratio=0.5): """ returns the magnification of an extended source with Gaussian light profile :param x_pos: x-axis positons of point sources :param y_pos: y-axis position of point sources :param kwargs_lens: lens model kwargs :param source_sigma: Gaussian sigma in arc sec in source :param window_size: size of window to compute the finite flux :param grid_number: number of grid cells per axis in the window to numerically compute the flux :return: numerically computed brightness of the sources """ mag_finite = np.zeros_like(x_pos) deltaPix = float(window_size) / grid_number from lenstronomy.LightModel.Profiles.gaussian import Gaussian quasar = Gaussian() x_grid, y_grid = util.make_grid(numPix=grid_number, deltapix=deltaPix, subgrid_res=1) if polar_grid is True: a = window_size * 0.5 b = window_size * 0.5 * aspect_ratio ellipse_inds = (x_grid * a**-1)**2 + (y_grid * b**-1)**2 <= 1 x_grid, y_grid = x_grid[ellipse_inds], y_grid[ellipse_inds] for i in range(len(x_pos)): ra, dec = x_pos[i], y_pos[i] center_x, center_y = self._lensModel.ray_shooting( ra, dec, kwargs_lens) if polar_grid is True: theta = np.arctan2(dec, ra) xcoord, ycoord = util.rotate(x_grid, y_grid, theta) else: xcoord, ycoord = x_grid, y_grid betax, betay = self._lensModel.ray_shooting( xcoord + ra, ycoord + dec, kwargs_lens) I_image = quasar.function(betax, betay, 1., source_sigma, center_x, center_y) mag_finite[i] = np.sum(I_image) * deltaPix**2 return mag_finite
def test_averaging_even_kernel(): subgrid_res = 4 x_grid, y_gird = Util.make_grid(19, 1., 1) sigma = 1.5 flux = gaussian.function(x_grid, y_gird, amp=1, sigma=sigma) kernel_super = Util.array2image(flux) kernel_pixel = kernel_util.averaging_even_kernel(kernel_super, subgrid_res) npt.assert_almost_equal(np.sum(kernel_pixel), 1, decimal=5) assert len(kernel_pixel) == 5 x_grid, y_gird = Util.make_grid(17, 1., 1) sigma = 1.5 flux = gaussian.function(x_grid, y_gird, amp=1, sigma=sigma) kernel_super = Util.array2image(flux) kernel_pixel = kernel_util.averaging_even_kernel(kernel_super, subgrid_res) npt.assert_almost_equal(np.sum(kernel_pixel), 1, decimal=5) assert len(kernel_pixel) == 5
def test_displace_eccentricity(): #x, y = np.array([1, 0]), np.array([0, 1]) x, y = util.make_grid(numPix=10, deltapix=1) e1 = 0.1 #.1 e2 = -0 #.1 center_x, center_y = 0, 0 x_, y_ = param_util.transform_e1e2_product_average(x, y, e1, e2, center_x=center_x, center_y=center_y) 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) print(cos_phi, sin_phi) xt1 = cos_phi * x_shift + sin_phi * y_shift xt2 = -sin_phi * x_shift + cos_phi * y_shift xt1 *= np.sqrt(q) xt2 /= np.sqrt(q) npt.assert_almost_equal(x_, xt1, decimal=8) npt.assert_almost_equal(y_, xt2, decimal=8) x, y = np.array([1, 0]), np.array([0, 1]) e1 = 0.1 #.1#.1 e2 = 0 center_x, center_y = 0, 0 x_, y_ = param_util.transform_e1e2_product_average(x, y, e1, e2, center_x=center_x, center_y=center_y) 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) print(cos_phi, sin_phi) xt1 = cos_phi * x_shift + sin_phi * y_shift xt2 = -sin_phi * x_shift + cos_phi * y_shift xt1 *= np.sqrt(q) xt2 /= np.sqrt(q) npt.assert_almost_equal(x_, xt1, decimal=8) npt.assert_almost_equal(y_, xt2, decimal=8)
def test_kernel_average_pixel(): gaussian = Gaussian() subgrid_res = 3 x_grid, y_gird = Util.make_grid(9, 1., subgrid_res) sigma = 2 flux = gaussian.function(x_grid, y_gird, amp=1, sigma=sigma) kernel_super = Util.array2image(flux) kernel_pixel = kernel_util.kernel_average_pixel(kernel_super, supersampling_factor=subgrid_res) npt.assert_almost_equal(np.sum(kernel_pixel), np.sum(kernel_super)) kernel_pixel = kernel_util.kernel_average_pixel(kernel_super, supersampling_factor=2) npt.assert_almost_equal(np.sum(kernel_pixel), np.sum(kernel_super))
def test_make_grid(): numPix = 11 deltapix = 1. grid = util.make_grid(numPix, deltapix) assert grid[0][0] == -5 assert np.sum(grid[0]) == 0. x_grid, y_grid = util.make_grid(numPix, deltapix, subgrid_res=2.) assert np.sum(x_grid) == 0. assert x_grid[0] == -5.25 x_grid, y_grid = util.make_grid(numPix, deltapix, subgrid_res=1, left_lower=True) assert x_grid[0] == 0. assert y_grid[0] == 0. # Similar tests for a non-rectangular grid x_grid, y_grid = util.make_grid((numPix, numPix - 1), deltapix) assert x_grid[0] == -5. assert y_grid[0] == -4.5 assert np.sum(x_grid) == np.sum(y_grid) == 0 x_grid, y_grid = util.make_grid(numPix, deltapix, subgrid_res=2.) assert np.sum(x_grid) == np.sum(y_grid) == 0 assert x_grid[0] == -5.25 x_grid, y_grid = util.make_grid(numPix, deltapix, left_lower=True) assert x_grid[0] == 0 assert y_grid[0] == 0
def pixel_kernel(self, num_pix): """ computes a pixelized kernel from the MGE parameters :param num_pix: int, size of kernel (odd number per axis) :return: pixel kernel centered """ from lenstronomy.LightModel.Profiles.gaussian import MultiGaussian mg = MultiGaussian() x, y = util.make_grid(numPix=num_pix, deltapix=self._pixel_scale) kernel = mg.function(x, y, amp=self._fraction_list, sigma=self._sigmas_scaled) kernel = util.array2image(kernel) return kernel / np.sum(kernel)
def test_get_axes(): numPix = 11 deltapix = 0.1 x_grid, y_grid = util.make_grid(numPix, deltapix) x_axes, y_axes = util.get_axes(x_grid, y_grid) npt.assert_almost_equal(x_axes[0], -0.5, decimal=12) npt.assert_almost_equal(y_axes[0], -0.5, decimal=12) npt.assert_almost_equal(x_axes[1], -0.4, decimal=12) npt.assert_almost_equal(y_axes[1], -0.4, decimal=12) x_grid += 1 x_axes, y_axes = util.get_axes(x_grid, y_grid) npt.assert_almost_equal(x_axes[0], 0.5, decimal=12) npt.assert_almost_equal(y_axes[0], -0.5, decimal=12)
def setup(self): lightModel = LightModel(light_model_list=['GAUSSIAN']) self.supersampling_factor = 3 self.delta_pix = 1 self.num_pix = 10 self.num_pix_kernel = 7 x, y = util.make_grid(numPix=self.num_pix_kernel, deltapix=self.delta_pix) kwargs_kernel = [{'amp': 1, 'sigma': 3, 'center_x': 0, 'center_y': 0}] kernel = lightModel.surface_brightness(x, y, kwargs_kernel) self.kernel = util.array2image(kernel) self.kernel /= np.sum(self.kernel) x_sub, y_sub = util.make_grid(numPix=self.num_pix_kernel, deltapix=self.delta_pix, subgrid_res=self.supersampling_factor) kernel_super = lightModel.surface_brightness(x_sub, y_sub, kwargs_kernel) self.kernel_super = util.array2image(kernel_super) self.kernel_super /= np.sum(self.kernel_super) x_sub, y_sub = util.make_grid(numPix=self.num_pix, deltapix=self.delta_pix, subgrid_res=self.supersampling_factor) kwargs = [{'amp': 1, 'sigma': 2, 'center_x': 0, 'center_y': 0}] flux = lightModel.surface_brightness(x_sub, y_sub, kwargs) self.model_super = util.array2image(flux) self.model = image_util.re_size(self.model_super, factor=self.supersampling_factor)
def test_get_axes(): numPix = 11 deltapix = 0.1 x_grid, y_grid = Util.make_grid(numPix, deltapix) x_axes, y_axes = Util.get_axes(x_grid, y_grid) assert x_axes[0] == -0.5 assert y_axes[0] == -0.5 assert x_axes[1] == -0.4 assert y_axes[1] == -0.4 x_grid += 1 x_axes, y_axes = Util.get_axes(x_grid, y_grid) assert x_axes[0] == 0.5 assert y_axes[0] == -0.5
def psf_configure_simple(psf_type="GAUSSIAN", fwhm=1, kernelsize=11, deltaPix=1, truncate=6, kernel=None): """ this routine generates keyword arguments to initialize a PSF() class in lenstronomy. Have a look at the PSF class documentation to see the full possibilities. :param psf_type: string, type of PSF model :param fwhm: Full width at half maximum of PSF (if GAUSSIAN psf) :param kernelsize: size in pixel of kernel (use odd numbers), only applicable for PIXEL kernels :param deltaPix: pixel size in angular units (only needed for GAUSSIAN kernel :param truncate: how many sigmas out is the truncation happening :param kernel: 2d numpy arra centered PSF (odd number per axis) :return: keyword arguments """ if psf_type == 'GAUSSIAN': sigma = util.fwhm2sigma(fwhm) sigma_axis = sigma gaussian = Gaussian() x_grid, y_grid = util.make_grid(kernelsize, deltaPix) kernel_large = gaussian.function(x_grid, y_grid, amp=1., sigma_x=sigma_axis, sigma_y=sigma_axis, center_x=0, center_y=0) kernel_large /= np.sum(kernel_large) kernel_large = util.array2image(kernel_large) kernel_pixel = kernel_util.pixel_kernel(kernel_large) kwargs_psf = { 'psf_type': psf_type, 'fwhm': fwhm, 'truncation': truncate * fwhm, 'kernel_point_source': kernel_large, 'kernel_pixel': kernel_pixel, 'pixel_size': deltaPix } elif psf_type == 'PIXEL': kernel_large = copy.deepcopy(kernel) kernel_large = kernel_util.cut_psf(kernel_large, psf_size=kernelsize) kwargs_psf = {'psf_type': "PIXEL", 'kernel_point_source': kernel_large} elif psf_type == 'NONE': kwargs_psf = {'psf_type': 'NONE'} else: raise ValueError("psf type %s not supported!" % psf_type) return kwargs_psf