power_space.py 13.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

theos's avatar
theos committed
19
20
import numpy as np

21
22
from d2o import distributed_data_object,\
    STRATEGIES as DISTRIBUTION_STRATEGIES
23

Martin Reinecke's avatar
Martin Reinecke committed
24
from ...spaces.space import Space
Martin Reinecke's avatar
Martin Reinecke committed
25
from functools import reduce
Martin Reinecke's avatar
Martin Reinecke committed
26
from ...config import nifty_configuration as gc
theos's avatar
theos committed
27
28


Theo Steininger's avatar
Theo Steininger committed
29
class PowerSpace(Space):
Theo Steininger's avatar
Theo Steininger committed
30
31
32
33
34
35
36
37
38
39
    """ NIFTY class for spaces of power spectra.

    Parameters
    ----------
    harmonic_partner : Space
        The harmonic Space of which this is the power space.
    distribution_strategy : str *optional*
        The distribution strategy used for the distributed_data_objects
        derived from this PowerSpace, e.g. the pindex.
        (default : 'not')
Martin Reinecke's avatar
Martin Reinecke committed
40
41
42
43
44
45
46
47
48
49
50
51
    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
52
53
54
55
56
        (default : None)

    Attributes
    ----------
    pindex : distributed_data_object
57
58
        This holds the information which pixel of the harmonic partner gets
        mapped to which power bin
Theo Steininger's avatar
Theo Steininger committed
59
    kindex : numpy.ndarray
60
        Sorted array of all k-modes.
Theo Steininger's avatar
Theo Steininger committed
61
62
63
    rho : numpy.ndarray
        The amount of k-modes that get mapped to one power bin is given by
        rho.
64
65
66
    dim : np.int
        Total number of dimensionality, i.e. the number of pixels.
    harmonic : bool
Martin Reinecke's avatar
Martin Reinecke committed
67
        Always True for this space.
68
69
70
71
    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
72
73
74
    binbounds : tuple or None
        Boundaries between the power spectrum bins; None is used to indicate
        natural binning
Theo Steininger's avatar
Theo Steininger committed
75
76
77
78
79
80
81

    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.

    """
82

83
84
    _powerIndexCache = {}

85
86
    # ---Overwritten properties and methods---

87
    def __init__(self, harmonic_partner, distribution_strategy=None,
88
                 volume_type='rho', binbounds=None):
Martin Reinecke's avatar
Martin Reinecke committed
89
        super(PowerSpace, self).__init__()
90
91
        self._needed_for_hash += ['_harmonic_partner', '_binbounds',
                                  'volume_type']
92

93
94
95
96
97
98
99
        if distribution_strategy is None:
            distribution_strategy = gc['default_distribution_strategy']
        elif distribution_strategy not in DISTRIBUTION_STRATEGIES['global']:
            raise ValueError(
                    "distribution_strategy must be a global-type "
                    "strategy.")

Martin Reinecke's avatar
Martin Reinecke committed
100
101
102
        if not (isinstance(harmonic_partner, Space) and
                harmonic_partner.harmonic):
            raise ValueError("harmonic_partner must be a harmonic space.")
103
        self._harmonic_partner = harmonic_partner
104

Martin Reinecke's avatar
Martin Reinecke committed
105
106
        if binbounds is not None:
            binbounds = tuple(binbounds)
107

Martin Reinecke's avatar
Martin Reinecke committed
108
        key = (harmonic_partner, distribution_strategy, binbounds)
109
110
111
112
        if self._powerIndexCache.get(key) is None:
            distance_array = \
                self.harmonic_partner.get_distance_array(distribution_strategy)
            temp_pindex = self._compute_pindex(
113
                                harmonic_partner=self.harmonic_partner,
114
                                distance_array=distance_array,
Martin Reinecke's avatar
Martin Reinecke committed
115
                                binbounds=binbounds,
116
117
                                distribution_strategy=distribution_strategy)
            temp_rho = temp_pindex.bincount().get_full_data()
Martin Reinecke's avatar
PEP8    
Martin Reinecke committed
118
            assert not np.any(temp_rho == 0), "empty bins detected"
119
120
121
            temp_kindex = \
                (temp_pindex.bincount(weights=distance_array).get_full_data() /
                 temp_rho)
