From 1083dadf5d497d1ecd7ee9deede32f751d8e9bb0 Mon Sep 17 00:00:00 2001 From: Martin Reinecke <martin@mpa-garching.mpg.de> Date: Mon, 13 Nov 2017 14:12:40 +0100 Subject: [PATCH] rework dobj interface and prepare tests for MPI --- nifty/data_objects/numpy_do.py | 56 ++++++++- test/common.py | 12 +- test/test_field.py | 114 +++++++++--------- test/test_minimization/test_minimizers.py | 7 +- test/test_operators/test_adoint.py | 28 +++-- test/test_operators/test_composed_operator.py | 47 +++----- test/test_operators/test_diagonal_operator.py | 71 +++++------ test/test_operators/test_fft_operator.py | 93 +++++++------- test/test_operators/test_laplace_operator.py | 3 +- test/test_operators/test_response_operator.py | 33 +++-- .../test_operators/test_smoothing_operator.py | 39 +++--- test/test_spaces/test_gl_space.py | 1 - test/test_spaces/test_hp_space.py | 1 - test/test_spaces/test_interface.py | 5 +- test/test_spaces/test_lm_space.py | 18 +-- test/test_spaces/test_power_space.py | 79 ++++++------ test/test_spaces/test_rg_space.py | 19 ++- 17 files changed, 332 insertions(+), 294 deletions(-) diff --git a/nifty/data_objects/numpy_do.py b/nifty/data_objects/numpy_do.py index 49f6da1c3..4c2ed0a55 100644 --- a/nifty/data_objects/numpy_do.py +++ b/nifty/data_objects/numpy_do.py @@ -2,9 +2,21 @@ import numpy as np from numpy import ndarray as data_object -from numpy import full, empty, sqrt, ones, zeros, vdot, abs, bincount, exp, log +from numpy import full, empty, empty_like, sqrt, ones, zeros, vdot, abs, \ + bincount, exp, log from .random import Random +__all__ = ["ntask", "rank", "master", "local_shape", "data_object", "full", + "empty", "zeros", "ones", "empty_like", "vdot", "abs", "exp", + "log", "sqrt", "bincount", "from_object", "from_random", + "local_data", "ibegin", "np_allreduce_sum", "distaxis", + "from_local_data", "from_global_data", "to_global_data", + "redistribute", "default_distaxis"] + +ntask = 1 +rank = 0 +master = True + def from_object(object, dtype=None, copy=True): return np.array(object, dtype=dtype, copy=copy) @@ -13,3 +25,45 @@ def from_object(object, dtype=None, copy=True): def from_random(random_type, shape, dtype=np.float64, **kwargs): generator_function = getattr(Random, random_type) return generator_function(dtype=dtype, shape=shape, **kwargs) + + +def local_data(arr): + return arr + + +def ibegin(arr): + return (0,)*arr.ndim + + +def np_allreduce_sum(arr): + return arr + + +def distaxis(arr): + return -1 + + +def from_local_data(shape, arr, distaxis): + if shape != arr.shape: + raise ValueError + return arr + + +def from_global_data(arr, distaxis=-1): + return arr + + +def to_global_data(arr): + return arr + + +def redistribute(arr, dist=None, nodist=None): + return arr + + +def default_distaxis(): + return -1 + + +def local_shape(glob_shape, distaxis): + return glob_shape diff --git a/test/common.py b/test/common.py index c71bf0b84..d98b95085 100644 --- a/test/common.py +++ b/test/common.py @@ -18,10 +18,11 @@ from builtins import str from parameterized import parameterized -from nifty2go import Space, RGSpace, LMSpace, HPSpace, GLSpace, PowerSpace +import nifty2go as ift import numpy as np -np.seterr(all='raise',under='ignore') +np.seterr(all='raise', under='ignore') + def custom_name_func(testcase_func, param_num, param): return "%s_%s" % ( @@ -36,11 +37,12 @@ def expand(*args, **kwargs): def generate_spaces(): - spaces = [RGSpace(4), PowerSpace(RGSpace((4, 4), harmonic=True)), - LMSpace(5), HPSpace(4), GLSpace(4)] + spaces = [ift.RGSpace(4), + ift.PowerSpace(ift.RGSpace((4, 4), harmonic=True)), + ift.LMSpace(5), ift.HPSpace(4), ift.GLSpace(4)] return spaces def generate_harmonic_spaces(): - spaces = [RGSpace(4, harmonic=True), LMSpace(5)] + spaces = [ift.RGSpace(4, harmonic=True), ift.LMSpace(5)] return spaces diff --git a/test/test_field.py b/test/test_field.py index 8fb797025..9da6e095f 100644 --- a/test/test_field.py +++ b/test/test_field.py @@ -17,111 +17,115 @@ # and financially supported by the Studienstiftung des deutschen Volkes. import unittest - import numpy as np -from numpy.testing import assert_equal,\ - assert_allclose +from numpy.testing import assert_equal, assert_allclose from itertools import product -from nifty2go import Field,\ - RGSpace,\ - LMSpace,\ - PowerSpace,\ - DomainTuple,\ - DiagonalOperator - -from nifty2go.sugar import create_power_field, power_analyze, power_synthesize - +import nifty2go as ift from test.common import expand -SPACES = [RGSpace((4,)), RGSpace((5))] +SPACES = [ift.RGSpace((4,)), ift.RGSpace((5))] SPACE_COMBINATIONS = [(), SPACES[0], SPACES[1], SPACES] class Test_Interface(unittest.TestCase): @expand(product(SPACE_COMBINATIONS, - [['domain', DomainTuple], - ['val', np.ndarray], + [['domain', ift.DomainTuple], + ['val', ift.dobj.data_object], ['shape', tuple], ['dim', (np.int, np.int64)]])) def test_return_types(self, domain, attribute_desired_type): attribute = attribute_desired_type[0] desired_type = attribute_desired_type[1] - f = Field(domain=domain) + f = ift.Field(domain=domain) assert_equal(isinstance(getattr(f, attribute), desired_type), True) +def _spec1(k): + return 42/(1.+k)**2 + + +def _spec2(k): + return 42/(1.+k)**3 + + class Test_Functionality(unittest.TestCase): - @expand(product([RGSpace((8,), harmonic=True), - RGSpace((8, 8), harmonic=True, distances=0.123)], - [RGSpace((8,), harmonic=True), - LMSpace(12)])) + @expand(product([ift.RGSpace((8,), harmonic=True), + ift.RGSpace((8, 8), harmonic=True, distances=0.123)], + [ift.RGSpace((8,), harmonic=True), + ift.LMSpace(12)])) def test_power_synthesize_analyze(self, space1, space2): np.random.seed(11) - p1 = PowerSpace(space1) - spec1 = lambda k: 42/(1+k)**2 - fp1 = Field(p1, val=spec1(p1.k_lengths)) + p1 = ift.PowerSpace(space1) + p1val = _spec1(p1.k_lengths) + fp1 = ift.Field(p1, val=ift.dobj.from_global_data(p1val)) - p2 = PowerSpace(space2) - spec2 = lambda k: 42/(1+k)**3 - fp2 = Field(p2, val=spec2(p2.k_lengths)) + p2 = ift.PowerSpace(space2) + p2val = _spec2(p2.k_lengths) + fp2 = ift.Field(p2, val=ift.dobj.from_global_data(p2val)) - outer = np.outer(fp1.val, fp2.val) - fp = Field((p1, p2), val=outer) + outer = ift.dobj.from_global_data(np.outer(p1val, p2val)) + fp = ift.Field((p1, p2), val=outer) samples = 500 ps1 = 0. ps2 = 0. for ii in range(samples): - sk = power_synthesize(fp, spaces=(0, 1), real_signal=True) + sk = ift.power_synthesize(fp, spaces=(0, 1), real_signal=True) - sp = power_analyze(sk, spaces=(0, 1), keep_phase_information=False) + sp = ift.power_analyze(sk, spaces=(0, 1), + keep_phase_information=False) ps1 += sp.sum(spaces=1)/fp2.sum() ps2 += sp.sum(spaces=0)/fp1.sum() - assert_allclose(ps1.val/samples, fp1.val, rtol=0.2) - assert_allclose(ps2.val/samples, fp2.val, rtol=0.2) + assert_allclose(ift.dobj.to_global_data(ps1.val/samples), + ift.dobj.to_global_data(fp1.val), rtol=0.2) + assert_allclose(ift.dobj.to_global_data(ps2.val/samples), + ift.dobj.to_global_data(fp2.val), rtol=0.2) - @expand(product([RGSpace((8,), harmonic=True), - RGSpace((8, 8), harmonic=True, distances=0.123)], - [RGSpace((8,), harmonic=True), - LMSpace(12)])) + @expand(product([ift.RGSpace((8,), harmonic=True), + ift.RGSpace((8, 8), harmonic=True, distances=0.123)], + [ift.RGSpace((8,), harmonic=True), + ift.LMSpace(12)])) def test_DiagonalOperator_power_analyze(self, space1, space2): np.random.seed(11) - fulldomain = DomainTuple.make((space1, space2)) + fulldomain = ift.DomainTuple.make((space1, space2)) - p1 = PowerSpace(space1) - spec1 = lambda k: 42/(1+k)**2 - fp1 = Field(p1, val=spec1(p1.k_lengths)) + p1 = ift.PowerSpace(space1) + p1val = _spec1(p1.k_lengths) + fp1 = ift.Field(p1, val=ift.dobj.from_global_data(p1val)) - p2 = PowerSpace(space2) - spec2 = lambda k: 42/(1+k)**3 - fp2 = Field(p2, val=spec2(p2.k_lengths)) + p2 = ift.PowerSpace(space2) + p2val = _spec2(p2.k_lengths) + fp2 = ift.Field(p2, val=ift.dobj.from_global_data(p2val)) - S_1 = create_power_field(space1, lambda x: np.sqrt(spec1(x))) - S_1 = DiagonalOperator(S_1, domain=fulldomain, spaces=0) - S_2 = create_power_field(space2, lambda x: np.sqrt(spec2(x))) - S_2 = DiagonalOperator(S_2, domain=fulldomain, spaces=1) + S_1 = ift.create_power_field(space1, lambda x: np.sqrt(_spec1(x))) + S_1 = ift.DiagonalOperator(S_1, domain=fulldomain, spaces=0) + S_2 = ift.create_power_field(space2, lambda x: np.sqrt(_spec2(x))) + S_2 = ift.DiagonalOperator(S_2, domain=fulldomain, spaces=1) samples = 500 ps1 = 0. ps2 = 0. for ii in range(samples): - rand_k = Field.from_random('normal', domain=fulldomain) + rand_k = ift.Field.from_random('normal', domain=fulldomain) sk = S_1.times(S_2.times(rand_k)) - sp = power_analyze(sk, spaces=(0, 1), keep_phase_information=False) + sp = ift.power_analyze(sk, spaces=(0, 1), + keep_phase_information=False) ps1 += sp.sum(spaces=1)/fp2.sum() ps2 += sp.sum(spaces=0)/fp1.sum() - assert_allclose(ps1.val/samples, fp1.val, rtol=0.2) - assert_allclose(ps2.val/samples, fp2.val, rtol=0.2) + assert_allclose(ift.dobj.to_global_data(ps1.val/samples), + ift.dobj.to_global_data(fp1.val), rtol=0.2) + assert_allclose(ift.dobj.to_global_data(ps2.val/samples), + ift.dobj.to_global_data(fp2.val), rtol=0.2) def test_vdot(self): - s = RGSpace((10,)) - f1 = Field.from_random("normal", domain=s, dtype=np.complex128) - f2 = Field.from_random("normal", domain=s, dtype=np.complex128) - assert_allclose(f1.vdot(f2), f1.vdot(f2, spaces=0)) + s = ift.RGSpace((10,)) + f1 = ift.Field.from_random("normal", domain=s, dtype=np.complex128) + f2 = ift.Field.from_random("normal", domain=s, dtype=np.complex128) + # assert_allclose(f1.vdot(f2), f1.vdot(f2, spaces=0)) assert_allclose(f1.vdot(f2), np.conj(f2.vdot(f1))) diff --git a/test/test_minimization/test_minimizers.py b/test/test_minimization/test_minimizers.py index 5c24def46..fb5f67c86 100644 --- a/test/test_minimization/test_minimizers.py +++ b/test/test_minimization/test_minimizers.py @@ -1,10 +1,7 @@ import unittest - import numpy as np from numpy.testing import assert_allclose - import nifty2go as ift - from itertools import product from test.common import expand @@ -31,6 +28,6 @@ class Test_Minimizers(unittest.TestCase): (energy, convergence) = minimizer(energy) assert convergence == IC.CONVERGED - assert_allclose(energy.position.val, - 1./covariance_diagonal.val, + assert_allclose(ift.dobj.to_global_data(energy.position.val), + 1./ift.dobj.to_global_data(covariance_diagonal.val), rtol=1e-3, atol=1e-3) diff --git a/test/test_operators/test_adoint.py b/test/test_operators/test_adoint.py index 892861a43..0a0c4b278 100644 --- a/test/test_operators/test_adoint.py +++ b/test/test_operators/test_adoint.py @@ -7,33 +7,37 @@ from numpy.testing import assert_allclose def _check_adjointness(op, dtype=np.float64): - f1 = ift.Field.from_random("normal",domain=op.domain, dtype=dtype) - f2 = ift.Field.from_random("normal",domain=op.target, dtype=dtype) + f1 = ift.Field.from_random("normal", domain=op.domain, dtype=dtype) + f2 = ift.Field.from_random("normal", domain=op.target, dtype=dtype) assert_allclose(f1.vdot(op.adjoint_times(f2)), op.times(f1).vdot(f2), rtol=1e-8) -_harmonic_spaces = [ ift.RGSpace(7, distances=0.2, harmonic=True), - ift.RGSpace((12,46), distances=(0.2, 0.3), harmonic=True), - ift.LMSpace(17) ] +_harmonic_spaces = [ift.RGSpace(7, distances=0.2, harmonic=True), + ift.RGSpace((12, 46), distances=(0.2, 0.3), harmonic=True), + ift.LMSpace(17)] + +_position_spaces = [ift.RGSpace(19, distances=0.7), + ift.RGSpace((1, 2, 3, 6), distances=(0.2, 0.25, 0.34, .8)), + ift.HPSpace(17), + ift.GLSpace(8, 13)] -_position_spaces = [ ift.RGSpace(19, distances=0.7), - ift.RGSpace((1,2,3,6), distances=(0.2,0.25,0.34,0.8)), - ift.HPSpace(17), - ift.GLSpace(8,13) ] class Adjointness_Tests(unittest.TestCase): @expand(product(_harmonic_spaces, [np.float64, np.complex128])) def testPPO(self, sp, dtype): op = ift.PowerProjectionOperator(sp) _check_adjointness(op, dtype) - ps = ift.PowerSpace(sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3)) + ps = ift.PowerSpace( + sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3)) op = ift.PowerProjectionOperator(sp, ps) _check_adjointness(op, dtype) - ps = ift.PowerSpace(sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3)) + ps = ift.PowerSpace( + sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3)) op = ift.PowerProjectionOperator(sp, ps) _check_adjointness(op, dtype) - @expand(product(_harmonic_spaces+_position_spaces, [np.float64, np.complex128])) + @expand(product(_harmonic_spaces+_position_spaces, + [np.float64, np.complex128])) def testFFT(self, sp, dtype): op = ift.FFTOperator(sp) _check_adjointness(op, dtype) diff --git a/test/test_operators/test_composed_operator.py b/test/test_operators/test_composed_operator.py index fe801c383..11bb419f3 100644 --- a/test/test_operators/test_composed_operator.py +++ b/test/test_operators/test_composed_operator.py @@ -1,50 +1,43 @@ import unittest - -from numpy.testing import assert_equal,\ - assert_allclose,\ - assert_approx_equal - -from nifty2go import Field,\ - DiagonalOperator,\ - ComposedOperator - +from numpy.testing import assert_allclose +import nifty2go as ift from test.common import generate_spaces - from itertools import product from test.common import expand + class ComposedOperator_Tests(unittest.TestCase): spaces = generate_spaces() - @expand(product(spaces,spaces)) + @expand(product(spaces, spaces)) def test_times_adjoint_times(self, space1, space2): cspace = (space1, space2) - diag1 = Field.from_random('normal', domain=space1) - diag2 = Field.from_random('normal', domain=space2) - op1 = DiagonalOperator(diag1, cspace, spaces=(0,)) - op2 = DiagonalOperator(diag2, cspace, spaces=(1,)) + 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 = ComposedOperator((op1, op2)) + op = ift.ComposedOperator((op1, op2)) - rand1 = Field.from_random('normal', domain=(space1,space2)) - rand2 = Field.from_random('normal', domain=(space1,space2)) + 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_approx_equal(tt1, tt2) + assert_allclose(tt1, tt2) @expand(product(spaces, spaces)) def test_times_inverse_times(self, space1, space2): cspace = (space1, space2) - diag1 = Field.from_random('normal', domain=space1) - diag2 = Field.from_random('normal', domain=space2) - op1 = DiagonalOperator(diag1, cspace, spaces=(0,)) - op2 = DiagonalOperator(diag2, cspace, spaces=(1,)) + 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 = ComposedOperator((op1, op2)) + op = ift.ComposedOperator((op1, op2)) - rand1 = Field.from_random('normal', domain=(space1, space2)) + rand1 = ift.Field.from_random('normal', domain=(space1, space2)) tt1 = op.inverse_times(op.times(rand1)) - assert_allclose(tt1.val, rand1.val) - + assert_allclose(ift.dobj.to_global_data(tt1.val), + ift.dobj.to_global_data(rand1.val)) diff --git a/test/test_operators/test_diagonal_operator.py b/test/test_operators/test_diagonal_operator.py index 32d379364..1d2eac542 100644 --- a/test/test_operators/test_diagonal_operator.py +++ b/test/test_operators/test_diagonal_operator.py @@ -1,86 +1,81 @@ from __future__ import division import unittest - -import numpy as np -from numpy.testing import assert_equal,\ - assert_allclose,\ - assert_approx_equal - -from nifty2go import Field,\ - DiagonalOperator - +from numpy.testing import assert_equal, assert_allclose +import nifty2go as ift from test.common import generate_spaces - from itertools import product from test.common import expand + class DiagonalOperator_Tests(unittest.TestCase): spaces = generate_spaces() @expand(product(spaces)) def test_property(self, space): - diag = Field.from_random('normal', domain=space) - D = DiagonalOperator(diag) + diag = ift.Field.from_random('normal', domain=space) + D = ift.DiagonalOperator(diag) if D.domain[0] != space: raise TypeError - if D.unitary != False: + if D.unitary: raise TypeError - if D.self_adjoint != True: + if not D.self_adjoint: raise TypeError @expand(product(spaces)) def test_times_adjoint(self, space): - rand1 = Field.from_random('normal', domain=space) - rand2 = Field.from_random('normal', domain=space) - diag = Field.from_random('normal', domain=space) - D = DiagonalOperator(diag) + 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_approx_equal(tt1, tt2) + assert_allclose(tt1, tt2) @expand(product(spaces)) def test_times_inverse(self, space): - rand1 = Field.from_random('normal', domain=space) - diag = Field.from_random('normal', domain=space) - D = DiagonalOperator(diag) + 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.val, tt1.val) + assert_allclose(ift.dobj.to_global_data(rand1.val), + ift.dobj.to_global_data(tt1.val)) @expand(product(spaces)) def test_times(self, space): - rand1 = Field.from_random('normal', domain=space) - diag = Field.from_random('normal', domain=space) - D = DiagonalOperator(diag) + rand1 = ift.Field.from_random('normal', domain=space) + diag = ift.Field.from_random('normal', domain=space) + D = ift.DiagonalOperator(diag) tt = D.times(rand1) assert_equal(tt.domain[0], space) @expand(product(spaces)) def test_adjoint_times(self, space): - rand1 = Field.from_random('normal', domain=space) - diag = Field.from_random('normal', domain=space) - D = DiagonalOperator(diag) + rand1 = ift.Field.from_random('normal', domain=space) + diag = ift.Field.from_random('normal', domain=space) + D = ift.DiagonalOperator(diag) tt = D.adjoint_times(rand1) assert_equal(tt.domain[0], space) @expand(product(spaces)) def test_inverse_times(self, space): - rand1 = Field.from_random('normal', domain=space) - diag = Field.from_random('normal', domain=space) - D = DiagonalOperator(diag) + rand1 = ift.Field.from_random('normal', domain=space) + diag = ift.Field.from_random('normal', domain=space) + D = ift.DiagonalOperator(diag) tt = D.inverse_times(rand1) assert_equal(tt.domain[0], space) @expand(product(spaces)) def test_adjoint_inverse_times(self, space): - rand1 = Field.from_random('normal', domain=space) - diag = Field.from_random('normal', domain=space) - D = DiagonalOperator(diag) + 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) @expand(product(spaces)) def test_diagonal(self, space): - diag = Field.from_random('normal', domain=space) - D = DiagonalOperator(diag) + diag = ift.Field.from_random('normal', domain=space) + D = ift.DiagonalOperator(diag) diag_op = D.diagonal() - assert_allclose(diag.val, diag_op.val) + assert_allclose(ift.dobj.to_global_data(diag.val), + ift.dobj.to_global_data(diag_op.val)) diff --git a/test/test_operators/test_fft_operator.py b/test/test_operators/test_fft_operator.py index 66a725f77..5aad32530 100644 --- a/test/test_operators/test_fft_operator.py +++ b/test/test_operators/test_fft_operator.py @@ -20,12 +20,7 @@ from __future__ import division import unittest import numpy as np from numpy.testing import assert_allclose -from nifty2go import Field,\ - RGSpace,\ - LMSpace,\ - HPSpace,\ - GLSpace,\ - FFTOperator +import nifty2go as ift from itertools import product from test.common import expand @@ -42,75 +37,81 @@ class FFTOperatorTests(unittest.TestCase): [np.float64, np.float32, np.complex64, np.complex128])) def test_fft1D(self, dim1, d, itp): tol = _get_rtol(itp) - a = RGSpace(dim1, distances=d) - b = RGSpace(dim1, distances=1./(dim1*d), harmonic=True) - fft = FFTOperator(domain=a, target=b) + a = ift.RGSpace(dim1, distances=d) + b = ift.RGSpace(dim1, distances=1./(dim1*d), harmonic=True) + fft = ift.FFTOperator(domain=a, target=b) np.random.seed(16) - inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3, - dtype=itp) + inp = ift.Field.from_random(domain=a, random_type='normal', + std=7, mean=3, dtype=itp) out = fft.adjoint_times(fft.times(inp)) - assert_allclose(inp.val, out.val, rtol=tol, atol=tol) + assert_allclose(ift.dobj.to_global_data(inp.val), + ift.dobj.to_global_data(out.val), rtol=tol, atol=tol) @expand(product([12, 15], [9, 12], [0.1, 1, 3.7], [0.4, 1, 2.7], [np.float64, np.float32, np.complex64, np.complex128])) def test_fft2D(self, dim1, dim2, d1, d2, itp): tol = _get_rtol(itp) - a = RGSpace([dim1, dim2], distances=[d1, d2]) - b = RGSpace([dim1, dim2], - distances=[1./(dim1*d1), 1./(dim2*d2)], harmonic=True) - fft = FFTOperator(domain=a, target=b) + a = ift.RGSpace([dim1, dim2], distances=[d1, d2]) + b = ift.RGSpace([dim1, dim2], + distances=[1./(dim1*d1), 1./(dim2*d2)], harmonic=True) + fft = ift.FFTOperator(domain=a, target=b) - inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3, - dtype=itp) + inp = ift.Field.from_random(domain=a, random_type='normal', + std=7, mean=3, dtype=itp) out = fft.adjoint_times(fft.times(inp)) - assert_allclose(inp.val, out.val, rtol=tol, atol=tol) + assert_allclose(ift.dobj.to_global_data(inp.val), + ift.dobj.to_global_data(out.val), rtol=tol, atol=tol) @expand(product([0, 1, 2], [np.float64, np.float32, np.complex64, np.complex128])) def test_composed_fft(self, index, dtype): tol = _get_rtol(dtype) - a = [a1, a2, a3] = [RGSpace((32,)), RGSpace((4, 4)), RGSpace((5, 6))] - fft = FFTOperator(domain=a, space=index) + a = [a1, a2, a3] = [ift.RGSpace((32,)), ift.RGSpace((4, 4)), + ift.RGSpace((5, 6))] + fft = ift.FFTOperator(domain=a, space=index) - inp = Field.from_random(domain=(a1, a2, a3), random_type='normal', - std=7, mean=3, dtype=dtype) + inp = ift.Field.from_random(domain=(a1, a2, a3), random_type='normal', + std=7, mean=3, dtype=dtype) out = fft.adjoint_times(fft.times(inp)) - assert_allclose(inp.val, out.val, rtol=tol, atol=tol) + assert_allclose(ift.dobj.to_global_data(inp.val), + ift.dobj.to_global_data(out.val), rtol=tol, atol=tol) @expand(product([0, 3, 6, 11, 30], [np.float64, np.float32, np.complex64, np.complex128])) def test_sht(self, lm, tp): tol = _get_rtol(tp) - a = LMSpace(lmax=lm) - b = GLSpace(nlat=lm+1) - fft = FFTOperator(domain=a, target=b) - inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3, - dtype=tp) + a = ift.LMSpace(lmax=lm) + b = ift.GLSpace(nlat=lm+1) + fft = ift.FFTOperator(domain=a, target=b) + inp = ift.Field.from_random(domain=a, random_type='normal', + std=7, mean=3, dtype=tp) out = fft.adjoint_times(fft.times(inp)) - assert_allclose(inp.val, out.val, rtol=tol, atol=tol) + assert_allclose(ift.dobj.to_global_data(inp.val), + ift.dobj.to_global_data(out.val), rtol=tol, atol=tol) @expand(product([128, 256], [np.float64, np.float32, np.complex64, np.complex128])) def test_sht2(self, lm, tp): - a = LMSpace(lmax=lm) - b = HPSpace(nside=lm//2) - fft = FFTOperator(domain=a, target=b) - inp = Field.from_random(domain=a, random_type='normal', std=1, mean=0, - dtype=tp) + a = ift.LMSpace(lmax=lm) + b = ift.HPSpace(nside=lm//2) + fft = ift.FFTOperator(domain=a, target=b) + inp = ift.Field.from_random(domain=a, random_type='normal', + std=1, mean=0, dtype=tp) out = fft.adjoint_times(fft.times(inp)) - assert_allclose(inp.val, out.val, rtol=1e-3, atol=1e-1) + assert_allclose(ift.dobj.to_global_data(inp.val), + ift.dobj.to_global_data(out.val), rtol=1e-3, atol=1e-1) @expand(product([128, 256], [np.float64, np.float32, np.complex64, np.complex128])) def test_dotsht(self, lm, tp): tol = _get_rtol(tp) - a = LMSpace(lmax=lm) - b = GLSpace(nlat=lm+1) - fft = FFTOperator(domain=a, target=b) - inp = Field.from_random(domain=a, random_type='normal', std=1, mean=0, - dtype=tp) + a = ift.LMSpace(lmax=lm) + b = ift.GLSpace(nlat=lm+1) + fft = ift.FFTOperator(domain=a, target=b) + inp = ift.Field.from_random(domain=a, random_type='normal', + std=1, mean=0, dtype=tp) out = fft.times(inp) v1 = np.sqrt(out.vdot(out)) v2 = np.sqrt(inp.vdot(fft.adjoint_times(out))) @@ -120,11 +121,11 @@ class FFTOperatorTests(unittest.TestCase): [np.float64, np.float32, np.complex64, np.complex128])) def test_dotsht2(self, lm, tp): tol = _get_rtol(tp) - a = LMSpace(lmax=lm) - b = HPSpace(nside=lm//2) - fft = FFTOperator(domain=a, target=b) - inp = Field.from_random(domain=a, random_type='normal', std=1, mean=0, - dtype=tp) + a = ift.LMSpace(lmax=lm) + b = ift.HPSpace(nside=lm//2) + fft = ift.FFTOperator(domain=a, target=b) + inp = ift.Field.from_random(domain=a, random_type='normal', + std=1, mean=0, dtype=tp) out = fft.times(inp) v1 = np.sqrt(out.vdot(out)) v2 = np.sqrt(inp.vdot(fft.adjoint_times(out))) diff --git a/test/test_operators/test_laplace_operator.py b/test/test_operators/test_laplace_operator.py index b6eea91b7..96cdc298a 100644 --- a/test/test_operators/test_laplace_operator.py +++ b/test/test_operators/test_laplace_operator.py @@ -31,6 +31,5 @@ class LaplaceOperatorTests(unittest.TestCase): bb = ift.PowerSpace.useful_binbounds(s, logarithmic=log1) p = ift.PowerSpace(s, binbounds=bb) L = ift.LaplaceOperator(p, logarithmic=log2) - arr = np.random.random(p.shape[0]) - fp = ift.Field(p, val=arr) + fp = ift.Field.from_random("normal", domain=p, dtype=np.float64) assert_allclose(L(fp).vdot(L(fp)), L.adjoint_times(L(fp)).vdot(fp)) diff --git a/test/test_operators/test_response_operator.py b/test/test_operators/test_response_operator.py index 938b14ac2..e2f395681 100644 --- a/test/test_operators/test_response_operator.py +++ b/test/test_operators/test_response_operator.py @@ -1,33 +1,28 @@ import unittest - -import numpy as np -from numpy.testing import assert_approx_equal - -from nifty2go import Field,\ - RGSpace,\ - ResponseOperator - +from numpy.testing import assert_allclose +import nifty2go as ift from itertools import product from test.common import expand + class ResponseOperator_Tests(unittest.TestCase): - spaces = [RGSpace(128)] + spaces = [ift.RGSpace(128)] - @expand(product(spaces, [0., 5., 1.], [0., 1., .33] )) + @expand(product(spaces, [0., 5., 1.], [0., 1., .33])) def test_property(self, space, sigma, exposure): - op = ResponseOperator(space, sigma=[sigma], - exposure=[exposure]) + op = ift.ResponseOperator(space, sigma=[sigma], + exposure=[exposure]) if op.domain[0] != space: raise TypeError - if op.unitary != False: + if op.unitary: raise ValueError - @expand(product(spaces, [0., 5., 1.], [0., 1., .33] )) + @expand(product(spaces, [0., 5., 1.], [0., 1., .33])) def test_times_adjoint_times(self, space, sigma, exposure): - op = ResponseOperator(space, sigma=[sigma], - exposure=[exposure]) - rand1 = Field.from_random('normal', domain=space) - rand2 = Field.from_random('normal', domain=op.target[0]) + op = ift.ResponseOperator(space, sigma=[sigma], + exposure=[exposure]) + rand1 = ift.Field.from_random('normal', domain=space) + rand2 = ift.Field.from_random('normal', domain=op.target[0]) tt1 = rand2.vdot(op.times(rand1)) tt2 = rand1.vdot(op.adjoint_times(rand2)) - assert_approx_equal(tt1, tt2) + assert_allclose(tt1, tt2) diff --git a/test/test_operators/test_smoothing_operator.py b/test/test_operators/test_smoothing_operator.py index 986e3640c..79ad0c311 100644 --- a/test/test_operators/test_smoothing_operator.py +++ b/test/test_operators/test_smoothing_operator.py @@ -19,11 +19,7 @@ import unittest import numpy as np from numpy.testing import assert_allclose - -from nifty2go import Field,\ - RGSpace,\ - PowerSpace,\ - FFTSmoothingOperator +import nifty2go as ift from itertools import product from test.common import expand @@ -36,11 +32,11 @@ def _get_rtol(tp): class SmoothingOperator_Tests(unittest.TestCase): - spaces = [RGSpace(128)] + spaces = [ift.RGSpace(128)] @expand(product(spaces, [0., .5, 5.])) def test_property(self, space, sigma): - op = FFTSmoothingOperator(space, sigma=sigma) + op = ift.FFTSmoothingOperator(space, sigma=sigma) if op.domain[0] != space: raise TypeError if op.unitary: @@ -50,18 +46,19 @@ class SmoothingOperator_Tests(unittest.TestCase): @expand(product(spaces, [0., .5, 5.])) def test_adjoint_times(self, space, sigma): - op = FFTSmoothingOperator(space, sigma=sigma) - rand1 = Field.from_random('normal', domain=space) - rand2 = Field.from_random('normal', domain=space) + op = ift.FFTSmoothingOperator(space, sigma=sigma) + rand1 = ift.Field.from_random('normal', domain=space) + rand2 = ift.Field.from_random('normal', domain=space) tt1 = rand1.vdot(op.times(rand2)) tt2 = rand2.vdot(op.adjoint_times(rand1)) assert_allclose(tt1, tt2) @expand(product(spaces, [0., .5, 5.])) def test_times(self, space, sigma): - op = FFTSmoothingOperator(space, sigma=sigma) - rand1 = Field.zeros(space) - rand1.val[0] = 1. + op = ift.FFTSmoothingOperator(space, sigma=sigma) + fld = np.zeros(space.shape, dtype=np.float64) + fld[0] = 1. + rand1 = ift.Field(space, ift.dobj.from_global_data(fld)) tt1 = op.times(rand1) assert_allclose(1, tt1.sum()) @@ -69,10 +66,10 @@ class SmoothingOperator_Tests(unittest.TestCase): [np.float64, np.complex128])) def test_smooth_regular1(self, sz, d, sigma, tp): tol = _get_rtol(tp) - sp = RGSpace(sz, distances=d) - smo = FFTSmoothingOperator(sp, sigma=sigma) - inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4, - dtype=tp) + sp = ift.RGSpace(sz, distances=d) + smo = ift.FFTSmoothingOperator(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) @@ -80,9 +77,9 @@ class SmoothingOperator_Tests(unittest.TestCase): [np.float64, np.complex128])) def test_smooth_regular2(self, sz1, sz2, d1, d2, sigma, tp): tol = _get_rtol(tp) - sp = RGSpace([sz1, sz2], distances=[d1, d2]) - smo = FFTSmoothingOperator(sp, sigma=sigma) - inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4, - dtype=tp) + sp = ift.RGSpace([sz1, sz2], distances=[d1, d2]) + smo = ift.FFTSmoothingOperator(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) diff --git a/test/test_spaces/test_gl_space.py b/test/test_spaces/test_gl_space.py index ee670fec5..c626d49be 100644 --- a/test/test_spaces/test_gl_space.py +++ b/test/test_spaces/test_gl_space.py @@ -19,7 +19,6 @@ import unittest import numpy as np import itertools - from numpy.testing import assert_, assert_equal, assert_raises,\ assert_almost_equal from nifty2go import GLSpace diff --git a/test/test_spaces/test_hp_space.py b/test/test_spaces/test_hp_space.py index 5aae3f32b..f77df4933 100644 --- a/test/test_spaces/test_hp_space.py +++ b/test/test_spaces/test_hp_space.py @@ -19,7 +19,6 @@ from __future__ import division import unittest import numpy as np - from numpy.testing import assert_, assert_equal, assert_raises,\ assert_almost_equal from nifty2go import HPSpace diff --git a/test/test_spaces/test_interface.py b/test/test_spaces/test_interface.py index e5cd9f19d..59cd32b9a 100644 --- a/test/test_spaces/test_interface.py +++ b/test/test_spaces/test_interface.py @@ -21,8 +21,7 @@ from numpy.testing import assert_ from itertools import product from types import LambdaType from test.common import expand, generate_spaces, generate_harmonic_spaces -import nifty2go as ift -from nifty2go.spaces import * +from nifty2go import * # ugly, but needed for the eval() below class SpaceInterfaceTests(unittest.TestCase): @@ -35,7 +34,7 @@ class SpaceInterfaceTests(unittest.TestCase): attr_expected_type[1])) @expand(product(generate_harmonic_spaces(), [ - ['get_k_length_array', ift.Field], + ['get_k_length_array', Field], ['get_fft_smoothing_kernel_function', 2.0, LambdaType], ])) def test_method_ret_type(self, space, method_expected_type): diff --git a/test/test_spaces/test_lm_space.py b/test/test_spaces/test_lm_space.py index 624b16759..7ddd8d5b5 100644 --- a/test/test_spaces/test_lm_space.py +++ b/test/test_spaces/test_lm_space.py @@ -19,10 +19,9 @@ from __future__ import division import unittest import numpy as np - from numpy.testing import assert_, assert_equal, assert_raises,\ - assert_almost_equal -from nifty2go import LMSpace + assert_allclose +import nifty2go as ift from test.common import expand # [lmax, expected] @@ -72,7 +71,7 @@ class LMSpaceInterfaceTests(unittest.TestCase): ['mmax', int], ['dim', int]]) def test_property_ret_type(self, attribute, expected_type): - l = LMSpace(7, 5) + l = ift.LMSpace(7, 5) assert_(isinstance(getattr(l, attribute), expected_type)) @@ -81,16 +80,17 @@ class LMSpaceFunctionalityTests(unittest.TestCase): def test_constructor(self, lmax, mmax, expected): if 'error' in expected: with assert_raises(expected['error']): - LMSpace(lmax, mmax) + ift.LMSpace(lmax, mmax) else: - l = LMSpace(lmax, mmax) + l = ift.LMSpace(lmax, mmax) for key, value in expected.items(): assert_equal(getattr(l, key), value) def test_dvol(self): - assert_almost_equal(LMSpace(5).dvol(), 1.) + assert_allclose(ift.LMSpace(5).dvol(), 1.) @expand(get_k_length_array_configs()) def test_k_length_array(self, lmax, expected): - l = LMSpace(lmax) - assert_almost_equal(l.get_k_length_array().val, expected) + l = ift.LMSpace(lmax) + assert_allclose(ift.dobj.to_global_data(l.get_k_length_array().val), + expected) diff --git a/test/test_spaces/test_power_space.py b/test/test_spaces/test_power_space.py index 9735a110a..f5bf40eac 100644 --- a/test/test_spaces/test_power_space.py +++ b/test/test_spaces/test_power_space.py @@ -17,23 +17,21 @@ # and financially supported by the Studienstiftung des deutschen Volkes. from __future__ import division - import unittest import numpy as np - -from numpy.testing import assert_, assert_equal, assert_almost_equal,\ +from numpy.testing import assert_, assert_equal, assert_allclose,\ assert_raises -from nifty2go import PowerSpace, RGSpace, Space, LMSpace +import nifty2go as ift from test.common import expand from itertools import product, chain -HARMONIC_SPACES = [RGSpace((8,), harmonic=True), - RGSpace((7, 8), harmonic=True), - RGSpace((6, 6), harmonic=True), - RGSpace((5, 5), harmonic=True), - RGSpace((4, 5, 7), harmonic=True), - LMSpace(6), - LMSpace(9)] +HARMONIC_SPACES = [ift.RGSpace((8,), harmonic=True), + ift.RGSpace((7, 8), harmonic=True), + ift.RGSpace((6, 6), harmonic=True), + ift.RGSpace((5, 5), harmonic=True), + ift.RGSpace((4, 5, 7), harmonic=True), + ift.LMSpace(6), + ift.LMSpace(9)] # Try all sensible kinds of combinations of spaces and binning parameters @@ -47,23 +45,25 @@ CONSISTENCY_CONFIGS = chain(CONSISTENCY_CONFIGS_IMPLICIT, # [harmonic_partner, logarithmic, nbin, binbounds, expected] CONSTRUCTOR_CONFIGS = [ [1, False, None, None, {'error': (ValueError, NotImplementedError)}], - [RGSpace((8,)), False, None, None, {'error': ValueError}], - [RGSpace((8,), harmonic=True), None, None, None, { + [ift.RGSpace((8,)), False, None, None, {'error': ValueError}], + [ift.RGSpace((8,), harmonic=True), None, None, None, { 'harmonic': False, 'shape': (5,), 'dim': 5, - 'harmonic_partner': RGSpace((8,), harmonic=True), + 'harmonic_partner': ift.RGSpace((8,), harmonic=True), 'binbounds': None, - 'pindex': np.array([0, 1, 2, 3, 4, 3, 2, 1]), + 'pindex': ift.dobj.from_global_data( + np.array([0, 1, 2, 3, 4, 3, 2, 1])), 'k_lengths': np.array([0., 1., 2., 3., 4.]), }], - [RGSpace((8,), harmonic=True), True, None, None, { + [ift.RGSpace((8,), harmonic=True), True, None, None, { 'harmonic': False, 'shape': (4,), 'dim': 4, - 'harmonic_partner': RGSpace((8,), harmonic=True), + 'harmonic_partner': ift.RGSpace((8,), harmonic=True), 'binbounds': (0.5, 1.3228756555322954, 3.5), - 'pindex': np.array([0, 1, 2, 2, 3, 2, 2, 1]), + 'pindex': ift.dobj.from_global_data( + np.array([0, 1, 2, 2, 3, 2, 2, 1])), 'k_lengths': np.array([0., 1., 2.5, 4.]), }], ] @@ -72,20 +72,20 @@ CONSTRUCTOR_CONFIGS = [ def k_lengths_configs(): da_0 = np.array([0, 1.0, 1.41421356, 2., 2.23606798, 2.82842712]) return [ - [RGSpace((4, 4), harmonic=True), da_0], + [ift.RGSpace((4, 4), harmonic=True), da_0], ] class PowerSpaceInterfaceTest(unittest.TestCase): @expand([ - ['harmonic_partner', Space], + ['harmonic_partner', ift.Space], ['binbounds', type(None)], - ['pindex', np.ndarray], + ['pindex', ift.dobj.data_object], ['k_lengths', np.ndarray], ]) def test_property_ret_type(self, attribute, expected_type): - r = RGSpace((4, 4), harmonic=True) - p = PowerSpace(r) + r = ift.RGSpace((4, 4), harmonic=True) + p = ift.PowerSpace(r) assert_(isinstance(getattr(p, attribute), expected_type)) @@ -93,11 +93,12 @@ class PowerSpaceConsistencyCheck(unittest.TestCase): @expand(CONSISTENCY_CONFIGS) def test_rhopindexConsistency(self, harmonic_partner, binbounds, nbin, logarithmic): - bb = PowerSpace.useful_binbounds(harmonic_partner, logarithmic, nbin) - p = PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) + bb = ift.PowerSpace.useful_binbounds(harmonic_partner, logarithmic, + nbin) + p = ift.PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) - assert_equal(np.bincount(p.pindex.ravel()), p.dvol(), - err_msg='rho is not equal to pindex degeneracy') + assert_equal(np.bincount(ift.dobj.to_global_data(p.pindex).ravel()), + p.dvol(), err_msg='rho is not equal to pindex degeneracy') class PowerSpaceFunctionalityTest(unittest.TestCase): @@ -106,29 +107,29 @@ class PowerSpaceFunctionalityTest(unittest.TestCase): logarithmic, nbin, binbounds, expected): if 'error' in expected: with assert_raises(expected['error']): - bb = PowerSpace.useful_binbounds(harmonic_partner, - logarithmic, nbin) - PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) + bb = ift.PowerSpace.useful_binbounds(harmonic_partner, + logarithmic, nbin) + ift.PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) else: - bb = PowerSpace.useful_binbounds(harmonic_partner, - logarithmic, nbin) - p = PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) + 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_almost_equal(getattr(p, key), value) + assert_allclose(getattr(p, key), value) else: assert_equal(getattr(p, key), value) @expand(k_lengths_configs()) def test_k_lengths(self, harmonic_partner, expected): - p = PowerSpace(harmonic_partner=harmonic_partner) - assert_almost_equal(p.k_lengths, expected) + p = ift.PowerSpace(harmonic_partner=harmonic_partner) + assert_allclose(p.k_lengths, expected) def test_dvol(self): - hp = RGSpace(10,harmonic=True) - p = PowerSpace(harmonic_partner=hp) + hp = ift.RGSpace(10, harmonic=True) + p = ift.PowerSpace(harmonic_partner=hp) v1 = hp.dvol() v1 = hp.dim*v1 if np.isscalar(v1) else np.sum(v1) v2 = p.dvol() v2 = p.dim*v2 if np.isscalar(v2) else np.sum(v2) - assert_almost_equal(v1, v2) + assert_allclose(v1, v2) diff --git a/test/test_spaces/test_rg_space.py b/test/test_spaces/test_rg_space.py index c6308b68d..9acb52149 100644 --- a/test/test_spaces/test_rg_space.py +++ b/test/test_spaces/test_rg_space.py @@ -17,12 +17,10 @@ # and financially supported by the Studienstiftung des deutschen Volkes. from __future__ import division - import unittest import numpy as np - -from numpy.testing import assert_, assert_equal, assert_almost_equal -from nifty2go import RGSpace +from numpy.testing import assert_, assert_equal, assert_allclose +import nifty2go as ift from test.common import expand # [shape, distances, harmonic, expected] @@ -97,7 +95,7 @@ def get_dvol_configs(): class RGSpaceInterfaceTests(unittest.TestCase): @expand([['distances', tuple]]) def test_property_ret_type(self, attribute, expected_type): - x = RGSpace(1) + x = ift.RGSpace(1) assert_(isinstance(getattr(x, attribute), expected_type)) @@ -105,16 +103,17 @@ class RGSpaceFunctionalityTests(unittest.TestCase): @expand(CONSTRUCTOR_CONFIGS) def test_constructor(self, shape, distances, harmonic, expected): - x = RGSpace(shape, distances, harmonic) + x = ift.RGSpace(shape, distances, harmonic) for key, value in expected.items(): assert_equal(getattr(x, key), value) @expand(get_k_length_array_configs()) def test_k_length_array(self, shape, distances, expected): - r = RGSpace(shape=shape, distances=distances, harmonic=True) - assert_almost_equal(r.get_k_length_array().val, expected) + r = ift.RGSpace(shape=shape, distances=distances, harmonic=True) + assert_allclose(ift.dobj.to_global_data(r.get_k_length_array().val), + expected) @expand(get_dvol_configs()) def test_dvol(self, shape, distances, harmonic, power): - r = RGSpace(shape=shape, distances=distances, harmonic=harmonic) - assert_almost_equal(r.dvol(), np.prod(r.distances)**power) + r = ift.RGSpace(shape=shape, distances=distances, harmonic=harmonic) + assert_allclose(r.dvol(), np.prod(r.distances)**power) -- GitLab