test_rg_space.py 6.85 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
14
15
16
17
#
# 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.
18

Jait Dixit's avatar
Jait Dixit committed
19
20
21
22
23
from __future__ import division

import unittest
import numpy as np

24
from numpy.testing import assert_, assert_equal, assert_almost_equal
Jait Dixit's avatar
Jait Dixit committed
25
26
from nifty import RGSpace
from test.common import expand
Martin Reinecke's avatar
Martin Reinecke committed
27
from itertools import product
Jait Dixit's avatar
Jait Dixit committed
28

Martin Reinecke's avatar
Martin Reinecke committed
29
# [shape, distances, harmonic, expected]
30
CONSTRUCTOR_CONFIGS = [
Martin Reinecke's avatar
Martin Reinecke committed
31
        [(8,), None, False,
Jait Dixit's avatar
Jait Dixit committed
32
33
34
35
36
37
38
            {
                'shape': (8,),
                'distances': (0.125,),
                'harmonic': False,
                'dim': 8,
                'total_volume': 1.0
            }],
Martin Reinecke's avatar
Martin Reinecke committed
39
        [(8,), None, True,
Jait Dixit's avatar
Jait Dixit committed
40
41
42
43
44
45
46
            {
                'shape': (8,),
                'distances': (1.0,),
                'harmonic': True,
                'dim': 8,
                'total_volume': 8.0
            }],
Martin Reinecke's avatar
Martin Reinecke committed
47
        [(8,), (12,), True,
Jait Dixit's avatar
Jait Dixit committed
48
49
50
51
52
53
54
            {
                'shape': (8,),
                'distances': (12.0,),
                'harmonic': True,
                'dim': 8,
                'total_volume': 96.0
            }],
Martin Reinecke's avatar
Martin Reinecke committed
55
        [(11, 11), None, False,
Jait Dixit's avatar
Jait Dixit committed
56
57
58
59
60
61
62
            {
                'shape': (11, 11),
                'distances': (1/11, 1/11),
                'harmonic': False,
                'dim': 121,
                'total_volume': 1.0
            }],
Martin Reinecke's avatar
Martin Reinecke committed
63
        [(11, 11), (1.3, 1.3), True,
Jait Dixit's avatar
Jait Dixit committed
64
65
66
67
68
69
70
71
72
73
74
            {
                'shape': (11, 11),
                'distances': (1.3, 1.3),
                'harmonic': True,
                'dim': 121,
                'total_volume': 204.49
            }]

    ]


75
def get_distance_array_configs():
Martin Reinecke's avatar
Martin Reinecke committed
76
    # for RGSpace(shape=(4, 4), distances=None)
Martin Reinecke's avatar
Martin Reinecke committed
77
78
79
80
81
82
83
    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)
84
    return [
Martin Reinecke's avatar
Martin Reinecke committed
85
        [(4, 4),  None, da_0],
86
87
88
89
        ]


def get_weight_configs():
Martin Reinecke's avatar
Martin Reinecke committed
90
91
92
93
94
95
96
97
98
99
100
    np.random.seed(42)
    # power 1
    w_0_x = np.random.rand(32, 12, 6)
    # for RGSpace(shape=(11,11), distances=None, harmonic=False)
    w_0_res = w_0_x * (1/11 * 1/11)
    # for RGSpace(shape=(11, 11), distances=(1.3,1.3), harmonic=False)
    w_1_res = w_0_x * (1.3 * 1.3)
    # for RGSpace(shape=(11,11), distances=None, harmonic=True)
    w_2_res = w_0_x * (1.0 * 1.0)
    # for RGSpace(shape=(11,11), distances=(1.3, 1,3), harmonic=True)
    w_3_res = w_0_x * (1.3 * 1.3)
101
    return [
Martin Reinecke's avatar
Martin Reinecke committed
102
103
104
105
106
107
108
109
        [(11, 11), None, False, w_0_x, 1, None, False, w_0_res],
        [(11, 11), None, False, w_0_x.copy(), 1, None,  True, w_0_res],
        [(11, 11), (1.3, 1.3), False, w_0_x, 1, None, False, w_1_res],
        [(11, 11), (1.3, 1.3), False, w_0_x.copy(), 1, None,  True, w_1_res],
        [(11, 11), None, True, w_0_x, 1, None, False, w_2_res],
        [(11, 11), None, True, w_0_x.copy(), 1, None,  True, w_2_res],
        [(11, 11), (1.3, 1.3), True, w_0_x, 1, None, False, w_3_res],
        [(11, 11), (1.3, 1.3), True, w_0_x.copy(), 1, None,  True, w_3_res]
110
111
112
        ]


Jait Dixit's avatar
Jait Dixit committed
113
class RGSpaceInterfaceTests(unittest.TestCase):
Martin Reinecke's avatar
Martin Reinecke committed
114
    @expand([['distances', tuple]])
115
    def test_property_ret_type(self, attribute, expected_type):
