power_space.py 8.1 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.
theos's avatar
theos committed
18

Martin Reinecke's avatar
Martin Reinecke committed
19
import ast
theos's avatar
theos committed
20
21
import numpy as np

Martin Reinecke's avatar
Martin Reinecke committed
22
from ...spaces.space import Space
Martin Reinecke's avatar
Martin Reinecke committed
23
from functools import reduce
theos's avatar
theos committed
24
25


Theo Steininger's avatar
Theo Steininger committed
26
class PowerSpace(Space):
Theo Steininger's avatar
Theo Steininger committed
27
28
29
30
31
32
    """ NIFTY class for spaces of power spectra.

    Parameters
    ----------
    harmonic_partner : Space
        The harmonic Space of which this is the power space.
Martin Reinecke's avatar
Martin Reinecke committed
33
34
35
36
37
38
39
40
41
42
43
44
    binbounds: None, or tuple/array/list of float
        if None:
            There will be as many bins as there are distinct k-vector lengths
            in the harmonic partner space.
            The "binbounds" property of the PowerSpace will also be None.

        else:
            the bin bounds requested for this PowerSpace. The array
            must be sorted and strictly ascending. The first entry is the right
            boundary of the first bin, and the last entry is the left boundary
            of the last bin, i.e. thee will be len(binbounds)+1 bins in total,
            with the first and last bins reaching to -+infinity, respectively.
Theo Steininger's avatar
Theo Steininger committed
45
46
47
48
        (default : None)

    Attributes
    ----------
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
49
    pindex : numpy.ndarray
50
51
        This holds the information which pixel of the harmonic partner gets
        mapped to which power bin
Theo Steininger's avatar
Theo Steininger committed
52
    kindex : numpy.ndarray
53
        Sorted array of all k-modes.
Theo Steininger's avatar
Theo Steininger committed
54
55
56
    rho : numpy.ndarray
        The amount of k-modes that get mapped to one power bin is given by
        rho.
57
58
59
    dim : np.int
        Total number of dimensionality, i.e. the number of pixels.
    harmonic : bool
Martin Reinecke's avatar
Martin Reinecke committed
60
        Always True for this space.
61
62
63
64
    total_volume : np.float
        The total volume of the space.
    shape : tuple of np.ints
        The shape of the space's data array.
Martin Reinecke's avatar
Martin Reinecke committed
65
66
67
    binbounds : tuple or None
        Boundaries between the power spectrum bins; None is used to indicate
        natural binning
Theo Steininger's avatar
Theo Steininger committed
68
69
70
71
72
73
74

    Notes
    -----
    A power space is the result of a projection of a harmonic space where
    k-modes of equal length get mapped to one power index.

    """
75

76
77
    _powerIndexCache = {}

78
79
    # ---Overwritten properties and methods---

Martin Reinecke's avatar
Martin Reinecke committed
80
    @staticmethod
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
81
    def linear_binbounds(nbin, first_bound, last_bound):
Martin Reinecke's avatar
Martin Reinecke committed
82
83
84
85
86
87
88
89
90
91
92
93
        """
        nbin: integer
            the number of bins
        first_bound, last_bound: float
            the k values for the right boundary of the first bin and the left
            boundary of the last bin, respectively. They are given in length
            units of the harmonic partner space.
        This will produce a binbounds array with nbin-1 entries with
        binbounds[0]=first_bound and binbounds[-1]=last_bound and the remaining
        values equidistantly spaced (in linear scale) between these two.
        """
        nbin = int(nbin)
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
94
95
        assert nbin >= 3, "nbin must be at least 3"
        return np.linspace(float(first_bound), float(last_bound), nbin-1)
Martin Reinecke's avatar
Martin Reinecke committed
96
97

    @staticmethod
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
98
    def logarithmic_binbounds(nbin, first_bound, last_bound):
Martin Reinecke's avatar
Martin Reinecke committed
99
100
101
102
103
104
105
106
107
108
109
110
        """
        nbin: integer
            the number of bins
        first_bound, last_bound: float
            the k values for the right boundary of the first bin and the left
            boundary of the last bin, respectively. They are given in length
            units of the harmonic partner space.
        This will produce a binbounds array with nbin-1 entries with
        binbounds[0]=first_bound and binbounds[-1]=last_bound and the remaining
        values equidistantly spaced (in natural logarithmic scale)
        between these two.
        """
Martin Reinecke's avatar
Martin Reinecke committed
111
        nbin = int(nbin)
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
112
        assert nbin >= 3, "nbin must be at least 3"
Martin Reinecke's avatar
Martin Reinecke committed
113
114
115
        return np.logspace(np.log(float(first_bound)),
                           np.log(float(last_bound)),
                           nbin-1, base=np.e)
Martin Reinecke's avatar
Martin Reinecke committed
116

Martin Reinecke's avatar
Martin Reinecke committed
117
    def __init__(self, harmonic_partner, binbounds=None):
Martin Reinecke's avatar
Martin Reinecke committed
118
        super(PowerSpace, self).__init__()
