test_nifty_transforms.py 6.01 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
55
56
57
class TestRGRGTransformation(unittest.TestCase):
    @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
58
59
        x = RGSpace((8, 8))
        with assert_raises(TypeError):
Jait Dixit's avatar
Jait Dixit committed
60
            transformator.create(x, space)
Jait Dixit's avatar
Jait Dixit committed
61

Jait Dixit's avatar
Jait Dixit committed
62
63
64
65
66
67
68
    @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
69
70
        assert (RGRGTransformation.check_codomain(x, x.get_codomain()))
        assert (RGRGTransformation.check_codomain(x, x.get_codomain()))
Jait Dixit's avatar
Jait Dixit committed
71

Jait Dixit's avatar
Jait Dixit committed
72
    @parameterized.expand(rg_rg_fft_modules, testcase_func_name=custom_name_func)
Jait Dixit's avatar
Jait Dixit committed
73
74
75
76
77
    def test_shapemismatch(self, module):
        x = RGSpace((8, 8))
        b = d2o.distributed_data_object(np.ones((8, 8)))
        with assert_raises(ValueError):
            transformator.create(
Jait Dixit's avatar
Jait Dixit committed
78
                x, module=module
Jait Dixit's avatar
Jait Dixit committed
79
80
81
            ).transform(b, axes=(0, 1, 2))

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
82
        itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
83
84
85
86
87
88
89
        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(
Jait Dixit's avatar
Jait Dixit committed
90
                x, module=module
Jait Dixit's avatar
Jait Dixit committed
91
            ).transform(a),
92
            weighted_np_transform(a, x, x.get_codomain())
Jait Dixit's avatar
Jait Dixit committed
93
94
95
        )

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
96
        itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
97
98
99
100
101
102
103
104
        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(
Jait Dixit's avatar
Jait Dixit committed
105
                x, module=module
Jait Dixit's avatar
Jait Dixit committed
106
            ).transform(b, axes=(1,)),
107
            weighted_np_transform(a, x, x.get_codomain(), axes=(1,))
Jait Dixit's avatar
Jait Dixit committed
108
109
110
        )

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
111
        itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
112
113
114
115
116
117
118
119
        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(
Jait Dixit's avatar
Jait Dixit committed
120
                x, module=module
Jait Dixit's avatar
Jait Dixit committed
121
            ).transform(b),
122
            weighted_np_transform(a, x, x.get_codomain())
Jait Dixit's avatar
Jait Dixit committed
123
124
125
        )

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
126
        itertools.product(rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
127
128
        testcase_func_name=custom_name_func
    )
Jait Dixit's avatar
Jait Dixit committed
129
    def test_mpi_axesnone(self, shape):
Jait Dixit's avatar
Jait Dixit committed
130
131
132
133
134
        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
135
                x, module='pyfftw'
Jait Dixit's avatar
Jait Dixit committed
136
            ).transform(b),
137
            weighted_np_transform(a, x, x.get_codomain())
Jait Dixit's avatar
Jait Dixit committed
138
139
140
        )

    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
141
        itertools.product(rg_rg_test_shapes),
Jait Dixit's avatar
Jait Dixit committed
142
143
144
145
146
147
148
149
        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(
Jait Dixit's avatar
Jait Dixit committed
150
                x, module='pyfftw'
Jait Dixit's avatar
Jait Dixit committed
151
            ).transform(b),
152
            weighted_np_transform(a, x, x.get_codomain())
Jait Dixit's avatar
Jait Dixit committed
153
154
        )

Jait Dixit's avatar
Jait Dixit committed
155
class TestGLLMTransformation(unittest.TestCase):
156
    @parameterized.expand(
Jait Dixit's avatar
Jait Dixit committed
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
        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):

    @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):
    @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)

186

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