test_nifty_spaces.py 63.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# -*- 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,\
13
14
15
16
    point_space,\
    rg_space,\
    field,\
    distributed_data_object
17
18
19
20

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

21
from nifty.rg.nifty_rg import RG_DISTRIBUTION_STRATEGIES,\
Ultima's avatar
Ultima committed
22
                              gc as RG_GC
Ultima's avatar
Ultima committed
23
from nifty.lm.nifty_lm import LM_DISTRIBUTION_STRATEGIES,\
Ultima's avatar
Ultima committed
24
25
                              GL_DISTRIBUTION_STRATEGIES,\
                              HP_DISTRIBUTION_STRATEGIES
Ultima's avatar
Ultima committed
26
from nifty.nifty_power_indices import power_indices
27
from nifty.nifty_utilities import _hermitianize_inverter as \
Ultima's avatar
Ultima committed
28
                                                        hermitianize_inverter
29

30
31
from nifty.operators.nifty_operators import power_operator

Ultima's avatar
Ultima committed
32
33
available = []
try:
34
    from nifty import lm_space
Ultima's avatar
Ultima committed
35
36
37
38
39
except ImportError:
    pass
else:
    available += ['lm_space']
try:
40
    from nifty import gl_space
Ultima's avatar
Ultima committed
41
42
43
44
45
except ImportError:
    pass
else:
    available += ['gl_space']
try:
46
    from nifty import hp_space
Ultima's avatar
Ultima committed
47
48
49
50
51
52
except ImportError:
    pass
else:
    available += ['hp_space']


53

Ultima's avatar
Ultima committed
54
###############################################################################
55

56
57
58
59
60
61
62
63
64
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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')]
81
82
83

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

Ultima's avatar
Ultima committed
84
DATAMODELS = {}
85
86
87
88
89
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
90
91
92

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

93
94
95
96
97
98
99
fft_modules = []
for name in ['gfft', 'gfft_dummy', 'pyfftw']:
    if RG_GC.validQ('fft_module', name):
        fft_modules += [name]

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

Ultima's avatar
Ultima committed
100
101
102
103
104
105
106
107
all_spaces = ['space', 'point_space', 'rg_space']
if 'lm_space' in available:
    all_spaces += ['lm_space']
if 'gl_space' in available:
    all_spaces += ['gl_space']
if 'hp_space' in available:
    all_spaces += ['hp_space']

108

Ultima's avatar
Ultima committed
109
110
111
112
113
114
115
point_like_spaces = ['point_space', 'rg_space']
if 'lm_space' in available:
    point_like_spaces += ['lm_space']
if 'gl_space' in available:
    point_like_spaces += ['gl_space']
if 'hp_space' in available:
    point_like_spaces += ['hp_space']
116

Ultima's avatar
Ultima committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
###############################################################################

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',
csongor's avatar
csongor committed
135
136
137
                    'argmin_nonflat', 'argmax', 'argmax_nonflat', 'conjugate',
                    'sum', 'prod', 'unique', 'copy', 'copy_empty', 'isnan',
                    'isinf', 'isfinite', 'nan_to_num', 'all', 'any', 'None']
Ultima's avatar
Ultima committed
138
139
140
141

binary_operations = ['add', 'radd', 'iadd', 'sub', 'rsub', 'isub', 'mul',
                     'rmul', 'imul', 'div', 'rdiv', 'idiv', 'pow', 'rpow',
                     'ipow', 'ne', 'lt', 'le', 'eq', 'ge', 'gt', 'None']
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
fft_test_data = np.array(
    [[0.38405405 + 0.32460996j, 0.02718878 + 0.08326207j,
      0.78792080 + 0.81192595j, 0.17535687 + 0.68054781j,
      0.93044845 + 0.71942995j, 0.21179999 + 0.00637665j],
     [0.10905553 + 0.3027462j, 0.37361237 + 0.68434316j,
      0.94070232 + 0.34129582j, 0.04658034 + 0.4575192j,
      0.45057929 + 0.64297612j, 0.01007361 + 0.24953504j],
     [0.39579662 + 0.70881906j, 0.01614435 + 0.82603832j,
      0.84036344 + 0.50321592j, 0.87699553 + 0.40337862j,
      0.11816016 + 0.43332373j, 0.76627757 + 0.66327959j],
     [0.77272335 + 0.18277367j, 0.93341953 + 0.58105518j,
      0.27227913 + 0.17458168j, 0.70204032 + 0.81397425j,
      0.12422993 + 0.19215286j, 0.30897158 + 0.47364969j],
     [0.24702012 + 0.54534373j, 0.55206013 + 0.98406613j,
      0.57408167 + 0.55685406j, 0.87991341 + 0.52534323j,
      0.93912604 + 0.97186519j, 0.77778942 + 0.45812051j],
     [0.79367868 + 0.48149411j, 0.42484378 + 0.74870011j,
      0.79611264 + 0.50926774j, 0.35372794 + 0.10468412j,
      0.46140736 + 0.09449825j, 0.82044644 + 0.95992843j]])

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


168
169
170
171
172
def generate_space(name):
    space_dict = {'space': space(),
                  'point_space': point_space(10),
                  'rg_space': rg_space((8, 8)),
                  }
Ultima's avatar
Ultima committed
173
174
175
176
177
178
179
    if 'lm_space' in available:
        space_dict['lm_space'] = lm_space(mmax=11, lmax=11)
    if 'hp_space' in available:
        space_dict['hp_space'] = hp_space(8)
    if 'gl_space' in available:
        space_dict['gl_space'] = gl_space(nlat=10, nlon=19)

180
181
182
    return space_dict[name]


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def generate_space_with_size(name, num, datamodel='fftw'):
    space_dict = {'space': space(),
                  'point_space': point_space(num, datamodel=datamodel),
                  'rg_space': rg_space((num, num), datamodel=datamodel),
                  }
    if 'lm_space' in available:
        space_dict['lm_space'] = lm_space(mmax=num, lmax=num,
                                          datamodel=datamodel)
    if 'hp_space' in available:
        space_dict['hp_space'] = hp_space(num, datamodel=datamodel)
    if 'gl_space' in available:
        space_dict['gl_space'] = gl_space(nlat=num, nlon=num,
                                          datamodel=datamodel)

    return space_dict[name]