Martin Reinecke's avatar
Martin Reinecke committed
122
            self._powerIndexCache[key] = (binbounds,
123
124
125
126
127
128
129
                                          temp_pindex,
                                          temp_kindex,
                                          temp_rho)

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

130
131
        self.volume_type = str(volume_type)

132
133
    @staticmethod
    def _compute_pindex(harmonic_partner, distance_array, binbounds,
134
                        distribution_strategy):
135

Martin Reinecke's avatar
Martin Reinecke committed
136
        # Compute pindex, kindex and rho according to bb
137
138
139
140
141
        pindex = distributed_data_object(
                                global_shape=distance_array.shape,
                                dtype=np.int,
                                distribution_strategy=distribution_strategy)
        if binbounds is None:
142
            binbounds = harmonic_partner.get_natural_binbounds()
143
144
145
        pindex.set_local_data(
                np.searchsorted(binbounds, distance_array.get_local_data()))
        return pindex
146

Theo Steininger's avatar
Theo Steininger committed
147
    def pre_cast(self, x, axes):
Theo Steininger's avatar
Theo Steininger committed
148
149
150
151
152
153
        """ Casts power spectrum functions to discretized power spectra.

        This function takes an array or a function. If it is an array it does
        nothing, otherwise it interpretes the function as power spectrum and
        evaluates it at every k-mode.

154
155
156
        Parameters
        ----------
        x : {array-like, function array-like -> array-like}
Theo Steininger's avatar
Theo Steininger committed
157
158
159
160
161
162
            power spectrum given either in discretized form or implicitly as a
            function
        axes : tuple of ints
            Specifies the axes of x which correspond to this space. For
            explicifying the power spectrum function, this is ignored.

163
164
        Returns
        -------
Theo Steininger's avatar
Theo Steininger committed
165
166
167
        array-like
            discretized power spectrum

168
        """
Theo Steininger's avatar
Theo Steininger committed
169

Martin Reinecke's avatar
Martin Reinecke committed
170
        return x(self.kindex) if callable(x) else x
171

172
173
    # ---Mandatory properties and methods---

174
175
    def __repr__(self):
        return ("PowerSpace(harmonic_partner=%r, distribution_strategy=%r, "
Martin Reinecke's avatar
Martin Reinecke committed
176
                "binbounds=%r)"
177
                % (self.harmonic_partner, self.pindex.distribution_strategy,
Martin Reinecke's avatar
Martin Reinecke committed
178
                   self._binbounds))
179

180
181
182
    @property
    def harmonic(self):
        return True
183

184
185
    @property
    def shape(self):
186
        return self.kindex.shape
187

188
189
190
191
192
193
194
    @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
195
        return float(reduce(lambda x, y: x*y, self.pindex.shape))
196
197

    def copy(self):
198
        distribution_strategy = self.pindex.distribution_strategy
199
        return self.__class__(harmonic_partner=self.harmonic_partner,
200
                              distribution_strategy=distribution_strategy,
Martin Reinecke's avatar
Martin Reinecke committed
201
                              binbounds=self._binbounds)
202

Martin Reinecke's avatar
Martin Reinecke committed
203
    def weight(self, x, power, axes, inplace=False):
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
        if self.volume_type == 'unit':
            if inplace:
                return x
            else:
                return x.copy()

        if self.volume_type == 'rho':
            weight = self.rho

        elif self.volume_type == 'volume':
            try:
                weight = self._volume_weight
            except AttributeError:
                k = self.kindex
                weight = np.empty_like(k)
                weight[1:-1] = (k[2:] - k[:-2])/2
                weight[0] = k[1] - k[0]
                weight[-1] = k[-1] - k[-2]
                self._volume_weight = weight

Jait Dixit's avatar
Jait Dixit committed
224
225
        reshaper = [1, ] * len(x.shape)
        # we know len(axes) is always 1
226
227
        reshaper[axes[0]] = self.shape[0]

228
        weight = weight.reshape(reshaper)
229
        if power != 1:
230
            weight = weight ** np.float(power)
231
232
233
234
235
236

        if inplace:
            x *= weight
            result_x = x
        else:
            result_x = x*weight
237
238
239

        return result_x

240
    def get_distance_array(self, distribution_strategy):
Martin Reinecke's avatar
Martin Reinecke committed
241
        return distributed_data_object(
Martin Reinecke's avatar
Martin Reinecke committed
242
                                self.kindex, dtype=np.float64,
243
                                distribution_strategy=distribution_strategy)
