test_rg_space.py 9.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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/>.

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
27
28
from nifty import RGSpace
from test.common import expand

# [shape, zerocenter, distances, harmonic, dtype, expected]
29
CONSTRUCTOR_CONFIGS = [
Jait Dixit's avatar
Jait Dixit committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        [(8,), False, None, False, None,
            {
                'shape': (8,),
                'zerocenter': (False,),
                'distances': (0.125,),
                'harmonic': False,
                'dtype': np.dtype('float'),
                'dim': 8,
                'total_volume': 1.0
            }],
        [(8,), True, None, False, None,
            {
                'shape': (8,),
                'zerocenter': (True,),
                'distances': (0.125,),
                'harmonic': False,
                'dtype': np.dtype('float'),
                'dim': 8,
                'total_volume': 1.0
            }],
        [(8,), False, None, True, None,
            {
                'shape': (8,),
                'zerocenter': (False,),
                'distances': (1.0,),
                'harmonic': True,
                'dtype': np.dtype('complex'),
                'dim': 8,
                'total_volume': 8.0
            }],
        [(8,), False, (12,), True, None,
            {
                'shape': (8,),
                'zerocenter': (False,),
                'distances': (12.0,),
                'harmonic': True,
                'dtype': np.dtype('complex'),
                'dim': 8,
                'total_volume': 96.0
            }],
        [(11, 11), (False, True), None, False, None,
            {
                'shape': (11, 11),
                'zerocenter': (False, True),
                'distances': (1/11, 1/11),
                'harmonic': False,
                'dtype': np.dtype('float'),
                'dim': 121,
                'total_volume': 1.0
            }],
        [(11, 11), True, (1.3, 1.3), True, None,
            {
                'shape': (11, 11),
                'zerocenter': (True, True),
                'distances': (1.3, 1.3),
                'harmonic': True,
                'dtype': np.dtype('complex'),
                'dim': 121,
                'total_volume': 204.49
            }]

    ]


