示例#1
0
def test_sbinterpolatedimage():
    """Test that we can make SBInterpolatedImages from Images of various types, and convert back.
    """
    import time
    t1 = time.time()
    # for each type, try to make an SBInterpolatedImage, and check that when we draw an image from
    # that SBInterpolatedImage that it is the same as the original
    lan3 = galsim.Lanczos(3, True, 1.E-4)
    lan3_2d = galsim.InterpolantXY(lan3)

    ftypes = [np.float32, np.float64]
    ref_array = np.array([[0.01, 0.08, 0.07, 0.02], [0.13, 0.38, 0.52, 0.06],
                          [0.09, 0.41, 0.44, 0.09], [0.04, 0.11, 0.10, 0.01]])

    for array_type in ftypes:
        image_in = galsim.ImageView[array_type](ref_array.astype(array_type))
        np.testing.assert_array_equal(
            ref_array.astype(array_type),
            image_in.array,
            err_msg=
            "Array from input Image differs from reference array for type %s" %
            array_type)
        sbinterp = galsim.SBInterpolatedImage(image_in, lan3_2d, dx=1.0)
        test_array = np.zeros(ref_array.shape, dtype=array_type)
        image_out = galsim.ImageView[array_type](test_array, scale=1.0)
        sbinterp.draw(image_out.view())
        np.testing.assert_array_equal(
            ref_array.astype(array_type),
            image_out.array,
            err_msg=
            "Array from output Image differs from reference array for type %s"
            % array_type)

        # Lanczos doesn't quite get the flux right.  Wrong at the 5th decimal place.
        # Gary says that's expected -- Lanczos isn't technically flux conserving.
        # He applied the 1st order correction to the flux, but expect to be wrong at around
        # the 10^-5 level.
        # Anyway, Quintic seems to be accurate enough.
        quint = galsim.Quintic(1.e-4)
        quint_2d = galsim.InterpolantXY(quint)
        sbinterp = galsim.SBInterpolatedImage(image_in, quint_2d, dx=1.0)
        sbinterp.setFlux(1.)
        do_shoot(galsim.GSObject(sbinterp), image_out, "InterpolatedImage")

        # Test kvalues
        do_kvalue(galsim.GSObject(sbinterp), "InterpolatedImage")

    t2 = time.time()
    print 'time for %s = %.2f' % (funcname(), t2 - t1)