theos's avatar
theos committed
244

245
    def get_fft_smoothing_kernel_function(self, sigma):
246
        raise NotImplementedError(
247
            "There is no fft smoothing function for PowerSpace.")
theos's avatar
theos committed
248

249
250
251
    # ---Added properties and methods---

    @property
252
    def harmonic_partner(self):
Theo Steininger's avatar
Theo Steininger committed
253
        """ Returns the Space of which this is the power space.
254
255
        """
        return self._harmonic_partner
256
257

    @property
Martin Reinecke's avatar
Martin Reinecke committed
258
259
    def binbounds(self):
        return self._binbounds
260
261
262

    @property
    def pindex(self):
263
        """ A distributed_data_object having the shape of the harmonic partner
Theo Steininger's avatar
Theo Steininger committed
264
265
        space containing the indices of the power bin a pixel belongs to.
        """
266
267
268
269
        return self._pindex

    @property
    def kindex(self):
Theo Steininger's avatar
Theo Steininger committed
270
271
        """ Sorted array of all k-modes.
        """
272
273
274
275
        return self._kindex

    @property
    def rho(self):
Theo Steininger's avatar
Theo Steininger committed
276
277
        """Degeneracy factor of the individual k-vectors.
        """
278
        return self._rho
279

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
    @staticmethod
    def linear_binbounds(nbin, first_bound, last_bound):
        """
        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)
        assert nbin >= 3, "nbin must be at least 3"
        return np.linspace(float(first_bound), float(last_bound), nbin-1)

    @staticmethod
    def logarithmic_binbounds(nbin, first_bound, last_bound):
        """
        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.
        """
        nbin = int(nbin)
        assert nbin >= 3, "nbin must be at least 3"
        return np.logspace(np.log(float(first_bound)),
                           np.log(float(last_bound)),
                           nbin-1, base=np.e)

    @staticmethod
    def useful_binbounds(space, logarithmic, nbin=None):
        if not (isinstance(space, Space) and space.harmonic):
            raise ValueError("first argument must be a harmonic space.")
        if logarithmic is None and nbin is None:
            return None
        nbin = None if nbin is None else int(nbin)
        logarithmic = bool(logarithmic)
        dists = space.get_unique_distances()
        if len(dists) < 3:
            raise ValueError("Space does not have enough unique k lengths")
        lbound = 0.5*(dists[0]+dists[1])
        rbound = 0.5*(dists[-2]+dists[-1])
        dists[0] = lbound
        dists[-1] = rbound
        if logarithmic:
            dists = np.log(dists)
        binsz_min = np.max(np.diff(dists))
        nbin_max = int((dists[-1]-dists[0])/binsz_min)+2
        if nbin is None:
            nbin = nbin_max
        assert nbin >= 3, "nbin must be at least 3"
        if nbin > nbin_max:
            raise ValueError("nbin is too large")
        if logarithmic:
            return PowerSpace.logarithmic_binbounds(nbin, lbound, rbound)
        else:
            return PowerSpace.linear_binbounds(nbin, lbound, rbound)

346
347
348
    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
Theo Steininger's avatar
Theo Steininger committed
349
350
        if self._binbounds is not None:
            hdf5_group['binbounds'] = np.array(self._binbounds)
Martin Reinecke's avatar
Martin Reinecke committed
351
352
        hdf5_group.attrs['distribution_strategy'] = \
            self._pindex.distribution_strategy
Theo Steininger's avatar
Theo Steininger committed
353
354
        hdf5_group.attrs['volume_type'] = self.volume_type
        return {'harmonic_partner': self.harmonic_partner}
355
356

    @classmethod
Theo Steininger's avatar
Theo Steininger committed
357
    def _from_hdf5(cls, hdf5_group, repository):
Martin Reinecke's avatar
Martin Reinecke committed
358
        hp = repository.get('harmonic_partner', hdf5_group)
Theo Steininger's avatar
Theo Steininger committed
359
360
361
362
        try:
            bb = tuple(hdf5_group['binbounds'])
        except KeyError:
            bb = None
363
        ds = str(hdf5_group.attrs['distribution_strategy'])
Theo Steininger's avatar
Theo Steininger committed
364
365
        volume_type = str(hdf5_group.attrs['volume_type'])
        return PowerSpace(hp, ds, binbounds=bb, volume_type=volume_type)