test_rg_space.py 7.39 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

Jait Dixit's avatar
Jait Dixit committed
19 20 21 22 23
from __future__ import division

import unittest
import numpy as np

Theo Steininger's avatar
Theo Steininger committed
24 25
from d2o import distributed_data_object

Theo Steininger's avatar
Theo Steininger committed
26 27 28
from numpy.testing import assert_, assert_equal, assert_almost_equal, \
                          assert_array_equal
from nifty import RGSpace, nifty_configuration
Jait Dixit's avatar
Jait Dixit committed
29
from test.common import expand
Martin Reinecke's avatar
Martin Reinecke committed
30
from itertools import product
Theo Steininger's avatar
Theo Steininger committed
31
from nose.plugins.skip import SkipTest
Jait Dixit's avatar
Jait Dixit committed
32

Martin Reinecke's avatar
Martin Reinecke committed
33
# [shape, zerocenter, distances, harmonic, expected]
34
CONSTRUCTOR_CONFIGS = [
Martin Reinecke's avatar
Martin Reinecke committed
35
        [(8,), False, None, False,
Jait Dixit's avatar
Jait Dixit committed
36 37 38 39 40 41 42 43
            {
                'shape': (8,),
                'zerocenter': (False,),
                'distances': (0.125,),
                'harmonic': False,
                'dim': 8,
                'total_volume': 1.0
            }],
Martin Reinecke's avatar
Martin Reinecke committed
44
        [(8,), True, None, False,
Jait Dixit's avatar
Jait Dixit committed
45 46 47 48 49 50 51 52
            {
                'shape': (8,),
                'zerocenter': (True,),
                'distances': (0.125,),
                'harmonic': False,
                'dim': 8,
                'total_volume': 1.0
            }],
Martin Reinecke's avatar
Martin Reinecke committed
53
        [(8,), False, None, True,
Jait Dixit's avatar
Jait Dixit committed
54 55 56 57 58 59 60 61
            {
                'shape': (8,),
                'zerocenter': (False,),
                'distances': (1.0,),
                'harmonic': True,
                'dim': 8,
                'total_volume': 8.0
            }],
Martin Reinecke's avatar
Martin Reinecke committed
62
        [(8,), False, (12,), True,
Jait Dixit's avatar
Jait Dixit committed
63 64 65 66 67 68 69 70
            {
                'shape': (8,),
                'zerocenter': (False,),
                'distances': (12.0,),
                'harmonic': True,
                'dim': 8,
                'total_volume': 96.0
            }],
Theo Steininger's avatar
Theo Steininger committed
71
        [(11, 11), False, None, False,
Jait Dixit's avatar
Jait Dixit committed
72 73
            {
                'shape': (11, 11),
Theo Steininger's avatar
Theo Steininger committed
74
                'zerocenter': (False, False),
Jait Dixit's avatar
Jait Dixit committed
75 76 77 78 79
                'distances': (1/11, 1/11),
                'harmonic': False,
                'dim': 121,
                'total_volume': 1.0
            }],
Theo Steininger's avatar
Theo Steininger committed
80
        [(12, 12), True, (1.3, 1.3), True,
Jait Dixit's avatar
Jait Dixit committed
81
            {
Theo Steininger's avatar
Theo Steininger committed
82
                'shape': (12, 12),
Jait Dixit's avatar
Jait Dixit committed
83 84 85
                'zerocenter': (True, True),
                'distances': (1.3, 1.3),
                'harmonic': True,
Theo Steininger's avatar
Theo Steininger committed
86 87
                'dim': 144,
                'total_volume': 243.36
Jait Dixit's avatar
Jait Dixit committed
88 89 90 91 92
            }]

    ]


