test_gl_space.py 2.08 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
# Copyright(C) 2013-2019 Max-Planck-Society
Theo Steininger's avatar
Theo Steininger committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17

18
19
import itertools

Jait Dixit's avatar
Jait Dixit committed
20
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
21
import pytest
22
23
from numpy.testing import (assert_, assert_almost_equal, assert_equal,
                           assert_raises)
Jait Dixit's avatar
Jait Dixit committed
24

Philipp Arras's avatar
Philipp Arras committed
25
26
27
28
from nifty5 import GLSpace

pmp = pytest.mark.parametrize

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


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

Jait Dixit's avatar
Jait Dixit committed
55

Philipp Arras's avatar
Philipp Arras committed
56
57
58
59
@pmp('attribute', ['nlat', 'nlon'])
def test_property_ret_type(attribute):
    g = GLSpace(2)
    assert_(isinstance(getattr(g, attribute), int))
Jait Dixit's avatar
Jait Dixit committed
60
61


Philipp Arras's avatar
Philipp Arras committed
62
63
64
@pmp('nlat, nlon, expected', CONSTRUCTOR_CONFIGS)
def test_constructor(nlat, nlon, expected):
    g = GLSpace(4)
Jait Dixit's avatar
Jait Dixit committed
65

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

Jait Dixit's avatar
Jait Dixit committed
74

Philipp Arras's avatar
Philipp Arras committed
75
76
77
@pmp('power, expected', get_dvol_configs())
def test_dvol(power, expected):
    assert_almost_equal(GLSpace(2).dvol, expected)