Skip to content

Commit

Permalink
do_pickle -> check_pickle
Browse files Browse the repository at this point in the history
  • Loading branch information
rmjarvis committed Aug 22, 2023
1 parent af52238 commit 7b2e988
Show file tree
Hide file tree
Showing 48 changed files with 620 additions and 620 deletions.
2 changes: 1 addition & 1 deletion docs/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Utilities Related to NumPy Functions
Test Suite Helper Functions and Contexts
----------------------------------------

.. autofunction:: galsim.utilities.do_pickle
.. autofunction:: galsim.utilities.check_pickle

.. autofunction:: galsim.utilities.all_obj_diff

Expand Down
2 changes: 1 addition & 1 deletion galsim/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,7 +1936,7 @@ def __exit__(self, type, value, traceback):
# The rest of these are only used by the tests in GalSim. But we make them available
# for other code bases who may want to use them as well.

def do_pickle(obj, func = lambda x : x, irreprable=False, random=None):
def check_pickle(obj, func = lambda x : x, irreprable=False, random=None):
"""Check that the object is picklable.
Also check some related desirable properties that we want for (almost) all galsim objects:
Expand Down
2 changes: 1 addition & 1 deletion tests/galsim_test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import copy
import galsim

from galsim.utilities import do_pickle, all_obj_diff, timer, CaptureLog, Profile
from galsim.utilities import check_pickle, all_obj_diff, timer, CaptureLog, Profile

# We used to roll our own versions of these, but numpy.testing has good ones now.
from numpy.testing import assert_raises
Expand Down
18 changes: 9 additions & 9 deletions tests/test_airy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_airy():
np.testing.assert_array_equal(
airy.obscuration, 0.1,
err_msg="Airy obscuration returned wrong value")
do_pickle(airy)
check_pickle(airy)

# Check with default_params
airy = galsim.Airy(lam_over_diam=1./0.8, obscuration=0.1, flux=1, gsparams=default_params)
Expand Down Expand Up @@ -101,10 +101,10 @@ def test_airy():
do_kvalue(airy,myImg, "Airy obscuration=0.1")

# Check picklability
do_pickle(airy0, lambda x: x.drawImage(method='no_pixel'))
do_pickle(airy0)
do_pickle(airy, lambda x: x.drawImage(method='no_pixel'))
do_pickle(airy)
check_pickle(airy0, lambda x: x.drawImage(method='no_pixel'))
check_pickle(airy0)
check_pickle(airy, lambda x: x.drawImage(method='no_pixel'))
check_pickle(airy)

# Test initialization separately with lam and diam, in various units. Since the above profiles
# have lam/diam = 1./0.8 in arbitrary units, we will tell it that lam=1.e9 nm and diam=0.8 m,
Expand All @@ -120,10 +120,10 @@ def test_airy():
# arcsec is the default scale_unit, so can leave this off.
airy4 = galsim.Airy(lam=lam, diam=0.3, obscuration=0.1, flux=1.7)
gsobject_compare(airy,airy4)
do_pickle(airy)
do_pickle(airy2)
do_pickle(airy3)
do_pickle(airy4)
check_pickle(airy)
check_pickle(airy2)
check_pickle(airy3)
check_pickle(airy4)

# Should raise an exception if both lam, lam_over_diam are provided
assert_raises(TypeError, galsim.Airy, lam_over_diam=3, lam=3, diam=1)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_bandpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def test_Bandpass_basic():

# Only the first one is not picklable
if k > 0:
do_pickle(b)
do_pickle(b, lambda x: (x(390), x(470), x(490), x(510), x(560)) )
check_pickle(b)
check_pickle(b, lambda x: (x(390), x(470), x(490), x(510), x(560)) )

assert_raises(ValueError, galsim.Bandpass, throughput="'eggs'", wave_type='nm',
blue_limit=400, red_limit=700)
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_Bandpass_mul():
err_msg="Found wrong value in Bandpass.__mul__")
np.testing.assert_array_almost_equal(f.wave_list, [1.1, 2.2, 3, 4.4, 5.5],
err_msg="wrong wave_list in Bandpass.__mul__")
do_pickle(f)
check_pickle(f)

# scalar * Bandpass
f = 1.21 * a
Expand All @@ -197,7 +197,7 @@ def test_Bandpass_mul():
err_msg="wrong wave_list in Bandpass.__mul__")

if a is a_lt:
do_pickle(f)
check_pickle(f)


@timer
Expand Down Expand Up @@ -243,7 +243,7 @@ def test_Bandpass_div():
if a is a_lt:
np.testing.assert_array_almost_equal(f.wave_list, [1, 2, 3, 4, 5],
err_msg="wrong wave_list in Bandpass.__div__")
do_pickle(f)
check_pickle(f)
else:
np.testing.assert_array_almost_equal(f.wave_list, [],
err_msg="wrong wave_list in Bandpass.__div__")
Expand Down
30 changes: 15 additions & 15 deletions tests/test_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def test_box():
do_shoot(pixel,myImg,"Pixel")

# Check picklability
do_pickle(pixel, lambda x: x.drawImage(method='no_pixel'))
do_pickle(pixel)
do_pickle(galsim.Pixel(1))
check_pickle(pixel, lambda x: x.drawImage(method='no_pixel'))
check_pickle(pixel)
check_pickle(galsim.Pixel(1))

# Check that non-square Box profiles work correctly
scale = 0.2939 # Use a strange scale here to make sure that the centers of the pixels
Expand Down Expand Up @@ -110,9 +110,9 @@ def test_box():
assert box2.withGSParams(maximum_fft_size=30000) == box.withGSParams(xvalue_accuracy=1.e-8, kvalue_accuracy=1.e-8)

# Check picklability
do_pickle(box, lambda x: x.drawImage(method='no_pixel'))
do_pickle(box)
do_pickle(galsim.Box(1,1))
check_pickle(box, lambda x: x.drawImage(method='no_pixel'))
check_pickle(box)
check_pickle(galsim.Box(1,1))

# Check sheared boxes the same way
box = galsim.Box(width=3, height=2, flux=test_flux, gsparams=gsp)
Expand All @@ -121,8 +121,8 @@ def test_box():
do_shoot(box,im, "Sheared Box")
if __name__ == '__main__':
do_kvalue(box,im, "Sheared Box")
do_pickle(box, lambda x: x.drawImage(method='no_pixel'))
do_pickle(box)
check_pickle(box, lambda x: x.drawImage(method='no_pixel'))
check_pickle(box)
cen = galsim.PositionD(0, 0)
np.testing.assert_equal(box.centroid, cen)
np.testing.assert_almost_equal(box.kValue(cen), (1+0j) * test_flux)
Expand All @@ -135,8 +135,8 @@ def test_box():
check_basic(conv, "Sheared Box convolved with pixel in real space",
approx_maxsb=True, scale=0.2)
do_kvalue(conv,im, "Sheared Box convolved with pixel in real space")
do_pickle(conv, lambda x: x.xValue(0.123,-0.456))
do_pickle(conv)
check_pickle(conv, lambda x: x.xValue(0.123,-0.456))
check_pickle(conv)


@timer
Expand Down Expand Up @@ -213,9 +213,9 @@ def test_tophat():
np.testing.assert_almost_equal(tophat.xValue(0., radius+0.001), 0.)

# Check picklability
do_pickle(tophat, lambda x: x.drawImage(method='no_pixel'))
do_pickle(tophat)
do_pickle(galsim.TopHat(1))
check_pickle(tophat, lambda x: x.drawImage(method='no_pixel'))
check_pickle(tophat)
check_pickle(galsim.TopHat(1))

# Check sheared tophat the same way
tophat = galsim.TopHat(radius=1.2, flux=test_flux)
Expand All @@ -233,8 +233,8 @@ def test_tophat():
np.testing.assert_almost_equal(tophat.xValue(cen), tophat.max_sb)

# Check picklability
do_pickle(tophat, lambda x: x.drawImage(method='no_pixel'))
do_pickle(tophat)
check_pickle(tophat, lambda x: x.drawImage(method='no_pixel'))
check_pickle(tophat)

# Check real-space convolution of the sheared tophat.
conv = galsim.Convolve(tophat, galsim.Pixel(scale=scale), real_space=True)
Expand Down
24 changes: 12 additions & 12 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_ascii_catalog():
np.testing.assert_equal(cat.getInt(1,11), 15)
np.testing.assert_almost_equal(cat.getFloat(2,1), 8000)

do_pickle(cat)
check_pickle(cat)

cat2 = galsim.Catalog('catalog.txt', 'config_input', comments='#', file_type='ASCII')
assert cat2 == cat
Expand All @@ -47,13 +47,13 @@ def test_ascii_catalog():
assert cat2.nobjects == cat.nobjects
np.testing.assert_array_equal(cat2.data, cat.data)
assert cat2 != cat
do_pickle(cat2)
check_pickle(cat2)

cat3 = galsim.Catalog('catalog3.txt', 'config_input', comments='')
assert len(cat3) == cat3.nobjects == cat.nobjects
np.testing.assert_array_equal(cat3.data, cat.data)
assert cat3 != cat
do_pickle(cat3)
check_pickle(cat3)

# Check construction errors
assert_raises(galsim.GalSimValueError, galsim.Catalog, 'catalog.txt', file_type='invalid')
Expand Down Expand Up @@ -82,7 +82,7 @@ def test_fits_catalog():
np.testing.assert_equal(cat.getInt(1,'angle2'), 15)
np.testing.assert_almost_equal(cat.getFloat(2,'float2'), 8000)

do_pickle(cat)
check_pickle(cat)

cat2 = galsim.Catalog('catalog.fits', 'config_input', hdu=1, file_type='FITS')
assert cat2 == cat
Expand All @@ -106,14 +106,14 @@ def test_fits_catalog():
assert len(cat2) == cat2.nobjects == cat.nobjects
np.testing.assert_array_equal(cat2.data, cat.data)
assert cat2 != cat
do_pickle(cat2)
check_pickle(cat2)

cat3 = galsim.Catalog('catalog2.fits', 'config_input', hdu='data')
assert cat3.nobjects == cat.nobjects
np.testing.assert_array_equal(cat3.data, cat.data)
assert cat3 != cat
assert cat3 != cat2 # Even though these are the same, it doesn't know 'data' is hdu 2.
do_pickle(cat3)
check_pickle(cat3)



Expand All @@ -132,7 +132,7 @@ def test_basic_dict():
np.testing.assert_almost_equal(d.get('f', 999.), 23.17) # In dict. Ignore default.
d2 = galsim.Dict(dir='config_input', file_name='dict.p', file_type='pickle')
assert d == d2
do_pickle(d)
check_pickle(d)

# JSON
d = galsim.Dict(dir='config_input', file_name='dict.json')
Expand All @@ -144,7 +144,7 @@ def test_basic_dict():
np.testing.assert_almost_equal(d.get('f', 999.), -17.23) # In dict. Ignore default.
d2 = galsim.Dict(dir='config_input', file_name='dict.json', file_type='json')
assert d == d2
do_pickle(d)
check_pickle(d)

# YAML
d = galsim.Dict(dir='config_input', file_name='dict.yaml')
Expand All @@ -156,7 +156,7 @@ def test_basic_dict():
np.testing.assert_almost_equal(d.get('f', 999.), 0.1) # In dict. Ignore default.
d2 = galsim.Dict(dir='config_input', file_name='dict.yaml', file_type='yaml')
assert d == d2
do_pickle(d)
check_pickle(d)

# We also have longer chained keys in dict.yaml
np.testing.assert_equal(d.get('noise.models.0.variance'), 0.12)
Expand All @@ -176,7 +176,7 @@ def test_basic_dict():
d3 = galsim.Dict('dict.yaml', 'config_input', key_split=None)
with assert_raises(KeyError):
d3.get('')
do_pickle(d3)
check_pickle(d3)

with assert_raises(galsim.GalSimValueError):
galsim.Dict(dir='config_input', file_name='dict.yaml', file_type='invalid')
Expand Down Expand Up @@ -320,9 +320,9 @@ def test_output_catalog():
assert cat2.data[key][3] == cat2.data[key][0]

# Check pickling
do_pickle(out_cat)
check_pickle(out_cat)
out_cat2 = galsim.OutputCatalog(names, types) # No data.
do_pickle(out_cat2)
check_pickle(out_cat2)

# Check errors
with assert_raises(galsim.GalSimValueError):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cdmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ def test_fluxconservation():
"Flat image flux is not left invariant by charge deflection")

# Check picklability
do_pickle(cd, lambda x: x.applyForward(image))
do_pickle(cd)
check_pickle(cd, lambda x: x.applyForward(image))
check_pickle(cd)


@timer
Expand Down
36 changes: 18 additions & 18 deletions tests/test_celestial.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ def test_angle():
numpy.testing.assert_equal(theta9.rad, theta1.rad)

# Check picklability
do_pickle(galsim.radians)
do_pickle(galsim.degrees)
do_pickle(galsim.hours)
do_pickle(galsim.arcmin)
do_pickle(galsim.arcsec)
do_pickle(gradians)
do_pickle(theta1)
do_pickle(theta2)
do_pickle(theta3)
do_pickle(theta4)
do_pickle(theta5)
do_pickle(theta6)
do_pickle(theta7)
do_pickle(theta8)
check_pickle(galsim.radians)
check_pickle(galsim.degrees)
check_pickle(galsim.hours)
check_pickle(galsim.arcmin)
check_pickle(galsim.arcsec)
check_pickle(gradians)
check_pickle(theta1)
check_pickle(theta2)
check_pickle(theta3)
check_pickle(theta4)
check_pickle(theta5)
check_pickle(theta6)
check_pickle(theta7)
check_pickle(theta8)

# Check invalid constructors
assert_raises(TypeError,galsim.AngleUnit, galsim.degrees)
Expand Down Expand Up @@ -169,10 +169,10 @@ def test_celestialcoord_basic():
assert_raises(ValueError, galsim.CelestialCoord.from_xyz, 0, 0, 0)

# Check picklability
do_pickle(c1)
do_pickle(c2)
do_pickle(c3)
do_pickle(c4)
check_pickle(c1)
check_pickle(c2)
check_pickle(c3)
check_pickle(c4)

assert c1 == galsim.CelestialCoord(ra=0.*galsim.degrees, dec=0.*galsim.degrees)
assert c2 == galsim.CelestialCoord(ra=165.*galsim.degrees, dec=-37.*galsim.degrees)
Expand Down
Loading

0 comments on commit 7b2e988

Please sign in to comment.