diff --git a/test/common.py b/test/common.py index cb711e8556ab7aebeb249e463d54f700327039f4..6ba410dd0d53868677e4355c3e4661ca1d5fd814 100644 --- a/test/common.py +++ b/test/common.py @@ -1,13 +1,16 @@ import d2o import numpy as np from nose_parameterized import parameterized -from nifty import RGSpace, LMSpace, HPSpace, GLSpace +from nifty import RGSpace, LMSpace, HPSpace, GLSpace, PowerSpace +from nifty.config import dependency_injector as di from string import strip def pretty_str(obj): if type(obj) == list: return " ".join(pretty_str(x) for x in obj) + if type(obj) == tuple: + return " ".join(pretty_str(x) for x in obj) if type(obj) == RGSpace: return type(obj).__name__ elif type(obj) == LMSpace: @@ -16,6 +19,8 @@ def pretty_str(obj): return type(obj).__name__ elif type(obj) == GLSpace: return type(obj).__name__ + elif type(obj) == PowerSpace: + return type(obj).__name__ elif isinstance(obj, d2o.distributed_data_object): return 'd2o' elif type(obj) == dict: @@ -40,3 +45,16 @@ def custom_name_func(testcase_func, param_num, param): def expand(*args, **kwargs): return parameterized.expand(*args, testcase_func_name=custom_name_func, **kwargs) + + +def generate_spaces(): + spaces = [RGSpace(4), PowerSpace(RGSpace((4, 4), harmonic=True))] + + if 'healpy' in di: + spaces.append(HPSpace(4)) + if 'libsharp_wrapper_gl' in di: + spaces.append(GLSpace(4)) + if 'healpy' in di or 'libsharp_wrapper_gl' in di: + spaces.append(LMSpace(5)) + + return spaces diff --git a/test/data/gen_lm_space_data.py b/test/data/gen_lm_space_data.py new file mode 100644 index 0000000000000000000000000000000000000000..cdc3d3599cc1dc51fd1259211cd423195484d3e8 --- /dev/null +++ b/test/data/gen_lm_space_data.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +from __future__ import division +import numpy as np + +# deterministic +np.random.seed(42) + + +def distance_array_helper(index_arr, lmax): + if index_arr <= lmax: + index_half = index_arr + else: + if (index_arr - lmax) % 2 == 0: + index_half = (index_arr + lmax) / 2 + else: + index_half = (index_arr + lmax + 1) / 2 + + m = ( + np.ceil(((2 * lmax + 1) - np.sqrt((2 * lmax + 1)**2 - + 8 * (index_half - lmax))) / 2) + ).astype(int) + + return index_half - m * (2 * lmax + 1 - m) // 2 + + +# for LMSpace(5) +da_0 = [distance_array_helper(idx, 5) for idx in np.arange(36)] + +# random input for weight +w_0_x = np.random.rand(32, 16, 6) + +# random input for hermitian +h_0_res_real = np.random.rand(32, 16, 6).astype(np.complex128) +h_0_res_imag = np.random.rand(32, 16, 6).astype(np.complex128) +h_0_x = h_0_res_real + h_0_res_imag * 1j + +# write everything to disk +np.savez('lm_space', da_0=da_0, w_0_x=w_0_x, w_0_res=w_0_x, h_0_x=h_0_x, + h_0_res_real=h_0_res_real, h_0_res_imag=h_0_res_imag) diff --git a/test/data/gen_power_space_data.py b/test/data/gen_power_space_data.py new file mode 100644 index 0000000000000000000000000000000000000000..6e53465c6e3f758994963c81d22c0872f496097a --- /dev/null +++ b/test/data/gen_power_space_data.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +from __future__ import division +import numpy as np + +# deterministic +np.random.seed(42) + +# for now directly the kindex +# RGSpace((4, 4), harmonic=True) +da_0 = np.array([0, 1.0, 1.41421356, 2., 2.23606798, 2.82842712]) + +# 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 + + +# write everything to disk +np.savez('power_space', + da_0=da_0, + w_0_x=w_0_x, + w_0_res=w_0_res) diff --git a/test/data/gen_rg_space_data.py b/test/data/gen_rg_space_data.py new file mode 100644 index 0000000000000000000000000000000000000000..c5a694e598f761f73f6f8f3ec15e17cfc544d5d7 --- /dev/null +++ b/test/data/gen_rg_space_data.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python +from __future__ import division +import numpy as np + +# deterministic +np.random.seed(42) + +# 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.fftshift(da_0) +temp = ((cords_0[1] - 4 // 2) * 0.25)**2 +temp = np.fft.fftshift(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) + +# 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) + +# hermitianization +h_0_x = np.array([ + [0.88250339+0.12102381j, 0.54293435+0.7345584j, 0.87057998+0.20515315j, + 0.16602950+0.09396132j], + [0.83853902+0.17974696j, 0.79735933+0.37104425j, 0.22057732+0.9498977j, + 0.14329183+0.47899678j], + [0.96934284+0.3792878j, 0.13118669+0.45643055j, 0.16372149+0.48235714j, + 0.66141537+0.20383357j], + [0.49168197+0.77572178j, 0.09570420+0.14219071j, 0.69735595+0.33017333j, + 0.83692452+0.18544449j]]) +h_0_res_real = np.array([ + [0.88250339+0.j, 0.35448193+0.32029854j, 0.87057998+0.j, + 0.35448193-0.32029854j], + [0.66511049-0.29798741j, 0.81714193+0.09279988j, 0.45896664+0.30986218j, + 0.11949801+0.16840303j], + [0.96934284+0.j, 0.39630103+0.12629849j, 0.16372149+0.j, + 0.39630103-0.12629849j], + [0.66511049+0.29798741j, 0.11949801-0.16840303j, 0.45896664-0.30986218j, + 0.81714193-0.09279988j]]) +h_0_res_imag = np.array([ + [0.12102381+0.j, 0.41425986-0.18845242j, 0.20515315+0.j, + 0.41425986+0.18845242j], + [0.47773437-0.17342852j, 0.27824437+0.0197826j, 0.64003551+0.23838932j, + 0.31059374-0.02379381j], + [0.37928780+0.j, 0.33013206+0.26511434j, 0.48235714+0.j, + 0.33013206-0.26511434j], + [0.47773437+0.17342852j, 0.31059374+0.02379381j, 0.64003551-0.23838932j, + 0.27824437-0.0197826j]]) + +h_1_x = np.array([ + [[0.23987021+0.41617749j, 0.34605012+0.55462234j, 0.07947035+0.73360723j, + 0.22853748+0.39275304j], + [0.90254910+0.02107809j, 0.28195470+0.56031588j, 0.23004043+0.33873536j, + 0.56398377+0.68913034j], + [0.81897406+0.2050369j, 0.88724852+0.8137488j, 0.84645004+0.0059284j, + 0.14950377+0.50013099j]], + [[0.93491597+0.73251066j, 0.74764790+0.11539037j, 0.48090736+0.04352568j, + 0.49363732+0.97233093j], + [0.72761881+0.74636216j, 0.46390134+0.4343401j, 0.88436859+0.79415269j, + 0.67027606+0.85498234j], + [0.86318727+0.19076379j, 0.36859448+0.89842333j, 0.73407193+0.85091112j, + 0.44187657+0.08936409j]] + ]) +h_1_res_real = np.array([ + [[0.23987021+0.j, 0.28729380+0.08093465j, 0.07947035+0.j, + 0.28729380-0.08093465j], + [0.90254910+0.j, 0.42296924-0.06440723j, 0.23004043+0.j, + 0.42296924+0.06440723j], + [0.81897406+0.j, 0.51837614+0.1568089j, 0.84645004+0.j, + 0.51837614-0.1568089j]], + [[0.93491597+0.j, 0.62064261-0.42847028j, 0.48090736+0.j, + 0.62064261+0.42847028j], + [0.72761881+0.j, 0.56708870-0.21032112j, 0.88436859+0.j, + 0.56708870+0.21032112j], + [0.86318727+0.j, 0.40523552+0.40452962j, 0.73407193+0.j, + 0.40523552-0.40452962j]] + ]) +h_1_res_imag = np.array([ + [[0.41617749+0.j, 0.47368769-0.05875632j, 0.73360723+0.j, + 0.47368769+0.05875632j], + [0.02107809+0.j, 0.62472311+0.14101454j, 0.33873536+0.j, + 0.62472311-0.14101454j], + [0.20503690+0.j, 0.65693990-0.36887238j, 0.00592840+0.j, + 0.65693990+0.36887238j]], + [[0.73251066+0.j, 0.54386065-0.12700529j, 0.04352568+0.j, + 0.54386065+0.12700529j], + [0.74636216+0.j, 0.64466122+0.10318736j, 0.79415269+0.j, + 0.64466122-0.10318736j], + [0.19076379+0.j, 0.49389371+0.03664104j, 0.85091112+0.j, + 0.49389371-0.03664104j]] + ]) + +# write everything to disk +np.savez('rg_space', + da_0=da_0, + da_1=da_1, + da_2=da_2, + w_0_x=w_0_x, + w_0_res=w_0_res, + w_1_res=w_1_res, + w_2_res=w_2_res, + w_3_res=w_3_res, + h_0_x=h_0_x, + h_0_res_real=h_0_res_real, + h_0_res_imag=h_0_res_imag, + h_1_x=h_1_x, + h_1_res_real=h_1_res_real, + h_1_res_imag=h_1_res_imag) diff --git a/test/data/gl_space.npz b/test/data/gl_space.npz index dca748e000573e798df7083a536383a075385646..250543e00232fd78b1d22835652327a95c2c3a86 100644 Binary files a/test/data/gl_space.npz and b/test/data/gl_space.npz differ diff --git a/test/data/hp_space.npz b/test/data/hp_space.npz index 944116e089962472f11a6b9cc4739d2e0557228c..a85a72ae566918d4bf9ff9f60c380c482bbab8f2 100644 Binary files a/test/data/hp_space.npz and b/test/data/hp_space.npz differ diff --git a/test/data/lm_space.npz b/test/data/lm_space.npz new file mode 100644 index 0000000000000000000000000000000000000000..69e843552f57fb417db5bd87bf8e3424058bea4f Binary files /dev/null and b/test/data/lm_space.npz differ diff --git a/test/data/power_space.npz b/test/data/power_space.npz new file mode 100644 index 0000000000000000000000000000000000000000..67a579116b204d435e57618923dd06b79360dacb Binary files /dev/null and b/test/data/power_space.npz differ diff --git a/test/data/rg_space.npz b/test/data/rg_space.npz new file mode 100644 index 0000000000000000000000000000000000000000..1073fe9f67304fcc31eecb3f1af6a0ea20ef9291 Binary files /dev/null and b/test/data/rg_space.npz differ diff --git a/test/test_serialization.py b/test/test_serialization.py new file mode 100644 index 0000000000000000000000000000000000000000..917ce4bec35ae2d366d84cb3610e06f82f6b6585 --- /dev/null +++ b/test/test_serialization.py @@ -0,0 +1,21 @@ +import unittest + +from numpy.testing import assert_equal +from keepers import Repository +from test.common import expand, generate_spaces + + +class SpaceSerializationTests(unittest.TestCase): + # variable to hold the repository + _repo = None + + @classmethod + def setUpClass(cls): + cls._repo = Repository('test.h5') + + @expand([[space] for space in generate_spaces()]) + def test_serialization(self, space): + self._repo.add(space, 'space') + self._repo.commit() + + assert_equal(space, self._repo.get('space')) diff --git a/test/test_spaces/test_gl_space.py b/test/test_spaces/test_gl_space.py index 5ed5cf27caf2e065afda7ef80419c3118dd0ec28..d909f6af33f70a9fb09fa2db08f67032cc9e01f6 100644 --- a/test/test_spaces/test_gl_space.py +++ b/test/test_spaces/test_gl_space.py @@ -38,7 +38,7 @@ def get_weight_configs(): return [ [npzfile['w_0_x'], 1, None, False, npzfile['w_0_res']], [npzfile['w_0_x'], 1, None, True, npzfile['w_0_res']], - [npzfile['w_1_x'], 1, None, True, npzfile['w_1_res']], + [npzfile['w_1_x'], 1, (2,), True, npzfile['w_1_res']], ] diff --git a/test/test_spaces/test_interface.py b/test/test_spaces/test_interface.py index 03be5c80edfda612e57d57030bee94b36d9006d4..03f9de80254e9daa0b7be4a4cb66c704ff1a5049 100644 --- a/test/test_spaces/test_interface.py +++ b/test/test_spaces/test_interface.py @@ -2,25 +2,12 @@ import unittest import numpy as np from itertools import product -from d2o import distributed_data_object from types import LambdaType from numpy.testing import assert_, assert_raises, assert_equal -from nifty import RGSpace, LMSpace, GLSpace, HPSpace +from nose.plugins.skip import SkipTest +from nifty import LMSpace, GLSpace, HPSpace from nifty.config import dependency_injector as di -from test.common import expand - - -def generate_spaces(): - spaces = [RGSpace(4)] - - if 'healpy' in di: - spaces.append(HPSpace(4)) - if 'libsharp_wrapper_gl' in di: - spaces.append(GLSpace(4)) - if 'healpy' in di or 'libsharp_wrapper_gl' in di: - spaces.append(LMSpace(5)) - - return spaces +from test.common import expand, generate_spaces class SpaceInterfaceTests(unittest.TestCase): @@ -53,6 +40,12 @@ class SpaceInterfaceTests(unittest.TestCase): ['get_fft_smoothing_kernel_function', 2.0, LambdaType], ])) def test_method_ret_type(self, space, method_expected_type): + # handle exceptions here + try: + getattr( + space, method_expected_type[0])(*method_expected_type[1:-1]) + except NotImplementedError: + raise SkipTest assert_equal( type(getattr( @@ -61,3 +54,10 @@ class SpaceInterfaceTests(unittest.TestCase): ), method_expected_type[-1] ) + + @expand([[space] for space in generate_spaces()]) + def test_copy(self, space): + # make sure it's a deep copy + assert_(space is not space.copy()) + # make sure contents are the same + assert_equal(space, space.copy()) diff --git a/test/test_spaces/test_lm_space.py b/test/test_spaces/test_lm_space.py index e4fa08597673aea51889ff689d7eb5957dcd27ad..3186402d6cbe399518590016bac9559391570244 100644 --- a/test/test_spaces/test_lm_space.py +++ b/test/test_spaces/test_lm_space.py @@ -1,14 +1,16 @@ import unittest import numpy as np -from numpy.testing import assert_, assert_equal, assert_raises +from numpy.testing import assert_, assert_equal, assert_raises,\ + assert_almost_equal from nose.plugins.skip import SkipTest +from d2o import distributed_data_object from nifty import LMSpace from nifty.config import dependency_injector as di from test.common import expand # [lmax, dtype, expected] -INIT_CONFIGS = [ +CONSTRUCTOR_CONFIGS = [ [5, None, { 'lmax': 5, 'mmax': 5, @@ -33,20 +35,40 @@ INIT_CONFIGS = [ ] +def get_distance_array_configs(): + npzfile = np.load('test/data/lm_space.npz') + return [[5, None, npzfile['da_0']]] + + +def get_weight_configs(): + npzfile = np.load('test/data/lm_space.npz') + return [ + [npzfile['w_0_x'], 1, None, False, npzfile['w_0_res']], + [npzfile['w_0_x'], 1, None, True, npzfile['w_0_res']] + ] + + +def get_hermitian_configs(): + npzfile = np.load('test/data/lm_space.npz') + return [ + [npzfile['h_0_x'], npzfile['h_0_res_real'], npzfile['h_0_res_imag']] + ] + + class LMSpaceIntefaceTests(unittest.TestCase): @expand([['lmax', int], ['mmax', int], ['dim', int]]) - def test_properties(self, attribute, expected_type): + def test_property_ret_type(self, attribute, expected_type): try: - x = LMSpace(7) + l = LMSpace(7) except ImportError: raise SkipTest - assert_(isinstance(getattr(x, attribute), expected_type)) + assert_(isinstance(getattr(l, attribute), expected_type)) class LMSpaceFunctionalityTests(unittest.TestCase): - @expand(INIT_CONFIGS) + @expand(CONSTRUCTOR_CONFIGS) def test_constructor(self, lmax, dtype, expected): if 'libsharp_wrapper_gl' not in di or 'healpy' not in di: raise SkipTest @@ -59,11 +81,34 @@ class LMSpaceFunctionalityTests(unittest.TestCase): for key, value in expected.iteritems(): assert_equal(getattr(l, key), value) - def test_hermitian_decomposition(self): - pass + @expand(get_hermitian_configs()) + def test_hermitian_decomposition(self, x, real, imag): + if 'libsharp_wrapper_gl' not in di or 'healpy' not in di: + raise SkipTest + else: + l = LMSpace(5) + assert_almost_equal( + l.hermitian_decomposition(distributed_data_object(x))[0], + real) + assert_almost_equal( + l.hermitian_decomposition(distributed_data_object(x))[1], + imag) - def test_weight(self): - pass + @expand(get_weight_configs()) + def test_weight(self, x, power, axes, inplace, expected): + if 'libsharp_wrapper_gl' not in di or 'healpy' not in di: + raise SkipTest + else: + l = LMSpace(5) + res = l.weight(x, power, axes, inplace) + assert_almost_equal(res, expected) + if inplace: + assert_(x is res) - def test_distance_array(self): - pass + @expand(get_distance_array_configs()) + def test_distance_array(self, lmax, dtype, expected): + if 'libsharp_wrapper_gl' not in di or 'healpy' not in di: + raise SkipTest + else: + l = LMSpace(lmax, dtype) + assert_almost_equal(l.get_distance_array('not').data, expected) diff --git a/test/test_spaces/test_power_space.py b/test/test_spaces/test_power_space.py new file mode 100644 index 0000000000000000000000000000000000000000..1f095b07219c1e78073316d1d9ea85590346627e --- /dev/null +++ b/test/test_spaces/test_power_space.py @@ -0,0 +1,122 @@ +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 +from nifty import PowerSpace, RGSpace, LMSpace, Space +from types import NoneType +from test.common import expand + +# [harmonic_domain, distribution_strategy, +# log, nbin, binbounds, dtype, expected] +CONSTRUCTOR_CONFIGS = [ + [1, 'not', False, None, None, None, {'error': ValueError}], + [RGSpace((8,)), 'not', False, None, None, None, {'error': ValueError}], + [RGSpace((8,), harmonic=True), 'not', False, None, None, None, { + 'harmonic': True, + 'shape': (5,), + 'dim': 5, + 'total_volume': 8.0, + 'harmonic_domain': RGSpace((8,), harmonic=True), + 'log': False, + '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.]), + 'dtype': np.dtype('float64') + }], + [RGSpace((8,), harmonic=True), 'not', True, None, None, None, { + 'harmonic': True, + 'shape': (2,), + 'dim': 2, + 'total_volume': 8.0, + 'harmonic_domain': RGSpace((8,), harmonic=True), + 'log': True, + '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]), + 'dtype': np.dtype('float64') + }], + ] + + +def get_distance_array_configs(): + npzfile = np.load('test/data/power_space.npz') + return [ + [RGSpace((4, 4), harmonic=True), npzfile['da_0']], + ] + + +def get_weight_configs(): + npzfile = np.load('test/data/power_space.npz') + return [ + [RGSpace((4, 4), harmonic=True), + npzfile['w_0_x'], 1, (2,), False, npzfile['w_0_res']], + [RGSpace((4, 4), harmonic=True), + npzfile['w_0_x'], 1, (2,), True, npzfile['w_0_res']], + ] + + +class PowerSpaceInterfaceTest(unittest.TestCase): + @expand([ + ['harmonic_domain', Space], + ['log', bool], + ['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)) + + +class PowerSpaceFunctionalityTest(unittest.TestCase): + @expand(CONSTRUCTOR_CONFIGS) + def test_constructor(self, harmonic_domain, distribution_strategy, log, + nbin, binbounds, dtype, expected): + if 'error' in expected: + with assert_raises(expected['error']): + PowerSpace(harmonic_domain=harmonic_domain, + distribution_strategy=distribution_strategy, + log=log, nbin=nbin, binbounds=binbounds, + dtype=dtype) + else: + p = PowerSpace(harmonic_domain=harmonic_domain, + distribution_strategy=distribution_strategy, + log=log, nbin=nbin, binbounds=binbounds, + dtype=dtype) + 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()) + def test_distance_array(self, harmonic_domain, expected): + p = PowerSpace(harmonic_domain=harmonic_domain) + assert_almost_equal(p.get_distance_array('not'), expected) + + @expand(get_weight_configs()) + def test_weight(self, harmonic_domain, x, power, axes, + inplace, expected): + p = PowerSpace(harmonic_domain=harmonic_domain) + res = p.weight(x, power, axes, inplace) + assert_almost_equal(res, expected) + if inplace: + assert_(x is res) diff --git a/test/test_spaces/test_rg_space.py b/test/test_spaces/test_rg_space.py index 423d5c8e72ff5b95d10c74497fd4831c9455619a..27d6722a77ce573a8c3ce6692d55790398b85af9 100644 --- a/test/test_spaces/test_rg_space.py +++ b/test/test_spaces/test_rg_space.py @@ -3,12 +3,12 @@ from __future__ import division import unittest import numpy as np -from numpy.testing import assert_, assert_equal +from numpy.testing import assert_, assert_equal, assert_almost_equal from nifty import RGSpace from test.common import expand # [shape, zerocenter, distances, harmonic, dtype, expected] -INIT_CONFIGS = [ +CONSTRUCTOR_CONFIGS = [ [(8,), False, None, False, None, { 'shape': (8,), @@ -73,27 +73,79 @@ INIT_CONFIGS = [ ] +def get_distance_array_configs(): + npzfile = np.load('test/data/rg_space.npz') + return [ + [(4, 4), None, [False, False], npzfile['da_0']], + [(4, 4), None, [True, True], npzfile['da_1']], + [(4, 4), (12, 12), [True, True], npzfile['da_2']] + ] + + +def get_weight_configs(): + npzfile = np.load('test/data/rg_space.npz') + return [ + [(11, 11), None, False, + npzfile['w_0_x'], 1, None, False, npzfile['w_0_res']], + [(11, 11), None, False, + npzfile['w_0_x'], 1, None, True, npzfile['w_0_res']], + [(11, 11), (1.3, 1.3), False, + npzfile['w_0_x'], 1, None, False, npzfile['w_1_res']], + [(11, 11), (1.3, 1.3), False, + npzfile['w_0_x'], 1, None, True, npzfile['w_1_res']], + [(11, 11), None, True, + npzfile['w_0_x'], 1, None, False, npzfile['w_2_res']], + [(11, 11), None, True, + npzfile['w_0_x'], 1, None, True, npzfile['w_2_res']], + [(11, 11), (1.3, 1.3), True, + npzfile['w_0_x'], 1, None, False, npzfile['w_3_res']], + [(11, 11), (1.3, 1.3), True, + npzfile['w_0_x'], 1, None, True, npzfile['w_3_res']] + ] + + +def get_hermitian_configs(): + npzfile = np.load('test/data/rg_space.npz') + return [ + [npzfile['h_0_x'], None, + npzfile['h_0_res_real'], npzfile['h_0_res_imag']], + [npzfile['h_1_x'], (2,), + npzfile['h_1_res_real'], npzfile['h_1_res_imag']] + ] + + class RGSpaceInterfaceTests(unittest.TestCase): @expand([['distances', tuple], ['zerocenter', tuple]]) - def test_properties(self, attribute, expected_type): + def test_property_ret_type(self, attribute, expected_type): x = RGSpace() assert_(isinstance(getattr(x, attribute), expected_type)) class RGSpaceFunctionalityTests(unittest.TestCase): - @expand(INIT_CONFIGS) + @expand(CONSTRUCTOR_CONFIGS) def test_constructor(self, shape, zerocenter, distances, harmonic, dtype, expected): x = RGSpace(shape, zerocenter, distances, harmonic, dtype) for key, value in expected.iteritems(): assert_equal(getattr(x, key), value) - def test_hermitian_decomposition(self): - pass + @expand(get_hermitian_configs()) + def test_hermitian_decomposition(self, x, axes, real, imag): + r = RGSpace(5) + assert_almost_equal(r.hermitian_decomposition(x, axes=axes)[0], real) + assert_almost_equal(r.hermitian_decomposition(x, axes=axes)[1], imag) - def test_weight(self): - pass + @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) - def test_distance_array(self): - pass + @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)