Commit f98653ce authored by Martin Reinecke's avatar Martin Reinecke
Browse files

stage2

parent eda70a19
......@@ -167,7 +167,7 @@ class RGSpace(StructuredDomain):
raise NotImplementedError
op = HarmonicTransformOperator(self, self.get_default_codomain())
dist = op.target[0]._get_dist_array()
kernel = Field.from_local_data(op.target, func(dist.local_data))
kernel = Field(op.target, func(dist.val))
kernel = kernel / kernel.integrate()
return op.adjoint_times(kernel.weight(1))
......
......@@ -32,8 +32,7 @@ __all__ = ["consistency_check", "check_jacobian_consistency",
def assert_allclose(f1, f2, atol, rtol):
if isinstance(f1, Field):
return np.testing.assert_allclose(f1.local_data, f2.local_data,
atol=atol, rtol=rtol)
return np.testing.assert_allclose(f1.val, f2.val, atol=atol, rtol=rtol)
for key, val in f1.items():
assert_allclose(val, f2[key], atol=atol, rtol=rtol)
......
......@@ -67,7 +67,7 @@ class InverseGammaOperator(Operator):
def apply(self, x):
self._check_input(x)
lin = isinstance(x, Linearization)
val = x.val.local_data if lin else x.local_data
val = x.val.val if lin else x.val
val = (np.clip(val, self._xmin, self._xmax) - self._xmin) / self._delta
......@@ -76,26 +76,26 @@ class InverseGammaOperator(Operator):
w = val - fi
res = np.exp((1 - w)*self._table[fi] + w*self._table[fi + 1])
points = Field.from_local_data(self._domain, res)
points = Field(self._domain, res)
if not lin:
return points
# Derivative of linear interpolation
der = self._deriv[fi]*res
jac = makeOp(Field.from_local_data(self._domain, der))
jac = makeOp(Field(self._domain, der))
jac = jac(x.jac)
return x.new(points, jac)
@staticmethod
def IG(field, alpha, q):
foo = invgamma.ppf(norm.cdf(field.local_data), alpha, scale=q)
return Field.from_local_data(field.domain, foo)
foo = invgamma.ppf(norm.cdf(field.val), alpha, scale=q)
return Field(field.domain, foo)
@staticmethod
def inverseIG(u, alpha, q):
res = norm.ppf(invgamma.cdf(u.local_data, alpha, scale=q))
return Field.from_local_data(u.domain, res)
res = norm.ppf(invgamma.cdf(u.val, alpha, scale=q))
return Field(u.domain, res)
@property
def alpha(self):
......
......@@ -227,9 +227,9 @@ class LOSResponse(LinearOperator):
def apply(self, x, mode):
self._check_input(x, mode)
if mode == self.TIMES:
result_arr = self._smat.matvec(x.local_data.reshape(-1))
result_arr = self._smat.matvec(x.val.reshape(-1))
return Field.from_global_data(self._target, result_arr,
sum_up=True)
local_input_data = x.to_global_data().reshape(-1)
res = self._smat.rmatvec(local_input_data).reshape(self._local_shape)
return Field.from_local_data(self._domain, res)
return Field(self._domain, res)
......@@ -320,10 +320,10 @@ class Linearization(object):
def sinc(self):
tmp = self._val.sinc()
tmp2 = ((np.pi*self._val).cos()-tmp)/self._val
ind = self._val.local_data == 0
loc = tmp2.local_data.copy()
ind = self._val.val == 0
loc = tmp2.val.copy()
loc[ind] = 0
tmp2 = Field.from_local_data(tmp.domain, loc)
tmp2 = Field(tmp.domain, loc)
return self.new(tmp, makeOp(tmp2)(self._jac))
def log(self):
......@@ -370,10 +370,10 @@ class Linearization(object):
tmp = self._val.absolute()
tmp2 = self._val.sign()
ind = self._val.local_data == 0
loc = tmp2.local_data.copy().astype(float)
ind = self._val.val == 0
loc = tmp2.val.copy().astype(float)
loc[ind] = np.nan
tmp2 = Field.from_local_data(tmp.domain, loc)
tmp2 = Field(tmp.domain, loc)
return self.new(tmp, makeOp(tmp2)(self._jac))
......
......@@ -47,7 +47,7 @@ class EnergyAdapter(Energy):
self._want_metric = want_metric
lin = Linearization.make_partial_var(position, constants, want_metric)
tmp = self._op(lin)
self._val = tmp.val.local_data[()]
self._val = tmp.val.val[()]
self._grad = tmp.gradient
self._metric = tmp._metric
......
......@@ -111,10 +111,10 @@ class MetricGaussianKL(Energy):
for s in self._samples:
tmp = self._hamiltonian(self._lin+s)
if v is None:
v = tmp.val.local_data[()]
v = tmp.val.val[()]
g = tmp.gradient
else:
v += tmp.val.local_data[()]
v += tmp.val.val[()]
g = g + tmp.gradient
self._val = v / len(self._samples)
self._grad = g * (1./len(self._samples))
......
......@@ -51,10 +51,9 @@ def np_allreduce_sum(arr):
def allreduce_sum_field(fld):
if isinstance(fld, Field):
return Field.from_local_data(fld.domain,
np_allreduce_sum(fld.local_data))
return Field(fld.domain, np_allreduce_sum(fld.val))
res = tuple(
Field.from_local_data(f.domain, np_allreduce_sum(f.local_data))
Field(f.domain, np_allreduce_sum(f.val))
for f in fld.values())
return MultiField(fld.domain, res)
......@@ -183,16 +182,16 @@ class MetricGaussianKL_MPI(Energy):
v, g = None, None
if len(self._samples) == 0: # hack if there are too many MPI tasks
tmp = self._hamiltonian(self._lin)
v = 0. * tmp.val.local_data
v = 0. * tmp.val.val
g = 0. * tmp.gradient
else:
for s in self._samples:
tmp = self._hamiltonian(self._lin+s)
if v is None:
v = tmp.val.local_data.copy()
v = tmp.val.val.copy()
g = tmp.gradient
else:
v += tmp.val.local_data
v += tmp.val.val
g = g + tmp.gradient
self._val = np_allreduce_sum(v)[()] / self._n_samples
self._grad = allreduce_sum_field(g) / self._n_samples
......
......@@ -32,7 +32,7 @@ def _multiToArray(fld):
ofs = 0
for val in fld.values():
sz2 = 2*val.size if iscomplextype(val.dtype) else val.size
locdat = val.local_data.reshape(-1)
locdat = val.val.reshape(-1)
if iscomplextype(val.dtype):
locdat = locdat.view(locdat.real.dtype)
res[ofs:ofs+sz2] = locdat
......@@ -42,20 +42,19 @@ def _multiToArray(fld):
def _toArray(fld):
if isinstance(fld, Field):
return fld.local_data.reshape(-1)
return fld.val.reshape(-1)
return _multiToArray(fld)
def _toArray_rw(fld):
if isinstance(fld, Field):
return fld.local_data.copy().reshape(-1)
return fld.val.copy().reshape(-1)
return _multiToArray(fld)
def _toField(arr, template):
if isinstance(template, Field):
return Field.from_local_data(template.domain,
arr.reshape(template.shape).copy())
return Field(template.domain, arr.reshape(template.shape).copy())
ofs = 0
res = []
for v in template.values():
......@@ -63,7 +62,7 @@ def _toField(arr, template):
locdat = arr[ofs:ofs+sz2].copy()
if iscomplextype(v.dtype):
locdat = locdat.view(np.complex128)
res.append(Field.from_local_data(v.domain, locdat.reshape(v.shape)))
res.append(Field(v.domain, locdat.reshape(v.shape)))
ofs += sz2
return MultiField(template.domain, tuple(res))
......
......@@ -73,9 +73,9 @@ class _DomRemover(LinearOperator):
@staticmethod
def _check_float_dtype(fld):
if isinstance(fld, MultiField):
dts = [ff.local_data.dtype for ff in fld.values()]
dts = [ff.dtype for ff in fld.values()]
elif isinstance(fld, Field):
dts = [fld.local_data.dtype]
dts = [fld.dtype]
else:
raise TypeError
for dt in dts:
......
......@@ -54,13 +54,13 @@ class ContractionOperator(LinearOperator):
def apply(self, x, mode):
self._check_input(x, mode)
if mode == self.ADJOINT_TIMES:
ldat = x.to_global_data() if 0 in self._spaces else x.local_data
ldat = x.val
shp = []
for i, dom in enumerate(self._domain):
tmp = dom.shape if i > 0 else dom.local_shape
tmp = dom.shape
shp += tmp if i not in self._spaces else (1,)*len(dom.shape)
ldat = np.broadcast_to(ldat.reshape(shp), self._domain.local_shape)
res = Field.from_local_data(self._domain, ldat)
ldat = np.broadcast_to(ldat.reshape(shp), self._domain.shape)
res = Field(self._domain, ldat)
if self._weight != 0:
res = res.weight(self._weight, spaces=self._spaces)
return res
......
......@@ -130,15 +130,15 @@ class DiagonalOperator(EndomorphicOperator):
self._check_input(x, mode)
# shortcut for most common cases
if mode == 1 or (not self._complex and mode == 2):
return Field.from_local_data(x.domain, x.local_data*self._ldiag)
return Field(x.domain, x.val*self._ldiag)
xdiag = self._ldiag
if self._complex and (mode & 10): # adjoint or inverse adjoint
xdiag = xdiag.conj()
if mode & 3:
return Field.from_local_data(x.domain, x.local_data*xdiag)
return Field.from_local_data(x.domain, x.local_data/xdiag)
return Field(x.domain, x.val*xdiag)
return Field(x.domain, x.val/xdiag)
def _flip_modes(self, trafo):
if trafo == self.ADJOINT_BIT and not self._complex: # shortcut
......
......@@ -64,18 +64,18 @@ class DOFDistributor(LinearOperator):
if partner != dofdex.domain[0]:
raise ValueError("incorrect dofdex domain")
ldat = dofdex.local_data
ldat = dofdex.val
if ldat.size == 0: # can happen for weird configurations
nbin = 0
else:
nbin = ldat.max()
nbin = nbin + 1
if partner.scalar_dvol is not None:
wgt = np.bincount(dofdex.local_data.ravel(), minlength=nbin)
wgt = np.bincount(dofdex.val.ravel(), minlength=nbin)
wgt = wgt*partner.scalar_dvol
else:
dvol = Field.from_global_data(partner, partner.dvol).local_data
wgt = np.bincount(dofdex.local_data.ravel(),
dvol = Field.from_global_data(partner, partner.dvol).val
wgt = np.bincount(dofdex.val.ravel(),
minlength=nbin, weights=dvol)
# The explicit conversion to float64 is necessary because bincount
# sometimes returns its result as an integer array, even when
......
......@@ -184,7 +184,7 @@ class PoissonianEnergy(EnergyOperator):
def __init__(self, d):
if not isinstance(d, Field) or not np.issubdtype(d.dtype, np.integer):
raise TypeError
if np.any(d.local_data < 0):
if np.any(d.val < 0):
raise ValueError
self._d = d
self._domain = DomainTuple.make(d.domain)
......@@ -192,7 +192,7 @@ class PoissonianEnergy(EnergyOperator):
def apply(self, x):
self._check_input(x)
res = x.sum()
tmp = res.val.local_data if isinstance(res, Linearization) else res
tmp = res.val.val if isinstance(res, Linearization) else res
# if we have no infinity here, we can continue with the calculation;
# otherwise we know that the result must also be infinity
if not np.isinf(tmp):
......@@ -231,8 +231,7 @@ class InverseGammaLikelihood(EnergyOperator):
raise TypeError
self._beta = beta
if np.isscalar(alpha):
alpha = Field.from_local_data(
beta.domain, np.full(beta.local_data.shape, alpha))
alpha = Field(beta.domain, np.full(beta.shape, alpha))
elif not isinstance(alpha, Field):
raise TypeError
self._alphap1 = alpha+1
......@@ -302,7 +301,7 @@ class BernoulliEnergy(EnergyOperator):
def __init__(self, d):
if not isinstance(d, Field) or not np.issubdtype(d.dtype, np.integer):
raise TypeError
if not np.all(np.logical_or(d.local_data == 0, d.local_data == 1)):
if not np.all(np.logical_or(d.val == 0, d.val == 1)):
raise ValueError
self._d = d
self._domain = DomainTuple.make(d.domain)
......
......@@ -42,7 +42,7 @@ class VdotOperator(LinearOperator):
self._check_mode(mode)
if mode == self.TIMES:
return Field.scalar(self._field.vdot(x))
return self._field*x.local_data[()]
return self._field*x.val[()]
class ConjugationOperator(EndomorphicOperator):
......
......@@ -51,10 +51,10 @@ class ValueInserter(LinearOperator):
def apply(self, x, mode):
self._check_input(x, mode)
x = x.to_global_data()
x = x.val
if mode == self.TIMES:
res = np.zeros(self.target.shape, dtype=x.dtype)
res[self._index] = x
return Field.from_global_data(self._tgt(mode), res)
return Field(self._tgt(mode), res)
else:
return Field.scalar(x[self._index])
......@@ -56,7 +56,7 @@ def test_inverse_gamma(field):
field = field.exp()
space = field.domain
d = np.random.normal(10, size=space.shape)**2
d = ift.Field.from_global_data(space, d)
d = ift.Field(space, d)
energy = ift.InverseGammaLikelihood(d)
ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)
......@@ -65,7 +65,7 @@ def testPoissonian(field):
field = field.exp()
space = field.domain
d = np.random.poisson(120, size=space.shape)
d = ift.Field.from_global_data(space, d)
d = ift.Field(space, d)
energy = ift.PoissonianEnergy(d)
ift.extra.check_jacobian_consistency(energy, field, tol=1e-7)
......@@ -86,6 +86,6 @@ def test_bernoulli(field):
field = field.sigmoid()
space = field.domain
d = np.random.binomial(1, 0.1, size=space.shape)
d = ift.Field.from_global_data(space, d)
d = ift.Field(space, d)
energy = ift.BernoulliEnergy(d)
ift.extra.check_jacobian_consistency(energy, field, tol=1e-5)
......@@ -68,8 +68,8 @@ def test_quadratic_minimization(minimizer, space):
assert_equal(convergence, IC.CONVERGED)
assert_allclose(
energy.position.local_data,
1./covariance_diagonal.local_data,
energy.position.val,
1./covariance_diagonal.val,
rtol=1e-3,
atol=1e-3)
......@@ -90,8 +90,8 @@ def test_WF_curvature(space):
iteration_controller_sampling=IC)
m = curv.inverse(required_result)
assert_allclose(
m.local_data,
1./all_diag.local_data,
m.val,
1./all_diag.val,
rtol=1e-3,
atol=1e-3)
curv.draw_sample()
......@@ -107,8 +107,8 @@ def test_WF_curvature(space):
iteration_controller_sampling=IC)
m = curv.inverse(required_result)
assert_allclose(
m.local_data,
1./all_diag.local_data,
m.val,
1./all_diag.val,
rtol=1e-3,
atol=1e-3)
curv.draw_sample()
......@@ -131,26 +131,26 @@ def test_rosenbrock(minimizer):
@property
def value(self):
return rosen(self._position.to_global_data_rw())
return rosen(self._position.val.copy())
@property
def gradient(self):
inp = self._position.to_global_data_rw()
out = ift.Field.from_global_data(space, rosen_der(inp))
inp = self._position.val.copy()
out = ift.Field(space, rosen_der(inp))
return out
@property
def metric(self):
class RBCurv(ift.EndomorphicOperator):
def __init__(self, loc):
self._loc = loc.to_global_data_rw()
self._loc = loc.val.copy()
self._capability = self.TIMES
self._domain = space
def apply(self, x, mode):
self._check_input(x, mode)
inp = x.to_global_data_rw()
out = ift.Field.from_global_data(
inp = x.val.copy()
out = ift.Field(
space, rosen_hess_prod(self._loc.copy(), inp))
return out
......@@ -159,9 +159,9 @@ def test_rosenbrock(minimizer):
return ift.InversionEnabler(RBCurv(self._position), t1)
def apply_metric(self, x):
inp = x.to_global_data_rw()
pos = self._position.to_global_data_rw()
return ift.Field.from_global_data(space, rosen_hess_prod(pos, inp))
inp = x.val.copy()
pos = self._position.val.copy()
return ift.Field(space, rosen_hess_prod(pos, inp))
try:
minimizer = eval(minimizer)
......@@ -172,7 +172,7 @@ def test_rosenbrock(minimizer):
raise SkipTest
assert_equal(convergence, IC.CONVERGED)
assert_allclose(energy.position.local_data, 1., rtol=1e-3, atol=1e-3)
assert_allclose(energy.position.val, 1., rtol=1e-3, atol=1e-3)
@pmp('minimizer', minimizers + slow_minimizers)
......@@ -186,16 +186,16 @@ def test_gauss(minimizer):
@property
def value(self):
x = self.position.to_global_data()[0]
x = self.position.val[0]
return -np.exp(-(x**2))
@property
def gradient(self):
x = self.position.to_global_data()[0]
x = self.position.val[0]
return ift.Field.full(self.position.domain, 2*x*np.exp(-(x**2)))
def apply_metric(self, x):
p = self.position.to_global_data()[0]
p = self.position.val[0]
v = (2 - 4*p*p)*np.exp(-p**2)
return ift.DiagonalOperator(
ift.Field.full(self.position.domain, v))(x)
......@@ -209,7 +209,7 @@ def test_gauss(minimizer):
raise SkipTest
assert_equal(convergence, IC.CONVERGED)
assert_allclose(energy.position.local_data, 0., atol=1e-3)
assert_allclose(energy.position.val, 0., atol=1e-3)
@pmp('minimizer', minimizers + newton_minimizers + slow_minimizers)
......@@ -223,23 +223,23 @@ def test_cosh(minimizer):
@property
def value(self):
x = self.position.to_global_data()[0]
x = self.position.val[0]
return np.cosh(x)
@property
def gradient(self):
x = self.position.to_global_data()[0]
x = self.position.val[0]
return ift.Field.full(self.position.domain, np.sinh(x))
@property
def metric(self):
x = self.position.to_global_data()[0]
x = self.position.val[0]
v = np.cosh(x)
return ift.DiagonalOperator(
ift.Field.full(self.position.domain, v))
def apply_metric(self, x):
p = self.position.to_global_data()[0]
p = self.position.val[0]
v = np.cosh(p)
return ift.DiagonalOperator(
ift.Field.full(self.position.domain, v))(x)
......@@ -253,4 +253,4 @@ def test_cosh(minimizer):
raise SkipTest
assert_equal(convergence, IC.CONVERGED)
assert_allclose(energy.position.local_data, 0., atol=1e-3)
assert_allclose(energy.position.val, 0., atol=1e-3)
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