116
        x = RGSpace(1)
Jait Dixit's avatar
Jait Dixit committed
117
118
119
120
        assert_(isinstance(getattr(x, attribute), expected_type))


class RGSpaceFunctionalityTests(unittest.TestCase):
121
    @expand(CONSTRUCTOR_CONFIGS)
Martin Reinecke's avatar
Martin Reinecke committed
122
    def test_constructor(self, shape, distances,
Martin Reinecke's avatar
Martin Reinecke committed
123
                         harmonic, expected):
Martin Reinecke's avatar
Martin Reinecke committed
124
        x = RGSpace(shape, distances, harmonic)
Jait Dixit's avatar
Jait Dixit committed
125
126
127
        for key, value in expected.iteritems():
            assert_equal(getattr(x, key), value)

128
    @expand(product([(10,), (11,), (1, 1), (4, 4), (5, 7), (8, 12), (7, 16),
129
130
131
                     (4, 6, 8), (17, 5, 3)]))
    def test_hermitian_decomposition(self, shape):
        r = RGSpace(shape, harmonic=True)
132
        v = np.empty(shape, dtype=np.complex128)
Martin Reinecke's avatar
Martin Reinecke committed
133
134
        v.real = np.random.random(shape)
        v.imag = np.random.random(shape)
135
        h, a = r.hermitian_decomposition(v)
Martin Reinecke's avatar
Martin Reinecke committed
136
        # make sure that data == h + a
Martin Reinecke's avatar
Martin Reinecke committed
137
        # NOTE: this is only correct for preserve_gaussian_variance==False,
138
139
140
141
142
        #       but I consider this an intrinsic property of a hermitian
        #       decomposition.
        assert_almost_equal(v, h+a)
        print (h, a)

Martin Reinecke's avatar
Martin Reinecke committed
143
        # test hermitianity of h
144
        it = np.nditer(h, flags=['multi_index'])
Martin Reinecke's avatar
Martin Reinecke committed
145
146
147
148
        while not it.finished:
            i1 = it.multi_index
            i2 = []
            for i in range(len(i1)):
149
                i2.append(h.shape[i]-i1[i] if i1[i] > 0 else 0)
Martin Reinecke's avatar
Martin Reinecke committed
150
            i2 = tuple(i2)
151
152
            assert_almost_equal(h[i1], np.conj(h[i2]))
            assert_almost_equal(a[i1], -np.conj(a[i2]))
Martin Reinecke's avatar
Martin Reinecke committed
153
            it.iternext()
154
155

    @expand(product([(10,), (11,), (1, 1), (4, 4), (5, 7), (8, 12), (7, 16),
156
157
158
                     (4, 6, 8), (17, 5, 3)]))
    def test_hermitian_decomposition2(self, shape):
        r = RGSpace(shape, harmonic=True)
Martin Reinecke's avatar
Martin Reinecke committed
159
        v = np.random.random(shape)
160
        h, a = r.hermitian_decomposition(v)
Martin Reinecke's avatar
Martin Reinecke committed
161
        # make sure that data == h + a
162
        assert_almost_equal(v, h+a)
Martin Reinecke's avatar
Martin Reinecke committed
163
        # test hermitianity of h
164
        it = np.nditer(h, flags=['multi_index'])
Martin Reinecke's avatar
Martin Reinecke committed
165
166
167
168
        while not it.finished:
            i1 = it.multi_index
            i2 = []
            for i in range(len(i1)):
169
                i2.append(h.shape[i]-i1[i] if i1[i] > 0 else 0)
Martin Reinecke's avatar
Martin Reinecke committed
170
            i2 = tuple(i2)
171
172
            assert_almost_equal(h[i1], np.conj(h[i2]))
            assert_almost_equal(a[i1], -np.conj(a[i2]))
Martin Reinecke's avatar
Martin Reinecke committed
173
            it.iternext()
Jait Dixit's avatar
Jait Dixit committed
174

175
    @expand(get_distance_array_configs())
Martin Reinecke's avatar
Martin Reinecke committed
176
177
    def test_distance_array(self, shape, distances, expected):
        r = RGSpace(shape=shape, distances=distances)
178
        assert_almost_equal(r.get_distance_array('not'), expected)
Jait Dixit's avatar
Jait Dixit committed
179

180
181
182
183
184
185
186
187
    @expand(get_weight_configs())
    def test_weight(self, shape, distances, harmonic, x, power, axes,
                    inplace, expected):
        r = RGSpace(shape=shape, distances=distances, harmonic=harmonic)
        res = r.weight(x, power, axes, inplace)
        assert_almost_equal(res, expected)
        if inplace:
            assert_(x is res)
188
189

    def test_hermitian_fixed_points(self):
190
        x = RGSpace((5, 6, 5, 6))
191
        assert_equal(x.hermitian_fixed_points(),
192
                     [(0, 0, 0, 0), (0, 0, 0, 3), (0, 3, 0, 0), (0, 3, 0, 3)])