Commit 2302056e authored by Martin Reinecke's avatar Martin Reinecke

Merge branch 'NIFTy_5' into plotting_improvements

parents 3ba3b27b 6b06f9da
......@@ -41,7 +41,7 @@ def make_correlated_field(s_space, amplitude_model):
amplitude_model : model for correlation structure
'''
h_space = s_space.get_default_codomain()
ht = HartleyOperator(h_space, s_space)
ht = HarmonicTransformOperator(h_space, s_space)
p_space = amplitude_model.value.domain[0]
power_distributor = PowerDistributor(h_space, p_space)
......
......@@ -75,6 +75,4 @@ class PointSources(Model):
@staticmethod
def inverseIG(u, alpha, q):
res = norm.ppf(invgamma.cdf(u, alpha, scale=q))
# # FIXME
# res = np.clip(res, 0, None)
return res
# 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-2018 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
from itertools import product
from test.common import expand
import nifty5 as ift
import numpy as np
class Energy_Tests(unittest.TestCase):
def make_model(self, type, **kwargs):
if type == 'Constant':
np.random.seed(kwargs['seed'])
S = ift.ScalingOperator(1., kwargs['space'])
s = S.draw_sample()
return ift.Constant(
ift.MultiField.from_dict({kwargs['space_key']: s}),
ift.MultiField.from_dict({kwargs['space_key']: s}))
elif type == 'Variable':
np.random.seed(kwargs['seed'])
S = ift.ScalingOperator(1., kwargs['space'])
s = S.draw_sample()
return ift.Variable(
ift.MultiField.from_dict({kwargs['space_key']: s}))
elif type == 'LinearModel':
return ift.LinearModel(
inp=kwargs['model'], lin_op=kwargs['lin_op'])
else:
raise ValueError('unknown type passed')
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testGaussian(self, type1, space, seed):
model = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
energy = ift.GaussianEnergy(model)
ift.extra.check_value_gradient_consistency(energy)
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testQuadratic(self, type1, space, seed):
np.random.seed(seed)
S = ift.ScalingOperator(1., space)
s = [S.draw_sample() for _ in range(3)]
energy = ift.QuadraticEnergy(s[0], ift.makeOp(s[1]), s[2])
ift.extra.check_value_gradient_consistency(energy)
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testPoissonian(self, type1, space, seed):
model = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
model = ift.PointwiseExponential(model)
d = np.random.poisson(120, size=space.shape)
d = ift.Field.from_global_data(space, d)
energy = ift.PoissonianEnergy(model, d)
ift.extra.check_value_gradient_consistency(energy)
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testHamiltonian_and_KL(self, type1, space, seed):
model = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
model = ift.PointwiseExponential(model)
lh = ift.GaussianEnergy(model)
hamiltonian = ift.Hamiltonian(lh)
ift.extra.check_value_gradient_consistency(hamiltonian)
S = ift.ScalingOperator(1., space)
samps = [S.draw_sample() for i in range(3)]
kl = ift.SampledKullbachLeiblerDivergence(hamiltonian, samps)
ift.extra.check_value_gradient_consistency(kl)
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testBernoulli(self, type1, space, seed):
model = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
model = ift.PointwisePositiveTanh(model)
d = np.random.binomial(1, 0.1, size=space.shape)
d = ift.Field.from_global_data(space, d)
energy = ift.BernoulliEnergy(model, d)
ift.extra.check_value_gradient_consistency(energy)
......@@ -111,6 +111,50 @@ class Model_Tests(unittest.TestCase):
ift.extra.check_value_gradient_consistency(
ift.PointwisePositiveTanh(model))
@expand(product(
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4],
[0.5],
[2.],
[3.],
[1.5],
[1.75],
[1.3],
[4, 78, 23],
))
def testModelLibrary(self, space, Npixdof, ceps_a,
ceps_k, sm, sv, im, iv, seed):
# tests amplitude model and coorelated field model
np.random.seed(seed)
model = ift.make_amplitude_model(space, Npixdof, ceps_a, ceps_k, sm,
sv, im, iv)[0]
S = ift.ScalingOperator(1., model.position.domain)
model = model.at(S.draw_sample())
ift.extra.check_value_gradient_consistency(model)
model2 = ift.make_correlated_field(space, model)[0]
S = ift.ScalingOperator(1., model2.position.domain)
model2 = model2.at(S.draw_sample())
ift.extra.check_value_gradient_consistency(model2)
@expand(product(
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]))
def testPointModel(seld, space, seed):
S = ift.ScalingOperator(1., space)
pos = ift.MultiField.from_dict(
{'points': S.draw_sample()})
alpha = 1.5
q = 0.73
model = ift.PointSources(pos, alpha, q)
# All those cdfs and ppfs are not that accurate
ift.extra.check_value_gradient_consistency(model, tol=1e-5)
@expand(product(
['Variable', 'Constant'],
[ift.GLSpace(15),
......
......@@ -35,6 +35,15 @@ _pow_spaces = [ift.PowerSpace(ift.RGSpace((17, 38), harmonic=True))]
class Consistency_Tests(unittest.TestCase):
@expand(product(_p_RG_spaces, [np.float64, np.complex128]))
def testLOSResponse(self, 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)
op = ift.LOSResponse(sp, starts, ends, sigma_low, sigma_ups)
ift.extra.consistency_check(op, dtype, dtype)
@expand(product(_h_spaces + _p_spaces + _pow_spaces,
[np.float64, np.complex128]))
def testOperatorCombinations(self, sp, dtype):
......
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