central_zero_padder.py 2.37 KB
Newer Older
Julia Stadler's avatar
Julia Stadler committed
1
2
3
4
5
6
7
8
9
10
import numpy as np
import itertools

from .. import utilities
from .linear_operator import LinearOperator
from ..domain_tuple import DomainTuple
from ..domains.rg_space import RGSpace
from ..field import Field


Philipp Arras's avatar
Philipp Arras committed
11
class CentralZeroPadder(LinearOperator):
Julia Stadler's avatar
Julia Stadler committed
12
13
14
15
16
17
18
19
20
21
22
23
24
    def __init__(self, domain, new_shape, space=0):
        super(CentralZeroPadder, self).__init__()

        self._domain = DomainTuple.make(domain)
        self._space = utilities.infer_space(self._domain, space)
        dom = self._domain[self._space]

        if not isinstance(dom, RGSpace):
            raise TypeError("RGSpace required")
        if dom.harmonic:
            raise TypeError("RGSpace must not be harmonic")
        if len(new_shape) != len(dom.shape):
            raise ValueError("Shape missmatch")
Philipp Arras's avatar
Philipp Arras committed
25
        if any([a < b for a, b in zip(new_shape, dom.shape)]):
Julia Stadler's avatar
Julia Stadler committed
26
27
28
29
30
31
32
33
34
35
36
37
38
            raise ValueError("New shape must be larger than old shape")

        tgt = RGSpace(new_shape, dom.distances)
        self._target = list(self._domain)
        self._target[self._space] = tgt
        self._target = DomainTuple.make(self._target)

        slicer = []
        axes = self._target.axes[self._space]
        for i in range(len(self._domain.shape)):
            if i in axes:
                slicer_fw = slice(0, self._domain.shape[i]/2)
                slicer_bw = slice(-self._domain.shape[i]/2, None)
Philipp Arras's avatar
Philipp Arras committed
39
                slicer.append([slicer_fw, slicer_bw])
Julia Stadler's avatar
Julia Stadler committed
40
41
42
43
        self.slicer = list(itertools.product(*slicer))

        for i in range(len(self.slicer)):
            for j in range(len(self._domain.shape)):
Philipp Arras's avatar
Philipp Arras committed
44
                if j not in axes:
Julia Stadler's avatar
Julia Stadler committed
45
46
47
                    tmp = (list(self.slicer[i]))
                    tmp.insert(j, slice(None))
                    self.slicer[i] = tmp
Philipp Arras's avatar
Philipp Arras committed
48

Julia Stadler's avatar
Julia Stadler committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    @property
    def domain(self):
        return self._domain

    @property
    def target(self):
        return self._target

    @property
    def capability(self):
        return self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
        self._check_input(x, mode)
        x = x.val
Philipp Arras's avatar
Philipp Arras committed
64

Julia Stadler's avatar
Julia Stadler committed
65
        if mode == self.TIMES:
Philipp Arras's avatar
Philipp Arras committed
66
            y = np.zeros(self._target.shape)
Julia Stadler's avatar
Julia Stadler committed
67
68
69
            for i in self.slicer:
                y[i] = x[i]
            return Field(self._target, val=y)
Philipp Arras's avatar
Philipp Arras committed
70

Julia Stadler's avatar
Julia Stadler committed
71
        if mode == self.ADJOINT_TIMES:
Philipp Arras's avatar
Philipp Arras committed
72
            y = np.zeros(self._domain.shape)
Julia Stadler's avatar
Julia Stadler committed
73
74
            for i in self.slicer:
                y[i] = x[i]
Philipp Arras's avatar
Philipp Arras committed
75
            return Field(self._domain, val=y)