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 testPPO(sp, dtype): _check_repr(ift.PowerDistributor(target=sp)) ps = ift.PowerSpace( sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3)) _check_repr(ift.PowerDistributor(target=sp, power_space=ps)) ps = ift.PowerSpace( sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3)) _check_repr(ift.PowerDistributor(target=sp, power_space=ps))
def testPPO(sp, dtype): op = ift.PowerDistributor(target=sp) ift.extra.consistency_check(op, dtype, dtype) ps = ift.PowerSpace( sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3)) op = ift.PowerDistributor(target=sp, power_space=ps) ift.extra.consistency_check(op, dtype, dtype) ps = ift.PowerSpace( sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3)) op = ift.PowerDistributor(target=sp, power_space=ps) ift.extra.consistency_check(op, dtype, dtype)
def test_constructor(harmonic_partner, logarithmic, nbin, binbounds, expected): if 'error' in expected: with assert_raises(expected['error']): bb = ift.PowerSpace.useful_binbounds(harmonic_partner, logarithmic, nbin) ift.PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) else: bb = ift.PowerSpace.useful_binbounds(harmonic_partner, logarithmic, nbin) p = ift.PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) for key, value in expected.items(): if isinstance(value, np.ndarray): assert_allclose(getattr(p, key), value) else: assert_equal(getattr(p, key), value)
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 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_dvol(): hp = ift.RGSpace(10, harmonic=True) p = ift.PowerSpace(harmonic_partner=hp) v1 = hp.dvol v1 = hp.size*v1 if np.isscalar(v1) else np.sum(v1) v2 = p.dvol v2 = p.size*v2 if np.isscalar(v2) else np.sum(v2) assert_allclose(v1, v2)
def test_rhopindexConsistency(harmonic_partner, binbounds, nbin, logarithmic): bb = ift.PowerSpace.useful_binbounds(harmonic_partner, logarithmic, nbin) p = ift.PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) assert_equal( np.bincount(ift.dobj.to_global_data(p.pindex).ravel()), p.dvol, err_msg='rho is not equal to pindex degeneracy')
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)
def __init__( self, N_bins=1024, power_spectrum_beta=lambda q: 1 / (q**4 + 1), power_spectrum_f=lambda q: 1 / (q**4 + 1), noise_var=0.1, ): """ N_bins : int number of bins for the sample spaces p_spec_beta : function power spectrum for beta p_spec_f : function power spectrum for f noise_var : scalar the variance of the noise variable """ self.N_bins = N_bins self.s_space = nifty5.RGSpace([N_bins], ) self.h_space = self.s_space.get_default_codomain() self.p_space = nifty5.PowerSpace(self.h_space) # covariance operator for the beta distribution self.power_spectrum_beta = power_spectrum_beta B_h = nifty5.create_power_operator( self.h_space, power_spectrum=self.power_spectrum_beta) fft = nifty5.FFTOperator(self.s_space) self.B = nifty5.SandwichOperator.make(fft, B_h) self.beta = self.B.draw_sample() # numerical values for p(x|beta) = exp(beta(x)) / sum_z exp(beta(z)) self.p_x_val = np.exp(np.array(self.beta.to_global_data())) self.p_x_val = (1 / np.sum(self.p_x_val)) * self.p_x_val # get the covariance operator for the f distribution self.power_spectrum_f = power_spectrum_f F_h = nifty5.create_power_operator( self.h_space, power_spectrum=self.power_spectrum_f) self.F = nifty5.SandwichOperator.make(fft, F_h) # sample the transformation function f self.f = self.F.draw_sample() self.f_val = np.array(self.f.to_global_data()) # set the noise variance self.noise_var = noise_var
# # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik. import numpy as np import nifty5 as ift from helpers import plot_WF, power_plot, generate_mysterious_data np.random.seed(42) position_space = ift.RGSpace(256) harmonic_space = position_space.get_default_codomain() HT = ift.HarmonicTransformOperator(harmonic_space, target=position_space) power_space = ift.PowerSpace(harmonic_space) # Set up an amplitude operator for the field # We want to set up a model for the amplitude spectrum with some magic numbers dct = { 'target': power_space, 'n_pix': 64, # 64 spectral bins # Spectral smoothness (affects Gaussian process part) 'a': 10, # relatively high variance of spectral curvature 'k0': .2, # quefrency mode below which cepstrum flattens # Power-law part of spectrum: 'sm': -4, # preferred power-law slope 'sv': .6, # low variance of power-law slope 'im': -6, # y-intercept mean, in-/decrease for more/less contrast 'iv': 2. # y-intercept variance }
def testSlopeOperator(args, dtype): tmp = ift.ExpTransform(ift.PowerSpace(args[0]), args[1], args[2]) tgt = tmp.domain[0] _check_repr(ift.SlopeOperator(tgt))
import nifty5 as ift from ..common import list2fixture _h_RG_spaces = [ ift.RGSpace(7, distances=0.2, harmonic=True), ift.RGSpace((12, 46), distances=(.2, .3), harmonic=True) ] _h_spaces = _h_RG_spaces + [ift.LMSpace(17)] _p_RG_spaces = [ ift.RGSpace(19, distances=0.7), ift.RGSpace((1, 2, 3, 6), distances=(0.2, 0.25, 0.34, .8)) ] _p_spaces = _p_RG_spaces + [ift.HPSpace(17), ift.GLSpace(8, 13)] _pow_spaces = [ift.PowerSpace(ift.RGSpace((17, 38), harmonic=True))] pmp = pytest.mark.parametrize dtype = list2fixture([np.float64, np.complex128]) def _check_repr(op): op.__repr__() @pmp('sp', _p_RG_spaces) def testLOSResponse(sp, dtype): starts = np.random.randn(len(sp.shape), 10) ends = np.random.randn(len(sp.shape), 10) sigma_low = 1e-4 * np.random.randn(10) sigma_ups = 1e-5 * np.random.randn(10)
# You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # Copyright(C) 2013-2019 Max-Planck-Society # # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik. from numpy.testing import assert_allclose, assert_equal import nifty5 as ift from ..common import list2fixture space1 = list2fixture([ ift.RGSpace(4), ift.PowerSpace(ift.RGSpace((4, 4), harmonic=True)), ift.LMSpace(5), ift.HPSpace(4), ift.GLSpace(4) ]) space2 = space1 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)
def test_k_lengths(harmonic_partner, expected): p = ift.PowerSpace(harmonic_partner=harmonic_partner) assert_allclose(p.k_lengths, expected)
def test_property_ret_type(attribute, expected_type): r = ift.RGSpace((4, 4), harmonic=True) p = ift.PowerSpace(r) assert_(isinstance(getattr(p, attribute), expected_type))
def testSlopeOperator(args, dtype): tmp = ift.ExpTransform(ift.PowerSpace(args[0]), args[1], args[2]) tgt = tmp.domain[0] op = ift.SlopeOperator(tgt) ift.extra.consistency_check(op, dtype, dtype)