コード例 #1
0
ファイル: test_kobs.py プロジェクト: aguinot/ngmix
def test_kobs():
    rng = np.random.RandomState(seed=11)

    with pytest.raises(ValueError):
        KObservation(kimage={})

    with pytest.raises(ValueError):
        KObservation(kimage=galsim.ImageD(rng.normal(size=(11, 13)), scale=0.3))

    with pytest.raises(AssertionError):
        KObservation(
            kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3),
            weight={},
        )

    with pytest.raises(ValueError):
        KObservation(
            kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3),
            weight=galsim.ImageD(rng.normal(size=(11, 12)), scale=0.3),
        )

    obs = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3))
    assert np.all(obs.weight.array == 1)
    assert obs.weight.array.shape == (11, 13)
    assert not obs.has_psf()

    psf_obs = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3))
    obs.set_psf(psf_obs)
    assert obs.has_psf()
    assert obs.psf is psf_obs
    obs.set_psf(None)
    assert not obs.has_psf()

    with pytest.raises(AssertionError):
        obs.set_psf({})

    psf_obs = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 12)), scale=0.3))
    with pytest.raises(ValueError):
        obs.set_psf(psf_obs)

    psf_obs = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.4))
    with pytest.raises(AssertionError):
        obs.set_psf(psf_obs)

    assert isinstance(obs.jacobian, DiagonalJacobian)
    assert np.allclose(obs.jacobian.scale, 0.3)
    assert np.array_equal(obs.jacobian.cen, [5, 6])

    obs = KObservation(kimage=galsim.ImageCD(rng.normal(size=(10, 12)), scale=0.3))
    assert np.array_equal(obs.jacobian.cen, [5, 6])

    obs = KObservation(kimage=galsim.ImageCD(rng.normal(size=(7, 12)), scale=0.3))
    assert np.array_equal(obs.jacobian.cen, [3, 6])

    obs = KObservation(kimage=galsim.ImageCD(rng.normal(size=(6, 11)), scale=0.3))
    assert np.array_equal(obs.jacobian.cen, [3, 5])
コード例 #2
0
ファイル: test_kobs.py プロジェクト: aguinot/ngmix
def test_kobslist():
    rng = np.random.RandomState(seed=11)
    obs1 = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3))
    obs2 = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3))

    kobslist = KObsList()
    kobslist.append(obs1)
    assert np.all(kobslist[0].kimage.array == obs1.kimage.array)
    kobslist.append(obs2)
    assert np.all(kobslist[0].kimage.array == obs1.kimage.array)
    assert np.all(kobslist[1].kimage.array == obs2.kimage.array)
    kobslist[1] = obs1
    assert np.all(kobslist[1].kimage.array == obs1.kimage.array)

    with pytest.raises(AssertionError):
        kobslist.append(None)

    with pytest.raises(AssertionError):
        kobslist[0] = None
コード例 #3
0
 def hparams(self, defaults, model_hparams):
   p = defaults
   p.pixel_scale = 0.168
   p.img_len = 64
   p.example_per_shard = 1000
   p.modality = {"inputs": modalities.ModalityType.IDENTITY,
                 "targets": modalities.ModalityType.IDENTITY}
   p.vocab_size = {"inputs": None,
                   "targets": None}
   p.psf = galsim.InterpolatedKImage(galsim.ImageCD(fits.getdata(os.path.join(_COSMOS_DATA_DIR, 'hsc_hann_window.fits'))+0j, scale=0.2921868167401221))
   p.rotation = True
