Commit a9c0d78c authored by Lukas Platz's avatar Lukas Platz
Browse files

replace FieldZeroPadder, build CentralFieldZeroPadder from old FieldZeroPadder

parent fc6f5a7a
Pipeline #107084 passed with stages
in 20 minutes and 46 seconds
......@@ -32,7 +32,7 @@ from .operators.endomorphic_operator import EndomorphicOperator
from .operators.harmonic_operators import (
FFTOperator, HartleyOperator, SHTOperator, HarmonicTransformOperator,
HarmonicSmoothingOperator)
from .operators.field_zero_padder import FieldZeroPadder, OffsetFieldZeroPadder
from .operators.field_zero_padder import FieldZeroPadder, CentralFieldZeroPadder
from .operators.inversion_enabler import InversionEnabler
from .operators.mask_operator import MaskOperator
from .operators.regridding_operator import RegriddingOperator
......
......@@ -24,9 +24,9 @@ from ..field import Field
from .linear_operator import LinearOperator
class FieldZeroPadder(LinearOperator):
"""Operator which applies zero-padding to one of the subdomains of its
input field
class CentralFieldZeroPadder(LinearOperator):
"""Operator which applies central zero-padding to one of the subdomains
of its input field
Parameters
----------
......@@ -39,19 +39,15 @@ class FieldZeroPadder(LinearOperator):
space : int
The index of the subdomain to be zero-padded. If None, it is set to 0
if domain contains exactly one space. domain[space] must be an RGSpace.
central : bool
If `False`, padding is performed at the end of the domain axes,
otherwise in the middle.
Notes
-----
When doing central padding on an axis with an even length, the "central"
entry should in principle be split up; this is currently not done.
split_even : boolean
When set to True and padding on an axis with even length, the
"central" entry will be split up. This is useful for padding in
harmonic spaces.
"""
def __init__(self, domain, new_shape, space=0, central=False):
def __init__(self, domain, new_shape, space=0, split_even=False):
self._domain = DomainTuple.make(domain)
self._space = utilities.infer_space(self._domain, space)
self._central = central
self._split_even = split_even
dom = self._domain[self._space]
if not isinstance(dom, RGSpace):
raise TypeError("RGSpace required")
......@@ -80,41 +76,40 @@ class FieldZeroPadder(LinearOperator):
shp = list(v.shape)
shp[d] = tgtshp[d]
xnew = np.zeros(shp, dtype=v.dtype)
if self._central:
Nyquist = v.shape[d]//2
i1 = idx + (slice(0, Nyquist+1),)
xnew[i1] = v[i1]
i1 = idx + (slice(None, -(Nyquist+1), -1),)
xnew[i1] = v[i1]
# if (v.shape[d] & 1) == 0: # even number of pixels
# i1 = idx+(Nyquist,)
# xnew[i1] *= 0.5
# i1 = idx+(-Nyquist,)
# xnew[i1] *= 0.5
else:
xnew[idx + (slice(0, v.shape[d]),)] = v
Nyquist = v.shape[d]//2
i1 = idx + (slice(0, Nyquist+1),)
xnew[i1] = v[i1]
i1 = idx + (slice(None, -(Nyquist+1), -1),)
xnew[i1] = v[i1]
if self._split_even and (v.shape[d] & 1) == 0:
# even number of pixels
i1 = idx+(Nyquist,)
xnew[i1] *= 0.5
i1 = idx+(-Nyquist,)
xnew[i1] *= 0.5
else: # ADJOINT_TIMES
if self._central:
shp = list(v.shape)
shp[d] = tgtshp[d]
xnew = np.zeros(shp, dtype=v.dtype)
Nyquist = xnew.shape[d]//2
i1 = idx + (slice(0, Nyquist+1),)
xnew[i1] = v[i1]
i1 = idx + (slice(None, -(Nyquist+1), -1),)
xnew[i1] += v[i1]
# if (xnew.shape[d] & 1) == 0: # even number of pixels
# i1 = idx+(Nyquist,)
# xnew[i1] *= 0.5
else:
xnew = v[idx + (slice(0, tgtshp[d]),)]
shp = list(v.shape)
shp[d] = tgtshp[d]
xnew = np.zeros(shp, dtype=v.dtype)
Nyquist = xnew.shape[d]//2
i1 = idx + (slice(0, Nyquist+1),)
xnew[i1] = v[i1]
i1 = idx + (slice(None, -(Nyquist+1), -1),)
xnew[i1] += v[i1]
if self._split_even and (xnew.shape[d] & 1) == 0:
# even number of pixels
i1 = idx+(Nyquist,)
xnew[i1] *= 0.5
curshp[d] = xnew.shape[d]
v = xnew
return Field(self._tgt(mode), v)
class OffsetFieldZeroPadder(LinearOperator):
class FieldZeroPadder(LinearOperator):
"""FieldZeroPadder with choosable offset
Parameters
......
......@@ -208,19 +208,19 @@ def testDomainTupleFieldInserter():
@pmp('space', [0, 2])
@pmp('factor', [1, 2, 2.7])
@pmp('central', [False, True])
def testZeroPadder(space, factor, dtype, central):
@pmp('split_even', [False, True])
def testCentralZeroPadder(space, factor, dtype, split_even):
dom = (ift.RGSpace(4), ift.UnstructuredDomain(5), ift.RGSpace(3, 4),
ift.HPSpace(2))
newshape = [int(factor*ll) for ll in dom[space].shape]
op = ift.FieldZeroPadder(dom, newshape, space, central)
op = ift.CentralFieldZeroPadder(dom, newshape, space, split_even)
ift.extra.check_linear_operator(op, dtype, dtype)
@pmp('space', [0, 2])
@pmp('factor', [1, 2, 2.7])
@pmp('offset', [False, True])
def testOffsetZeroPadder(space, factor, offset, dtype):
def testZeroPadder(space, factor, offset, dtype):
dom = (ift.RGSpace(4), ift.UnstructuredDomain(5), ift.RGSpace(3, 4),
ift.HPSpace(2))
newshape = [int(factor*ll) for ll in dom[space].shape]
......@@ -230,7 +230,7 @@ def testOffsetZeroPadder(space, factor, offset, dtype):
else:
offset_val = None
op = ift.OffsetFieldZeroPadder(dom, newshape, space, offset_val)
op = ift.FieldZeroPadder(dom, newshape, space, offset_val)
ift.extra.check_linear_operator(op, dtype, dtype)
......
......@@ -172,18 +172,18 @@ def testDomainTupleFieldInserter():
@pmp('space', [0, 2])
@pmp('factor', [1, 2, 2.7])
@pmp('central', [False, True])
def testZeroPadder(space, factor, dtype, central):
@pmp('split_even', [False, True])
def testCentralZeroPadder(space, factor, dtype, split_even):
dom = (ift.RGSpace(10), ift.UnstructuredDomain(13), ift.RGSpace(7, 12),
ift.HPSpace(4))
newshape = [int(factor*l) for l in dom[space].shape]
_check_repr(ift.FieldZeroPadder(dom, newshape, space, central))
_check_repr(ift.CentralFieldZeroPadder(dom, newshape, space, split_even))
@pmp('space', [0, 2])
@pmp('factor', [1, 2, 2.7])
@pmp('offset', [False, True])
def testOffsetZeroPadder(space, factor, offset, dtype):
def testZeroPadder(space, factor, offset, dtype):
dom = (ift.RGSpace(4), ift.UnstructuredDomain(5), ift.RGSpace(3, 4),
ift.HPSpace(2))
newshape = [int(factor*ll) for ll in dom[space].shape]
......@@ -193,7 +193,7 @@ def testOffsetZeroPadder(space, factor, offset, dtype):
else:
offset_val = None
op = ift.OffsetFieldZeroPadder(dom, newshape, space, offset_val)
op = ift.FieldZeroPadder(dom, newshape, space, offset_val)
_check_repr(op)
......
Supports Markdown
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