test_lm_space.py 3.48 KB
Newer Older
Jait Dixit's avatar
Jait Dixit committed
1
2
3
import unittest
import numpy as np

4
5
from numpy.testing import assert_, assert_equal, assert_raises,\
        assert_almost_equal
Jait Dixit's avatar
Jait Dixit committed
6
from nose.plugins.skip import SkipTest
7
from d2o import distributed_data_object
Jait Dixit's avatar
Jait Dixit committed
8
9
10
11
12
from nifty import LMSpace
from nifty.config import dependency_injector as di
from test.common import expand

# [lmax, dtype, expected]
13
CONSTRUCTOR_CONFIGS = [
Jait Dixit's avatar
Jait Dixit committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        [5, None, {
            'lmax': 5,
            'mmax': 5,
            'shape': (36,),
            'harmonic': True,
            'dim': 36,
            'total_volume': 36.0,
            'dtype': np.dtype('float64')
            }],
        [7, np.dtype('float64'), {
            'lmax': 7,
            'mmax': 7,
            'shape': (64,),
            'harmonic': True,
            'dim': 64,
            'total_volume': 64.0,
            'dtype': np.dtype('float64')
            }],
        [-1, None, {
            'error': ValueError
            }]
    ]


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def get_distance_array_configs():
    npzfile = np.load('test/data/lm_space.npz')
    return [[5, None, npzfile['da_0']]]


def get_weight_configs():
    npzfile = np.load('test/data/lm_space.npz')
    return [
        [npzfile['w_0_x'], 1, None, False, npzfile['w_0_res']],
        [npzfile['w_0_x'], 1, None, True, npzfile['w_0_res']]
        ]


def get_hermitian_configs():
    npzfile = np.load('test/data/lm_space.npz')
    return [
        [npzfile['h_0_x'], npzfile['h_0_res_real'], npzfile['h_0_res_imag']]
    ]


Jait Dixit's avatar
Jait Dixit committed
58
59
60
61
class LMSpaceIntefaceTests(unittest.TestCase):
    @expand([['lmax', int],
            ['mmax', int],
            ['dim', int]])
62
    def test_property_ret_type(self, attribute, expected_type):
Jait Dixit's avatar
Jait Dixit committed
63
        try:
64
            l = LMSpace(7)
Jait Dixit's avatar
Jait Dixit committed
65
66
        except ImportError:
            raise SkipTest
67
        assert_(isinstance(getattr(l, attribute), expected_type))
Jait Dixit's avatar
Jait Dixit committed
68
69
70


class LMSpaceFunctionalityTests(unittest.TestCase):
71
    @expand(CONSTRUCTOR_CONFIGS)
Jait Dixit's avatar
Jait Dixit committed
72
73
74
75
76
77
78
79
80
81
82
83
    def test_constructor(self, lmax, dtype, expected):
        if 'libsharp_wrapper_gl' not in di or 'healpy' not in di:
            raise SkipTest
        else:
            if 'error' in expected:
                with assert_raises(expected['error']):
                    LMSpace(lmax, dtype)
            else:
                l = LMSpace(lmax, dtype)
                for key, value in expected.iteritems():
                    assert_equal(getattr(l, key), value)

84
85
86
87
88
89
90
91
92
93
94
95
    @expand(get_hermitian_configs())
    def test_hermitian_decomposition(self, x, real, imag):
        if 'libsharp_wrapper_gl' not in di or 'healpy' not in di:
            raise SkipTest
        else:
            l = LMSpace(5)
            assert_almost_equal(
                l.hermitian_decomposition(distributed_data_object(x))[0],
                real)
            assert_almost_equal(
                l.hermitian_decomposition(distributed_data_object(x))[1],
                imag)
Jait Dixit's avatar
Jait Dixit committed
96

97
98
99
100
101
102
103
104
105
106
    @expand(get_weight_configs())
    def test_weight(self, x, power, axes, inplace, expected):
        if 'libsharp_wrapper_gl' not in di or 'healpy' not in di:
            raise SkipTest
        else:
            l = LMSpace(5)
            res = l.weight(x, power, axes, inplace)
            assert_almost_equal(res, expected)
            if inplace:
                assert_(x is res)
Jait Dixit's avatar
Jait Dixit committed
107

108
109
110
111
112
113
114
    @expand(get_distance_array_configs())
    def test_distance_array(self, lmax, dtype, expected):
        if 'libsharp_wrapper_gl' not in di or 'healpy' not in di:
            raise SkipTest
        else:
            l = LMSpace(lmax, dtype)
            assert_almost_equal(l.get_distance_array('not').data, expected)