94
def get_distance_array_configs():
Martin Reinecke's avatar
Martin Reinecke committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    # for RGSpace(shape=(4, 4), distances=None, zerocenter=[False, False])
    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)
    # for RGSpace(shape=(4, 4), distances=None, zerocenter=[True, True])
    da_1 = ((cords_0[0] - 4 // 2) * 0.25)**2
    temp = ((cords_0[1] - 4 // 2) * 0.25)**2
    da_1 = da_1 + temp
    da_1 = np.sqrt(da_1)
    # for RGSpace(shape=(4, 4), distances=(12, 12), zerocenter=[True, True])
    da_2 = ((cords_0[0] - 4 // 2) * 12)**2
    temp = ((cords_0[1] - 4 // 2) * 12)**2
    da_2 = da_2 + temp
    da_2 = np.sqrt(da_2)
113
    return [
Martin Reinecke's avatar
Martin Reinecke committed
114
115
116
        [(4, 4),  None, [False, False], da_0],
        [(4, 4),  None, [True, True], da_1],
        [(4, 4),  (12, 12), [True, True], da_2]
117
118
119
120
        ]


def get_weight_configs():
Martin Reinecke's avatar
Martin Reinecke committed
121
122
123
124
125
126
127
128
129
130
131
    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)
132
    return [
Martin Reinecke's avatar
Martin Reinecke committed
133
134
135
136
137
138
139
140
        [(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]
141
142
143
144
        ]


def get_hermitian_configs():
Martin Reinecke's avatar
Martin Reinecke committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    h_0_x = np.array([
        [0.88250339+0.12102381j,  0.54293435+0.7345584j, 0.87057998+0.20515315j,
            0.16602950+0.09396132j],
        [0.83853902+0.17974696j,  0.79735933+0.37104425j, 0.22057732+0.9498977j,
            0.14329183+0.47899678j],
        [0.96934284+0.3792878j, 0.13118669+0.45643055j, 0.16372149+0.48235714j,
            0.66141537+0.20383357j],
        [0.49168197+0.77572178j, 0.09570420+0.14219071j, 0.69735595+0.33017333j,
            0.83692452+0.18544449j]])
    h_0_res_real = np.array([
        [0.88250339+0.j, 0.35448193+0.32029854j, 0.87057998+0.j,
            0.35448193-0.32029854j],
        [0.66511049-0.29798741j, 0.81714193+0.09279988j, 0.45896664+0.30986218j,
            0.11949801+0.16840303j],
        [0.96934284+0.j, 0.39630103+0.12629849j, 0.16372149+0.j,
            0.39630103-0.12629849j],
        [0.66511049+0.29798741j, 0.11949801-0.16840303j, 0.45896664-0.30986218j,
            0.81714193-0.09279988j]])
    h_0_res_imag = np.array([
        [0.12102381+0.j, 0.41425986-0.18845242j, 0.20515315+0.j,
            0.41425986+0.18845242j],
        [0.47773437-0.17342852j, 0.27824437+0.0197826j, 0.64003551+0.23838932j,
            0.31059374-0.02379381j],
        [0.37928780+0.j, 0.33013206+0.26511434j, 0.48235714+0.j,
            0.33013206-0.26511434j],
        [0.47773437+0.17342852j, 0.31059374+0.02379381j, 0.64003551-0.23838932j,
            0.27824437-0.0197826j]])

    h_1_x = np.array([
        [[0.23987021+0.41617749j, 0.34605012+0.55462234j, 0.07947035+0.73360723j,
            0.22853748+0.39275304j],
         [0.90254910+0.02107809j, 0.28195470+0.56031588j, 0.23004043+0.33873536j,
             0.56398377+0.68913034j],
         [0.81897406+0.2050369j, 0.88724852+0.8137488j, 0.84645004+0.0059284j,
             0.14950377+0.50013099j]],
        [[0.93491597+0.73251066j, 0.74764790+0.11539037j, 0.48090736+0.04352568j,
            0.49363732+0.97233093j],
         [0.72761881+0.74636216j, 0.46390134+0.4343401j, 0.88436859+0.79415269j,
             0.67027606+0.85498234j],
         [0.86318727+0.19076379j, 0.36859448+0.89842333j, 0.73407193+0.85091112j,
             0.44187657+0.08936409j]]
        ])
    h_1_res_real = np.array([
        [[0.23987021+0.j, 0.28729380+0.08093465j, 0.07947035+0.j,
            0.28729380-0.08093465j],
         [0.90254910+0.j, 0.42296924-0.06440723j, 0.23004043+0.j,
             0.42296924+0.06440723j],
         [0.81897406+0.j, 0.51837614+0.1568089j, 0.84645004+0.j,
             0.51837614-0.1568089j]],
        [[0.93491597+0.j, 0.62064261-0.42847028j, 0.48090736+0.j,
            0.62064261+0.42847028j],
         [0.72761881+0.j, 0.56708870-0.21032112j, 0.88436859+0.j,
             0.56708870+0.21032112j],
         [0.86318727+0.j, 0.40523552+0.40452962j, 0.73407193+0.j,
             0.40523552-0.40452962j]]
        ])
    h_1_res_imag = np.array([
        [[0.41617749+0.j, 0.47368769-0.05875632j, 0.73360723+0.j,
            0.47368769+0.05875632j],
         [0.02107809+0.j, 0.62472311+0.14101454j, 0.33873536+0.j,
             0.62472311-0.14101454j],
         [0.20503690+0.j, 0.65693990-0.36887238j, 0.00592840+0.j,
             0.65693990+0.36887238j]],
        [[0.73251066+0.j, 0.54386065-0.12700529j, 0.04352568+0.j,
            0.54386065+0.12700529j],
         [0.74636216+0.j, 0.64466122+0.10318736j, 0.79415269+0.j,
             0.64466122-0.10318736j],
         [0.19076379+0.j, 0.49389371+0.03664104j, 0.85091112+0.j,
             0.49389371-0.03664104j]]
        ])
215
    return [
Martin Reinecke's avatar
Martin Reinecke committed
216
217
        [h_0_x, None, h_0_res_real, h_0_res_imag],
        [h_1_x, (2,), h_1_res_real, h_1_res_imag]
218
219
220
    ]


Jait Dixit's avatar
Jait Dixit committed
221
222
223
class RGSpaceInterfaceTests(unittest.TestCase):
    @expand([['distances', tuple],
            ['zerocenter', tuple]])
224
    def test_property_ret_type(self, attribute, expected_type):
Jait Dixit's avatar
Jait Dixit committed
225
226
227
228
229
        x = RGSpace()
        assert_(isinstance(getattr(x, attribute), expected_type))


class RGSpaceFunctionalityTests(unittest.TestCase):
230
    @expand(CONSTRUCTOR_CONFIGS)
Jait Dixit's avatar
Jait Dixit committed
231
232
233
234
235
236
    def test_constructor(self, shape, zerocenter, distances,
                         harmonic, dtype, expected):
        x = RGSpace(shape, zerocenter, distances, harmonic, dtype)
        for key, value in expected.iteritems():
            assert_equal(getattr(x, key), value)

237
238
239
240
241
    @expand(get_hermitian_configs())
    def test_hermitian_decomposition(self, x, axes, real, imag):
        r = RGSpace(5)
        assert_almost_equal(r.hermitian_decomposition(x, axes=axes)[0], real)
        assert_almost_equal(r.hermitian_decomposition(x, axes=axes)[1], imag)
Jait Dixit's avatar
Jait Dixit committed
242

243
244
245
246
    @expand(get_distance_array_configs())
    def test_distance_array(self, shape, distances, zerocenter, expected):
        r = RGSpace(shape=shape, distances=distances, zerocenter=zerocenter)
        assert_almost_equal(r.get_distance_array('not'), expected)
Jait Dixit's avatar
Jait Dixit committed
247

248
249
250
251
252
253
254
255
    @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)