test_nifty_spaces.py 19.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: utf-8 -*-

from numpy.testing import assert_equal,\
    assert_almost_equal,\
    assert_raises

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

from nifty import space,\
                  point_space,\
                  rg_space,\
                  lm_space,\
                  hp_space,\
Ultima's avatar
Ultima committed
17
18
19
                  gl_space,\
                  field,\
                  distributed_data_object
20
21
22
23

from nifty.nifty_paradict import space_paradict
from nifty.nifty_core import POINT_DISTRIBUTION_STRATEGIES

Ultima's avatar
Ultima committed
24
25
26
27
28
29
from nifty.rg.nifty_rg import RG_DISTRIBUTION_STRATEGIES
from nifty.lm.nifty_lm import LM_DISTRIBUTION_STRATEGIES,\
                              GL_DISTRIBUTION_STRATEGIES,\
                              HP_DISTRIBUTION_STRATEGIES
from nifty.nifty_power_indices import power_indices

30
31
32
33
34
35
36
37
38
39
40
41

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

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

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

Ultima's avatar
Ultima committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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')]
58
59
60

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

Ultima's avatar
Ultima committed
61
62
63
64
65
66
DATAMODELS = {}
DATAMODELS['point_space'] = ['np'] + POINT_DISTRIBUTION_STRATEGIES
DATAMODELS['rg_space'] = ['np'] + RG_DISTRIBUTION_STRATEGIES
DATAMODELS['lm_space'] = ['np'] + LM_DISTRIBUTION_STRATEGIES
DATAMODELS['gl_space'] = ['np'] + GL_DISTRIBUTION_STRATEGIES
DATAMODELS['hp_space'] = ['np'] + HP_DISTRIBUTION_STRATEGIES
67
68
69
70
71
72
73
74
75

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

all_spaces = ['space', 'point_space', 'rg_space', 'lm_space', 'hp_space',
              'gl_space']

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

Ultima's avatar
Ultima committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
###############################################################################

np_spaces = point_like_spaces
d2o_spaces = []
if POINT_DISTRIBUTION_STRATEGIES != []:
    d2o_spaces += ['point_space']
if RG_DISTRIBUTION_STRATEGIES != []:
    d2o_spaces += ['rg_space']
if LM_DISTRIBUTION_STRATEGIES != []:
    d2o_spaces += ['lm_space']
if GL_DISTRIBUTION_STRATEGIES != []:
    d2o_spaces += ['gl_space']
if HP_DISTRIBUTION_STRATEGIES != []:
    d2o_spaces += ['hp_space']


unary_operations = ['pos', 'neg', 'abs', 'real', 'imag', 'nanmin', 'amin',
                    'nanmax', 'amax', 'median', 'mean', 'std', 'var', 'argmin',
                    'argmin_flat', 'argmax', 'argmax_flat', 'conjugate', 'sum',
                    'prod', 'unique', 'copy', 'copy_empty', 'isnan', 'isinf',
                    'isfinite', 'nan_to_num', 'all', 'any', 'None']

binary_operations = ['add', 'radd', 'iadd', 'sub', 'rsub', 'isub', 'mul',
                     'rmul', 'imul', 'div', 'rdiv', 'idiv', 'pow', 'rpow',
                     'ipow', 'ne', 'lt', 'le', 'eq', 'ge', 'gt', 'None']
101
102
103
104
105
106
107
108
109
110
111
112
113
114

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

def generate_space(name):
    space_dict = {'space': space(),
                  'point_space': point_space(10),
                  'rg_space': rg_space((8, 8)),
                  'lm_space': lm_space(mmax=11, lmax=11),
                  'hp_space': hp_space(8),
                  'gl_space': gl_space(nlat=10, nlon=19),
                  }
    return space_dict[name]


Ultima's avatar
Ultima committed
115
116
117
118
119
120
def generate_data(space):
    a = np.arange(space.get_dim()).reshape(space.get_shape())
    data = space.cast(a)
    return data


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
###############################################################################
###############################################################################

class Test_Common_Space_Features(unittest.TestCase):
    @parameterized.expand(all_spaces,
                          testcase_func_name=custom_name_func)
    def test_successfull_init_and_attributes(self, name):
        s = generate_space(name)
        assert(isinstance(s.paradict, space_paradict))

    @parameterized.expand(all_spaces,
                          testcase_func_name=custom_name_func)
    def test_successfull_init_and_methods(self, name):
        s = generate_space(name)
        assert(callable(s._identifier))
        assert(callable(s.__eq__))
        assert(callable(s.__ne__))
        assert(callable(s.__len__))
        assert(callable(s.copy))
        assert(callable(s.getitem))
        assert(callable(s.setitem))
        assert(callable(s.apply_scalar_function))
        assert(callable(s.unary_operation))
        assert(callable(s.binary_operation))
        assert(callable(s.get_norm))
        assert(callable(s.get_shape))
        assert(callable(s.get_dim))
        assert(callable(s.get_dof))
        assert(callable(s.get_meta_volume))
        assert(callable(s.cast))
        assert(callable(s.enforce_power))
        assert(callable(s.check_codomain))
        assert(callable(s.get_codomain))
        assert(callable(s.get_random_values))
        assert(callable(s.calc_weight))
        assert(callable(s.get_weight))
        assert(callable(s.calc_dot))
        assert(callable(s.calc_transform))
        assert(callable(s.calc_smooth))
        assert(callable(s.calc_power))
        assert(callable(s.calc_real_Q))
        assert(callable(s.calc_bincount))
        assert(callable(s.get_plot))
        assert(callable(s.__repr__))
        assert(callable(s.__str__))


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

Ultima's avatar
Ultima committed
171
class Test_Common_Point_Like_Space_Interface(unittest.TestCase):
172
173
174
175
176
177
178
179
180
181
182
183
184

    @parameterized.expand(point_like_spaces,
                          testcase_func_name=custom_name_func)
    def test_successfull_init_and_attributes(self, name):
        s = generate_space(name)

        assert(isinstance(s.paradict, space_paradict))
        assert(isinstance(s.paradict, space_paradict))
        assert(isinstance(s.dtype, np.dtype))
        assert(isinstance(s.datamodel, str))
        assert(isinstance(s.discrete, bool))
        assert(isinstance(s.harmonic, bool))
        assert(isinstance(s.distances, tuple))
Ultima's avatar
Ultima committed
185
186
        if s.harmonic:
            assert(isinstance(s.power_indices, power_indices))
187
188
189

    @parameterized.expand(point_like_spaces,
                          testcase_func_name=custom_name_func)
Ultima's avatar
Ultima committed
190
    def test_getters(self, name):
191
192
193
194
195
196
197
198
        s = generate_space(name)
        assert(isinstance(s.get_shape(), tuple))
        assert(isinstance(s.get_dim(), np.int))

        assert(isinstance(s.get_dof(), np.int))
        assert(isinstance(s.get_dof(split=True), tuple))
        assert_equal(s.get_dof(), np.prod(s.get_dof(split=True)))

Ultima's avatar
Ultima committed
199
200
201
        assert(isinstance(s.get_vol(), np.float))
        assert(isinstance(s.get_dof(split=True), tuple))

202
203
204
205
206
        assert(isinstance(s.get_meta_volume(), np.float))
        assert(isinstance(s.get_meta_volume(split=True), type(s.cast(1))))
        assert_almost_equal(
            s.get_meta_volume(), s.get_meta_volume(split=True).sum(), 2)

Ultima's avatar
Ultima committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#
#class Test_Common_Point_Like_Space_Functions(unittest.TestCase):
#
#    @parameterized.expand(point_like_spaces,
#                          testcase_func_name=custom_name_func)
#    def test_copy(self, name):
#        s = generate_space(name)
#        t = s.copy()
#        assert(s == t)
#        assert(id(s) != id(t))
#
#    @parameterized.expand(point_like_spaces,
#                          testcase_func_name=custom_name_func)
#    def test_unary_operations(self, name):
#        s = generate_space(name)
#        t = s.copy()
#
#
#    @parameterized.expand(point_like_spaces,
#                          testcase_func_name=custom_name_func)
#    def test_apply_scalar_function(self, name):
#        s = generate_space(name)
#        d = generate_data(s)
#        d2 = s.apply_scalar_function(d, lambda x: x**2)
#        assert(isinstance(d2, type(d)))

233
234
235
236

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

Ultima's avatar
Ultima committed
237
class Test_Point_Space(unittest.TestCase):
238
239
240

    @parameterized.expand(
        itertools.product([0, 1, 10],
Ultima's avatar
Ultima committed
241
242
                          all_point_datatypes,
                          DATAMODELS['point_space']),
243
244
245
246
247
248
249
250
251
252
253
        testcase_func_name=custom_name_func)
    def test_successfull_init(self, num, dtype, datamodel):
        p = point_space(num, dtype, datamodel)
        assert_equal(p.paradict['num'], num)
        assert_equal(p.dtype, dtype)
        assert_equal(p.datamodel, datamodel)

        assert_equal(p.discrete, True)
        assert_equal(p.harmonic, False)
        assert_equal(p.distances, (np.float(1.),))

Ultima's avatar
Ultima committed
254
255
###############################################################################

256
257
258
259
260
261
262
263
264
    def test_para(self):
        num = 10
        p = point_space(num)
        assert_equal(p.para[0], num)

        new_num = 15
        p.para = np.array([new_num])
        assert_equal(p.para[0], new_num)

Ultima's avatar
Ultima committed
265
266
###############################################################################

267
268
269
270
271
    def test_init_fail(self):
        assert_raises(ValueError, lambda: point_space(-5))
        assert_raises(ValueError, lambda: point_space((10, 10)))
        assert_raises(ValueError, lambda: point_space(10, np.uint))

