Skip to content

Commit eb4593c

Browse files
committed
test: add test of ne
1 parent 1742c24 commit eb4593c

1 file changed

Lines changed: 55 additions & 26 deletions

File tree

tests/jax/test_api.py

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,29 @@ def test_api_same():
4444
]
4545

4646

47-
def _attempt_init(cls, kwargs):
47+
def _attempt_init(cls, kwargs, offset=0):
4848
try:
49-
return cls(**kwargs)
49+
if "flux" in kwargs:
50+
flux = kwargs.pop("flux")
51+
else:
52+
flux = 1
53+
return cls(**kwargs, flux=offset + flux)
5054
except Exception as e:
5155
if any(estr in repr(e) for estr in OK_ERRORS):
5256
pass
5357
else:
5458
raise e
5559

5660
try:
57-
return cls(jnp.array(2.0), **kwargs)
61+
return cls(jnp.array(2.0 + offset), **kwargs)
5862
except Exception as e:
5963
if any(estr in repr(e) for estr in OK_ERRORS):
6064
pass
6165
else:
6266
raise e
6367

6468
try:
65-
return cls(jnp.array(2.0), jnp.array(4.0), **kwargs)
69+
return cls(jnp.array(2.0 + offset), jnp.array(4.0 + offset), **kwargs)
6670
except Exception as e:
6771
if any(estr in repr(e) for estr in OK_ERRORS):
6872
pass
@@ -71,7 +75,7 @@ def _attempt_init(cls, kwargs):
7175

7276
if cls in [jax_galsim.Convolution, jax_galsim.Deconvolution]:
7377
try:
74-
return cls(jax_galsim.Gaussian(**kwargs))
78+
return cls(jax_galsim.Gaussian(**kwargs).withFlux(offset + 1))
7579
except Exception as e:
7680
if any(estr in repr(e) for estr in OK_ERRORS):
7781
pass
@@ -81,7 +85,7 @@ def _attempt_init(cls, kwargs):
8185
if cls in [jax_galsim.InterpolatedImage]:
8286
try:
8387
return cls(
84-
jax_galsim.ImageD(jnp.arange(100).reshape((10, 10))),
88+
jax_galsim.ImageD(jnp.arange(100).reshape((10, 10)) + offset),
8589
scale=1.3,
8690
**kwargs,
8791
)
@@ -114,7 +118,10 @@ def _kfun(x, prof):
114118
_kgradfun_vmap = jax.jit(jax.vmap(_kgradfun, in_axes=(0, None)))
115119

116120

117-
def _run_object_checks(obj, cls, kind):
121+
def _run_object_checks(obj, cls, kind, other_obj=None):
122+
if other_obj is not None:
123+
assert obj != other_obj
124+
118125
if kind == "pickle-eval-repr":
119126
from numpy import array # noqa: F401
120127

@@ -134,6 +141,7 @@ def _run_object_checks(obj, cls, kind):
134141
assert isinstance(eval(repr(obj)) == obj, bool)
135142
else:
136143
assert isinstance(eval(repr(obj)) == obj, jnp.ndarray)
144+
137145
elif kind == "to-from-galsim":
138146
gs_obj = obj.to_galsim()
139147
jgs_obj = obj.from_galsim(gs_obj)
@@ -460,12 +468,13 @@ def test_api_gsobject(kind):
460468
else:
461469
kwargs = {}
462470
obj = _attempt_init(cls, kwargs)
471+
other_obj = _attempt_init(cls, kwargs, offset=1.5)
463472

464473
if obj is not None and obj.__class__ is not jax_galsim.GSObject:
465474
cls_tested.add(cls.__name__)
466475
print(obj)
467476

468-
_run_object_checks(obj, cls, kind)
477+
_run_object_checks(obj, cls, kind, other_obj=other_obj)
469478

470479
if cls.__name__ == "Gaussian":
471480
_obj = obj + obj
@@ -499,8 +508,9 @@ def test_api_gsobject(kind):
499508
],
500509
)
501510
def test_api_shear(obj):
511+
other_obj = jax_galsim.Shear(g1=0, g2=0.5)
502512
_run_object_checks(obj, jax_galsim.Shear, "docs-methods")
503-
_run_object_checks(obj, jax_galsim.Shear, "pickle-eval-repr")
513+
_run_object_checks(obj, jax_galsim.Shear, "pickle-eval-repr", other_obj=other_obj)
504514
_run_object_checks(obj, jax_galsim.Shear, "to-from-galsim")
505515