示例#2
0
文件: real.py 项目: maxmen/GalSim
def simReal(real_galaxy,
            target_PSF,
            target_pixel_scale,
            g1=0.0,
            g2=0.0,
            rotation_angle=None,
            rand_rotate=True,
            rng=None,
            target_flux=1000.0,
            image=None):
    """Function to simulate images (no added noise) from real galaxy training data.

    This function takes a RealGalaxy from some training set, and manipulates it as needed to 
    simulate a (no-noise-added) image from some lower-resolution telescope.  It thus requires a
    target PSF (which could be an image, or one of our base classes) that represents all PSF 
    components including the pixel response, and a target pixel scale.  

    The default rotation option is to impose a random rotation to make irrelevant any real shears 
    in the galaxy training data (optionally, the RNG can be supplied).  This default can be turned 
    off by setting `rand_rotate = False` or by requesting a specific rotation angle using the
    `rotation_angle` keyword, in which case `rand_rotate` is ignored.

    Optionally, the user can specify a shear (default 0).  Finally, they can specify a flux 
    normalization for the final image, default 1000.

    @param real_galaxy         The RealGalaxy object to use, not modified in generating the
                               simulated image.
    @param target_PSF          The target PSF, either one of our base classes or an ImageView/Image.
    @param target_pixel_scale  The pixel scale for the final image, in arcsec.
    @param g1                  First component of shear to impose (components defined with respect
                               to pixel coordinates), default `g1 = 0.`
    @param g2                  Second component of shear to impose, default `g2 = 0.`
    @param rotation_angle      Angle by which to rotate the galaxy (must be a galsim.Angle 
                               instance).
    @param rand_rotate         If `rand_rotate = True` (default) then impose a random rotation on 
                               the training galaxy; this is ignored if `rotation_angle` is set.
    @param rng                 A random number generator to use for selection of the random 
                               rotation angle. (optional, may be any kind of galsim.BaseDeviate 
                               or None)
    @param target_flux         The target flux in the output galaxy image, default 
                               `target_flux = 1000.`
    @param image               As with the GSObject.draw() function, if an image is provided,
                               then it will be used and returned.
                               If `image=None`, then an appropriately sized image will be created.
    @return A simulated galaxy image.  The input RealGalaxy is unmodified. 
    """
    # do some checking of arguments
    if not isinstance(real_galaxy, galsim.RealGalaxy):
        raise RuntimeError("Error: simReal requires a RealGalaxy!")
    for Class in galsim.Image.itervalues():
        if isinstance(target_PSF, Class):
            lan5 = galsim.Lanczos(5, conserve_flux=True, tol=1.e-4)
            interp2d = galsim.InterpolantXY(lan5)
            target_PSF = galsim.SBInterpolatedImage(target_PSF.view(),
                                                    xInterp=interp2d,
                                                    dx=target_pixel_scale)
            break
    for Class in galsim.ImageView.itervalues():
        if isinstance(target_PSF, Class):
            lan5 = galsim.Lanczos(5, conserve_flux=True, tol=1.e-4)
            interp2d = galsim.InterpolantXY(lan5)
            target_PSF = galsim.SBInterpolatedImage(target_PSF,
                                                    xInterp=interp2d,
                                                    dx=target_pixel_scale)
            break
    if isinstance(target_PSF, galsim.GSObject):
        target_PSF = target_PSF.SBProfile
    if not isinstance(target_PSF, galsim.SBProfile):
        raise RuntimeError(
            "Error: target PSF is not an Image, ImageView, SBProfile, or GSObject!"
        )
    if rotation_angle != None and not isinstance(rotation_angle, galsim.Angle):
        raise RuntimeError(
            "Error: specified rotation angle is not an Angle instance!")
    if (target_pixel_scale < real_galaxy.pixel_scale):
        import warnings
        message = "Warning: requested pixel scale is higher resolution than original!"
        warnings.warn(message)
    import math  # needed for pi, sqrt below
    g = math.sqrt(g1**2 + g2**2)
    if g > 1:
        raise RuntimeError("Error: requested shear is >1!")

    # make sure target PSF is normalized
    target_PSF.setFlux(1.0)

    real_galaxy_copy = real_galaxy.copy()

    # rotate
    if rotation_angle != None:
        real_galaxy_copy.applyRotation(rotation_angle)
    elif rotation_angle == None and rand_rotate == True:
        if rng == None:
            uniform_deviate = galsim.UniformDeviate()
        elif isinstance(rng, galsim.BaseDeviate):
            uniform_deviate = galsim.UniformDeviate(rng)
        else:
            raise TypeError(
                "The rng provided to drawShoot is not a BaseDeviate")
        rand_angle = galsim.Angle(math.pi * uniform_deviate(), galsim.radians)
        real_galaxy_copy.applyRotation(rand_angle)

    # set fluxes
    real_galaxy_copy.setFlux(target_flux)

    # shear
    if (g1 != 0.0 or g2 != 0.0):
        real_galaxy_copy.applyShear(g1=g1, g2=g2)

    # convolve, resample
    out_gal = galsim.Convolve([real_galaxy_copy, galsim.GSObject(target_PSF)])
    image = out_gal.draw(image=image, dx=target_pixel_scale)

    # return simulated image
    return image
示例#3
0
def test_shapelet_adjustments():
    """Test that adjusting the Shapelet profile in various ways does the right thing
    """
    ftypes = [np.float32, np.float64]

    nx = 128
    ny = 128
    scale = 0.2
    im = galsim.ImageF(nx, ny, scale=scale)

    sigma = 1.8
    order = 6
    bvec = [
        1.3,  # n = 0
        0.02,
        0.03,  # n = 1
        0.23,
        -0.19,
        0.08,  # n = 2
        0.01,
        0.02,
        0.04,
        -0.03,  # n = 3
        -0.09,
        0.07,
        -0.11,
        -0.08,
        0.11,  # n = 4
        -0.03,
        -0.02,
        -0.08,
        0.01,
        -0.06,
        -0.03,  # n = 5
        0.06,
        -0.02,
        0.00,
        -0.05,
        -0.04,
        0.01,
        0.09
    ]  # n = 6

    ref_shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec)
    ref_im = galsim.ImageF(nx, ny)
    ref_shapelet.drawImage(ref_im, scale=scale)

    # Test PQ and NM access
    np.testing.assert_equal(ref_shapelet.getPQ(0, 0), (bvec[0], 0))
    np.testing.assert_equal(ref_shapelet.getPQ(1, 1), (bvec[5], 0))
    np.testing.assert_equal(ref_shapelet.getPQ(1, 2), (bvec[8], -bvec[9]))
    np.testing.assert_equal(ref_shapelet.getPQ(3, 2), (bvec[19], bvec[20]))
    np.testing.assert_equal(ref_shapelet.getNM(0, 0), (bvec[0], 0))
    np.testing.assert_equal(ref_shapelet.getNM(2, 0), (bvec[5], 0))
    np.testing.assert_equal(ref_shapelet.getNM(3, -1), (bvec[8], -bvec[9]))
    np.testing.assert_equal(ref_shapelet.getNM(5, 1), (bvec[19], bvec[20]))

    # Test that the Shapelet withFlux does the same thing as the GSObject withFlux
    gsref_shapelet = galsim.GSObject(
        ref_shapelet)  # Make it opaque to the Shapelet versions
    gsref_shapelet.withFlux(23.).drawImage(ref_im, method='no_pixel')
    shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec)
    shapelet.withFlux(23.).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet withFlux disagrees with GSObject withFlux")

    # Test that scaling the Shapelet flux does the same thing as the GSObject scaling
    (gsref_shapelet * 0.23).drawImage(ref_im, method='no_pixel')
    (shapelet * 0.23).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet *= 0.23 disagrees with GSObject *= 0.23")

    # Test that the Shapelet rotate does the same thing as the GSObject rotate
    gsref_shapelet.rotate(23. * galsim.degrees).drawImage(ref_im,
                                                          method='no_pixel')
    shapelet.rotate(23. * galsim.degrees).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet rotate disagrees with GSObject rotate")

    # Test that the Shapelet dilate does the same thing as the GSObject dilate
    gsref_shapelet.dilate(1.3).drawImage(ref_im, method='no_pixel')
    shapelet.dilate(1.3).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet dilate disagrees with GSObject dilate")

    # Test that the Shapelet expand does the same thing as the GSObject expand
    gsref_shapelet.expand(1.7).drawImage(ref_im, method='no_pixel')
    shapelet.expand(1.7).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet expand disagrees with GSObject expand")

    # Test that the Shapelet magnify does the same thing as the GSObject magnify
    gsref_shapelet.magnify(0.8).drawImage(ref_im, method='no_pixel')
    shapelet.magnify(0.8).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet magnify disagrees with GSObject magnify")

    # Test that lens works on Shapelet
    gsref_shapelet.lens(-0.05, 0.15, 1.1).drawImage(ref_im, method='no_pixel')
    shapelet.lens(-0.05, 0.15, 1.1).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet lens disagrees with GSObject lens")
