Commit d4179f8a authored by Theo Steininger's avatar Theo Steininger

Merge branch 'tests' into 'master'

Tests

See merge request !53
parents a689cd42 1e8ce667
Pipeline #10847 passed with stages
in 30 minutes and 6 seconds
#!/bin/bash
cd test/data
for filename in *.py; do
./$filename
done
cd ../..
......@@ -458,7 +458,7 @@ class Field(Loggable, Versionable, object):
try:
return reduce(lambda x, y: x * y, volume_tuple)
except TypeError:
return 0
return 0.
# ---Special unary/binary operations---
......
......@@ -131,10 +131,9 @@ class GLSpace(Space):
def weight(self, x, power=1, axes=None, inplace=False):
nlon = self.nlon
nlat = self.nlat
vol = gl.vol(nlat) ** power
weight = np.array(list(itertools.chain.from_iterable(
itertools.repeat(x ** power, nlon)
for x in gl.vol(nlat))))
itertools.repeat(x, nlon) for x in vol)))
if axes is not None:
# reshape the weight array to match the input shape
......
......@@ -149,7 +149,7 @@ class HPSpace(Space):
dtype=self.dtype)
def weight(self, x, power=1, axes=None, inplace=False):
weight = ((4*np.pi) / (12 * self.nside**2)) ** power
weight = ((4 * np.pi) / (12 * self.nside**2))**power
if inplace:
x *= weight
......
import d2o
import numpy as np
from nose_parameterized import parameterized
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:
return type(obj).__name__
elif type(obj) == HPSpace:
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:
if 'error' in obj:
return 'error_' + obj['error'].__name__
else:
return ''
elif type(obj) == np.ndarray:
return 'DATA'
else:
return str(obj)
def custom_name_func(testcase_func, param_num, param):
return "%s_%s" % (
testcase_func.__name__,
strip(parameterized.to_safe_name(
" ".join(pretty_str(x) for x in param.args)), '_')
)
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
#!/usr/bin/env python
import itertools
import numpy as np
import libsharp_wrapper_gl as gl
# deterministic
np.random.seed(42)
def distance_array(nlat, nlon, latitude, longitude):
lat = latitude * (np.pi / (nlat - 1))
lon = longitude * (2 * np.pi / (nlon - 1))
# Vincenty formula: https://en.wikipedia.org/wiki/Great-circle_distance
# phi_1, lambda_1 = lat, lon
# phi_2, lambda_2 = 0
numerator = np.sqrt((np.cos(0) * np.sin(lon - 0))**2 +
((np.cos(lat) * np.sin(0)) -
(np.sin(lat) * np.cos(0) * np.cos(lon - 0)))**2)
denominator = (
np.sin(lat) * np.sin(0)) + (np.cos(lat) * np.cos(0) * np.cos(lon - 0))
return np.arctan(numerator/denominator)
# for GLSpace(nlat=2, nlon=3)
da_0 = np.array(
[distance_array(2, 3, *divmod(idx, 3)) for idx in np.arange(6)])
# for GLSpace(nlat=2, nlon=3)
weight_0 = np.array(list(itertools.chain.from_iterable(
itertools.repeat(x, 3) for x in gl.vol(2))))
w_0_x = np.random.rand(6)
w_0_res = w_0_x * weight_0
weight_1 = np.array(list(itertools.chain.from_iterable(
itertools.repeat(x, 3) for x in gl.vol(2))))
weight_1 = weight_1.reshape([1, 1, 6])
w_1_x = np.random.rand(32, 16, 6)
w_1_res = w_1_x * weight_1
# write everything to disk
np.savez(
'gl_space', da_0=da_0, w_0_x=w_0_x, w_0_res=w_0_res, w_1_x=w_1_x,
w_1_res=w_1_res)
#!/usr/bin/env python
import numpy as np
import healpy as hp
# deterministic
np.random.seed(42)
# for HPSpace(nside=2)
da_0 = np.array([np.arccos(hp.pix2vec(2, idx)[0]) for idx in np.arange(48)])
# for HPSpace(nside=2)
w_0_x = np.random.rand(48)
w_0_res = w_0_x * ((4 * np.pi) / 48)
w_1_res = w_0_x * (((4 * np.pi) / 48)**2)
# write everything to disk
np.savez('hp_space', da_0=da_0, w_0_x=w_0_x, w_0_res=w_0_res, w_1_x=w_0_x,
w_1_res=w_1_res)
#!/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)
#!/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)
#!/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)
# -*- coding: utf-8 -*-
import unittest
import numpy as np
from numpy.testing import assert_,\
assert_equal
from itertools import product
from nifty import Field,\
RGSpace,\
FieldArray
from d2o import distributed_data_object,\
STRATEGIES
from test.common import expand
np.random.seed(123)
SPACES = [RGSpace((4,), dtype=np.float), RGSpace((5), dtype=np.complex)]
SPACE_COMBINATIONS = [(), SPACES[0], SPACES[1], SPACES]
class Test_Interface(unittest.TestCase):
@expand(product(SPACE_COMBINATIONS,
[['dtype', np.dtype],
['distribution_strategy', str],
['domain', tuple],
['domain_axes', tuple],
['val', distributed_data_object],
['shape', tuple],
['dim', np.int],
['dof', np.int],
['total_volume', np.float]]))
def test_return_types(self, domain, attribute_desired_type):
attribute = attribute_desired_type[0]
desired_type = attribute_desired_type[1]
f = Field(domain=domain)
assert_(isinstance(getattr(f, attribute), desired_type))
#class Test_Initialization(unittest.TestCase):
#
# @parameterized.expand(
# itertools.product(SPACE_COMBINATIONS,
# []
# )
# def test_
import nifty as nt
import numpy as np
import unittest
def weighted_np_transform(val, domain, codomain, axes=None):
if codomain.harmonic:
# correct for forward fft
val = domain.weight(val, power=1, axes=axes)
# Perform the transformation
Tval = np.fft.fftn(val, axes=axes)
if not codomain.harmonic:
# correct for inverse fft
Tval = codomain.weight(Tval, power=-1, axes=axes)
return Tval
def test_simple_fft():
x = nt.RGSpace((16,))
x_p = nt.FFTOperator.get_default_codomain(x)
f = nt.Field((x, x), val=1)
val_p = np.ones((16,16))
fft = nt.FFTOperator(x)
assert np.allclose(
fft(f, spaces=(1,)).val,
weighted_np_transform(val_p, x, x_p, axes=(1,))
)
import unittest
from nifty import dependency_injector as di
from numpy.testing import assert_equal
from keepers import Repository
from test.common import expand, generate_spaces
try:
import h5py
except ImportError:
h5py_available = False
else:
h5py_available = True
if h5py_available:
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'))
import unittest
import numpy as np
from numpy.testing import assert_, assert_equal, assert_raises,\
assert_almost_equal
from nose.plugins.skip import SkipTest
from nifty import GLSpace
from nifty.config import dependency_injector as di
from test.common import expand
# [nlat, nlon, dtype, expected]
CONSTRUCTOR_CONFIGS = [
[2, None, None, {
'nlat': 2,
'nlon': 3,
'harmonic': False,
'shape': (6,),
'dim': 6,
'total_volume': 4 * np.pi,
'dtype': np.dtype('float64')
}],
[1, None, None, {
'error': ValueError
}],
[5, None, None, {
'error': ValueError
}]
]
def get_distance_array_configs():
npzfile = np.load('test/data/gl_space.npz')
return [[2, None, None, npzfile['da_0']]]
def get_weight_configs():
npzfile = np.load('test/data/gl_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']],
[npzfile['w_1_x'], 1, (2,), True, npzfile['w_1_res']],
]
class GLSpaceInterfaceTests(unittest.TestCase):
@expand([['nlat', int],
['nlon', int]])
def test_property_ret_type(self, attribute, expected_type):
try:
g = GLSpace()
except ImportError:
raise SkipTest
assert_(isinstance(getattr(g, attribute), expected_type))
class GLSpaceFunctionalityTests(unittest.TestCase):
@expand(CONSTRUCTOR_CONFIGS)
def test_constructor(self, nlat, nlon, dtype, expected):
if 'libsharp_wrapper_gl' not in di:
raise SkipTest
else:
if 'error' in expected:
with assert_raises(expected['error']):
GLSpace(nlat, nlon, dtype)
else:
g = GLSpace(nlat, nlon, dtype)
for key, value in expected.iteritems():
assert_equal(getattr(g, key), value)
@expand(get_weight_configs())
def test_weight(self, x, power, axes, inplace, expected):
if 'libsharp_wrapper_gl' not in di:
raise SkipTest
else:
g = GLSpace(2)
res = g.weight(x, power, axes, inplace)
assert_almost_equal(res, expected)
if inplace:
assert_(x is res)
@expand(get_distance_array_configs())
def test_distance_array(self, nlat, nlon, dtype, expected):
if 'libsharp_wrapper_gl' not in di:
raise SkipTest
else:
g = GLSpace(nlat, nlon, dtype)
assert_almost_equal(g.get_distance_array('not').data, expected)
import unittest
import numpy as np
from numpy.testing import assert_, assert_equal, assert_raises,\
assert_almost_equal
from nose.plugins.skip import SkipTest
from nifty import HPSpace
from nifty.config import dependency_injector as di
from test.common import expand
# [nside, dtype, expected]
CONSTRUCTOR_CONFIGS = [
[2, None, {
'nside': 2,
'harmonic': False,
'shape': (48,),
'dim': 48,
'total_volume': 4 * np.pi,
'dtype': np.dtype('float64')
}],
[5, None, {
'error': ValueError
}],