506516
def _reg_sfun(g1):
@@ -546,8 +556,9 @@ def _reg_sfun(g1):
546556
],
547557
)
548558
def test_api_bounds(obj):
559+
other_obj = obj.__class__(-1, 1, -1, 1)
549560
_run_object_checks(obj, obj.__class__, "docs-methods")
550-
_run_object_checks(obj, obj.__class__, "pickle-eval-repr")
561+
_run_object_checks(obj, obj.__class__, "pickle-eval-repr", other_obj=other_obj)
551562
_run_object_checks(obj, obj.__class__, "to-from-galsim")
552563

553564
# JAX tracing should be an identity
@@ -600,8 +611,9 @@ def _reg_sfun(g1):
600611
],
601612
)
602613
def test_api_position(obj):
614+
other_obj = obj.__class__(-1, 1)
603615
_run_object_checks(obj, obj.__class__, "docs-methods")
604-
_run_object_checks(obj, obj.__class__, "pickle-eval-repr")
616+
_run_object_checks(obj, obj.__class__, "pickle-eval-repr", other_obj=other_obj)
605617
_run_object_checks(obj, obj.__class__, "to-from-galsim")
606618

607619
# JAX tracing should be an identity
@@ -647,8 +659,9 @@ def _reg_sfun(g1):
647659
],
648660
)
649661
def test_api_image(obj):
662+
other_obj = obj + 11.0
650663
_run_object_checks(obj, obj.__class__, "docs-methods")
651-
_run_object_checks(obj, obj.__class__, "pickle-eval-repr-img")
664+
_run_object_checks(obj, obj.__class__, "pickle-eval-repr-img", other_obj=other_obj)
652665
_run_object_checks(obj, obj.__class__, "to-from-galsim")
653666

654667
# JAX tracing should be an identity
@@ -695,11 +708,11 @@ def _reg_sfun(g1):
695708
]
696709

697710

698-
def _attempt_init_wcs(cls):
711+
def _attempt_init_wcs(cls, offset=0):
699712
obj = None
700713

701714
try:
702-
obj = cls(jnp.array(0.4))
715+
obj = cls(jnp.array(0.4 + offset))
703716
except Exception as e:
704717
if any(estr in repr(e) for estr in OK_ERRORS_WCS):
705718
pass
@@ -708,7 +721,8 @@ def _attempt_init_wcs(cls):
708721

709722
try:
710723
obj = cls(
711-
jnp.array(0.4), jax_galsim.Shear(g1=jnp.array(0.1), g2=jnp.array(0.2))
724+
jnp.array(0.4 + offset),
725+
jax_galsim.Shear(g1=jnp.array(0.1), g2=jnp.array(0.2)),
712726
)
713727
except Exception as e:
714728
if any(estr in repr(e) for estr in OK_ERRORS_WCS):
@@ -717,7 +731,12 @@ def _attempt_init_wcs(cls):
717731
raise e
718732

719733
try:
720-
obj = cls(jnp.array(0.45), jnp.array(-0.02), jnp.array(0.04), jnp.array(-0.35))
734+
obj = cls(
735+
jnp.array(0.45 + offset),
736+
jnp.array(-0.02),
737+
jnp.array(0.04),
738+
jnp.array(-0.35),
739+
)
721740
except Exception as e:
722741
if any(estr in repr(e) for estr in OK_ERRORS_WCS):
723742
pass
@@ -730,6 +749,7 @@ def _attempt_init_wcs(cls):
730749
)
731750
file_name = "DECam_00158414_01.fits.fz"
732751
obj = cls(file_name, dir=dr)
752+
obj = obj.shiftOrigin(jax_galsim.PositionD(offset, offset))
733753
except Exception as e:
734754
if any(estr in repr(e) for estr in OK_ERRORS_WCS):
735755
pass
@@ -776,11 +796,12 @@ def test_api_wcs():
776796
tested = set()
777797
for cls in classes:
778798
obj = _attempt_init_wcs(cls)
799+
other_obj = _attempt_init_wcs(cls, offset=1.5)
779800
if obj is not None:
780801
print(obj)
781802
tested.add(cls.__name__)
782803
_run_object_checks(obj, cls, "docs-methods")
783-
_run_object_checks(obj, cls, "pickle-eval-repr-wcs")
804+
_run_object_checks(obj, cls, "pickle-eval-repr-wcs", other_obj=other_obj)
784805
_run_object_checks(obj, cls, "to-from-galsim")
785806
if isinstance(obj, jax_galsim.wcs.CelestialWCS):
786807
_run_object_checks(obj, cls, "vmap-jit-grad-celestialwcs")
@@ -800,8 +821,9 @@ def test_api_wcs():
800821

