Commit 29334bdb authored by Jakob Knollmüller's avatar Jakob Knollmüller
Browse files

tests

parent 9c1c6245
......@@ -179,7 +179,7 @@ class GaussianEntropy(EnergyOperator):
self._check_input(x)
res = -0.5*(2*np.pi*np.e*x**2).log().sum()
if not isinstance(x, Linearization):
return Field.scalar(res)
return res
if not x.want_metric:
return res
# FIXME not sure about metric
......
# 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-2020 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
import numpy as np
from numpy.testing import assert_allclose
import nifty7 as ift
from ...nifty7.library.variational_models import DiagonalSelector
def test_diagonal_selector():
N = 42
square_space = ift.RGSpace([N,N])
linear_space = ift.RGSpace(N)
myField = ift.from_random(square_space)
myDiagonalSelector = DiagonalSelector(square_space)
selected = myDiagonalSelector(myField).val
np_selected = np.diag(myField.val)
assert_allclose(np_selected, selected)
# 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-2020 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
import numpy as np
from numpy.testing import assert_allclose
import nifty7 as ift
from ...nifty7.library.variational_models import GaussianEntropy
def test_gaussian_entropy():
N = 42
linear_space = ift.RGSpace(N)
myField = ift.from_random(linear_space, 'uniform')
vals = myField.val
entropy = - 0.5 * np.sum(np.log(2 * vals**2 * np.pi * np.e)) # minus due to subtraction in KL
myEntropy = GaussianEntropy(myField.domain)
assert_allclose(entropy, myEntropy(myField).val)
# 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-2020 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
import numpy as np
from numpy.testing import assert_allclose
import nifty7 as ift
from ...nifty7.library.variational_models import LowerTriangularInserter
def test_lower_triangular_inserter():
N = 42
square_space = ift.RGSpace([N,N])
myInserter = LowerTriangularInserter(square_space)
flat_space = myInserter.domain
assert_allclose(flat_space.size, N*(N+1)//2)
myField = ift.from_random(flat_space)
myMatrix = myInserter(myField)
assert_allclose(np_selected, selected)
upper_i = []
upper_j = []
for i in range(0,N):
for j in range(i+1,N):
upper_i.append(i)
upper_j.append(j)
zeros = myMatrix.val[(upper_i,upper_j)]
assert_allclose(zeros, np.zeros((N-1)*N//2))
assert_allclose((myMatrix==0).val.sum(), (N-1)*N//2)
# 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-2020 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
import numpy as np
from numpy.testing import assert_allclose
import nifty7 as ift
def test_multifield2vector():
myFirstSpace = ift.RGSpace([42,43])
mySecondSpace = ift.RGSpace([1,2,3])
myDict = {
'first': ift.from_random(myFirstSpace),
'second': ift.from_random(mySecondSpace)
}
myMultiField = ift.MultiField.from_dict(myDict)
myMultifield2Vector = ift.Multifield2Vector(myMultiField.domain)
myVector = myMultifield2Vector(myMultiField)
assert_allclose(myVector.size, myMultiField.size)
myNumpyVector = np.empty(myMultiField.size)
myNumpyVector[:myFirstSpace.size] = myDict['first'].val.flatten()
myNumpyVector[myFirstSpace.size:] = myDict['second'].val.flatten()
assert_allclose(myNumpyVector, myVector.val)
mySecondMultiField = myMultifield2Vector.adjoint(myVector)
assert_allclose(mySecondMultiField['first'].val, myMultiField['first'].val)
assert_allclose(mySecondMultiField['second'].val, myMultiField['second'].val)
Supports Markdown
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