test_power_space.py 5.42 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
20
21
from __future__ import division
import unittest
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
22
from numpy.testing import assert_, assert_equal, assert_allclose,\
23
        assert_raises
Philipp Arras's avatar
Philipp Arras committed
24
import nifty5 as ift
25
from test.common import expand
Reimar H Leike's avatar
Reimar H Leike committed
26
from itertools import product, chain
27

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


Martin Reinecke's avatar
stage1    
Martin Reinecke committed
37
# Try all sensible kinds of combinations of spaces and binning parameters
Martin Reinecke's avatar
Martin Reinecke committed
38
39
40
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
41
                                       [[0., 1.3]], [None], [None])
Martin Reinecke's avatar
Martin Reinecke committed
42
43
CONSISTENCY_CONFIGS = chain(CONSISTENCY_CONFIGS_IMPLICIT,
                            CONSISTENCY_CONFIGS_EXPLICIT)
44

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


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


class PowerSpaceInterfaceTest(unittest.TestCase):
    @expand([
Martin Reinecke's avatar
Martin Reinecke committed
81
        ['harmonic_partner', ift.StructuredDomain],
Martin Reinecke's avatar
Martin Reinecke committed
82
        ['binbounds', type(None)],
Martin Reinecke's avatar
Martin Reinecke committed
83
        ['pindex', ift.dobj.data_object],
Martin Reinecke's avatar
Martin Reinecke committed
84
        ['k_lengths', np.ndarray],
85
86
        ])
    def test_property_ret_type(self, attribute, expected_type):
Martin Reinecke's avatar
Martin Reinecke committed
87
88
        r = ift.RGSpace((4, 4), harmonic=True)
        p = ift.PowerSpace(r)
89
90
        assert_(isinstance(getattr(p, attribute), expected_type))

Martin Reinecke's avatar
Martin Reinecke committed
91

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

Martin Reinecke's avatar
Martin Reinecke committed
100
        assert_equal(np.bincount(ift.dobj.to_global_data(p.pindex).ravel()),
Martin Reinecke's avatar
Martin Reinecke committed
101
                     p.dvol, err_msg='rho is not equal to pindex degeneracy')
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
102

Martin Reinecke's avatar
Martin Reinecke committed
103

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

123
124
    @expand(k_lengths_configs())
    def test_k_lengths(self, harmonic_partner, expected):
Martin Reinecke's avatar
Martin Reinecke committed
125
126
        p = ift.PowerSpace(harmonic_partner=harmonic_partner)
        assert_allclose(p.k_lengths, expected)
127

128
    def test_dvol(self):
Martin Reinecke's avatar
Martin Reinecke committed
129
130
        hp = ift.RGSpace(10, harmonic=True)
        p = ift.PowerSpace(harmonic_partner=hp)
Martin Reinecke's avatar
Martin Reinecke committed
131
        v1 = hp.dvol
Martin Reinecke's avatar
Martin Reinecke committed
132
        v1 = hp.size*v1 if np.isscalar(v1) else np.sum(v1)
Martin Reinecke's avatar
Martin Reinecke committed
133
        v2 = p.dvol
Martin Reinecke's avatar
Martin Reinecke committed
134
        v2 = p.size*v2 if np.isscalar(v2) else np.sum(v2)
Martin Reinecke's avatar
Martin Reinecke committed
135
        assert_allclose(v1, v2)