test_gl_space.py 2.35 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
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
16
17
#
# 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
from numpy.testing import assert_, assert_equal, assert_raises,\
        assert_almost_equal
Martin Reinecke's avatar
Martin Reinecke committed
24
from nifty4 import GLSpace
Jait Dixit's avatar
Jait Dixit committed
25
26
from test.common import expand

Martin Reinecke's avatar
Martin Reinecke committed
27
# [nlat, nlon, expected]
Jait Dixit's avatar
Jait Dixit committed
28
CONSTRUCTOR_CONFIGS = [
Martin Reinecke's avatar
Martin Reinecke committed
29
        [2, None, {
Jait Dixit's avatar
Jait Dixit committed
30
31
32
33
            'nlat': 2,
            'nlon': 3,
            'harmonic': False,
            'shape': (6,),
Martin Reinecke's avatar
Martin Reinecke committed
34
            'size': 6,
Jait Dixit's avatar
Jait Dixit committed
35
            }],
Martin Reinecke's avatar
Martin Reinecke committed
36
        [0, None, {
Jait Dixit's avatar
Jait Dixit committed
37
38
39
40
41
            'error': ValueError
            }]
    ]


42
def get_dvol_configs():
Martin Reinecke's avatar
Martin Reinecke committed
43
    np.random.seed(42)
Theo Steininger's avatar
Theo Steininger committed
44
    wgt = [2.0943951,  2.0943951]
Martin Reinecke's avatar
Martin Reinecke committed
45
    # for GLSpace(nlat=2, nlon=3)
46
    dvol_0 = np.array(list(itertools.chain.from_iterable(
Martin Reinecke's avatar
Martin Reinecke committed
47
        itertools.repeat(x, 3) for x in wgt)))
Jait Dixit's avatar
Jait Dixit committed
48
    return [
49
        [1, dvol_0],
Jait Dixit's avatar
Jait Dixit committed
50
51
52
53
54
55
56
        ]


class GLSpaceInterfaceTests(unittest.TestCase):
    @expand([['nlat', int],
            ['nlon', int]])
    def test_property_ret_type(self, attribute, expected_type):
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
57
58
        g = GLSpace(2)
        assert_(isinstance(getattr(g, attribute), expected_type))
Jait Dixit's avatar
Jait Dixit committed
59
60
61
62


class GLSpaceFunctionalityTests(unittest.TestCase):
    @expand(CONSTRUCTOR_CONFIGS)
Martin Reinecke's avatar
Martin Reinecke committed
63
    def test_constructor(self, nlat, nlon, expected):
Martin Reinecke's avatar
cleanup  
Martin Reinecke committed
64
        g = GLSpace(4)
Theo Steininger's avatar
Theo Steininger committed
65

66
67
        if 'error' in expected:
            with assert_raises(expected['error']):
Martin Reinecke's avatar
Martin Reinecke committed
68
                GLSpace(nlat, nlon)
Jait Dixit's avatar
Jait Dixit committed
69
        else:
Martin Reinecke's avatar
Martin Reinecke committed
70
            g = GLSpace(nlat, nlon)
Martin Reinecke's avatar
Martin Reinecke committed
71
            for key, value in expected.items():
72
                assert_equal(getattr(g, key), value)
Jait Dixit's avatar
Jait Dixit committed
73

74
75
    @expand(get_dvol_configs())
    def test_dvol(self, power, expected):
Martin Reinecke's avatar
Martin Reinecke committed
76
        assert_almost_equal(GLSpace(2).dvol, expected)