test_power_space.py 5.17 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

19
20
21
22
23
24
25
from __future__ import division

import unittest
import numpy as np

from numpy.testing import assert_, assert_equal, assert_almost_equal,\
        assert_raises
Martin Reinecke's avatar
Martin Reinecke committed
26
from nifty2go import PowerSpace, RGSpace, Space, LMSpace
27
from test.common import expand
Reimar H Leike's avatar
Reimar H Leike committed
28
from itertools import product, chain
29

30
HARMONIC_SPACES = [RGSpace((8,), harmonic=True),
Martin Reinecke's avatar
Martin Reinecke committed
31
                   RGSpace((7, 8), harmonic=True),
Martin Reinecke's avatar
Martin Reinecke committed
32
                   RGSpace((6, 6), harmonic=True),
Martin Reinecke's avatar
Martin Reinecke committed
33
34
35
36
37
38
                   RGSpace((5, 5), harmonic=True),
                   RGSpace((4, 5, 7), harmonic=True),
                   LMSpace(6),
                   LMSpace(9)]


Martin Reinecke's avatar
stage1    
Martin Reinecke committed
39
# Try all sensible kinds of combinations of spaces and binning parameters
Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
CONSISTENCY_CONFIGS_IMPLICIT = product(HARMONIC_SPACES,
                                       [None], [None, 3, 4], [True, False])
CONSISTENCY_CONFIGS_EXPLICIT = product(HARMONIC_SPACES,
Martin Reinecke's avatar
Martin Reinecke committed
43
                                       [[0., 1.3]], [None], [None])
Martin Reinecke's avatar
Martin Reinecke committed
44
45
CONSISTENCY_CONFIGS = chain(CONSISTENCY_CONFIGS_IMPLICIT,
                            CONSISTENCY_CONFIGS_EXPLICIT)
46

Martin Reinecke's avatar
stage1    
Martin Reinecke committed
47
# [harmonic_partner, logarithmic, nbin, binbounds, expected]
48
CONSTRUCTOR_CONFIGS = [
49
    [1, False, None, None, {'error': (ValueError, NotImplementedError)}],
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
50
51
    [RGSpace((8,)), False, None, None, {'error': ValueError}],
    [RGSpace((8,), harmonic=True), None, None, None, {
52
53
54
        'harmonic': True,
        'shape': (5,),
        'dim': 5,
Theo Steininger's avatar
Theo Steininger committed
55
        'harmonic_partner': RGSpace((8,), harmonic=True),
Martin Reinecke's avatar
Martin Reinecke committed
56
        'binbounds': None,
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
57
        'pindex': np.array([0, 1, 2, 3, 4, 3, 2, 1]),
58
59
60
        'kindex': np.array([0., 1., 2., 3., 4.]),
        'rho': np.array([1, 2, 2, 2, 1]),
        }],
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
61
    [RGSpace((8,), harmonic=True), True, None, None, {
62
        'harmonic': True,
63
64
        'shape': (4,),
        'dim': 4,
Theo Steininger's avatar
Theo Steininger committed
65
        'harmonic_partner': RGSpace((8,), harmonic=True),
66
67
68
69
        'binbounds': (0.5, 1.3228756555322954, 3.5),
        'pindex': np.array([0, 1, 2, 2, 3, 2, 2, 1]),
        'kindex': np.array([0., 1., 2.5, 4.]),
        'rho': np.array([1, 2, 4, 1]),
70
71
72
73
        }],
    ]


74
def get_k_length_array_configs():
Martin Reinecke's avatar
Martin Reinecke committed
75
    da_0 = np.array([0, 1.0, 1.41421356, 2., 2.23606798, 2.82842712])
76
    return [
Martin Reinecke's avatar
Martin Reinecke committed
77
        [RGSpace((4, 4), harmonic=True),  da_0],
78
79
80
81
82
        ]


class PowerSpaceInterfaceTest(unittest.TestCase):
    @expand([
Theo Steininger's avatar
Theo Steininger committed
83
        ['harmonic_partner', Space],
Martin Reinecke's avatar
Martin Reinecke committed
84
        ['binbounds', type(None)],
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
85
        ['pindex', np.ndarray],
86
87
88
89
90
91
92
93
        ['kindex', np.ndarray],
        ['rho', np.ndarray],
        ])
    def test_property_ret_type(self, attribute, expected_type):
        r = RGSpace((4, 4), harmonic=True)
        p = PowerSpace(r)
        assert_(isinstance(getattr(p, attribute), expected_type))

Martin Reinecke's avatar
Martin Reinecke committed
94

95
class PowerSpaceConsistencyCheck(unittest.TestCase):
96
    @expand(CONSISTENCY_CONFIGS)
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
97
    def test_rhopindexConsistency(self, harmonic_partner,
Martin Reinecke's avatar
Martin Reinecke committed
98
                                  binbounds, nbin, logarithmic):
99
100
        bb = PowerSpace.useful_binbounds(harmonic_partner, logarithmic, nbin)
        p = PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb)
Martin Reinecke's avatar
Martin Reinecke committed
101

Martin Reinecke's avatar
Martin Reinecke committed
102
        assert_equal(np.bincount(p.pindex.ravel()), p.rho,
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
103
104
                     err_msg='rho is not equal to pindex degeneracy')

Martin Reinecke's avatar
Martin Reinecke committed
105

106
class PowerSpaceFunctionalityTest(unittest.TestCase):
Martin Reinecke's avatar
Martin Reinecke committed
107
    @expand(CONSTRUCTOR_CONFIGS)
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
108
    def test_constructor(self, harmonic_partner,
Theo Steininger's avatar
Theo Steininger committed
109
                         logarithmic, nbin, binbounds, expected):
110
111
        if 'error' in expected:
            with assert_raises(expected['error']):
112
113
114
                bb = PowerSpace.useful_binbounds(harmonic_partner,
                                                 logarithmic, nbin)
                PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb)
115
        else:
116
117
118
            bb = PowerSpace.useful_binbounds(harmonic_partner,
                                             logarithmic, nbin)
            p = PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb)
Martin Reinecke's avatar
Martin Reinecke committed
119
            for key, value in expected.items():
120
121
122
123
124
                if isinstance(value, np.ndarray):
                    assert_almost_equal(getattr(p, key), value)
                else:
                    assert_equal(getattr(p, key), value)

125
126
    @expand(get_k_length_array_configs())
    def test_k_length_array(self, harmonic_partner, expected):
Theo Steininger's avatar
Theo Steininger committed
127
        p = PowerSpace(harmonic_partner=harmonic_partner)
128
        assert_almost_equal(p.get_k_length_array(), expected)
129

130
    def test_dvol(self):
131
        p = PowerSpace(harmonic_partner=RGSpace(10,harmonic=True))
132
        assert_almost_equal(p.dvol(),p.rho)