CosmoStat / autometacal

Metacalibration and shape measurement by automatic differentiation
MIT License
4 stars 1 forks source link

U/andrevitorelli/restructure #31

Closed andrevitorelli closed 2 years ago

andrevitorelli commented 2 years ago

Closes #28

EiffL commented 2 years ago

Arf ^^' also the tests don't pass:

$ pytest tests/test_tf_ngmix.py

tells me that np. is not defined. Quickly looking at the test, I see that you also don't import tf etc....

Can you check if the test passes once the imports are fixed?

EiffL commented 2 years ago

Thanks to your notebook I could easily test what I was suggesting, i.e. simply adapting our existing get_ellipticity function. Here is what it looks like:

import tensorflow_probability as tfp
tfd = tfp.distributions

def get_ellipticity(image, fwhm, pixel_scale):
    image = tf.convert_to_tensor(image)
    bs, nx, ny = tf.shape(image)

    # Compute the coordinates 
    X,Y = tf.meshgrid(np.arange(-nx/2 + 0.5, nx/2 + 0.5, dtype='float32') * pixel_scale, 
                      np.arange(-ny/2 + 0.5 ,ny/2 + 0.5, dtype='float32') * pixel_scale)

    # Evaluate gaussian weights
    dist = tfd.MultivariateNormalDiag(loc=[0,0],
                scale_identity_multiplier=fwhm_to_sigma(fwhm))
    w = dist.prob(tf.stack([X,Y], axis=-1))

    # Compute moments
    norm = tf.reduce_sum(tf.reduce_sum(w*im, axis=-1), axis=-1)
    Q11 = tf.reduce_sum(tf.reduce_sum(w*im*X*X, axis=-1), axis=-1)/norm
    Q12 = tf.reduce_sum(tf.reduce_sum(w*im*X*Y, axis=-1), axis=-1)/norm
    Q22 = tf.reduce_sum(tf.reduce_sum(w*im*Y*Y, axis=-1), axis=-1)/norm

    # Compute ellipticity
    q1 = Q11 - Q22      
    q2 = 2*Q12
    T= Q11 + Q22 
    r = tf.stack([q1/T, q2/T], axis=-1)

    return r

And it returns:

>>> get_ellipticity(gal, weight_fwhm, 0.2)
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.17396864, 0.08788937], dtype=float32)>

which matches the ngmix measurement exactly.

I'm posting my test notebook on this gist if you want to take a look at the steps I followed: https://gist.github.com/EiffL/80d3e6c867850c517ca5fcfc6af71381

EiffL commented 2 years ago

Hi @andrevitorelli :-) Is this branch ready for another round of review? We should try to merge it while still fresh in our memories