Exemplo n.º 1
0
def testMask(sp, dtype):
    # Create mask
    f = ift.from_random('normal', sp).to_global_data()
    mask = np.zeros_like(f)
    mask[f > 0] = 1
    mask = ift.Field.from_global_data(sp, mask)
    # Test MaskOperator
    _check_repr(ift.MaskOperator(mask))
Exemplo n.º 2
0
def testMask(sp, dtype):
    # Create mask
    f = ift.from_random('normal', sp).to_global_data()
    mask = np.zeros_like(f)
    mask[f > 0] = 1
    mask = ift.Field.from_global_data(sp, mask)
    # Test MaskOperator
    op = ift.MaskOperator(mask)
    ift.extra.consistency_check(op, dtype, dtype)
Exemplo n.º 3
0
def test_value_inserter(sp, seed):
    np.random.seed(seed)
    ind = tuple([np.random.randint(0, ss - 1) for ss in sp.shape])
    op = ift.ValueInserter(sp, ind)
    f = ift.from_random('normal', ift.UnstructuredDomain((1,)))
    inp = f.to_global_data()[0]
    ret = op(f).to_global_data()
    assert_(ret[ind] == inp)
    assert_(np.sum(ret) == inp)
Exemplo n.º 4
0
def test_WF_curvature(space):
    np.random.seed(42)
    starting_point = ift.Field.from_random('normal', domain=space)*10
    required_result = ift.full(space, 1.)

    s = ift.Field.from_random('uniform', domain=space) + 0.5
    S = ift.DiagonalOperator(s)
    r = ift.Field.from_random('uniform', domain=space)
    R = ift.DiagonalOperator(r)
    n = ift.Field.from_random('uniform', domain=space) + 0.5
    N = ift.DiagonalOperator(n)
    all_diag = 1./s + r**2/n
    curv = ift.WienerFilterCurvature(R, N, S, iteration_controller=IC,
                                     iteration_controller_sampling=IC)
    m = curv.inverse(required_result)
    assert_allclose(
        m.local_data,
        1./all_diag.local_data,
        rtol=1e-3,
        atol=1e-3)
    curv.draw_sample()
    curv.draw_sample(from_inverse=True)

    if len(space.shape) == 1:
        R = ift.ValueInserter(space, [0])
        n = ift.from_random('uniform', R.domain) + 0.5
        N = ift.DiagonalOperator(n)
        all_diag = 1./s + R(1/n)
        curv = ift.WienerFilterCurvature(R.adjoint, N, S,
                                         iteration_controller=IC,
                                         iteration_controller_sampling=IC)
        m = curv.inverse(required_result)
        assert_allclose(
            m.local_data,
            1./all_diag.local_data,
            rtol=1e-3,
            atol=1e-3)
        curv.draw_sample()
        curv.draw_sample(from_inverse=True)
Exemplo n.º 5
0
def test_value(s):
    Regrid = ift.RegriddingOperator(s, s.shape)
    f = ift.from_random('normal', Regrid.domain)
    assert_allclose(f.to_global_data(), Regrid(f).to_global_data())
Exemplo n.º 6
0
def testOuter(fdomain, domain):
    f = ift.from_random('normal', fdomain)
    _check_repr(ift.OuterProduct(f, domain))
Exemplo n.º 7
0
def make_random_mask():
    # Random mask for spherical mode
    mask = ift.from_random('pm1', position_space)
    mask = (mask + 1) / 2
    return mask.to_global_data()
Exemplo n.º 8
0
def testOuter(fdomain, domain):
    f = ift.from_random('normal', fdomain)
    op = ift.OuterProduct(f, domain)
    ift.extra.consistency_check(op)
