def testOperatorCombinations(sp, dtype): a = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) b = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) _check_repr(ift.SandwichOperator.make(a, b)) _check_repr(a(b)) _check_repr(a + b) _check_repr(a - b) _check_repr(a * b) _check_repr(a**2)
def testOperatorCombinations(sp, dtype): a = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) b = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) op = ift.SandwichOperator.make(a, b) ift.extra.consistency_check(op, dtype, dtype) op = a(b) ift.extra.consistency_check(op, dtype, dtype) op = a + b ift.extra.consistency_check(op, dtype, dtype) op = a - b ift.extra.consistency_check(op, dtype, dtype)
def test_times_inverse_times(space1, space2): cspace = (space1, space2) diag1 = ift.Field.from_random('normal', domain=space1) diag2 = ift.Field.from_random('normal', domain=space2) op1 = ift.DiagonalOperator(diag1, cspace, spaces=(0, )) op2 = ift.DiagonalOperator(diag2, cspace, spaces=(1, )) op = op2(op1) rand1 = ift.Field.from_random('normal', domain=(space1, space2)) tt1 = op.inverse_times(op.times(rand1)) assert_allclose(tt1.local_data, rand1.local_data)
def test_times_adjoint_times(space1, space2): cspace = (space1, space2) diag1 = ift.Field.from_random('normal', domain=space1) diag2 = ift.Field.from_random('normal', domain=space2) op1 = ift.DiagonalOperator(diag1, cspace, spaces=(0, )) op2 = ift.DiagonalOperator(diag2, cspace, spaces=(1, )) op = op2(op1) rand1 = ift.Field.from_random('normal', domain=(space1, space2)) rand2 = ift.Field.from_random('normal', domain=(space1, space2)) tt1 = rand2.vdot(op.times(rand1)) tt2 = rand1.vdot(op.adjoint_times(rand2)) assert_allclose(tt1, tt2)
def __init__( self, domain, beta, k, grid, power_spectrum=lambda q: 2/(q**4 + 1), rho=1, verbosity=0): super().__init__() self.beta = beta self._domain = (domain, ) self.fft = nifty5.FFTOperator(self._domain) self.h_space = self.fft.target[0] self.grid = grid self.k = k self.rho = rho B_h = nifty5.create_power_operator( domain=self.h_space, power_spectrum=power_spectrum) self.B = nifty5.SandwichOperator.make(self.fft, B_h) # the diagonal operator rho*e^beta rho_e_beta = np.zeros(domain.shape[0]) for i, pos in enumerate(self.grid): rho_e_beta[pos] = ( self.rho*np.exp(self.beta.val[pos])) rho_e_beta_field = nifty5.Field(domain=domain, val=rho_e_beta) self.rho_e_beta_diag = nifty5.DiagonalOperator( domain=self._domain, diagonal=rho_e_beta_field)
def apply(self, x, mode): self._check_mode(mode=mode) if mode == self.TIMES: x_tau_beta = x.val['tau_beta'] # (delta_{zz'} beta.del_B_inv_z.beta + smoothing_beta) field_for_diagonal = nifty5.Field.from_local_data( domain=self.p_space, arr=np.array([ self.beta.vdot(del_B_inv.times(self.beta)) for del_B_inv in self.del_B_inv_list ])) delta_beta_del_B_inv_beta = nifty5.DiagonalOperator( domain=self.p_space, diagonal=field_for_diagonal) term_tau_beta_1 = delta_beta_del_B_inv_beta.times(x_tau_beta) term_tau_beta_2 = self.smoothness_operator_beta.times(x_tau_beta) result_fields = { 'beta': nifty5.Field.zeros(domain=self.s_space), 'tau_beta': term_tau_beta_1 + term_tau_beta_2, } result = global_newton.MultiField(domain=x.domain, val=result_fields) return result
def test_times_adjoint(space): rand1 = ift.Field.from_random('normal', domain=space) rand2 = ift.Field.from_random('normal', domain=space) diag = ift.Field.from_random('normal', domain=space) D = ift.DiagonalOperator(diag) tt1 = rand1.vdot(D.times(rand2)) tt2 = rand2.vdot(D.times(rand1)) assert_allclose(tt1, tt2)
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)
def SlopeSpectrumOperator(target, m=0, n=0, sigma_m=.1, sigma_n=.1): codomain = target.get_default_codomain() pos_diagonals = np.ones(target.shape[0]) pos_diagonals[target.shape[0] // 2 + 1:] = -1 flipper = ift.DiagonalOperator( ift.Field(ift.DomainTuple.make(codomain), pos_diagonals)) slope = LinearSlopeOperator(target.get_default_codomain()) mean = np.array([m, n]) sig = np.array([sigma_m, sigma_n]) mean = ift.Field.from_global_data(slope.domain, mean) sig = ift.Field.from_global_data(slope.domain, sig) linear_operator = flipper @ slope @ ift.Adder(mean) @ ift.makeOp(sig) return linear_operator.ducktape('slope')
def test_quadratic_minimization(minimizer, space): np.random.seed(42) starting_point = ift.Field.from_random('normal', domain=space)*10 covariance_diagonal = ift.Field.from_random('uniform', domain=space) + 0.5 covariance = ift.DiagonalOperator(covariance_diagonal) required_result = ift.full(space, 1.) try: minimizer = eval(minimizer) energy = ift.QuadraticEnergy( A=covariance, b=required_result, position=starting_point) (energy, convergence) = minimizer(energy) except NotImplementedError: raise SkipTest assert_equal(convergence, IC.CONVERGED) assert_allclose( energy.position.local_data, 1./covariance_diagonal.local_data, rtol=1e-3, atol=1e-3)
# 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) 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',
def test_times_inverse(space): rand1 = ift.Field.from_random('normal', domain=space) diag = ift.Field.from_random('normal', domain=space) D = ift.DiagonalOperator(diag) tt1 = D.times(D.inverse_times(rand1)) assert_allclose(rand1.local_data, tt1.local_data)
def testOperatorAdaptor(sp, dtype): op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) _check_repr(op.adjoint) _check_repr(op.inverse) _check_repr(op.inverse.adjoint)
def test_adjoint_inverse_times(space): rand1 = ift.Field.from_random('normal', domain=space) diag = ift.Field.from_random('normal', domain=space) D = ift.DiagonalOperator(diag) tt = D.adjoint_inverse_times(rand1) assert_equal(tt.domain[0], space)
def testDiagonal(sp, dtype): op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) _check_repr(op)
def test_diagonal(space): diag = ift.Field.from_random('normal', domain=space) D = ift.DiagonalOperator(diag) diag_op = D(ift.Field.full(space, 1.)) assert_allclose(diag.local_data, diag_op.local_data)
def apply_metric(self, x): p = self.position.to_global_data()[0] v = (2 - 4*p*p)*np.exp(-p**2) return ift.DiagonalOperator( ift.Field.full(self.position.domain, v))(x)
# 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) # The response operator consists of # - an harmonic transform (to get to image space) # - the application of the mask
def testOperatorAdaptor(sp, dtype): op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) ift.extra.consistency_check(op.adjoint, dtype, dtype) ift.extra.consistency_check(op.inverse, dtype, dtype) ift.extra.consistency_check(op.inverse.adjoint, dtype, dtype) ift.extra.consistency_check(op.adjoint.inverse, dtype, dtype)
def metric(self): x = self.position.to_global_data()[0] v = np.cosh(x) return ift.DiagonalOperator( ift.Field.full(self.position.domain, v))
def testDiagonal(sp, dtype): op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) ift.extra.consistency_check(op, dtype, dtype)
def test_property(space): diag = ift.Field.from_random('normal', domain=space) D = ift.DiagonalOperator(diag) if D.domain[0] != space: raise TypeError
def apply_metric(self, x): p = self.position.to_global_data()[0] v = np.cosh(p) return ift.DiagonalOperator( ift.Field.full(self.position.domain, v))(x)