test_power_space.py 5.1 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
20

from itertools import chain, product

import numpy as np
Philipp Arras's avatar
Philipp Arras committed
21
import pytest
22
from numpy.testing import assert_allclose, assert_equal, assert_raises
23

Martin Reinecke's avatar
Martin Reinecke committed
24
import nifty7 as ift
25

26
from ..common import setup_function, teardown_function
Philipp Arras's avatar
Philipp Arras committed
27

Philipp Arras's avatar
Philipp Arras committed
28
pmp = pytest.mark.parametrize
Martin Reinecke's avatar
Martin Reinecke committed
29

Philipp Arras's avatar
Philipp Arras committed
30
31
32
33
34
35
36
37
38
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
39

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

Martin Reinecke's avatar
stage1    
Martin Reinecke committed
48
# [harmonic_partner, logarithmic, nbin, binbounds, expected]
49
CONSTRUCTOR_CONFIGS = [
Philipp Arras's avatar
Philipp Arras committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    [1, False, None, None, {
        'error': (ValueError, NotImplementedError)
    }],
    [ift.RGSpace((8,)), False, None, None, {
        'error': ValueError
    }],
    [
        ift.RGSpace((8,), harmonic=True), None, None, None, {
            'harmonic':
            False,
            'shape': (5,),
            'size':
            5,
            'harmonic_partner':
            ift.RGSpace((8,), harmonic=True),
            'binbounds':
            None,
            'pindex':
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
68
            np.array([0, 1, 2, 3, 4, 3, 2, 1]),
Philipp Arras's avatar
Philipp Arras committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            'k_lengths':
            np.array([0., 1., 2., 3., 4.]),
        }
    ],
    [
        ift.RGSpace((8,), harmonic=True), True, None, None, {
            'harmonic':
            False,
            'shape': (4,),
            'size':
            4,
            'harmonic_partner':
            ift.RGSpace((8,), harmonic=True),
            'binbounds': (0.5, 1.3228756555322954, 3.5),
            'pindex':
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
84
            np.array([0, 1, 2, 2, 3, 2, 2, 1]),
Philipp Arras's avatar
Philipp Arras committed
85
86
87
88
89
            'k_lengths':
            np.array([0., 1., 2.5, 4.]),
        }
    ],
]
90
91


92
def k_lengths_configs():
Martin Reinecke's avatar
Martin Reinecke committed
93
    da_0 = np.array([0, 1.0, 1.41421356, 2., 2.23606798, 2.82842712])
94
    return [
Philipp Arras's avatar
Philipp Arras committed
95
96
97
98
99
100
101
        [ift.RGSpace((4, 4), harmonic=True), da_0],
    ]


@pmp('attribute, expected_type', [
    ['harmonic_partner', ift.StructuredDomain],
    ['binbounds', type(None)],
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
102
    ['pindex', np.ndarray],
Philipp Arras's avatar
Philipp Arras committed
103
104
105
106
107
    ['k_lengths', np.ndarray],
])
def test_property_ret_type(attribute, expected_type):
    r = ift.RGSpace((4, 4), harmonic=True)
    p = ift.PowerSpace(r)
108
    ift.myassert(isinstance(getattr(p, attribute), expected_type))
Philipp Arras's avatar
Philipp Arras committed
109
110
111
112
113
114
115
116


@pmp('harmonic_partner, binbounds, nbin, logarithmic', CONSISTENCY_CONFIGS)
def test_rhopindexConsistency(harmonic_partner, binbounds, nbin, logarithmic):
    bb = ift.PowerSpace.useful_binbounds(harmonic_partner, logarithmic, nbin)
    p = ift.PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb)

    assert_equal(
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
117
        np.bincount(p.pindex.ravel()),
Philipp Arras's avatar
Philipp Arras committed
118
119
120
121
122
123
124
125
126
127
128
129
130
        p.dvol,
        err_msg='rho is not equal to pindex degeneracy')


@pmp('harmonic_partner, logarithmic, nbin, binbounds, expected',
     CONSTRUCTOR_CONFIGS)
def test_constructor(harmonic_partner, logarithmic, nbin, binbounds, expected):
    if 'error' in expected:
        with assert_raises(expected['error']):
            bb = ift.PowerSpace.useful_binbounds(harmonic_partner, logarithmic,
                                                 nbin)
            ift.PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb)
    else:
Martin Reinecke's avatar
Martin Reinecke committed
131
132
133
        bb = ift.PowerSpace.useful_binbounds(harmonic_partner, logarithmic,
                                             nbin)
        p = ift.PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb)
Philipp Arras's avatar
Philipp Arras committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
        for key, value in expected.items():
            if isinstance(value, np.ndarray):
                assert_allclose(getattr(p, key), value)
            else:
                assert_equal(getattr(p, key), value)


@pmp('harmonic_partner, expected', k_lengths_configs())
def test_k_lengths(harmonic_partner, expected):
    p = ift.PowerSpace(harmonic_partner=harmonic_partner)
    assert_allclose(p.k_lengths, expected)


def test_dvol():
    hp = ift.RGSpace(10, harmonic=True)
    p = ift.PowerSpace(harmonic_partner=hp)
    v1 = hp.dvol
    v1 = hp.size*v1 if np.isscalar(v1) else np.sum(v1)
    v2 = p.dvol
    v2 = p.size*v2 if np.isscalar(v2) else np.sum(v2)
    assert_allclose(v1, v2)