def test_ne(): """ Check that inequality works as expected.""" # These should all compare as unequal. x = [1, 2, 3] f = [4, 5, 6] x2 = [1.1, 2.2, 3.3] f2 = [4.4, 5.5, 6.6] lts = [ galsim.LookupTable(x, f), galsim.LookupTable(x, f2), galsim.LookupTable(x2, f), galsim.LookupTable(x, f, interpolant='floor'), galsim.LookupTable(x, f, x_log=True), galsim.LookupTable(x, f, f_log=True) ] ff = f + np.array(f)[:, None] ff2 = f2 + np.array(f2)[:, None] lts.extend([ galsim.LookupTable2D(x, x, ff), galsim.LookupTable2D(x2, x, ff), galsim.LookupTable2D(x, x2, ff), galsim.LookupTable2D(x, x2, ff2), galsim.LookupTable2D(x, x, ff, interpolant='nearest'), galsim.LookupTable2D(x, x, ff, interpolant=galsim.Nearest()), galsim.LookupTable2D(x, x, ff, edge_mode='wrap'), galsim.LookupTable2D(x, x, ff, constant=1), galsim.LookupTable2D(x, x, ff, interpolant='spline'), galsim.LookupTable2D(x, x, ff, interpolant='spline', dfdx=ff, dfdy=ff, d2fdxdy=ff) ]) all_obj_diff(lts)
A simple script used to quickly generate plots for the discussion of which interpolant should be used to describe correlation functions. See the discussion at https://github.com/GalSim-developers/GalSim/pull/452#discussion-diff-5701561 """ import numpy as np import matplotlib.pyplot as plt import galsim CFSIZE=9 UPSAMPLING = 3 rng = galsim.BaseDeviate(752424) gd = galsim.GaussianNoise(rng) noise_image = galsim.ImageD(CFSIZE, CFSIZE) noise_image.addNoise(gd) cn = galsim.CorrelatedNoise(rng, noise_image, x_interpolant=galsim.Nearest(tol=1.e-4), dx=1.) test_image = galsim.ImageD( 2 * UPSAMPLING * CFSIZE, 2 * UPSAMPLING * CFSIZE, scale=1. / float(UPSAMPLING)) cn.applyRotation(30. * galsim.degrees) cn.draw(test_image) plt.clf() plt.pcolor(test_image.array) plt.colorbar() plt.savefig('cf_nearest.png') plt.show()
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)
use Sersic models drawn into `InterpolatedImage` instances to try and get to the nitty-gritty of the issues with interpolators. 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
https://github.com/GalSim-developers/GalSim/pull/452#discussion-diff-5701561 """ import numpy as np import matplotlib.pyplot as plt import galsim CFSIZE = 9 UPSAMPLING = 3 rng = galsim.BaseDeviate(752424) gd = galsim.GaussianNoise(rng) noise_image = galsim.ImageD(CFSIZE, CFSIZE) noise_image.addNoise(gd) cn = galsim.CorrelatedNoise(rng, noise_image, x_interpolant=galsim.Nearest(tol=1.e-4), dx=1.) test_image = galsim.ImageD(2 * UPSAMPLING * CFSIZE, 2 * UPSAMPLING * CFSIZE, scale=1. / float(UPSAMPLING)) cn.applyRotation(30. * galsim.degrees) cn.draw(test_image) plt.clf() plt.pcolor(test_image.array) plt.colorbar() plt.savefig('cf_nearest.png') plt.show()