Ultima's avatar
Ultima committed
199
200
201
202
203
204
def generate_data(space):
    a = np.arange(space.get_dim()).reshape(space.get_shape())
    data = space.cast(a)
    return data


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def check_equality(space, data1, data2):
    return space.unary_operation(space.binary_operation(data1, data2, 'eq'),
                                 'all')


def check_almost_equality(space, data1, data2, integers=7):
    return space.unary_operation(
        space.binary_operation(
            space.unary_operation(
                space.binary_operation(data1, data2, 'sub'),
                'abs'),
            10.**(-1. * integers), 'le'),
        'all')


def flip(space, data):
    return space.unary_operation(hermitianize_inverter(data), 'conjugate')

Ultima's avatar
Ultima committed
223

224
225
226
227
###############################################################################
###############################################################################

class Test_Common_Space_Features(unittest.TestCase):
228

229
230
231
232
233
234
235
236
237
238
    @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)
Ultima's avatar
Ultima committed
239
        assert(callable(s.__hash__))
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
        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_shape))
        assert(callable(s.get_dim))
        assert(callable(s.get_dof))
        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))
Ultima's avatar
Ultima committed
259
        assert(callable(s.calc_norm))
260
261
262
263
264
265
266
267
268
269
        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
270
271
272
        assert(s.check_codomain(None) == False)
        assert(isinstance(repr(s), str))

Ultima's avatar
Ultima committed
273
274
275
276
277
278
279
    @parameterized.expand(all_spaces,
                          testcase_func_name=custom_name_func)
    def test_successfull_hashing(self, name):
        s1 = generate_space(name)
        s2 = generate_space(name)
        assert(s1.__hash__() == s2.__hash__())

280
281
282
283

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

Ultima's avatar
Ultima committed
284
class Test_Common_Point_Like_Space_Interface(unittest.TestCase):
285
286
287
288
289
290
291
292
293
294
295

    @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))
Ultima's avatar
Ultima committed
296
#        assert(isinstance(s.harmonic, bool))
297
        assert(isinstance(s.distances, tuple))
Ultima's avatar
Ultima committed
298
299
300
        if hasattr(s, 'harmonic'):
            if s.harmonic:
                assert(isinstance(s.power_indices, power_indices))
301
302
303

    @parameterized.expand(point_like_spaces,
                          testcase_func_name=custom_name_func)
Ultima's avatar
Ultima committed
304
    def test_getters(self, name):
305
306
307
308
309
310
311
312
        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
313
314
315
        assert(isinstance(s.get_vol(), np.float))
        assert(isinstance(s.get_dof(split=True), tuple))

316
        assert(isinstance(s.get_meta_volume(), np.float))
csongor's avatar
csongor committed
317
        print(s.get_meta_volume(split=True), type(s.cast(1)))
318
319
320
321
        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)

322
323
324
325
326
327
328
    @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))
Ultima's avatar
Ultima committed
329

330
331
332
333

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

Ultima's avatar
Ultima committed
334
class Test_Point_Space(unittest.TestCase):
335
336
337

    @parameterized.expand(
        itertools.product([0, 1, 10],
Ultima's avatar
Ultima committed
338
339
                          all_point_datatypes,
                          DATAMODELS['point_space']),
340
341
342
343
344
345
346
347
348
349
        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.distances, (np.float(1.),))

Ultima's avatar
Ultima committed
350
351
###############################################################################

352
353
354
355
356
357
358
359
360
    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
361
362
###############################################################################

363
364
365
366
367
    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
368
369
370
371
372
373
374
375
376
377
###############################################################################

    @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)
378
        assert(check_equality(s, d**2, t))
Ultima's avatar
Ultima committed
379
380
381
        assert(id(d) != id(t))

        t = s.apply_scalar_function(d, lambda x: x**2, inplace=True)
382
        assert(check_equality(s, d, t))
Ultima's avatar
Ultima committed
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
        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(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):
434
435
            assert_equal(s.get_dof(), 2 * num)
            assert_equal(s.get_dof(split=True), (2 * num,))
Ultima's avatar
Ultima committed
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
        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.)
464
        assert(check_equality(s, s.get_meta_volume(split=True), s.cast(1)))
Ultima's avatar
Ultima committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484

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

    @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)
485
        assert(check_equality(s, casted_scalar, d))
Ultima's avatar
Ultima committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        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)
508
        assert(check_equality(s, casted_f, d))
Ultima's avatar
Ultima committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
        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)
530
        assert(check_equality(s, casted_a, d))
Ultima's avatar
Ultima committed
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
        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)
553
        assert(check_equality(s, casted_a, d))
Ultima's avatar
Ultima committed
554
555
556
557
558
559
560
561
        if datamodel != 'np':
            assert(d.equal(casted_a))


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

    def test_raise_on_not_implementable_methods(self):
        s = point_space(10)
562
563
564
565
        assert_raises(AttributeError, lambda: s.enforce_power(1))
        assert_raises(AttributeError, lambda: s.calc_smooth(1))
        assert_raises(AttributeError, lambda: s.calc_power(1))
        assert_raises(AttributeError, lambda: s.calc_transform(1))
Ultima's avatar
Ultima committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590

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

    @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)
591
592
593
594
    def test_get_random_values(self, dtype, datamodel):
        if dtype == np.dtype('bool'):
            return None

Ultima's avatar
Ultima committed
595
596
597
598
        num = 100000
        s = point_space(num, dtype, datamodel=datamodel)

        pm = s.get_random_values(random='pm1')
599
600
601
        assert(abs(s.unary_operation(pm, op='mean')) < 0.1)
        if datamodel != 'np':
            assert(pm.distribution_strategy == datamodel)
Ultima's avatar
Ultima committed
602

603
604
605
606
607
608
609
        std = 4
        mean = 5
        gau = s.get_random_values(random='gau', mean=mean, std=std)
        assert(abs(gau.std() - std) / std < 0.2)
        assert(abs(gau.mean() - mean) / mean < 0.2)
        if datamodel != 'np':
            assert(pm.distribution_strategy == datamodel)
Ultima's avatar
Ultima committed
610

611
612
613
614
615
616
617
        vmin = -4
        vmax = 10
        uni = s.get_random_values(random='uni', vmin=vmin, vmax=vmax)
        assert(abs(uni.real.mean() - 3.) / 3. < 0.1)
        assert(abs(uni.real.std() - 4.) / 4. < 0.1)
        if datamodel != 'np':
            assert(pm.distribution_strategy == datamodel)
Ultima's avatar
Ultima committed
618

619
###############################################################################
Ultima's avatar
Ultima committed
620

