Commit fa1348a7 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

streamline test cases

parent 7dd3081f
Pipeline #20794 failed with stage
in 4 minutes and 39 seconds
...@@ -18,10 +18,11 @@ ...@@ -18,10 +18,11 @@
from builtins import str from builtins import str
from parameterized import parameterized from parameterized import parameterized
from nifty2go import Space, RGSpace, LMSpace, HPSpace, GLSpace, PowerSpace from nifty2go import RGSpace, LMSpace, HPSpace, GLSpace, PowerSpace
import numpy as np 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): def custom_name_func(testcase_func, param_num, param):
return "%s_%s" % ( return "%s_%s" % (
......
...@@ -4,7 +4,7 @@ from numpy.testing import assert_allclose ...@@ -4,7 +4,7 @@ from numpy.testing import assert_allclose
import nifty2go as ift import nifty2go as ift
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as gfd from nifty2go.dobj import to_ndarray as to_np
spaces = [ift.RGSpace([1024], distances=0.123), ift.HPSpace(32)] spaces = [ift.RGSpace([1024], distances=0.123), ift.HPSpace(32)]
minimizers = [ift.SteepestDescent, ift.RelaxedNewton, ift.VL_BFGS, minimizers = [ift.SteepestDescent, ift.RelaxedNewton, ift.VL_BFGS,
...@@ -29,6 +29,6 @@ class Test_Minimizers(unittest.TestCase): ...@@ -29,6 +29,6 @@ class Test_Minimizers(unittest.TestCase):
(energy, convergence) = minimizer(energy) (energy, convergence) = minimizer(energy)
assert convergence == IC.CONVERGED assert convergence == IC.CONVERGED
assert_allclose(gfd(energy.position.val), assert_allclose(to_np(energy.position.val),
1./gfd(covariance_diagonal.val), 1./to_np(covariance_diagonal.val),
rtol=1e-3, atol=1e-3) rtol=1e-3, atol=1e-3)
...@@ -7,33 +7,37 @@ from numpy.testing import assert_allclose ...@@ -7,33 +7,37 @@ from numpy.testing import assert_allclose
def _check_adjointness(op, dtype=np.float64): def _check_adjointness(op, dtype=np.float64):
f1 = ift.Field.from_random("normal",domain=op.domain, dtype=dtype) f1 = ift.Field.from_random("normal", domain=op.domain, dtype=dtype)
f2 = ift.Field.from_random("normal",domain=op.target, 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), assert_allclose(f1.vdot(op.adjoint_times(f2)), op.times(f1).vdot(f2),
rtol=1e-8) rtol=1e-8)
_harmonic_spaces = [ ift.RGSpace(7, distances=0.2, harmonic=True), _harmonic_spaces = [ift.RGSpace(7, distances=0.2, harmonic=True),
ift.RGSpace((12,46), distances=(0.2, 0.3), harmonic=True), ift.RGSpace((12, 46), distances=(0.2, 0.3), harmonic=True),
ift.LMSpace(17) ] 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): class Adjointness_Tests(unittest.TestCase):
@expand(product(_harmonic_spaces, [np.float64, np.complex128])) @expand(product(_harmonic_spaces, [np.float64, np.complex128]))
def testPPO(self, sp, dtype): def testPPO(self, sp, dtype):
op = ift.PowerProjectionOperator(sp) op = ift.PowerProjectionOperator(sp)
_check_adjointness(op, dtype) _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) op = ift.PowerProjectionOperator(sp, ps)
_check_adjointness(op, dtype) _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) op = ift.PowerProjectionOperator(sp, ps)
_check_adjointness(op, dtype) _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): def testFFT(self, sp, dtype):
op = ift.FFTOperator(sp) op = ift.FFTOperator(sp)
_check_adjointness(op, dtype) _check_adjointness(op, dtype)
import unittest import unittest
from numpy.testing import assert_equal,\ from numpy.testing import assert_allclose
assert_allclose,\ from nifty2go import Field, DiagonalOperator, ComposedOperator
assert_approx_equal
from nifty2go import Field,\
DiagonalOperator,\
ComposedOperator
from test.common import generate_spaces from test.common import generate_spaces
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np from nifty2go.dobj import to_ndarray as to_np
class ComposedOperator_Tests(unittest.TestCase): class ComposedOperator_Tests(unittest.TestCase):
spaces = generate_spaces() spaces = generate_spaces()
@expand(product(spaces,spaces)) @expand(product(spaces, spaces))
def test_times_adjoint_times(self, space1, space2): def test_times_adjoint_times(self, space1, space2):
cspace = (space1, space2) cspace = (space1, space2)
diag1 = Field.from_random('normal', domain=space1) diag1 = Field.from_random('normal', domain=space1)
...@@ -24,12 +20,12 @@ class ComposedOperator_Tests(unittest.TestCase): ...@@ -24,12 +20,12 @@ class ComposedOperator_Tests(unittest.TestCase):
op = ComposedOperator((op1, op2)) op = ComposedOperator((op1, op2))
rand1 = Field.from_random('normal', domain=(space1,space2)) rand1 = Field.from_random('normal', domain=(space1, space2))
rand2 = Field.from_random('normal', domain=(space1,space2)) rand2 = Field.from_random('normal', domain=(space1, space2))
tt1 = rand2.vdot(op.times(rand1)) tt1 = rand2.vdot(op.times(rand1))
tt2 = rand1.vdot(op.adjoint_times(rand2)) tt2 = rand1.vdot(op.adjoint_times(rand2))
assert_approx_equal(tt1, tt2) assert_allclose(tt1, tt2)
@expand(product(spaces, spaces)) @expand(product(spaces, spaces))
def test_times_inverse_times(self, space1, space2): def test_times_inverse_times(self, space1, space2):
...@@ -45,4 +41,3 @@ class ComposedOperator_Tests(unittest.TestCase): ...@@ -45,4 +41,3 @@ class ComposedOperator_Tests(unittest.TestCase):
tt1 = op.inverse_times(op.times(rand1)) tt1 = op.inverse_times(op.times(rand1))
assert_allclose(to_np(tt1.val), to_np(rand1.val)) assert_allclose(to_np(tt1.val), to_np(rand1.val))
from __future__ import division from __future__ import division
import unittest import unittest
import numpy as np from numpy.testing import assert_equal, assert_allclose
from numpy.testing import assert_equal,\
assert_allclose,\
assert_approx_equal
from nifty2go import Field, DiagonalOperator from nifty2go import Field, DiagonalOperator
from test.common import generate_spaces from test.common import generate_spaces
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np from nifty2go.dobj import to_ndarray as to_np
class DiagonalOperator_Tests(unittest.TestCase): class DiagonalOperator_Tests(unittest.TestCase):
spaces = generate_spaces() spaces = generate_spaces()
...@@ -19,9 +17,9 @@ class DiagonalOperator_Tests(unittest.TestCase): ...@@ -19,9 +17,9 @@ class DiagonalOperator_Tests(unittest.TestCase):
D = DiagonalOperator(diag) D = DiagonalOperator(diag)
if D.domain[0] != space: if D.domain[0] != space:
raise TypeError raise TypeError
if D.unitary != False: if D.unitary:
raise TypeError raise TypeError
if D.self_adjoint != True: if not D.self_adjoint:
raise TypeError raise TypeError
@expand(product(spaces)) @expand(product(spaces))
...@@ -32,7 +30,7 @@ class DiagonalOperator_Tests(unittest.TestCase): ...@@ -32,7 +30,7 @@ class DiagonalOperator_Tests(unittest.TestCase):
D = DiagonalOperator(diag) D = DiagonalOperator(diag)
tt1 = rand1.vdot(D.times(rand2)) tt1 = rand1.vdot(D.times(rand2))
tt2 = rand2.vdot(D.times(rand1)) tt2 = rand2.vdot(D.times(rand1))
assert_approx_equal(tt1, tt2) assert_allclose(tt1, tt2)
@expand(product(spaces)) @expand(product(spaces))
def test_times_inverse(self, space): def test_times_inverse(self, space):
......
...@@ -19,17 +19,11 @@ ...@@ -19,17 +19,11 @@
from __future__ import division from __future__ import division
import unittest import unittest
import numpy as np import numpy as np
from numpy.testing import assert_equal,\ from numpy.testing import assert_allclose
assert_allclose from nifty2go import Field, RGSpace, LMSpace, HPSpace, GLSpace, FFTOperator
from nifty2go import Field,\
RGSpace,\
LMSpace,\
HPSpace,\
GLSpace,\
FFTOperator
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np from nifty2go.dobj import to_ndarray as to_np
def _get_rtol(tp): def _get_rtol(tp):
......
...@@ -22,7 +22,6 @@ import nifty2go as ift ...@@ -22,7 +22,6 @@ import nifty2go as ift
from numpy.testing import assert_allclose from numpy.testing import assert_allclose
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
class LaplaceOperatorTests(unittest.TestCase): class LaplaceOperatorTests(unittest.TestCase):
......
import unittest import unittest
from numpy.testing import assert_allclose
import numpy as np import nifty2go as ift
from numpy.testing import assert_approx_equal
from nifty2go import Field,\
RGSpace,\
ResponseOperator
from itertools import product from itertools import product
from test.common import expand from test.common import expand
class ResponseOperator_Tests(unittest.TestCase): 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): def test_property(self, space, sigma, exposure):
op = ResponseOperator(space, sigma=[sigma], op = ift.ResponseOperator(space, sigma=[sigma],
exposure=[exposure]) exposure=[exposure])
if op.domain[0] != space: if op.domain[0] != space:
raise TypeError raise TypeError
if op.unitary != False: if op.unitary:
raise ValueError 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): def test_times_adjoint_times(self, space, sigma, exposure):
op = ResponseOperator(space, sigma=[sigma], op = ift.ResponseOperator(space, sigma=[sigma],
exposure=[exposure]) exposure=[exposure])
rand1 = Field.from_random('normal', domain=space) rand1 = ift.Field.from_random('normal', domain=space)
rand2 = Field.from_random('normal', domain=op.target[0]) rand2 = ift.Field.from_random('normal', domain=op.target[0])
tt1 = rand2.vdot(op.times(rand1)) tt1 = rand2.vdot(op.times(rand1))
tt2 = rand1.vdot(op.adjoint_times(rand2)) tt2 = rand1.vdot(op.adjoint_times(rand2))
assert_approx_equal(tt1, tt2) assert_allclose(tt1, tt2)
...@@ -19,13 +19,7 @@ ...@@ -19,13 +19,7 @@
import unittest import unittest
import numpy as np import numpy as np
from numpy.testing import assert_allclose from numpy.testing import assert_allclose
import nifty2go as ift
from nifty2go import Field,\
RGSpace,\
PowerSpace,\
FFTSmoothingOperator,\
DirectSmoothingOperator
from itertools import product from itertools import product
from test.common import expand from test.common import expand
...@@ -38,11 +32,11 @@ def _get_rtol(tp): ...@@ -38,11 +32,11 @@ def _get_rtol(tp):
class SmoothingOperator_Tests(unittest.TestCase): class SmoothingOperator_Tests(unittest.TestCase):
spaces = [RGSpace(128)] spaces = [ift.RGSpace(128)]
@expand(product(spaces, [0., .5, 5.])) @expand(product(spaces, [0., .5, 5.]))
def test_property(self, space, sigma): def test_property(self, space, sigma):
op = FFTSmoothingOperator(space, sigma=sigma) op = ift.FFTSmoothingOperator(space, sigma=sigma)
if op.domain[0] != space: if op.domain[0] != space:
raise TypeError raise TypeError
if op.unitary: if op.unitary:
...@@ -52,17 +46,17 @@ class SmoothingOperator_Tests(unittest.TestCase): ...@@ -52,17 +46,17 @@ class SmoothingOperator_Tests(unittest.TestCase):
@expand(product(spaces, [0., .5, 5.])) @expand(product(spaces, [0., .5, 5.]))
def test_adjoint_times(self, space, sigma): def test_adjoint_times(self, space, sigma):
op = FFTSmoothingOperator(space, sigma=sigma) op = ift.FFTSmoothingOperator(space, sigma=sigma)
rand1 = Field.from_random('normal', domain=space) rand1 = ift.Field.from_random('normal', domain=space)
rand2 = Field.from_random('normal', domain=space) rand2 = ift.Field.from_random('normal', domain=space)
tt1 = rand1.vdot(op.times(rand2)) tt1 = rand1.vdot(op.times(rand2))
tt2 = rand2.vdot(op.adjoint_times(rand1)) tt2 = rand2.vdot(op.adjoint_times(rand1))
assert_allclose(tt1, tt2) assert_allclose(tt1, tt2)
@expand(product(spaces, [0., .5, 5.])) @expand(product(spaces, [0., .5, 5.]))
def test_times(self, space, sigma): def test_times(self, space, sigma):
op = FFTSmoothingOperator(space, sigma=sigma) op = ift.FFTSmoothingOperator(space, sigma=sigma)
rand1 = Field.zeros(space) rand1 = ift.Field.zeros(space)
rand1.val[0] = 1. rand1.val[0] = 1.
tt1 = op.times(rand1) tt1 = op.times(rand1)
assert_allclose(1, tt1.sum()) assert_allclose(1, tt1.sum())
...@@ -71,10 +65,10 @@ class SmoothingOperator_Tests(unittest.TestCase): ...@@ -71,10 +65,10 @@ class SmoothingOperator_Tests(unittest.TestCase):
[np.float64, np.complex128])) [np.float64, np.complex128]))
def test_smooth_regular1(self, sz, d, sigma, tp): def test_smooth_regular1(self, sz, d, sigma, tp):
tol = _get_rtol(tp) tol = _get_rtol(tp)
sp = RGSpace(sz, distances=d) sp = ift.RGSpace(sz, distances=d)
smo = FFTSmoothingOperator(sp, sigma=sigma) smo = ift.FFTSmoothingOperator(sp, sigma=sigma)
inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4, inp = ift.Field.from_random(domain=sp, random_type='normal', std=1,
dtype=tp) mean=4, dtype=tp)
out = smo(inp) out = smo(inp)
assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol) assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)
...@@ -82,10 +76,10 @@ class SmoothingOperator_Tests(unittest.TestCase): ...@@ -82,10 +76,10 @@ class SmoothingOperator_Tests(unittest.TestCase):
[np.float64, np.complex128])) [np.float64, np.complex128]))
def test_smooth_regular2(self, sz1, sz2, d1, d2, sigma, tp): def test_smooth_regular2(self, sz1, sz2, d1, d2, sigma, tp):
tol = _get_rtol(tp) tol = _get_rtol(tp)
sp = RGSpace([sz1, sz2], distances=[d1, d2]) sp = ift.RGSpace([sz1, sz2], distances=[d1, d2])
smo = FFTSmoothingOperator(sp, sigma=sigma) smo = ift.FFTSmoothingOperator(sp, sigma=sigma)
inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4, inp = ift.Field.from_random(domain=sp, random_type='normal', std=1,
dtype=tp) mean=4, dtype=tp)
out = smo(inp) out = smo(inp)
assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol) assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)
...@@ -93,12 +87,12 @@ class SmoothingOperator_Tests(unittest.TestCase): ...@@ -93,12 +87,12 @@ class SmoothingOperator_Tests(unittest.TestCase):
[np.float64, np.complex128])) [np.float64, np.complex128]))
def test_smooth_irregular1(self, sz, log, sigma, tp): def test_smooth_irregular1(self, sz, log, sigma, tp):
tol = _get_rtol(tp) tol = _get_rtol(tp)
sp = RGSpace(sz, harmonic=True) sp = ift.RGSpace(sz, harmonic=True)
bb = PowerSpace.useful_binbounds(sp, logarithmic=log) bb = ift.PowerSpace.useful_binbounds(sp, logarithmic=log)
ps = PowerSpace(sp, binbounds=bb) ps = ift.PowerSpace(sp, binbounds=bb)
smo = DirectSmoothingOperator(ps, sigma=sigma) smo = ift.DirectSmoothingOperator(ps, sigma=sigma)
inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4, inp = ift.Field.from_random(domain=ps, random_type='normal', std=1,
dtype=tp) mean=4, dtype=tp)
out = smo(inp) out = smo(inp)
assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol) assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)
...@@ -106,11 +100,11 @@ class SmoothingOperator_Tests(unittest.TestCase): ...@@ -106,11 +100,11 @@ class SmoothingOperator_Tests(unittest.TestCase):
[np.float64, np.complex128])) [np.float64, np.complex128]))
def test_smooth_irregular2(self, sz1, sz2, log, sigma, tp): def test_smooth_irregular2(self, sz1, sz2, log, sigma, tp):
tol = _get_rtol(tp) tol = _get_rtol(tp)
sp = RGSpace([sz1, sz2], harmonic=True) sp = ift.RGSpace([sz1, sz2], harmonic=True)
bb = PowerSpace.useful_binbounds(sp, logarithmic=log) bb = ift.PowerSpace.useful_binbounds(sp, logarithmic=log)
ps = PowerSpace(sp, binbounds=bb) ps = ift.PowerSpace(sp, binbounds=bb)
smo = DirectSmoothingOperator(ps, sigma=sigma) smo = ift.DirectSmoothingOperator(ps, sigma=sigma)
inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4, inp = ift.Field.from_random(domain=ps, random_type='normal', std=1,
dtype=tp) mean=4, dtype=tp)
out = smo(inp) out = smo(inp)
assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol) assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
import unittest import unittest
import numpy as np import numpy as np
import itertools import itertools
from numpy.testing import assert_, assert_equal, assert_raises,\ from numpy.testing import assert_, assert_equal, assert_raises,\
assert_almost_equal assert_almost_equal
from nifty2go import GLSpace from nifty2go import GLSpace
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
from __future__ import division from __future__ import division
import unittest import unittest
import numpy as np import numpy as np
from numpy.testing import assert_, assert_equal, assert_raises,\ from numpy.testing import assert_, assert_equal, assert_raises,\
assert_almost_equal assert_almost_equal
from nifty2go import HPSpace from nifty2go import HPSpace
......
...@@ -18,13 +18,11 @@ ...@@ -18,13 +18,11 @@
import unittest import unittest
import numpy as np import numpy as np
from numpy.testing import assert_, assert_equal from numpy.testing import assert_
from itertools import product from itertools import product
from types import LambdaType from types import LambdaType
from test.common import expand, generate_spaces, generate_harmonic_spaces from test.common import expand, generate_spaces, generate_harmonic_spaces
from nifty2go import * # ugly, but needed for the eval() below
from nifty2go.spaces import *
class SpaceInterfaceTests(unittest.TestCase): class SpaceInterfaceTests(unittest.TestCase):
......
...@@ -19,10 +19,9 @@ ...@@ -19,10 +19,9 @@
from __future__ import division from __future__ import division
import unittest import unittest
import numpy as np import numpy as np
from numpy.testing import assert_, assert_equal, assert_raises,\ from numpy.testing import assert_, assert_equal, assert_raises,\
assert_almost_equal, assert_array_almost_equal assert_allclose
from nifty2go import LMSpace import nifty2go as ift
from test.common import expand from test.common import expand
# [lmax, expected] # [lmax, expected]
...@@ -72,7 +71,7 @@ class LMSpaceInterfaceTests(unittest.TestCase): ...@@ -72,7 +71,7 @@ class LMSpaceInterfaceTests(unittest.TestCase):
['mmax', int], ['mmax', int],
['dim', int]]) ['dim', int]])
def test_property_ret_type(self, attribute, expected_type): def test_property_ret_type(self, attribute, expected_type):
l = LMSpace(7) l = ift.LMSpace(7)
assert_(isinstance(getattr(l, attribute), expected_type)) assert_(isinstance(getattr(l, attribute), expected_type))
...@@ -81,16 +80,16 @@ class LMSpaceFunctionalityTests(unittest.TestCase): ...@@ -81,16 +80,16 @@ class LMSpaceFunctionalityTests(unittest.TestCase):
def test_constructor(self, lmax, expected): def test_constructor(self, lmax, expected):
if 'error' in expected: if 'error' in expected:
with assert_raises(expected['error']): with assert_raises(expected['error']):
LMSpace(lmax) ift.LMSpace(lmax)
else: else: