Beispiel #1
0
def test_measures():
    f1 = filters.GaussianFilter(radius=5)
    fh = FieldHandler(Ndim=3, Lbox=15, dimensions=16, Pk=(k, Pk), filter=f1)
    dx = fh.Lbox / fh.dimensions
    sfield = fh.get_smoothed(f1)

    # Test density measurement
    c = C.DensityConstrain([8, 8, 8], filter=f1, value=1, field_handler=fh)
    val = c.measure()
    tgt = sfield[8, 8, 8]
    assert_allclose(val, tgt)

    # Test gradient measurement
    c = C.GradientConstrain([8, 8, 8],
                            filter=f1,
                            value=[0, 0, 0],
                            field_handler=fh)
    val = c.measure()
    tgt = np.array(np.gradient(sfield, dx))[:, 8, 8, 8]
    assert_allclose(val, tgt)

    # Test hessian measurement
    c = C.HessianConstrain([8, 8, 8],
                           filter=f1,
                           value=[0] * 6,
                           field_handler=fh)
    val = c.measure()
    tgt = np.array(np.gradient(np.gradient(sfield, dx), dx,
                               axis=(-3, -2, -1)))[:, :, 8, 8, 8]
    assert_allclose(val, tgt)
Beispiel #2
0
def test_xi():
    positions = np.random.rand(10, 3) * 20

    # Build reference
    c = Correlator(quiet=True)
    c.add_point([0, 0, 0], ["hessian"], 5)
    for x in positions:
        c.add_point(x, ["delta"], 5)
    ref = c.cov[:6, 6:]

    # Build answer
    WG = filters.GaussianFilter(radius=5)
    fh = FieldHandler(Ndim=3,
                      Lbox=2,
                      dimensions=128,
                      filter=WG,
                      Pk=(c.k, c.Pk))
    c_hess = C.HessianConstrain([0, 0, 0],
                                filter=WG,
                                field_handler=fh,
                                value=None)

    order = [0, 3, 5, 1, 2, 4]
    ans = (c_hess.xi(positions) / (fh.sigma(0) * fh.sigma(2)))[:, 0, order].T

    # Check
    assert_allclose(ref, ans, rtol=5e-2)
Beispiel #3
0
def test_field_sigma():
    fh = FieldHandler(Ndim=3, Lbox=100, dimensions=128, Pk=(k, Pk))

    smoothing_scales = np.geomspace(5, 10, 20)
    sigma_th = [fh.sigma(0, filters.TopHatFilter(R)) for R in smoothing_scales]
    sigma_exp = [
        fh.get_smoothed(filters.TopHatFilter(R)).std() for R in smoothing_scales
    ]

    # Check the field variance
    assert_allclose(sigma_th, sigma_exp, rtol=5e-2)
Beispiel #4
0
def test_constrain_correlation_nolag():
    f1 = filters.GaussianFilter(radius=5)
    fh = FieldHandler(Ndim=3, Lbox=50, dimensions=16, Pk=(k, Pk), filter=f1)
    f2 = filters.GaussianFilter(radius=6)
    f3 = filters.GaussianFilter(radius=7)
    f4 = filters.GaussianFilter(radius=8)

    X1 = np.array([0, 0, 0])

    c1 = C.DensityConstrain(X1, filter=f1, value=1.69, field_handler=fh)
    c2 = C.GradientConstrain(X1, filter=f2, value=[0, 0, 0], field_handler=fh)
    c3 = C.HessianConstrain(X1,
                            filter=f3,
                            value=[0, 0, 0, 0, 0, 0],
                            field_handler=fh)
    c4 = C.ThirdDerivativeConstrain(X1,
                                    filter=f4,
                                    value=list(range(10)),
                                    field_handler=fh)

    fh.add_constrain(c1)
    fh.add_constrain(c2)
    fh.add_constrain(c3)
    fh.add_constrain(c4)

    cov = fh.compute_covariance()

    # Check positive definite
    evals = np.linalg.eigvalsh(cov)
    assert_array_less(np.zeros_like(evals), evals)

    # Check symmetry
    assert_allclose(cov, cov.T)
Beispiel #5
0
def test_constrain_initialisation():
    filter = filters.GaussianFilter(radius=5)
    fh = FieldHandler(Ndim=3,
                      Lbox=50,
                      dimensions=16,
                      Pk=(k, Pk),
                      filter=filter)
    c = C.Constrain(position=[0, 0, 0],
                    filter=filter,
                    value=1.69,
                    field_handler=fh)
    c
Beispiel #6
0
def test_sigma():
    filt = filters.GaussianFilter(radius=5)
    fh = FieldHandler(Ndim=3, Lbox=50, dimensions=16, Pk=(k, Pk), filter=filt)

    def test_sigma_n(i):
        got = fh.sigma(i)
        expected = np.sqrt(
            np.trapz(k ** (2 + 2 * i) * Pk * filt.W(k) ** 2, k) / (2 * np.pi ** 2)
        )

        assert_allclose(got, expected)

    for i in range(-5, 5):
        yield test_sigma_n, i
Beispiel #7
0
    def test_it(use_cache):

        # Note: here we use the old package's k and Pk so that their result agree can be
        # be compared
        fh = FieldHandler(
            Ndim=3,
            Lbox=50,
            dimensions=16,
            Pk=(c.k, c.Pk),
            use_covariance_cache=use_cache,
            filter=f1,
        )

        c1 = C.DensityConstrain(X1, filter=f1, value=1.69, field_handler=fh)
        c2 = C.GradientConstrain(X2,
                                 filter=f2,
                                 value=[0, 0, 0],
                                 field_handler=fh)
        c3 = C.HessianConstrain(X3,
                                filter=f3,
                                value=[0, 0, 0, 0, 0, 0],
                                field_handler=fh)
        fh.add_constrain(c1)
        fh.add_constrain(c2)
        fh.add_constrain(c3)

        new = fh.compute_covariance() / S

        # Check closeness (note: the order may be different so only
        # check det and eigenvalues)
        det_ref = np.linalg.det(ref)
        det_new = np.linalg.det(new)

        assert_allclose(det_ref, det_new)

        eval_ref = np.linalg.eigvalsh(ref)
        eval_new = np.linalg.eigvalsh(new)

        assert_allclose(eval_ref, eval_new)
Beispiel #8
0
def test_filtering():
    N = 64
    v0 = 10
    fh = FieldHandler(Ndim=3, Lbox=N, dimensions=N, Pk=(k, Pk))
    fh.white_noise[:] = 0
    fh.white_noise[N // 2, N // 2, N // 2] = v0
    fh.white_noise_fft = np.fft.rfftn(fh.white_noise)

    R = 10
    field = fh.get_smoothed(R)
    x, y, z = (
        np.array(np.meshgrid(*[np.linspace(0, N - 1, N)] * 3, indexing="ij")) - N // 2
    )
    ref = (
        1
        / (2 * np.pi * R ** 2) ** (3 / 2)
        * np.exp(-(x ** 2 + y ** 2 + z ** 2) / 2 / R ** 2)
        * v0
    )

    assert_allclose(field, ref, atol=1e-5)