Exemplo n.º 9
0
def plot_prior_samples_2d(n_samps,
                          signal,
                          R,
                          correlated_field,
                          A,
                          likelihood,
                          N=None):
    samples, pspecmin, pspecmax = [], np.inf, 0
    pspec = A * A
    for _ in range(n_samps):
        ss = ift.from_random('normal', signal.domain)
        samples.append(ss)
        foo = pspec.force(ss).to_global_data()
        pspecmin = min([min(foo), pspecmin])
        pspecmax = max([max(foo), pspecmin])

    fig, ax = plt.subplots(nrows=n_samps,
                           ncols=5,
                           figsize=(2 * 5, 2 * n_samps))
    for ii, sample in enumerate(samples):
        cf = correlated_field(sample)
        signal_response = R @ signal
        sg = signal(sample)
        sr = (R.adjoint @ R @ signal)(sample)
        if likelihood == 'gauss':
            data = signal_response(sample) + N.draw_sample()
        elif likelihood == 'poisson':
            rate = signal_response(sample).to_global_data()
            data = ift.from_global_data(signal_response.target,
                                        np.random.poisson(rate))
        elif likelihood == 'bernoulli':
            rate = signal_response(sample).to_global_data()
            data = ift.from_global_data(signal_response.target,
                                        np.random.binomial(1, rate))
        else:
            raise ValueError('likelihood type not implemented')
        data = R.adjoint(data + 0.)

        As = pspec.force(sample)
        ax[ii, 0].plot(As.domain[0].k_lengths, As.to_global_data())
        ax[ii, 0].set_ylim(pspecmin, pspecmax)
        ax[ii, 0].set_yscale('log')
        ax[ii, 0].set_xscale('log')
        ax[ii, 0].get_xaxis().set_visible(False)

        ax[ii, 1].imshow(cf.to_global_data(), aspect='auto')
        ax[ii, 1].get_xaxis().set_visible(False)
        ax[ii, 1].get_yaxis().set_visible(False)

        ax[ii, 2].imshow(sg.to_global_data(), aspect='auto')
        ax[ii, 2].get_xaxis().set_visible(False)
        ax[ii, 2].get_yaxis().set_visible(False)

        ax[ii, 3].imshow(sr.to_global_data(), aspect='auto')
        ax[ii, 3].get_xaxis().set_visible(False)
        ax[ii, 3].get_yaxis().set_visible(False)

        ax[ii, 4].imshow(data.to_global_data(), cmap='viridis', aspect='auto')
        ax[ii, 4].get_xaxis().set_visible(False)
        ax[ii, 4].yaxis.tick_right()
        ax[ii, 4].get_yaxis().set_visible(False)

        if ii == 0:
            ax[0, 0].set_title('power spectrum')
            ax[0, 1].set_title('correlated field')
            ax[0, 2].set_title('signal')
            ax[0, 3].set_title('signal Response')
            ax[0, 4].set_title('synthetic data')

        ax[n_samps - 1, 0].get_xaxis().set_visible(True)
    plt.tight_layout()
    plt.savefig('prior_samples_{}.png'.format(likelihood))
    plt.close('all')
Exemplo n.º 10
0
def testBinary(type1, type2, space, seed):
    dom1 = ift.MultiDomain.make({'s1': space})
    dom2 = ift.MultiDomain.make({'s2': space})

    # FIXME Remove this?
    _make_linearization(type1, dom1, seed)
    _make_linearization(type2, dom2, seed)

    dom = ift.MultiDomain.union((dom1, dom2))
    select_s1 = ift.ducktape(None, dom1, "s1")
    select_s2 = ift.ducktape(None, dom2, "s2")
    model = select_s1 * select_s2
    pos = ift.from_random("normal", dom)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = select_s1 + select_s2
    pos = ift.from_random("normal", dom)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = select_s1.scale(3.)
    pos = ift.from_random("normal", dom1)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = ift.ScalingOperator(2.456, space)(select_s1 * select_s2)
    pos = ift.from_random("normal", dom)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = ift.sigmoid(2.456 * (select_s1 * select_s2))
    pos = ift.from_random("normal", dom)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    pos = ift.from_random("normal", dom)
    model = ift.OuterProduct(pos['s1'], ift.makeDomain(space))
    ift.extra.check_jacobian_consistency(model, pos['s2'], ntries=20)
    model = select_s1**2
    pos = ift.from_random("normal", dom1)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    model = select_s1.clip(-1, 1)
    pos = ift.from_random("normal", dom1)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    f = ift.from_random("normal", space)
    model = select_s1.clip(f - 0.1, f + 1.)
    pos = ift.from_random("normal", dom1)
    ift.extra.check_jacobian_consistency(model, pos, ntries=20)
    if isinstance(space, ift.RGSpace):
        model = ift.FFTOperator(space)(select_s1 * select_s2)
        pos = ift.from_random("normal", dom)
        ift.extra.check_jacobian_consistency(model, pos, ntries=20)
