def testModelLibrary(space, seed): # Tests amplitude model and coorelated field model np.random.seed(seed) domain = ift.PowerSpace(space.get_default_codomain()) model = ift.SLAmplitude(target=domain, n_pix=4, a=.5, k0=2, sm=3, sv=1.5, im=1.75, iv=1.3) assert_(isinstance(model, ift.Operator)) S = ift.ScalingOperator(1., model.domain) pos = S.draw_sample() ift.extra.check_jacobian_consistency(model, pos, ntries=20) model2 = ift.CorrelatedField(space, model) S = ift.ScalingOperator(1., model2.domain) pos = S.draw_sample() ift.extra.check_jacobian_consistency(model2, pos, ntries=20) domtup = ift.DomainTuple.make((space, space)) model3 = ift.MfCorrelatedField(domtup, [model, model]) S = ift.ScalingOperator(1., model3.domain) pos = S.draw_sample() ift.extra.check_jacobian_consistency(model3, pos, ntries=20)
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_mix(space1): op1 = ift.makeOp(ift.Field.full(space1, 2.)) op2 = ift.ScalingOperator(3., space1) full_op = op1(op2 + op2)(op1)(op1) - op1(op2) x = ift.Field.full(space1, 1.) res = full_op(x) assert_equal(isinstance(full_op, ift.DiagonalOperator), True) assert_allclose(res.local_data, 42.)
def testPointModel(space, seed): S = ift.ScalingOperator(1., space) pos = S.draw_sample() alpha = 1.5 q = 0.73 model = ift.InverseGammaOperator(space, alpha, q) # FIXME All those cdfs and ppfs are not very accurate ift.extra.check_jacobian_consistency(model, pos, tol=1e-2, ntries=20)
def _make_linearization(type, space, seed): np.random.seed(seed) S = ift.ScalingOperator(1., space) s = S.draw_sample() if type == "Constant": return ift.Linearization.make_const(s) elif type == "Variable": return ift.Linearization.make_var(s) raise ValueError('unknown type passed')
def test_hamiltonian_and_KL(field): field = field.exp() space = field.domain lh = ift.GaussianEnergy(domain=space) hamiltonian = ift.StandardHamiltonian(lh) ift.extra.check_jacobian_consistency(hamiltonian, field) S = ift.ScalingOperator(1., space) samps = [S.draw_sample() for i in range(3)] kl = ift.AveragedEnergy(hamiltonian, samps) ift.extra.check_jacobian_consistency(kl, field)
def test_blockdiagonal(): op = ift.BlockDiagonalOperator( dom, {"d1": ift.ScalingOperator(20., dom["d1"])}) op2 = op(op) ift.extra.consistency_check(op2) assert_equal(type(op2), ift.BlockDiagonalOperator) f1 = op2(ift.full(dom, 1)) for val in f1.values(): assert_equal((val == 400).all(), True) op2 = op + op assert_equal(type(op2), ift.BlockDiagonalOperator) f1 = op2(ift.full(dom, 1)) for val in f1.values(): assert_equal((val == 40).all(), True)
def testDynamicModel(target, causal, minimum_phase, seed): dct = { 'target': target, 'harmonic_padding': None, 'sm_s0': 3., 'sm_x0': 1., 'key': 'f', 'causal': causal, 'minimum_phase': minimum_phase } model, _ = ift.dynamic_operator(**dct) S = ift.ScalingOperator(1., model.domain) pos = S.draw_sample() # FIXME I dont know why smaller tol fails for 3D example ift.extra.check_jacobian_consistency(model, pos, tol=1e-5, ntries=20) if len(target.shape) > 1: dct = { 'target': target, 'harmonic_padding': None, 'sm_s0': 3., 'sm_x0': 1., 'key': 'f', 'lightcone_key': 'c', 'sigc': 1., 'quant': 5, 'causal': causal, 'minimum_phase': minimum_phase } dct['lightcone_key'] = 'c' dct['sigc'] = 1. dct['quant'] = 5 model, _ = ift.dynamic_lightcone_operator(**dct) S = ift.ScalingOperator(1., model.domain) pos = S.draw_sample() # FIXME I dont know why smaller tol fails for 3D example ift.extra.check_jacobian_consistency(model, pos, tol=1e-5, ntries=20)
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)
'iv': 2. # y-intercept variance } A = ift.SLAmplitude(**dct) correlated_field = ift.CorrelatedField(position_space, A) ### SETTING UP SPECIFIC SCENARIO #### R = ift.GeometryRemover(position_space) data_space = R.target signal_response = R(correlated_field) # Set up likelihood and load data N = ift.ScalingOperator(0.1, data_space) data, ground_truth = generate_mysterious_data(position_space) data = ift.from_global_data(data_space, data) likelihood = ift.GaussianEnergy(mean=data, inverse_covariance=N.inverse)(signal_response) #### SOLVING PROBLEM #### ic_sampling = ift.GradientNormController(iteration_limit=100) ic_newton = ift.GradInfNormController( name='Newton', tol=1e-6, iteration_limit=30) minimizer = ift.NewtonCG(ic_newton) H = ift.StandardHamiltonian(likelihood, ic_sampling)
def field(request): np.random.seed(request.param[0]) S = ift.ScalingOperator(1., request.param[1]) s = S.draw_sample() return ift.MultiField.from_dict({'s1': s})['s1']
Mask = ift.DiagonalOperator(mask) # The response operator consists of # - an harmonic transform (to get to image space) # - the application of the mask # - the removal of geometric information # Operators can be composed either with parenthesis R = GR(Mask(HT)) # or with @ R = GR @ Mask @ HT data_space = GR.target # Set the noise covariance N noise = 5. N = ift.ScalingOperator(noise, data_space) # Create mock data MOCK_SIGNAL = S.draw_sample() MOCK_NOISE = N.draw_sample() data = R(MOCK_SIGNAL) + MOCK_NOISE # Build inverse propagator D and information source j D_inv = R.adjoint @ N.inverse @ R + S.inverse j = R.adjoint_times(N.inverse_times(data)) # Make D_inv invertible (via Conjugate Gradient) IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3) D = ift.InversionEnabler(D_inv, IC, approximation=S.inverse).inverse # Calculate WIENER FILTER solution m = D(j)
def testBasics(space, seed): var = _make_linearization("Variable", space, seed) model = ift.ScalingOperator(6., var.target) ift.extra.check_jacobian_consistency(model, var.val)