test_lm_space.py 4.74 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

Jait Dixit's avatar
Jait Dixit committed
19
20
21
import unittest
import numpy as np

22
23
from numpy.testing import assert_, assert_equal, assert_raises,\
        assert_almost_equal
Jait Dixit's avatar
Jait Dixit committed
24
from nose.plugins.skip import SkipTest
25
from d2o import distributed_data_object
Jait Dixit's avatar
Jait Dixit committed
26
27
28
29
30
from nifty import LMSpace
from nifty.config import dependency_injector as di
from test.common import expand

# [lmax, dtype, expected]
31
CONSTRUCTOR_CONFIGS = [
Jait Dixit's avatar
Jait Dixit committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        [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
            }]
    ]


Martin Reinecke's avatar
Martin Reinecke committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def _distance_array_helper(index_arr, lmax):
    if index_arr <= lmax:
        index_half = index_arr
    else:
        if (index_arr - lmax) % 2 == 0:
            index_half = (index_arr + lmax) / 2
        else:
            index_half = (index_arr + lmax + 1) / 2

    m = (
            np.ceil(((2 * lmax + 1) - np.sqrt((2 * lmax + 1)**2 -
                    8 * (index_half - lmax))) / 2)
        ).astype(int)

    return index_half - m * (2 * lmax + 1 - m) // 2


73
def get_distance_array_configs():
Martin Reinecke's avatar
Martin Reinecke committed
74
75
    da_0 = [_distance_array_helper(idx, 5) for idx in np.arange(36)]
    return [[5, None, da_0]]
76
77
78


def get_weight_configs():
Martin Reinecke's avatar
Martin Reinecke committed
79
80
    np.random.seed(42)
    w_0_x = np.random.rand(32, 16, 6)
81
    return [
Martin Reinecke's avatar
Martin Reinecke committed
82
83
        [w_0_x, 1, None, False, w_0_x],
        [w_0_x.copy(), 1, None,  True, w_0_x]
84
85
86
87
        ]


def get_hermitian_configs():
Martin Reinecke's avatar
Martin Reinecke committed
88
89
90
91
    np.random.seed(42)
    h_0_res_real = np.random.rand(32, 16, 6).astype(np.complex128)
    h_0_res_imag = np.random.rand(32, 16, 6).astype(np.complex128)
    h_0_x = h_0_res_real + h_0_res_imag * 1j
92
    return [
Martin Reinecke's avatar
Martin Reinecke committed
93
        [h_0_x, h_0_res_real, h_0_res_imag]
94
95
96
    ]


Jait Dixit's avatar
Jait Dixit committed
97
98
99
100
class LMSpaceIntefaceTests(unittest.TestCase):
    @expand([['lmax', int],
            ['mmax', int],
            ['dim', int]])
101
    def test_property_ret_type(self, attribute, expected_type):
Jait Dixit's avatar
Jait Dixit committed
102
        try:
103
            l = LMSpace(7)
Jait Dixit's avatar
Jait Dixit committed
104
105
        except ImportError:
            raise SkipTest
106
        assert_(isinstance(getattr(l, attribute), expected_type))
Jait Dixit's avatar
Jait Dixit committed
107
108
109


class LMSpaceFunctionalityTests(unittest.TestCase):
110
    @expand(CONSTRUCTOR_CONFIGS)
Jait Dixit's avatar
Jait Dixit committed
111
112
113
114
115
116
117
118
119
120
121
122
    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)

123
124
125
126
127
128
129
130
131
132
133
134
    @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
135

136
137
138
139
140
141
142
143
144
145
    @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
146

147
148
149
150
151
152
153
    @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)