test_nifty_transforms.py 6.82 KB
Newer Older
Jait Dixit's avatar
Jait Dixit committed
1
2
3
4
5
6
7
8
9
import numpy as np
from numpy.testing import assert_equal, assert_almost_equal, assert_raises

from nose_parameterized import parameterized
import unittest
import itertools

from nifty import RGSpace, LMSpace, HPSpace, GLSpace
from nifty import transformator
Jait Dixit's avatar
Jait Dixit committed
10
from nifty.transformations.rgrgtransformation import RGRGTransformation
Jait Dixit's avatar
Jait Dixit committed
11
12
13
14
15
16
17
18
19
20
21
22
from nifty.rg.rg_space import gc as RG_GC
import d2o


###############################################################################

def custom_name_func(testcase_func, param_num, param):
    return "%s_%s" % (
        testcase_func.__name__,
        parameterized.to_safe_name("_".join(str(x) for x in param.args)),
    )

23
24
25
26
def weighted_np_transform(val, domain, codomain, axes=None):
    if codomain.harmonic:
        # correct for forward fft
        val = domain.calc_weight(val, power=1)
Jait Dixit's avatar
Jait Dixit committed
27

28
29
    # Perform the transformation
    Tval = np.fft.fftn(val, axes=axes)
30

31
32
33
    if not codomain.harmonic:
        # correct for inverse fft
        Tval = codomain.calc_weight(Tval, power=-1)
34

35
    return Tval
36

Jait Dixit's avatar
Jait Dixit committed
37
38
###############################################################################

Jait Dixit's avatar
Jait Dixit committed
39
rg_rg_fft_modules = []
Jait Dixit's avatar
Jait Dixit committed
40
41
for name in ['gfft', 'gfft_dummy', 'pyfftw']:
    if RG_GC.validQ('fft_module', name):
Jait Dixit's avatar
Jait Dixit committed
42
        rg_rg_fft_modules += [name]
Jait Dixit's avatar
Jait Dixit committed
43

Jait Dixit's avatar
Jait Dixit committed
44
45
46
47
48
rg_rg_test_shapes = [(128, 128), (179, 179), (512, 512)]

rg_rg_test_spaces = [(GLSpace(8),), (HPSpace(8),), (LMSpace(8),)]
gl_hp_lm_test_spaces = [(GLSpace(8),), (HPSpace(8),), (RGSpace(8),)]
lm_gl_hp_test_spaces = [(LMSpace(8),), (RGSpace(8),)]
49

Jait Dixit's avatar
Jait Dixit committed
50
51
###############################################################################

Jait Dixit's avatar
Jait Dixit committed
52
53
54
class TestRGRGTransformation(unittest.TestCase):
    # all domain/codomain checks
    def test_check_codomain_none(self):
Jait Dixit's avatar
Jait Dixit committed
55
56
        x = RGSpace((8, 8))
        with assert_raises(ValueError):
Jait Dixit's avatar
Jait Dixit committed
57
            transformator.create(x, None)
Jait Dixit's avatar
Jait Dixit committed
58

Jait Dixit's avatar
Jait Dixit committed
59
60
61
62
63
    @parameterized.expand(
        rg_rg_test_spaces,
        testcase_func_name=custom_name_func
    )
    def test_check_codomain_mismatch(self, space):
Jait Dixit's avatar
Jait Dixit committed
64
65
        x = RGSpace((8, 8))
        with assert_raises(TypeError):
Jait Dixit's avatar
Jait Dixit committed
66
            transformator.create(x, space)
Jait Dixit's avatar
Jait Dixit committed
67

Jait Dixit's avatar
Jait Dixit committed
68
69
70
71
72
73
74
    @parameterized.expand(
        itertools.product([0, 1, 2], [None, (1, 1), (10, 10)], [False, True]),
        testcase_func_name=custom_name_func
    )
    def test_check_codomain_rgspecific(self, complexity, distances, harmonic):
        x = RGSpace((8, 8), complexity=complexity,
                    distances=distances, harmonic=harmonic)
Jait Dixit's avatar
Jait Dixit committed
75
76
        assert (RGRGTransformation.check_codomain(x, x.get_codomain()))
        assert (RGRGTransformation.check_codomain(x, x.get_codomain()))
Jait Dixit's avatar
Jait Dixit committed
77

Jait Dixit's avatar
Jait Dixit committed
78
    @parameterized.expand(rg_rg_fft_modules, testcase_func_name=custom_name_func)
Jait Dixit's avatar
Jait Dixit committed
79
80
81
82
83
84
85
86
87
    def test_shapemismatch(self, module):
        x = RGSpace((8, 8))
        b = d2o.distributed_data_object(np.ones((8, 8)))
        with assert_raises(ValueError):
            transformator.create(
                x, x.get_codomain(), module=module
            ).transform(b, axes=(0, 1, 2))

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
88
        itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
