def test_shapelet_fit(): """Test fitting a Shapelet decomposition of an image """ import time t1 = time.time() for method, norm in [('no_pixel','f'), ('sb','sb')]: # We fit a shapelet approximation of a distorted Moffat profile: flux = 20 psf = galsim.Moffat(beta=3.4, half_light_radius=1.2, flux=flux) psf = psf.shear(g1=0.11,g2=0.07).shift(0.03,0.04) scale = 0.2 pixel = galsim.Pixel(scale) conv = galsim.Convolve([psf,pixel]) im1 = conv.drawImage(scale=scale, method=method) sigma = 1.2 # Match half-light-radius as a decent first approximation. shapelet = galsim.FitShapelet(sigma, 10, im1, normalization=norm) print 'fitted shapelet coefficients = ',shapelet.bvec # Check flux print 'flux = ',shapelet.getFlux(),' cf. ',flux np.testing.assert_almost_equal(shapelet.getFlux() / flux, 1., 1, err_msg="Fitted shapelet has the wrong flux") # Test centroid print 'centroid = ',shapelet.centroid(),' cf. ',conv.centroid() np.testing.assert_almost_equal(shapelet.centroid().x, conv.centroid().x, 2, err_msg="Fitted shapelet has the wrong centroid (x)") np.testing.assert_almost_equal(shapelet.centroid().y, conv.centroid().y, 2, err_msg="Fitted shapelet has the wrong centroid (y)") # Test drawing image from shapelet im2 = im1.copy() shapelet.drawImage(im2, method=method) # Check that images are close to the same: print 'norm(diff) = ',np.sum((im1.array-im2.array)**2) print 'norm(im) = ',np.sum(im1.array**2) assert np.sum((im1.array-im2.array)**2) < 1.e-3 * np.sum(im1.array**2) # Remeasure -- should now be very close to the same. shapelet2 = galsim.FitShapelet(sigma, 10, im2, normalization=norm) np.testing.assert_equal(shapelet.sigma, shapelet2.sigma, err_msg="Second fitted shapelet has the wrong sigma") np.testing.assert_equal(shapelet.order, shapelet2.order, err_msg="Second fitted shapelet has the wrong order") np.testing.assert_almost_equal(shapelet.bvec, shapelet2.bvec, 6, err_msg="Second fitted shapelet coefficients do not match original") t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def fitImage(self, image, center=None, normalization='flux'): """An obsolete method that is roughly equivalent to self = galsim.FitShapelet(self.sigma, self.order, image) """ new_obj = galsim.FitShapelet(self.sigma, self.order, image, center, normalization) bvec = new_obj.SBProfile.getBVec() GSObject.__init__(self, galsim._galsim.SBShapelet(self.sigma, bvec))
def Shapelet_fitImage(self, image, center=None, normalization='flux'): """A deprecated method that is roughly equivalent to self = galsim.FitShapelet(self.sigma, self.order, image) """ depr('fitImage', 1.1, 'galsim.FitShapelet') new_obj = galsim.FitShapelet(self.sigma, self.order, image, center, normalization) bvec = new_obj.SBProfile.getBVec() galsim.GSObject.__init__(self, galsim._galsim.SBShapelet(self.sigma, bvec))
def test_dep_shapelet(): """Test the deprecated methods in galsim/deprecated/shapelet.py """ np.testing.assert_almost_equal(check_dep(galsim.LVectorSize, 12), galsim.ShapeletSize(12)) # The next bit is from the old test_shapelet_adjustments() test ftypes = [np.float32, np.float64] nx = 128 ny = 128 scale = 0.2 im = galsim.ImageF(nx, ny, scale=scale) sigma = 1.8 order = 6 bvec = [ 1.3, # n = 0 0.02, 0.03, # n = 1 0.23, -0.19, 0.08, # n = 2 0.01, 0.02, 0.04, -0.03, # n = 3 -0.09, 0.07, -0.11, -0.08, 0.11, # n = 4 -0.03, -0.02, -0.08, 0.01, -0.06, -0.03, # n = 5 0.06, -0.02, 0.00, -0.05, -0.04, 0.01, 0.09 ] # n = 6 ref_shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec) ref_im = galsim.ImageF(nx, ny) ref_shapelet.drawImage(ref_im, scale=scale, method='no_pixel') # test setsigma shapelet = galsim.Shapelet(sigma=1., order=order, bvec=bvec) check_dep(shapelet.setSigma, sigma) shapelet.drawImage(im, method='no_pixel') np.testing.assert_array_almost_equal( im.array, ref_im.array, 6, err_msg="Shapelet set with setSigma disagrees with reference Shapelet") # Test setBVec shapelet = galsim.Shapelet(sigma=sigma, order=order) check_dep(shapelet.setBVec, bvec) shapelet.drawImage(im, method='no_pixel') np.testing.assert_array_almost_equal( im.array, ref_im.array, 6, err_msg="Shapelet set with setBVec disagrees with reference Shapelet") # Test setOrder shapelet = galsim.Shapelet(sigma=sigma, order=2) check_dep(shapelet.setOrder, order) check_dep(shapelet.setBVec, bvec) shapelet.drawImage(im, method='no_pixel') np.testing.assert_array_almost_equal( im.array, ref_im.array, 6, err_msg="Shapelet set with setOrder disagrees with reference Shapelet") # Test that changing the order preserves the values to the extent possible. shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec) check_dep(shapelet.setOrder, 10) np.testing.assert_array_equal( shapelet.getBVec()[0:28], bvec, err_msg="Shapelet setOrder to larger doesn't preserve existing values." ) np.testing.assert_array_equal( shapelet.getBVec()[28:66], np.zeros(66 - 28), err_msg="Shapelet setOrder to larger doesn't fill with zeros.") check_dep(shapelet.setOrder, 6) np.testing.assert_array_equal( shapelet.getBVec(), bvec, err_msg= "Shapelet setOrder back to original from larger doesn't preserve existing values." ) check_dep(shapelet.setOrder, 3) np.testing.assert_array_equal( shapelet.getBVec()[0:10], bvec[0:10], err_msg="Shapelet setOrder to smaller doesn't preserve existing values." ) check_dep(shapelet.setOrder, 6) np.testing.assert_array_equal( shapelet.getBVec()[0:10], bvec[0:10], err_msg= "Shapelet setOrder back to original from smaller doesn't preserve existing values." ) check_dep(shapelet.setOrder, 6) np.testing.assert_array_equal( shapelet.getBVec()[10:28], np.zeros(28 - 10), err_msg= "Shapelet setOrder back to original from smaller doesn't fill with zeros." ) # Test that setting a Shapelet with setNM gives the right profile shapelet = galsim.Shapelet(sigma=sigma, order=order) i = 0 for n in range(order + 1): for m in range(n, -1, -2): if m == 0: check_dep(shapelet.setNM, n, m, bvec[i]) i = i + 1 else: check_dep(shapelet.setNM, n, m, bvec[i], bvec[i + 1]) i = i + 2 shapelet.drawImage(im, method='no_pixel') np.testing.assert_array_almost_equal( im.array, ref_im.array, 6, err_msg="Shapelet set with setNM disagrees with reference Shapelet") # Test that setting a Shapelet with setPQ gives the right profile shapelet = galsim.Shapelet(sigma=sigma, order=order) i = 0 for n in range(order + 1): for m in range(n, -1, -2): p = (n + m) // 2 q = (n - m) // 2 if m == 0: check_dep(shapelet.setPQ, p, q, bvec[i]) i = i + 1 else: check_dep(shapelet.setPQ, p, q, bvec[i], bvec[i + 1]) i = i + 2 shapelet.drawImage(im, method='no_pixel') np.testing.assert_array_almost_equal( im.array, ref_im.array, 6, err_msg="Shapelet set with setPQ disagrees with reference Shapelet") # Check fitImage s1 = galsim.Shapelet(sigma=sigma, order=10) check_dep(s1.fitImage, image=im) s2 = galsim.FitShapelet(sigma=sigma, order=10, image=im) np.testing.assert_array_almost_equal(s1.getBVec(), s2.getBVec())
def test_shapelet_fit(): """Test fitting a Shapelet decomposition of an image """ for method, norm in [('no_pixel', 'f'), ('sb', 'sb')]: # We fit a shapelet approximation of a distorted Moffat profile: flux = 20 psf = galsim.Moffat(beta=3.4, half_light_radius=1.2, flux=flux) psf = psf.shear(g1=0.11, g2=0.07).shift(0.03, 0.04) scale = 0.2 pixel = galsim.Pixel(scale) conv = galsim.Convolve([psf, pixel]) im1 = conv.drawImage(scale=scale, method=method) sigma = 1.2 # Match half-light-radius as a decent first approximation. shapelet = galsim.FitShapelet(sigma, 10, im1, normalization=norm) #print('fitted shapelet coefficients = ',shapelet.bvec) # Check flux print('flux = ', shapelet.getFlux(), ' cf. ', flux) np.testing.assert_almost_equal( shapelet.getFlux() / flux, 1., 1, err_msg="Fitted shapelet has the wrong flux") # Test centroid print('centroid = ', shapelet.centroid(), ' cf. ', conv.centroid()) np.testing.assert_almost_equal( shapelet.centroid().x, conv.centroid().x, 2, err_msg="Fitted shapelet has the wrong centroid (x)") np.testing.assert_almost_equal( shapelet.centroid().y, conv.centroid().y, 2, err_msg="Fitted shapelet has the wrong centroid (y)") # Test drawing image from shapelet im2 = im1.copy() shapelet.drawImage(im2, method=method) # Check that images are close to the same: print('norm(diff) = ', np.sum((im1.array - im2.array)**2)) print('norm(im) = ', np.sum(im1.array**2)) print('max(diff) = ', np.max(np.abs(im1.array - im2.array))) print('max(im) = ', np.max(np.abs(im1.array))) peak_scale = np.max(np.abs( im1.array)) * 3 # Check agreement to within 3% of peak value. np.testing.assert_almost_equal( im2.array / peak_scale, im1.array / peak_scale, decimal=2, err_msg="Shapelet version not a good match to original") # Remeasure -- should now be very close to the same. shapelet2 = galsim.FitShapelet(sigma, 10, im2, normalization=norm) np.testing.assert_equal( shapelet.sigma, shapelet2.sigma, err_msg="Second fitted shapelet has the wrong sigma") np.testing.assert_equal( shapelet.order, shapelet2.order, err_msg="Second fitted shapelet has the wrong order") np.testing.assert_almost_equal( shapelet.bvec, shapelet2.bvec, 6, err_msg="Second fitted shapelet coefficients do not match original" ) # Test drawing off center im2 = im1.copy() offset = galsim.PositionD(0.3, 1.4) shapelet.drawImage(im2, method=method, offset=offset) shapelet2 = galsim.FitShapelet(sigma, 10, im2, normalization=norm, center=im2.trueCenter() + offset) np.testing.assert_equal( shapelet.sigma, shapelet2.sigma, err_msg="Second fitted shapelet has the wrong sigma") np.testing.assert_equal( shapelet.order, shapelet2.order, err_msg="Second fitted shapelet has the wrong order") np.testing.assert_almost_equal( shapelet.bvec, shapelet2.bvec, 6, err_msg="Second fitted shapelet coefficients do not match original" )