def test_power_synthesize_analyze(space1, space2): np.random.seed(11) p1 = ift.PowerSpace(space1) fp1 = ift.PS_field(p1, _spec1) p2 = ift.PowerSpace(space2) fp2 = ift.PS_field(p2, _spec2) outer = np.outer(fp1.to_global_data(), fp2.to_global_data()) fp = ift.Field.from_global_data((p1, p2), outer) op1 = ift.create_power_operator((space1, space2), _spec1, 0) op2 = ift.create_power_operator((space1, space2), _spec2, 1) opfull = op2(op1) samples = 500 sc1 = ift.StatCalculator() sc2 = ift.StatCalculator() for ii in range(samples): sk = opfull.draw_sample() sp = ift.power_analyze(sk, spaces=(0, 1), keep_phase_information=False) sc1.add(sp.sum(spaces=1) / fp2.sum()) sc2.add(sp.sum(spaces=0) / fp1.sum()) assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2) assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)
def test_gaussian_energy(space, nonlinearity, noise, seed): np.random.seed(seed) dim = len(space.shape) hspace = space.get_default_codomain() ht = ift.HarmonicTransformOperator(hspace, target=space) binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False) pspace = ift.PowerSpace(hspace, binbounds=binbounds) Dist = ift.PowerDistributor(target=hspace, power_space=pspace) xi0 = ift.Field.from_random(domain=hspace, random_type='normal') def pspec(k): return 1 / (1 + k**2)**dim pspec = ift.PS_field(pspace, pspec) A = Dist(ift.sqrt(pspec)) N = ift.ScalingOperator(noise, space) n = N.draw_sample() R = ift.ScalingOperator(10., space) def d_model(): if nonlinearity == "": return R(ht(ift.makeOp(A))) else: tmp = ht(ift.makeOp(A)) nonlin = getattr(tmp, nonlinearity)() return R(nonlin) d = d_model()(xi0) + n if noise == 1: N = None energy = ift.GaussianEnergy(d, N)(d_model()) ift.extra.check_jacobian_consistency(energy, xi0, ntries=10, tol=5e-8)
def test_DiagonalOperator_power_analyze2(space1, space2): np.random.seed(11) fp1 = ift.PS_field(ift.PowerSpace(space1), _spec1) fp2 = ift.PS_field(ift.PowerSpace(space2), _spec2) S_1 = ift.create_power_operator((space1, space2), _spec1, 0) S_2 = ift.create_power_operator((space1, space2), _spec2, 1) S_full = S_2(S_1) samples = 500 sc1 = ift.StatCalculator() sc2 = ift.StatCalculator() for ii in range(samples): sk = S_full.draw_sample() sp = ift.power_analyze(sk, spaces=(0, 1), keep_phase_information=False) sc1.add(sp.sum(spaces=1) / fp2.sum()) sc2.add(sp.sum(spaces=0) / fp1.sum()) assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2) assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)
# Harmonic transform from harmonic space to position space HT = ift.HarmonicTransformOperator(harmonic_space, target=position_space) # Set prior correlation covariance with a power spectrum leading to # homogeneous and isotropic statistics def power_spectrum(k): return 100. / (20. + k**3) # 1D spectral space on which the power spectrum is defined power_space = ift.PowerSpace(harmonic_space) # Mapping to (higher dimensional) harmonic space PD = ift.PowerDistributor(harmonic_space, power_space) # Apply the mapping prior_correlation_structure = PD(ift.PS_field(power_space, power_spectrum)) # Insert the result into the diagonal of an harmonic space operator S = ift.DiagonalOperator(prior_correlation_structure) # S is the prior field covariance # Build instrument response consisting of a discretization, mask # and harmonic transformaion # Data is defined on a geometry-free space, thus the geometry is removed GR = ift.GeometryRemover(position_space) # Masking operator to model that parts of the field have not been observed mask = ift.Field.from_global_data(position_space, mask) Mask = ift.DiagonalOperator(mask)
exposure = ift.Field.full(position_space, 100.) # Define harmonic space and harmonic transform harmonic_space = position_space.get_default_codomain() HT = ift.HarmonicTransformOperator(harmonic_space, position_space) # Domain on which the field's degrees of freedom are defined domain = ift.DomainTuple.make(harmonic_space) # Define amplitude (square root of power spectrum) def sqrtpspec(k): return 1. / (20. + k**2) p_space = ift.PowerSpace(harmonic_space) 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)