Commit 5e0f300b authored by Philipp Arras's avatar Philipp Arras
Browse files

More tests

parent a63ef1fc
Pipeline #24128 failed with stage
in 3 minutes and 58 seconds
......@@ -24,17 +24,10 @@ from test.common import expand
from numpy.testing import assert_allclose
_harmonic_spaces = [ift.RGSpace(7, distances=0.2, harmonic=True),
ift.RGSpace((12, 46), distances=(0.2, 0.3), harmonic=True),
ift.LMSpace(17)]
# TODO Add also other space types
_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)]
class Energy_Tests(unittest.TestCase):
class Map_Energy_Tests(unittest.TestCase):
@expand(product([ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[ift.library.Exponential, ift.library.Linear]))
......@@ -76,99 +69,47 @@ class Energy_Tests(unittest.TestCase):
assert_allclose(a, b, rtol=tol, atol=tol)
@expand(product([ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[ift.library.Exponential, ift.library.Linear]))
def testNonlinearPower(self, space, nonlinearity):
f = nonlinearity()
dim = len(space.shape)
fft = ift.FFTOperator(space)
hspace = fft.target[0]
binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=True)
pspace = ift.PowerSpace(hspace, binbounds=binbounds)
P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
xi = ift.Field.from_random(domain=hspace, random_type='normal')
def pspec(k): return 1 / (1 + k**2)**dim
tau0 = ift.PS_field(pspace, pspec)
A = P.adjoint_times(ift.sqrt(tau0))
n = ift.Field.from_random(domain=space, random_type='normal')
s = fft.inverse_times(xi * A)
diag = ift.Field.ones(space) * 10
R = ift.DiagonalOperator(diag)
diag = ift.Field.ones(space)
N = ift.DiagonalOperator(diag)
d = R(f(s)) + n
direction = ift.Field.from_random('normal', pspace)
direction /= np.sqrt(direction.var())
eps = 1e-10
tau1 = tau0 + eps * direction
IC = ift.GradientNormController(name='IC', verbose=False, iteration_limit=100, tol_abs_gradnorm=1e-5)
inverter = ift.ConjugateGradient(IC)
S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
D = ift.library.NonlinearWienerFilterEnergy(position=xi, d=d, Instrument=R, nonlinearity=f, FFT=fft, power=A, N=N, S=S, inverter=inverter).curvature
energy0 = ift.library.NonlinearPowerEnergy(
position=tau0, d=d, m=xi, D=D, Instrument=R, Projection=P, nonlinearity=f, FFT=fft, N=N, inverter=inverter)
energy1 = ift.library.NonlinearPowerEnergy(
position=tau1, d=d, m=xi, D=D, Instrument=R, Projection=P, nonlinearity=f, FFT=fft, N=N, inverter=inverter)
a = (energy1.value - energy0.value) / eps
b = energy0.gradient.vdot(direction)
tol = 1e-2
assert_allclose(a, b, rtol=tol, atol=tol)
@expand(product([ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[ift.library.Exponential, ift.library.Linear]))
def testNoise(self, space, nonlinearity):
f = nonlinearity()
ift.RGSpace([32, 32], distances=.789)]))
def testLinearMap(self, space):
dim = len(space.shape)
fft = ift.FFTOperator(space)
hspace = fft.target[0]
hspace = space.get_default_codomain()
ht = ift.HarmonicTransformOperator(hspace, target=space)
binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
pspace = ift.PowerSpace(hspace, binbounds=binbounds)
P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
xi = ift.Field.from_random(domain=hspace, random_type='normal')
xi0 = ift.Field.from_random(domain=hspace, random_type='normal')
def pspec(k): return 1 / (1 + k**2)**dim
tau = ift.PS_field(pspace, pspec)
A = P.adjoint_times(ift.sqrt(tau))
pspec = ift.PS_field(pspace, pspec)
A = P.adjoint_times(ift.sqrt(pspec))
n = ift.Field.from_random(domain=space, random_type='normal')
s = fft.inverse_times(xi * A)
s0 = xi0 * A
diag = ift.Field.ones(space) * 10
R = ift.DiagonalOperator(diag)
Instrument = ift.DiagonalOperator(diag)
R = Instrument * ht
diag = ift.Field.ones(space)
eta0 = ift.log(diag)
N = ift.DiagonalOperator(diag)
d = R(f(s)) + n
alpha = ift.Field(d.domain, val=2.)
q = ift.Field(d.domain, val=1e-5)
d = R(s0) + n
direction = ift.Field.from_random('normal', d.domain)
direction = ift.Field.from_random('normal', hspace)
direction /= np.sqrt(direction.var())
eps = 1e-10
eta1 = eta0 + eps * direction
s1 = s0 + eps * direction
IC = ift.GradientNormController(name='IC', verbose=False, iteration_limit=100, tol_abs_gradnorm=1e-5)
IC = ift.GradientNormController(
name='IC',
verbose=False,
iteration_limit=100,
tol_abs_gradnorm=1e-5)
inverter = ift.ConjugateGradient(IC)
S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
D = ift.library.NonlinearWienerFilterEnergy(position=xi, d=d, Instrument=R, nonlinearity=f, FFT=fft, power=A, N=N, S=S, inverter=inverter).curvature
energy0 = ift.library.NoiseEnergy(
position=eta0, d=d, m=xi, D=D, t=tau, Instrument=R,
alpha=alpha, q=q, Projection=P, nonlinearity=f,
FFT=fft, samples=3)
energy1 = ift.library.NoiseEnergy(
position=eta1, d=d, m=xi, D=D, t=tau, Instrument=R,
alpha=alpha, q=q, Projection=P, nonlinearity=f,
FFT=fft, samples=3)
energy0 = ift.library.WienerFilterEnergy(
position=s0, d=d, R=R, N=N, S=S, inverter=inverter)
energy1 = ift.library.WienerFilterEnergy(
position=s1, d=d, R=R, N=N, S=S, inverter=inverter)
a = (energy1.value - energy0.value) / eps
b = energy0.gradient.vdot(direction)
tol = 1e-2
tol = 1e-3
assert_allclose(a, b, rtol=tol, atol=tol)
# 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-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
import unittest
import nifty4 as ift
import numpy as np
from itertools import product
from test.common import expand
from numpy.testing import assert_allclose
# TODO Add also other space types
class Noise_Energy_Tests(unittest.TestCase):
@expand(product([ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[ift.library.Exponential, ift.library.Linear]))
def testNoise(self, space, nonlinearity):
f = nonlinearity()
dim = len(space.shape)
fft = ift.FFTOperator(space)
hspace = fft.target[0]
binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=False)
pspace = ift.PowerSpace(hspace, binbounds=binbounds)
P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
xi = ift.Field.from_random(domain=hspace, random_type='normal')
def pspec(k): return 1 / (1 + k**2)**dim
tau = ift.PS_field(pspace, pspec)
A = P.adjoint_times(ift.sqrt(tau))
n = ift.Field.from_random(domain=space, random_type='normal')
s = fft.inverse_times(xi * A)
diag = ift.Field.ones(space) * 10
R = ift.DiagonalOperator(diag)
diag = ift.Field.ones(space)
eta0 = ift.log(diag)
N = ift.DiagonalOperator(diag)
d = R(f(s)) + n
alpha = ift.Field(d.domain, val=2.)
q = ift.Field(d.domain, val=1e-5)
direction = ift.Field.from_random('normal', d.domain)
direction /= np.sqrt(direction.var())
eps = 1e-10
eta1 = eta0 + eps * direction
IC = ift.GradientNormController(
name='IC',
verbose=False,
iteration_limit=100,
tol_abs_gradnorm=1e-5)
inverter = ift.ConjugateGradient(IC)
S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
D = ift.library.NonlinearWienerFilterEnergy(
position=xi,
d=d,
Instrument=R,
nonlinearity=f,
FFT=fft,
power=A,
N=N,
S=S,
inverter=inverter).curvature
energy0 = ift.library.NoiseEnergy(
position=eta0, d=d, m=xi, D=D, t=tau, Instrument=R,
alpha=alpha, q=q, Projection=P, nonlinearity=f,
FFT=fft, samples=3)
energy1 = ift.library.NoiseEnergy(
position=eta1, d=d, m=xi, D=D, t=tau, Instrument=R,
alpha=alpha, q=q, Projection=P, nonlinearity=f,
FFT=fft, samples=3)
a = (energy1.value - energy0.value) / eps
b = energy0.gradient.vdot(direction)
tol = 1e-2
assert_allclose(a, b, rtol=tol, atol=tol)
# 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-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
import unittest
import nifty4 as ift
import numpy as np
from itertools import product
from test.common import expand
from numpy.testing import assert_allclose
# TODO Add also other space types
class Power_Energy_Tests(unittest.TestCase):
@expand(product([ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)]))
def testLinearPower(self, space):
dim = len(space.shape)
hspace = space.get_default_codomain()
ht = ift.HarmonicTransformOperator(hspace, space)
binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=True)
pspace = ift.PowerSpace(hspace, binbounds=binbounds)
P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
xi = ift.Field.from_random(domain=hspace, random_type='normal')
def pspec(k): return 1 / (1 + k**2)**dim
tau0 = ift.PS_field(pspace, pspec)
A = P.adjoint_times(ift.sqrt(tau0))
n = ift.Field.from_random(domain=space, random_type='normal')
s = xi * A
diag = ift.Field.ones(space) * 10
Instrument = ift.DiagonalOperator(diag)
R = Instrument * ht
diag = ift.Field.ones(space)
N = ift.DiagonalOperator(diag)
d = R(s) + n
direction = ift.Field.from_random('normal', pspace)
direction /= np.sqrt(direction.var())
eps = 1e-10
tau1 = tau0 + eps * direction
IC = ift.GradientNormController(
name='IC',
verbose=False,
iteration_limit=100,
tol_abs_gradnorm=1e-5)
inverter = ift.ConjugateGradient(IC)
S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
D = ift.library.WienerFilterEnergy(position=s, d=d, R=R, N=N, S=S,
inverter=inverter).curvature
energy0 = ift.library.CriticalPowerEnergy(
position=tau0, m=xi, D=D, inverter=inverter)
energy1 = ift.library.CriticalPowerEnergy(
position=tau1, m=xi, D=D, inverter=inverter)
a = (energy1.value - energy0.value) / eps
b = energy0.gradient.vdot(direction)
tol = 1e-10
assert_allclose(a, b, rtol=tol, atol=tol)
@expand(product([ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[ift.library.Exponential, ift.library.Linear]))
def testNonlinearPower(self, space, nonlinearity):
f = nonlinearity()
dim = len(space.shape)
fft = ift.FFTOperator(space)
hspace = fft.target[0]
binbounds = ift.PowerSpace.useful_binbounds(hspace, logarithmic=True)
pspace = ift.PowerSpace(hspace, binbounds=binbounds)
P = ift.PowerProjectionOperator(domain=hspace, power_space=pspace)
xi = ift.Field.from_random(domain=hspace, random_type='normal')
def pspec(k): return 1 / (1 + k**2)**dim
tau0 = ift.PS_field(pspace, pspec)
A = P.adjoint_times(ift.sqrt(tau0))
n = ift.Field.from_random(domain=space, random_type='normal')
s = fft.inverse_times(xi * A)
diag = ift.Field.ones(space) * 10
R = ift.DiagonalOperator(diag)
diag = ift.Field.ones(space)
N = ift.DiagonalOperator(diag)
d = R(f(s)) + n
direction = ift.Field.from_random('normal', pspace)
direction /= np.sqrt(direction.var())
eps = 1e-10
tau1 = tau0 + eps * direction
IC = ift.GradientNormController(
name='IC',
verbose=False,
iteration_limit=100,
tol_abs_gradnorm=1e-5)
inverter = ift.ConjugateGradient(IC)
S = ift.create_power_operator(hspace, power_spectrum=lambda k: 1.)
D = ift.library.NonlinearWienerFilterEnergy(
position=xi,
d=d,
Instrument=R,
nonlinearity=f,
FFT=fft,
power=A,
N=N,
S=S,
inverter=inverter).curvature
energy0 = ift.library.NonlinearPowerEnergy(
position=tau0,
d=d,
m=xi,
D=D,
Instrument=R,
Projection=P,
nonlinearity=f,
FFT=fft,
N=N,
inverter=inverter)
energy1 = ift.library.NonlinearPowerEnergy(
position=tau1,
d=d,
m=xi,
D=D,
Instrument=R,
Projection=P,
nonlinearity=f,
FFT=fft,
N=N,
inverter=inverter)
a = (energy1.value - energy0.value) / eps
b = energy0.gradient.vdot(direction)
tol = 1e-2
assert_allclose(a, b, rtol=tol, atol=tol)
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