test_lm_space.py 2.56 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

import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19
import pytest
20
21
from numpy.testing import assert_, assert_allclose, assert_equal, assert_raises

Martin Reinecke's avatar
Martin Reinecke committed
22
import nifty7 as ift
23
from ..common import setup_function, teardown_function
Philipp Arras's avatar
Philipp Arras committed
24
25

pmp = pytest.mark.parametrize
Martin Reinecke's avatar
Martin Reinecke committed
26
# [lmax, expected]
Philipp Arras's avatar
Philipp Arras committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
CONSTRUCTOR_CONFIGS = [[
    5, None, {
        'lmax': 5,
        'mmax': 5,
        'shape': (36,),
        'harmonic': True,
        'size': 36,
    }
], [
    7, 4, {
        'lmax': 7,
        'mmax': 4,
        'shape': (52,),
        'harmonic': True,
        'size': 52,
    }
], [-1, 28, {
    'error': ValueError
}]]
Jait Dixit's avatar
Jait Dixit committed
46

Theo Steininger's avatar
Theo Steininger committed
47

48
def _k_length_array_helper(index_arr, lmax):
Martin Reinecke's avatar
Martin Reinecke committed
49
50
51
52
    if index_arr <= lmax:
        index_half = index_arr
    else:
        if (index_arr - lmax) % 2 == 0:
Martin Reinecke's avatar
Martin Reinecke committed
53
            index_half = (index_arr + lmax)//2
Martin Reinecke's avatar
Martin Reinecke committed
54
        else:
Martin Reinecke's avatar
Martin Reinecke committed
55
            index_half = (index_arr + lmax + 1)//2
Martin Reinecke's avatar
Martin Reinecke committed
56

Martin Reinecke's avatar
Martin Reinecke committed
57
    m = np.ceil(((2*lmax + 1) - np.sqrt((2*lmax + 1)**2 - 8 *
Philipp Arras's avatar
Philipp Arras committed
58
                                        (index_half - lmax)))/2).astype(int)
Martin Reinecke's avatar
Martin Reinecke committed
59

60
    return index_half - m*(2*lmax + 1 - m)//2
Martin Reinecke's avatar
Martin Reinecke committed
61

Theo Steininger's avatar
Theo Steininger committed
62

63
64
def get_k_length_array_configs():
    da_0 = [_k_length_array_helper(idx, 5) for idx in np.arange(36)]
Martin Reinecke's avatar
Martin Reinecke committed
65
    return [[5, da_0]]
66
67


Philipp Arras's avatar
Philipp Arras committed
68
69
@pmp('attribute', ['lmax', 'mmax', 'size'])
def test_property_ret_type(attribute):
Philipp Arras's avatar
Philipp Arras committed
70
    assert_(isinstance(getattr(ift.LMSpace(7, 5), attribute), int))
Jait Dixit's avatar
Jait Dixit committed
71
72


Philipp Arras's avatar
Philipp Arras committed
73
74
75
76
77
78
79
@pmp('lmax, mmax, expected', CONSTRUCTOR_CONFIGS)
def test_constructor(lmax, mmax, expected):
    if 'error' in expected:
        with assert_raises(expected['error']):
            ift.LMSpace(lmax, mmax)
    else:
        for key, value in expected.items():
Philipp Arras's avatar
Philipp Arras committed
80
            assert_equal(getattr(ift.LMSpace(lmax, mmax), key), value)
Philipp Arras's avatar
Philipp Arras committed
81
82
83
84


def test_dvol():
    assert_allclose(ift.LMSpace(5).dvol, 1.)
Jait Dixit's avatar
Jait Dixit committed
85
86


Philipp Arras's avatar
Philipp Arras committed
87
88
@pmp('lmax, expected', get_k_length_array_configs())
def test_k_length_array(lmax, expected):
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
89
    assert_allclose(ift.LMSpace(lmax).get_k_length_array().val, expected)