コード例 #4
0
ファイル: test_kobs.py プロジェクト: aguinot/ngmix
def test_get_kmb_obs():
    rng = np.random.RandomState(seed=11)
    obs = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3))
    mbobs = get_kmb_obs(obs)
    rng = np.random.RandomState(seed=11)
    assert np.all(mbobs[0][0].kimage.array == rng.normal(size=(11, 13)))
    assert len(mbobs) == 1
    assert len(mbobs[0]) == 1

    rng = np.random.RandomState(seed=12)
    obslist = KObsList()
    for _ in range(3):
        obslist.append(KObservation(
            kimage=galsim.ImageCD(rng.normal(size=(12, 17)), scale=0.3)))
    mbobs = get_kmb_obs(obslist)
    rng = np.random.RandomState(seed=12)
    for i in range(3):
        assert np.all(mbobs[0][i].kimage.array == rng.normal(size=(12, 17)))
    assert len(mbobs) == 1
    assert len(mbobs[0]) == 3

    rng = np.random.RandomState(seed=13)
    mbobs = KMultiBandObsList()
    for _ in range(5):
        obslist = KObsList()
        for _ in range(3):
            obslist.append(KObservation(
                kimage=galsim.ImageCD(rng.normal(size=(13, 15)), scale=0.3)))
        mbobs.append(obslist)

    new_mbobs = get_kmb_obs(mbobs)
    rng = np.random.RandomState(seed=13)
    for obslist in new_mbobs:
        for obs in obslist:
            assert np.all(obs.kimage.array == rng.normal(size=(13, 15)))

    with pytest.raises(ValueError):
        get_kmb_obs(None)
コード例 #5
0
ファイル: test_kobs.py プロジェクト: aguinot/ngmix
def test_kmbobslist():
    rng = np.random.RandomState(seed=11)
    obs1 = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3))
    obs2 = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3))
    obs3 = KObservation(kimage=galsim.ImageCD(rng.normal(size=(11, 13)), scale=0.3))
    kobslist1 = KObsList()
    kobslist1.append(obs1)
    kobslist1.append(obs2)
    kobslist2 = KObsList()
    kobslist2.append(obs3)

    kmbobs = KMultiBandObsList()
    kmbobs.append(kobslist1)
    assert all(
        np.all(obs.kimage.array == _obs.kimage.array)
        for obs, _obs in zip(kmbobs[0], kobslist1)
    )
    kmbobs.append(kobslist2)
    assert all(
        np.all(obs.kimage.array == _obs.kimage.array)
        for obs, _obs in zip(kmbobs[0], kobslist1)
    )
    assert all(
        np.all(obs.kimage.array == _obs.kimage.array)
        for obs, _obs in zip(kmbobs[1], kobslist2)
    )
    kmbobs[1] = kobslist1
    assert all(
        np.all(obs.kimage.array == _obs.kimage.array)
        for obs, _obs in zip(kmbobs[1], kobslist1)
    )

    with pytest.raises(AssertionError):
        kmbobs.append(None)

    with pytest.raises(AssertionError):
        kmbobs[0] = None
