Example #1
0
def test_alpha_method_fft():
    pixels = 64
    phys = PhysicalModel(pixels=pixels, method="fft")
    phys_analytic = AnalyticalPhysicalModel(pixels=pixels, image_fov=7)
    phys2 = PhysicalModel(pixels=pixels, method="conv2d")

    # test out noise
    kappa = tf.random.uniform(shape=[1, pixels, pixels, 1])
    alphax, alphay = phys.deflection_angle(kappa)
    alphax2, alphay2 = phys2.deflection_angle(kappa)

    # assert np.allclose(alphax, alphax2, atol=1e-4)
    # assert np.allclose(alphay, alphay2, atol=1e-4)

    # test out an analytical profile
    kappa = phys_analytic.kappa_field(2, 0.4, 0, 0.1, 0.5)
    alphax, alphay = phys.deflection_angle(kappa)

    alphax2, alphay2 = phys2.deflection_angle(kappa)
    #
    # assert np.allclose(alphax, alphax2, atol=1e-4)
    # assert np.allclose(alphay, alphay2, atol=1e-4)
    im1 = phys_analytic.lens_source_func_given_alpha(
        tf.concat([alphax, alphay], axis=-1))
    im2 = phys_analytic.lens_source_func_given_alpha(
        tf.concat([alphax2, alphay2], axis=-1))
    return alphax, alphax2, im1, im2
Example #2
0
def test_interpolated_kappa():
    import tensorflow_addons as tfa
    phys = PhysicalModel(pixels=128,
                         src_pixels=32,
                         image_fov=7.68,
                         kappa_fov=5)
    phys_a = AnalyticalPhysicalModel(pixels=128, image_fov=7.68)
    kappa = phys_a.kappa_field(r_ein=2., e=0.2)
    kappa += phys_a.kappa_field(r_ein=1., x0=2., y0=2.)
    true_lens = phys.lens_source_func(kappa, w=0.2)
    true_kappa = kappa

    # Test interpolation of alpha angles on a finer grid
    # phys = PhysicalModel(pixels=128, src_pixels=32, kappa_pixels=32)
    phys_a = AnalyticalPhysicalModel(pixels=32, image_fov=7.68)
    kappa = phys_a.kappa_field(r_ein=2., e=0.2)
    kappa += phys_a.kappa_field(r_ein=1., x0=2., y0=2.)

    # kappa2 = phys_a.kappa_field(r_ein=2., e=0.2)
    # kappa2 += phys_a.kappa_field(r_ein=1., x0=2., y0=2.)
    #
    # kappa = tf.concat([kappa, kappa2], axis=1)

    # Test interpolated kappa lens
    x = np.linspace(-1, 1, 128) * phys.kappa_fov / 2
    x, y = np.meshgrid(x, x)
    x = tf.constant(x[np.newaxis, ..., np.newaxis], tf.float32)
    y = tf.constant(y[np.newaxis, ..., np.newaxis], tf.float32)
    dx = phys.kappa_fov / (32 - 1)
    xmin = -0.5 * phys.kappa_fov
    ymin = -0.5 * phys.kappa_fov
    i_coord = (x - xmin) / dx
    j_coord = (y - ymin) / dx
    wrap = tf.concat([i_coord, j_coord], axis=-1)
    # test_kappa1 = tfa.image.resampler(kappa, wrap)  # bilinear interpolation of source on wrap grid
    # test_lens1 = phys.lens_source_func(test_kappa1, w=0.2)
    phys2 = PhysicalModel(pixels=128,
                          kappa_pixels=32,
                          method="fft",
                          image_fov=7.68,
                          kappa_fov=5)
    test_lens1 = phys2.lens_source_func(kappa, w=0.2)

    # Test interpolated alpha angles lens
    phys2 = PhysicalModel(pixels=32,
                          src_pixels=32,
                          image_fov=7.68,
                          kappa_fov=5)
    alpha1, alpha2 = phys2.deflection_angle(kappa)
    alpha = tf.concat([alpha1, alpha2], axis=-1)
    alpha = tfa.image.resampler(alpha, wrap)
    test_lens2 = phys.lens_source_func_given_alpha(alpha, w=0.2)

    return true_lens, test_lens1, test_lens2
