Commit 0b69d7ed authored by Theo Steininger's avatar Theo Steininger
Browse files

Merge branch 'move_transforms_to_transformator' into 'feature/field_multiple_space'

Move transforms to transformator



See merge request !18
parents 76de9882 cae16bd4
......@@ -93,4 +93,4 @@ from demos import get_demo_dir
from pickling import _pickle_method, _unpickle_method
#import pyximport; pyximport.install(pyimport = True)
from transformations import tf as transformator
......@@ -34,7 +34,7 @@ setup(name="ift_nifty",
url="http://www.mpa-garching.mpg.de/ift/nifty/",
packages=["nifty", "nifty.demos", "nifty.rg", "nifty.lm",
"nifty.operators", "nifty.dummys", "nifty.field_types",
"nifty.config", "nifty.power"],
"nifty.config", "nifty.power", "nifty.transformations"],
package_dir={"nifty": ""},
zip_safe=False,
dependency_links=[
......
import nifty as nt
import numpy as np
import unittest
import d2o
class TestFFTWTransform(unittest.TestCase):
def test_comm(self):
x = nt.RGSpace((8, 8), fft_module='pyfftw')
a = np.ones((8, 8))
b = d2o.distributed_data_object(a)
b.comm = [1, 2, 3] # change comm to something not supported
with self.assertRaises(RuntimeError):
x.fft_machine.transform(b, x, x.get_codomain())
def test_shapemismatch(self):
x = nt.RGSpace((8, 8), fft_module='pyfftw')
a = np.ones((8, 8))
b = x.cast(a)
with self.assertRaises(ValueError):
x.fft_machine.transform(b, x, x.get_codomain(), axes=(0, 1, 2))
def test_local_ndarray(self):
x = nt.RGSpace((8, 8), fft_module='pyfftw')
a = np.ones((8, 8))
self.assertTrue(
np.allclose(
x.fft_machine.transform(a, x, x.get_codomain()),
np.fft.fftn(a)
), 'results do not match numpy.fft.fftn'
)
def test_local_notzero(self):
x = nt.RGSpace(8, fft_module='pyfftw')
a = np.ones((8, 8))
b = x.cast(a)
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain(), axes=(1,)),
np.fft.fftn(a, axes=(1,))
), 'results do not match numpy.fft.fftn'
)
def test_not(self):
x = nt.RGSpace((8, 8), fft_module='pyfftw')
a = np.ones((8, 8))
b = d2o.distributed_data_object(a, distribution_strategy='not')
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain()),
np.fft.fftn(a)
), 'results do not match numpy.fft.fftn'
)
def test_mpi_axesnone(self):
x = nt.RGSpace((8, 8), fft_module='pyfftw')
a = np.ones((8, 8))
b = x.cast(a)
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain()),
np.fft.fftn(a)
), 'results do not match numpy.fft.fftn'
)
def test_mpi_axesnone_equal(self):
x = nt.RGSpace((8, 8), fft_module='pyfftw')
a = np.ones((8, 8))
b = d2o.distributed_data_object(a, distribution_strategy='equal')
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain()),
np.fft.fftn(a)
), 'results do not match numpy.fft.fftn'
)
def test_mpi_axesall(self):
x = nt.RGSpace((8, 8), fft_module='pyfftw')
a = np.ones((8, 8))
b = x.cast(a)
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain(), axes=(0, 1)),
np.fft.fftn(a)
), 'results do not match numpy.fft.fftn'
)
def test_mpi_axesall_equal(self):
x = nt.RGSpace((8, 8), fft_module='pyfftw')
a = np.ones((8, 8))
b = d2o.distributed_data_object(a, distribution_strategy='equal')
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain(), axes=(0, 1)),
np.fft.fftn(a)
), 'results do not match numpy.fft.fftn'
)
def test_mpi_zero(self):
x = nt.RGSpace(8, fft_module='pyfftw')
a = np.ones((8, 8)) + 1j*np.zeros((8, 8))
b = x.cast(a)
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain(), axes=(0,)),
np.fft.fftn(a, axes=(0,))
), 'results do not match numpy.fft.fftn'
)
def test_mpi_zero_equal(self):
x = nt.RGSpace(8, fft_module='pyfftw')
a = np.ones((8, 8)) + 1j*np.zeros((8, 8))
b = d2o.distributed_data_object(a, distribution_strategy='equal')
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain(), axes=(0,)),
np.fft.fftn(a, axes=(0,))
), 'results do not match numpy.fft.fftn'
)
def test_mpi_zero_not(self):
x = nt.RGSpace(8, fft_module='pyfftw')
a = np.ones((8, 8)) + 1j*np.zeros((8, 8))
b = d2o.distributed_data_object(a, distribution_strategy='not')
self.assertTrue(
np.allclose(
x.fft_machine.transform(b, x, x.get_codomain(), axes=(0,)),
np.fft.fftn(a, axes=(0,))
), 'results do not match numpy.fft.fftn'
)
if __name__ == '__main__':
unittest.main()
import numpy as np
from numpy.testing import assert_equal, assert_almost_equal, assert_raises
from nose_parameterized import parameterized
import unittest
import itertools
from nifty import RGSpace, LMSpace, HPSpace, GLSpace
from nifty import transformator
from nifty.transformations.rgrgtransformation import RGRGTransformation
from nifty.rg.rg_space import gc as RG_GC
import d2o
###############################################################################
def custom_name_func(testcase_func, param_num, param):
return "%s_%s" % (
testcase_func.__name__,
parameterized.to_safe_name("_".join(str(x) for x in param.args)),
)
def weighted_np_transform(val, domain, codomain, axes=None):
if codomain.harmonic:
# correct for forward fft
val = domain.calc_weight(val, power=1)
# Perform the transformation
Tval = np.fft.fftn(val, axes=axes)
if not codomain.harmonic:
# correct for inverse fft
Tval = codomain.calc_weight(Tval, power=-1)
return Tval
###############################################################################
rg_rg_fft_modules = []
for name in ['gfft', 'gfft_dummy', 'pyfftw']:
if RG_GC.validQ('fft_module', name):
rg_rg_fft_modules += [name]
rg_rg_test_shapes = [(128, 128), (179, 179), (512, 512)]
rg_rg_test_spaces = [(GLSpace(8),), (HPSpace(8),), (LMSpace(8),)]
gl_hp_lm_test_spaces = [(GLSpace(8),), (HPSpace(8),), (RGSpace(8),)]
lm_gl_hp_test_spaces = [(LMSpace(8),), (RGSpace(8),)]
###############################################################################
class TestRGRGTransformation(unittest.TestCase):
# all domain/codomain checks
def test_check_codomain_none(self):
x = RGSpace((8, 8))
with assert_raises(ValueError):
transformator.create(x, None)
@parameterized.expand(
rg_rg_test_spaces,
testcase_func_name=custom_name_func
)
def test_check_codomain_mismatch(self, space):
x = RGSpace((8, 8))
with assert_raises(TypeError):
transformator.create(x, space)
@parameterized.expand(
itertools.product([0, 1, 2], [None, (1, 1), (10, 10)], [False, True]),
testcase_func_name=custom_name_func
)
def test_check_codomain_rgspecific(self, complexity, distances, harmonic):
x = RGSpace((8, 8), complexity=complexity,
distances=distances, harmonic=harmonic)
assert (RGRGTransformation.check_codomain(x, x.get_codomain()))
assert (RGRGTransformation.check_codomain(x, x.get_codomain()))
@parameterized.expand(rg_rg_fft_modules, testcase_func_name=custom_name_func)
def test_shapemismatch(self, module):
x = RGSpace((8, 8))
b = d2o.distributed_data_object(np.ones((8, 8)))
with assert_raises(ValueError):
transformator.create(
x, x.get_codomain(), module=module
).transform(b, axes=(0, 1, 2))
@parameterized.expand(
itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
testcase_func_name=custom_name_func
)
def test_local_ndarray(self, module, shape):
x = RGSpace(shape)
a = np.ones(shape)
assert np.allclose(
transformator.create(
x, x.get_codomain(), module=module
).transform(a),
weighted_np_transform(a, x, x.get_codomain())
)
@parameterized.expand(
itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
testcase_func_name=custom_name_func
)
def test_local_notzero(self, module, shape):
x = RGSpace(shape[0]) # all tests along axis 1
a = np.ones(shape)
b = d2o.distributed_data_object(a)
assert np.allclose(
transformator.create(
x, x.get_codomain(), module=module
).transform(b, axes=(1,)),
weighted_np_transform(a, x, x.get_codomain(), axes=(1,))
)
@parameterized.expand(
itertools.product(rg_rg_fft_modules, rg_rg_test_shapes),
testcase_func_name=custom_name_func
)
def test_not(self, module, shape):
x = RGSpace(shape)
a = np.ones(shape)
b = d2o.distributed_data_object(a, distribution_strategy='not')
assert np.allclose(
transformator.create(
x, x.get_codomain(), module=module
).transform(b),
weighted_np_transform(a, x, x.get_codomain())
)
@parameterized.expand(
itertools.product(rg_rg_test_shapes),
testcase_func_name=custom_name_func
)
def test_mpi_axesnone(self, shape):
x = RGSpace(shape)
a = np.ones(shape)
b = d2o.distributed_data_object(a)
assert np.allclose(
transformator.create(
x, x.get_codomain(), module='pyfftw'
).transform(b),
weighted_np_transform(a, x, x.get_codomain())
)
@parameterized.expand(
itertools.product(rg_rg_test_shapes),
testcase_func_name=custom_name_func
)
def test_mpi_axesnone_equal(self, shape):
x = RGSpace(shape)
a = np.ones(shape)
b = d2o.distributed_data_object(a, distribution_strategy='equal')
assert np.allclose(
transformator.create(
x, x.get_codomain(), module='pyfftw'
).transform(b),
weighted_np_transform(a, x, x.get_codomain())
)
class TestGLLMTransformation(unittest.TestCase):
# all domain/codomain checks
def test_check_codomain_none(self):
x = GLSpace(8)
with assert_raises(ValueError):
transformator.create(x, None)
@parameterized.expand(
gl_hp_lm_test_spaces,
testcase_func_name=custom_name_func
)
def test_check_codomain_mismatch(self, space):
x = GLSpace(8)
with assert_raises(TypeError):
transformator.create(x, space)
class TestHPLMTransformation(unittest.TestCase):
# all domain/codomain checks
def test_check_codomain_none(self):
x = HPSpace(8)
with assert_raises(ValueError):
transformator.create(x, None)
@parameterized.expand(
gl_hp_lm_test_spaces,
testcase_func_name=custom_name_func
)
def test_check_codomain_mismatch(self, space):
x = GLSpace(8)
with assert_raises(TypeError):
transformator.create(x, space)
class TestLMTransformation(unittest.TestCase):
# all domain/codomain checks
def test_check_codomain_none(self):
x = LMSpace(8)
with assert_raises(ValueError):
transformator.create(x, None)
@parameterized.expand(
lm_gl_hp_test_spaces,
testcase_func_name=custom_name_func
)
def test_check_codomain_mismatch(self, space):
x = LMSpace(8)
with assert_raises(ValueError):
transformator.create(x, space)
if __name__ == '__main__':
unittest.main()
from transformation_factory import TransformationFactory
tf = TransformationFactory()
import numpy as np
from transformation import Transformation
from d2o import distributed_data_object
from nifty.config import dependency_injector as gdi
import nifty.nifty_utilities as utilities
from nifty import GLSpace, LMSpace
gl = gdi.get('libsharp_wrapper_gl')
class GLLMTransformation(Transformation):
def __init__(self, domain, codomain, module=None):
if 'libsharp_wrapper_gl' not in gdi:
raise ImportError("The module libsharp is needed but not available")
if self.check_codomain(domain, codomain):
self.domain = domain
self.codomain = codomain
else:
raise ValueError("ERROR: Incompatible codomain!")
@staticmethod
def check_codomain(domain, codomain):
if not isinstance(domain, GLSpace):
raise TypeError('ERROR: domain is not a GLSpace')
if codomain is None:
return False
if not isinstance(codomain, LMSpace):
raise TypeError('ERROR: codomain must be a LMSpace.')
nlat = domain.paradict['nlat']
nlon = domain.paradict['nlon']
lmax = codomain.paradict['lmax']
mmax = codomain.paradict['mmax']
if (nlon != 2 * nlat - 1) or (lmax != nlat - 1) or (lmax != mmax):
return False
return True
def transform(self, val, axes=None, **kwargs):
"""
GL -> LM transform method.
Parameters
----------
val : np.ndarray or distributed_data_object
The value array which is to be transformed
axes : None or tuple
The axes along which the transformation should take place
"""
if self.domain.discrete:
val = self.domain.calc_weight(val, power=-0.5)
# shorthands for transform parameters
nlat = self.domain.paradict['nlat']
nlon = self.domain.paradict['nlon']
lmax = self.codomain.paradict['lmax']
mmax = self.codomain.paradict['mmax']
if isinstance(val, distributed_data_object):
temp_val = val.get_full_data()
else:
temp_val = val
return_val = None
for slice_list in utilities.get_slice_list(temp_val.shape, axes):
if slice_list == [slice(None, None)]:
inp = temp_val
else:
if return_val is None:
return_val = np.empty_like(temp_val)
inp = temp_val[slice_list]
if self.domain.dtype == np.dtype('float32'):
inp = gl.map2alm_f(inp,
nlat=nlat, nlon=nlon,
lmax=lmax, mmax=mmax)
else:
inp = gl.map2alm(inp,
nlat=nlat, nlon=nlon,
lmax=lmax, mmax=mmax)
if slice_list == [slice(None, None)]:
return_val = inp
else:
return_val[slice_list] = inp
if isinstance(val, distributed_data_object):
new_val = val.copy_empty(dtype=self.codomain.dtype)
new_val.set_full_data(return_val, copy=False)
else:
return_val = return_val.astype(self.codomain.dtype, copy=False)
return return_val
import numpy as np
from transformation import Transformation
from d2o import distributed_data_object
from nifty.config import dependency_injector as gdi
import nifty.nifty_utilities as utilities
from nifty import HPSpace, LMSpace
hp = gdi.get('healpy')
class HPLMTransformation(Transformation):
def __init__(self, domain, codomain, module=None):
if 'healpy' not in gdi:
raise ImportError("The module healpy is needed but not available")
if self.check_codomain(domain, codomain):
self.domain = domain
self.codomain = codomain
else:
raise ValueError("ERROR: Incompatible codomain!")
@staticmethod
def check_codomain(domain, codomain):
if not isinstance(domain, HPSpace):
raise TypeError('ERROR: domain is not a HPSpace')
if codomain is None:
return False
if not isinstance(codomain, LMSpace):
raise TypeError('ERROR: codomain must be a LMSpace.')
nside = domain.paradict['nside']
lmax = codomain.paradict['lmax']
mmax = codomain.paradict['mmax']
if (3 * nside - 1 != lmax) or (lmax != mmax):
return False
return True
def transform(self, val, axes=None, **kwargs):
"""
HP -> LM transform method.
Parameters
----------
val : np.ndarray or distributed_data_object
The value array which is to be transformed
axes : None or tuple
The axes along which the transformation should take place
"""
# get by number of iterations from kwargs
niter = kwargs['niter'] if 'niter' in kwargs else 0
if self.domain.discrete:
val = self.domain.calc_weight(val, power=-0.5)
# shorthands for transform parameters
lmax = self.codomain.paradict['lmax']
mmax = self.codomain.paradict['mmax']
if isinstance(val, distributed_data_object):
temp_val = val.get_full_data()
else:
temp_val = val
return_val = None
for slice_list in utilities.get_slice_list(temp_val.shape, axes):
if slice_list == [slice(None, None)]:
inp = temp_val
else:
if return_val is None:
return_val = np.empty_like(temp_val)
inp = temp_val[slice_list]
inp = hp.map2alm(inp.astype(np.float64, copy=False),
lmax=lmax, mmax=mmax, iter=niter, pol=True,
use_weights=False, datapath=None)
if slice_list == [slice(None, None)]:
return_val = inp
else:
return_val[slice_list] = inp
if isinstance(val, distributed_data_object):
new_val = val.copy_empty(dtype=self.codomain.dtype)
new_val.set_full_data(return_val, copy=False)
else:
return_val = return_val.astype(self.codomain.dtype, copy=False)
return return_val
import numpy as np
from transformation import Transformation
from d2o import distributed_data_object
from nifty.config import dependency_injector as gdi
import nifty.nifty_utilities as utilities
from nifty import GLSpace, LMSpace
gl = gdi.get('libsharp_wrapper_gl')
class LMGLTransformation(Transformation):
def __init__(self, domain, codomain, module=None):
if gdi.get('libsharp_wrapper_gl') is None:
raise ImportError(
"The module libsharp is needed but not available.")
if self.check_codomain(domain, codomain):
self.domain = domain
self.codomain = codomain
else:
raise ValueError("ERROR: Incompatible codomain!")
@staticmethod
def check_codomain(domain, codomain):
if not isinstance(domain, LMSpace):
raise TypeError('ERROR: domain is not a LMSpace')
if codomain is None:
return False
if not isinstance(codomain, GLSpace):
raise TypeError('ERROR: codomain must be a GLSpace.')
nlat = codomain.paradict['nlat']
nlon = codomain.paradict['nlon']
lmax = domain.paradict['lmax']