Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 54 additions & 161 deletions tests/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ def check_dep(f, *args, **kwargs):

@timer
def test_gsparams():
if is_jax_galsim():
pass
else:
check_dep(galsim.GSParams, allowed_flux_variation=0.90)
check_dep(galsim.GSParams, range_division_for_extrema=50)
check_dep(galsim.GSParams, small_fraction_of_flux=1.e-6)
if is_jax_galsim(): return
check_dep(galsim.GSParams, allowed_flux_variation=0.90)
check_dep(galsim.GSParams, range_division_for_extrema=50)
check_dep(galsim.GSParams, small_fraction_of_flux=1.e-6)


@timer
Expand Down Expand Up @@ -498,6 +496,8 @@ def test_hsm_depr():

@timer
def test_photon_array_depr():
if is_jax_galsim(): return

nphotons = 1000

# First create from scratch
Expand Down Expand Up @@ -544,203 +544,96 @@ def test_photon_array_depr():

# Using the getter is allowed, but deprecated.
photon_array = galsim.PhotonArray(nphotons)
if is_jax_galsim():
# jax-galsim always sets these additional properties
# dxdz = check_dep(getattr, photon_array, 'dxdz')
# however jax-galsim sets them to NaN so they are not allocated
assert not photon_array.hasAllocatedAngles()
assert len(photon_array.dxdz) == nphotons
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.dxdz = 0.17
# non-nan means allocated for jax-galsim
assert photon_array.hasAllocatedAngles()
else:
dxdz = check_dep(getattr, photon_array, 'dxdz')
assert photon_array.hasAllocatedAngles()
assert photon_array.hasAllocatedAngles()
assert len(photon_array.dxdz) == nphotons
assert len(photon_array.dydz) == nphotons
dxdz[:] = 0.17
dxdz = check_dep(getattr, photon_array, 'dxdz')
assert photon_array.hasAllocatedAngles()
assert photon_array.hasAllocatedAngles()
assert len(photon_array.dxdz) == nphotons
assert len(photon_array.dydz) == nphotons
dxdz[:] = 0.17
np.testing.assert_array_equal(photon_array.dxdz, 0.17)
np.testing.assert_array_equal(photon_array.dydz, 0.)

if is_jax_galsim():
assert hasattr(photon_array, "dydz")
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.dydz = 0.59
else:
dydz = photon_array.dydz # Allowed now.
dydz[:] = 0.59
dydz = photon_array.dydz # Allowed now.
dydz[:] = 0.59
np.testing.assert_array_equal(photon_array.dydz, 0.59)

if is_jax_galsim():
# jax-galsim always sets these additional properties
# wave = check_dep(getattr, photon_array, 'wavelength')
# however jax-galsim sets them to NaN so they are not allocated
assert not photon_array.hasAllocatedWavelengths()
else:
wave = check_dep(getattr, photon_array, 'wavelength')
assert photon_array.hasAllocatedWavelengths()
wave = check_dep(getattr, photon_array, 'wavelength')
assert photon_array.hasAllocatedWavelengths()
assert len(photon_array.wavelength) == nphotons
if is_jax_galsim():
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.wavelength = 500.0
# jax-galsim is allocated now
assert photon_array.hasAllocatedWavelengths()
else:
wave[:] = 500.
wave[:] = 500.
np.testing.assert_array_equal(photon_array.wavelength, 500)

if is_jax_galsim():
# jax-galsim always sets these additional properties
# u = check_dep(getattr, photon_array, "pupil_u")
# however jax-galsim sets them to NaN so they are not allocated
assert not photon_array.hasAllocatedPupil()
else:
u = check_dep(getattr, photon_array, 'pupil_u')
assert photon_array.hasAllocatedPupil()
assert len(photon_array.pupil_u) == nphotons
u = check_dep(getattr, photon_array, 'pupil_u')
assert photon_array.hasAllocatedPupil()
assert len(photon_array.pupil_u) == nphotons
assert len(photon_array.pupil_v) == nphotons
if is_jax_galsim():
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.pupil_u = 6.0
# jax-galsim is allocated now
assert photon_array.hasAllocatedPupil()
else:
u[:] = 6.0
u[:] = 6.0
np.testing.assert_array_equal(photon_array.pupil_u, 6.0)
np.testing.assert_array_equal(photon_array.pupil_v, 0.0)
if is_jax_galsim():
assert hasattr(photon_array, "pupil_v")
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.pupil_v = 10.0
else:
v = photon_array.pupil_v
v[:] = 10.0
v = photon_array.pupil_v
v[:] = 10.0
np.testing.assert_array_equal(photon_array.pupil_v, 10.0)

