test_nifty_field.py 9.99 KB
Newer Older
1
2
# -*- coding: utf-8 -*-

csongor's avatar
csongor committed
3
4
from numpy.testing import assert_equal, \
    assert_almost_equal, \
5
6
7
8
9
10
11
    assert_raises

from nose_parameterized import parameterized
import unittest
import itertools
import numpy as np

csongor's avatar
csongor committed
12
13
from d2o import distributed_data_object

csongor's avatar
csongor committed
14
15
16
17
18
19
from nifty import Space, \
    point_space

from nifty.rg import RgSpace

from nifty.lm import *
csongor's avatar
csongor committed
20

21
from nifty.field import Field
22
23

from nifty.nifty_core import POINT_DISTRIBUTION_STRATEGIES
csongor's avatar
csongor committed
24
25
26
27
28
from nifty.rg.nifty_rg import RG_DISTRIBUTION_STRATEGIES, \
    gc as RG_GC
from nifty.lm.nifty_lm import LM_DISTRIBUTION_STRATEGIES, \
    GL_DISTRIBUTION_STRATEGIES, \
    HP_DISTRIBUTION_STRATEGIES
29
30
31
32
33
34
35
36
37
38


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

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)),
    )

csongor's avatar
csongor committed
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
###############################################################################
###############################################################################

all_point_datatypes = [np.dtype('bool'),
                       np.dtype('int16'),
                       np.dtype('int32'),
                       np.dtype('int64'),
                       np.dtype('float32'),
                       np.dtype('float64'),
                       np.dtype('complex64'),
                       np.dtype('complex128')]

all_lm_datatypes = [np.dtype('complex64'),
                    np.dtype('complex128')]

all_gl_datatypes = [np.dtype('float64'),
                    np.dtype('float128')]

all_hp_datatypes = [np.dtype('float64')]

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

DATAMODELS = {}
63
64
65
66
67
DATAMODELS['point_space'] = POINT_DISTRIBUTION_STRATEGIES
DATAMODELS['rg_space'] = RG_DISTRIBUTION_STRATEGIES
DATAMODELS['lm_space'] = LM_DISTRIBUTION_STRATEGIES
DATAMODELS['gl_space'] = GL_DISTRIBUTION_STRATEGIES
DATAMODELS['hp_space'] = HP_DISTRIBUTION_STRATEGIES
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

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

fft_modules = []
for name in ['gfft', 'gfft_dummy', 'pyfftw']:
    if RG_GC.validQ('fft_module', name):
        fft_modules += [name]

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

point_like_spaces = ['point_space', 'rg_space', 'lm_space', 'hp_space',
                     'gl_space']

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

space_list = []

# Add point_spaces
for param in itertools.product([1, 10],
csongor's avatar
csongor committed
87
88
                               all_point_datatypes):
    space_list += [[point_space(num=param[0], dtype=param[1])]]
89
90
91
92
93
94

# Add rg_spaces
for param in itertools.product([(1,), (4, 6), (5, 8)],
                               [False, True],
                               [0, 1, 2],
                               [None, 0.3],
csongor's avatar
csongor committed
95
                               [False],
96
97
                               DATAMODELS['rg_space'],
                               fft_modules):
csongor's avatar
csongor committed
98
    space_list += [[RgSpace(shape=param[0],
csongor's avatar
csongor committed
99
100
101
102
103
                             zerocenter=param[1],
                             complexity=param[2],
                             distances=param[3],
                             harmonic=param[4],
                             fft_module=param[6]), param[5]]]
104
105


csongor's avatar
csongor committed
106
def generate_space_with_size(name, num):
csongor's avatar
csongor committed
107
    space_dict = {'space': Space(),
csongor's avatar
csongor committed
108
                  'point_space': point_space(num),
csongor's avatar
csongor committed
109
110
111
112
                  'rg_space': RgSpace((num, num)),
                  'lm_space': LmSpace(mmax=num+1, lmax=num+1),
                  'hp_space': HpSpace(num),
                  'gl_space': GlSpace(nlat=num, nlon=2*num-1),
csongor's avatar
csongor committed
113
114
115
116
117
                  }
    return space_dict[name]


def generate_data(space):
118
    a = np.arange(space.dim).reshape(space.shape)
csongor's avatar
csongor committed
119
120
    return distributed_data_object(a)

121

122
123
124
125
###############################################################################
###############################################################################

class Test_field_init(unittest.TestCase):
csongor's avatar
csongor committed
126
127
128
129
130
131
132
133
134
135
136
137
    @parameterized.expand(
        itertools.product([(1,), (4, 6), (5, 8)],
                          [False, True],
                          [0, 1, 2],
                          [None, 0.3],
                          [False],
                          fft_modules,
                          DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_successfull_init_and_attributes(self, shape, zerocenter,
                                             complexity, distances, harmonic,
                                             fft_module, datamodel):
csongor's avatar
csongor committed
138
        s = RgSpace(shape=shape, zerocenter=zerocenter,
csongor's avatar
csongor committed
139
140
                     complexity=complexity, distances=distances,
                     harmonic=harmonic, fft_module=fft_module)
141
        f = Field(domain=(s,), dtype=s.dtype, datamodel=datamodel)
csongor's avatar
csongor committed
142
143
        assert (f.domain[0] is s)
        assert (s.check_codomain(f.codomain[0]))
144
        assert (s.shape == f.shape)
145
146


csongor's avatar
csongor committed
147
148
149
150
151
152
153
154
class Test_field_init2(unittest.TestCase):
    @parameterized.expand(
        itertools.product(point_like_spaces, [4],
                          DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_successfull_init_and_attributes(self, name, num, datamodel):
        s = generate_space_with_size(name, num)
        d = generate_data(s)
155
        f = Field(val=d, domain=(s,), dtype=s.dtype, datamodel=datamodel)
csongor's avatar
csongor committed
156
157
        assert (f.domain[0] is s)
        assert (s.check_codomain(f.codomain[0]))
158
        assert (s.shape == f.shape)
csongor's avatar
csongor committed
159

160

csongor's avatar
csongor committed
161
class Test_field_multiple_rg_init(unittest.TestCase):
162
163
164
165
166
167
168
169
170
171
172
173
    @parameterized.expand(
        itertools.product([(1,)],
                          [True],
                          [0],
                          [None],
                          [False],
                          fft_modules,
                          DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_multiple_space_init(self, shape, zerocenter,
                                 complexity, distances, harmonic,
                                 fft_module, datamodel):
csongor's avatar
csongor committed
174
        s1 = RgSpace(shape=shape, zerocenter=zerocenter,
175
176
                      complexity=complexity, distances=distances,
                      harmonic=harmonic, fft_module=fft_module)
csongor's avatar
csongor committed
177
        s2 = RgSpace(shape=shape, zerocenter=zerocenter,
178
179
                      complexity=complexity, distances=distances,
                      harmonic=harmonic, fft_module=fft_module)
180
        f = Field(domain=(s1, s2), dtype=s1.dtype, datamodel=datamodel)
181
182
183
184
        assert (f.domain[0] is s1)
        assert (f.domain[1] is s2)
        assert (s1.check_codomain(f.codomain[0]))
        assert (s2.check_codomain(f.codomain[1]))
185
        assert (s1.shape + s2.shape == f.shape)
csongor's avatar
csongor committed
186

187

csongor's avatar
csongor committed
188
189
190
191
192
193
194
class Test_field_multiple_init(unittest.TestCase):
    @parameterized.expand(
        itertools.product(point_like_spaces, point_like_spaces, [4]),
        testcase_func_name=custom_name_func)
    def test_multiple_space_init(self, space1, space2, shape):
        s1 = generate_space_with_size(space1, shape)
        s2 = generate_space_with_size(space2, shape)
195
        f = Field(domain=(s1, s2))
csongor's avatar
csongor committed
196
197
198
199
        assert (f.domain[0] is s1)
        assert (f.domain[1] is s2)
        assert (s1.check_codomain(f.codomain[0]))
        assert (s2.check_codomain(f.codomain[1]))
200
        assert (s1.shape + s2.shape == f.shape)
201
202
        s3 = generate_space_with_size('hp_space', shape)
        f = Field(domain=(s1, s2, s3))
csongor's avatar
csongor committed
203
204
205
206
207
208
        assert (f.domain[0] is s1)
        assert (f.domain[1] is s2)
        assert (f.domain[2] is s3)
        assert (s1.check_codomain(f.codomain[0]))
        assert (s2.check_codomain(f.codomain[1]))
        assert (s3.check_codomain(f.codomain[2]))
209
210
        assert (s1.shape + s2.shape + s3.shape ==
                f.shape)
csongor's avatar
csongor committed
211

csongor's avatar
csongor committed
212
213
214
215
216

class Test_axis(unittest.TestCase):
    @parameterized.expand(
        itertools.product(point_like_spaces, [4],
                          ['sum', 'prod', 'mean', 'var', 'std', 'median', 'all',
csongor's avatar
csongor committed
217
                           'any', 'min', 'nanmin', 'argmin', 'max', 'nanmax',
csongor's avatar
csongor committed
218
219
220
221
222
223
224
225
                           'argmax'],
                          [None, (0,)],
                          DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_unary_operations(self, name, num, op, axis, datamodel):
        s = generate_space_with_size(name, num)
        d = generate_data(s)
        a = d.get_full_data()
226
227
        f = Field(val=d, domain=(s,), dtype=s.dtype, datamodel=datamodel)
        if op in ['argmin', 'argmax']:
csongor's avatar
csongor committed
228
            assert_almost_equal(getattr(f, op)(),
229
                                getattr(np, op)(a), decimal=4)
csongor's avatar
csongor committed
230
231
        else:
            assert_almost_equal(getattr(f, op)(axis=axis),
csongor's avatar
csongor committed
232
                                getattr(np, op)(a, axis=axis), decimal=4)
csongor's avatar
csongor committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251


binary_operations = [('add','__add__'),('radd','__radd__'),('iadd','__iadd__'),
                     ('sub','__sub__'),('rsub','__rsub__'),('isub','__isub__'),
                     ('mul','__mul__'),('rmul','__rmul__'),('imul','__imul__'),
                     ('div','__div__'),('rdiv','__rdiv__'),('idiv','__idiv__'),
                     ('pow','__pow__'),('rpow','__rpow__'),('ipow','__ipow__'),
                     ('ne','__ne__'),('lt','__lt__'),('eq','__eq__'),
                     ('ge','__ge__'),('gt','__gt__')]

class Test_binary_operation(unittest.TestCase):
    @parameterized.expand(
    itertools.product([point_like_spaces[0]], [4], binary_operations,
                      DATAMODELS['rg_space']),
    testcase_func_name=custom_name_func)
    def test_binary_operations(self, name, num, op, datamodel):
        s = generate_space_with_size(name, num)
        d = generate_data(s)
        a = d.get_full_data()
252
        f = Field(val=d, domain=(s,), dtype=s.dtype, datamodel=datamodel)
csongor's avatar
csongor committed
253
254
255
256
257
258
259
260
261
        d2 = d[::-1]
        a2 = np.copy(a[::-1])
        if op[0] in ['iadd','isub','imul','idiv']:
            getattr(a, op[1])(a2)
            f.binary_operation(d, d2, op[0])
            assert_almost_equal(a,d,4)
        else:
            assert_almost_equal(getattr(a, op[1])(a2),f.binary_operation(d, d2,
                                                                      op[0]), 4)