test_hp_space.py 2.88 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

Martin Reinecke's avatar
Martin Reinecke committed
19
from __future__ import division
Jait Dixit's avatar
Jait Dixit committed
20
21
22
23
24
import unittest
import numpy as np

from numpy.testing import assert_, assert_equal, assert_raises,\
                          assert_almost_equal
Martin Reinecke's avatar
Martin Reinecke committed
25
from nifty2go import HPSpace
Jait Dixit's avatar
Jait Dixit committed
26
27
from test.common import expand

Martin Reinecke's avatar
Martin Reinecke committed
28
# [nside, expected]
Jait Dixit's avatar
Jait Dixit committed
29
CONSTRUCTOR_CONFIGS = [
Martin Reinecke's avatar
Martin Reinecke committed
30
        [2, {
Jait Dixit's avatar
Jait Dixit committed
31
32
33
34
35
36
            'nside': 2,
            'harmonic': False,
            'shape': (48,),
            'dim': 48,
            'total_volume': 4 * np.pi,
            }],
Martin Reinecke's avatar
Martin Reinecke committed
37
        [5, {
38
39
40
41
42
            'nside': 5,
            'harmonic': False,
            'shape': (300,),
            'dim': 300,
            'total_volume': 4 * np.pi,
Jait Dixit's avatar
Jait Dixit committed
43
            }],
Martin Reinecke's avatar
Martin Reinecke committed
44
        [1, {
45
46
47
48
49
50
            'nside': 1,
            'harmonic': False,
            'shape': (12,),
            'dim': 12,
            'total_volume': 4 * np.pi,
            }],
Martin Reinecke's avatar
Martin Reinecke committed
51
        [0, {
Jait Dixit's avatar
Jait Dixit committed
52
53
54
55
56
57
            'error': ValueError
            }]
    ]


def get_weight_configs():
Martin Reinecke's avatar
Martin Reinecke committed
58
59
60
61
62
63
    np.random.seed(42)

    # for HPSpace(nside=2)
    w_0_x = np.random.rand(48)
    w_0_res = w_0_x * ((4 * np.pi) / 48)
    w_1_res = w_0_x * (((4 * np.pi) / 48)**2)
Jait Dixit's avatar
Jait Dixit committed
64
    return [
Martin Reinecke's avatar
Martin Reinecke committed
65
66
67
        [w_0_x, 1, None, False, w_0_res],
        [w_0_x.copy(), 1, None, True, w_0_res],
        [w_0_x, 2, None, False, w_1_res],
Jait Dixit's avatar
Jait Dixit committed
68
69
70
71
72
73
        ]


class HPSpaceInterfaceTests(unittest.TestCase):
    @expand([['nside', int]])
    def test_property_ret_type(self, attribute, expected_type):
74
        x = HPSpace(2)
Jait Dixit's avatar
Jait Dixit committed
75
76
77
78
79
        assert_(isinstance(getattr(x, attribute), expected_type))


class HPSpaceFunctionalityTests(unittest.TestCase):
    @expand(CONSTRUCTOR_CONFIGS)
Martin Reinecke's avatar
Martin Reinecke committed
80
    def test_constructor(self, nside, expected):
81
82
        if 'error' in expected:
            with assert_raises(expected['error']):
Martin Reinecke's avatar
Martin Reinecke committed
83
                HPSpace(nside)
Jait Dixit's avatar
Jait Dixit committed
84
        else:
Martin Reinecke's avatar
Martin Reinecke committed
85
            h = HPSpace(nside)
Martin Reinecke's avatar
Martin Reinecke committed
86
            for key, value in expected.items():
87
                assert_equal(getattr(h, key), value)
Jait Dixit's avatar
Jait Dixit committed
88
89
90

    @expand(get_weight_configs())
    def test_weight(self, x, power, axes, inplace, expected):
91
92
93
94
95
        h = HPSpace(2)
        res = h.weight(x, power, axes, inplace)
        assert_almost_equal(res, expected)
        if inplace:
            assert_(x is res)