-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spergel #86
Conversation
…z_nup1. 3) implement shoot photon
I do not understand such message in the pytest used in the PR causing test failure
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a few small typos that may help get the tests to pass.
Co-authored-by: Matthew R. Becker <[email protected]>
Co-authored-by: Matthew R. Becker <[email protected]>
Now test_api.py have detected that my xValue is not/ jit-grad friendly. I must investigate. After some tests the problem seems to be in fsmallz_nu(z, nu) as def new_xValue(self, pos):
r = jnp.sqrt(pos.x**2 + pos.y**2) * self._inv_r0
#jnp.where(z <= 1.0e-10, fsmallz_nu(z, nu), jnp.power(z, nu) * _Knu(nu, z))
res = jnp.where(r <= 1.0e-10,
jnp.power(r, self.nu) * galsim.spergel._Knu(self.nu, r),
jnp.power(r, self.nu) * galsim.spergel._Knu(self.nu, r)
)
return self._xnorm * res
galsim.spergel.Spergel._xValue = new_xValue Is ok. |
Well I'm a bit puzzled but I guess this is related to in fact fsmallz_nu cannot be used with integer nu so then the gardient. So I need to protect it according to the above doc. A working hack is (here a code tested in Google colab) def new_fsmallz_nu(z, nu):
def fnu(z, nu):
"""z^nu K_nu[z] z -> 0 O(z^4) z > 0"""
nu += 1.e-10 #to garanty that nu is not an integer
z2 = z * z
z4 = z2 * z2
c1 = jnp.power(2.0, -6.0 - nu)
c2 = galsim.spergel._gamma(-2.0 - nu)
c3 = galsim.spergel._gamma(-2.0 + nu)
c4 = jnp.power(z, 2.0 * nu)
c5 = z4 * 8.0 * z2 * (2.0 + nu) + 32.0 * (1.0 + nu) * (2.0 + nu)
c6 = z2 * (16.0 + z2 - 8.0 * nu) * c3
return c1 * (c4 * c5 * c2 + jnp.power(4.0, nu) * (c6 + 32.0 * galsim.spergel._gamma(nu)))
return jnp.select(
[nu == 0, nu == 1, nu == 2, nu == 3, nu == 4],
[galsim.spergel.f0(z), galsim.spergel.f1(z), fgalsim.spergel.2(z), galsim.spergel.f3(z), galsim.spergel.f4(z)],
default=fnu(z,nu),
) notice that in fact the NaN originate from the jnp.where used in def fz_nu(z, nu):
"""z^nu K_nu[z], z > 0"""
return jnp.where(z <= 1.0e-10, fsmallz_nu(z, nu), jnp.power(z, nu) * _Knu(nu, z)) |
I have made progress to get test_serpel.py::test_spergel do_shoot working. obj = galsim.Spergel(nu=-0.6, half_light_radius=3.5, flux=1.e4) fails as added_flux, photons = obj.drawPhot(im, poisson_flux=False, rng=rng.duplicate()) differs from obj.flux = 10000.0
added_flux = 9999.0 Now, looking at
My comment is the following:
as the following property for So, I agree that has no divergence at So, I do not see the need to make the procedure mentionned in GalSim and I do suspect that it introduces the difference detected by pytest. Now, despite what I've written above, I will try to implement the GalSim procedure. |
I have opened an issue in the GalSim repo to understand how they manage the case nu<0 as I may found a pb in the normalisation. Let see what Mike will tell me. |
…here is a normalisation pb.
I have coded
is a non-diverging integral when
Now either we stick to the Galsim coding because I may be wrong in interpreting the Galsim coding, or we adopt a more natural way to proceed as from point (1) there is no matter to distinguish |
…e profile for small r, and 2) use a normalisation different of GalSim as explained in comment
Hi, I have implemented a |
…ead of _Nnu & polish
There is still a missing test to assert that nu parameter is in [-0.86, 4.0]. |
I'll take a look tomorrow! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left one suggestion for rephrasing the docs.
My final comment is that the galsim APIs provide some bessel functions in galsim.bessel. Any bessel functions defined in this file that are provided by galsim in the galsim.bessel
module should be moved there.
Co-authored-by: Matthew R. Becker <[email protected]>
I have rename "Knu" (modified Bessel 2nd kind) as "kv" to follow Galsim notation. "kv" is moved to Now in the |
No we should not do this. The goal is to match the galsim API exactly. So we only put bessel functions that galsim provides in jax_galsim/bessel.py. Anything extra, we can add to core since this contains private APIs to jax_galsim. |
Who press the |
Either! I'll go ahead! |
This is an implementation of the Spergel profile.
In the Galsim code the Spergel index (nu) is restricted to [-0.85, 4.0]. In the JAX code I do not know to assert a use of non valid nu input parameter.
Notice that nu=0.5 is exactly the Exponential profile. Spergel profile might be a good alternative to the Sersic profile as realized here.
The present code pass test_spergel.py Galsim tests excepting the photon shooting even if I have setup a first version using a inversion method using JAX-Galsim bisection root algo.
A side remark. For Moffat & Spergel we need K_nu Bessel function. For the time beeing this Bessel func is defined both in moffat.py and spergel.py. I guess this is not optimal and this function would have to be defined in the core directory.