Commit 7dd3081f authored by Martin Reinecke's avatar Martin Reinecke
Browse files

testcases working

parent a13b4c77
Pipeline #20793 failed with stage
in 3 minutes and 59 seconds
...@@ -9,7 +9,10 @@ class data_object(object): ...@@ -9,7 +9,10 @@ class data_object(object):
res = self._data[key] res = self._data[key]
return res if np.isscalar(res) else data_object(res) return res if np.isscalar(res) else data_object(res)
def __setitem__(self, key, value): def __setitem__(self, key, value):
self._data[key] = value if isinstance(value, data_object):
self._data[key] = value._data
else:
self._data[key] = value
@property @property
def dtype(self): def dtype(self):
return self._data.dtype return self._data.dtype
......
...@@ -79,13 +79,14 @@ class RGRGTransformation(Transformation): ...@@ -79,13 +79,14 @@ class RGRGTransformation(Transformation):
class SlicingTransformation(Transformation): class SlicingTransformation(Transformation):
def transform(self, val, axes=None): def transform(self, val, axes=None):
val = to_np(val)
return_shape = np.array(val.shape) return_shape = np.array(val.shape)
return_shape[list(axes)] = self.codomain.shape return_shape[list(axes)] = self.codomain.shape
return_val = np.empty(tuple(return_shape), dtype=val.dtype) return_val = np.empty(tuple(return_shape), dtype=val.dtype)
for slice in utilities.get_slice_list(val.shape, axes): for slice in utilities.get_slice_list(val.shape, axes):
return_val[slice] = self._transformation_of_slice(val[slice]) return_val[slice] = self._transformation_of_slice(val[slice])
return return_val, 1. return from_np(return_val), 1.
def _transformation_of_slice(self, inp): def _transformation_of_slice(self, inp):
raise NotImplementedError raise NotImplementedError
......
...@@ -22,6 +22,7 @@ from ..spaces.power_space import PowerSpace ...@@ -22,6 +22,7 @@ from ..spaces.power_space import PowerSpace
from .endomorphic_operator import EndomorphicOperator from .endomorphic_operator import EndomorphicOperator
from .. import DomainTuple from .. import DomainTuple
from .. import nifty_utilities as utilities from .. import nifty_utilities as utilities
from ..dobj import to_ndarray as to_np, from_ndarray as from_np
class LaplaceOperator(EndomorphicOperator): class LaplaceOperator(EndomorphicOperator):
...@@ -89,6 +90,7 @@ class LaplaceOperator(EndomorphicOperator): ...@@ -89,6 +90,7 @@ class LaplaceOperator(EndomorphicOperator):
return self._logarithmic return self._logarithmic
def _times(self, x): def _times(self, x):
val = to_np(x.val)
axes = x.domain.axes[self._space] axes = x.domain.axes[self._space]
axis = axes[0] axis = axes[0]
nval = len(self._dposc) nval = len(self._dposc)
...@@ -97,15 +99,15 @@ class LaplaceOperator(EndomorphicOperator): ...@@ -97,15 +99,15 @@ class LaplaceOperator(EndomorphicOperator):
sl_r = prefix + (slice(1, None),) # "right" slice sl_r = prefix + (slice(1, None),) # "right" slice
dpos = self._dpos.reshape((1,)*axis + (nval-1,)) dpos = self._dpos.reshape((1,)*axis + (nval-1,))
dposc = self._dposc.reshape((1,)*axis + (nval,)) dposc = self._dposc.reshape((1,)*axis + (nval,))
deriv = (x.val[sl_r]-x.val[sl_l])/dpos # defined between points deriv = (val[sl_r]-val[sl_l])/dpos # defined between points
ret = np.empty_like(x.val) ret = np.empty_like(val)
ret[sl_l] = deriv ret[sl_l] = deriv
ret[prefix + (-1,)] = 0. ret[prefix + (-1,)] = 0.
ret[sl_r] -= deriv ret[sl_r] -= deriv
ret /= np.sqrt(dposc) ret /= np.sqrt(dposc)
ret[prefix + (slice(None, 2),)] = 0. ret[prefix + (slice(None, 2),)] = 0.
ret[prefix + (-1,)] = 0. ret[prefix + (-1,)] = 0.
return Field(self.domain, val=ret).weight(-0.5, spaces=self._space) return Field(self.domain, val=from_np(ret)).weight(-0.5, spaces=self._space)
def _adjoint_times(self, x): def _adjoint_times(self, x):
axes = x.domain.axes[self._space] axes = x.domain.axes[self._space]
...@@ -116,13 +118,13 @@ class LaplaceOperator(EndomorphicOperator): ...@@ -116,13 +118,13 @@ class LaplaceOperator(EndomorphicOperator):
sl_r = prefix + (slice(1, None),) # "right" slice sl_r = prefix + (slice(1, None),) # "right" slice
dpos = self._dpos.reshape((1,)*axis + (nval-1,)) dpos = self._dpos.reshape((1,)*axis + (nval-1,))
dposc = self._dposc.reshape((1,)*axis + (nval,)) dposc = self._dposc.reshape((1,)*axis + (nval,))
y = x.weight(0.5, spaces=self._space).val y = to_np(x.weight(0.5, spaces=self._space).val)
y /= np.sqrt(dposc) y /= np.sqrt(dposc)
y[prefix + (slice(None, 2),)] = 0. y[prefix + (slice(None, 2),)] = 0.
y[prefix + (-1,)] = 0. y[prefix + (-1,)] = 0.
deriv = (y[sl_r]-y[sl_l])/dpos # defined between points deriv = (y[sl_r]-y[sl_l])/dpos # defined between points
ret = np.empty_like(x.val) ret = np.empty_like(y)
ret[sl_l] = deriv ret[sl_l] = deriv
ret[prefix + (-1,)] = 0. ret[prefix + (-1,)] = 0.
ret[sl_r] -= deriv ret[sl_r] -= deriv
return Field(self.domain, val=ret).weight(-1, spaces=self._space) return Field(self.domain, val=from_np(ret)).weight(-1, spaces=self._space)
...@@ -67,7 +67,7 @@ class PowerProjectionOperator(LinearOperator): ...@@ -67,7 +67,7 @@ class PowerProjectionOperator(LinearOperator):
pindex = self._target[self._space].pindex pindex = self._target[self._space].pindex
pindex = pindex.reshape((1, pindex.size, 1)) pindex = pindex.reshape((1, pindex.size, 1))
arr = x.val.reshape(x.domain.collapsed_shape_for_domain(self._space)) arr = x.val.reshape(x.domain.collapsed_shape_for_domain(self._space))
out = arr[(slice(None), pindex.ravel()._data, slice(None))] out = arr[(slice(None), dobj.to_ndarray(pindex.ravel()), slice(None))]
return Field(self._domain, out.reshape(self._domain.shape)) return Field(self._domain, out.reshape(self._domain.shape))
@property @property
......
...@@ -141,11 +141,10 @@ class PowerSpace(Space): ...@@ -141,11 +141,10 @@ class PowerSpace(Space):
harmonic_partner=self.harmonic_partner, harmonic_partner=self.harmonic_partner,
k_length_array=k_length_array, k_length_array=k_length_array,
binbounds=binbounds) binbounds=binbounds)
temp_rho = dobj.bincount(temp_pindex.ravel()) temp_rho = dobj.to_ndarray(dobj.bincount(temp_pindex.ravel()))
assert not (temp_rho == 0).any(), "empty bins detected" assert not (temp_rho == 0).any(), "empty bins detected"
temp_k_lengths = dobj.bincount(temp_pindex.ravel(), temp_k_lengths = dobj.to_ndarray(dobj.bincount(temp_pindex.ravel(),
weights=dobj.from_ndarray(k_length_array.ravel())) \ weights=dobj.from_ndarray(k_length_array.ravel())) / temp_rho)
/ temp_rho
temp_dvol = temp_rho*pdvol temp_dvol = temp_rho*pdvol
self._powerIndexCache[key] = (binbounds, self._powerIndexCache[key] = (binbounds,
temp_pindex, temp_pindex,
......
...@@ -224,7 +224,7 @@ def create_power_field(domain, power_spectrum, dtype=None): ...@@ -224,7 +224,7 @@ def create_power_field(domain, power_spectrum, dtype=None):
else: else:
power_domain = PowerSpace(domain) power_domain = PowerSpace(domain)
fp = Field(power_domain, fp = Field(power_domain,
val=dobj.from_ndarray(power_spectrum(dobj.to_ndarray(power_domain.k_lengths))), val=dobj.from_ndarray(power_spectrum(power_domain.k_lengths)),
dtype=dtype) dtype=dtype)
P = PowerProjectionOperator(domain, power_domain) P = PowerProjectionOperator(domain, power_domain)
f = P.adjoint_times(fp) f = P.adjoint_times(fp)
......
...@@ -59,10 +59,10 @@ class Test_Functionality(unittest.TestCase): ...@@ -59,10 +59,10 @@ class Test_Functionality(unittest.TestCase):
np.random.seed(11) np.random.seed(11)
p1 = ift.PowerSpace(space1) p1 = ift.PowerSpace(space1)
fp1 = ift.Field(p1, val=from_np(_spec1(to_np(p1.k_lengths)))) fp1 = ift.Field(p1, val=from_np(_spec1(p1.k_lengths)))
p2 = ift.PowerSpace(space2) p2 = ift.PowerSpace(space2)
fp2 = ift.Field(p2, val=from_np(_spec2(to_np(p2.k_lengths)))) fp2 = ift.Field(p2, val=from_np(_spec2(p2.k_lengths)))
outer = from_np(np.outer(to_np(fp1.val), to_np(fp2.val))) outer = from_np(np.outer(to_np(fp1.val), to_np(fp2.val)))
fp = ift.Field((p1, p2), val=outer) fp = ift.Field((p1, p2), val=outer)
...@@ -91,10 +91,10 @@ class Test_Functionality(unittest.TestCase): ...@@ -91,10 +91,10 @@ class Test_Functionality(unittest.TestCase):
fulldomain = ift.DomainTuple.make((space1, space2)) fulldomain = ift.DomainTuple.make((space1, space2))
p1 = ift.PowerSpace(space1) p1 = ift.PowerSpace(space1)
fp1 = ift.Field(p1, val=from_np(_spec1(to_np(p1.k_lengths)))) fp1 = ift.Field(p1, val=from_np(_spec1(p1.k_lengths)))
p2 = ift.PowerSpace(space2) p2 = ift.PowerSpace(space2)
fp2 = ift.Field(p2, val=from_np(_spec2(to_np(p2.k_lengths)))) fp2 = ift.Field(p2, val=from_np(_spec2(p2.k_lengths)))
S_1 = ift.create_power_field(space1, lambda x: np.sqrt(_spec1(x))) S_1 = ift.create_power_field(space1, lambda x: np.sqrt(_spec1(x)))
S_1 = ift.DiagonalOperator(S_1, domain=fulldomain, spaces=0) S_1 = ift.DiagonalOperator(S_1, domain=fulldomain, spaces=0)
......
import unittest import unittest
from numpy.testing import assert_equal,\ from numpy.testing import assert_equal,\
assert_allclose,\ assert_allclose,\
assert_approx_equal assert_approx_equal
from nifty2go import Field,\ from nifty2go import Field,\
DiagonalOperator,\ DiagonalOperator,\
ComposedOperator ComposedOperator
from test.common import generate_spaces from test.common import generate_spaces
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
class ComposedOperator_Tests(unittest.TestCase): class ComposedOperator_Tests(unittest.TestCase):
spaces = generate_spaces() spaces = generate_spaces()
...@@ -46,5 +44,5 @@ class ComposedOperator_Tests(unittest.TestCase): ...@@ -46,5 +44,5 @@ class ComposedOperator_Tests(unittest.TestCase):
rand1 = Field.from_random('normal', domain=(space1, space2)) rand1 = Field.from_random('normal', domain=(space1, space2))
tt1 = op.inverse_times(op.times(rand1)) tt1 = op.inverse_times(op.times(rand1))
assert_allclose(tt1.val, rand1.val) assert_allclose(to_np(tt1.val), to_np(rand1.val))
from __future__ import division from __future__ import division
import unittest import unittest
import numpy as np import numpy as np
from numpy.testing import assert_equal,\ from numpy.testing import assert_equal,\
assert_allclose,\ assert_allclose,\
assert_approx_equal assert_approx_equal
from nifty2go import Field, DiagonalOperator
from nifty2go import Field,\
DiagonalOperator
from test.common import generate_spaces from test.common import generate_spaces
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
class DiagonalOperator_Tests(unittest.TestCase): class DiagonalOperator_Tests(unittest.TestCase):
spaces = generate_spaces() spaces = generate_spaces()
...@@ -44,7 +40,7 @@ class DiagonalOperator_Tests(unittest.TestCase): ...@@ -44,7 +40,7 @@ class DiagonalOperator_Tests(unittest.TestCase):
diag = Field.from_random('normal', domain=space) diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(diag) D = DiagonalOperator(diag)
tt1 = D.times(D.inverse_times(rand1)) tt1 = D.times(D.inverse_times(rand1))
assert_allclose(rand1.val, tt1.val) assert_allclose(to_np(rand1.val), to_np(tt1.val))
@expand(product(spaces)) @expand(product(spaces))
def test_times(self, space): def test_times(self, space):
...@@ -83,4 +79,4 @@ class DiagonalOperator_Tests(unittest.TestCase): ...@@ -83,4 +79,4 @@ class DiagonalOperator_Tests(unittest.TestCase):
diag = Field.from_random('normal', domain=space) diag = Field.from_random('normal', domain=space)
D = DiagonalOperator(diag) D = DiagonalOperator(diag)
diag_op = D.diagonal() diag_op = D.diagonal()
assert_allclose(diag.val, diag_op.val) assert_allclose(to_np(diag.val), to_np(diag_op.val))
...@@ -29,6 +29,7 @@ from nifty2go import Field,\ ...@@ -29,6 +29,7 @@ from nifty2go import Field,\
FFTOperator FFTOperator
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
def _get_rtol(tp): def _get_rtol(tp):
...@@ -54,7 +55,7 @@ class FFTOperatorTests(unittest.TestCase): ...@@ -54,7 +55,7 @@ class FFTOperatorTests(unittest.TestCase):
inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3, inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3,
dtype=itp) dtype=itp)
out = fft.adjoint_times(fft.times(inp)) out = fft.adjoint_times(fft.times(inp))
assert_allclose(inp.val, out.val, rtol=tol, atol=tol) assert_allclose(to_np(inp.val), to_np(out.val), rtol=tol, atol=tol)
@expand(product([12, 15], [9, 12], [0.1, 1, 3.7], @expand(product([12, 15], [9, 12], [0.1, 1, 3.7],
[0.4, 1, 2.7], [0.4, 1, 2.7],
...@@ -73,7 +74,7 @@ class FFTOperatorTests(unittest.TestCase): ...@@ -73,7 +74,7 @@ class FFTOperatorTests(unittest.TestCase):
inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3, inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3,
dtype=itp) dtype=itp)
out = fft.adjoint_times(fft.times(inp)) out = fft.adjoint_times(fft.times(inp))
assert_allclose(inp.val, out.val, rtol=tol, atol=tol) assert_allclose(to_np(inp.val), to_np(out.val), rtol=tol, atol=tol)
@expand(product([0, 1, 2], @expand(product([0, 1, 2],
[np.float64, np.float32, np.complex64, np.complex128], [np.float64, np.float32, np.complex64, np.complex128],
...@@ -87,7 +88,7 @@ class FFTOperatorTests(unittest.TestCase): ...@@ -87,7 +88,7 @@ class FFTOperatorTests(unittest.TestCase):
inp = Field.from_random(domain=(a1, a2, a3), random_type='normal', inp = Field.from_random(domain=(a1, a2, a3), random_type='normal',
std=7, mean=3, dtype=dtype) std=7, mean=3, dtype=dtype)
out = fft.adjoint_times(fft.times(inp)) out = fft.adjoint_times(fft.times(inp))
assert_allclose(inp.val, out.val, rtol=tol, atol=tol) assert_allclose(to_np(inp.val), to_np(out.val), rtol=tol, atol=tol)
@expand(product([0, 3, 6, 11, 30], @expand(product([0, 3, 6, 11, 30],
[np.float64, np.float32, np.complex64, np.complex128])) [np.float64, np.float32, np.complex64, np.complex128]))
...@@ -99,7 +100,7 @@ class FFTOperatorTests(unittest.TestCase): ...@@ -99,7 +100,7 @@ class FFTOperatorTests(unittest.TestCase):
inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3, inp = Field.from_random(domain=a, random_type='normal', std=7, mean=3,
dtype=tp) dtype=tp)
out = fft.adjoint_times(fft.times(inp)) out = fft.adjoint_times(fft.times(inp))
assert_allclose(inp.val, out.val, rtol=tol, atol=tol) assert_allclose(to_np(inp.val), to_np(out.val), rtol=tol, atol=tol)
@expand(product([128, 256], @expand(product([128, 256],
[np.float64, np.float32, np.complex64, np.complex128])) [np.float64, np.float32, np.complex64, np.complex128]))
...@@ -110,7 +111,7 @@ class FFTOperatorTests(unittest.TestCase): ...@@ -110,7 +111,7 @@ class FFTOperatorTests(unittest.TestCase):
inp = Field.from_random(domain=a, random_type='normal', std=1, mean=0, inp = Field.from_random(domain=a, random_type='normal', std=1, mean=0,
dtype=tp) dtype=tp)
out = fft.adjoint_times(fft.times(inp)) out = fft.adjoint_times(fft.times(inp))
assert_allclose(inp.val, out.val, rtol=1e-3, atol=1e-1) assert_allclose(to_np(inp.val), to_np(out.val), rtol=1e-3, atol=1e-1)
@expand(product([128, 256], @expand(product([128, 256],
[np.float64, np.float32, np.complex64, np.complex128])) [np.float64, np.float32, np.complex64, np.complex128]))
......
...@@ -22,6 +22,7 @@ import nifty2go as ift ...@@ -22,6 +22,7 @@ import nifty2go as ift
from numpy.testing import assert_allclose from numpy.testing import assert_allclose
from itertools import product from itertools import product
from test.common import expand from test.common import expand
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
class LaplaceOperatorTests(unittest.TestCase): class LaplaceOperatorTests(unittest.TestCase):
...@@ -31,6 +32,5 @@ class LaplaceOperatorTests(unittest.TestCase): ...@@ -31,6 +32,5 @@ class LaplaceOperatorTests(unittest.TestCase):
bb = ift.PowerSpace.useful_binbounds(s, logarithmic=log1) bb = ift.PowerSpace.useful_binbounds(s, logarithmic=log1)
p = ift.PowerSpace(s, binbounds=bb) p = ift.PowerSpace(s, binbounds=bb)
L = ift.LaplaceOperator(p, logarithmic=log2) L = ift.LaplaceOperator(p, logarithmic=log2)
arr = np.random.random(p.shape[0]) fp = ift.Field.from_random("normal", domain=p, dtype=np.float64)
fp = ift.Field(p, val=arr)
assert_allclose(L(fp).vdot(L(fp)), L.adjoint_times(L(fp)).vdot(fp)) assert_allclose(L(fp).vdot(L(fp)), L.adjoint_times(L(fp)).vdot(fp))
...@@ -23,9 +23,10 @@ import numpy as np ...@@ -23,9 +23,10 @@ import numpy as np
from numpy.testing import assert_, assert_equal, assert_almost_equal,\ from numpy.testing import assert_, assert_equal, assert_almost_equal,\
assert_raises assert_raises
from nifty2go import PowerSpace, RGSpace, Space, LMSpace from nifty2go import PowerSpace, RGSpace, Space, LMSpace, dobj
from test.common import expand from test.common import expand
from itertools import product, chain from itertools import product, chain
from nifty2go.dobj import to_ndarray as to_np, from_ndarray as from_np
HARMONIC_SPACES = [RGSpace((8,), harmonic=True), HARMONIC_SPACES = [RGSpace((8,), harmonic=True),
RGSpace((7, 8), harmonic=True), RGSpace((7, 8), harmonic=True),
...@@ -54,7 +55,7 @@ CONSTRUCTOR_CONFIGS = [ ...@@ -54,7 +55,7 @@ CONSTRUCTOR_CONFIGS = [
'dim': 5, 'dim': 5,
'harmonic_partner': RGSpace((8,), harmonic=True), 'harmonic_partner': RGSpace((8,), harmonic=True),
'binbounds': None, 'binbounds': None,
'pindex': np.array([0, 1, 2, 3, 4, 3, 2, 1]), 'pindex': from_np(np.array([0, 1, 2, 3, 4, 3, 2, 1])),
'k_lengths': np.array([0., 1., 2., 3., 4.]), 'k_lengths': np.array([0., 1., 2., 3., 4.]),
}], }],
[RGSpace((8,), harmonic=True), True, None, None, { [RGSpace((8,), harmonic=True), True, None, None, {
...@@ -63,7 +64,7 @@ CONSTRUCTOR_CONFIGS = [ ...@@ -63,7 +64,7 @@ CONSTRUCTOR_CONFIGS = [
'dim': 4, 'dim': 4,
'harmonic_partner': RGSpace((8,), harmonic=True), 'harmonic_partner': RGSpace((8,), harmonic=True),
'binbounds': (0.5, 1.3228756555322954, 3.5), 'binbounds': (0.5, 1.3228756555322954, 3.5),
'pindex': np.array([0, 1, 2, 2, 3, 2, 2, 1]), 'pindex': from_np(np.array([0, 1, 2, 2, 3, 2, 2, 1])),
'k_lengths': np.array([0., 1., 2.5, 4.]), 'k_lengths': np.array([0., 1., 2.5, 4.]),
}], }],
] ]
...@@ -80,7 +81,7 @@ class PowerSpaceInterfaceTest(unittest.TestCase): ...@@ -80,7 +81,7 @@ class PowerSpaceInterfaceTest(unittest.TestCase):
@expand([ @expand([
['harmonic_partner', Space], ['harmonic_partner', Space],
['binbounds', type(None)], ['binbounds', type(None)],
['pindex', np.ndarray], ['pindex', dobj.data_object],
['k_lengths', np.ndarray], ['k_lengths', np.ndarray],
]) ])
def test_property_ret_type(self, attribute, expected_type): def test_property_ret_type(self, attribute, expected_type):
...@@ -96,7 +97,7 @@ class PowerSpaceConsistencyCheck(unittest.TestCase): ...@@ -96,7 +97,7 @@ class PowerSpaceConsistencyCheck(unittest.TestCase):
bb = PowerSpace.useful_binbounds(harmonic_partner, logarithmic, nbin) bb = PowerSpace.useful_binbounds(harmonic_partner, logarithmic, nbin)
p = PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb) p = PowerSpace(harmonic_partner=harmonic_partner, binbounds=bb)
assert_equal(np.bincount(p.pindex.ravel()), p.dvol(), assert_equal(np.bincount(to_np(p.pindex.ravel())), p.dvol(),
err_msg='rho is not equal to pindex degeneracy') err_msg='rho is not equal to pindex degeneracy')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment