def test_fft2D(dim1, dim2, d1, d2, dtype, op, fftw): if fftw: ift.fft.enable_fftw() tol = _get_rtol(dtype) a = ift.RGSpace([dim1, dim2], distances=[d1, d2]) b = ift.RGSpace([dim1, dim2], distances=[1. / (dim1 * d1), 1. / (dim2 * d2)], harmonic=True) fft = op(domain=a, target=b) inp = ift.Field.from_random(domain=a, random_type='normal', std=7, mean=3, dtype=dtype) out = fft.inverse_times(fft.times(inp)) assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol) a, b = b, a fft = ift.FFTOperator(domain=a, target=b) inp = ift.Field.from_random(domain=a, random_type='normal', std=7, mean=3, dtype=dtype) out = fft.inverse_times(fft.times(inp)) assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol) ift.fft.disable_fftw()
def test_fft1D(d, dtype, op, fftw): if fftw: ift.fft.enable_fftw() dim1 = 16 tol = _get_rtol(dtype) a = ift.RGSpace(dim1, distances=d) b = ift.RGSpace(dim1, distances=1. / (dim1 * d), harmonic=True) np.random.seed(16) fft = op(domain=a, target=b) inp = ift.Field.from_random(domain=a, random_type='normal', std=7, mean=3, dtype=dtype) out = fft.inverse_times(fft.times(inp)) assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol) a, b = b, a fft = ift.FFTOperator(domain=a, target=b) inp = ift.Field.from_random(domain=a, random_type='normal', std=7, mean=3, dtype=dtype) out = fft.inverse_times(fft.times(inp)) assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol) ift.fft.disable_fftw()
def test_simplification(): from nifty5.operators.operator import _ConstantOperator f1 = ift.Field.full(ift.RGSpace(10), 2.) op = ift.FFTOperator(f1.domain) _, op2 = op.simplify_for_constant_input(f1) assert_equal(isinstance(op2, _ConstantOperator), True) assert_allclose(op(f1).local_data, op2(f1).local_data) dom = {"a": ift.RGSpace(10)} f1 = ift.full(dom, 2.) op = ift.FFTOperator(f1.domain["a"]).ducktape("a") _, op2 = op.simplify_for_constant_input(f1) assert_equal(isinstance(op2, _ConstantOperator), True) assert_allclose(op(f1).local_data, op2(f1).local_data) dom = {"a": ift.RGSpace(10), "b": ift.RGSpace(5)} f1 = ift.full(dom, 2.) pdom = {"a": ift.RGSpace(10)} f2 = ift.full(pdom, 2.) o1 = ift.FFTOperator(f1.domain["a"]) o2 = ift.FFTOperator(f1.domain["b"]) op = (o1.ducktape("a").ducktape_left("a") + o2.ducktape("b").ducktape_left("b")) _, op2 = op.simplify_for_constant_input(f2) assert_equal(isinstance(op2._op1, _ConstantOperator), True) assert_allclose(op(f1)["a"].local_data, op2(f1)["a"].local_data) assert_allclose(op(f1)["b"].local_data, op2(f1)["b"].local_data) lin = ift.Linearization.make_var(ift.MultiField.full(op2.domain, 2.), True) assert_allclose(op(lin).val["a"].local_data, op2(lin).val["a"].local_data) assert_allclose(op(lin).val["b"].local_data, op2(lin).val["b"].local_data)
def test_outer(): x1 = ift.RGSpace((9, )) x2 = ift.RGSpace((3, )) m1 = ift.Field.full(x1, .5) m2 = ift.Field.full(x2, 3.) res = m1.outer(m2) assert_allclose(res.to_global_data(), np.full((9, 3), 1.5))
def test_integrate(): x1 = ift.RGSpace((9, ), distances=2.) x2 = ift.RGSpace((2, 12), distances=(0.3, )) m1 = ift.Field.from_global_data(ift.makeDomain(x1), np.arange(9)) m2 = ift.Field.full(ift.makeDomain((x1, x2)), 0.45) res1 = m1.integrate() res2 = m2.integrate(spaces=1) assert_allclose(res1, 36 * 2) assert_allclose(res2.to_global_data(), np.full(9, 2 * 12 * 0.45 * 0.3**2))
def test_get_signal_variance(): space = ift.RGSpace(3) hspace = space.get_default_codomain() spec1 = lambda x: np.ones_like(x) assert_equal(ift.get_signal_variance(spec1, hspace), 3.) space = ift.RGSpace(3, distances=1.) hspace = space.get_default_codomain() def spec2(k): t = np.zeros_like(k) t[k == 0] = 1. return t assert_equal(ift.get_signal_variance(spec2, hspace), 1 / 9.)
def test_stdfunc(): s = ift.RGSpace((200, )) f = ift.Field.full(s, 27) assert_equal(f.local_data, 27) assert_equal(f.shape, (200, )) assert_equal(f.dtype, np.int) fx = ift.full(f.domain, 0) assert_equal(f.dtype, fx.dtype) assert_equal(f.shape, fx.shape) assert_equal(fx.local_data, 0) fx = ift.full(f.domain, 1) assert_equal(f.dtype, fx.dtype) assert_equal(f.shape, fx.shape) assert_equal(fx.local_data, 1) fx = ift.full(f.domain, 67.) assert_equal(f.shape, fx.shape) assert_equal(fx.local_data, 67.) f = ift.Field.from_random("normal", s) f2 = ift.Field.from_random("normal", s) assert_equal((f > f2).local_data, f.local_data > f2.local_data) assert_equal((f >= f2).local_data, f.local_data >= f2.local_data) assert_equal((f < f2).local_data, f.local_data < f2.local_data) assert_equal((f <= f2).local_data, f.local_data <= f2.local_data) assert_equal((f != f2).local_data, f.local_data != f2.local_data) assert_equal((f == f2).local_data, f.local_data == f2.local_data) assert_equal((f + f2).local_data, f.local_data + f2.local_data) assert_equal((f - f2).local_data, f.local_data - f2.local_data) assert_equal((f * f2).local_data, f.local_data * f2.local_data) assert_equal((f / f2).local_data, f.local_data / f2.local_data) assert_equal((-f).local_data, -(f.local_data)) assert_equal(abs(f).local_data, abs(f.local_data))
def testLinearInterpolator(): sp = ift.RGSpace((10, 8), distances=(0.1, 3.5)) pos = np.random.rand(2, 23) pos[0, :] *= 0.9 pos[1, :] *= 7 * 3.5 op = ift.LinearInterpolator(sp, pos) ift.extra.consistency_check(op)
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_composed_fft(index, dtype, op, fftw): if fftw: ift.fft.enable_fftw() tol = _get_rtol(dtype) a = [a1, a2, a3] = [ift.RGSpace((32, )), ift.RGSpace((4, 4)), ift.RGSpace((5, 6))] fft = op(domain=a, space=index) inp = ift.Field.from_random(domain=(a1, a2, a3), random_type='normal', std=7, mean=3, dtype=dtype) out = fft.inverse_times(fft.times(inp)) assert_allclose(inp.local_data, out.local_data, rtol=tol, atol=tol) ift.fft.disable_fftw()
def test_smooth_regular2(sz1, sz2, d1, d2, sigma, tp): tol = _get_rtol(tp) sp = ift.RGSpace([sz1, sz2], distances=[d1, d2]) smo = ift.HarmonicSmoothingOperator(sp, sigma=sigma) inp = ift.Field.from_random(domain=sp, random_type='normal', std=1, mean=4, dtype=tp) out = smo(inp) assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)
def test_err(): s1 = ift.RGSpace((10, )) s2 = ift.RGSpace((11, )) f1 = ift.Field.full(s1, 27) with assert_raises(ValueError): f2 = ift.Field(ift.DomainTuple.make(s2), f1.val) with assert_raises(TypeError): f2 = ift.Field.full(s2, "xyz") with assert_raises(TypeError): if f1: pass with assert_raises(TypeError): f1.full((2, 4, 6)) with assert_raises(TypeError): f2 = ift.Field(None, None) with assert_raises(TypeError): f2 = ift.Field(s1, None) with assert_raises(ValueError): f1.imag with assert_raises(TypeError): f1.vdot(42) with assert_raises(ValueError): f1.vdot(ift.Field.full(s2, 1.)) with assert_raises(TypeError): ift.full(s1, [2, 3]) with assert_raises(TypeError): ift.Field(s2, [0, 1]) with assert_raises(TypeError): f1.outer([0, 1]) with assert_raises(ValueError): f1.extract(s2) with assert_raises(TypeError): f1 += f1 f2 = ift.Field.full(s2, 27) with assert_raises(ValueError): f1 + f2
def test_weight(): s1 = ift.RGSpace((10, )) f = ift.Field.full(s1, 10.) f2 = f.weight(1) assert_equal(f.weight(1).local_data, f2.local_data) assert_equal(f.total_volume(), 1) assert_equal(f.total_volume(0), 1) assert_equal(f.total_volume((0, )), 1) assert_equal(f.scalar_weight(), 0.1) assert_equal(f.scalar_weight(0), 0.1) assert_equal(f.scalar_weight((0, )), 0.1) s1 = ift.GLSpace(10) f = ift.Field.full(s1, 10.) assert_equal(f.scalar_weight(), None) assert_equal(f.scalar_weight(0), None) assert_equal(f.scalar_weight((0, )), None)
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
def test_trivialities(): s1 = ift.RGSpace((10, )) f1 = ift.Field.full(s1, 27) assert_equal(f1.clip(min=29).local_data, 29.) assert_equal(f1.clip(max=25).local_data, 25.) assert_equal(f1.local_data, f1.real.local_data) assert_equal(f1.local_data, (+f1).local_data) f1 = ift.Field.full(s1, 27. + 3j) assert_equal(f1.one_over().local_data, (1. / f1).local_data) assert_equal(f1.real.local_data, 27.) assert_equal(f1.imag.local_data, 3.) assert_equal(f1.sum(), f1.sum(0)) assert_equal(f1.conjugate().local_data, ift.Field.full(s1, 27. - 3j).local_data) f1 = ift.from_global_data(s1, np.arange(10)) # assert_equal(f1.min(), 0) # assert_equal(f1.max(), 9) assert_equal(f1.prod(), 0)
def wf_test(signal, noise, signal_boost, npix = 400): pixel_space = ift.RGSpace([npix, npix]) fourier_space = pixel_space.get_default_codomain() signal_field = ift.Field.from_global_data(pixel_space, signal.astype(float)) HT = ift.HartleyOperator(fourier_space, target=pixel_space) power_field = ift.power_analyze(HT.inverse(signal_field), binbounds=ift.PowerSpace.useful_binbounds(fourier_space, True)) Sh = ift.create_power_operator(fourier_space, power_spectrum=power_field) R = HT noise_field = ift.Field.from_global_data(pixel_space, noise.astype(float)) noise_power_field = ift.power_analyze(HT.inverse(noise_field), binbounds=ift.PowerSpace.useful_binbounds(fourier_space, True)) N = ift.create_power_operator(HT.domain, noise_power_field) N_inverse = HT@[email protected] amplify = len(signal_boost) s_data = np.zeros((amplify, npix, npix)) m_data = np.zeros((amplify, npix, npix)) d_data = np.zeros((amplify, npix, npix)) for i in np.arange(amplify): data = noise_field # Wiener filtering the data j = (R.adjoint @N_inverse.inverse)(data) D_inv = R.adjoint @ N_inverse.inverse @ R + Sh.inverse IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3) D = ift.InversionEnabler(D_inv, IC, approximation=Sh.inverse).inverse m = D(j) s_data[i,:,:] = (signal_field * signal_boost[i]).to_global_data() m_data[i,:,:] = HT(m).to_global_data() d_data[i,:,:] = data.to_global_data() return (s_data, m_data, d_data)
def test_dvol(shape, distances, harmonic, power): r = ift.RGSpace(shape=shape, distances=distances, harmonic=harmonic) assert_allclose(r.dvol, np.prod(r.distances)**power)
# # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik. import numpy as np import pytest import nifty5 as ift from itertools import product # Currently it is not possible to parametrize fixtures. But this will # hopefully be fixed in the future. # https://docs.pytest.org/en/latest/proposals/parametrize_with_fixtures.html SPACES = [ ift.GLSpace(15), ift.RGSpace(64, distances=.789), ift.RGSpace([32, 32], distances=.789) ] SEEDS = [4, 78, 23] PARAMS = product(SEEDS, SPACES) @pytest.fixture(params=PARAMS) 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'] def test_gaussian(field):
def testLinearInterpolator(): sp = ift.RGSpace((10, 8), distances=(0.1, 3.5)) pos = np.random.rand(2, 23) pos[0, :] *= 0.9 pos[1, :] *= 7 * 3.5 _check_repr(ift.LinearInterpolator(sp, pos))
def testZeroPadder(space, factor, dtype, central): dom = (ift.RGSpace(10), ift.UnstructuredDomain(13), ift.RGSpace(7, 12), ift.HPSpace(4)) newshape = [int(factor * l) for l in dom[space].shape] _check_repr(ift.FieldZeroPadder(dom, newshape, space, central))
def testDomainTupleFieldInserter(): target = ift.DomainTuple.make( (ift.UnstructuredDomain([3, 2]), ift.UnstructuredDomain(7), ift.RGSpace([4, 22]))) _check_repr(ift.DomainTupleFieldInserter(target, 1, (5, )))
def testContractionOperator(spaces, wgt, dtype): dom = (ift.RGSpace(10), ift.RGSpace(13), ift.GLSpace(5), ift.HPSpace(4)) _check_repr(ift.ContractionOperator(dom, spaces, wgt))
# 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. import numpy as np import pytest 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):
# # 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, ))
# 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. import numpy as np import nifty5 as ift from helpers import (checkerboard_response, generate_gaussian_data, plot_prior_samples_2d, plot_reconstruction_2d) np.random.seed(42) position_space = ift.RGSpace(2*(256,)) harmonic_space = position_space.get_default_codomain() HT = ift.HarmonicTransformOperator(harmonic_space, target=position_space) power_space = ift.PowerSpace(harmonic_space) # Set up generative model A = ift.SLAmplitude( **{ 'target': power_space, 'n_pix': 64, # 64 spectral bins # Smoothness of spectrum '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
def test_k_length_array(shape, distances, expected): r = ift.RGSpace(shape=shape, distances=distances, harmonic=True) assert_allclose(r.get_k_length_array().to_global_data(), expected)
def test_constructor(shape, distances, harmonic, expected): x = ift.RGSpace(shape, distances, harmonic) for key, value in expected.items(): assert_equal(getattr(x, key), value)
# # 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. 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:
# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # 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 import nifty5 as ift from ..common import list2fixture s = list2fixture([ ift.RGSpace(8, distances=12.9), ift.RGSpace(59, distances=.24, harmonic=True), ift.RGSpace([12, 3]) ]) 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())
def test_property_ret_type(attribute, expected_type): x = ift.RGSpace(1) assert_(isinstance(getattr(x, attribute), expected_type))