test_gl_space.py 3.34 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
import unittest
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
21
import itertools
Jait Dixit's avatar
Jait Dixit committed
22
23
24
25
26
27
28
29

from numpy.testing import assert_, assert_equal, assert_raises,\
        assert_almost_equal
from nose.plugins.skip import SkipTest
from nifty import GLSpace
from nifty.config import dependency_injector as di
from test.common import expand

Martin Reinecke's avatar
Martin Reinecke committed
30
# [nlat, nlon, expected]
Jait Dixit's avatar
Jait Dixit committed
31
CONSTRUCTOR_CONFIGS = [
Martin Reinecke's avatar
Martin Reinecke committed
32
        [2, None, {
Jait Dixit's avatar
Jait Dixit committed
33
34
35
36
37
            'nlat': 2,
            'nlon': 3,
            'harmonic': False,
            'shape': (6,),
            'dim': 6,
Martin Reinecke's avatar
Martin Reinecke committed
38
            'total_volume': 4 * np.pi
Jait Dixit's avatar
Jait Dixit committed
39
            }],
Martin Reinecke's avatar
Martin Reinecke committed
40
        [0, None, {
Jait Dixit's avatar
Jait Dixit committed
41
42
43
44
45
46
            'error': ValueError
            }]
    ]


def get_weight_configs():
Martin Reinecke's avatar
Martin Reinecke committed
47
    np.random.seed(42)
Theo Steininger's avatar
Theo Steininger committed
48
    wgt = [2.0943951,  2.0943951]
Martin Reinecke's avatar
Martin Reinecke committed
49
50
51
52
53
54
55
56
57
58
59
    # for GLSpace(nlat=2, nlon=3)
    weight_0 = np.array(list(itertools.chain.from_iterable(
        itertools.repeat(x, 3) for x in wgt)))
    w_0_x = np.random.rand(6)
    w_0_res = w_0_x * weight_0

    weight_1 = np.array(list(itertools.chain.from_iterable(
        itertools.repeat(x, 3) for x in wgt)))
    weight_1 = weight_1.reshape([1, 1, 6])
    w_1_x = np.random.rand(32, 16, 6)
    w_1_res = w_1_x * weight_1
Jait Dixit's avatar
Jait Dixit committed
60
    return [
Martin Reinecke's avatar
Martin Reinecke committed
61
62
63
        [w_0_x, 1, None, False, w_0_res],
        [w_0_x.copy(), 1, None, True, w_0_res],
        [w_1_x.copy(), 1, (2,), True, w_1_res],
Jait Dixit's avatar
Jait Dixit committed
64
65
66
67
68
69
70
        ]


class GLSpaceInterfaceTests(unittest.TestCase):
    @expand([['nlat', int],
            ['nlon', int]])
    def test_property_ret_type(self, attribute, expected_type):
Theo Steininger's avatar
Theo Steininger committed
71
72
73
74
75
76
        try:
            g = GLSpace(2)
        except ImportError:
            raise SkipTest
        else:
            assert_(isinstance(getattr(g, attribute), expected_type))
Jait Dixit's avatar
Jait Dixit committed
77
78
79
80


class GLSpaceFunctionalityTests(unittest.TestCase):
    @expand(CONSTRUCTOR_CONFIGS)
Martin Reinecke's avatar
Martin Reinecke committed
81
    def test_constructor(self, nlat, nlon, expected):
Theo Steininger's avatar
Theo Steininger committed
82
83
84
85
86
        try:
            g = GLSpace(4)
        except ImportError:
            raise SkipTest

87
88
        if 'error' in expected:
            with assert_raises(expected['error']):
Martin Reinecke's avatar
Martin Reinecke committed
89
                GLSpace(nlat, nlon)
Jait Dixit's avatar
Jait Dixit committed
90
        else:
Martin Reinecke's avatar
Martin Reinecke committed
91
            g = GLSpace(nlat, nlon)
92
93
            for key, value in expected.iteritems():
                assert_equal(getattr(g, key), value)
Jait Dixit's avatar
Jait Dixit committed
94
95
96

    @expand(get_weight_configs())
    def test_weight(self, x, power, axes, inplace, expected):
Theo Steininger's avatar
Theo Steininger committed
97
98
99
100
101
        try:
            g = GLSpace(4)
        except ImportError:
            raise SkipTest

102
        if 'pyHealpix' not in di:
Jait Dixit's avatar
Jait Dixit committed
103
104
105
106
107
108
109
            raise SkipTest
        else:
            g = GLSpace(2)
            res = g.weight(x, power, axes, inplace)
            assert_almost_equal(res, expected)
            if inplace:
                assert_(x is res)