test_lm_space.py 4.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13 14 15 16 17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18

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

22
from numpy.testing import assert_, assert_equal, assert_raises,\
Theo Steininger's avatar
Theo Steininger committed
23
        assert_almost_equal, assert_array_almost_equal
24
from d2o import distributed_data_object
Jait Dixit's avatar
Jait Dixit committed
25 26 27
from nifty import LMSpace
from test.common import expand

Martin Reinecke's avatar
Martin Reinecke committed
28
# [lmax, expected]
29
CONSTRUCTOR_CONFIGS = [
Martin Reinecke's avatar
Martin Reinecke committed
30
        [5, {
Jait Dixit's avatar
Jait Dixit committed
31 32 33 34 35 36 37
            'lmax': 5,
            'mmax': 5,
            'shape': (36,),
            'harmonic': True,
            'dim': 36,
            'total_volume': 36.0,
            }],
Martin Reinecke's avatar
Martin Reinecke committed
38
        [7, {
Jait Dixit's avatar
Jait Dixit committed
39 40 41 42 43 44 45
            'lmax': 7,
            'mmax': 7,
            'shape': (64,),
            'harmonic': True,
            'dim': 64,
            'total_volume': 64.0,
            }],
Martin Reinecke's avatar
Martin Reinecke committed
46
        [-1, {
Jait Dixit's avatar
Jait Dixit committed
47 48 49 50
            'error': ValueError
            }]
    ]

Theo Steininger's avatar
Theo Steininger committed
51

Martin Reinecke's avatar
Martin Reinecke committed
52 53 54 55 56
def _distance_array_helper(index_arr, lmax):
    if index_arr <= lmax:
        index_half = index_arr
    else:
        if (index_arr - lmax) % 2 == 0:
57
            index_half = (index_arr + lmax)/2
Martin Reinecke's avatar
Martin Reinecke committed
58
        else:
59
            index_half = (index_arr + lmax + 1)/2
Martin Reinecke's avatar
Martin Reinecke committed
60

61 62
    m = np.ceil(((2*lmax + 1) - np.sqrt((2*lmax + 1)**2 -
                 8*(index_half - lmax)))/2).astype(int)
Martin Reinecke's avatar
Martin Reinecke committed
63

64
    return index_half - m*(2*lmax + 1 - m)//2
Martin Reinecke's avatar
Martin Reinecke committed
65

Theo Steininger's avatar
Theo Steininger committed
66

67
def get_distance_array_configs():
Martin Reinecke's avatar
Martin Reinecke committed
68
    da_0 = [_distance_array_helper(idx, 5) for idx in np.arange(36)]
Martin Reinecke's avatar
Martin Reinecke committed
69
    return [[5, da_0]]
70 71 72


def get_weight_configs():
Martin Reinecke's avatar
Martin Reinecke committed
73 74
    np.random.seed(42)
    w_0_x = np.random.rand(32, 16, 6)
75
    return [
Martin Reinecke's avatar
Martin Reinecke committed
76 77
        [w_0_x, 1, None, False, w_0_x],
        [w_0_x.copy(), 1, None,  True, w_0_x]
78 79
        ]

Theo Steininger's avatar
Theo Steininger committed
80

81
def get_hermitian_configs():
Martin Reinecke's avatar
Martin Reinecke committed
82 83 84 85
    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
86
    return [
Martin Reinecke's avatar
Martin Reinecke committed
87
        [h_0_x, h_0_res_real, h_0_res_imag]
88 89
    ]

Theo Steininger's avatar
Theo Steininger committed
90

91
class LMSpaceInterfaceTests(unittest.TestCase):
Jait Dixit's avatar
Jait Dixit committed
92 93 94
    @expand([['lmax', int],
            ['mmax', int],
            ['dim', int]])
95
    def test_property_ret_type(self, attribute, expected_type):
96
        l = LMSpace(7)
97
        assert_(isinstance(getattr(l, attribute), expected_type))
Jait Dixit's avatar
Jait Dixit committed
98 99 100


class LMSpaceFunctionalityTests(unittest.TestCase):
101
    @expand(CONSTRUCTOR_CONFIGS)
Martin Reinecke's avatar
Martin Reinecke committed
102
    def test_constructor(self, lmax, expected):
103 104
        if 'error' in expected:
            with assert_raises(expected['error']):
Martin Reinecke's avatar
Martin Reinecke committed
105
                LMSpace(lmax)
Jait Dixit's avatar
Jait Dixit committed
106
        else:
Martin Reinecke's avatar
Martin Reinecke committed
107
            l = LMSpace(lmax)
108 109
            for key, value in expected.iteritems():
                assert_equal(getattr(l, key), value)
Jait Dixit's avatar
Jait Dixit committed
110

Theo Steininger's avatar
Theo Steininger committed
111
    def test_hermitianize_inverter(self):
112
        l = LMSpace(5)
Theo Steininger's avatar
Theo Steininger committed
113 114 115 116
        v = distributed_data_object(global_shape=l.shape, dtype=np.complex128)
        v[:] = np.random.random(l.shape) + 1j*np.random.random(l.shape)
        inverted = l.hermitianize_inverter(v, axes=(0,))
        assert_array_almost_equal(inverted.get_full_data(), v.get_full_data())
Jait Dixit's avatar
Jait Dixit committed
117

118 119
    @expand(get_weight_configs())
    def test_weight(self, x, power, axes, inplace, expected):
120 121 122 123 124
        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
125

126
    @expand(get_distance_array_configs())
Martin Reinecke's avatar
Martin Reinecke committed
127 128
    def test_distance_array(self, lmax, expected):
        l = LMSpace(lmax)
129
        assert_almost_equal(l.get_distance_array('not').data, expected)