def read_fits(self): """Read in a DES_Shapelet stored using the the FITS-file version. """ import pyfits cat = pyfits.getdata(self.file_name,1) # These fields each only contain one element, hence the [0]'s. self.psf_order = cat.field('psf_order')[0] self.psf_size = (self.psf_order+1) * (self.psf_order+2) / 2 self.sigma = cat.field('sigma')[0] self.fit_order = cat.field('fit_order')[0] self.fit_size = (self.fit_order+1) * (self.fit_order+2) / 2 self.npca = cat.field('npca')[0] self.bounds = galsim.BoundsD( float(cat.field('xmin')[0]), float(cat.field('xmax')[0]), float(cat.field('ymin')[0]), float(cat.field('ymax')[0])) self.ave_psf = cat.field('ave_psf')[0] assert self.ave_psf.shape == (self.psf_size,) # Note: older pyfits versions don't get the shape right. # For newer pyfits versions the reshape command should be a no op. self.rot_matrix = cat.field('rot_matrix')[0].reshape((self.psf_size,self.npca)).T assert self.rot_matrix.shape == (self.npca, self.psf_size) self.interp_matrix = cat.field('interp_matrix')[0].reshape((self.npca,self.fit_size)).T assert self.interp_matrix.shape == (self.fit_size, self.npca)
def test_dep_bounds(): """Test the deprecated methods in galsim/deprecated/bounds.py """ import time t1 = time.time() bi = galsim.BoundsI(123,345,234,567) bf = galsim.BoundsD(123.,345.,234.,567.) for b in [bi, bf]: check_dep(b.setXMin,101) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 345) np.testing.assert_almost_equal(b.ymin, 234) np.testing.assert_almost_equal(b.ymax, 567) check_dep(b.setXMax,401) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 401) np.testing.assert_almost_equal(b.ymin, 234) np.testing.assert_almost_equal(b.ymax, 567) check_dep(b.setYMin,201) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 401) np.testing.assert_almost_equal(b.ymin, 201) np.testing.assert_almost_equal(b.ymax, 567) check_dep(b.setYMax,501) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 401) np.testing.assert_almost_equal(b.ymin, 201) np.testing.assert_almost_equal(b.ymax, 501) b2 = check_dep(b.addBorder,2) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 401) np.testing.assert_almost_equal(b.ymin, 201) np.testing.assert_almost_equal(b.ymax, 501) np.testing.assert_almost_equal(b2.xmin, 99) np.testing.assert_almost_equal(b2.xmax, 403) np.testing.assert_almost_equal(b2.ymin, 199) np.testing.assert_almost_equal(b2.ymax, 503) t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def test_dep_bounds(): """Test the deprecated methods in galsim/deprecated/bounds.py """ bi = galsim.BoundsI(123, 345, 234, 567) bf = galsim.BoundsD(123., 345., 234., 567.) for b in [bi, bf]: check_dep(b.setXMin, 101) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 345) np.testing.assert_almost_equal(b.ymin, 234) np.testing.assert_almost_equal(b.ymax, 567) check_dep(b.setXMax, 401) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 401) np.testing.assert_almost_equal(b.ymin, 234) np.testing.assert_almost_equal(b.ymax, 567) check_dep(b.setYMin, 201) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 401) np.testing.assert_almost_equal(b.ymin, 201) np.testing.assert_almost_equal(b.ymax, 567) check_dep(b.setYMax, 501) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 401) np.testing.assert_almost_equal(b.ymin, 201) np.testing.assert_almost_equal(b.ymax, 501) b2 = check_dep(b.addBorder, 2) np.testing.assert_almost_equal(b.xmin, 101) np.testing.assert_almost_equal(b.xmax, 401) np.testing.assert_almost_equal(b.ymin, 201) np.testing.assert_almost_equal(b.ymax, 501) np.testing.assert_almost_equal(b2.xmin, 99) np.testing.assert_almost_equal(b2.xmax, 403) np.testing.assert_almost_equal(b2.ymin, 199) np.testing.assert_almost_equal(b2.ymax, 503)
def read_ascii(self): """Read in a DES_Shapelet stored using the the ASCII-file version. """ import numpy fin = open(self.file_name, 'r') lines = fin.readlines() temp = lines[0].split() self.psf_order = int(temp[0]) self.psf_size = (self.psf_order+1) * (self.psf_order+2) / 2 self.sigma = float(temp[1]) self.fit_order = int(temp[2]) self.fit_size = (self.fit_order+1) * (self.fit_order+2) / 2 self.npca = int(temp[3]) temp = lines[1].split() self.bounds = galsim.BoundsD( float(temp[0]), float(temp[1]), float(temp[2]), float(temp[3])) temp = lines[2].split() assert int(temp[0]) == self.psf_size self.ave_psf = numpy.array(temp[2:self.psf_size+2]).astype(float) assert self.ave_psf.shape == (self.psf_size,) temp = lines[3].split() assert int(temp[0]) == self.npca assert int(temp[1]) == self.psf_size self.rot_matrix = numpy.array( [ lines[4+k].split()[1:self.psf_size+1] for k in range(self.npca) ] ).astype(float) assert self.rot_matrix.shape == (self.npca, self.psf_size) temp = lines[5+self.npca].split() assert int(temp[0]) == self.fit_size assert int(temp[1]) == self.npca self.interp_matrix = numpy.array( [ lines[6+self.npca+k].split()[1:self.npca+1] for k in range(self.fit_size) ] ).astype(float) assert self.interp_matrix.shape == (self.fit_size, self.npca)
out_coadd_file = os.path.join(out_dir,os.path.basename(coadd_file)) print 'out_coadd_file = ',out_coadd_file assert out_coadd_file.endswith('.fz') if os.path.isfile(out_coadd_file): os.remove(out_coadd_file) out_coadd_file = out_coadd_file[:-3] if os.path.isfile(out_coadd_file): os.remove(out_coadd_file) new_hdu_list.writeto(out_coadd_file, clobber=True) print 'Wrote output coadd_file ',out_coadd_file # Run fpack on the file subprocess.Popen(['fpack','-D','-Y',out_coadd_file], close_fds=True).communicate() # We will use the bounds of the coadd image below... # GalSim doesn't currently convert easily between BoundsI and BoundsD... coadd_bounds = galsim.BoundsD(coadd_im.bounds.xmin, coadd_im.bounds.xmax, coadd_im.bounds.ymin, coadd_im.bounds.ymax) # I add a slight border to make sure we don't draw things near the border twice. It seems # like that would be more problematic than missing some objects near the border. coadd_bounds = coadd_bounds.addBorder(20.) print 'using coadd_bounds = ',coadd_bounds # Write the truth file out_truth_file = os.path.join(out_dir,'end2end-truth.fits') columns = [ pyfits.Column(name='id', format='J', array=coadd_cat['NUMBER']), pyfits.Column(name='flags', format='J', array=flags), pyfits.Column(name='is_star', format='I', array=is_star), pyfits.Column(name='true_g1', format='D', array=true_g1), pyfits.Column(name='true_g2', format='D', array=true_g2), pyfits.Column(name='true_hlr', format='D', array=true_hlr), pyfits.Column(name='ra', format='D', array=coadd_cat['ALPHAWIN_J2000']), pyfits.Column(name='dec', format='D', array=coadd_cat['DELTAWIN_J2000']),
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 buildImages(self, config, base, file_num, image_num, obj_num, ignore, logger): """Build the images @param config The configuration dict for the output field. @param base The base configuration dict. @param file_num The current file_num. @param image_num The current image_num. @param obj_num The current obj_num. @param ignore A list of parameters that are allowed to be in config that we can ignore here. i.e. it won't be an error if they are present. @param logger If given, a logger object to log progress. @returns a list of the images built """ if 'nobjects' not in config: nobjects = galsim.config.ProcessInputNObjects(base) config['nobjects'] = nobjects req = { 'nobjects': int, 'nchips': int, 'output_style': str, } opt = { 'pointing_ra': galsim.Angle, 'pointing_dec': galsim.Angle, } ignore = ignore + ['reference_wcs', 'reference_image'] kwargs, safe = galsim.config.GetAllParams(config, base, req=req, opt=opt, ignore=ignore) nobjects = kwargs['nobjects'] nchips = kwargs['nchips'] output_style = kwargs['output_style'] base['image']['nobjects'] = nobjects if 'eval_variables' not in base: base['eval_variables'] = {} # Read the reference wcs and image size if 'reference_wcs' not in config: raise ValueError( "reference_wcs is required for FocalPlane output type") if 'reference_image' not in config: raise ValueError( "reference_image is required for FocalPlane output type") if 'file_name' not in config['reference_image']: raise ValueError("file_name is required for reference_image") bounds = galsim.BoundsD() self.focal_wcs = [] self.focal_size = [] w_pos_list = [] for chip_num in range(nchips): base['chip_num'] = chip_num base['eval_variables']['ichip_num'] = chip_num wcs_type = config['reference_wcs']['type'] wcs_builder = galsim.config.wcs.valid_wcs_types[wcs_type] wcs = wcs_builder.buildWCS(config['reference_wcs'], base) self.focal_wcs.append(wcs) file_name = galsim.config.ParseValue(config['reference_image'], 'file_name', base, str)[0] if 'dir' in config['reference_image']: dir = galsim.config.ParseValue(config['reference_image'], 'dir', base, str)[0] file_name = os.path.join(dir, file_name) header = galsim.FitsHeader(file_name) xsize = header['NAXIS1'] ysize = header['NAXIS2'] self.focal_size.append((xsize, ysize)) im_pos1 = galsim.PositionD(0, 0) im_pos2 = galsim.PositionD(0, ysize) im_pos3 = galsim.PositionD(xsize, 0) im_pos4 = galsim.PositionD(xsize, ysize) w_pos_list.append(wcs.toWorld(im_pos1)) w_pos_list.append(wcs.toWorld(im_pos2)) w_pos_list.append(wcs.toWorld(im_pos3)) w_pos_list.append(wcs.toWorld(im_pos4)) for w_pos in w_pos_list: w_pos._set_aux() pointing_x = np.mean([w_pos._x for w_pos in w_pos_list]) pointing_y = np.mean([w_pos._y for w_pos in w_pos_list]) pointing_z = np.mean([w_pos._z for w_pos in w_pos_list]) # TODO: Fix this to work when crossing RA = 0. pointing_minra = np.min([w_pos.ra for w_pos in w_pos_list]) pointing_maxra = np.max([w_pos.ra for w_pos in w_pos_list]) pointing_mindec = np.min([w_pos.dec for w_pos in w_pos_list]) pointing_maxdec = np.max([w_pos.dec for w_pos in w_pos_list]) pointing_ra = np.arctan2(pointing_y, pointing_x) pointing_dec = np.arctan2(pointing_z, np.sqrt(pointing_x**2 + pointing_y**2)) pointing = galsim.CelestialCoord(pointing_ra, pointing_dec) proj_list = [ pointing.project(w_pos, projection='gnomonic') for w_pos in w_pos_list ] for proj in proj_list: bounds += proj base['pointing'] = pointing base['eval_variables']['fpointing_ra'] = pointing_ra base['eval_variables']['fpointing_dec'] = pointing_dec base['eval_variables']['fpointing_minra'] = pointing_minra base['eval_variables']['fpointing_maxra'] = pointing_maxra base['eval_variables']['fpointing_mindec'] = pointing_mindec base['eval_variables']['fpointing_maxdec'] = pointing_maxdec base['eval_variables']['ifirst_image_num'] = base['image_num'] base['eval_variables']['ichip_num'] = '$image_num - first_image_num' rmax = np.max([proj.x**2 + proj.y**2 for proj in proj_list])**0.5 seed_offset = [1 - k * nobjects for k in range(nimages)] base['stamp']['seed_offset'] = { 'index_key': 'image_num', 'type': 'List', 'items': seed_offset } if 'meta_params' in base: for key in base['meta_params']: param = galsim.config.ParseValue(base['meta_params'], key, base, float) base['eval_variables']['f' + key] = param base['eval_variables']['ffocal_xmin'] = bounds.xmin base['eval_variables']['ffocal_xmax'] = bounds.xmax base['eval_variables']['ffocal_ymin'] = bounds.ymin base['eval_variables']['ffocal_ymax'] = bounds.ymax base['eval_variables']['ffocal_r'] = { 'type': 'Eval', 'str': "base['pointing'].project(galsim.CelestialCoord(@gal.ra, @gal.dec))" } base['eval_variables']['ffocal_rmax'] = rmax return galsim.config.BuildImages(nimages, base, image_num, obj_num, logger=logger)
def test_bounds(): """Simple tests of Bounds classes """ bi1 = galsim.BoundsI(11, 23, 17, 50) assert bi1.xmin == bi1.getXMin() == 11 assert bi1.xmax == bi1.getXMax() == 23 assert bi1.ymin == bi1.getYMin() == 17 assert bi1.ymax == bi1.getYMax() == 50 assert isinstance(bi1.xmin, int) assert isinstance(bi1.xmax, int) assert isinstance(bi1.ymin, int) assert isinstance(bi1.ymax, int) bi2 = galsim.BoundsI(galsim.PositionI(11, 17), galsim.PositionI(23, 50)) bi3 = galsim.BoundsI(galsim.PositionD(11., 50.), galsim.PositionD(23., 17.)) bi4 = galsim.BoundsI(galsim.PositionD(11., 17.)) + galsim.BoundsI( galsim.PositionI(23, 50)) bi5 = galsim.BoundsI(galsim.PositionI(11, 17)) + galsim.PositionI(23, 50) bi6 = galsim.PositionI(11, 17) + galsim.BoundsI(galsim.PositionI(23, 50)) bi7 = galsim.BoundsI(bi1) bi8 = bi1 + galsim.BoundsI() bi9 = galsim.BoundsI() + bi1 bi10 = galsim.BoundsI() + galsim.PositionI(11, 17) + galsim.PositionI( 23, 50) bi11 = galsim.BoundsI(galsim.BoundsD(11., 23., 17., 50.)) bi12 = galsim.BoundsI(xmin=11, ymin=17, xmax=23, ymax=50) bi13 = galsim._BoundsI(11, 23, 17, 50) bi14 = galsim.BoundsI() bi14 += galsim.PositionI(11, 17) bi14 += galsim.PositionI(23, 50) for b in [ bi1, bi2, bi3, bi4, bi5, bi6, bi7, bi8, bi9, bi10, bi11, bi12, bi13, bi14 ]: assert b.isDefined() assert b == bi1 assert isinstance(b.xmin, int) assert isinstance(b.xmax, int) assert isinstance(b.ymin, int) assert isinstance(b.ymax, int) assert b.origin == galsim.PositionI(11, 17) assert b.center == galsim.PositionI(17, 34) assert b.true_center == galsim.PositionD(17, 33.5) bd1 = galsim.BoundsD(11., 23., 17., 50.) assert bd1.xmin == bd1.getXMin() == 11. assert bd1.xmax == bd1.getXMax() == 23. assert bd1.ymin == bd1.getYMin() == 17. assert bd1.ymax == bd1.getYMax() == 50. assert isinstance(bd1.xmin, float) assert isinstance(bd1.xmax, float) assert isinstance(bd1.ymin, float) assert isinstance(bd1.ymax, float) bd2 = galsim.BoundsD(galsim.PositionI(11, 17), galsim.PositionI(23, 50)) bd3 = galsim.BoundsD(galsim.PositionD(11., 50.), galsim.PositionD(23., 17.)) bd4 = galsim.BoundsD(galsim.PositionD(11., 17.)) + galsim.BoundsD( galsim.PositionI(23, 50)) bd5 = galsim.BoundsD(galsim.PositionI(11, 17)) + galsim.PositionD(23, 50) bd6 = galsim.PositionD(11, 17) + galsim.BoundsD(galsim.PositionI(23, 50)) bd7 = galsim.BoundsD(bd1) bd8 = bd1 + galsim.BoundsD() bd9 = galsim.BoundsD() + bd1 bd10 = galsim.BoundsD() + galsim.PositionD(11, 17) + galsim.PositionD( 23, 50) bd11 = galsim.BoundsD(galsim.BoundsI(11, 23, 17, 50)) bd12 = galsim.BoundsD(xmin=11.0, ymin=17.0, xmax=23.0, ymax=50.0) bd13 = galsim._BoundsD(11, 23, 17, 50) bd14 = galsim.BoundsD() bd14 += galsim.PositionD(11., 17.) bd14 += galsim.PositionD(23, 50) for b in [ bd1, bd2, bd3, bd4, bd5, bd6, bd7, bd8, bd9, bd10, bd11, bd12, bd13, bd14 ]: assert b.isDefined() assert b == bd1 assert isinstance(b.xmin, float) assert isinstance(b.xmax, float) assert isinstance(b.ymin, float) assert isinstance(b.ymax, float) assert b.origin == galsim.PositionD(11, 17) assert b.center == galsim.PositionD(17, 33.5) assert b.true_center == galsim.PositionD(17, 33.5) assert_raises(TypeError, galsim.BoundsI, 11) assert_raises(TypeError, galsim.BoundsI, 11, 23) assert_raises(TypeError, galsim.BoundsI, 11, 23, 9) assert_raises(TypeError, galsim.BoundsI, 11, 23, 9, 12, 59) assert_raises(TypeError, galsim.BoundsI, xmin=11, xmax=23, ymin=17, ymax=50, z=23) assert_raises(TypeError, galsim.BoundsI, xmin=11, xmax=50) assert_raises(TypeError, galsim.BoundsI, 11, 23.5, 17, 50.9) assert_raises(TypeError, galsim.BoundsI, 11, 23, 9, 12, xmin=19, xmax=2) with assert_raises(TypeError): bi1 += (11, 23) assert_raises(TypeError, galsim.BoundsD, 11) assert_raises(TypeError, galsim.BoundsD, 11, 23) assert_raises(TypeError, galsim.BoundsD, 11, 23, 9) assert_raises(TypeError, galsim.BoundsD, 11, 23, 9, 12, 59) assert_raises(TypeError, galsim.BoundsD, xmin=11, xmax=23, ymin=17, ymax=50, z=23) assert_raises(TypeError, galsim.BoundsD, xmin=11, xmax=50) assert_raises(ValueError, galsim.BoundsD, 11, 23, 17, "blue") assert_raises(TypeError, galsim.BoundsD, 11, 23, 9, 12, xmin=19, xmax=2) with assert_raises(TypeError): bd1 += (11, 23) # Can't use base class directly. assert_raises(TypeError, galsim.Bounds, 11, 23, 9, 12) assert_raises(NotImplementedError, galsim.Bounds) # Check intersection assert bi1 == galsim.BoundsI(0, 100, 0, 100) & bi1 assert bi1 == bi1 & galsim.BoundsI(0, 100, 0, 100) assert bi1 == galsim.BoundsI(0, 23, 0, 50) & galsim.BoundsI( 11, 100, 17, 100) assert bi1 == galsim.BoundsI(0, 23, 17, 100) & galsim.BoundsI( 11, 100, 0, 50) assert not (bi1 & galsim.BoundsI()).isDefined() assert not (galsim.BoundsI() & bi1).isDefined() assert bd1 == galsim.BoundsD(0, 100, 0, 100) & bd1 assert bd1 == bd1 & galsim.BoundsD(0, 100, 0, 100) assert bd1 == galsim.BoundsD(0, 23, 0, 50) & galsim.BoundsD( 11, 100, 17, 100) assert bd1 == galsim.BoundsD(0, 23, 17, 100) & galsim.BoundsD( 11, 100, 0, 50) assert not (bd1 & galsim.BoundsD()).isDefined() assert not (galsim.BoundsD() & bd1).isDefined() with assert_raises(TypeError): bi1 & galsim.PositionI(1, 2) with assert_raises(TypeError): bi1 & galsim.PositionD(1, 2) with assert_raises(TypeError): bd1 & galsim.PositionI(1, 2) with assert_raises(TypeError): bd1 & galsim.PositionD(1, 2) # Check withBorder assert bi1.withBorder(4) == galsim.BoundsI(7, 27, 13, 54) assert bi1.withBorder(0) == galsim.BoundsI(11, 23, 17, 50) assert bi1.withBorder(-1) == galsim.BoundsI(12, 22, 18, 49) assert bd1.withBorder(4.1) == galsim.BoundsD(6.9, 27.1, 12.9, 54.1) assert bd1.withBorder(0) == galsim.BoundsD(11, 23, 17, 50) assert bd1.withBorder(-1) == galsim.BoundsD(12, 22, 18, 49) assert_raises(TypeError, bi1.withBorder, 'blue') assert_raises(TypeError, bi1.withBorder, 4.1) assert_raises(TypeError, bi1.withBorder, '4') assert_raises(TypeError, bi1.withBorder, None) assert_raises(TypeError, bd1.withBorder, 'blue') assert_raises(TypeError, bd1.withBorder, '4.1') assert_raises(TypeError, bd1.withBorder, None) # Check expand assert bi1.expand(2) == galsim.BoundsI(5, 29, 0, 67) assert bi1.expand(1.1) == galsim.BoundsI(10, 24, 15, 52) assert bd1.expand(2) == galsim.BoundsD(5, 29, 0.5, 66.5) np.testing.assert_almost_equal( bd1.expand(1.1)._getinitargs(), (10.4, 23.6, 15.35, 51.65)) # Check shift assert bi1.shift(galsim.PositionI(2, 5)) == galsim.BoundsI(13, 25, 22, 55) assert bd1.shift(galsim.PositionD(2, 5)) == galsim.BoundsD(13, 25, 22, 55) assert bd1.shift(galsim.PositionD(2.3, 5.9)) == galsim.BoundsD( 13.3, 25.3, 22.9, 55.9) assert_raises(TypeError, bi1.shift, galsim.PositionD(2, 5)) assert_raises(TypeError, bd1.shift, galsim.PositionI(2, 5)) # Check area assert bd1.area() == 12 * 33 assert bi1.area() == 13 * 34 assert galsim.BoundsI(galsim.PositionI(11, 23)).area() == 1 assert galsim.BoundsD(galsim.PositionI(11, 23)).area() == 0 # Check includes for b in [bi1, bd1]: assert b.includes(galsim.PositionI(11, 23)) assert b.includes(galsim.BoundsI(14, 18, 30, 38)) assert b.includes(galsim.BoundsD(14.7, 18.1, 30.2, 38.6)) assert b.includes(17, 23) assert b.includes(17.9, 23.9) assert b.includes(galsim.PositionD(11.9, 40.7)) assert b.includes(galsim.PositionI(23, 41)) assert not bd1.includes(galsim.PositionD(10.99, 38)) assert not bd1.includes(galsim.PositionI(11, 51)) assert not bd1.includes(17, 16.99) assert not bd1.includes(galsim.BoundsD(0, 100, 0, 100)) assert not bd1.includes(galsim.BoundsI(14, 29, 20, 30)) assert not bd1.includes(galsim.BoundsD(22, 23.01, 49, 50.01)) assert_raises(TypeError, b.includes, 'blue') assert_raises(TypeError, b.includes) assert_raises(TypeError, b.includes, galsim.PositionI(17, 23), galsim.PositionI(12, 13)) assert_raises(TypeError, b.includes, 2, 3, 4) # Check undefined bounds assert not galsim.BoundsI().isDefined() assert galsim.BoundsI() == galsim.BoundsI() & bi1 assert galsim.BoundsI() == bi1 & galsim.BoundsI() assert galsim.BoundsI() == galsim.BoundsI() & galsim.BoundsI() assert galsim.BoundsI() == galsim.BoundsI() + galsim.BoundsI() assert galsim.BoundsI().area() == 0 assert not galsim.BoundsD().isDefined() assert galsim.BoundsD() == galsim.BoundsD() & bd1 assert galsim.BoundsD() == bd1 & galsim.BoundsD() assert galsim.BoundsD() == galsim.BoundsD() & galsim.BoundsD() assert galsim.BoundsD() == galsim.BoundsD() + galsim.BoundsD() assert galsim.BoundsD().area() == 0 assert galsim.BoundsI(23, 11, 17, 50) == galsim.BoundsI() assert galsim.BoundsI(11, 23, 50, 17) == galsim.BoundsI() assert galsim.BoundsD(23, 11, 17, 50) == galsim.BoundsD() assert galsim.BoundsD(11, 23, 50, 17) == galsim.BoundsD() assert_raises(galsim.GalSimUndefinedBoundsError, getattr, galsim.BoundsI(), 'center') assert_raises(galsim.GalSimUndefinedBoundsError, getattr, galsim.BoundsD(), 'center') assert_raises(galsim.GalSimUndefinedBoundsError, getattr, galsim.BoundsI(), 'true_center') assert_raises(galsim.GalSimUndefinedBoundsError, getattr, galsim.BoundsD(), 'true_center') do_pickle(bi1) do_pickle(bd1) do_pickle(galsim.BoundsI()) do_pickle(galsim.BoundsD())