Martin Reinecke's avatar
Martin Reinecke committed
119
        self._ignore_for_hash += ['_pindex', '_kindex', '_rho']
120

Martin Reinecke's avatar
Martin Reinecke committed
121
122
123
        if not (isinstance(harmonic_partner, Space) and
                harmonic_partner.harmonic):
            raise ValueError("harmonic_partner must be a harmonic space.")
124
        self._harmonic_partner = harmonic_partner
125

Martin Reinecke's avatar
Martin Reinecke committed
126
127
        if binbounds is not None:
            binbounds = tuple(binbounds)
128

Martin Reinecke's avatar
Martin Reinecke committed
129
        key = (harmonic_partner, binbounds)
130
131
        if self._powerIndexCache.get(key) is None:
            distance_array = \
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
132
                self.harmonic_partner.get_distance_array()
133
            temp_pindex = self._compute_pindex(
134
                                harmonic_partner=self.harmonic_partner,
135
                                distance_array=distance_array,
Martin Reinecke's avatar
Martin Reinecke committed
136
                                binbounds=binbounds)
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
137
            temp_rho = np.bincount(temp_pindex.flatten())
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
138
            assert not np.any(temp_rho == 0), "empty bins detected"
Martin Reinecke's avatar
Martin Reinecke committed
139
140
            temp_kindex = np.bincount(temp_pindex.flatten(),
                weights=distance_array.flatten()) / temp_rho
Martin Reinecke's avatar
Martin Reinecke committed
141
            self._powerIndexCache[key] = (binbounds,
142
143
144
145
146
147
148
                                          temp_pindex,
                                          temp_kindex,
                                          temp_rho)

        (self._binbounds, self._pindex, self._kindex, self._rho) = \
            self._powerIndexCache[key]

149
    @staticmethod
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
150
    def _compute_pindex(harmonic_partner, distance_array, binbounds):
Martin Reinecke's avatar
Martin Reinecke committed
151
        pindex = np.empty(distance_array.shape,dtype=np.int)
152
        if binbounds is None:
153
            binbounds = harmonic_partner.get_natural_binbounds()
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
154
        return np.searchsorted(binbounds, distance_array)
155

156
157
    # ---Mandatory properties and methods---

158
    def __repr__(self):
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
159
160
        return ("PowerSpace(harmonic_partner=%r, binbounds=%r)"
                % (self.harmonic_partner, self._binbounds))
161

162
163
164
    @property
    def harmonic(self):
        return True
165

166
167
    @property
    def shape(self):
168
        return self.kindex.shape
169

170
171
172
173
174
175
176
    @property
    def dim(self):
        return self.shape[0]

    @property
    def total_volume(self):
        # every power-pixel has a volume of 1
Jait Dixit's avatar
Jait Dixit committed
177
        return float(reduce(lambda x, y: x*y, self.pindex.shape))
178
179

    def copy(self):
180
        return self.__class__(harmonic_partner=self.harmonic_partner,
Martin Reinecke's avatar
Martin Reinecke committed
181
                              binbounds=self._binbounds)
182

Martin Reinecke's avatar
Martin Reinecke committed
183
    def weight(self, x, power, axes, inplace=False):
Jait Dixit's avatar
Jait Dixit committed
184
185
        reshaper = [1, ] * len(x.shape)
        # we know len(axes) is always 1
186
187
        reshaper[axes[0]] = self.shape[0]

188
        weight = self.rho.reshape(reshaper)
189
        if power != 1:
190
            weight = weight ** np.float(power)
191
192
193
194
195
196

        if inplace:
            x *= weight
            result_x = x
        else:
            result_x = x*weight
197
198
199

        return result_x

Martin Reinecke's avatar
stage1    
Martin Reinecke committed
200
201
    def get_distance_array(self):
        return self.kindex.copy()
theos's avatar
theos committed
202

203
    def get_fft_smoothing_kernel_function(self, sigma):
204
        raise NotImplementedError(
205
            "There is no fft smoothing function for PowerSpace.")
theos's avatar
theos committed
206

207
208
209
    # ---Added properties and methods---

    @property
210
    def harmonic_partner(self):
Theo Steininger's avatar
Theo Steininger committed
211
        """ Returns the Space of which this is the power space.
212
213
        """
        return self._harmonic_partner
214
215

    @property
Martin Reinecke's avatar
Martin Reinecke committed
216
217
    def binbounds(self):
        return self._binbounds
218
219
220

    @property
    def pindex(self):
Martin Reinecke's avatar
stage1    
Martin Reinecke committed
221
        """ A numpy.ndarray having the shape of the harmonic partner
Theo Steininger's avatar
Theo Steininger committed
222
223
        space containing the indices of the power bin a pixel belongs to.
        """
224
225
226
227
        return self._pindex

    @property
    def kindex(self):
Theo Steininger's avatar
Theo Steininger committed
228
229
        """ Sorted array of all k-modes.
        """
230
231
232
233
        return self._kindex

    @property
    def rho(self):
Theo Steininger's avatar
Theo Steininger committed
234
235
        """Degeneracy factor of the individual k-vectors.
        """
236
        return self._rho