Exemplo n.º 11
0
    mode = 2
    if mode == 0:
        # One-dimensional regular grid
        position_space = ift.RGSpace(1024)
    elif mode == 1:
        # Two-dimensional regular grid
        position_space = ift.RGSpace([512, 512])
    else:
        # Sphere
        position_space = ift.HPSpace(128)

    # Define harmonic space and transform
    harmonic_space = position_space.get_default_codomain()
    HT = ift.HarmonicTransformOperator(harmonic_space, position_space)

    position = ift.from_random('normal', harmonic_space)

    # Define power spectrum and amplitudes
    def sqrtpspec(k):
        return 1. / (20. + k**2)

    A = ift.create_power_operator(harmonic_space, sqrtpspec)

    # Set up a sky operator and instrumental response
    sky = ift.sigmoid(HT(A))
    GR = ift.GeometryRemover(position_space)
    R = GR

    # Generate mock data
    p = R(sky)
    mock_position = ift.from_random('normal', harmonic_space)
Exemplo n.º 12
0
def test_func():
    f1 = ift.from_random("normal", domain=dom, dtype=np.complex128)
    assert_allclose(
        ift.log(ift.exp((f1)))["d1"].local_data, f1["d1"].local_data)
Exemplo n.º 13
0
def test_vdot():
    f1 = ift.from_random("normal", domain=dom, dtype=np.complex128)
    f2 = ift.from_random("normal", domain=dom, dtype=np.complex128)
    assert_allclose(f1.vdot(f2), np.conj(f2.vdot(f1)))
Exemplo n.º 14
0
    # Apply a nonlinearity
    signal = ift.sigmoid(correlated_field)

    # Build the line-of-sight response and define signal response
    LOS_starts, LOS_ends = random_los(100) if mode == 0 else radial_los(100)
    R = ift.LOSResponse(position_space, starts=LOS_starts, ends=LOS_ends)
    signal_response = R(signal)

    # Specify noise
    data_space = R.target
    noise = .001
    N = ift.ScalingOperator(noise, data_space)

    # Generate mock signal and data
    mock_position = ift.from_random('normal', signal_response.domain)
    data = signal_response(mock_position) + N.draw_sample()

    # Minimization parameters
    ic_sampling = ift.GradientNormController(iteration_limit=100)
    ic_newton = ift.GradInfNormController(
        name='Newton', tol=1e-7, iteration_limit=35)
    minimizer = ift.NewtonCG(ic_newton)

    # Set up likelihood and information Hamiltonian
    likelihood = ift.GaussianEnergy(mean=data, covariance=N)(signal_response)
    H = ift.StandardHamiltonian(likelihood, ic_sampling)

    initial_mean = ift.MultiField.full(H.domain, 0.)
    mean = initial_mean
Exemplo n.º 15
0
    pd = ift.PowerDistributor(harmonic_space, p_space)
    a = ift.PS_field(p_space, sqrtpspec)
    A = pd(a)

    # Define sky operator
    sky = ift.exp(HT(ift.makeOp(A)))

    M = ift.DiagonalOperator(exposure)
    GR = ift.GeometryRemover(position_space)
    # Define instrumental response
    R = GR(M)

    # Generate mock data and define likelihood operator
    d_space = R.target[0]
    lamb = R(sky)
    mock_position = ift.from_random('normal', domain)
    data = lamb(mock_position)
    data = np.random.poisson(data.to_global_data().astype(np.float64))
    data = ift.Field.from_global_data(d_space, data)
    likelihood = ift.PoissonianEnergy(data)(lamb)

    # Settings for minimization
    ic_newton = ift.DeltaEnergyController(name='Newton',
                                          iteration_limit=100,
                                          tol_rel_deltaE=1e-8)
    minimizer = ift.NewtonCG(ic_newton)

    # Compute MAP solution by minimizing the information Hamiltonian
    H = ift.StandardHamiltonian(likelihood)
    initial_position = ift.from_random('normal', domain)
    H = ift.EnergyAdapter(initial_position, H, want_metric=True)