Example #3
0
def test_deflection_angle_conv2():
    phys = PhysicalModel(pixels=64, src_pixels=64)
    kappa = tf.random.normal([1, 64, 64, 1])
    phys.deflection_angle(kappa)
Example #4
0
def test_analytic2():
    phys = AnalyticalPhysicalModelv2(pixels=64)
    # try to oversample kappa map for comparison
    # phys2 = AnalyticalPhysicalModelv2(pixels=256)
    # phys_ref = PhysicalModel(pixels=256, src_pixels=64, method="fft", kappa_fov=7.68)
    phys_ref = PhysicalModel(pixels=64, method="fft")

    # make some dummy coordinates
    x = np.linspace(-1, 1, 64) * 3.0 / 2
    xx, yy = np.meshgrid(x, x)

    # lens params
    r_ein = 2
    x0 = 0.1
    y0 = 0.1
    q = 0.98
    phi = 0.  #np.pi/3

    gamma = 0.
    phi_gamma = 0.

    # source params
    xs = 0.1
    ys = 0.1
    qs = 0.9
    phi_s = 0.  #np.pi/4
    r_eff = 0.4
    n = 1.

    source = phys.sersic_source(xx, yy, xs, ys, qs, phi_s, n, r_eff)[None, ...,
                                                                     None]
    # kappa = phys2.kappa_field(r_ein, q, phi, x0, y0)
    kappa = phys.kappa_field(r_ein, q, phi, x0, y0)
    lens_a = phys.lens_source_sersic_func(r_ein, q, phi, x0, y0, gamma,
                                          phi_gamma, xs, ys, qs, phi_s, n,
                                          r_eff)
    lens_b = phys_ref.lens_source(source, kappa)
    # lens_b = tf.image.resize(lens_b, [64, 64])
    lens_c = phys.lens_source(source, r_ein, q, phi, x0, y0, gamma, phi_gamma)
    # alpha1t, alpha2t = tf.split(phys.analytical_deflection_angles(r_ein, q, phi, x0, y0), 2, axis=-1)
    alpha1t, alpha2t = tf.split(phys.approximate_deflection_angles(
        r_ein, q, phi, x0, y0),
                                2,
                                axis=-1)
    alpha1, alpha2 = phys_ref.deflection_angle(kappa)
    # alpha1 = tf.image.resize(alpha1, [64, 64])
    # alpha2 = tf.image.resize(alpha2, [64, 64])
    beta1 = phys.theta1 - alpha1
    beta2 = phys.theta2 - alpha2
    lens_d = phys.sersic_source(beta1, beta2, xs, ys, q, phi, n, r_eff)

    # plt.imshow(source[0, ..., 0], cmap="twilight", origin="lower")
    # plt.colorbar()
    # plt.show()
    # plt.imshow(np.log10(kappa[0, ..., 0]), cmap="twilight", origin="lower")
    # plt.colorbar()
    # plt.show()
    plt.imshow(lens_a[0, ..., 0], cmap="twilight", origin="lower")
    plt.colorbar()
    plt.show()
    plt.imshow(lens_b[0, ..., 0], cmap="twilight", origin="lower")
    plt.colorbar()
    plt.show()
    # plt.imshow(lens_c[0, ..., 0], cmap="twilight", origin="lower")
    # plt.colorbar()
    # plt.show()
    # plt.imshow(lens_d[0, ..., 0], cmap="twilight", origin="lower")
    # plt.colorbar()
    # plt.show()
    plt.imshow(((lens_a - lens_b))[0, ..., 0],
               cmap="seismic",
               vmin=-0.1,
               vmax=0.1,
               origin="lower")
    plt.colorbar()
    plt.show()
    # plt.imshow(((lens_a - lens_c))[0, ..., 0], cmap="seismic", vmin=-0.1, vmax=0.1, origin="lower")
    # plt.colorbar()
    # plt.show()
    # plt.imshow(((lens_a - lens_d))[0, ..., 0], cmap="seismic", vmin=-0.1, vmax=0.1, origin="lower")
    # plt.colorbar()
    # plt.show()
    plt.imshow(((alpha1t - alpha1))[0, ..., 0],
               cmap="seismic",
               vmin=-1,
               vmax=1,
               origin="lower")
    plt.colorbar()
    plt.show()

    pass