linear_interpolation.py 4.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17

Philipp Arras's avatar
Philipp Arras committed
18
19
20
from functools import reduce
from operator import mul

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
21
import numpy as np
Philipp Arras's avatar
Cleanup    
Philipp Arras committed
22
23
from scipy.sparse import coo_matrix
from scipy.sparse.linalg import aslinearoperator
24

25
from ..domains.rg_space import RGSpace
Philipp Arras's avatar
Cleanup    
Philipp Arras committed
26
27
from ..domains.unstructured_domain import UnstructuredDomain
from ..field import Field
28
from ..sugar import makeDomain
29
30
31
32
from .linear_operator import LinearOperator


class LinearInterpolator(LinearOperator):
33
    """Multilinear interpolation for points in an RGSpace
34
35
36
37
38
39
40
41
42
43
44
45
46
47

    Parameters
    ----------
    domain : RGSpace
    positions : numpy.ndarray
        Positions at which to interpolate
        Field with UnstructuredDomain, shape (dim, ndata)

    Notes
    -----
    Positions that are not within the RGSpace are wrapped according to
    periodic boundary conditions. This reflects the general property of
    RGSpaces to be tori topologically.
    """
Philipp Arras's avatar
Philipp Arras committed
48
    def __init__(self, domain, sampling_points):
49
        self._domain = makeDomain(domain)
Philipp Arras's avatar
Philipp Arras committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        for dom in self.domain:
            if not isinstance(dom, RGSpace):
                raise TypeError
        dims = [len(dom.shape) for dom in self.domain]

        # FIXME This needs to be removed as soon as the bug below is fixed.
        if not dims.count(dims[0]) == len(dims):
            raise TypeError(
                'This is a bug. Please extend LinearInterpolators functionality!'
            )

        shp = sampling_points.shape
        if not (isinstance(sampling_points, np.ndarray) and len(shp) == 2):
            raise TypeError
        n_dim, n_points = shp
        if not n_dim == reduce(mul, dims):
            raise TypeError
        self._target = makeDomain(UnstructuredDomain(n_points))
68
        self._capability = self.TIMES | self.ADJOINT_TIMES
Philipp Arras's avatar
Philipp Arras committed
69
        self._build_mat(sampling_points, n_points)
70

Philipp Arras's avatar
Philipp Arras committed
71
72
    def _build_mat(self, sampling_points, N_points):
        ndim = sampling_points.shape[0]
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
73
74
        mg = np.mgrid[(slice(0, 2),)*ndim]
        mg = np.array(list(map(np.ravel, mg)))
Philipp Arras's avatar
Philipp Arras committed
75
76
77
        dist = [list(dom.distances) for dom in self.domain]
        # FIXME This breaks as soon as not all domains have the same number of
        # dimensions.
Philipp Arras's avatar
Tweaks    
Philipp Arras committed
78
        dist = np.array(dist).reshape(-1, 1)
Philipp Arras's avatar
Philipp Arras committed
79
80
        pos = sampling_points/dist
        excess = pos - pos.astype(np.int64)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
81
82
83
84
85
        pos = pos.astype(np.int64)
        max_index = np.array(self.domain.shape).reshape(-1, 1)
        data = np.zeros((len(mg[0]), N_points))
        ii = np.zeros((len(mg[0]), N_points), dtype=np.int64)
        jj = np.zeros((len(mg[0]), N_points), dtype=np.int64)
86
        for i in range(len(mg[0])):
Philipp Arras's avatar
Philipp Arras committed
87
88
            factor = np.prod(
                np.abs(1 - mg[:, i].reshape(-1, 1) - excess), axis=0)
89
            data[i, :] = factor
Philipp Arras's avatar
Philipp Arras committed
90
            fromi = (pos + mg[:, i].reshape(-1, 1)) % max_index
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
91
92
            ii[i, :] = np.arange(N_points)
            jj[i, :] = np.ravel_multi_index(fromi, self.domain.shape)
93
        self._mat = coo_matrix((data.reshape(-1),
Philipp Arras's avatar
Philipp Arras committed
94
                                (ii.reshape(-1), jj.reshape(-1))),
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
95
                               (N_points, np.prod(self.domain.shape)))
96
        self._mat = aslinearoperator(self._mat)
97

98
99
100
101
    def apply(self, x, mode):
        self._check_input(x, mode)
        x_val = x.to_global_data()
        if mode == self.TIMES:
Philipp Arras's avatar
Tweaks    
Philipp Arras committed
102
            res = self._mat.matvec(x_val.reshape(-1))
Philipp Arras's avatar
Philipp Arras committed
103
104
105
        else:
            res = self._mat.rmatvec(x_val).reshape(self.domain.shape)
        return Field.from_global_data(self._tgt(mode), res)