space.py 9.64 KB
Newer Older
1
2
3
4
5
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
6
7
8
9
# 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.
10
#
11
12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
14
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Marco Selig's avatar
Marco Selig committed
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
    ..                  __   ____   __
    ..                /__/ /   _/ /  /_
    ..      __ ___    __  /  /_  /   _/  __   __
    ..    /   _   | /  / /   _/ /  /   /  / /  /
    ..   /  / /  / /  / /  /   /  /_  /  /_/  /
    ..  /__/ /__/ /__/ /__/    \___/  \___   /  core
    ..                               /______/

    .. The NIFTY project homepage is http://www.mpa-garching.mpg.de/ift/nifty/

    NIFTY [#]_, "Numerical Information Field Theory", is a versatile
    library designed to enable the development of signal inference algorithms
    that operate regardless of the underlying spatial grid and its resolution.
    Its object-oriented framework is written in Python, although it accesses
    libraries written in Cython, C++, and C for efficiency.

    NIFTY offers a toolkit that abstracts discretized representations of
    continuous spaces, fields in these spaces, and operators acting on fields
    into classes. Thereby, the correct normalization of operations on fields is
    taken care of automatically without concerning the user. This allows for an
    abstract formulation and programming of inference algorithms, including
    those derived within information field theory. Thus, NIFTY permits its user
    to rapidly prototype algorithms in 1D and then apply the developed code in
    higher-dimensional settings of real world problems. The set of spaces on
    which NIFTY operates comprises point sets, n-dimensional regular grids,
    spherical spaces, their harmonic counterparts, and product spaces
    constructed as combinations of those.

    References
    ----------
    .. [#] Selig et al., "NIFTY -- Numerical Information Field Theory --
        a versatile Python library for signal inference",
        `A&A, vol. 554, id. A26 <http://dx.doi.org/10.1051/0004-6361/201321236>`_,
        2013; `arXiv:1301.4499 <http://www.arxiv.org/abs/1301.4499>`_

    Class & Feature Overview
    ------------------------
    The NIFTY library features three main classes: **spaces** that represent
    certain grids, **fields** that are defined on spaces, and **operators**
    that apply to fields.

    .. Overview of all (core) classes:
    ..
    .. - switch
    .. - notification
    .. - _about
    .. - random
    .. - space
    ..     - point_space
    ..     - rg_space
    ..     - lm_space
    ..     - gl_space
    ..     - hp_space
    ..     - nested_space
    .. - field
    .. - operator
    ..     - diagonal_operator
    ..         - power_operator
    ..     - projection_operator
    ..     - vecvec_operator
    ..     - response_operator
    .. - probing
    ..     - trace_probing
    ..     - diagonal_probing

    Overview of the main classes and functions:

    .. automodule:: nifty

    - :py:class:`space`
        - :py:class:`point_space`
        - :py:class:`rg_space`
        - :py:class:`lm_space`
        - :py:class:`gl_space`
        - :py:class:`hp_space`
        - :py:class:`nested_space`
    - :py:class:`field`
    - :py:class:`operator`
        - :py:class:`diagonal_operator`
            - :py:class:`power_operator`
        - :py:class:`projection_operator`
        - :py:class:`vecvec_operator`
        - :py:class:`response_operator`

        .. currentmodule:: nifty.nifty_tools

        - :py:class:`invertible_operator`
        - :py:class:`propagator_operator`

        .. currentmodule:: nifty.nifty_explicit

        - :py:class:`explicit_operator`

    .. automodule:: nifty

    - :py:class:`probing`
        - :py:class:`trace_probing`
        - :py:class:`diagonal_probing`

        .. currentmodule:: nifty.nifty_explicit

        - :py:class:`explicit_probing`

    .. currentmodule:: nifty.nifty_tools

    - :py:class:`conjugate_gradient`
    - :py:class:`steepest_descent`

    .. currentmodule:: nifty.nifty_explicit

    - :py:func:`explicify`

    .. currentmodule:: nifty.nifty_power

    - :py:func:`weight_power`,
      :py:func:`smooth_power`,
      :py:func:`infer_power`,
      :py:func:`interpolate_power`

"""
from __future__ import division

142
143
import abc

144
from nifty.domain_object import DomainObject
Theo Steininger's avatar
Theo Steininger committed
145

146

147
class Space(DomainObject):
148
149
    def __init__(self):
        """
Theo Steininger's avatar
Theo Steininger committed
150
151
            Parameters
            ----------
152
            None.
Marco Selig's avatar
Marco Selig committed
153

154
155
156
            Returns
            -------
            None.
157
        """
158

Martin Reinecke's avatar
Martin Reinecke committed
159
        super(Space, self).__init__()
160

161
162
    @abc.abstractproperty
    def harmonic(self):
163
164
165
166
167
168
169
170
171
172
        """Returns True if this Space can be regarded as a harmonic space.
        
        Returns
        -------
        bool : True if can be regarded as a harmonic space to another space. False otherwise
        Raises
        ------
        NotImplementedError : If it is called for an abstract class, all non-abstract child-classes should
        implement this.        
        """
173
        raise NotImplementedError
174

175
    @abc.abstractproperty
176
    def total_volume(self):
177
178
179
180
181
182
183
184
185
        """Returns the total volume of the space
        Returns
        -------
        Floating Point : An real number representing the sum of all pixel volumes.
        Raises
        ------
        NotImplementedError : If it is called for an abstract class, all non-abstract child-classes should
        implement this.
        """
186
187
        raise NotImplementedError(
            "There is no generic volume for the Space base class.")
188

189
190
    @abc.abstractmethod
    def copy(self):
191
192
193
194
195
196
        """Returns a copied version of this Space.
        
        Returns
        -------
        Space : A copy of this object.
        """
Martin Reinecke's avatar
Martin Reinecke committed
197
        return self.__class__()
198

199
    def get_distance_array(self, distribution_strategy):
200
201
202
203
204
205
206
207
208
209
210
211
        """The distances of the pixel to zero.
        
        In a harmonic space, this return an array that gives for each pixel its 
        distance to the zero-mode
        Returns
        -------
        array-like : An array representing the distances of each pixel to the zero-mode
        Raises
        ------
        NotImplementedError : If it is called for an abstract class, all non-abstract child-classes 
        that can be regarded as harmonic space should implement this.
        """
212
        raise NotImplementedError(
213
214
            "There is no generic distance structure for Space base class.")

215
    def get_fft_smoothing_kernel_function(self, sigma):
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
        """This method returns a function applying a smoothing kernel.
        
        This method, which is only implemented for harmonic spaces,
        helps smoothing functions that live in a position space that has this space as its harmonic space.
        The returned function multiplies field values of a field with a zero centered Gaussian which corresponds to convolution with a
        Gaussian kernel and sigma standard deviation in position space.
        
        Parameters
        ----------
        sigma : Floating Point
        A real number representing a physical scale on which the smoothing takes place. The smoothing is defined with respect to
        the real physical field and points that are closer together than one sigma are blurred together. Mathematically
        sigma is the standard deviation of a convolution with a normalized, zero-centered Gaussian that takes place in position space.
        
        Returns
        -------
        function Field -> Field : A smoothing operation that multiplies values with a Gaussian kernel.
        
        Raises
        ------
        NotImplementedError : If it is called for an abstract class, all non-abstract child-classes 
        that can be regarded as harmonic space should implement this.
        """
239
240
        raise NotImplementedError(
            "There is no generic co-smoothing kernel for Space base class.")
241

242
243
    def hermitian_decomposition(self, x, axes=None,
                                preserve_gaussian_variance=False):
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
        """
        Decomposes the field x into its hermitian and anti-hermitian constituents.
        
        If the space is harmonic, this method decomposes a field x into
        a hermitian and an antihermitian part, which corresponds to a real and imaginary part
        in a corresponding position space. This is an internal function that is mainly used for
        drawing real fields.
        
        Parameters
        ----------
        x : Field
            A field with this space as domain to be decomposed.
        axes : {int, tuple}, *optional*
            Specifies the axes of x which represent this domain.
            (default: None).
            If axes==None:
260
                Assumes the axes to be the fields first axes
261
262
263
264
265
266
267
268
269
270
271
        preserve_gaussian_variance : bool *optional*
            FIXME: figure out what this does
        Returns
        -------
        (Field, Field) : A tuple of two fields, the first field being the hermitian and the second the anti-hermitian part of x.
        
        Raises
        ------
        NotImplementedError : If it is called for an abstract class, all non-abstract child-classes 
        that can be regarded as harmonic space should implement this.
        """
272
273
        raise NotImplementedError

274
    def __repr__(self):
Theo Steininger's avatar
Theo Steininger committed
275
276
277
        string = ""
        string += str(type(self)) + "\n"
        return string