smoothing_operator.py 6.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# 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/>.
Theo Steininger's avatar
Theo Steininger committed
13
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
18

19
20
import abc

Jait Dixit's avatar
Jait Dixit committed
21
22
23
import numpy as np

from nifty.operators.endomorphic_operator import EndomorphicOperator
24
from nifty.spaces import RGSpace, GLSpace, HPSpace, PowerSpace
Jait Dixit's avatar
Jait Dixit committed
25

26

27
class SmoothingOperator(EndomorphicOperator):
Theo Steininger's avatar
Theo Steininger committed
28
    """ NIFTY class for smoothing operators.
29
30
31

    The NIFTy SmoothingOperator smooths Fields, with a given kernel length.
    Fields which are not living over a PowerSpace are smoothed
Theo Steininger's avatar
Theo Steininger committed
32
33
    via a gaussian convolution. Fields living over the PowerSpace are directly
    smoothed.
34
35
36

    Parameters
    ----------
37
38
39
    domain : DomainObject, i.e. Space or FieldType
        The Space on which the operator acts. The SmoothingOperator
        can only live on one space or FieldType
40
41
    sigma : float
        Sets the length of the Gaussian convolution kernel
Theo Steininger's avatar
Theo Steininger committed
42
43
44
    log_distances : boolean *optional*
        States whether the convolution happens on the logarithmic grid or not
        (default: False).
45
46
    default_spaces : tuple of ints *optional*
        Defines on which space(s) of a given field the Operator acts by
Theo Steininger's avatar
Theo Steininger committed
47
        default (default: None).
48
49
50

    Attributes
    ----------
51
52
53
54
55
56
57
58
59
    domain : DomainObject, i.e. Space or FieldType
        The domain on which the Operator's input Field lives.
    target : tuple of DomainObjects, i.e. Spaces and FieldTypes
        The domain in which the outcome of the operator lives. As the Operator
        is endomorphic this is the same as its domain.
    unitary : boolean
        Indicates whether the Operator is unitary or not.
    self_adjoint : boolean
        Indicates whether the operator is self_adjoint or not.
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    sigma : float
        Sets the length of the Gaussian convolution kernel
    log_distances : boolean
        States whether the convolution happens on the logarithmic grid or not.

    Raises
    ------
    ValueError
        Raised if
            * the given domain inherits more than one space. The
              SmoothingOperator acts only on one Space.

    Notes
    -----

    Examples
    --------
    >>> x = RGSpace(5)
    >>> S = SmoothingOperator(x, sigma=1.)
    >>> f = Field(x, val=[1,2,3,4,5])
    >>> S.times(f).val
    <distributed_data_object>
    array([ 3.,  3.,  3.,  3.,  3.])

    See Also
    --------
    DiagonalOperator, SmoothingOperator,
    PropagatorOperator, ProjectionOperator,
    ComposedOperator

    """

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    _fft_smoothing_spaces = [RGSpace,
                             GLSpace,
                             HPSpace]
    _direct_smoothing_spaces = [PowerSpace]

    def __new__(cls, domain, *args, **kwargs):
        if cls is SmoothingOperator:
            domain = cls._parse_domain(domain)

            if len(domain) != 1:
                raise ValueError("SmoothingOperator only accepts exactly one "
                                 "space as input domain.")

            if np.any([isinstance(domain[0], sp)
                       for sp in cls._fft_smoothing_spaces]):
                from .fft_smoothing_operator import FFTSmoothingOperator
                return super(SmoothingOperator, cls).__new__(
                        FFTSmoothingOperator, domain, *args, **kwargs)

            elif np.any([isinstance(domain[0], sp)
                         for sp in cls._direct_smoothing_spaces]):
                from .direct_smoothing_operator import DirectSmoothingOperator
                return super(SmoothingOperator, cls).__new__(
                        DirectSmoothingOperator, domain, *args, **kwargs)

            else:
                raise NotImplementedError("For the given Space smoothing "
                                          " is not available.")
        else:
            return super(SmoothingOperator, cls).__new__(cls,
                                                         domain,
                                                         *args,
                                                         **kwargs)

Jait Dixit's avatar
Jait Dixit committed
126
    # ---Overwritten properties and methods---
Martin Reinecke's avatar
Martin Reinecke committed
127
    def __init__(self, domain, sigma, log_distances=False,
128
129
                 default_spaces=None):
        super(SmoothingOperator, self).__init__(default_spaces)
130

131
132
133
134
135
        # # the _parse_domain is already done in the __new__ method
        # self._domain = self._parse_domain(domain)
        # if len(self.domain) != 1:
        #     raise ValueError("SmoothingOperator only accepts exactly one "
        #                      "space as input domain.")
136
        self._domain = self._parse_domain(domain)
Jait Dixit's avatar
Jait Dixit committed
137

138
139
        self._sigma = sigma
        self._log_distances = log_distances
140

141
    def _inverse_times(self, x, spaces):
142
143
144
145
146
147
148
149
150
151
152
        if self.sigma == 0:
            return x.copy()

        # the domain of the smoothing operator contains exactly one space.
        # Hence, if spaces is None, but we passed LinearOperator's
        # _check_input_compatibility, we know that x is also solely defined
        # on that space
        if spaces is None:
            spaces = (0,)

        return self._smooth(x, spaces, inverse=True)
Jait Dixit's avatar
Jait Dixit committed
153

154
    def _times(self, x, spaces):
155
156
157
158
159
160
161
162
163
164
165
        if self.sigma == 0:
            return x.copy()

        # the domain of the smoothing operator contains exactly one space.
        # Hence, if spaces is None, but we passed LinearOperator's
        # _check_input_compatibility, we know that x is also solely defined
        # on that space
        if spaces is None:
            spaces = (0,)

        return self._smooth(x, spaces, inverse=False)
Jait Dixit's avatar
Jait Dixit committed
166

Jait Dixit's avatar
Jait Dixit committed
167
    # ---Mandatory properties and methods---
168
169
170
171
    @property
    def domain(self):
        return self._domain

Jait Dixit's avatar
Jait Dixit committed
172
    @property
Martin Reinecke's avatar
Martin Reinecke committed
173
    def self_adjoint(self):
theos's avatar
theos committed
174
        return True
Jait Dixit's avatar
Jait Dixit committed
175

Jait Dixit's avatar
Jait Dixit committed
176
177
178
    @property
    def unitary(self):
        return False
Jait Dixit's avatar
Jait Dixit committed
179
180

    # ---Added properties and methods---
181

Jait Dixit's avatar
Jait Dixit committed
182
183
184
185
    @property
    def sigma(self):
        return self._sigma

186
187
188
189
    @property
    def log_distances(self):
        return self._log_distances

190
191
192
    @abc.abstractmethod
    def _smooth(self, x, spaces, inverse):
        raise NotImplementedError