def test_interpolate(self): shapeletsInterp = Shapelets(interpolation=True) x, y = 0.99, 0 beta = 0.5 flux_full = self.shapelets.function(x, y, amp=1., n1=0, n2=0, beta=beta, center_x=0, center_y=0) flux_interp = shapeletsInterp.function(x, y, amp=1., n1=0, n2=0, beta=beta, center_x=0, center_y=0) npt.assert_almost_equal(flux_interp, flux_full, decimal=10)
class TestShapeletSet(object): """ class to test Shapelets """ def setup(self): self.shapeletSet = ShapeletSet() self.shapelets = Shapelets(precalc=False) self.x, self.y = util.make_grid(10, 0.1, 1) def test_shapelet_set(self): """ :return: """ n_max = 2 beta = 1. amp = [1, 0, 0, 0, 0, 0] output = self.shapeletSet.function(np.array(1), np.array(1), amp, n_max, beta, center_x=0, center_y=0) assert output == 0.20755374871029739 input = np.array(0.) input += output output = self.shapeletSet.function(self.x, self.y, amp, n_max, beta, center_x=0, center_y=0) assert output[10] == 0.47957022395315946 output = self.shapeletSet.function(1, 1, amp, n_max, beta, center_x=0, center_y=0) assert output == 0.20755374871029739 n_max = -1 beta = 1. amp = [1, 0, 0, 0, 0, 0] output = self.shapeletSet.function(np.array(1), np.array(1), amp, n_max, beta, center_x=0, center_y=0) assert output == 0 beta = 1. amp = 1 shapelets = Shapelets(precalc=False, stable_cut=False) output = shapelets.function(np.array(1), np.array(1), amp, beta, 0, 0, center_x=0, center_y=0) npt.assert_almost_equal(0.2075537487102974, output, decimal=8) def test_shapelet_basis(self): num_order = 5 beta = 1 numPix = 10 kernel_list = self.shapeletSet.shapelet_basis_2d( num_order, beta, numPix) assert kernel_list[0][4, 4] == 0.43939128946772255 def test_decomposition(self): """ :return: """ n_max = 2 beta = 10. deltaPix = 1 amp = np.array([1, 1, 1, 1, 1, 1]) x, y = util.make_grid(100, deltaPix, 1) input = self.shapeletSet.function(x, y, amp, n_max, beta, center_x=0, center_y=0) amp_out = self.shapeletSet.decomposition(input, x, y, n_max, beta, deltaPix, center_x=0, center_y=0) for i in range(len(amp)): npt.assert_almost_equal(amp_out[i], amp[i], decimal=4) def test_function_split(self): n_max = 2 beta = 10. deltaPix = 0.1 amp = np.array([1, 1, 1, 1, 1, 1]) x, y = util.make_grid(10, deltaPix, 1) function_set = self.shapeletSet.function_split(x, y, amp, n_max, beta, center_x=0, center_y=0) test_flux = self.shapelets.function(x, y, amp=1., n1=0, n2=0, beta=beta, center_x=0, center_y=0) print(np.shape(function_set)) print(np.shape(test_flux)) assert function_set[0][10] == test_flux[10] def test_interpolate(self): shapeletsInterp = Shapelets(interpolation=True) x, y = 0.99, 0 beta = 0.5 flux_full = self.shapelets.function(x, y, amp=1., n1=0, n2=0, beta=beta, center_x=0, center_y=0) flux_interp = shapeletsInterp.function(x, y, amp=1., n1=0, n2=0, beta=beta, center_x=0, center_y=0) npt.assert_almost_equal(flux_interp, flux_full, decimal=10) def test_hermval(self): x = np.linspace(0, 2000, 2001) n_array = [1, 2, 3, 0, 1] import numpy.polynomial.hermite as hermite out_true = hermite.hermval(x, n_array) out_approx = self.shapelets.hermval(x, n_array) shape_true = out_true * np.exp(-x**2 / 2.) shape_approx = out_approx * np.exp(-x**2 / 2.) npt.assert_almost_equal(shape_approx, shape_true, decimal=6) x = 2 n_array = [1, 2, 3, 0, 1] out_true = hermite.hermval(x, n_array) out_approx = self.shapelets.hermval(x, n_array) npt.assert_almost_equal(out_approx, out_true, decimal=6) x = 2001 n_array = [1, 2, 3, 0, 1] out_true = hermite.hermval(x, n_array) out_approx = self.shapelets.hermval(x, n_array) shape_true = out_true * np.exp(-x**2 / 2.) shape_approx = out_approx * np.exp(-x**2 / 2.) npt.assert_almost_equal(shape_approx, shape_true, decimal=6)
def setup(self): self.shapeletSet = ShapeletSet() self.shapelets = Shapelets(precalc=False) self.x, self.y = util.make_grid(10, 0.1, 1)
class TestShapeletSet(object): """ class to test Shapelets """ def setup(self): self.shapeletSet = ShapeletSet() self.shapelets = Shapelets(precalc=False) self.x, self.y = util.make_grid(10, 0.1, 1) def test_shapelet_set(self): """ :return: """ n_max = 2 beta = 1. amp = [1, 0, 0, 0, 0, 0] output = self.shapeletSet.function(np.array(1), np.array(1), amp, n_max, beta, center_x=0, center_y=0) assert output == 0.20755374871029739 input = np.array(0.) input += output output = self.shapeletSet.function(self.x, self.y, amp, n_max, beta, center_x=0, center_y=0) assert output[10] == 0.47957022395315946 output = self.shapeletSet.function(1, 1, amp, n_max, beta, center_x=0, center_y=0) assert output == 0.20755374871029739 def test_shapelet_basis(self): num_order = 5 beta = 1 numPix = 10 kernel_list = self.shapeletSet.shapelet_basis_2d( num_order, beta, numPix) assert kernel_list[0][4, 4] == 0.43939128946772255 def test_decomposition(self): """ :return: """ n_max = 2 beta = 10. deltaPix = 1 amp = np.array([1, 1, 1, 1, 1, 1]) x, y = util.make_grid(100, deltaPix, 1) input = self.shapeletSet.function(x, y, amp, n_max, beta, center_x=0, center_y=0) amp_out = self.shapeletSet.decomposition(input, x, y, n_max, beta, deltaPix, center_x=0, center_y=0) for i in range(len(amp)): npt.assert_almost_equal(amp_out[i], amp[i], decimal=4) def test_function_split(self): n_max = 2 beta = 10. deltaPix = 0.1 amp = np.array([1, 1, 1, 1, 1, 1]) x, y = util.make_grid(10, deltaPix, 1) function_set = self.shapeletSet.function_split(x, y, amp, n_max, beta, center_x=0, center_y=0) test_flux = self.shapelets.function(x, y, amp=1., n1=0, n2=0, beta=beta, center_x=0, center_y=0) print(np.shape(function_set)) print(np.shape(test_flux)) assert function_set[0][10] == test_flux[10] def test_interpolate(self): shapeletsInterp = Shapelets(interpolation=True) x, y = 0.99, 0 beta = 0.5 flux_full = self.shapelets.function(x, y, amp=1., n1=0, n2=0, beta=beta, center_x=0, center_y=0) flux_interp = shapeletsInterp.function(x, y, amp=1., n1=0, n2=0, beta=beta, center_x=0, center_y=0) npt.assert_almost_equal(flux_interp, flux_full, decimal=10)
def get_source_iter(self, param, num_order, beta, x_grid, y_grid, kwargs_source, x_pos, y_pos, sigma, cov_param=None): """ :param param: :param num_order: :param beta: :return: """ if not self.kwargs_options['source_type'] == 'NONE': new = {'I0_sersic': param[0], 'center_x': 0, 'center_y': 0} kwargs_source_new = kwargs_source.copy() kwargs_source_new.update(new) source = self.get_surface_brightness(x_grid, y_grid, **kwargs_source_new) else: source = np.zeros_like(x_grid) x_center = kwargs_source['center_x'] y_center = kwargs_source['center_y'] num_clumps = self.kwargs_options.get('num_clumps', 0) num_param_shapelets = (num_order+2)*(num_order+1)/2 if not self.kwargs_options.get('source_clump_type', 'Gaussian') == 'Shapelets': numShapelets_clump = 1 else: num_order_clump = self.kwargs_options.get('num_order_clump', 1) numShapelets_clump = (num_order_clump+2)*(num_order_clump+1)/2 shapelets = Shapelets(interpolation=False, precalc=False) error_map_source = np.zeros_like(x_grid) n1 = 0 n2 = 0 basis_functions = np.zeros((len(param), len(x_grid))) for i in range(len(param)-num_param_shapelets-num_clumps*numShapelets_clump, len(param)-num_clumps*numShapelets_clump): source += shapelets.function(x_grid, y_grid, param[i], beta, n1, n2, center_x=0, center_y=0) basis_functions[i, :] = shapelets.function(x_grid, y_grid, 1, beta, n1, n2, center_x=0, center_y=0) if n1 == 0: n1 = n2 + 1 n2 = 0 else: n1 -= 1 n2 += 1 if self.kwargs_options.get('source_clump_type', 'Gaussian') == 'Gaussian': for i in range(num_clumps): j = i + len(param) - num_clumps*numShapelets_clump source += self.gaussian.function(x_grid, y_grid, amp=param[j], sigma_x=sigma[i], sigma_y=sigma[i], center_x=x_pos[i]-x_center, center_y=y_pos[i]-y_center) elif self.kwargs_options.get('source_clump_type', 'Gaussian') == 'Shapelets': i = len(param)-num_clumps*numShapelets_clump for j in range(0, num_clumps): H_x, H_y = self.shapelets.pre_calc(x_grid, y_grid, sigma[j], num_order, x_pos[j]-x_center, y_pos[j]-y_center) n1 = 0 n2 = 0 for k in range(0, numShapelets_clump): kwargs_source_shapelet = {'center_x': x_pos[j], 'center_y': y_pos[j], 'n1': n1, 'n2': n2, 'beta': sigma[j], 'amp': param[i]} source += self.shapelets.function(H_x, H_y, **kwargs_source_shapelet) if n1 == 0: n1 = n2 + 1 n2 = 0 else: n1 -= 1 n2 += 1 i += 1 else: raise ValueError("clump_type %s not valid." %(self.kwargs_options['source_clump_type'])) if cov_param is not None: error_map_source = np.zeros_like(x_grid) for i in range(len(error_map_source)): error_map_source[i] = basis_functions[:, i].T.dot(cov_param).dot(basis_functions[:,i]) return util.array2image(source), util.array2image(error_map_source)