801822
def test_api_angleunit():
802823
obj = jax_galsim.AngleUnit(jnp.array(0.1))
824+
other_obj = jax_galsim.AngleUnit(jnp.array(0.5))
803825
_run_object_checks(obj, obj.__class__, "docs-methods")
804-
_run_object_checks(obj, obj.__class__, "pickle-eval-repr")
826+
_run_object_checks(obj, obj.__class__, "pickle-eval-repr", other_obj=other_obj)
805827

806828
# JAX tracing should be an identity
807829
assert obj.__class__.tree_unflatten(*((obj.tree_flatten())[::-1])) == obj
@@ -834,8 +856,9 @@ def _reg_sfun(g1):
834856

835857
def test_api_angle():
836858
obj = jax_galsim.Angle(jnp.array(0.1) * jax_galsim.degrees)
859+
other_obj = jax_galsim.Angle(jnp.array(0.5) * jax_galsim.degrees)
837860
_run_object_checks(obj, obj.__class__, "docs-methods")
838-
_run_object_checks(obj, obj.__class__, "pickle-eval-repr")
861+
_run_object_checks(obj, obj.__class__, "pickle-eval-repr", other_obj=other_obj)
839862
_run_object_checks(obj, obj.__class__, "to-from-galsim")
840863

841864
# JAX tracing should be an identity
@@ -872,8 +895,11 @@ def _reg_sfun(g1):
872895

873896
def test_api_celestial_coord():
874897
obj = jax_galsim.CelestialCoord(45 * jax_galsim.degrees, -30 * jax_galsim.degrees)
898+
other_obj = jax_galsim.CelestialCoord(
899+
41 * jax_galsim.degrees, -33 * jax_galsim.degrees
900+
)
875901
_run_object_checks(obj, obj.__class__, "docs-methods")
876-
_run_object_checks(obj, obj.__class__, "pickle-eval-repr")
902+
_run_object_checks(obj, obj.__class__, "pickle-eval-repr", other_obj=other_obj)
877903
_run_object_checks(obj, obj.__class__, "to-from-galsim")
878904

879905
# JAX tracing should be an identity
@@ -917,10 +943,11 @@ def test_api_random():
917943
tested = set()
918944
for cls in classes:
919945
obj = cls(seed=42)
946+
other_obj = cls(seed=10)
920947
print(obj)
921948
tested.add(cls.__name__)
922949
_run_object_checks(obj, cls, "docs-methods")
923-
_run_object_checks(obj, cls, "pickle-eval-repr-img")
950+
_run_object_checks(obj, cls, "pickle-eval-repr-img", other_obj=other_obj)
924951
_run_object_checks(obj, cls, "vmap-jit-grad-random")
925952

926953
assert {
@@ -934,9 +961,9 @@ def test_api_random():
934961
} <= tested
935962

936963

937-
def _init_noise(cls):
964+
def _init_noise(cls, offset=0):
938965
try:
939-
obj = cls(jax_galsim.random.GaussianDeviate(seed=42))
966+
obj = cls(jax_galsim.random.GaussianDeviate(seed=42 + offset))
940967
except Exception as e:
941968
if "__init__() missing 1 required positional argument: 'var_image'" in str(e):
942969
pass
@@ -947,7 +974,7 @@ def _init_noise(cls):
947974

948975
try:
949976
obj = cls(
950-
jax_galsim.random.GaussianDeviate(seed=42),
977+
jax_galsim.random.GaussianDeviate(seed=42 + offset),
951978
jax_galsim.ImageD(jnp.ones((10, 10)) * 2.0),
952979
)
953980
except Exception as e:
@@ -970,10 +997,11 @@ def test_api_noise():
970997
tested = set()
971998
for cls in classes:
972999
obj = _init_noise(cls)
1000+
other_obj = _init_noise(cls, offset=10)
9731001
print(obj)
9741002
tested.add(cls.__name__)
9751003
_run_object_checks(obj, cls, "docs-methods")
976-
_run_object_checks(obj, cls, "pickle-eval-repr-img")
1004+
_run_object_checks(obj, cls, "pickle-eval-repr-img", other_obj=other_obj)
9771005
# _run_object_checks(obj, cls, "vmap-jit-grad-random")
9781006

9791007
assert {
@@ -1100,9 +1128,10 @@ def func(x):
11001128

11011129
def test_api_photon_array():
11021130
pa = jax_galsim.PhotonArray(101)
1131+
other_obj = jax_galsim.PhotonArray(102)
11031132

11041133
_run_object_checks(pa, pa.__class__, "docs-methods")
1105-
_run_object_checks(pa, pa.__class__, "pickle-eval-repr-nohash")
1134+
_run_object_checks(pa, pa.__class__, "pickle-eval-repr-nohash", other_obj=other_obj)
11061135
_run_object_checks(pa, pa.__class__, "jax-compatible")
11071136

11081137

0 commit comments

Comments
 (0)