test_rg_space.py 3.43 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
from numpy.testing import assert_allclose, assert_equal
21

Martin Reinecke's avatar
Martin Reinecke committed
22
import nifty7 as ift
23

24
from ..common import setup_function, teardown_function
Jait Dixit's avatar
Jait Dixit committed
25

Philipp Arras's avatar
Philipp Arras committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pmp = pytest.mark.parametrize
# [shape, distances, harmonic, expected]
CONSTRUCTOR_CONFIGS = [[(8,), None, False, {
    'shape': (8,),
    'distances': (0.125,),
    'harmonic': False,
    'size': 8,
}], [(8,), None, True, {
    'shape': (8,),
    'distances': (1.0,),
    'harmonic': True,
    'size': 8,
}], [(8,), (12,), True, {
    'shape': (8,),
    'distances': (12.0,),
    'harmonic': True,
    'size': 8,
}], [(11, 11), None, False, {
    'shape': (11, 11),
    'distances': (1/11, 1/11),
    'harmonic': False,
    'size': 121,
}], [(12, 12), (1.3, 1.3), True, {
    'shape': (12, 12),
    'distances': (1.3, 1.3),
    'harmonic': True,
    'size': 144,
}]]
Jait Dixit's avatar
Jait Dixit committed
54
55


56
def get_k_length_array_configs():
Martin Reinecke's avatar
Martin Reinecke committed
57
    # for RGSpace(shape=(4, 4), distances=(0.25,0.25))
Martin Reinecke's avatar
Martin Reinecke committed
58
    cords_0 = np.ogrid[0:4, 0:4]
Philipp Arras's avatar
Philipp Arras committed
59
    da_0 = ((cords_0[0] - 4//2)*0.25)**2
Martin Reinecke's avatar
Martin Reinecke committed
60
    da_0 = np.fft.ifftshift(da_0)
Philipp Arras's avatar
Philipp Arras committed
61
    temp = ((cords_0[1] - 4//2)*0.25)**2
Martin Reinecke's avatar
Martin Reinecke committed
62
63
64
    temp = np.fft.ifftshift(temp)
    da_0 = da_0 + temp
    da_0 = np.sqrt(da_0)
65
    return [
Martin Reinecke's avatar
Martin Reinecke committed
66
        [(4, 4), (0.25, 0.25), da_0],
Philipp Arras's avatar
Philipp Arras committed
67
    ]
68
69


70
def get_dvol_configs():
Philipp Arras's avatar
Philipp Arras committed
71
72
73
74
75
76
77
78
79
80
    return [[(11, 11), None, False, 1], [(11, 11), None, False, 1],
            [(11, 11), (1.3, 1.3), False, 1], [(11, 11), (1.3, 1.3), False,
                                               1], [(11, 11), None, True, 1],
            [(11, 11), None, True, 1], [(11, 11), (1.3, 1.3), True,
                                        1], [(11, 11), (1.3, 1.3), True, 1]]


@pmp('attribute, expected_type', [['distances', tuple]])
def test_property_ret_type(attribute, expected_type):
    x = ift.RGSpace(1)
81
    ift.myassert(isinstance(getattr(x, attribute), expected_type))
82
83


Philipp Arras's avatar
Philipp Arras committed
84
85
86
87
88
@pmp('shape, distances, harmonic, expected', CONSTRUCTOR_CONFIGS)
def test_constructor(shape, distances, harmonic, expected):
    x = ift.RGSpace(shape, distances, harmonic)
    for key, value in expected.items():
        assert_equal(getattr(x, key), value)
Jait Dixit's avatar
Jait Dixit committed
89
90


Philipp Arras's avatar
Philipp Arras committed
91
92
93
@pmp('shape, distances, expected', get_k_length_array_configs())
def test_k_length_array(shape, distances, expected):
    r = ift.RGSpace(shape=shape, distances=distances, harmonic=True)
Martin Reinecke's avatar
stage 3    
Martin Reinecke committed
94
    assert_allclose(r.get_k_length_array().val, expected)
Jait Dixit's avatar
Jait Dixit committed
95
96


Philipp Arras's avatar
Philipp Arras committed
97
98
99
100
@pmp('shape, distances, harmonic, power', get_dvol_configs())
def test_dvol(shape, distances, harmonic, power):
    r = ift.RGSpace(shape=shape, distances=distances, harmonic=harmonic)
    assert_allclose(r.dvol, np.prod(r.distances)**power)
101
102
103
104
105
106


def test_codomain():
    for i in range(1, 1000):
        r = ift.RGSpace(shape=(i,), distances=(1.,), harmonic=False)
        assert_equal(r.get_default_codomain().get_default_codomain(), r)