test_power_space.py 6.24 KB
 Theo Steininger committed Apr 13, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ``````# NIFTy # Copyright (C) 2017 Theo Steininger # # Author: Theo Steininger # # 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 . `````` Jait Dixit committed Feb 12, 2017 19 20 21 22 23 24 25 26 ``````from __future__ import division import unittest import numpy as np from d2o import distributed_data_object from numpy.testing import assert_, assert_equal, assert_almost_equal,\ assert_raises `````` Theo Steininger committed Apr 21, 2017 27 ``````from nifty import PowerSpace, RGSpace, Space `````` Jait Dixit committed Feb 12, 2017 28 29 30 ``````from types import NoneType from test.common import expand `````` Reimar H Leike committed May 11, 2017 31 32 33 34 35 36 37 38 39 40 41 `````` HARMONIC_SPACES = [RGSpace((8,), harmonic=True), RGSpace((7,8), harmonic=True), RGSpace((5,5), harmonic=True), RGSpace((4,5,7), harmonic=True), LMSpace(6),LMSpace(9)] BINNINGS = product([None], [None, 3,4], [True, False]) + product([0.,1.3],[None],[False]) #Try all sensible kinds of combinations of spaces, distributuion strategy and #binning parameters CONSISTENCY_CONFIGS = product(HARMONIC_SPACES, ["not", "equal", "fftw", "freeform"], BINNINGS) `````` Theo Steininger committed May 11, 2017 42 ``````# [harmonic_partner, distribution_strategy, `````` Theo Steininger committed May 11, 2017 43 ``````# logarithmic, nbin, binbounds, expected] `````` Jait Dixit committed Feb 12, 2017 44 ``````CONSTRUCTOR_CONFIGS = [ `````` Martin Reinecke committed Apr 28, 2017 45 46 47 `````` [1, 'not', False, None, None, {'error': ValueError}], [RGSpace((8,)), 'not', False, None, None, {'error': ValueError}], [RGSpace((8,), harmonic=True), 'not', False, None, None, { `````` Jait Dixit committed Feb 12, 2017 48 49 50 51 `````` 'harmonic': True, 'shape': (5,), 'dim': 5, 'total_volume': 8.0, `````` Theo Steininger committed May 11, 2017 52 `````` 'harmonic_partner': RGSpace((8,), harmonic=True), `````` Theo Steininger committed May 11, 2017 53 `````` 'logarithmic': False, `````` Jait Dixit committed Feb 12, 2017 54 55 56 57 58 59 60 61 `````` 'nbin': None, 'binbounds': None, 'pindex': distributed_data_object([0, 1, 2, 3, 4, 3, 2, 1]), 'kindex': np.array([0., 1., 2., 3., 4.]), 'rho': np.array([1, 2, 2, 2, 1]), 'pundex': np.array([0, 1, 2, 3, 4]), 'k_array': np.array([0., 1., 2., 3., 4., 3., 2., 1.]), }], `````` Martin Reinecke committed Apr 28, 2017 62 `````` [RGSpace((8,), harmonic=True), 'not', True, None, None, { `````` Jait Dixit committed Feb 12, 2017 63 64 65 66 `````` 'harmonic': True, 'shape': (2,), 'dim': 2, 'total_volume': 8.0, `````` Theo Steininger committed May 11, 2017 67 `````` 'harmonic_partner': RGSpace((8,), harmonic=True), `````` Theo Steininger committed May 11, 2017 68 `````` 'logarithmic': True, `````` Jait Dixit committed Feb 12, 2017 69 70 71 72 73 74 75 76 77 78 79 80 81 `````` 'nbin': None, 'binbounds': None, 'pindex': distributed_data_object([0, 1, 1, 1, 1, 1, 1, 1]), 'kindex': np.array([0., 2.28571429]), 'rho': np.array([1, 7]), 'pundex': np.array([0, 1]), 'k_array': np.array([0., 2.28571429, 2.28571429, 2.28571429, 2.28571429, 2.28571429, 2.28571429, 2.28571429]), }], ] def get_distance_array_configs(): `````` Martin Reinecke committed Apr 20, 2017 82 `````` da_0 = np.array([0, 1.0, 1.41421356, 2., 2.23606798, 2.82842712]) `````` Jait Dixit committed Feb 12, 2017 83 `````` return [ `````` Martin Reinecke committed Apr 20, 2017 84 `````` [RGSpace((4, 4), harmonic=True), da_0], `````` Jait Dixit committed Feb 12, 2017 85 86 87 88 `````` ] def get_weight_configs(): `````` Martin Reinecke committed Apr 20, 2017 89 90 91 92 93 94 95 96 97 `````` np.random.seed(42) # power 1 w_0_x = np.random.rand(32, 16, 6) # RGSpace((4, 4), harmonic=True) # using rho directly weight_0 = np.array([1, 4, 4, 2, 4, 1]) weight_0 = weight_0.reshape([1, 1, 6]) w_0_res = w_0_x * weight_0 `````` Jait Dixit committed Feb 12, 2017 98 99 `````` return [ [RGSpace((4, 4), harmonic=True), `````` Martin Reinecke committed Apr 20, 2017 100 `````` w_0_x, 1, (2,), False, w_0_res], `````` Jait Dixit committed Feb 12, 2017 101 `````` [RGSpace((4, 4), harmonic=True), `````` Martin Reinecke committed Apr 20, 2017 102 `````` w_0_x.copy(), 1, (2,), True, w_0_res], `````` Jait Dixit committed Feb 12, 2017 103 104 105 106 107 `````` ] class PowerSpaceInterfaceTest(unittest.TestCase): @expand([ `````` Theo Steininger committed May 11, 2017 108 `````` ['harmonic_partner', Space], `````` Theo Steininger committed May 11, 2017 109 `````` ['logarithmic', bool], `````` Jait Dixit committed Feb 12, 2017 110 111 112 113 114 115 116 117 118 119 120 121 122 `````` ['nbin', (int, NoneType)], ['binbounds', (list, NoneType)], ['pindex', distributed_data_object], ['kindex', np.ndarray], ['rho', np.ndarray], ['pundex', np.ndarray], ['k_array', distributed_data_object], ]) 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)) `````` Reimar H Leike committed May 11, 2017 123 ``````class PowerSpaceConsistencyCheck(unittest.TestCase): `````` Reimar H Leike committed May 11, 2017 124 `````` @expand(CONSISTENCY_CONFIGS) `````` Reimar H Leike committed May 11, 2017 125 `````` def test_pipundexInversion(self, harmonic_partner, distribution_strategy, `````` Reimar H Leike committed May 11, 2017 126 127 `````` binbounds, nbin,logarithmic): p = PowerSpace(harmonic_partner=harmonic_partner, `````` Reimar H Leike committed May 11, 2017 128 129 130 `````` distribution_strategy=distribution_strategy, logarithmic=logarithmic, nbin=nbin, binbounds=binbounds) `````` Reimar H Leike committed May 11, 2017 131 `````` assert_equal(p.pindex[p.pundex],np.arange(p.dim),err_msg='pundex is not right-inverse of pindex!') `````` Jait Dixit committed Feb 12, 2017 132 133 `````` class PowerSpaceFunctionalityTest(unittest.TestCase): `````` Reimar H Leike committed May 11, 2017 134 `````` @expand(CONSISTENCY_CONFIGS) `````` Theo Steininger committed May 11, 2017 135 `````` def test_constructor(self, harmonic_partner, distribution_strategy, `````` Theo Steininger committed May 11, 2017 136 `````` logarithmic, nbin, binbounds, expected): `````` Jait Dixit committed Feb 12, 2017 137 138 `````` if 'error' in expected: with assert_raises(expected['error']): `````` Theo Steininger committed May 11, 2017 139 `````` PowerSpace(harmonic_partner=harmonic_partner, `````` Jait Dixit committed Feb 12, 2017 140 `````` distribution_strategy=distribution_strategy, `````` Theo Steininger committed May 11, 2017 141 142 `````` logarithmic=logarithmic, nbin=nbin, binbounds=binbounds) `````` Jait Dixit committed Feb 12, 2017 143 `````` else: `````` Theo Steininger committed May 11, 2017 144 `````` p = PowerSpace(harmonic_partner=harmonic_partner, `````` Jait Dixit committed Feb 12, 2017 145 `````` distribution_strategy=distribution_strategy, `````` Theo Steininger committed May 11, 2017 146 147 `````` logarithmic=logarithmic, nbin=nbin, binbounds=binbounds) `````` Jait Dixit committed Feb 12, 2017 148 149 150 151 152 153 154 `````` for key, value in expected.iteritems(): if isinstance(value, np.ndarray): assert_almost_equal(getattr(p, key), value) else: assert_equal(getattr(p, key), value) @expand(get_distance_array_configs()) `````` Theo Steininger committed May 11, 2017 155 156 `````` def test_distance_array(self, harmonic_partner, expected): p = PowerSpace(harmonic_partner=harmonic_partner) `````` Jait Dixit committed Feb 12, 2017 157 158 159 `````` assert_almost_equal(p.get_distance_array('not'), expected) @expand(get_weight_configs()) `````` Theo Steininger committed May 11, 2017 160 `````` def test_weight(self, harmonic_partner, x, power, axes, `````` Jait Dixit committed Feb 12, 2017 161 `````` inplace, expected): `````` Theo Steininger committed May 11, 2017 162 `````` p = PowerSpace(harmonic_partner=harmonic_partner) `````` Jait Dixit committed Feb 12, 2017 163 164 165 166 `````` res = p.weight(x, power, axes, inplace) assert_almost_equal(res, expected) if inplace: assert_(x is res)``````