コード例 #6
0
ファイル: test_transforms.py プロジェクト: sweverett/GalSim
def test_compound():
    """Check that transformations of transformations work the same whether they are compounded
    automatically or not.
    """
    gal1 = galsim.Gaussian(fwhm=1.7, flux=2.3)
    gal2 = gal1.shear(g1=0.21, g2=0.12).rotate(33 * galsim.degrees).shift(0.1,0.4) * 1.1
    gal3 = gal2.shear(g1=0.18, g2=0.09).rotate(12 * galsim.degrees).shift(-0.3,0.5) * 8.9
    # These should have compounded automatically into a single transformation
    print('gal3 = ',gal3)
    print('gal3.original = ',gal3.original)
    assert gal3.original == gal1

    gal4 = gal2 + 0. * gal1  # Equivalent to gal2, but the sum kills the automatic compounding.
    gal5 = gal4.shear(g1=0.18, g2=0.09).rotate(12 * galsim.degrees).shift(-0.3,0.5) * 8.9
    print('gal5 = ',gal5)
    print('gal5.original = ',gal5.original)
    assert gal5.original != gal1
    assert gal5.original == gal4

    # Despite that, the gal3 and gal5 should draw the same in both real and k-space
    im3_d = galsim.ImageD(8,8)
    im5_d = galsim.ImageD(8,8)
    im3_f = galsim.ImageF(8,8)
    im5_f = galsim.ImageF(8,8)
    im3_cd = galsim.ImageCD(8,8)
    im5_cd = galsim.ImageCD(8,8)
    im3_cf = galsim.ImageCF(8,8)
    im5_cf = galsim.ImageCF(8,8)

    # Note: these are not equal.  gal5 lost track of the overall transformation and thinks it
    # needs a bit larger maxk, smaller stepk.  ~10% different.
    print('gal3.stepk = ',gal3.stepk)
    print('gal5.stepk = ',gal5.stepk)
    print('gal3.maxk = ',gal3.maxk)
    print('gal5.maxk = ',gal5.maxk)

    gal3.drawImage(image=im3_d, method='sb', scale=0.2)
    gal5.drawImage(image=im5_d, method='sb', scale=0.2)
    np.testing.assert_almost_equal(im3_d[1,1], gal3.xValue(-0.7,-0.7), decimal=12)
    np.testing.assert_almost_equal(im5_d[1,1], gal3.xValue(-0.7,-0.7), decimal=12)
    np.testing.assert_almost_equal(im3_d.array[0,0], gal3.xValue(-0.7,-0.7), decimal=12)
    np.testing.assert_almost_equal(im5_d.array[0,0], gal3.xValue(-0.7,-0.7), decimal=12)
    np.testing.assert_array_almost_equal(im3_d.array, im5_d.array, decimal=12)

    gal3.drawImage(image=im3_f, method='sb', scale=0.2)
    gal5.drawImage(image=im5_f, method='sb', scale=0.2)
    np.testing.assert_almost_equal(im3_f[1,1], gal3.xValue(-0.7,-0.7), decimal=4)
    np.testing.assert_almost_equal(im5_f[1,1], gal3.xValue(-0.7,-0.7), decimal=4)
    np.testing.assert_almost_equal(im3_f.array, im5_f.array, decimal=4)
    np.testing.assert_almost_equal(im3_f.array, im3_d.array, decimal=4)
    np.testing.assert_almost_equal(im5_f.array, im5_d.array, decimal=4)

    gal3.drawKImage(image=im3_cd, scale=0.5)
    gal5.drawKImage(image=im5_cd, scale=0.5)
    np.testing.assert_almost_equal(im3_cd[-4,-4], gal3.kValue(-2.,-2.), decimal=12)
    np.testing.assert_almost_equal(im5_cd[-4,-4], gal3.kValue(-2.,-2.), decimal=12)
    np.testing.assert_almost_equal(im3_cd.array[0,0], gal3.kValue(-2.,-2.), decimal=12)
    np.testing.assert_almost_equal(im5_cd.array[0,0], gal3.kValue(-2.,-2.), decimal=12)
    np.testing.assert_array_almost_equal(im3_cd.array, im5_cd.array, decimal=12)

    gal3.drawKImage(image=im3_cf, scale=0.5)
    gal5.drawKImage(image=im5_cf, scale=0.5)
    np.testing.assert_almost_equal(im3_cf[-4,-4], gal3.kValue(-2.,-2.), decimal=3)
    np.testing.assert_almost_equal(im5_cf[-4,-4], gal3.kValue(-2.,-2.), decimal=3)
    np.testing.assert_array_almost_equal(im3_cf.array, im5_cf.array, decimal=3)
    np.testing.assert_array_almost_equal(im3_cf.array, im3_cd.array, decimal=3)
    np.testing.assert_array_almost_equal(im5_cf.array, im5_cd.array, decimal=3)