示例#4
0
def test_shapelet_adjustments():
    """Test that adjusting the Shapelet profile in various ways does the right thing
    """
    import time
    t1 = time.time()

    ftypes = [np.float32, np.float64]

    nx = 128
    ny = 128
    scale = 0.2
    im = galsim.ImageF(nx,ny, scale=scale)

    sigma = 1.8
    order = 6
    bvec = [1.3,                                            # n = 0
            0.02, 0.03,                                     # n = 1
            0.23, -0.19, 0.08,                              # n = 2
            0.01, 0.02, 0.04, -0.03,                        # n = 3
            -0.09, 0.07, -0.11, -0.08, 0.11,                # n = 4
            -0.03, -0.02, -0.08, 0.01, -0.06, -0.03,        # n = 5
            0.06, -0.02, 0.00, -0.05, -0.04, 0.01, 0.09 ]   # n = 6

    ref_shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec)
    ref_im = galsim.ImageF(nx,ny)
    ref_shapelet.drawImage(ref_im, scale=scale)

    # Test that the Shapelet withFlux does the same thing as the GSObject withFlux
    gsref_shapelet = galsim.GSObject(ref_shapelet)  # Make it opaque to the Shapelet versions
    gsref_shapelet.withFlux(23.).drawImage(ref_im, method='no_pixel')
    shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec)
    shapelet.withFlux(23.).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array, ref_im.array, 6,
        err_msg="Shapelet withFlux disagrees with GSObject withFlux")

    # Test that scaling the Shapelet flux does the same thing as the GSObject scaling
    gsref_shapelet *= 0.23
    gsref_shapelet.drawImage(ref_im, method='no_pixel')
    shapelet *= 0.23
    shapelet.drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array, ref_im.array, 6,
        err_msg="Shapelet *= 0.23 disagrees with GSObject *= 0.23")

    # Test that the Shapelet rotate does the same thing as the GSObject rotate
    gsref_shapelet.rotate(23. * galsim.degrees).drawImage(ref_im, method='no_pixel')
    shapelet.rotate(23. * galsim.degrees).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array, ref_im.array, 6,
        err_msg="Shapelet rotate disagrees with GSObject rotate")

    # Test that the Shapelet dilate does the same thing as the GSObject dilate
    gsref_shapelet.dilate(1.3).drawImage(ref_im, method='no_pixel')
    shapelet.dilate(1.3).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array, ref_im.array, 6,
        err_msg="Shapelet dilate disagrees with GSObject dilate")

    # Test that the Shapelet magnify does the same thing as the GSObject magnify
    gsref_shapelet.magnify(0.8).drawImage(ref_im, method='no_pixel')
    shapelet.magnify(0.8).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array, ref_im.array, 6,
        err_msg="Shapelet magnify disagrees with GSObject magnify")

    # Test that lens works on Shapelet
    gsref_shapelet.lens(-0.05, 0.15, 1.1).drawImage(ref_im, method='no_pixel')
    shapelet.lens(-0.05, 0.15, 1.1).drawImage(im, method='no_pixel')
    np.testing.assert_array_almost_equal(
        im.array, ref_im.array, 6,
        err_msg="Shapelet lens disagrees with GSObject lens")

    t2 = time.time()
    print 'time for %s = %.2f'%(funcname(),t2-t1)
示例#5
0
def test_shapelet_adjustments():
    """Test that adjusting the Shapelet profile in various ways does the right thing
    """
    import time
    t1 = time.time()

    ftypes = [np.float32, np.float64]

    nx = 128
    ny = 128
    scale = 0.2
    im = galsim.ImageF(nx, ny, scale=scale)

    sigma = 1.8
    order = 6
    bvec = [
        1.3,  # n = 0
        0.02,
        0.03,  # n = 1
        0.23,
        -0.19,
        0.08,  # n = 2
        0.01,
        0.02,
        0.04,
        -0.03,  # n = 3
        -0.09,
        0.07,
        -0.11,
        -0.08,
        0.11,  # n = 4
        -0.03,
        -0.02,
        -0.08,
        0.01,
        -0.06,
        -0.03,  # n = 5
        0.06,
        -0.02,
        0.00,
        -0.05,
        -0.04,
        0.01,
        0.09
    ]  # n = 6

    ref_shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec)
    ref_im = galsim.ImageF(nx, ny)
    ref_shapelet.draw(ref_im, scale=scale)

    # Test setSigma
    shapelet = galsim.Shapelet(sigma=1., order=order, bvec=bvec)
    shapelet.setSigma(sigma)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet set with setSigma disagrees with reference Shapelet")

    # Test setBVec
    shapelet = galsim.Shapelet(sigma=sigma, order=order)
    shapelet.setBVec(bvec)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet set with setBVec disagrees with reference Shapelet")

    # Test setOrder
    shapelet = galsim.Shapelet(sigma=sigma, order=2)
    shapelet.setOrder(order)
    shapelet.setBVec(bvec)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet set with setOrder disagrees with reference Shapelet")

    # Test that changing the order preserves the values to the extent possible.
    shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec)
    shapelet.setOrder(10)
    np.testing.assert_array_equal(
        shapelet.getBVec()[0:28],
        bvec,
        err_msg="Shapelet setOrder to larger doesn't preserve existing values."
    )
    np.testing.assert_array_equal(
        shapelet.getBVec()[28:66],
        np.zeros(66 - 28),
        err_msg="Shapelet setOrder to larger doesn't fill with zeros.")
    shapelet.setOrder(6)
    np.testing.assert_array_equal(
        shapelet.getBVec(),
        bvec,
        err_msg=
        "Shapelet setOrder back to original from larger doesn't preserve existing values."
    )
    shapelet.setOrder(3)
    np.testing.assert_array_equal(
        shapelet.getBVec()[0:10],
        bvec[0:10],
        err_msg="Shapelet setOrder to smaller doesn't preserve existing values."
    )
    shapelet.setOrder(6)
    np.testing.assert_array_equal(
        shapelet.getBVec()[0:10],
        bvec[0:10],
        err_msg=
        "Shapelet setOrder back to original from smaller doesn't preserve existing values."
    )
    shapelet.setOrder(6)
    np.testing.assert_array_equal(
        shapelet.getBVec()[10:28],
        np.zeros(28 - 10),
        err_msg=
        "Shapelet setOrder back to original from smaller doesn't fill with zeros."
    )

    # Test that setting a Shapelet with setNM gives the right profile
    shapelet = galsim.Shapelet(sigma=sigma, order=order)
    i = 0
    for n in range(order + 1):
        for m in range(n, -1, -2):
            if m == 0:
                shapelet.setNM(n, m, bvec[i])
                i = i + 1
            else:
                shapelet.setNM(n, m, bvec[i], bvec[i + 1])
                i = i + 2
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet set with setNM disagrees with reference Shapelet")

    # Test that setting a Shapelet with setPQ gives the right profile
    shapelet = galsim.Shapelet(sigma=sigma, order=order)
    i = 0
    for n in range(order + 1):
        for m in range(n, -1, -2):
            p = (n + m) / 2
            q = (n - m) / 2
            if m == 0:
                shapelet.setPQ(p, q, bvec[i])
                i = i + 1
            else:
                shapelet.setPQ(p, q, bvec[i], bvec[i + 1])
                i = i + 2
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet set with setPQ disagrees with reference Shapelet")

    # Test that the Shapelet setFlux does the same thing as the GSObject setFlux
    gsref_shapelet = galsim.GSObject(
        ref_shapelet)  # Make it opaque to the Shapelet versions
    gsref_shapelet.setFlux(23.)
    gsref_shapelet.draw(ref_im)
    shapelet = galsim.Shapelet(sigma=sigma, order=order, bvec=bvec)
    shapelet.setFlux(23.)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet setFlux disagrees with GSObject setFlux")

    # Test that the Shapelet scaleFlux does the same thing as the GSObject scaleFlux
    gsref_shapelet.scaleFlux(0.23)
    gsref_shapelet.draw(ref_im)
    shapelet.scaleFlux(0.23)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet setFlux disagrees with SObject scaleFlux")

    # Test that the Shapelet applyRotation does the same thing as the GSObject applyRotation
    gsref_shapelet.applyRotation(23. * galsim.degrees)
    gsref_shapelet.draw(ref_im)
    shapelet.applyRotation(23. * galsim.degrees)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet applyRotation disagrees with GSObject applyRotation")

    # Test that the Shapelet applyDilation does the same thing as the GSObject applyDilation
    gsref_shapelet.applyDilation(1.3)
    gsref_shapelet.draw(ref_im)
    shapelet.applyDilation(1.3)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet applyDilation disagrees with GSObject applyDilation")

    # Test that the Shapelet applyMagnification does the same thing as the GSObject
    # applyMagnification
    gsref_shapelet.applyMagnification(0.8)
    gsref_shapelet.draw(ref_im)
    shapelet.applyMagnification(0.8)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg=
        "Shapelet applyMagnification disagrees with GSObject applyMagnification"
    )

    # Test that applyLensing works on Shapelet
    gsref_shapelet.applyLensing(-0.05, 0.15, 1.1)
    gsref_shapelet.draw(ref_im)
    shapelet.applyLensing(-0.05, 0.15, 1.1)
    shapelet.draw(im)
    np.testing.assert_array_almost_equal(
        im.array,
        ref_im.array,
        6,
        err_msg="Shapelet applyLensing disagrees with GSObject applyLensing")

    t2 = time.time()
    print 'time for %s = %.2f' % (funcname(), t2 - t1)