test_rg_space.py 3.79 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
16
17
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18

Jait Dixit's avatar
Jait Dixit committed
19
20
21
from __future__ import division
import unittest
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
22
from numpy.testing import assert_, assert_equal, assert_allclose
Philipp Arras's avatar
Philipp Arras committed
23
import nifty5 as ift
Jait Dixit's avatar
Jait Dixit committed
24
25
from test.common import expand

Martin Reinecke's avatar
Martin Reinecke committed
26
# [shape, distances, harmonic, expected]
27
CONSTRUCTOR_CONFIGS = [
Martin Reinecke's avatar
Martin Reinecke committed
28
        [(8,), None, False,
Jait Dixit's avatar
Jait Dixit committed
29
30
31
32
            {
                'shape': (8,),
                'distances': (0.125,),
                'harmonic': False,
Martin Reinecke's avatar
Martin Reinecke committed
33
                'size': 8,
Jait Dixit's avatar
Jait Dixit committed
34
            }],
Martin Reinecke's avatar
Martin Reinecke committed
35
        [(8,), None, True,
Jait Dixit's avatar
Jait Dixit committed
36
37
38
39
            {
                'shape': (8,),
                'distances': (1.0,),
                'harmonic': True,
Martin Reinecke's avatar
Martin Reinecke committed
40
                'size': 8,
Jait Dixit's avatar
Jait Dixit committed
41
            }],
Martin Reinecke's avatar
Martin Reinecke committed
42
        [(8,), (12,), True,
Jait Dixit's avatar
Jait Dixit committed
43
44
45
46
            {
                'shape': (8,),
                'distances': (12.0,),
                'harmonic': True,
Martin Reinecke's avatar
Martin Reinecke committed
47
                'size': 8,
Jait Dixit's avatar
Jait Dixit committed
48
            }],
Martin Reinecke's avatar
Martin Reinecke committed
49
        [(11, 11), None, False,
Jait Dixit's avatar
Jait Dixit committed
50
51
52
53
            {
                'shape': (11, 11),
                'distances': (1/11, 1/11),
                'harmonic': False,
Martin Reinecke's avatar
Martin Reinecke committed
54
                'size': 121,
Jait Dixit's avatar
Jait Dixit committed
55
            }],
Martin Reinecke's avatar
Martin Reinecke committed
56
        [(12, 12), (1.3, 1.3), True,
Jait Dixit's avatar
Jait Dixit committed
57
            {
Theo Steininger's avatar
Theo Steininger committed
58
                'shape': (12, 12),
Jait Dixit's avatar
Jait Dixit committed
59
60
                'distances': (1.3, 1.3),
                'harmonic': True,
Martin Reinecke's avatar
Martin Reinecke committed
61
                'size': 144,
Jait Dixit's avatar
Jait Dixit committed
62
63
64
65
66
            }]

    ]


67
def get_k_length_array_configs():
Martin Reinecke's avatar
Martin Reinecke committed
68
    # for RGSpace(shape=(4, 4), distances=(0.25,0.25))
Martin Reinecke's avatar
Martin Reinecke committed
69
70
71
72
73
74
75
    cords_0 = np.ogrid[0:4, 0:4]
    da_0 = ((cords_0[0] - 4 // 2) * 0.25)**2
    da_0 = np.fft.ifftshift(da_0)
    temp = ((cords_0[1] - 4 // 2) * 0.25)**2
    temp = np.fft.ifftshift(temp)
    da_0 = da_0 + temp
    da_0 = np.sqrt(da_0)
76
    return [
Martin Reinecke's avatar
Martin Reinecke committed
77
        [(4, 4), (0.25, 0.25), da_0],
78
79
80
        ]


81
def get_dvol_configs():
Martin Reinecke's avatar
Martin Reinecke committed
82
    np.random.seed(42)
83
    return [
84
85
86
87
88
89
90
91
        [(11, 11), None, False, 1],
        [(11, 11), None, False, 1],
        [(11, 11), (1.3, 1.3), False, 1],
        [(11, 11), (1.3, 1.3), False, 1],
        [(11, 11), None, True, 1],
        [(11, 11), None, True, 1],
        [(11, 11), (1.3, 1.3), True, 1],
        [(11, 11), (1.3, 1.3), True, 1]
92
93
94
        ]


Jait Dixit's avatar
Jait Dixit committed
95
class RGSpaceInterfaceTests(unittest.TestCase):
Martin Reinecke's avatar
Martin Reinecke committed
96
    @expand([['distances', tuple]])
97
    def test_property_ret_type(self, attribute, expected_type):
Martin Reinecke's avatar
Martin Reinecke committed
98
        x = ift.RGSpace(1)
Jait Dixit's avatar
Jait Dixit committed
99
100
101
102
        assert_(isinstance(getattr(x, attribute), expected_type))


class RGSpaceFunctionalityTests(unittest.TestCase):
103
    @expand(CONSTRUCTOR_CONFIGS)
Martin Reinecke's avatar
Martin Reinecke committed
104
    def test_constructor(self, shape, distances,
Martin Reinecke's avatar
Martin Reinecke committed
105
                         harmonic, expected):
Martin Reinecke's avatar
Martin Reinecke committed
106
        x = ift.RGSpace(shape, distances, harmonic)
Martin Reinecke's avatar
Martin Reinecke committed
107
        for key, value in expected.items():
Jait Dixit's avatar
Jait Dixit committed
108
109
            assert_equal(getattr(x, key), value)

110
111
    @expand(get_k_length_array_configs())
    def test_k_length_array(self, shape, distances, expected):
Martin Reinecke's avatar
Martin Reinecke committed
112
        r = ift.RGSpace(shape=shape, distances=distances, harmonic=True)
Martin Reinecke's avatar
Martin Reinecke committed
113
        assert_allclose(r.get_k_length_array().to_global_data(), expected)
Jait Dixit's avatar
Jait Dixit committed
114

115
116
    @expand(get_dvol_configs())
    def test_dvol(self, shape, distances, harmonic, power):
Martin Reinecke's avatar
Martin Reinecke committed
117
        r = ift.RGSpace(shape=shape, distances=distances, harmonic=harmonic)
Martin Reinecke's avatar
Martin Reinecke committed
118
        assert_allclose(r.dvol, np.prod(r.distances)**power)