Commit 1a9b8429 authored by Martin Reinecke's avatar Martin Reinecke

Merge branch 'more_tests' into 'NIFTy_5'

More tests

See merge request ift/nifty-dev!200
parents bab4d8c1 4b934985
...@@ -39,9 +39,9 @@ test_serial: ...@@ -39,9 +39,9 @@ test_serial:
script: script:
- pytest-3 -q --cov=nifty5 test - pytest-3 -q --cov=nifty5 test
- > - >
python3 -m coverage report --omit "*plotting*,*distributed_do*" python3 -m coverage report --omit "*plot*,*distributed_do*"
- > - >
python3 -m coverage report --omit "*plotting*,*distributed_do*" | grep TOTAL | awk '{ print "TOTAL: "$4; }' python3 -m coverage report --omit "*plot*,*distributed_do*" | grep TOTAL | awk '{ print "TOTAL: "$4; }'
test_mpi: test_mpi:
stage: test stage: test
......
...@@ -307,7 +307,7 @@ for f in ["sqrt", "exp", "log", "tanh", "conjugate", "sin", "cos", "tan", ...@@ -307,7 +307,7 @@ for f in ["sqrt", "exp", "log", "tanh", "conjugate", "sin", "cos", "tan",
def clip(x, a_min=None, a_max=None): def clip(x, a_min=None, a_max=None):
return data_object(x.shape, np.clip(x.data, a_min, a_max), x.distaxis) return data_object(x.shape, np.clip(x._data, a_min, a_max), x._distaxis)
def from_object(object, dtype, copy, set_locked): def from_object(object, dtype, copy, set_locked):
...@@ -387,10 +387,14 @@ def distaxis(arr): ...@@ -387,10 +387,14 @@ def distaxis(arr):
def from_local_data(shape, arr, distaxis=0): def from_local_data(shape, arr, distaxis=0):
if arr.dtype.kind not in "fciub":
raise TypeError
return data_object(shape, arr, distaxis) return data_object(shape, arr, distaxis)
def from_global_data(arr, sum_up=False, distaxis=0): def from_global_data(arr, sum_up=False, distaxis=0):
if arr.dtype.kind not in "fciub":
raise TypeError
if sum_up: if sum_up:
arr = np_allreduce_sum(arr) arr = np_allreduce_sum(arr)
if distaxis == -1: if distaxis == -1:
......
...@@ -97,10 +97,14 @@ def distaxis(arr): ...@@ -97,10 +97,14 @@ def distaxis(arr):
def from_local_data(shape, arr, distaxis=-1): def from_local_data(shape, arr, distaxis=-1):
if tuple(shape) != arr.shape: if tuple(shape) != arr.shape:
raise ValueError raise ValueError
if arr.dtype.kind not in "fciub":
raise TypeError
return arr return arr
def from_global_data(arr, sum_up=False, distaxis=-1): def from_global_data(arr, sum_up=False, distaxis=-1):
if arr.dtype.kind not in "fciub":
raise TypeError
return arr return arr
......
...@@ -199,12 +199,17 @@ def test_empty_domain(): ...@@ -199,12 +199,17 @@ def test_empty_domain():
def test_trivialities(): def test_trivialities():
s1 = ift.RGSpace((10,)) s1 = ift.RGSpace((10,))
f1 = ift.Field.full(s1, 27) 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.real.local_data)
assert_equal(f1.local_data, (+f1).local_data)
f1 = ift.Field.full(s1, 27. + 3j) 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.real.local_data, 27.)
assert_equal(f1.imag.local_data, 3.) assert_equal(f1.imag.local_data, 3.)
assert_equal(f1.local_data, +f1.local_data)
assert_equal(f1.sum(), f1.sum(0)) 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)) f1 = ift.from_global_data(s1, np.arange(10))
# assert_equal(f1.min(), 0) # assert_equal(f1.min(), 0)
# assert_equal(f1.max(), 9) # assert_equal(f1.max(), 9)
...@@ -266,6 +271,17 @@ def test_err(): ...@@ -266,6 +271,17 @@ def test_err():
f1.vdot(ift.Field.full(s2, 1.)) f1.vdot(ift.Field.full(s2, 1.))
with assert_raises(TypeError): with assert_raises(TypeError):
ift.full(s1, [2, 3]) 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_stdfunc(): def test_stdfunc():
...@@ -323,3 +339,17 @@ def test_funcs(num, dom, func): ...@@ -323,3 +339,17 @@ def test_funcs(num, dom, func):
res = getattr(f, func)() res = getattr(f, func)()
res2 = getattr(np, func)(num) res2 = getattr(np, func)(num)
assert_allclose(res.local_data, res2) assert_allclose(res.local_data, res2)
@pmp('rtype', ['normal', 'pm1', 'uniform'])
@pmp('dtype', [np.float64, np.complex128])
def test_from_random(rtype, dtype):
sp = ift.RGSpace(3)
f = ift.Field.from_random(rtype, sp, dtype=dtype)
def test_field_of_objects():
arr = np.array(['x', 'y', 'z'])
sp = ift.RGSpace(3)
with assert_raises(TypeError):
f = ift.Field.from_global_data(sp, arr)
...@@ -74,6 +74,48 @@ def test_quadratic_minimization(minimizer, space): ...@@ -74,6 +74,48 @@ def test_quadratic_minimization(minimizer, space):
atol=1e-3) atol=1e-3)
@pmp('space', spaces)
def test_WF_curvature(space):
np.random.seed(42)
starting_point = ift.Field.from_random('normal', domain=space)*10
required_result = ift.full(space, 1.)
s = ift.Field.from_random('uniform', domain=space) + 0.5
S = ift.DiagonalOperator(s)
r = ift.Field.from_random('uniform', domain=space)
R = ift.DiagonalOperator(r)
n = ift.Field.from_random('uniform', domain=space) + 0.5
N = ift.DiagonalOperator(n)
all_diag = 1./s + r**2/n
curv = ift.WienerFilterCurvature(R, N, S, iteration_controller=IC,
iteration_controller_sampling=IC)
m = curv.inverse(required_result)
assert_allclose(
m.local_data,
1./all_diag.local_data,
rtol=1e-3,
atol=1e-3)
curv.draw_sample()
curv.draw_sample(from_inverse=True)
if len(space.shape) == 1:
R = ift.ValueInserter(space, [0])
n = ift.from_random('uniform', R.domain) + 0.5
N = ift.DiagonalOperator(n)
all_diag = 1./s + R(1/n)
curv = ift.WienerFilterCurvature(R.adjoint, N, S,
iteration_controller=IC,
iteration_controller_sampling=IC)
m = curv.inverse(required_result)
assert_allclose(
m.local_data,
1./all_diag.local_data,
rtol=1e-3,
atol=1e-3)
curv.draw_sample()
curv.draw_sample(from_inverse=True)
@pmp('minimizer', minimizers + newton_minimizers) @pmp('minimizer', minimizers + newton_minimizers)
def test_rosenbrock(minimizer): def test_rosenbrock(minimizer):
try: try:
......
...@@ -35,11 +35,25 @@ def test_func(): ...@@ -35,11 +35,25 @@ def test_func():
ift.log(ift.exp((f1)))["d1"].local_data, f1["d1"].local_data) ift.log(ift.exp((f1)))["d1"].local_data, f1["d1"].local_data)
def test_multifield_field_consistency():
f1 = ift.full(dom, 27)
f2 = ift.from_global_data(dom['d1'], f1['d1'].to_global_data())
assert_equal(f1.sum(), f2.sum())
assert_equal(f1.size, f2.size)
def test_dataconv(): def test_dataconv():
f1 = ift.full(dom, 27) f1 = ift.full(dom, 27)
f2 = ift.from_global_data(dom, f1.to_global_data()) f2 = ift.from_global_data(dom, f1.to_global_data())
for key, val in f1.items(): for key, val in f1.items():
assert_equal(val.local_data, f2[key].local_data) assert_equal(val.local_data, f2[key].local_data)
if "d1" not in f2:
raise KeyError()
assert_equal({"d1": f1}, f2.to_dict())
f3 = ift.full(dom, 27+1.j)
f4 = ift.full(dom, 1.j)
assert_equal(f2, f3.real)
assert_equal(f4, f3.imag)
def test_blockdiagonal(): def test_blockdiagonal():
......
...@@ -58,6 +58,8 @@ def testOperatorCombinations(sp, dtype): ...@@ -58,6 +58,8 @@ def testOperatorCombinations(sp, dtype):
ift.extra.consistency_check(op, dtype, dtype) ift.extra.consistency_check(op, dtype, dtype)
op = a + b op = a + b
ift.extra.consistency_check(op, dtype, dtype) ift.extra.consistency_check(op, dtype, dtype)
op = a - b
ift.extra.consistency_check(op, dtype, dtype)
def testLinearInterpolator(): def testLinearInterpolator():
......
...@@ -21,7 +21,7 @@ from numpy.testing import assert_ ...@@ -21,7 +21,7 @@ from numpy.testing import assert_
import nifty5 as ift import nifty5 as ift
from .common import list2fixture from ..common import list2fixture
pmp = pytest.mark.parametrize pmp = pytest.mark.parametrize
space = list2fixture([ space = list2fixture([
...@@ -81,6 +81,12 @@ def testBinary(type1, type2, space, seed): ...@@ -81,6 +81,12 @@ def testBinary(type1, type2, space, seed):
pos = ift.from_random("normal", dom) pos = ift.from_random("normal", dom)
model = ift.OuterProduct(pos['s1'], ift.makeDomain(space)) model = ift.OuterProduct(pos['s1'], ift.makeDomain(space))
ift.extra.check_value_gradient_consistency(model, pos['s2'], ntries=20) ift.extra.check_value_gradient_consistency(model, pos['s2'], ntries=20)
model = select_s1**2
pos = ift.from_random("normal", dom1)
ift.extra.check_value_gradient_consistency(model, pos, ntries=20)
model = select_s1.clip(-1, 1)
pos = ift.from_random("normal", dom1)
ift.extra.check_value_gradient_consistency(model, pos, ntries=20)
if isinstance(space, ift.RGSpace): if isinstance(space, ift.RGSpace):
model = ift.FFTOperator(space)(select_s1*select_s2) model = ift.FFTOperator(space)(select_s1*select_s2)
pos = ift.from_random("normal", dom) pos = ift.from_random("normal", dom)
...@@ -121,9 +127,9 @@ def testPointModel(space, seed): ...@@ -121,9 +127,9 @@ def testPointModel(space, seed):
@pmp('target', [ @pmp('target', [
ift.RGSpace(64, distances=.789,harmonic=True), ift.RGSpace(64, distances=.789, harmonic=True),
ift.RGSpace([32, 32], distances=.789,harmonic=True), ift.RGSpace([32, 32], distances=.789, harmonic=True),
ift.RGSpace([32, 32, 8], distances=.789,harmonic=True) ift.RGSpace([32, 32, 8], distances=.789, harmonic=True)
]) ])
@pmp('causal', [True, False]) @pmp('causal', [True, False])
@pmp('minimum_phase', [True, False]) @pmp('minimum_phase', [True, False])
......
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
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):
op.__repr__()
@pmp('sp', _p_RG_spaces)
def testLOSResponse(sp, dtype):
starts = np.random.randn(len(sp.shape), 10)
ends = np.random.randn(len(sp.shape), 10)
sigma_low = 1e-4*np.random.randn(10)
sigma_ups = 1e-5*np.random.randn(10)
_check_repr(ift.LOSResponse(sp, starts, ends, sigma_low, sigma_ups))
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testOperatorCombinations(sp, dtype):
a = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
b = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
_check_repr(ift.SandwichOperator.make(a, b))
_check_repr(a(b))
_check_repr(a+b)
_check_repr(a-b)
_check_repr(a*b)
_check_repr(a**2)
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))
@pmp('args', [(ift.RGSpace(10, harmonic=True), 4, 0), (ift.RGSpace(
(24, 31), distances=(0.4, 2.34), harmonic=True), 3, 0),
(ift.LMSpace(4), 10, 0)])
def testSlopeOperator(args, dtype):
tmp = ift.ExpTransform(ift.PowerSpace(args[0]), args[1], args[2])
tgt = tmp.domain[0]
_check_repr(ift.SlopeOperator(tgt))
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testOperatorAdaptor(sp, dtype):
op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
_check_repr(op.adjoint)
_check_repr(op.inverse)
_check_repr(op.inverse.adjoint)
@pmp('sp1', _h_spaces + _p_spaces + _pow_spaces)
@pmp('sp2', _h_spaces + _p_spaces + _pow_spaces)
def testNullOperator(sp1, sp2, dtype):
op = ift.NullOperator(sp1, sp2)
ift.extra.consistency_check(op, dtype, dtype)
mdom1 = ift.MultiDomain.make({'a': sp1})
mdom2 = ift.MultiDomain.make({'b': sp2})
_check_repr(ift.NullOperator(mdom1, mdom2))
_check_repr(ift.NullOperator(sp1, mdom2))
_check_repr(ift.NullOperator(mdom1, sp2))
@pmp('sp', _p_RG_spaces)
def testHarmonicSmoothingOperator(sp, dtype):
_check_repr(ift.HarmonicSmoothingOperator(sp, 0.1))
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testDOFDistributor(sp, dtype):
# TODO: Test for DomainTuple
if sp.size < 4:
return
dofdex = np.arange(sp.size).reshape(sp.shape) % 3
dofdex = ift.Field.from_global_data(sp, dofdex)
_check_repr(ift.DOFDistributor(dofdex))
@pmp('sp', _h_spaces)
def testPPO(sp, dtype):
_check_repr(ift.PowerDistributor(target=sp))
ps = ift.PowerSpace(
sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3))
_check_repr(ift.PowerDistributor(target=sp, power_space=ps))
ps = ift.PowerSpace(
sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3))
_check_repr(ift.PowerDistributor(target=sp, power_space=ps))
@pmp('sp', _h_RG_spaces + _p_RG_spaces)
def testFFT(sp, dtype):
_check_repr(ift.FFTOperator(sp))
_check_repr(ift.FFTOperator(sp.get_default_codomain()))
@pmp('sp', _h_RG_spaces + _p_RG_spaces)
def testHartley(sp, dtype):
_check_repr(ift.HartleyOperator(sp))
_check_repr(ift.HartleyOperator(sp.get_default_codomain()))
@pmp('sp', _h_spaces)
def testHarmonic(sp, dtype):
_check_repr(ift.HarmonicTransformOperator(sp))
@pmp('sp', _p_spaces)
def testMask(sp, dtype):
# Create mask
f = ift.from_random('normal', sp).to_global_data()
mask = np.zeros_like(f)
mask[f > 0] = 1
mask = ift.Field.from_global_data(sp, mask)
# Test MaskOperator
_check_repr(ift.MaskOperator(mask))
@pmp('sp', _h_spaces + _p_spaces)
def testDiagonal(sp, dtype):
op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype))
_check_repr(op)
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
def testGeometryRemover(sp, dtype):
_check_repr(ift.GeometryRemover(sp))
@pmp('spaces', [0, 1, 2, 3, (0, 1), (0, 2), (0, 1, 2), (0, 2, 3), (1, 3)])
@pmp('wgt', [0, 1, 2, -1])
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))
def testDomainTupleFieldInserter():
target = ift.DomainTuple.make((ift.UnstructuredDomain([3, 2]),
ift.UnstructuredDomain(7),
ift.RGSpace([4, 22])))
_check_repr(ift.DomainTupleFieldInserter(target, 1, (5,)))
@pmp('space', [0, 2])
def testSymmetrizingOperator(space, dtype):
dom = (ift.LogRGSpace(10, [2.], [1.]), ift.UnstructuredDomain(13),
ift.LogRGSpace((5, 27), [1., 2.7], [0., 4.]), ift.HPSpace(4))
_check_repr(ift.SymmetrizingOperator(dom, space))
@pmp('space', [0, 2])
@pmp('factor', [1, 2, 2.7])
@pmp('central', [False, True])
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))
@pmp('args',
[(ift.RGSpace(10, harmonic=True), 4, 0), (ift.RGSpace(
(24, 31), distances=(0.4, 2.34), harmonic=True), (4, 3), 0),
((ift.HPSpace(4), ift.RGSpace(27, distances=0.3, harmonic=True)),
(10,), 1),
(ift.PowerSpace(ift.RGSpace(10, distances=0.3, harmonic=True)), 6, 0)])
def testExpTransform(args, dtype):
_check_repr(ift.ExpTransform(args[0], args[1], args[2]))
@pmp('args',
[(ift.LogRGSpace([10, 17], [2., 3.], [1., 0.]), 0),
((ift.LogRGSpace(10, [2.], [1.]), ift.UnstructuredDomain(13)), 0),
((ift.UnstructuredDomain(13), ift.LogRGSpace(17, [3.], [.7])), 1)])
def testQHTOperator(args):
dtype = np.float64
tgt = ift.DomainTuple.make(args[0])
_check_repr(ift.QHTOperator(tgt, args[1]))
@pmp('args', [[ift.RGSpace(
(13, 52, 40)), (4, 6, 25), None], [ift.RGSpace(
(128, 128)), (45, 48), 0], [ift.RGSpace(13), (7,), None], [
(ift.HPSpace(3), ift.RGSpace((12, 24), distances=0.3)), (12, 12), 1
]])
def testRegridding(args):
_check_repr(ift.RegriddingOperator(*args))
@pmp(
'fdomain',
[
ift.DomainTuple.make((ift.RGSpace(
(3, 5, 4)), ift.RGSpace((16,), distances=(7.,))),),
ift.DomainTuple.make(ift.HPSpace(12),)
],
)
@pmp('domain', [
ift.DomainTuple.make((ift.RGSpace((2,)), ift.GLSpace(10)),),
ift.DomainTuple.make(ift.RGSpace((10, 12), distances=(0.1, 1.)),)
])
def testOuter(fdomain, domain):
f = ift.from_random('normal', fdomain)
_check_repr(ift.OuterProduct(f, domain))
@pmp('sp', _h_spaces + _p_spaces + _pow_spaces)
@pmp('seed', [12, 3])
def testValueInserter(sp, seed):
np.random.seed(seed)
ind = []
for ss in sp.shape:
if ss == 1:
ind.append(0)
else:
ind.append(np.random.randint(0, ss-1))
_check_repr(ift.ValueInserter(sp, ind))
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_equal, assert_raises
import nifty5 as ift
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.)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment