Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Neel Shah
NIFTy
Commits
29334bdb
Commit
29334bdb
authored
Jun 07, 2021
by
Jakob Knollmüller
Browse files
tests
parent
9c1c6245
Changes
5
Hide whitespace changes
Inline
Sidebyside
src/library/variational_models.py
View file @
29334bdb
...
...
@@ 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
...
...
test/test_operators/test_diagonal_selector.py
0 → 100644
View file @
29334bdb
# 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) 20132020 MaxPlanckSociety
#
# NIFTy is being developed at the MaxPlanckInstitut 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
)
test/test_operators/test_gaussian_entropy.py
0 → 100644
View file @
29334bdb
# 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) 20132020 MaxPlanckSociety
#
# NIFTy is being developed at the MaxPlanckInstitut 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
)
test/test_operators/test_lower_triangular_inserter.py
0 → 100644
View file @
29334bdb
# 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) 20132020 MaxPlanckSociety
#
# NIFTy is being developed at the MaxPlanckInstitut 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
)
test/test_operators/test_multifield2vector.py
0 → 100644
View file @
29334bdb
# 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) 20132020 MaxPlanckSociety
#
# NIFTy is being developed at the MaxPlanckInstitut 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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment