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

remove unused code

parent 1da76afb
......@@ -32,7 +32,6 @@ from .operators.harmonic_operators import (
HarmonicSmoothingOperator)
from .operators.field_zero_padder import FieldZeroPadder
from .operators.inversion_enabler import InversionEnabler
from .operators.laplace_operator import LaplaceOperator
from .operators.linear_operator import LinearOperator
from .operators.mask_operator import MaskOperator
from .operators.offset_operator import OffsetOperator
......@@ -42,7 +41,6 @@ from .operators.sampling_enabler import SamplingEnabler
from .operators.sandwich_operator import SandwichOperator
from .operators.scaling_operator import ScalingOperator
from .operators.slope_operator import SlopeOperator
from .operators.smoothness_operator import SmoothnessOperator
from .operators.symmetrizing_operator import SymmetrizingOperator
from .operators.block_diagonal_operator import BlockDiagonalOperator
from .operators.outer_product_operator import OuterProduct
......
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from __future__ import absolute_import, division, print_function
import numpy as np
from .. import dobj
from ..compat import *
from ..domain_tuple import DomainTuple
from ..domains.power_space import PowerSpace
from ..field import Field
from ..utilities import infer_space
from .endomorphic_operator import EndomorphicOperator
class LaplaceOperator(EndomorphicOperator):
"""An irregular LaplaceOperator with free boundary and excluding monopole.
This LaplaceOperator implements the second derivative of a Field in
PowerSpace on logarithmic or linear scale with vanishing curvature at the
boundary, starting at the second entry of the Field. The second derivative
of the Field on the irregular grid is calculated using finite differences.
Parameters
----------
logarithmic : bool, optional
Whether smoothness is calculated on a logarithmic scale or linear scale
default : True
space : int
The index of the domain on which the operator acts
"""
def __init__(self, domain, space=None, logarithmic=True):
self._domain = DomainTuple.make(domain)
self._capability = self.TIMES | self.ADJOINT_TIMES
self._space = infer_space(self._domain, space)
if not isinstance(self._domain[self._space], PowerSpace):
raise ValueError("Operator must act on a PowerSpace.")
pos = self.domain[self._space].k_lengths.copy()
if logarithmic:
pos[1:] = np.log(pos[1:])
pos[0] = pos[1]-1.
self._dpos = pos[1:]-pos[:-1] # defined between points
# centered distances (also has entries for the first and last point
# for convenience, but they will never affect the result)
self._dposc = np.empty_like(pos)
self._dposc[:-1] = self._dpos
self._dposc[-1] = 0.
self._dposc[1:] += self._dpos
self._dposc *= 0.5
def apply(self, x, mode):
self._check_input(x, mode)
axes = x.domain.axes[self._space]
axis = axes[0]
nval = len(self._dposc)
prefix = (slice(None),) * axis
sl_l = prefix + (slice(None, -1),) # "left" slice
sl_r = prefix + (slice(1, None),) # "right" slice
dpos = self._dpos.reshape((1,)*axis + (nval-1,))
dposc = self._dposc.reshape((1,)*axis + (nval,))
v, val = dobj.ensure_not_distributed(x.val, (axis,))
ret = np.empty_like(val)
if mode == self.TIMES:
deriv = (val[sl_r]-val[sl_l])/dpos # defined between points
ret[sl_l] = deriv
ret[prefix + (-1,)] = 0.
ret[sl_r] -= deriv
ret /= dposc
ret[prefix + (slice(None, 2),)] = 0.
ret[prefix + (-1,)] = 0.
else:
val = val/dposc
val[prefix + (slice(None, 2),)] = 0.
val[prefix + (-1,)] = 0.
deriv = (val[sl_r]-val[sl_l])/dpos # defined between points
ret[sl_l] = deriv
ret[prefix + (-1,)] = 0.
ret[sl_r] -= deriv
ret = dobj.from_local_data(x.shape, ret, dobj.distaxis(v))
return Field(self.domain, dobj.ensure_default_distributed(ret))
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
from __future__ import absolute_import, division, print_function
from ..compat import *
from .laplace_operator import LaplaceOperator
from .scaling_operator import ScalingOperator
def SmoothnessOperator(domain, strength=1., logarithmic=True, space=None):
"""An operator measuring the smoothness on an irregular grid with respect
to some scale.
This operator applies the irregular LaplaceOperator and its adjoint to some
Field over a PowerSpace which corresponds to its smoothness and weights the
result with a scale parameter sigma. For this purpose we use free boundary
conditions in the LaplaceOperator, having no curvature at both ends. In
addition the first entry is ignored as well, corresponding to the overall
mean of the map. The mean is therefore not considered in the smoothness
prior.
Parameters
----------
domain : Domain, tuple of Domain, or DomainTuple
The total domain of the operator's input and output fields
strength : nonnegative float
Specifies the strength of the SmoothnessOperator
logarithmic : bool, optional
Whether smoothness is calculated on a logarithmic scale or linear scale
default : True
space : int, optional
The index of the sub-domain on which the operator acts.
Can be omitted if `domain` only has one sub-domain.
"""
if strength < 0:
raise ValueError("ERROR: strength must be nonnegative.")
if strength == 0.:
return ScalingOperator(0., domain)
laplace = LaplaceOperator(domain, logarithmic=logarithmic, space=space)
return laplace.adjoint(laplace).scale(strength**2)
......@@ -170,16 +170,6 @@ class Consistency_Tests(unittest.TestCase):
dtype=dtype))
ift.extra.consistency_check(op, dtype, dtype)
@expand(product(_pow_spaces, [np.float64, np.complex128]))
def testLaplace(self, sp, dtype):
op = ift.LaplaceOperator(sp)
ift.extra.consistency_check(op, dtype, dtype)
@expand(product(_pow_spaces, [np.float64, np.complex128]))
def testSmoothness(self, sp, dtype):
op = ift.SmoothnessOperator(sp)
ift.extra.consistency_check(op, dtype, dtype)
@expand(product(_h_spaces+_p_spaces+_pow_spaces,
[np.float64, np.complex128]))
def testGeometryRemover(self, sp, dtype):
......
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
import unittest
from itertools import product
from test.common import expand
import nifty5 as ift
import numpy as np
from numpy.testing import assert_allclose
class LaplaceOperatorTests(unittest.TestCase):
@expand(product([None, False, True], [False, True], [10, 100, 1000]))
def test_Laplace(self, log1, log2, sz):
s = ift.RGSpace(sz, harmonic=True)
bb = ift.PowerSpace.useful_binbounds(s, logarithmic=log1)
p = ift.PowerSpace(s, binbounds=bb)
L = ift.LaplaceOperator(p, logarithmic=log2)
fp = ift.Field.from_random("normal", domain=p, dtype=np.float64)
assert_allclose(L(fp).vdot(L(fp)), L.adjoint_times(L(fp)).vdot(fp))
@expand(product([10, 100, 1000]))
def test_Laplace2(self, sz):
s = ift.RGSpace(sz, harmonic=True, distances=0.764)
bb = ift.PowerSpace.useful_binbounds(s, logarithmic=False)
p = ift.PowerSpace(s, binbounds=bb)
foo = ift.PS_field(p, lambda k: 2*k**2)
L = ift.LaplaceOperator(p, logarithmic=False)
result = np.full(p.shape, 2*2.)
result[0] = result[1] = result[-1] = 0.
assert_allclose(L(foo).to_global_data(), result)
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