621
622
623
624
625
626
627
628
629
630
631
    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_get_calc_weight(self, dtype, datamodel):
        num = 100
        s = point_space(num, dtype, datamodel=datamodel)
        weight = 1
        assert_equal(s.get_weight(), weight)
        assert_equal(s.get_weight(power=4), weight)
        assert_equal(s.get_weight(power=4, split=True), (weight,))
Ultima's avatar
Ultima committed
632

633
634
        data = s.cast(2)
        assert(check_equality(s, data, s.calc_weight(data)))
Ultima's avatar
Ultima committed
635

636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
###############################################################################

    @parameterized.expand(
        itertools.product(all_point_datatypes,
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_calc_dot(self, dtype, datamodel):
        num = 100
        s = point_space(num, dtype, datamodel=datamodel)
        if dtype == np.dtype('bool'):
            assert_equal(s.calc_dot(1, 1), 1)
        else:
            assert_equal(s.calc_dot(1, 1), num)
            assert_equal(s.calc_dot(np.arange(num), 1), num * (num - 1.) / 2.)

Ultima's avatar
Ultima committed
651
652
653
654
655
656
657
658
659
660
661
662
###############################################################################

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

663
###############################################################################
Ultima's avatar
Ultima committed
664

665
666
667
668
669
670
671
672
673
674
    @parameterized.expand(
        itertools.product(DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_calc_real_Q(self, datamodel):
        num = 100
        s = point_space(num, dtype=np.complex, datamodel=datamodel)
        real_data = s.cast(1)
        assert(s.calc_real_Q(real_data))
        complex_data = s.cast(1 + 1j)
        assert(s.calc_real_Q(complex_data) == False)
Ultima's avatar
Ultima committed
675

676
###############################################################################
Ultima's avatar
Ultima committed
677

678
679
680
681
682
683
684
685
686
687
688
689
    @parameterized.expand(
        itertools.product(DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
    def test_calc_bincount(self, datamodel):
        num = 10
        s = point_space(num, dtype=np.int, datamodel=datamodel)
        data = s.cast(np.array([1, 1, 2, 0, 5, 8, 4, 5, 4, 5]))
        weights = np.arange(10) / 10.
        assert_equal(s.calc_bincount(data),
                     np.array([1, 2, 1, 0, 2, 3, 0, 0, 1]))
        assert_equal(s.calc_bincount(data, weights=weights),
                     np.array([0.3, 0.1, 0.2, 0, 1.4, 2, 0, 0, 0.5]))
Ultima's avatar
Ultima committed
690
691


692
693
###############################################################################
###############################################################################
Ultima's avatar
Ultima committed
694

695
class Test_RG_Space(unittest.TestCase):
696

697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
    @parameterized.expand(
        itertools.product([(1,), (10, 10)],
                          [0, 1, 2],
                          [True, False],
                          [None, 0.5],
                          [True, False],
                          fft_modules,
                          DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_successfull_init(self, shape, complexity, zerocenter, distances,
                              harmonic, fft_module, datamodel):
        x = rg_space(shape,
                     complexity=complexity,
                     zerocenter=zerocenter,
                     distances=distances,
                     harmonic=harmonic,
                     fft_module=fft_module,
                     datamodel=datamodel)
Ultima's avatar
Ultima committed
715
        assert(isinstance(x.harmonic, bool))
716
717
718
719
720
721
722
723
        assert_equal(x.get_shape(), shape)
        assert_equal(x.dtype,
                     np.dtype('float64') if complexity == 0 else
                     np.dtype('complex128'))
        assert_equal(x.datamodel, datamodel)
        assert_equal(x.distances,
                     1. / np.array(shape) if distances is None else
                     np.ones(len(shape)) * distances)
724

725
###############################################################################
726

727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
    def test_para(self):
        shape = (10, 10)
        zerocenter = True
        complexity = 2
        x = rg_space(shape, zerocenter=zerocenter, complexity=complexity)
        assert_equal(x.para, np.array([10, 10, 2, 1, 1]))

        new_para = np.array([6, 6, 1, 0, 1])
        x.para = new_para
        assert_equal(x.para, new_para)

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

    def test_init_fail(self):
        assert_raises(ValueError, lambda: rg_space((-3, 10)))
        assert_raises(ValueError, lambda: rg_space((10, 10), complexity=3))
        assert_raises(ValueError, lambda: rg_space((10, 10),
                                                   distances=[1, 1, 1]))
        assert_raises(ValueError, lambda: rg_space((10, 10),
                                                   zerocenter=[1, 1, 1]))

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

    @parameterized.expand(
        DATAMODELS['rg_space'],
        testcase_func_name=custom_name_func)
    def test_cast_to_hermitian(self, datamodel):
        shape = (10, 10)
        x = rg_space(shape, complexity=1)
        data = np.random.random(shape) + np.random.random(shape) * 1j
        casted_data = x.cast(data)
        flipped_data = flip(x, casted_data)
        assert(check_equality(x, flipped_data, casted_data))

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

    @parameterized.expand(
        DATAMODELS['rg_space'],
        testcase_func_name=custom_name_func)
    def test_enforce_power(self, datamodel):
        shape = (6, 6)
Ultima's avatar
Ultima committed
768
        x = rg_space(shape, datamodel=datamodel)
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959

        assert_equal(x.enforce_power(2),
                     np.array([2., 2., 2., 2., 2., 2., 2., 2., 2., 2.]))
        assert_almost_equal(
            x.enforce_power(lambda x: 42 / (1 + x)**5),
            np.array([4.20000000e+01, 1.31250000e+00, 5.12118970e-01,
                      1.72839506e-01, 1.18348051e-01, 5.10678257e-02,
                      4.10156250e-02, 3.36197167e-02, 2.02694134e-02,
                      1.06047106e-02]))

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

    @parameterized.expand(
        itertools.product([0, 1, 2],
                          [None, 1, 10],
                          [False, True]),
        testcase_func_name=custom_name_func)
    def test_get_check_codomain(self, complexity, distances, harmonic):
        shape = (6, 6)
        x = rg_space(shape, complexity=complexity, distances=distances,
                     harmonic=harmonic)
        y = x.get_codomain()
        assert(x.check_codomain(y))
        assert(y.check_codomain(x))

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

    @parameterized.expand(
        itertools.product([True], #[True, False],
                          ['pyfftw']),
                          #DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_get_random_values(self, harmonic, datamodel):
        x = rg_space((4, 4), complexity=1, harmonic=harmonic,
                     datamodel=datamodel)

        # pm1
        data = x.get_random_values(random='pm1')
        flipped_data = flip(x, data)
        assert(check_almost_equality(x, data, flipped_data))

        # gau
        data = x.get_random_values(random='gau', mean=4 + 3j, std=2)
        flipped_data = flip(x, data)
        assert(check_almost_equality(x, data, flipped_data))

        # uni
        data = x.get_random_values(random='uni', vmin=-2, vmax=4)
        flipped_data = flip(x, data)
        assert(check_almost_equality(x, data, flipped_data))

        # syn
        data = x.get_random_values(random='syn',
                                   spec=lambda x: 42 / (1 + x)**3)
        flipped_data = flip(x, data)
        assert(check_almost_equality(x, data, flipped_data))

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

    @parameterized.expand(
        DATAMODELS['rg_space'],
        testcase_func_name=custom_name_func)
    def test_calc_dot(self, datamodel):
        shape = (8, 8)
        a = np.arange(np.prod(shape)).reshape(shape)
        x = rg_space(shape)
        assert_equal(x.calc_dot(a, a), 85344)
        assert_equal(x.calc_dot(a, 1), 2016)
        assert_equal(x.calc_dot(1, a), 2016)

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

    @parameterized.expand(
        itertools.product([0, 1],
                          DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_calc_transform_general(self, complexity, datamodel):
        data = fft_test_data.copy()
        shape = data.shape

        x = rg_space(shape, complexity=complexity, datamodel=datamodel)
        data = fft_test_data.copy()
        data = x.cast(data)
        check_equality(x, data, x.calc_transform(x.calc_transform(data)))

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

    @parameterized.expand(
        itertools.product(fft_modules,
                          DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_calc_transform_explicit(self, fft_module, datamodel):
        data = fft_test_data.copy()
        shape = data.shape

        x = rg_space(shape, complexity=2, zerocenter=False,
                     fft_module=fft_module, datamodel=datamodel)
        casted_data = x.cast(data)
        assert(check_almost_equality(x, x.calc_transform(casted_data),
                                     np.array([[0.50541615 + 0.50558267j, -0.01458536 - 0.01646137j,
                                                0.01649006 + 0.01990988j, 0.04668049 - 0.03351745j,
                                                -0.04382765 - 0.06455639j, -0.05978564 + 0.01334044j],
                                               [-0.05347464 + 0.04233343j, -0.05167177 + 0.00643947j,
                                                -0.01995970 - 0.01168872j, 0.10653817 + 0.03885947j,
                                                -0.03298075 - 0.00374715j, 0.00622585 - 0.01037453j],
                                               [-0.01128964 - 0.02424692j, -0.03347793 - 0.0358814j,
                                                -0.03924164 - 0.01978305j, 0.03821242 - 0.00435542j,
                                                0.07533170 + 0.14590143j, -0.01493027 - 0.02664675j],
                                               [0.02238926 + 0.06140625j, -0.06211313 + 0.03317753j,
                                                0.01519073 + 0.02842563j, 0.00517758 + 0.08601604j,
                                                -0.02246912 - 0.01942764j, -0.06627311 - 0.08763801j],
                                               [-0.02492378 - 0.06097411j, 0.06365649 - 0.09346585j,
                                                0.05031486 + 0.00858656j, -0.00881969 + 0.01842357j,
                                                -0.01972641 - 0.00994365j, 0.05289453 - 0.06822038j],
                                               [-0.01865586 - 0.08640926j, 0.03414096 - 0.02605602j,
                                                -0.09492552 + 0.01306734j, 0.09355730 + 0.07553701j,
                                                -0.02395259 - 0.02185743j, -0.03107832 - 0.04714527j]])))

        x = rg_space(shape, complexity=2, zerocenter=True,
                     fft_module=fft_module, datamodel=datamodel)
        casted_data = x.cast(data)
        assert(check_almost_equality(x, x.calc_transform(casted_data),
                                     np.array([[0.00517758 + 0.08601604j, 0.02246912 + 0.01942764j,
                                                -0.06627311 - 0.08763801j, -0.02238926 - 0.06140625j,
                                                -0.06211313 + 0.03317753j, -0.01519073 - 0.02842563j],
                                               [0.00881969 - 0.01842357j, -0.01972641 - 0.00994365j,
                                                -0.05289453 + 0.06822038j, -0.02492378 - 0.06097411j,
                                                -0.06365649 + 0.09346585j, 0.05031486 + 0.00858656j],
                                               [0.09355730 + 0.07553701j, 0.02395259 + 0.02185743j,
                                                -0.03107832 - 0.04714527j, 0.01865586 + 0.08640926j,
                                                0.03414096 - 0.02605602j, 0.09492552 - 0.01306734j],
                                               [-0.04668049 + 0.03351745j, -0.04382765 - 0.06455639j,
                                                0.05978564 - 0.01334044j, 0.50541615 + 0.50558267j,
                                                0.01458536 + 0.01646137j, 0.01649006 + 0.01990988j],
                                               [0.10653817 + 0.03885947j, 0.03298075 + 0.00374715j,
                                                0.00622585 - 0.01037453j, 0.05347464 - 0.04233343j,
                                                -0.05167177 + 0.00643947j, 0.01995970 + 0.01168872j],
                                               [-0.03821242 + 0.00435542j, 0.07533170 + 0.14590143j,
                                                0.01493027 + 0.02664675j, -0.01128964 - 0.02424692j,
                                                0.03347793 + 0.0358814j, -0.03924164 - 0.01978305j]])))

        x = rg_space(shape, complexity=2, zerocenter=[True, False],
                     fft_module=fft_module, datamodel=datamodel)
        casted_data = x.cast(data)
        assert(check_almost_equality(x, x.calc_transform(casted_data),
                                     np.array([[-0.02238926 - 0.06140625j, 0.06211313 - 0.03317753j,
                                                -0.01519073 - 0.02842563j, -0.00517758 - 0.08601604j,
                                                0.02246912 + 0.01942764j, 0.06627311 + 0.08763801j],
                                               [-0.02492378 - 0.06097411j, 0.06365649 - 0.09346585j,
                                                0.05031486 + 0.00858656j, -0.00881969 + 0.01842357j,
                                                -0.01972641 - 0.00994365j, 0.05289453 - 0.06822038j],
                                               [0.01865586 + 0.08640926j, -0.03414096 + 0.02605602j,
                                                0.09492552 - 0.01306734j, -0.09355730 - 0.07553701j,
                                                0.02395259 + 0.02185743j, 0.03107832 + 0.04714527j],
                                               [0.50541615 + 0.50558267j, -0.01458536 - 0.01646137j,
                                                0.01649006 + 0.01990988j, 0.04668049 - 0.03351745j,
                                                -0.04382765 - 0.06455639j, -0.05978564 + 0.01334044j],
                                               [0.05347464 - 0.04233343j, 0.05167177 - 0.00643947j,
                                                0.01995970 + 0.01168872j, -0.10653817 - 0.03885947j,
                                                0.03298075 + 0.00374715j, -0.00622585 + 0.01037453j],
                                               [-0.01128964 - 0.02424692j, -0.03347793 - 0.0358814j,
                                                -0.03924164 - 0.01978305j, 0.03821242 - 0.00435542j,
                                                0.07533170 + 0.14590143j, -0.01493027 - 0.02664675j]])))

        x = rg_space(shape, complexity=2, zerocenter=[True, False],
                     fft_module=fft_module, datamodel=datamodel)
        y = rg_space(shape, complexity=2, zerocenter=[False, True],
                     distances=[1, 1], harmonic=True,
                     fft_module=fft_module, datamodel=datamodel)
        casted_data = x.cast(data)
        assert(check_almost_equality(x, x.calc_transform(casted_data,
                                                         codomain=y),
                                     np.array([[0.04668049 - 0.03351745j, -0.04382765 - 0.06455639j,
                                                -0.05978564 + 0.01334044j, 0.50541615 + 0.50558267j,
                                                -0.01458536 - 0.01646137j, 0.01649006 + 0.01990988j],
                                               [-0.10653817 - 0.03885947j, 0.03298075 + 0.00374715j,
                                                -0.00622585 + 0.01037453j, 0.05347464 - 0.04233343j,
                                                0.05167177 - 0.00643947j, 0.01995970 + 0.01168872j],
                                               [0.03821242 - 0.00435542j, 0.07533170 + 0.14590143j,
                                                -0.01493027 - 0.02664675j, -0.01128964 - 0.02424692j,
                                                -0.03347793 - 0.0358814j, -0.03924164 - 0.01978305j],
                                               [-0.00517758 - 0.08601604j, 0.02246912 + 0.01942764j,
                                                0.06627311 + 0.08763801j, -0.02238926 - 0.06140625j,
                                                0.06211313 - 0.03317753j, -0.01519073 - 0.02842563j],
                                               [-0.00881969 + 0.01842357j, -0.01972641 - 0.00994365j,
                                                0.05289453 - 0.06822038j, -0.02492378 - 0.06097411j,
                                                0.06365649 - 0.09346585j, 0.05031486 + 0.00858656j],
                                               [-0.09355730 - 0.07553701j, 0.02395259 + 0.02185743j,
                                                0.03107832 + 0.04714527j, 0.01865586 + 0.08640926j,
                                                -0.03414096 + 0.02605602j, 0.09492552 - 0.01306734j]])))

Ultima's avatar
Ultima committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
###############################################################################

    @parameterized.expand(
        itertools.product(fft_modules,
                          [(6, 6), (8, 8), (6, 8)],
                          [(True, True), (False, False),
                           (True, False), (False, True)],
                          [(True, True), (False, False),
                           (True, False), (False, True)],
                          DATAMODELS['rg_space']),
        testcase_func_name=custom_name_func)
    def test_calc_transform_variations(self, fft_module, shape, zerocenter_in,
                                       zerocenter_out, datamodel):
        data = np.arange(np.prod(shape)).reshape(shape)
        x = rg_space(shape, complexity=2, zerocenter=zerocenter_in,
                     fft_module=fft_module, datamodel=datamodel)
        y = x.get_codomain()
        y.paradict['zerocenter'] = zerocenter_out

        casted_data = x.cast(data)
        x_result = x.calc_transform(casted_data, codomain=y)

        np_data = data.copy()
        np_data = np.fft.fftshift(np_data, axes=np.where(zerocenter_in)[0])
        np_data = np.fft.fftn(np_data)
        np_data = np.fft.fftshift(np_data, axes=np.where(zerocenter_out)[0])
        np_result = np_data/np.prod(shape)
        assert(check_almost_equality(x, x_result, np_result))

989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
###############################################################################

    @parameterized.expand(DATAMODELS['rg_space'],
                          testcase_func_name=custom_name_func)
    def test_calc_smooth(self, datamodel):
        sigma = 0.01
        shape = (8, 8)
        a = np.arange(np.prod(shape)).reshape(shape)
        x = rg_space(shape)
        casted_a = x.cast(a)
        assert(check_almost_equality(x, x.calc_smooth(casted_a, sigma=sigma),
                                     np.array([[0.3869063,   1.33370382,   2.34906384,   3.3400879,
                                                4.34774552,   5.33876958,   6.3541296,   7.30092712],
                                               [7.96128648,   8.90808401,   9.92344403,  10.91446809,
                                                11.9221257,  12.91314976,  13.92850978,  14.87530731],
                                               [16.08416664,  17.03096417,  18.04632419,  19.03734824,
                                                20.04500586,  21.03602992,  22.05138994,  22.99818747],
                                               [24.01235911,  24.95915664,  25.97451666,  26.96554072,
                                                27.97319833,  28.96422239,  29.97958241,  30.92637994],
                                               [32.07362006,  33.02041759,  34.03577761,  35.02680167,
                                                36.03445928,  37.02548334,  38.04084336,  38.98764089],
                                               [40.00181253,  40.94861006,  41.96397008,  42.95499414,
                                                43.96265176,  44.95367581,  45.96903583,  46.91583336],
                                               [48.12469269,  49.07149022,  50.08685024,  51.0778743,
                                                52.08553191,  53.07655597,  54.09191599,  55.03871352],
                                               [55.69907288,  56.6458704,  57.66123042,  58.65225448,
                                                59.6599121,  60.65093616,  61.66629618,  62.6130937]])))

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

    @parameterized.expand(DATAMODELS['rg_space'],
                          testcase_func_name=custom_name_func)
    def test_calc_power(self, datamodel):
        shape = (8, 8)
        a = np.arange(np.prod(shape)).reshape(shape)
        x = rg_space(shape)
        assert_almost_equal(x.calc_power(a),
                            np.array([992.25, 55.48097039, 0., 16.25,
                                      0., 0., 9.51902961, 0.,
                                      0., 8.125, 0., 0.,
                                      0., 0., 0.]))
Ultima's avatar
Ultima committed
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043


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

class Test_Lm_Space(unittest.TestCase):

    @parameterized.expand(
        itertools.product([1, 17],
                          [None, 12, 17],
                          all_lm_datatypes,
                          DATAMODELS['lm_space']),
        testcase_func_name=custom_name_func)
    def test_successfull_init(self, lmax, mmax, dtype, datamodel):
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
        if datamodel in ['not']:
            l = lm_space(lmax, mmax=mmax, dtype=dtype, datamodel=datamodel)
            assert(isinstance(l.harmonic, bool))
            assert_equal(l.paradict['lmax'], lmax)
            if mmax is None or mmax > lmax:
                assert_equal(l.paradict['mmax'], lmax)
            else:
                assert_equal(l.paradict['mmax'], mmax)
            assert_equal(l.dtype, dtype)
            assert_equal(l.datamodel, datamodel)
            assert_equal(l.discrete, True)
            assert_equal(l.harmonic, True)
            assert_equal(l.distances, (np.float(1),))
Ultima's avatar
Ultima committed
1057
        else:
1058
1059
            with assert_raises(NotImplementedError): lm_space(lmax, mmax=mmax, dtype=dtype, datamodel=datamodel)

Ultima's avatar
Ultima committed
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101

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

    def test_para(self):
        lmax = 17
        mmax = 12
        l = lm_space(lmax, mmax=mmax)
        assert_equal(l.para, np.array([lmax, mmax]))

        new_para = np.array([9, 12])
        l.para = new_para
        assert_equal(l.para, np.array([9, 9]))

    def test_get_shape_dof_meta_volume(self):
        lmax = 17
        mmax = 12
        l = lm_space(lmax, mmax=mmax)

        assert_equal(l.get_shape(), (156,))
        assert_equal(l.get_dof(), 294)
        assert_equal(l.get_dof(split=True), (294,))
        assert_equal(l.get_meta_volume(), 294.)
        assert_equal(l.get_meta_volume(split=True),
                     l.cast(np.concatenate([np.ones(18), np.ones(138)*2])))

    def test_cast(self):
        lmax = 17
        mmax = 12
        l = lm_space(lmax, mmax=mmax)

        casted = l.cast(1+1j)
        real_part = casted[:18]
        assert(real_part,  l.unary_operation(real_part, 'real'))

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

    @parameterized.expand(
        DATAMODELS['lm_space'],
        testcase_func_name=custom_name_func)
    def test_enforce_power(self, datamodel):
        lmax = 17
        mmax = 12
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
        if datamodel in ['not']:
            l = lm_space(lmax, mmax=mmax, datamodel=datamodel)

            assert_equal(l.enforce_power(2),
                         np.ones(18)*2)
            assert_almost_equal(
                l.enforce_power(lambda x: 42 / (1 + x)**5),
                np.array([  4.20000000e+01,   1.31250000e+00,   1.72839506e-01,
             4.10156250e-02,   1.34400000e-02,   5.40123457e-03,
             2.49895877e-03,   1.28173828e-03,   7.11273688e-04,
             4.20000000e-04,   2.60786956e-04,   1.68788580e-04,
             1.13118211e-04,   7.80924615e-05,   5.53086420e-05,
             4.00543213e-05,   2.95804437e-05,   2.22273027e-05]))
        else:
            with assert_raises(NotImplementedError): lm_space(lmax, mmax=mmax, datamodel=datamodel)
Ultima's avatar
Ultima committed
1117
1118
1119
1120
1121
1122
1123
1124

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

    @parameterized.expand(DATAMODELS['lm_space'],
                          testcase_func_name=custom_name_func)
    def test_get_check_codomain(self, datamodel):
        lmax = 23
        mmax = 23
1125
1126
        if datamodel in ['not']:
            l = lm_space(lmax, mmax=mmax, datamodel=datamodel)
Ultima's avatar
Ultima committed
1127

1128
            y = l.get_codomain()
Ultima's avatar
Ultima committed
1129
1130
1131
            assert(l.check_codomain(y))
            assert(y.check_codomain(l))

1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
            if 'hp_space' in available:
                y = l.get_codomain('hp')
                assert(l.check_codomain(y))
                assert(y.check_codomain(l))
            if 'gl_space' in available:
                y = l.get_codomain('gl')
                assert(l.check_codomain(y))
                assert(y.check_codomain(l))
        else:
            with assert_raises(NotImplementedError): lm_space(lmax, mmax=mmax, datamodel=datamodel)

Ultima's avatar
Ultima committed
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354

###############################################################################
#
#    @parameterized.expand(
#        itertools.product([True], #[True, False],
#                          ['pyfftw']),
#                          #DATAMODELS['rg_space']),
#        testcase_func_name=custom_name_func)
#    def test_get_random_values(self, harmonic, datamodel):
#        x = rg_space((4, 4), complexity=1, harmonic=harmonic,
#                     datamodel=datamodel)
#
#        # pm1
#        data = x.get_random_values(random='pm1')
#        flipped_data = flip(x, data)
#        assert(check_almost_equality(x, data, flipped_data))
#
#        # gau
#        data = x.get_random_values(random='gau', mean=4 + 3j, std=2)
#        flipped_data = flip(x, data)
#        assert(check_almost_equality(x, data, flipped_data))
#
#        # uni
#        data = x.get_random_values(random='uni', vmin=-2, vmax=4)
#        flipped_data = flip(x, data)
#        assert(check_almost_equality(x, data, flipped_data))
#
#        # syn
#        data = x.get_random_values(random='syn',
#                                   spec=lambda x: 42 / (1 + x)**3)
#        flipped_data = flip(x, data)
#        assert(check_almost_equality(x, data, flipped_data))
#
################################################################################
#
#    @parameterized.expand(
#        DATAMODELS['rg_space'],
#        testcase_func_name=custom_name_func)
#    def test_calc_dot(self, datamodel):
#        shape = (8, 8)
#        a = np.arange(np.prod(shape)).reshape(shape)
#        x = rg_space(shape)
#        assert_equal(x.calc_dot(a, a), 85344)
#        assert_equal(x.calc_dot(a, 1), 2016)
#        assert_equal(x.calc_dot(1, a), 2016)
#
################################################################################
#
#    @parameterized.expand(
#        itertools.product([0, 1],
#                          DATAMODELS['rg_space']),
#        testcase_func_name=custom_name_func)
#    def test_calc_transform_general(self, complexity, datamodel):
#        data = fft_test_data.copy()
#        shape = data.shape
#
#        x = rg_space(shape, complexity=complexity, datamodel=datamodel)
#        data = fft_test_data.copy()
#        data = x.cast(data)
#        check_equality(x, data, x.calc_transform(x.calc_transform(data)))
#
################################################################################
#
#    @parameterized.expand(
#        itertools.product(fft_modules,
#                          DATAMODELS['rg_space']),
#        testcase_func_name=custom_name_func)
#    def test_calc_transform_explicit(self, fft_module, datamodel):
#        data = fft_test_data.copy()
#        shape = data.shape
#
#        x = rg_space(shape, complexity=2, zerocenter=False,
#                     fft_module=fft_module, datamodel=datamodel)
#        casted_data = x.cast(data)
#        assert(check_almost_equality(x, x.calc_transform(casted_data),
#                                     np.array([[0.50541615 + 0.50558267j, -0.01458536 - 0.01646137j,
#                                                0.01649006 + 0.01990988j, 0.04668049 - 0.03351745j,
#                                                -0.04382765 - 0.06455639j, -0.05978564 + 0.01334044j],
#                                               [-0.05347464 + 0.04233343j, -0.05167177 + 0.00643947j,
#                                                -0.01995970 - 0.01168872j, 0.10653817 + 0.03885947j,
#                                                -0.03298075 - 0.00374715j, 0.00622585 - 0.01037453j],
#                                               [-0.01128964 - 0.02424692j, -0.03347793 - 0.0358814j,
#                                                -0.03924164 - 0.01978305j, 0.03821242 - 0.00435542j,
#                                                0.07533170 + 0.14590143j, -0.01493027 - 0.02664675j],
#                                               [0.02238926 + 0.06140625j, -0.06211313 + 0.03317753j,
#                                                0.01519073 + 0.02842563j, 0.00517758 + 0.08601604j,
#                                                -0.02246912 - 0.01942764j, -0.06627311 - 0.08763801j],
#                                               [-0.02492378 - 0.06097411j, 0.06365649 - 0.09346585j,
#                                                0.05031486 + 0.00858656j, -0.00881969 + 0.01842357j,
#                                                -0.01972641 - 0.00994365j, 0.05289453 - 0.06822038j],
#                                               [-0.01865586 - 0.08640926j, 0.03414096 - 0.02605602j,
#                                                -0.09492552 + 0.01306734j, 0.09355730 + 0.07553701j,
#                                                -0.02395259 - 0.02185743j, -0.03107832 - 0.04714527j]])))
#
#        x = rg_space(shape, complexity=2, zerocenter=True,
#                     fft_module=fft_module, datamodel=datamodel)
#        casted_data = x.cast(data)
#        assert(check_almost_equality(x, x.calc_transform(casted_data),
#                                     np.array([[0.00517758 + 0.08601604j, 0.02246912 + 0.01942764j,
#                                                -0.06627311 - 0.08763801j, -0.02238926 - 0.06140625j,
#                                                -0.06211313 + 0.03317753j, -0.01519073 - 0.02842563j],
#                                               [0.00881969 - 0.01842357j, -0.01972641 - 0.00994365j,
#                                                -0.05289453 + 0.06822038j, -0.02492378 - 0.06097411j,
#                                                -0.06365649 + 0.09346585j, 0.05031486 + 0.00858656j],
#                                               [0.09355730 + 0.07553701j, 0.02395259 + 0.02185743j,
#                                                -0.03107832 - 0.04714527j, 0.01865586 + 0.08640926j,
#                                                0.03414096 - 0.02605602j, 0.09492552 - 0.01306734j],
#                                               [-0.04668049 + 0.03351745j, -0.04382765 - 0.06455639j,
#                                                0.05978564 - 0.01334044j, 0.50541615 + 0.50558267j,
#                                                0.01458536 + 0.01646137j, 0.01649006 + 0.01990988j],
#                                               [0.10653817 + 0.03885947j, 0.03298075 + 0.00374715j,
#                                                0.00622585 - 0.01037453j, 0.05347464 - 0.04233343j,
#                                                -0.05167177 + 0.00643947j, 0.01995970 + 0.01168872j],
#                                               [-0.03821242 + 0.00435542j, 0.07533170 + 0.14590143j,
#                                                0.01493027 + 0.02664675j, -0.01128964 - 0.02424692j,
#                                                0.03347793 + 0.0358814j, -0.03924164 - 0.01978305j]])))
#
#        x = rg_space(shape, complexity=2, zerocenter=[True, False],
#                     fft_module=fft_module, datamodel=datamodel)
#        casted_data = x.cast(data)
#        assert(check_almost_equality(x, x.calc_transform(casted_data),
#                                     np.array([[-0.02238926 - 0.06140625j, 0.06211313 - 0.03317753j,
#                                                -0.01519073 - 0.02842563j, -0.00517758 - 0.08601604j,
#                                                0.02246912 + 0.01942764j, 0.06627311 + 0.08763801j],
#                                               [-0.02492378 - 0.06097411j, 0.06365649 - 0.09346585j,
#                                                0.05031486 + 0.00858656j, -0.00881969 + 0.01842357j,
#                                                -0.01972641 - 0.00994365j, 0.05289453 - 0.06822038j],
#                                               [0.01865586 + 0.08640926j, -0.03414096 + 0.02605602j,
#                                                0.09492552 - 0.01306734j, -0.09355730 - 0.07553701j,
#                                                0.02395259 + 0.02185743j, 0.03107832 + 0.04714527j],
#                                               [0.50541615 + 0.50558267j, -0.01458536 - 0.01646137j,
#                                                0.01649006 + 0.01990988j, 0.04668049 - 0.03351745j,
#                                                -0.04382765 - 0.06455639j, -0.05978564 + 0.01334044j],
#                                               [0.05347464 - 0.04233343j, 0.05167177 - 0.00643947j,
#                                                0.01995970 + 0.01168872j, -0.10653817 - 0.03885947j,
#                                                0.03298075 + 0.00374715j, -0.00622585 + 0.01037453j],
#                                               [-0.01128964 - 0.02424692j, -0.03347793 - 0.0358814j,
#                                                -0.03924164 - 0.01978305j, 0.03821242 - 0.00435542j,
#                                                0.07533170 + 0.14590143j, -0.01493027 - 0.02664675j]])))
#
#        x = rg_space(shape, complexity=2, zerocenter=[True, False],
#                     fft_module=fft_module, datamodel=datamodel)
#        y = rg_space(shape, complexity=2, zerocenter=[False, True],
#                     distances=[1, 1], harmonic=True,
#                     fft_module=fft_module, datamodel=datamodel)
#        casted_data = x.cast(data)
#        assert(check_almost_equality(x, x.calc_transform(casted_data,
#                                                         codomain=y),
#                                     np.array([[0.04668049 - 0.03351745j, -0.04382765 - 0.06455639j,
#                                                -0.05978564 + 0.01334044j, 0.50541615 + 0.50558267j,
#                                                -0.01458536 - 0.01646137j, 0.01649006 + 0.01990988j],
#                                               [-0.10653817 - 0.03885947j, 0.03298075 + 0.00374715j,
#                                                -0.00622585 + 0.01037453j, 0.05347464 - 0.04233343j,
#                                                0.05167177 - 0.00643947j, 0.01995970 + 0.01168872j],
#                                               [0.03821242 - 0.00435542j, 0.07533170 + 0.14590143j,
#                                                -0.01493027 - 0.02664675j, -0.01128964 - 0.02424692j,
#                                                -0.03347793 - 0.0358814j, -0.03924164 - 0.01978305j],
#                                               [-0.00517758 - 0.08601604j, 0.02246912 + 0.01942764j,
#                                                0.06627311 + 0.08763801j, -0.02238926 - 0.06140625j,
#                                                0.06211313 - 0.03317753j, -0.01519073 - 0.02842563j],
#                                               [-0.00881969 + 0.01842357j, -0.01972641 - 0.00994365j,
#                                                0.05289453 - 0.06822038j, -0.02492378 - 0.06097411j,
#                                                0.06365649 - 0.09346585j, 0.05031486 + 0.00858656j],
#                                               [-0.09355730 - 0.07553701j, 0.02395259 + 0.02185743j,
#                                                0.03107832 + 0.04714527j, 0.01865586 + 0.08640926j,
#                                                -0.03414096 + 0.02605602j, 0.09492552 - 0.01306734j]])))
#
################################################################################
#
#    @parameterized.expand(DATAMODELS['rg_space'],
#                          testcase_func_name=custom_name_func)
#    def test_calc_smooth(self, datamodel):
#        sigma = 0.01
#        shape = (8, 8)
#        a = np.arange(np.prod(shape)).reshape(shape)
#        x = rg_space(shape)
#        casted_a = x.cast(a)
#        assert(check_almost_equality(x, x.calc_smooth(casted_a, sigma=sigma),
#                                     np.array([[0.3869063,   1.33370382,   2.34906384,   3.3400879,
#                                                4.34774552,   5.33876958,   6.3541296,   7.30092712],
#                                               [7.96128648,   8.90808401,   9.92344403,  10.91446809,
#                                                11.9221257,  12.91314976,  13.92850978,  14.87530731],
#                                               [16.08416664,  17.03096417,  18.04632419,  19.03734824,
#                                                20.04500586,  21.03602992,  22.05138994,  22.99818747],
#                                               [24.01235911,  24.95915664,  25.97451666,  26.96554072,
#                                                27.97319833,  28.96422239,  29.97958241,  30.92637994],
#                                               [32.07362006,  33.02041759,  34.03577761,  35.02680167,
#                                                36.03445928,  37.02548334,  38.04084336,  38.98764089],
#                                               [40.00181253,  40.94861006,  41.96397008,  42.95499414,
#                                                43.96265176,  44.95367581,  45.96903583,  46.91583336],
#                                               [48.12469269,  49.07149022,  50.08685024,  51.0778743,
#                                                52.08553191,  53.07655597,  54.09191599,  55.03871352],
#                                               [55.69907288,  56.6458704,  57.66123042,  58.65225448,
#                                                59.6599121,  60.65093616,  61.66629618,  62.6130937]])))
#
################################################################################
#
#    @parameterized.expand(DATAMODELS['rg_space'],
#                          testcase_func_name=custom_name_func)
#    def test_calc_power(self, datamodel):
#        shape = (8, 8)
#        a = np.arange(np.prod(shape)).reshape(shape)
#        x = rg_space(shape)
#        assert_almost_equal(x.calc_power(a),
#                            np.array([992.25, 55.48097039, 0., 16.25,
#                                      0., 0., 9.51902961, 0.,
#                                      0., 8.125, 0., 0.,
#                                      0., 0., 0.]))
#


print all_spaces
1355
1356
1357
1358
1359
1360
print generate_space('rg_space')

class Test_axis(unittest.TestCase):
    @parameterized.expand(
        itertools.product(point_like_spaces, [8, 16],
                          ['sum', 'prod', 'mean', 'var', 'std', 'median', 'all',
1361
1362
                           'any', 'amin', 'nanmin', 'argmin', 'amax', 'nanmax',
                           'argmax'],
csongor's avatar
csongor committed
1363
                          [None, (0,)],
1364
1365
                          DATAMODELS['point_space']),
        testcase_func_name=custom_name_func)
1366
    def test_unary_operations(self, name, num, op, axis, datamodel):
1367
        s = generate_space_with_size(name, num, datamodel=datamodel)
1368
1369
        d = generate_data(s)
        a = d.get_full_data()
1370
1371
1372
        if op in ['argmin', 'argmax'] and axis is not None:
            assert_raises(NotImplementedError, lambda: s.unary_operation
                          (d, op, axis=axis))
csongor's avatar
csongor committed
1373
1374
1375
1376
        else:
            assert_almost_equal(s.unary_operation(d, op, axis=axis),
                                getattr(np, op)(a, axis=axis), decimal=4)
            if name in ['rg_space']:
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
                if op in ['argmin', 'argmax']:
                    assert_raises(NotImplementedError, lambda: s.unary_operation
                                  (d, op, axis=(0, 1)))
                    assert_raises(NotImplementedError, lambda: s.unary_operation
                                  (d, op, axis=(1, )))
                else:
                    assert_almost_equal(s.unary_operation(d, op, axis=(0, 1)),
                                        getattr(np, op)(a, axis=(0, 1)),
                                        decimal=4)
                    assert_almost_equal(s.unary_operation(d, op, axis=(1,)),
                                        getattr(np, op)(a, axis=(1,)),
                                        decimal=4)