test_rg_space.py 5.85 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def get_distance_array_configs():
    npzfile = np.load('test/data/rg_space.npz')
    return [
        [(4, 4),  None, [False, False], npzfile['da_0']],
        [(4, 4),  None, [True, True], npzfile['da_1']],
        [(4, 4),  (12, 12), [True, True], npzfile['da_2']]
        ]


def get_weight_configs():
    npzfile = np.load('test/data/rg_space.npz')
    return [
        [(11, 11), None, False,
            npzfile['w_0_x'], 1, None, False, npzfile['w_0_res']],
        [(11, 11), None, False,
            npzfile['w_0_x'], 1, None, True, npzfile['w_0_res']],
        [(11, 11), (1.3, 1.3), False,
            npzfile['w_0_x'], 1, None, False, npzfile['w_1_res']],
        [(11, 11), (1.3, 1.3), False,
            npzfile['w_0_x'], 1, None, True, npzfile['w_1_res']],
        [(11, 11), None, True,
            npzfile['w_0_x'], 1, None, False, npzfile['w_2_res']],
        [(11, 11), None, True,
            npzfile['w_0_x'], 1, None, True, npzfile['w_2_res']],
        [(11, 11), (1.3, 1.3), True,
            npzfile['w_0_x'], 1, None, False, npzfile['w_3_res']],
        [(11, 11), (1.3, 1.3), True,
            npzfile['w_0_x'], 1, None, True, npzfile['w_3_res']]
        ]


def get_hermitian_configs():
    npzfile = np.load('test/data/rg_space.npz')
    return [
        [npzfile['h_0_x'], None,
            npzfile['h_0_res_real'], npzfile['h_0_res_imag']],
        [npzfile['h_1_x'], (2,),
            npzfile['h_1_res_real'], npzfile['h_1_res_imag']]
    ]


Jait Dixit's avatar
Jait Dixit committed
135
136
137
class RGSpaceInterfaceTests(unittest.TestCase):
    @expand([['distances', tuple],
            ['zerocenter', tuple]])
138
    def test_property_ret_type(self, attribute, expected_type):
Jait Dixit's avatar
Jait Dixit committed
139
140
141
142
143
        x = RGSpace()
        assert_(isinstance(getattr(x, attribute), expected_type))


class RGSpaceFunctionalityTests(unittest.TestCase):
144
    @expand(CONSTRUCTOR_CONFIGS)
Jait Dixit's avatar
Jait Dixit committed
145
146
147
148
149
150
    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)

151
152
153
154
155
    @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
156

157
158
159
160
    @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
161

162
163
164
165
166
167
168
169
    @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)