コード例 #7
0
def test_simple():
    """Test the default Sensor class that acts basically like not passing any sensor object.
    """

    # Start with photon shooting, since that's the most typical way that sensors are used.
    obj = galsim.Gaussian(flux=10000, sigma=1.3)

    # We'll draw the same object using SiliconSensor, Sensor, and the default (sensor=None)
    im1 = galsim.ImageD(64, 64, scale=0.3)  # Refefence image with sensor=None
    im2 = galsim.ImageD(64, 64, scale=0.3)  # Use sensor=simple

    rng1 = galsim.BaseDeviate(5678)
    rng2 = galsim.BaseDeviate(5678)

    simple = galsim.Sensor()

    # Start with photon shooting, since that's more straightforward.
    obj.drawImage(im1, method='phot', poisson_flux=False, rng=rng1)
    obj.drawImage(im2, method='phot', poisson_flux=False, sensor=simple, rng=rng2)

    # Should be exactly equal
    np.testing.assert_array_equal(im2.array, im1.array)

    # Fluxes should all equal obj.flux
    np.testing.assert_almost_equal(im1.array.sum(), obj.flux, decimal=6)
    np.testing.assert_almost_equal(im2.array.sum(), obj.flux, decimal=6)
    np.testing.assert_almost_equal(im1.added_flux, obj.flux, decimal=6)
    np.testing.assert_almost_equal(im2.added_flux, obj.flux, decimal=6)

    # Now test fft drawing, which is more complicated with possible temporaries and subsampling.

    im1 = galsim.ImageD(64, 64, scale=0.3)  # Reference image with sensor=None
    im2 = galsim.ImageD(64, 64, scale=0.3)  # Use sensor=simple
    im3 = galsim.ImageD(64, 64, scale=0.3)  # Use sensor=simple, no subsampling
    im4 = galsim.ImageCD(64, 64, scale=0.3) # Equivalent to image2, but checks using a temporary.
                                            # Also check add_to_image=True with im5.
    im5 = galsim.ImageD(64, 64, scale=0.3)  # Check manually convolving by the pixel.
    im6 = galsim.ImageD(64, 64, scale=0.3)  # Check manually convolving by the pixel, n_subsample=1

    # The rng shouldn't matter anymore for these, so just use the default rng=None
    obj.drawImage(im1, method='fft')
    obj.drawImage(im2, method='fft', sensor=simple)
    obj.drawImage(im3, method='fft', sensor=simple, n_subsample=1)
    obj.drawImage(im4, method='fft', sensor=simple, add_to_image=True)

    obj_with_pixel = galsim.Convolve(obj, galsim.Pixel(0.3))
    obj_with_pixel.drawImage(im5, method='no_pixel', sensor=simple)
    obj_with_pixel.drawImage(im6, method='no_pixel', sensor=simple, n_subsample=1)

    # Fluxes should all equal obj.flux
    np.testing.assert_almost_equal(im1.array.sum(), obj.flux, decimal=3)
    np.testing.assert_almost_equal(im2.array.sum(), obj.flux, decimal=3)
    np.testing.assert_almost_equal(im3.array.sum(), obj.flux, decimal=3)
    np.testing.assert_almost_equal(im4.array.sum(), obj.flux, decimal=3)
    np.testing.assert_almost_equal(im5.array.sum(), obj.flux, decimal=3)
    np.testing.assert_almost_equal(im6.array.sum(), obj.flux, decimal=3)
    np.testing.assert_almost_equal(im1.added_flux, obj.flux, decimal=3)
    np.testing.assert_almost_equal(im2.added_flux, obj.flux, decimal=3)
    np.testing.assert_almost_equal(im3.added_flux, obj.flux, decimal=3)
    np.testing.assert_almost_equal(im4.added_flux, obj.flux, decimal=3)
    np.testing.assert_almost_equal(im5.added_flux, obj.flux, decimal=3)
    np.testing.assert_almost_equal(im6.added_flux, obj.flux, decimal=3)

    # im1 and im2 are not precisely equal, since im2 was made with subsampling and then binning,
    # but with a largish object relative to the pixel, it's very close. (cf. similar test below
    # in test_silicon_fft, where the agreement is not so good.)
    print('max diff between im1, im2 with fft = ',np.max(np.abs(im2.array-im1.array)))
    np.testing.assert_almost_equal(im2.array, im1.array, decimal=10)

    # With no subsampling it should be nearly perfect (although this would be expected to be worse
    # when done with a real Sensor model).
    print('max diff without subsampling = ',np.max(np.abs(im3.array-im1.array)))
    np.testing.assert_almost_equal(im3.array, im1.array, decimal=12)

    # Using a temporary (and add_to_image) shouldn't affect anything for the D -> CD case.
    print('max diff with temporary = ',np.max(np.abs(im4.array-im2.array)))
    np.testing.assert_almost_equal(im4.array.real, im2.array, decimal=12)

    # Manual convolution should be identical to what 'fft' does automatically.
    print('max diff with manual pixel conv = ',np.max(np.abs(im5.array-im2.array)))
    #np.testing.assert_almost_equal(im5.array, im2.array, decimal=12)
    print('max diff with manual pixel conv, no subsampling = ',np.max(np.abs(im6.array-im3.array)))
    np.testing.assert_almost_equal(im6.array, im3.array, decimal=12)

    do_pickle(simple)