Ultima's avatar
Ultima committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
###############################################################################

    @parameterized.expand(
        itertools.product([0, 1, 10],
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_apply_scalar_function(self, num, datamodel):
        s = point_space(num, datamodel=datamodel)
        d = generate_data(s)
        t = s.apply_scalar_function(d, lambda x: x**2)
        assert(s.unary_operation(s.binary_operation(d**2, t, 'eq'), 'all'))
        assert(id(d) != id(t))

        t = s.apply_scalar_function(d, lambda x: x**2, inplace=True)
        assert(s.unary_operation(s.binary_operation(d, t, 'eq'), 'all'))
        assert(id(d) == id(t))

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

    @parameterized.expand(
        itertools.product([1, 10],
                          unary_operations,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_unary_operations(self, num, op, datamodel):
        s = point_space(num, datamodel=datamodel)
        d = s.cast(np.arange(num))
        s.unary_operation(d, op)
        # TODO: Implement value verification

    @parameterized.expand(
        itertools.product([1, 10],
                          binary_operations,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_binary_operations(self, num, op, datamodel):
        s = point_space(num, datamodel=datamodel)
        d = s.cast(np.arange(num))
        d2 = d[::-1]
        s.binary_operation(d, d2, op)
        # TODO: Implement value verification

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

    @parameterized.expand(
        itertools.product(DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_get_norm(self, datamodel):
        num = 10
        s = point_space(num, datamodel=datamodel)
        d = s.cast(np.arange(num))
        assert_almost_equal(s.get_norm(d), 16.881943016134134)
        assert_almost_equal(s.get_norm(d, q=3), 12.651489979526238)

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_get_shape_dim(self, dtype, datamodel):
        num = 10
        s = point_space(num, dtype, datamodel=datamodel)

        assert_equal(s.get_shape(), (num,))
        assert_equal(s.get_dim(), num)

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_get_shape_dof(self, dtype, datamodel):
        num = 10
        s = point_space(num, dtype, datamodel=datamodel)

        if issubclass(dtype.type, np.complexfloating):
            assert_equal(s.get_dof(), 2*num)
            assert_equal(s.get_dof(split=True), (2*num,))
        else:
            assert_equal(s.get_dof(), num)
            assert_equal(s.get_dof(split=True), (num,))

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_get_shape_vol(self, dtype, datamodel):
        num = 10
        s = point_space(num, dtype, datamodel=datamodel)

        assert_equal(s.get_vol(), 1.)
        assert_equal(s.get_vol(split=True), (1.,))

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_get_shape_metavolume(self, dtype, datamodel):
        num = 10
        s = point_space(num, dtype, datamodel=datamodel)

        assert_equal(s.get_meta_volume(), 10.)
        assert(s.unary_operation(s.binary_operation(
                                                s.get_meta_volume(split=True),
                                                s.cast(1), 'eq'),
                                 'all'))

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_cast_from_scalar(self, dtype, datamodel):
        num = 10
        scalar = 4
        s = point_space(num, dtype, datamodel=datamodel)
        if datamodel == 'np':
            d = (np.ones((num,)) * scalar).astype(dtype=dtype)
        else:
            d = distributed_data_object(scalar,
                                        global_shape=(num,),
                                        dtype=dtype,
                                        distribution_strategy=datamodel)

        casted_scalar = s.cast(scalar)
        assert(s.unary_operation(s.binary_operation(casted_scalar, d, 'eq'),
                                 'all'))
        if datamodel != 'np':
            assert(d.equal(casted_scalar))

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_cast_from_field(self, dtype, datamodel):
        num = 10
        a = np.arange(num,).astype(dtype)
        s = point_space(num, dtype, datamodel=datamodel)
        f = field(s, val=a)

        if datamodel == 'np':
            d = a
        else:
            d = distributed_data_object(a, dtype=dtype,
                                        distribution_strategy=datamodel)

        casted_f = s.cast(f)
        assert(s.unary_operation(s.binary_operation(casted_f, d, 'eq'),
                                 'all'))
        if datamodel != 'np':
            assert(d.equal(casted_f))

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_cast_from_ndarray(self, dtype, datamodel):
        num = 10
        a = np.arange(num,)
        s = point_space(num, dtype, datamodel=datamodel)

        if datamodel == 'np':
            d = a.astype(dtype)
        else:
            d = distributed_data_object(a, dtype=dtype,
                                        distribution_strategy=datamodel)

        casted_a = s.cast(a)
        assert(s.unary_operation(s.binary_operation(casted_a, d, 'eq'),
                                 'all'))
        if datamodel != 'np':
            assert(d.equal(casted_a))

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_cast_from_d2o(self, dtype, datamodel):
        num = 10
        pre_a = np.arange(num,)
        a = distributed_data_object(pre_a)
        s = point_space(num, dtype, datamodel=datamodel)

        if datamodel == 'np':
            d = pre_a.astype(dtype)
        else:
            d = distributed_data_object(a, dtype=dtype,
                                        distribution_strategy=datamodel)

        casted_a = s.cast(a)
        assert(s.unary_operation(s.binary_operation(casted_a, d, 'eq'),
                                 'all'))
        if datamodel != 'np':
            assert(d.equal(casted_a))


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

    def test_raise_on_not_implementable_methods(self):
        s = point_space(10)
        assert_raises(lambda: s.enforce_power)
        assert_raises(lambda: s.calc_smooth)
        assert_raises(lambda: s.calc_power)

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

    @parameterized.expand(
        [[10, np.dtype('float64'), 'equal'],
         [10, np.dtype('float32'), 'np'],
         [12, np.dtype('float64'), 'np']],
        testcase_func_name=custom_name_func)
    def test_get_check_codomain(self, num, dtype, datamodel):
        s = point_space(10, dtype=np.dtype('float64'), datamodel='np')

        t = s.get_codomain()
        assert(s.check_codomain(t))

        t_bad = point_space(num, dtype=dtype, datamodel=datamodel)
        assert(s.check_codomain(t_bad) == False)

        assert(s.check_codomain(None) == False)

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

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_cast_from_d2o(self, dtype, datamodel):
        num = 100000
        s = point_space(num, dtype, datamodel=datamodel)

        pm = s.get_random_values(random='pm1')
        assert_almost_equal(0, s.unary_operation(pm, op='mean'), 2)

        gau = s.get_random_values(random='gau',
                                  mean=10,
                                  dev=)











534
535
536