test_gl_space.py 2.37 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

19
import itertools
Jait Dixit's avatar
Jait Dixit committed
20
import unittest
21
22
from test.common import expand

Jait Dixit's avatar
Jait Dixit committed
23
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
24
from nifty5 import GLSpace
25
26
from numpy.testing import (assert_, assert_almost_equal, assert_equal,
                           assert_raises)
Jait Dixit's avatar
Jait Dixit committed
27

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


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


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
58
59
        g = GLSpace(2)
        assert_(isinstance(getattr(g, attribute), expected_type))
Jait Dixit's avatar
Jait Dixit committed
60
61
62
63


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

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

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