lm_space.py 7 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

csongor's avatar
csongor committed
19
20
21
22
from __future__ import division

import numpy as np

Martin Reinecke's avatar
Martin Reinecke committed
23
from ..space import Space
theos's avatar
theos committed
24

25
from d2o import arange, distributed_data_object
Jait Dixit's avatar
Jait Dixit committed
26

csongor's avatar
csongor committed
27

Theo Steininger's avatar
Theo Steininger committed
28
class LMSpace(Space):
csongor's avatar
csongor committed
29
30
31
32
33
34
35
36
37
38
39
    """
        ..       __
        ..     /  /
        ..    /  /    __ ____ ___
        ..   /  /   /   _    _   |
        ..  /  /_  /  / /  / /  /
        ..  \___/ /__/ /__/ /__/  space class

        NIFTY subclass for spherical harmonics components, for representations
        of fields on the two-sphere.

40
41
42
43
44
45
        Parameters
        ----------
        lmax : int
            The maximal :math:`l` value of any spherical harmonics
            :math:`Y_{lm}` that is represented in this Space.

46
        Attributes
csongor's avatar
csongor committed
47
48
        ----------
        lmax : int
49
50
51
52
53
            The maximal :math:`l` value of any spherical harmonics
            :math:`Y_{lm}` that is represented in this Space.
        mmax : int
            The maximal :math:`m` value of any spherical harmonic
            :math:`Y_{lm}` that is represented in this Space.
54
55
56
57
58
59
60
61
        dim : np.int
            Total number of dimensionality, i.e. the number of pixels.
        harmonic : bool
            Specifies whether the space is a signal or harmonic space.
        total_volume : np.float
            The total volume of the space.
        shape : tuple of np.ints
            The shape of the space's data array.
csongor's avatar
csongor committed
62
63
64
65
66
67
68

        See Also
        --------
        hp_space : A class for the HEALPix discretization of the sphere [#]_.
        gl_space : A class for the Gauss-Legendre discretization of the
            sphere [#]_.

Theo Steininger's avatar
Theo Steininger committed
69
70
71
72
73
        Raises
        ------
        ValueError
            If given lmax is negative.

74
75
        Notes
        -----
Theo Steininger's avatar
Theo Steininger committed
76
            This implementation implicitly sets the mmax parameter to lmax.
77

csongor's avatar
csongor committed
78
79
80
81
82
83
84
85
86
87
        References
        ----------
        .. [#] K.M. Gorski et al., 2005, "HEALPix: A Framework for
               High-Resolution Discretization and Fast Analysis of Data
               Distributed on the Sphere", *ApJ* 622..759G.
        .. [#] M. Reinecke and D. Sverre Seljebotn, 2013, "Libsharp - spherical
               harmonic transforms revisited";
               `arXiv:1303.4945 <http://www.arxiv.org/abs/1303.4945>`_
    """

Martin Reinecke's avatar
Martin Reinecke committed
88
89
    def __init__(self, lmax):
        super(LMSpace, self).__init__()
90
        self._needed_for_hash += ["_lmax"]
csongor's avatar
csongor committed
91
        self._lmax = self._parse_lmax(lmax)
csongor's avatar
csongor committed
92

Theo Steininger's avatar
Theo Steininger committed
93
    # ---Mandatory properties and methods---
csongor's avatar
csongor committed
94

95
96
97
    def __repr__(self):
        return ("LMSpace(lmax=%r)" % self.lmax)

csongor's avatar
csongor committed
98
99
100
    @property
    def harmonic(self):
        return True
csongor's avatar
csongor committed
101
102

    @property
103
104
    def shape(self):
        return (self.dim, )
csongor's avatar
csongor committed
105
106

    @property
107
108
    def dim(self):
        l = self.lmax
109
        # the LMSpace consists of the full triangle (including -m's!),
theos's avatar
theos committed
110
111
        # minus two little triangles if mmax < lmax
        # dim = (((2*(l+1)-1)+1)**2/4 - 2 * (l-m)(l-m+1)/2
112
113
        # dim = np.int((l+1)**2 - (l-m)*(l-m+1.))
        # We fix l == m
114
        return np.int((l+1)*(l+1))
csongor's avatar
csongor committed
115

116
117
118
    @property
    def total_volume(self):
        # the individual pixels have a fixed volume of 1.
Martin Reinecke's avatar
Martin Reinecke committed
119
        return np.float64(self.dim)
csongor's avatar
csongor committed
120

121
    def copy(self):
Martin Reinecke's avatar
Martin Reinecke committed
122
        return self.__class__(lmax=self.lmax)
