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 @@
from builtins import str
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
np.seterr(all='raise',under='ignore')
np.seterr(all='raise', under='ignore')
def custom_name_func(testcase_func, param_num, param):
return "%s_%s" % (
......
......@@ -4,7 +4,7 @@ from numpy.testing import assert_allclose
import nifty2go as ift
from itertools import product
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)]
minimizers = [ift.SteepestDescent, ift.RelaxedNewton, ift.VL_BFGS,
......@@ -29,6 +29,6 @@ class Test_Minimizers(unittest.TestCase):
(energy, convergence) = minimizer(energy)
assert convergence == IC.CONVERGED
assert_allclose(gfd(energy.position.val),
1./gfd(covariance_diagonal.val),
assert_allclose(to_np(energy.position.val),
1./to_np(covariance_diagonal.val),
rtol=1e-3, atol=1e-3)
......@@ -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)
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
from nifty2go import Field, DiagonalOperator, ComposedOperator
from test.common import generate_spaces
from itertools import product
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):
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)
......@@ -24,12 +20,12 @@ class ComposedOperator_Tests(unittest.TestCase):
op = ComposedOperator((op1, op2))
rand1 = Field.from_random('normal', domain=(space1,space2))
rand2 = Field.from_random('normal', domain=(space1,space2))
rand1 = Field.from_random('normal', domain=(space1, space2))
rand2 = 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):
......@@ -45,4 +41,3 @@ class ComposedOperator_Tests(unittest.TestCase):
tt1 = op.inverse_times(op.times(rand1))
assert_allclose(to_np(tt1.val), to_np(rand1.val))
from __future__ import division
import unittest
import numpy as np
from numpy.testing import assert_equal,\
assert_allclose,\
assert_approx_equal
from numpy.testing import assert_equal, assert_allclose
from nifty2go import Field, DiagonalOperator
from test.common import generate_spaces
from itertools import product
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):
spaces = generate_spaces()
......@@ -19,9 +17,9 @@ class DiagonalOperator_Tests(unittest.TestCase):
D = 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))
......@@ -32,7 +30,7 @@ class DiagonalOperator_Tests(unittest.TestCase):
D = 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):
......
......@@ -19,17 +19,11 @@
from __future__ import division
import unittest
import numpy as np
from numpy.testing import assert_equal,\
assert_allclose
from nifty2go import Field,\
RGSpace,\
LMSpace,\
HPSpace,\
GLSpace,\
FFTOperator
from numpy.testing import assert_allclose
from nifty2go import Field, RGSpace, LMSpace, HPSpace, GLSpace, FFTOperator
from itertools import product
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):
......
......@@ -22,7 +22,6 @@ import nifty2go as ift
from numpy.testing import assert_allclose
from itertools import product
from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
class LaplaceOperatorTests(unittest.TestCase):
......
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)
......@@ -19,13 +19,7 @@
import unittest
import numpy as np
from numpy.testing import assert_allclose
from nifty2go import Field,\
RGSpace,\
PowerSpace,\
FFTSmoothingOperator,\
DirectSmoothingOperator
import nifty2go as ift
from itertools import product
from test.common import expand
......@@ -38,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:
......@@ -52,17 +46,17 @@ 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)
op = ift.FFTSmoothingOperator(space, sigma=sigma)
rand1 = ift.Field.zeros(space)
rand1.val[0] = 1.
tt1 = op.times(rand1)
assert_allclose(1, tt1.sum())
......@@ -71,10 +65,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)
......@@ -82,10 +76,10 @@ 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)
......@@ -93,12 +87,12 @@ class SmoothingOperator_Tests(unittest.TestCase):
[np.float64, np.complex128]))
def test_smooth_irregular1(self, sz, log, sigma, tp):
tol = _get_rtol(tp)
sp = RGSpace(sz, harmonic=True)
bb = PowerSpace.useful_binbounds(sp, logarithmic=log)
ps = PowerSpace(sp, binbounds=bb)
smo = DirectSmoothingOperator(ps, sigma=sigma)
inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4,
dtype=tp)
sp = ift.RGSpace(sz, harmonic=True)
bb = ift.PowerSpace.useful_binbounds(sp, logarithmic=log)
ps = ift.PowerSpace(sp, binbounds=bb)
smo = ift.DirectSmoothingOperator(ps, sigma=sigma)
inp = ift.Field.from_random(domain=ps, random_type='normal', std=1,
mean=4, dtype=tp)
out = smo(inp)
assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)
......@@ -106,11 +100,11 @@ class SmoothingOperator_Tests(unittest.TestCase):
[np.float64, np.complex128]))
def test_smooth_irregular2(self, sz1, sz2, log, sigma, tp):
tol = _get_rtol(tp)
sp = RGSpace([sz1, sz2], harmonic=True)
bb = PowerSpace.useful_binbounds(sp, logarithmic=log)
ps = PowerSpace(sp, binbounds=bb)
smo = DirectSmoothingOperator(ps, sigma=sigma)
inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4,
dtype=tp)
sp = ift.RGSpace([sz1, sz2], harmonic=True)
bb = ift.PowerSpace.useful_binbounds(sp, logarithmic=log)
ps = ift.PowerSpace(sp, binbounds=bb)
smo = ift.DirectSmoothingOperator(ps, sigma=sigma)
inp = ift.Field.from_random(domain=ps, random_type='normal', std=1,
mean=4, dtype=tp)
out = smo(inp)
assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)
......@@ -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
......
......@@ -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
......
......@@ -18,13 +18,11 @@
import unittest
import numpy as np
from numpy.testing import assert_, assert_equal
from numpy.testing import assert_
from itertools import product
from types import LambdaType
from test.common import expand, generate_spaces, generate_harmonic_spaces
from nifty2go.spaces import *
from nifty2go import * # ugly, but needed for the eval() below
class SpaceInterfaceTests(unittest.TestCase):
......
......@@ -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, assert_array_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)
l = ift.LMSpace(7)
assert_(isinstance(getattr(l, attribute), expected_type))
......@@ -81,16 +80,16 @@ class LMSpaceFunctionalityTests(unittest.TestCase):
def test_constructor(self, lmax, expected):
if 'error' in expected:
with assert_raises(expected['error']):
LMSpace(lmax)
ift.LMSpace(lmax)
else:
l = LMSpace(lmax)
l = ift.LMSpace(lmax)
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(), expected)
l = ift.LMSpace(lmax)
assert_allclose(l.get_k_length_array(), expected)
......@@ -17,24 +17,22 @@
# 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, dobj
import nifty2go as ift
from test.common import expand
from itertools import product, chain
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
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
......@@ -48,21 +46,21 @@ 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': from_np(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': from_np(np.array([0, 1, 2, 2, 3, 2, 2, 1])),
'k_lengths': np.array([0., 1., 2.5, 4.]),
......@@ -73,20 +71,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', dobj.data_object],
['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))
......@@ -94,8 +92,9 @@ 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(to_np(p.pindex.ravel())), p.dvol(),
err_msg='rho is not equal to pindex degeneracy')
......@@ -107,29 +106,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)