if is_jax_galsim():
# jax-galsim always sets these additional properties
# t = check_dep(getattr, photon_array, "time")
# however jax-galsim sets them to NaN so they are not allocated
assert not photon_array.hasAllocatedTimes()
# jax-galsim needs to set 0
photon_array.time = 0.0
# jax-galsim is allocated now
assert photon_array.hasAllocatedTimes()
else:
t = check_dep(getattr, photon_array, 'time')
t = check_dep(getattr, photon_array, 'time')
assert photon_array.hasAllocatedTimes()
assert len(photon_array.time) == nphotons
np.testing.assert_array_equal(photon_array.time, 0.0)
if is_jax_galsim():
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.time = 10
else:
t[:] = 10
t[:] = 10
np.testing.assert_array_equal(photon_array.time, 10.0)

# For coverage, also need to test the two pair ones in other order.
photon_array = galsim.PhotonArray(nphotons)
if is_jax_galsim():
# jax-galsim always sets these additional properties
# dydz = check_dep(getattr, photon_array, "dydz")
# however jax-galsim sets them to NaN so they are not allocated
assert not photon_array.hasAllocatedAngles()
else:
dydz = check_dep(getattr, photon_array, 'dydz')
assert photon_array.hasAllocatedAngles()
assert photon_array.hasAllocatedAngles()
dydz = check_dep(getattr, photon_array, 'dydz')
assert photon_array.hasAllocatedAngles()
assert photon_array.hasAllocatedAngles()
assert len(photon_array.dxdz) == nphotons
assert len(photon_array.dydz) == nphotons
if is_jax_galsim():
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.dydz = 0.59
# non-nan means allocated for jax-galsim
assert photon_array.hasAllocatedAngles()
else:
dydz[:] = 0.59
dydz[:] = 0.59
np.testing.assert_array_equal(photon_array.dxdz, 0.)
np.testing.assert_array_equal(photon_array.dydz, 0.59)

if is_jax_galsim():
assert hasattr(photon_array, "dxdz")
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.dxdz = 0.17
else:
dxdz = photon_array.dxdz # Allowed now.
dxdz[:] = 0.17
dxdz = photon_array.dxdz # Allowed now.
dxdz[:] = 0.17
np.testing.assert_array_equal(photon_array.dxdz, 0.17)

if is_jax_galsim():
# jax-galsim always sets these additional properties
# v = check_dep(getattr, photon_array, "pupil_v")
# however jax-galsim sets them to NaN so they are not allocated
assert not photon_array.hasAllocatedPupil()
else:
v = check_dep(getattr, photon_array, 'pupil_v')
assert photon_array.hasAllocatedPupil()
assert len(photon_array.pupil_u) == nphotons
v = check_dep(getattr, photon_array, 'pupil_v')
assert photon_array.hasAllocatedPupil()
assert len(photon_array.pupil_u) == nphotons
assert len(photon_array.pupil_v) == nphotons
if is_jax_galsim():
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.pupil_v = 10.0
# jax-galsim is allocated now
assert photon_array.hasAllocatedPupil()
else:
v[:] = 10.0
v[:] = 10.0
np.testing.assert_array_equal(photon_array.pupil_u, 0.0)
np.testing.assert_array_equal(photon_array.pupil_v, 10.0)
if is_jax_galsim():
assert hasattr(photon_array, "pupil_u")
# JAX-Galsim does not allow by reference setting - changed this
# to make tests below run
photon_array.pupil_u = 6.0
else:
u = photon_array.pupil_u
u[:] = 6.0
u = photon_array.pupil_u
u[:] = 6.0
np.testing.assert_array_equal(photon_array.pupil_u, 6.0)

# Check assignAt
pa1 = galsim.PhotonArray(50)
pa1.x = photon_array.x[:50]
if is_jax_galsim():
pa1.y = photon_array.y[:50]
pa1.flux.at[0:50].set(photon_array.flux[:50])
else:
for i in range(50):
pa1.y[i] = photon_array.y[i]
pa1.flux[0:50] = photon_array.flux[:50]
for i in range(50):
pa1.y[i] = photon_array.y[i]
pa1.flux[0:50] = photon_array.flux[:50]
pa1.dxdz = photon_array.dxdz[:50]
pa1.dydz = photon_array.dydz[:50]
pa1.pupil_u = photon_array.pupil_u[:50]
pa1.pupil_v = photon_array.pupil_v[:50]
pa2 = galsim.PhotonArray(100)
check_dep(pa2.assignAt, 0, pa1)
check_dep(pa2.assignAt, 50, pa1)
np.testing.assert_array_almost_equal(pa2.x[:50], pa1.x)
np.testing.assert_array_almost_equal(pa2.y[:50], pa1.y)
np.testing.assert_array_almost_equal(pa2.flux[:50], pa1.flux)
np.testing.assert_array_almost_equal(pa2.dxdz[:50], pa1.dxdz)
np.testing.assert_array_almost_equal(pa2.dydz[:50], pa1.dydz)
np.testing.assert_array_almost_equal(pa2.pupil_u[:50], pa1.pupil_u)
np.testing.assert_array_almost_equal(pa2.pupil_v[:50], pa1.pupil_v)
np.testing.assert_array_almost_equal(pa2.x[50:], pa1.x)
np.testing.assert_array_almost_equal(pa2.y[50:], pa1.y)
np.testing.assert_array_almost_equal(pa2.flux[50:], pa1.flux)
np.testing.assert_array_almost_equal(pa2.dxdz[50:], pa1.dxdz)
np.testing.assert_array_almost_equal(pa2.dydz[50:], pa1.dydz)
np.testing.assert_array_almost_equal(pa2.pupil_u[50:], pa1.pupil_u)
np.testing.assert_array_almost_equal(pa2.pupil_v[50:], pa1.pupil_v)
np.testing.assert_almost_equal(pa2.x[:50], pa1.x)
np.testing.assert_almost_equal(pa2.y[:50], pa1.y)
np.testing.assert_almost_equal(pa2.flux[:50], pa1.flux)
np.testing.assert_almost_equal(pa2.dxdz[:50], pa1.dxdz)
np.testing.assert_almost_equal(pa2.dydz[:50], pa1.dydz)
np.testing.assert_almost_equal(pa2.pupil_u[:50], pa1.pupil_u)
np.testing.assert_almost_equal(pa2.pupil_v[:50], pa1.pupil_v)
np.testing.assert_almost_equal(pa2.x[50:], pa1.x)
np.testing.assert_almost_equal(pa2.y[50:], pa1.y)
np.testing.assert_almost_equal(pa2.flux[50:], pa1.flux)
np.testing.assert_almost_equal(pa2.dxdz[50:], pa1.dxdz)
np.testing.assert_almost_equal(pa2.dydz[50:], pa1.dydz)
np.testing.assert_almost_equal(pa2.pupil_u[50:], pa1.pupil_u)
np.testing.assert_almost_equal(pa2.pupil_v[50:], pa1.pupil_v)

# Error if it doesn't fit.
with assert_raises(ValueError):
Expand Down
14 changes: 2 additions & 12 deletions tests/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,12 +1259,7 @@ def test_fft():
[4,6,8,4],
[2,4,6,6] ],
xmin=-2, ymin=-2, dtype=dt, scale=0.1)
if is_jax_galsim():
if dt not in [np.complex128, complex]:
kim = xim.calculate_fft()
xim2 = kim.calculate_inverse_fft()
np.testing.assert_array_almost_equal(xim.array, xim2.array)
else:
if (not is_jax_galsim()) or (is_jax_galsim() and (not np.iscomplexobj(dt(1)))):
kim = xim.calculate_fft()
xim2 = kim.calculate_inverse_fft()
np.testing.assert_array_almost_equal(xim.array, xim2.array)
Expand Down Expand Up @@ -1299,12 +1294,7 @@ def test_fft():
xim2 = galsim.Image([ [2,4,6],
[4,6,8] ],
xmin=-2, ymin=-1, dtype=dt, scale=0.1)
if is_jax_galsim():
if dt not in [np.complex128, complex]:
kim = xim.calculate_fft()
kim2 = xim2.calculate_fft()
np.testing.assert_array_almost_equal(kim.array, kim2.array)
else:
if (not is_jax_galsim()) or (is_jax_galsim() and (not np.iscomplexobj(dt(1)))):
kim = xim.calculate_fft()
kim2 = xim2.calculate_fft()
np.testing.assert_array_almost_equal(kim.array, kim2.array)
Expand Down
57 changes: 28 additions & 29 deletions tests/test_interpolatedimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1892,38 +1892,37 @@ def test_drawreal_seg_fault():
"""Test to reproduce bug report in Issue #1164 that was causing seg faults
"""
# this test only runs with real galsim
if is_jax_galsim():
pass
else:
import pickle
if is_jax_galsim(): return

prof_file = os.path.join(
os.path.dirname(__file__),
'input/test_interpolatedimage_seg_fault_prof.pkl'
)
with open(prof_file, 'rb') as f:
prof = pickle.load(f)
print(repr(prof))

image = galsim.Image(
galsim.BoundsI(
xmin=-12,
xmax=12,
ymin=-12,
ymax=12
),
dtype=float,
scale=1
)
import pickle

prof_file = os.path.join(
os.path.dirname(__file__),
'input/test_interpolatedimage_seg_fault_prof.pkl'
)
with open(prof_file, 'rb') as f:
prof = pickle.load(f)
print(repr(prof))

image = galsim.Image(
galsim.BoundsI(
xmin=-12,
xmax=12,
ymin=-12,
ymax=12
),
dtype=float,
scale=1
)

image.fill(3)
prof.drawReal(image)
image.fill(3)
prof.drawReal(image)

# The problem was that the object is shifted fully off the target image and that was leading
# to an attempt to create a stack of length -1, which caused the seg fault.
# So mostly this test just confirms that this runs without seg faulting.
# But we can check that the image is now correctly all zeros.
np.testing.assert_array_equal(image.array, 0)
# The problem was that the object is shifted fully off the target image and that was leading
# to an attempt to create a stack of length -1, which caused the seg fault.
# So mostly this test just confirms that this runs without seg faulting.
# But we can check that the image is now correctly all zeros.
np.testing.assert_array_equal(image.array, 0)


@timer
Expand Down
Loading
Loading