psf_ground, (galsim.Deconvolve(psf_cosmos)).createSheared(g1=0.03, g2=-0.01) ]) # Then define the convolved cosmos correlated noise model conv_cn = cn.copy() conv_cn.convolveWith(psf_shera) # Then draw the correlation function for this correlated noise as the reference refim = galsim.ImageD(smallim_size, smallim_size) conv_cn.draw(refim, dx=0.18) # Now start the tests... # # First we generate a COSMOS noise field (cosimage), read it into an InterpolatedImage and # then convolve it with psf size_factor = .25 # scale the sizes, need size_factor * largeim_size to be an integer interp = galsim.Linear( tol=1.e-4) # interpolation kernel to use in making convimages # Number of tests nsum_test = 3000 print "Calculating results for size_factor = " + str(size_factor) cosimage = galsim.ImageD( int(size_factor * largeim_size * 6), # Note 6 here since 0.18 = 6 * 0.03 int(size_factor * largeim_size * 6)) # large image to beat down noise print "Unpadded underlying COSMOS noise image bounds = " + str( cosimage.bounds) cosimage_padded = galsim.ImageD( int(size_factor * largeim_size * 6) + 256, # Note 6 here since 0.18 = 6 * 0.03 int(size_factor * largeim_size * 6) + 256) # large image to beat down noise + padding
def test_table2d(): """Check LookupTable2D functionality. """ has_scipy = False try: import scipy from distutils.version import LooseVersion if LooseVersion(scipy.__version__) < LooseVersion('0.11'): raise ImportError except ImportError: print("SciPy tests require SciPy version 0.11 or greater") else: from scipy.interpolate import interp2d has_scipy = True def f(x_, y_): return np.sin(x_) * np.cos(y_) + x_ x = np.linspace(0.1, 3.3, 25) y = np.linspace(0.2, 10.4, 75) yy, xx = np.meshgrid(y, x) # Note the ordering of both input and output here! z = f(xx, yy) tab2d = galsim.LookupTable2D(x, y, z) do_pickle(tab2d) np.testing.assert_array_equal(tab2d.getXArgs(), x) np.testing.assert_array_equal(tab2d.getYArgs(), y) np.testing.assert_array_equal(tab2d.getVals(), z) assert tab2d.interpolant == 'linear' assert tab2d.edge_mode == 'raise' newx = np.linspace(0.2, 3.1, 45) newy = np.linspace(0.3, 10.1, 85) newyy, newxx = np.meshgrid(newy, newx) # Compare different ways of evaluating Table2D ref = tab2d(newxx, newyy) np.testing.assert_array_almost_equal(ref, tab2d(newx, newy, grid=True)) np.testing.assert_array_almost_equal( ref, np.array([[tab2d(x0, y0) for y0 in newy] for x0 in newx])) if has_scipy: scitab2d = interp2d(x, y, np.transpose(z)) np.testing.assert_array_almost_equal( ref, np.transpose(scitab2d(newx, newy))) # Try using linear GSInterp tab2d2 = galsim.LookupTable2D(x, y, z, interpolant=galsim.Linear()) np.testing.assert_array_almost_equal(tab2d(newxx, newyy), tab2d2(newxx, newyy)) np.testing.assert_array_almost_equal(tab2d(newxx.T, newyy.T), tab2d2(newxx.T, newyy.T)) # Try again using Nearest() tab2d2 = galsim.LookupTable2D(x, y, z, interpolant=galsim.Nearest()) tab2d3 = galsim.LookupTable2D(x, y, z, interpolant='nearest') np.testing.assert_array_almost_equal(tab2d2(newxx, newyy), tab2d3(newxx, newyy)) np.testing.assert_array_almost_equal(tab2d2(newxx.T, newyy.T), tab2d3(newxx.T, newyy.T)) # Make sure we exercise the special case in T2DInterpolant2D::interp np.testing.assert_array_almost_equal(tab2d2(0.3, 0.4), tab2d3(0.3, 0.4)) np.testing.assert_array_almost_equal(tab2d2(0.3, 0.4), tab2d3(0.3, 0.4)) # Test non-equally-spaced table. x = np.delete(x, 10) y = np.delete(y, 10) yy, xx = np.meshgrid(y, x) z = f(xx, yy) tab2d = galsim.LookupTable2D(x, y, z) ref = tab2d(newxx, newyy) np.testing.assert_array_almost_equal(ref, tab2d(newx, newy, grid=True)) np.testing.assert_array_almost_equal( ref, np.array([[tab2d(x0, y0) for y0 in newy] for x0 in newx])) if has_scipy: scitab2d = interp2d(x, y, np.transpose(z)) np.testing.assert_array_almost_equal( ref, np.transpose(scitab2d(newx, newy))) # Using a galsim.Interpolant should raise an exception if x/y are not equal spaced. with assert_raises(galsim.GalSimIncompatibleValuesError): tab2d2 = galsim.LookupTable2D(x, y, z, interpolant=galsim.Linear()) # Try a simpler interpolation function. We should be able to interpolate a (bi-)linear function # exactly with a linear interpolant. def f(x_, y_): return 2 * x_ + 3 * y_ z = f(xx, yy) tab2d = galsim.LookupTable2D(x, y, z) np.testing.assert_array_almost_equal(f(newxx, newyy), tab2d(newxx, newyy)) np.testing.assert_array_almost_equal( f(newxx, newyy), np.array([[tab2d(x0, y0) for y0 in newy] for x0 in newx])) # Test edge exception with assert_raises(ValueError): tab2d(1e6, 1e6) with assert_raises(ValueError): tab2d(np.array([1e5, 1e6]), np.array([1e5, 1e6])) with assert_raises(ValueError): tab2d(np.array([1e5, 1e6]), np.array([1e5, 1e6]), grid=True) with assert_raises(ValueError): tab2d.gradient(1e6, 1e6) with assert_raises(ValueError): tab2d.gradient(np.array([1e5, 1e6]), np.array([1e5, 1e6])) with assert_raises(ValueError): tab2d.gradient(np.array([1e5, 1e6]), np.array([1e5, 1e6]), grid=True) with assert_raises(galsim.GalSimError): galsim.utilities.find_out_of_bounds_position( np.array([1, 3]), np.array([2, 4]), galsim.BoundsD(0, 5, 0, 5)) with assert_raises(galsim.GalSimError): galsim.utilities.find_out_of_bounds_position(np.array([1, 3]), np.array([2, 4]), galsim.BoundsD( 0, 5, 0, 5), grid=True) # Check warning mode tab2dw = galsim.LookupTable2D(x, y, z, edge_mode='warn', constant=1) with assert_warns(galsim.GalSimWarning): assert tab2dw(1e6, 1e6) == 1 with assert_warns(galsim.GalSimWarning): np.testing.assert_array_equal( tab2dw(np.array([1e5, 1e6]), np.array([1e5, 1e6])), np.array([1.0, 1.0])) with assert_warns(galsim.GalSimWarning): np.testing.assert_array_equal( tab2dw(np.array([1e5, 1e6]), np.array([1e5, 1e6]), grid=True), np.array([[1.0, 1.0], [1.0, 1.0]])) with assert_warns(galsim.GalSimWarning): assert tab2dw.gradient(1e6, 1e6) == (0.0, 0.0) with assert_warns(galsim.GalSimWarning): np.testing.assert_array_equal( tab2dw.gradient(np.array([1e5, 1e6]), np.array([1e5, 1e6])), np.zeros((2, 2), dtype=float)) with assert_warns(galsim.GalSimWarning): np.testing.assert_array_equal( tab2dw.gradient(np.array([1e5, 1e6]), np.array([1e5, 1e6]), grid=True), np.zeros((2, 2, 2), dtype=float)) # But doesn't warn if in bounds tab2dw(1.0, 1.0) tab2dw(np.array([1.0]), np.array([1.0])) tab2dw(np.array([1.0]), np.array([1.0]), grid=True) tab2dw.gradient(1.0, 1.0) tab2dw.gradient(np.array([1.0]), np.array([1.0])) # Test edge wrapping # Check that can't construct table with edge-wrapping if edges don't match with assert_raises(ValueError): galsim.LookupTable2D(x, y, z, edge_mode='wrap') # Extend edges and make vals match x = np.append(x, x[-1] + (x[-1] - x[-2])) y = np.append(y, y[-1] + (y[-1] - y[-2])) z = np.pad(z, [(0, 1), (0, 1)], mode='wrap') tab2d = galsim.LookupTable2D(x, y, z, edge_mode='wrap') np.testing.assert_array_almost_equal( tab2d(newxx, newyy), tab2d(newxx + 3 * (x[-1] - x[0]), newyy)) np.testing.assert_array_almost_equal( tab2d(newx, newy, grid=True), tab2d(newxx + 3 * (x[-1] - x[0]), newyy)) np.testing.assert_array_almost_equal( tab2d(newxx, newyy), tab2d(newxx, newyy + 13 * (y[-1] - y[0]))) np.testing.assert_array_almost_equal( tab2d(newxx, newyy), tab2d(newx, newy + 13 * (y[-1] - y[0]), grid=True)) # Test edge_mode='constant' tab2d = galsim.LookupTable2D(x, y, z, edge_mode='constant', constant=42) assert type(tab2d(x[0] - 1, y[0] - 1)) in [float, np.float64] assert tab2d(x[0] - 1, y[0] - 1) == 42.0 # One in-bounds, one out-of-bounds np.testing.assert_array_almost_equal( tab2d([x[0], x[0] - 1], [y[0], y[0] - 1]), [tab2d(x[0], y[0]), 42.0]) # Test floor/ceil/nearest interpolant x = np.arange(5) y = np.arange(5) z = x + y[:, np.newaxis] tab2d = galsim.LookupTable2D(x, y, z, interpolant='ceil') assert tab2d(2.4, 3.6) == 3 + 4, "Ceil interpolant failed." tab2d = galsim.LookupTable2D(x, y, z, interpolant='floor') assert tab2d(2.4, 3.6) == 2 + 3, "Floor interpolant failed." tab2d = galsim.LookupTable2D(x, y, z, interpolant='nearest') assert tab2d(2.4, 3.6) == 2 + 4, "Nearest interpolant failed." tab2d = galsim.LookupTable2D(x, y, z, interpolant=galsim.Nearest()) assert tab2d(2.4, 3.6) == 2 + 4, "Nearest interpolant failed." assert_raises(ValueError, galsim.LookupTable2D, x, y, z, interpolant='invalid') assert_raises(ValueError, galsim.LookupTable2D, x, y, z, edge_mode='invalid') assert_raises(ValueError, galsim.LookupTable2D, x, y, z[:-1, :-1]) # Test that x,y arrays need to be strictly increasing. x[0] = x[1] assert_raises(ValueError, galsim.LookupTable2D, x, y, z) x[0] = x[1] + 1 assert_raises(ValueError, galsim.LookupTable2D, x, y, z) x[0] = x[1] - 1 y[0] = y[1] assert_raises(ValueError, galsim.LookupTable2D, x, y, z) y[0] = y[1] + 1 assert_raises(ValueError, galsim.LookupTable2D, x, y, z) # Test that x,y arrays are larger than interpolant x = y = np.arange(2) z = x[:, None] + y assert_raises(ValueError, galsim.LookupTable2D, x, y, z, interpolant='spline', edge_mode='wrap') # Check dfdx input x = y = np.arange(20) z = x[:, None] + y # Not using interpolant='spline' assert_raises(ValueError, galsim.LookupTable2D, x, y, z, dfdx=z) # Only specifying one derivative assert_raises(ValueError, galsim.LookupTable2D, x, y, z, dfdx=z, interpolant='spline') # Derivative is wrong shape assert_raises(ValueError, galsim.LookupTable2D, x, y, z, dfdx=z, dfdy=z, d2fdxdy=z[::2], interpolant='spline') # Check private shortcut instantiation new_tab2d = galsim.table._LookupTable2D(tab2d.x, tab2d.y, tab2d.f, tab2d.interpolant, tab2d.edge_mode, tab2d.constant, tab2d.dfdx, tab2d.dfdy, tab2d.d2fdxdy) assert tab2d == new_tab2d assert tab2d(2.4, 3.6) == new_tab2d(2.4, 3.6)
def test_table(): """Test the spline tabulation of the k space Cubic interpolant. """ for interp in interps: table1 = galsim.LookupTable(x=args1, f=vals1, interpolant=interp) testvals1 = [table1(x) for x in testargs1] assert len(table1) == len(args1) np.testing.assert_array_equal(table1.getArgs(), args1) np.testing.assert_array_equal(table1.getVals(), vals1) assert table1.interpolant == interp assert table1.isLogX() == False assert table1.isLogF() == False # The 4th item is in the args list, so it should be exactly the same as the # corresponding item in the vals list. np.testing.assert_almost_equal( testvals1[3], vals1[3], DECIMAL, err_msg= "Interpolated value for exact arg entry does not match val entry") # Compare the results in testvals1 with the results if we reshape the input array to be # 2-dimensional. np.testing.assert_array_almost_equal( np.array(testvals1).reshape((2, 3)), table1(np.array(testargs1).reshape((2, 3))), DECIMAL, err_msg= "Interpolated values do not match when input array shape changes") if interp != 'nearest': # Do a full regression test based on a version of the code thought to be working. ref1 = np.loadtxt( os.path.join(TESTDIR, 'table_test1_%s.txt' % interp)) np.testing.assert_array_almost_equal( ref1, testvals1, DECIMAL, err_msg= "Interpolated values from LookupTable do not match saved " + "data for evenly-spaced args, with interpolant %s." % interp) # Same thing, but now for args that are not evenly spaced. # (The Table class uses a different algorithm when the arguments are evenly spaced # than when they are not.) table2 = galsim.LookupTable(x=args2, f=vals2, interpolant=interp) testvals2 = [table2(x) for x in testargs2] np.testing.assert_almost_equal( testvals2[3], vals2[3], DECIMAL, err_msg= "Interpolated value for exact arg entry does not match val entry" ) ref2 = np.loadtxt( os.path.join(TESTDIR, 'table_test2_%s.txt' % interp)) np.testing.assert_array_almost_equal( ref2, testvals2, DECIMAL, err_msg= "Interpolated values from LookupTable do not match saved " + "data for non-evenly-spaced args, with interpolant %s." % interp) # Check that out of bounds arguments, or ones with some crazy shape, raise an exception: assert_raises(ValueError, table1, args1[0] - 0.01) assert_raises(ValueError, table1, args1[-1] + 0.01) assert_raises(ValueError, table2, args2[0] - 0.01) assert_raises(ValueError, table2, args2[-1] + 0.01) # These shouldn't raise any exception: table1(args1[0] + 0.01) table1(args1[-1] - 0.01) table2(args2[0] + 0.01) table2(args2[-1] - 0.01) table1(np.zeros((3, 3)) + args1[0] + 0.01) table1(np.zeros(3) + args1[0] + 0.01) table1((args1[0] + 0.01, args1[0] + 0.01)) table1([args1[0] + 0.01, args1[0] + 0.01]) # Check 2d arrays (square) table1(np.zeros((3, 3)) + args1[0]) # Check 2d arrays (non-square) table1(np.array(testargs1).reshape((2, 3))) # Check picklability do_pickle( table1, lambda x: (tuple(x.getArgs()), tuple(x.getVals()), x.getInterp())) do_pickle( table2, lambda x: (tuple(x.getArgs()), tuple(x.getVals()), x.getInterp())) do_pickle(table1) do_pickle(table2) assert_raises(ValueError, galsim.LookupTable, x=args1, f=vals1, interpolant='invalid') assert_raises(ValueError, galsim.LookupTable, x=[1], f=[1], interpolant='linear') assert_raises(ValueError, galsim.LookupTable, x=[1, 2], f=[1, 2], interpolant='spline') assert_raises(ValueError, galsim.LookupTable, x=[1, 1, 1], f=[1, 2, 3]) assert_raises(ValueError, galsim.LookupTable, x=[0, 1, 2], f=[1, 2, 3], x_log=True) assert_raises(ValueError, galsim.LookupTable, x=[-1, 0, 1], f=[1, 2, 3], x_log=True) assert_raises(ValueError, galsim.LookupTable, x=[0, 1, 2], f=[0, 1, 2], f_log=True) assert_raises(ValueError, galsim.LookupTable, x=[0, 1, 2], f=[2, -1, 2], f_log=True) assert_raises(ValueError, galsim.LookupTable, x=args2, f=vals2, interpolant=galsim.Linear()) assert_raises(ValueError, galsim.LookupTable, x=args1, f=vals1, interpolant=galsim.Linear(), x_log=True)
def test_log(): """Some simple tests of interpolation using logs.""" # Set up some test vectors that are strictly positive, and others that are negative. x = 0.01 * np.arange(1000) + 0.01 y = 1. * x x_neg = -1. * x y_neg = 1. * x_neg # Check that interpolation agrees for the positive ones when using log interpolation (for some # reasonable tolerance). tab_1 = galsim.LookupTable(x=x, f=y) tab_2 = galsim.LookupTable(x=x, f=y, x_log=True, f_log=True) tab_3 = galsim.LookupTable(x=x, f=y, x_log=True) tab_4 = galsim.LookupTable(x=x, f=y, f_log=True) test_x_vals = [2.641, 3.985, 8.123125] for test_val in test_x_vals: result_1 = tab_1(test_val) result_2 = tab_2(test_val) result_3 = tab_3(test_val) result_4 = tab_4(test_val) print(result_1, result_2, result_3, result_4) np.testing.assert_almost_equal( result_2, result_1, decimal=3, err_msg='Disagreement when interpolating in log(f) and log(x)') np.testing.assert_almost_equal( result_3, result_1, decimal=3, err_msg='Disagreement when interpolating in log(x)') np.testing.assert_almost_equal( result_4, result_1, decimal=3, err_msg='Disagreement when interpolating in log(f)') with assert_raises(galsim.GalSimRangeError): tab_2(-1) with assert_raises(galsim.GalSimRangeError): tab_3(-1) with assert_raises(galsim.GalSimRangeError): tab_2(x_neg) with assert_raises(galsim.GalSimRangeError): tab_3(x_neg) # Check picklability do_pickle(tab_1) do_pickle(tab_2) do_pickle(tab_3) do_pickle(tab_4) # Check storage of args and vals for log vs. linear, which should be the same to high precision. np.testing.assert_array_almost_equal( tab_1.getArgs(), tab_3.getArgs(), decimal=12, err_msg='Args differ for linear vs. log storage') np.testing.assert_array_almost_equal( tab_1.getVals(), tab_4.getVals(), decimal=12, err_msg='Vals differ for linear vs. log storage') # Check other properties assert not tab_1.x_log assert not tab_1.f_log assert tab_2.x_log assert tab_2.f_log assert tab_3.x_log assert not tab_3.f_log assert not tab_1.isLogX() assert not tab_1.isLogF() assert tab_2.isLogX() assert tab_2.isLogF() assert tab_3.isLogX() assert not tab_3.isLogF() # Check that an appropriate exception is thrown when trying to do interpolation using negative # ones. assert_raises(ValueError, galsim.LookupTable, x=x_neg, f=y_neg, x_log=True) assert_raises(ValueError, galsim.LookupTable, x=x_neg, f=y_neg, f_log=True) assert_raises(ValueError, galsim.LookupTable, x=x_neg, f=y_neg, x_log=True, f_log=True) # Check that doing log transform explicitly matches expected behavior of x_log and f_log. expx = np.exp(x) expy = np.exp(y) for interpolant in ['linear', 'spline', galsim.Quintic()]: tab_1 = galsim.LookupTable(x, y, interpolant=interpolant) tab_2 = galsim.LookupTable(expx, y, x_log=True, interpolant=interpolant) tab_3 = galsim.LookupTable(x, expy, f_log=True, interpolant=interpolant) tab_4 = galsim.LookupTable(expx, expy, x_log=True, f_log=True, interpolant=interpolant) for test_val in test_x_vals: result_1 = tab_1(test_val) result_2 = tab_2(np.exp(test_val)) result_3 = np.log(tab_3(test_val)) result_4 = np.log(tab_4(np.exp(test_val))) np.testing.assert_almost_equal( result_2, result_1, decimal=10, err_msg='Disagreement when interpolating in log(x)') np.testing.assert_almost_equal( result_3, result_1, decimal=10, err_msg='Disagreement when interpolating in log(f)') np.testing.assert_almost_equal( result_4, result_1, decimal=10, err_msg='Disagreement when interpolating in log(f) and log(x)') # Verify for exception when using x_log with non-equal-spaced galsim.Interpolant galsim.LookupTable(x, y, x_log=True, interpolant='linear') # works fine assert_raises(ValueError, galsim.LookupTable, x, y, x_log=True, interpolant=galsim.Linear())
The parameters of the Sersic images come from a COSMOS best-fitting Sersic model catalog. """ import cPickle import numpy as np import galsim import test_interpolants SERSIC_IMAGE_SIZE = 512 # For initial image of the Sersic at Hubble resolution, make nice and large TEST_IMAGE_SIZE = SERSIC_IMAGE_SIZE # For speed could make this smaller # Dictionary for parsing the test_interpolants.interpolant_list into galsim Interpolants INTERPOLANT_DICT = { "nearest": galsim.Nearest(), "sinc": galsim.SincInterpolant(), "linear": galsim.Linear(), "cubic": galsim.Cubic(), "quintic": galsim.Quintic(), "lanczos3": galsim.Lanczos(3), "lanczos4": galsim.Lanczos(4), "lanczos5": galsim.Lanczos(5), "lanczos7": galsim.Lanczos(7) } # Output filenames DELTA_FILENAME = 'interpolant_test_parametric_output_delta.dat' ORIGINAL_FILENAME = 'interpolant_test_parametric_output_original.dat' NITEMS = 30 # For now, look at a few only LAM_OVER_DIAM_COSMOS = 814.e-9 / 2.4 # All the original images in Melanie's tests were from COSMOS
def getCOSMOSNoise(rng, file_name, dx_cosmos=0.03, variance=0., x_interpolant=None): """Returns a representation of correlated noise in the HST COSMOS F814W unrotated science coadd images. See http://cosmos.astro.caltech.edu/astronomer/hst.html for information about the COSMOS survey, and Leauthaud et al (2007) for detailed information about the unrotated F814W coadds used for weak lensing science. This function uses a stacked estimate of the correlation function in COSMOS noise fields, the location of which should be input to this function via the `file_name` argument. This image is stored in FITS format, and is generated as described in `YOUR/REPO/PATH/GalSim/devel/external/hst/make_cosmos_cfimage.py`. The image itself can also be found within the GalSim repo, located at: /YOUR/REPO/PATH/GalSim/examples/data/acs_I_unrot_sci_20_cf.fits @param rng Must be a galsim.BaseDeviate or derived class instance, provides the random number generator used by the returned _BaseCorrelatedNoise instance. @param file_name String containing the path and filename above but modified to match the location of the GalSim repository on your system. @param dx_cosmos COSMOS ACS F814W coadd image pixel scale in the units you are using to describe GSObjects and image scales in GalSim: defaults to 0.03 arcsec, see below for more information. @param variance Scales the correlation function so that its point variance, equivalent to its value at zero separation distance, matches this value. The default `variance = 0.` uses the variance in the original COSMOS noise fields. @param x_interpolant Forces use of a non-default interpolant for interpolation of the internal lookup table in real space. Supplied kwarg must be an InterpolantXY instance or an Interpolant instance (from which an InterpolantXY will be automatically generated). Defaults to use of the bilinear interpolant `galsim.InterpolantXY(galsim.Linear(tol=1.e-4))`, see below. @return A _BaseCorrelatedNoise instance representing correlated noise in F814W COSMOS images. The interpolation used if `x_interpolant=None` (default) is a galsim.InterpolantXY(galsim.Linear(tol=1.e-4)), which uses bilinear interpolation. Initial tests indicate the favourable performance of this interpolant in applications involving correlated noise. Important note regarding units ------------------------------ The ACS coadd images in COSMOS have a pixel scale of 0.03 arcsec, and so the pixel scale `dx_cosmos` adopted in the representation of of the correlation function takes a default value dx_cosmos = 0.03 If you wish to use other units, ensure that the input keyword `dx_cosmos` takes the value corresponding to 0.03 arcsec in your chosen system. Example usage ------------- The following commands use this function to generate a 300 pixel x 300 pixel image of noise with HST COSMOS correlation properties (substitute in your own file and path for the `filestring`). >>> filestring='/YOUR/REPO/PATH/GalSim/devel/external/hst/acs_I_unrot_sci_20_cf.fits' >>> import galsim >>> rng = galsim.UniformDeviate(123456) >>> cf = galsim.correlatednoise.getCOSMOSNoise(rng, filestring) >>> im = galsim.ImageD(300, 300) >>> im.setScale(0.03) >>> cf.applyTo(im) >>> im.write('out.fits') The FITS file `out.fits` should then contain an image of randomly-generated, COSMOS-like noise. """ # First try to read in the image of the COSMOS correlation function stored in the repository import os if not os.path.isfile(file_name): raise IOError("The input file_name '"+str(file_name)+"' does not exist.") try: cfimage = galsim.fits.read(file_name) except Exception: # Give a vaguely helpful warning, then raise the original exception for extra diagnostics import warnings warnings.warn( "Function getCOSMOSNoise() unable to read FITS image from "+str(file_name)+", "+ "more information on the error in the following Exception...") raise # Then check for negative variance before doing anything time consuming if variance < 0: raise ValueError("Input keyword variance must be zero or positive.") # If x_interpolant not specified on input, use bilinear if x_interpolant == None: linear = galsim.Linear(tol=1.e-4) x_interpolant = galsim.InterpolantXY(linear) else: x_interpolant = utilities.convert_interpolant_to_2d(x_interpolant) # Use this info to then generate a correlated noise model DIRECTLY: note this is non-standard # usage, but tolerated since we can be sure that the input cfimage is appropriately symmetric # and peaked at the origin ret = _BaseCorrelatedNoise(rng, galsim.InterpolatedImage( cfimage, dx=dx_cosmos, normalization="sb", calculate_stepk=False, calculate_maxk=False, x_interpolant=x_interpolant)) # If the input keyword variance is non-zero, scale the correlation function to have this # variance if variance > 0.: ret.setVariance(variance) return ret
def __init__(self, rng, image, dx=0., x_interpolant=None, correct_periodicity=True, subtract_mean=False): # Check that the input image is in fact a galsim.ImageSIFD class instance if not isinstance(image, ( galsim.BaseImageD, galsim.BaseImageF, galsim.BaseImageS, galsim.BaseImageI)): raise TypeError( "Input image not a galsim.Image class object (e.g. ImageD, ImageViewS etc.)") # Build a noise correlation function (CF) from the input image, using DFTs # Calculate the power spectrum then a (preliminary) CF ft_array = np.fft.fft2(image.array) ps_array = (ft_array * ft_array.conj()).real if subtract_mean: # Quickest non-destructive way to make the PS correspond to the # mean-subtracted case ps_array[0, 0] = 0. # Note need to normalize due to one-directional 1/N^2 in FFT conventions cf_array_prelim = (np.fft.ifft2(ps_array)).real / np.product(image.array.shape) store_rootps = True # Currently the ps_array above corresponds to cf, but this may change... # Apply a correction for the DFT assumption of periodicity unless user requests otherwise if correct_periodicity: cf_array_prelim *= _cf_periodicity_dilution_correction(cf_array_prelim.shape) store_rootps = False # Roll CF array to put the centre in image centre. Remember that numpy stores data [y,x] cf_array_prelim = utilities.roll2d( cf_array_prelim, (cf_array_prelim.shape[0] / 2, cf_array_prelim.shape[1] / 2)) # The underlying C++ object is expecting the CF to be represented by an odd-dimensioned # array with the central pixel denoting the zero-distance correlation (variance), even # even if the input image was even-dimensioned on one or both sides. # We therefore copy-paste and zero pad the CF calculated above to ensure that these # expectations are met. # # Determine the largest dimension of the input image, and use it to generate an empty CF # array for final output, padding by one to make odd if necessary: cf_array = np.zeros(( 1 + 2 * (cf_array_prelim.shape[0] / 2), 1 + 2 * (cf_array_prelim.shape[1] / 2))) # using integer division # Then put the data from the prelim CF into this array cf_array[0:cf_array_prelim.shape[0], 0:cf_array_prelim.shape[1]] = cf_array_prelim # Then copy-invert-paste data from the leftmost column to the rightmost column, and lowest # row to the uppermost row, if the original CF had even dimensions in the x and y # directions, respectively (remembering again that NumPy stores data [y,x] in arrays) if cf_array_prelim.shape[1] % 2 == 0: # first do x lhs_column = cf_array[:, 0] cf_array[:, cf_array_prelim.shape[1]] = lhs_column[::-1] # inverts order as required if cf_array_prelim.shape[0] % 2 == 0: # then do y bottom_row = cf_array[0, :] cf_array[cf_array_prelim.shape[0], :] = bottom_row[::-1] # inverts order as required # Wrap correlation function in an image cf_image = galsim.ImageViewD(np.ascontiguousarray(cf_array)) # Correctly record the original image scale if set if dx > 0.: cf_image.setScale(dx) elif image.getScale() > 0.: cf_image.setScale(image.getScale()) else: # sometimes Images are instantiated with scale=0, in which case we will assume unit # pixel scale cf_image.setScale(1.) # If x_interpolant not specified on input, use bilinear if x_interpolant == None: linear = galsim.Linear(tol=1.e-4) x_interpolant = galsim.InterpolantXY(linear) else: x_interpolant = utilities.convert_interpolant_to_2d(x_interpolant) # Then initialize... cf_object = galsim.InterpolatedImage( cf_image, x_interpolant=x_interpolant, dx=cf_image.getScale(), normalization="sb", calculate_stepk=False, calculate_maxk=False) # these internal calculations do not seem # to do very well with often sharp-peaked # correlation function images... _BaseCorrelatedNoise.__init__(self, rng, cf_object) if store_rootps: # If it corresponds to the CF above, store useful data as a (rootps, dx) tuple for # efficient later use: self._profile_for_stored = self._profile self._rootps_store.append((np.sqrt(ps_array), cf_image.getScale()))
def test_interpolated_image(): """Test various ways to build an InterpolatedImage """ imgdir = 'SBProfile_comparison_images' file_name = os.path.join(imgdir,'gauss_smallshear.fits') imgdir2 = 'fits_files' file_name2 = os.path.join(imgdir2,'interpim_hdu_test.fits') config = { 'gal1' : { 'type' : 'InterpolatedImage', 'image' : file_name }, 'gal2' : { 'type' : 'InterpolatedImage', 'image' : file_name, 'x_interpolant' : 'linear' }, 'gal3' : { 'type' : 'InterpolatedImage', 'image' : file_name, 'x_interpolant' : 'cubic', 'normalization' : 'sb', 'flux' : 1.e4 }, 'gal4' : { 'type' : 'InterpolatedImage', 'image' : file_name, 'x_interpolant' : 'lanczos5', 'scale' : 0.7, 'flux' : 1.e5 }, 'gal5' : { 'type' : 'InterpolatedImage', 'image' : file_name, 'noise_pad' : 0.001 }, 'gal6' : { 'type' : 'InterpolatedImage', 'image' : file_name, 'noise_pad' : 'fits_files/blankimg.fits' }, 'gal7' : { 'type' : 'InterpolatedImage', 'image' : file_name, 'pad_image' : 'fits_files/blankimg.fits' }, 'galmulti' : { 'type' : 'InterpolatedImage', 'image' : file_name2, 'hdu' : 2 } } rng = galsim.UniformDeviate(1234) config['rng'] = galsim.UniformDeviate(1234) # A second copy starting with the same seed. gal1a = galsim.config.BuildGSObject(config, 'gal1')[0] im = galsim.fits.read(file_name) gal1b = galsim.InterpolatedImage(im) gsobject_compare(gal1a, gal1b) gal2a = galsim.config.BuildGSObject(config, 'gal2')[0] gal2b = galsim.InterpolatedImage(im, x_interpolant=galsim.Linear()) gsobject_compare(gal2a, gal2b) gal3a = galsim.config.BuildGSObject(config, 'gal3')[0] gal3b = galsim.InterpolatedImage(im, x_interpolant=galsim.Cubic(), normalization='sb', flux=1.e4) gsobject_compare(gal3a, gal3b) gal4a = galsim.config.BuildGSObject(config, 'gal4')[0] interp = galsim.Lanczos(n=5, conserve_dc=True) gal4b = galsim.InterpolatedImage(im, x_interpolant=interp, scale=0.7, flux=1.e5) gsobject_compare(gal4a, gal4b) gal5a = galsim.config.BuildGSObject(config, 'gal5')[0] gal5b = galsim.InterpolatedImage(im, rng=rng, noise_pad=0.001) gsobject_compare(gal5a, gal5b) gal6a = galsim.config.BuildGSObject(config, 'gal6')[0] gal6b = galsim.InterpolatedImage(im, rng=rng, noise_pad='fits_files/blankimg.fits') gsobject_compare(gal6a, gal6b) gal7a = galsim.config.BuildGSObject(config, 'gal7')[0] gal7b = galsim.InterpolatedImage(im, pad_image = 'fits_files/blankimg.fits') gsobject_compare(gal7a, gal7b) # Now test the reading from some particular HDU galmulti = galsim.config.BuildGSObject(config, 'galmulti')[0] im = galmulti.drawImage(scale=0.2, method='no_pixel') test_g2 = im.FindAdaptiveMom().observed_shape.g2 np.testing.assert_almost_equal( test_g2, 0.7, decimal=3, err_msg='Did not get right shape image after reading InterpolatedImage from HDU')