89
90
91
92
93
94
95
96
97
        testcase_func_name=custom_name_func
    )
    def test_local_ndarray(self, module, shape):
        x = RGSpace(shape)
        a = np.ones(shape)
        assert np.allclose(
            transformator.create(
                x, x.get_codomain(), module=module
            ).transform(a),
98
            weighted_np_transform(a, x, x.get_codomain())
Jait Dixit's avatar
Jait Dixit committed
99
100
101
        )

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
102
        itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
103
104
105
106
107
108
109
110
111
112
        testcase_func_name=custom_name_func
    )
    def test_local_notzero(self, module, shape):
        x = RGSpace(shape[0])  # all tests along axis 1
        a = np.ones(shape)
        b = d2o.distributed_data_object(a)
        assert np.allclose(
            transformator.create(
                x, x.get_codomain(), module=module
            ).transform(b, axes=(1,)),
113
            weighted_np_transform(a, x, x.get_codomain(), axes=(1,))
Jait Dixit's avatar
Jait Dixit committed
114
115
116
        )

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
117
        itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
118
119
120
121
122
123
124
125
126
127
        testcase_func_name=custom_name_func
    )
    def test_not(self, module, shape):
        x = RGSpace(shape)
        a = np.ones(shape)
        b = d2o.distributed_data_object(a, distribution_strategy='not')
        assert np.allclose(
            transformator.create(
                x, x.get_codomain(), module=module
            ).transform(b),
128
            weighted_np_transform(a, x, x.get_codomain())
Jait Dixit's avatar
Jait Dixit committed
129
130
131
        )

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
132
        itertools.product(rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
133
134
        testcase_func_name=custom_name_func
    )
Jait Dixit's avatar
Jait Dixit committed
135
    def test_mpi_axesnone(self, shape):
Jait Dixit's avatar
Jait Dixit committed
136
137
138
139
140
        x = RGSpace(shape)
        a = np.ones(shape)
        b = d2o.distributed_data_object(a)
        assert np.allclose(
            transformator.create(
Jait Dixit's avatar
Jait Dixit committed
141
142
                x, x.get_codomain(), module='pyfftw'
            ).transform(b),
143
            weighted_np_transform(a, x, x.get_codomain())
Jait Dixit's avatar
Jait Dixit committed
144
145
146
        )

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
147
        itertools.product(rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
148
149
150
151
152
153
154
155
156
        testcase_func_name=custom_name_func
    )
    def test_mpi_axesnone_equal(self, shape):
        x = RGSpace(shape)
        a = np.ones(shape)
        b = d2o.distributed_data_object(a, distribution_strategy='equal')
        assert np.allclose(
            transformator.create(
                x, x.get_codomain(), module='pyfftw'
Jait Dixit's avatar
Jait Dixit committed
157
            ).transform(b),
158
            weighted_np_transform(a, x, x.get_codomain())
Jait Dixit's avatar
Jait Dixit committed
159
160
        )

Jait Dixit's avatar
Jait Dixit committed
161
162
163
164
165
166
167
class TestGLLMTransformation(unittest.TestCase):
    # all domain/codomain checks
    def test_check_codomain_none(self):
        x = GLSpace(8)
        with assert_raises(ValueError):
            transformator.create(x, None)

168
    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
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
        gl_hp_lm_test_spaces,
        testcase_func_name=custom_name_func
    )
    def test_check_codomain_mismatch(self, space):
        x = GLSpace(8)
        with assert_raises(TypeError):
            transformator.create(x, space)

class TestHPLMTransformation(unittest.TestCase):
    # all domain/codomain checks
    def test_check_codomain_none(self):
        x = HPSpace(8)
        with assert_raises(ValueError):
            transformator.create(x, None)

    @parameterized.expand(
        gl_hp_lm_test_spaces,
        testcase_func_name=custom_name_func
    )
    def test_check_codomain_mismatch(self, space):
        x = GLSpace(8)
        with assert_raises(TypeError):
            transformator.create(x, space)

class TestLMTransformation(unittest.TestCase):
    # all domain/codomain checks
    def test_check_codomain_none(self):
        x = LMSpace(8)
        with assert_raises(ValueError):
            transformator.create(x, None)

    @parameterized.expand(
        lm_gl_hp_test_spaces,
        testcase_func_name=custom_name_func
    )
    def test_check_codomain_mismatch(self, space):
        x = LMSpace(8)
        with assert_raises(ValueError):
            transformator.create(x, space)

209

Jait Dixit's avatar
Jait Dixit committed
210
211
if __name__ == '__main__':
    unittest.main()