93
def get_distance_array_configs():
Martin Reinecke's avatar
Martin Reinecke committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    # for RGSpace(shape=(4, 4), distances=None, zerocenter=[False, False])
    cords_0 = np.ogrid[0:4, 0:4]
    da_0 = ((cords_0[0] - 4 // 2) * 0.25)**2
    da_0 = np.fft.ifftshift(da_0)
    temp = ((cords_0[1] - 4 // 2) * 0.25)**2
    temp = np.fft.ifftshift(temp)
    da_0 = da_0 + temp
    da_0 = np.sqrt(da_0)
    # for RGSpace(shape=(4, 4), distances=None, zerocenter=[True, True])
    da_1 = ((cords_0[0] - 4 // 2) * 0.25)**2
    temp = ((cords_0[1] - 4 // 2) * 0.25)**2
    da_1 = da_1 + temp
    da_1 = np.sqrt(da_1)
    # for RGSpace(shape=(4, 4), distances=(12, 12), zerocenter=[True, True])
    da_2 = ((cords_0[0] - 4 // 2) * 12)**2
    temp = ((cords_0[1] - 4 // 2) * 12)**2
    da_2 = da_2 + temp
    da_2 = np.sqrt(da_2)
112
    return [
Martin Reinecke's avatar
Martin Reinecke committed
113 114 115
        [(4, 4),  None, [False, False], da_0],
        [(4, 4),  None, [True, True], da_1],
        [(4, 4),  (12, 12), [True, True], da_2]
116 117 118 119
        ]


def get_weight_configs():
Martin Reinecke's avatar
Martin Reinecke committed
120 121 122 123 124 125 126 127 128 129 130
    np.random.seed(42)
    # power 1
    w_0_x = np.random.rand(32, 12, 6)
    # for RGSpace(shape=(11,11), distances=None, harmonic=False)
    w_0_res = w_0_x * (1/11 * 1/11)
    # for RGSpace(shape=(11, 11), distances=(1.3,1.3), harmonic=False)
    w_1_res = w_0_x * (1.3 * 1.3)
    # for RGSpace(shape=(11,11), distances=None, harmonic=True)
    w_2_res = w_0_x * (1.0 * 1.0)
    # for RGSpace(shape=(11,11), distances=(1.3, 1,3), harmonic=True)
    w_3_res = w_0_x * (1.3 * 1.3)
131
    return [
Martin Reinecke's avatar
Martin Reinecke committed
132 133 134 135 136 137 138 139
        [(11, 11), None, False, w_0_x, 1, None, False, w_0_res],
        [(11, 11), None, False, w_0_x.copy(), 1, None,  True, w_0_res],
        [(11, 11), (1.3, 1.3), False, w_0_x, 1, None, False, w_1_res],
        [(11, 11), (1.3, 1.3), False, w_0_x.copy(), 1, None,  True, w_1_res],
        [(11, 11), None, True, w_0_x, 1, None, False, w_2_res],
        [(11, 11), None, True, w_0_x.copy(), 1, None,  True, w_2_res],
        [(11, 11), (1.3, 1.3), True, w_0_x, 1, None, False, w_3_res],
        [(11, 11), (1.3, 1.3), True, w_0_x.copy(), 1, None,  True, w_3_res]
140 141 142
        ]


Jait Dixit's avatar
Jait Dixit committed
143 144 145
class RGSpaceInterfaceTests(unittest.TestCase):
    @expand([['distances', tuple],
            ['zerocenter', tuple]])
146
    def test_property_ret_type(self, attribute, expected_type):
147
        x = RGSpace(1)
Jait Dixit's avatar
Jait Dixit committed
148 149 150 151
        assert_(isinstance(getattr(x, attribute), expected_type))


class RGSpaceFunctionalityTests(unittest.TestCase):
152
    @expand(CONSTRUCTOR_CONFIGS)
Jait Dixit's avatar
Jait Dixit committed
153
    def test_constructor(self, shape, zerocenter, distances,
Martin Reinecke's avatar
Martin Reinecke committed
154 155
                         harmonic, expected):
        x = RGSpace(shape, zerocenter, distances, harmonic)
Jait Dixit's avatar
Jait Dixit committed
156 157 158
        for key, value in expected.iteritems():
            assert_equal(getattr(x, key), value)

159 160
    @expand(product([(10,), (11,), (1, 1), (4, 4), (5, 7), (8, 12), (7, 16),
                     (4, 6, 8), (17, 5, 3)],
Theo Steininger's avatar
Theo Steininger committed
161 162 163
                    [True, False],
                    ['real', 'complex']))
    def test_hermitianize_inverter(self, shape, zerocenter, base):
Theo Steininger's avatar
Theo Steininger committed
164 165 166 167
        try:
            r = RGSpace(shape, harmonic=True, zerocenter=zerocenter)
        except ValueError:
            raise SkipTest
Theo Steininger's avatar
Theo Steininger committed
168 169
        v = distributed_data_object(global_shape=shape, dtype=np.complex128)
        v[:] = np.random.random(shape) + 1j*np.random.random(shape)
Theo Steininger's avatar
Theo Steininger committed
170 171

        nifty_configuration['harmonic_rg_base'] = base
172
        inverted = r.hermitianize_inverter(v, axes=range(len(shape)))
173

Theo Steininger's avatar
Theo Steininger committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        if base == 'complex':
            # test hermitian flipping of `inverted`
            it = np.nditer(v, flags=['multi_index'])
            while not it.finished:
                i1 = it.multi_index
                i2 = []
                for i in range(len(i1)):
                    if r.zerocenter[i] and r.shape[i] % 2 != 0:
                        i2.append(v.shape[i]-i1[i]-1)
                    else:
                        i2.append(v.shape[i]-i1[i] if i1[i] > 0 else 0)
                i2 = tuple(i2)
                assert_almost_equal(inverted[i1], v[i2])
                it.iternext()
        else:
            assert_array_equal(v, inverted)
Martin Reinecke's avatar
Martin Reinecke committed
190

191 192 193 194
    @expand(get_distance_array_configs())
    def test_distance_array(self, shape, distances, zerocenter, expected):
        r = RGSpace(shape=shape, distances=distances, zerocenter=zerocenter)
        assert_almost_equal(r.get_distance_array('not'), expected)
Jait Dixit's avatar
Jait Dixit committed
195

196 197 198 199 200 201 202 203
    @expand(get_weight_configs())
    def test_weight(self, shape, distances, harmonic, x, power, axes,
                    inplace, expected):
        r = RGSpace(shape=shape, distances=distances, harmonic=harmonic)
        res = r.weight(x, power, axes, inplace)
        assert_almost_equal(res, expected)
        if inplace:
            assert_(x is res)