Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
g1 = 0.23
g2 = -0.17
psf = galsim.Gaussian(sigma=sigma).shear(g1=g1, g2=g2)
for x,y in zip(x_list, y_list):
bounds = galsim.BoundsI(int(x-31), int(x+32), int(y-31), int(y+32))
psf.drawImage(image[bounds], center=galsim.PositionD(x,y), method='no_pixel')
image.addNoise(galsim.GaussianNoise(rng=galsim.BaseDeviate(1234), sigma=1e-6))
image_file = os.path.join('output','test_load_images_im.fits')
image.write(image_file)
dtype = [ ('x','f8'), ('y','f8') ]
data = np.empty(len(x_list), dtype=dtype)
data['x'] = x_list
data['y'] = y_list
cat_file = os.path.join('output','test_load_images_cat.fits')
fitsio.write(cat_file, data, clobber=True)
# Make star data
config = { 'image_file_name' : image_file,
'cat_file_name': cat_file }
orig_stars, wcs, pointing = piff.Input.process(config, logger)
# Fit these with a simple Mean, Gaussian
model = piff.Gaussian()
interp = piff.Mean()
psf = piff.SimplePSF(model, interp)
psf.fit(orig_stars, wcs, pointing, logger=logger)
psf_file = os.path.join('output','test_load_images_psf.fits')
psf.write(psf_file, logger)
# Read this file back in. It has the star data, but the images are blank.
psf2 = piff.read(psf_file, logger)
def test_pupil_im(pupil_plane_im='input/DECam_pupil_128.fits'):
import galsim
# make sure we can load up a pupil image
model = piff.Optical(diam=4.274419, lam=500., r0=0.1, pupil_plane_im=pupil_plane_im)
test_optical(model)
# make sure we really loaded it
pupil_plane_im = galsim.Image(fitsio.read(pupil_plane_im))
model_pupil_plane_im = model.optical_psf_kwargs['pupil_plane_im']
np.testing.assert_array_equal(pupil_plane_im.array, model_pupil_plane_im.array)
# test passing a different optical template that includes diam
piff.optical_model.optical_templates['test'] = {'diam': 2, 'lam':500, 'r0':0.1}
model = piff.Optical(pupil_plane_im=pupil_plane_im, template='test')
model_pupil_plane_im = model.optical_psf_kwargs['pupil_plane_im']
np.testing.assert_array_equal(pupil_plane_im.array, model_pupil_plane_im.array)
print('true_xi = ',true_xi)
print('nk.xi = ',nk.xi)
np.testing.assert_allclose(nk.xi, true_xi, rtol=1.e-4, atol=1.e-8)
try:
import fitsio
except ImportError:
print('Skipping FITS tests, since fitsio is not installed')
return
# Check that running via the corr2 script works correctly.
config = treecorr.config.read_config('configs/nk_direct.yaml')
cat1.write(config['file_name'])
cat2.write(config['file_name2'])
treecorr.corr2(config)
data = fitsio.read(config['nk_file_name'])
np.testing.assert_allclose(data['r_nom'], nk.rnom)
np.testing.assert_allclose(data['npairs'], nk.npairs)
np.testing.assert_allclose(data['weight'], nk.weight)
np.testing.assert_allclose(data['kappa'], nk.xi, rtol=1.e-3)
# Invalid with only one file_name
del config['file_name2']
with assert_raises(TypeError):
treecorr.corr2(config)
config['file_name2'] = 'data/nk_direct_cat2.fits'
# Invalid to request compoensated if no rand_file
config['nk_statistic'] = 'compensated'
with assert_raises(TypeError):
treecorr.corr2(config)
# Repeat with binslop = 0, since the code flow is different from brute=True
print('kg.xi = ',kg.xi)
np.testing.assert_allclose(kg.xi, true_xi.real, rtol=1.e-4, atol=1.e-8)
np.testing.assert_allclose(kg.xi_im, true_xi.imag, rtol=1.e-4, atol=1.e-8)
try:
import fitsio
except ImportError:
print('Skipping FITS tests, since fitsio is not installed')
return
# Check that running via the corr2 script works correctly.
config = treecorr.config.read_config('configs/kg_direct_spherical.yaml')
cat1.write(config['file_name'])
cat2.write(config['file_name2'])
treecorr.corr2(config)
data = fitsio.read(config['kg_file_name'])
np.testing.assert_allclose(data['r_nom'], kg.rnom)
np.testing.assert_allclose(data['npairs'], kg.npairs)
np.testing.assert_allclose(data['weight'], kg.weight)
np.testing.assert_allclose(data['kgamT'], kg.xi, rtol=1.e-3)
np.testing.assert_allclose(data['kgamX'], kg.xi_im, rtol=1.e-3)
# Repeat with binslop = 0
# And don't do any top-level recursion so we actually test not going to the leaves.
kg = treecorr.KGCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins,
sep_units='deg', bin_slop=0, max_top=0)
kg.process(cat1, cat2)
np.testing.assert_array_equal(kg.npairs, true_npairs)
np.testing.assert_allclose(kg.weight, true_weight, rtol=1.e-5, atol=1.e-8)
np.testing.assert_allclose(kg.xi, true_xi.real, rtol=1.e-3, atol=1.e-3)
np.testing.assert_allclose(kg.xi_im, true_xi.imag, rtol=1.e-3, atol=1.e-3)
'kernel' : kernel,
'white_noise':1e-5,
'optimize' : optimize
}
}
logger = piff.config.setup_logger()
interp3 = piff.Interp.process(config['interp'], logger)
iterate(stars, interp3)
validate(validate_stars, interp3)
# Check that we can write interp to disk and read back in.
if file_name is not None:
testfile = os.path.join('output', file_name)
with fitsio.FITS(testfile, 'rw', clobber=True) as f:
interp.write(f, 'interp')
with fitsio.FITS(testfile, 'r') as f:
interp2 = piff.GPGeorgeInterp.read(f, 'interp')
print("Revalidating after i/o.")
X = np.vstack([training_data['u'], training_data['v']]).T
for i in range(interp.nparams):
print('A')
np.testing.assert_allclose(interp.gps[i].get_matrix(X), interp2.gps[i].get_matrix(X))
print('B')
np.testing.assert_allclose(interp._init_theta[i], interp2._init_theta[i])
print('C')
np.testing.assert_allclose(interp.gps[i].get_parameter_vector(), interp2.gps[i].get_parameter_vector())
#print('D')
#print(interp.gps[i]._alpha)
#print(interp2.gps[i]._alpha)
#np.testing.assert_allclose(interp.gps[i]._alpha, interp2.gps[i]._alpha, rtol=1e-6, atol=1.e-7)
print('E')
np.testing.assert_allclose(interp.gps[i]._x, interp2.gps[i]._x)
'interp' : {
'type' : 'GPGeorgeInterp',
'kernel' : kernel,
'white_noise':1e-5,
'optimize' : optimize
}
}
logger = piff.config.setup_logger()
interp3 = piff.Interp.process(config['interp'], logger)
iterate(stars, interp3)
validate(validate_stars, interp3)
# Check that we can write interp to disk and read back in.
if file_name is not None:
testfile = os.path.join('output', file_name)
with fitsio.FITS(testfile, 'rw', clobber=True) as f:
interp.write(f, 'interp')
with fitsio.FITS(testfile, 'r') as f:
interp2 = piff.GPGeorgeInterp.read(f, 'interp')
print("Revalidating after i/o.")
X = np.vstack([training_data['u'], training_data['v']]).T
for i in range(interp.nparams):
print('A')
np.testing.assert_allclose(interp.gps[i].get_matrix(X), interp2.gps[i].get_matrix(X))
print('B')
np.testing.assert_allclose(interp._init_theta[i], interp2._init_theta[i])
print('C')
np.testing.assert_allclose(interp.gps[i].get_parameter_vector(), interp2.gps[i].get_parameter_vector())
#print('D')
#print(interp.gps[i]._alpha)
#print(interp2.gps[i]._alpha)
#np.testing.assert_allclose(interp.gps[i]._alpha, interp2.gps[i]._alpha, rtol=1e-6, atol=1.e-7)
def test_disk():
# save and load
r0 = 0.1
sigma = 1.2
g1 = -0.1
g2 = 0.05
gsparams = galsim.GSParams(minimum_fft_size=32)
model = piff.Optical(r0=r0, sigma=sigma, g1=g1, g2=g2, lam=700.0, template='des',
pad_factor=0.5, oversampling=0.5, gsparams=gsparams)
model_file = os.path.join('output','optics.fits')
with fitsio.FITS(model_file, 'rw', clobber=True) as f:
model.write(f, 'optics')
model2 = piff.Optical.read(f, 'optics')
for key in model.kwargs:
assert key in model2.kwargs, 'key %r missing from model2 kwargs'%key
assert model.kwargs[key] == model2.kwargs[key], 'key %r mismatch'%key
for key in model.optical_psf_kwargs:
assert key in model2.optical_psf_kwargs, 'key %r missing from model2 optical_psf_kwargs'%key
assert model.optical_psf_kwargs[key] == model2.optical_psf_kwargs[key], 'key %r mismatch'%key
for key in model.kolmogorov_kwargs:
assert key in model2.kolmogorov_kwargs, 'key %r missing from model2 kolmogorov_kwargs'%key
assert model.kolmogorov_kwargs[key] == model2.kolmogorov_kwargs[key], 'key %r mismatch'%key
assert model.sigma == model2.sigma,'sigma mismatch'
assert model.g1 == model2.g1,'g1 mismatch'
assert model.g2 == model2.g2,'g2 mismatch'
targetwcs.add_to_header(hdr)
newpsf = GaussianMixturePSF(psf.mog.amp, newmean, newvar)
newpsf.toFitsHeader(hdr, 'PSF_')
# First time, overwrite existing file. Later, append
clobber = not band in written
written.add(band)
if band == 'r':
rimgs.append(img)
fn = stamp_pattern % band
print('writing', fn)
fitsio.write(fn, img.astype(np.float32), clobber=clobber, header=hdr)
fitsio.write(fn, iv.astype(np.float32))
if clobber:
outfns.append(fn)
if band == 'r':
N = len(rimgs)
ncols = int(np.ceil(np.sqrt(float(N))))
nrows = int(np.ceil(float(N) / ncols))
plt.clf()
for k,img in enumerate(rimgs):
plt.subplot(nrows, ncols, k+1)
dimshow(img, vmin=-0.1, vmax=1., ticks=False)
plt.savefig('stamps-%.4f-%.4f.png' % (ra, dec))
return outfns
cat_file_name = os.path.join('input', 'test_input_cat_00.fits')
data = fitsio.read(cat_file_name)
print('flag = ',data['flag'])
new_flag = np.empty((len(data), 50), dtype=bool)
for bit in range(50):
new_flag[:,bit] = data['flag'] & 2**bit != 0
print('new_flag = ',new_flag)
# Write out the catalog to a file
print('dtype = ',new_flag.dtype)
dtype = [ ('x','f8'), ('y','f8'), ('flag', bool, 50) ]
new_data = np.empty(len(data), dtype=dtype)
new_data['x'] = data['x']
new_data['y'] = data['y']
new_data['flag'] = new_flag
new_cat_file_name = os.path.join('input','test_input_boolarray.fits')
fitsio.write(new_cat_file_name, new_data, clobber=True)
# Specifiable columns are: x, y, flag, use, sky, gain. (We'll do flag, use below.)
config = {
'dir' : 'input',
'image_file_name' : 'test_input_image_00.fits',
'cat_file_name' : 'test_input_boolarray.fits',
'x_col' : 'x',
'y_col' : 'y',
'flag_col' : 'flag',
'skip_flag' : '$2**1 + 2**2 + 2**39'
}
input = piff.InputFiles(config, logger=logger)
assert input.nimages == 1
_, _, image_pos, _, _, _ = input.getRawImageData(0)
print('len = ',len(image_pos))
assert len(image_pos) == 80
np.testing.assert_almost_equal(header['tot'], dd.tot)
out_file_name5 = os.path.join('output','nn_out5.fits')
del dd.xi # Equivalent to not having called calculateXi
del dd.varxi
dd.write(out_file_name5)
data = fitsio.read(out_file_name5)
np.testing.assert_almost_equal(data['r_nom'], np.exp(dd.logr))
np.testing.assert_almost_equal(data['meanr'], dd.meanr)
np.testing.assert_almost_equal(data['meanlogr'], dd.meanlogr)
np.testing.assert_almost_equal(data['DD'], dd.npairs)
assert 'xi' not in data.dtype.names
assert 'varxi' not in data.dtype.names
assert 'RR' not in data.dtype.names
assert 'DR' not in data.dtype.names
header = fitsio.read_header(out_file_name5, 1)
np.testing.assert_almost_equal(header['tot'], dd.tot)
with assert_raises(TypeError):
dd.write(out_file_name3, dr=dr)
with assert_raises(TypeError):
dd.write(out_file_name3, rd=dr)
# Check the read function
dd.calculateXi(rr,dr) # gets xi, varxi back in dd
dd2 = treecorr.NNCorrelation(bin_size=0.1, min_sep=1., max_sep=25., sep_units='arcmin')
dd2.read(out_file_name1)
np.testing.assert_almost_equal(dd2.logr, dd.logr)
np.testing.assert_almost_equal(dd2.meanr, dd.meanr)
np.testing.assert_almost_equal(dd2.meanlogr, dd.meanlogr)
np.testing.assert_almost_equal(dd2.npairs, dd.npairs)
np.testing.assert_almost_equal(dd2.tot, dd.tot)