csongor's avatar
csongor committed
123

124
125
126
    def weight(self, x, power=1, axes=None, inplace=False):
        if inplace:
            return x
csongor's avatar
csongor committed
127
        else:
128
            return x.copy()
csongor's avatar
csongor committed
129

130
    def get_distance_array(self, distribution_strategy):
Martin Reinecke's avatar
Martin Reinecke committed
131
132
133
134
135
136
137
138
139
140
141
        if distribution_strategy == 'not':  # short cut
            lmax = self.lmax
            ldist = np.empty((self.dim,), dtype=np.float64)
            ldist[0:lmax+1] = np.arange(lmax+1, dtype=np.float64)
            tmp = np.empty((2*lmax+2), dtype=np.float64)
            tmp[0::2] = np.arange(lmax+1)
            tmp[1::2] = np.arange(lmax+1)
            idx = lmax+1
            for l in range(1, lmax+1):
                ldist[idx:idx+2*(lmax+1-l)] = tmp[2*l:]
                idx += 2*(lmax+1-l)
142
143
144
145
            dists = distributed_data_object(
                            global_shape=self.shape,
                            dtype=np.float,
                            distribution_strategy=distribution_strategy)
Martin Reinecke's avatar
Martin Reinecke committed
146
147
148
            dists.set_local_data(ldist)
            return dists

149
150
151
152
        dists = arange(start=0, stop=self.shape[0],
                       distribution_strategy=distribution_strategy)

        dists = dists.apply_scalar_function(
153
            lambda x: self._distance_array_helper(x, self.lmax),
Martin Reinecke's avatar
Martin Reinecke committed
154
            dtype=np.float64)
155
156
157

        return dists

Martin Reinecke's avatar
Martin Reinecke committed
158
159
160
161
162
163
    def get_unique_distances(self):
        return np.arange(self.lmax+1, dtype=np.float64)

    def get_natural_binbounds(self):
        return np.arange(self.lmax, dtype=np.float64) + 0.5

164
165
166
167
168
169
170
171
    @staticmethod
    def _distance_array_helper(index_array, lmax):
        u = 2*lmax + 1
        index_half = (index_array+np.minimum(lmax, index_array)+1)//2
        m = (np.ceil((u - np.sqrt(u*u - 8*(index_half - lmax)))/2)).astype(int)
        res = (index_half - m*(u - m)//2).astype(np.float64)
        return res

172
    def get_fft_smoothing_kernel_function(self, sigma):
Theo Steininger's avatar
Theo Steininger committed
173
174
175
        # cf. "All-sky convolution for polarimetry experiments"
        # by Challinor et al.
        # http://arxiv.org/abs/astro-ph/0008228
176
        return lambda x: np.exp(-0.5 * x * (x + 1) * sigma*sigma)
177

csongor's avatar
csongor committed
178
179
180
181
    # ---Added properties and methods---

    @property
    def lmax(self):
Theo Steininger's avatar
Theo Steininger committed
182
183
        """ Returns the maximal :math:`l` value of any spherical harmonics
        :math:`Y_{lm}` that is represented in this Space.
184
        """
csongor's avatar
csongor committed
185
186
187
188
        return self._lmax

    @property
    def mmax(self):
Theo Steininger's avatar
Theo Steininger committed
189
190
191
192
193
        """ Returns the maximal :math:`m` value of any spherical harmonic
        :math:`Y_{lm}` that is represented in this Space. As :math:`m` goes
        from :math:`-l` to :math:`l` for every :math:`l` this just returns the
        same as lmax.

194
195
        See Also
        --------
Theo Steininger's avatar
Theo Steininger committed
196
197
198
        lmax : Returns the maximal :math:`l`-value of the spherical harmonics
            being used.

199
        """
Theo Steininger's avatar
Theo Steininger committed
200

201
        return self._lmax
csongor's avatar
csongor committed
202
203
204

    def _parse_lmax(self, lmax):
        lmax = np.int(lmax)
205
206
        if lmax < 0:
            raise ValueError("lmax must be >=0.")
csongor's avatar
csongor committed
207
        return lmax
Jait Dixit's avatar
Jait Dixit committed
208
209
210
211
212
213
214
215

    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
        hdf5_group['lmax'] = self.lmax
        return None

    @classmethod
Theo Steininger's avatar
Theo Steininger committed
216
    def _from_hdf5(cls, hdf5_group, repository):
Jait Dixit's avatar
Jait Dixit committed
217
218
219
220
        result = cls(
            lmax=hdf5_group['lmax'][()],
            )
        return result