gl_space.py 5.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
from __future__ import division

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

Martin Reinecke's avatar
Martin Reinecke committed
24
25
from ..space import Space
from ...config import dependency_injector as gdi
26

Theo Steininger's avatar
Theo Steininger committed
27
28
29
pyHealpix = gdi.get('pyHealpix')


Theo Steininger's avatar
Theo Steininger committed
30
class GLSpace(Space):
csongor's avatar
csongor committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    """
        ..                 __
        ..               /  /
        ..     ____ __  /  /
        ..   /   _   / /  /
        ..  /  /_/  / /  /_
        ..  \___   /  \___/  space class
        .. /______/

        NIFTY subclass for Gauss-Legendre pixelizations [#]_ of the two-sphere.

        Parameters
        ----------
        nlat : int
45
46
            Number of latitudinal bins (or rings) that are used for this
            pixelization.
47
        nlon : int, *optional*
48
            Number of longitudinal bins that are used for this pixelization.
49

50
        Attributes
csongor's avatar
csongor committed
51
        ----------
52
53
54
55
        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.
csongor's avatar
csongor committed
56
        nlat : int
57
58
            Number of latitudinal bins (or rings) that are used for this
            pixelization.
59
        nlon : int
60
            Number of longitudinal bins that are used for this pixelization.
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.
csongor's avatar
csongor committed
65

Theo Steininger's avatar
Theo Steininger committed
66
67
68
69
70
71
72
        Raises
        ------
        ValueError
            If input `nlat` or `nlon` is invalid.
        ImportError
            If the pyHealpix module is not available

csongor's avatar
csongor committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        See Also
        --------
        hp_space : A class for the HEALPix discretization of the sphere [#]_.
        lm_space : A class for spherical harmonic components.

        References
        ----------
        .. [#] M. Reinecke and D. Sverre Seljebotn, 2013, "Libsharp - spherical
               harmonic transforms revisited";
               `arXiv:1303.4945 <http://www.arxiv.org/abs/1303.4945>`_
        .. [#] 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.

    """

89
90
    # ---Overwritten properties and methods---

Martin Reinecke's avatar
Martin Reinecke committed
91
    def __init__(self, nlat, nlon=None):
92
        if pyHealpix is None:
Theo Steininger's avatar
Theo Steininger committed
93
94
            raise ImportError(
                "The module pyHealpix is needed but not available.")
95

Martin Reinecke's avatar
Martin Reinecke committed
96
        super(GLSpace, self).__init__()
97
        self._needed_for_hash += ["_nlat", "_nlon"]
csongor's avatar
csongor committed
98

99
100
        self._nlat = self._parse_nlat(nlat)
        self._nlon = self._parse_nlon(nlon)
csongor's avatar
csongor committed
101

102
    # ---Mandatory properties and methods---
csongor's avatar
csongor committed
103

104
105
106
    def __repr__(self):
        return ("GLSpace(nlat=%r, nlon=%r)" % (self.nlat, self.nlon))

107
108
109
    @property
    def harmonic(self):
        return False
csongor's avatar
csongor committed
110
111
112

    @property
    def shape(self):
113
        return (np.int((self.nlat * self.nlon)),)
csongor's avatar
csongor committed
114

115
    @property
116
    def dim(self):
117
        return np.int((self.nlat * self.nlon))
118
119
120
121

    @property
    def total_volume(self):
        return 4 * np.pi
122

123
124
    def copy(self):
        return self.__class__(nlat=self.nlat,
Martin Reinecke's avatar
Martin Reinecke committed
125
                              nlon=self.nlon)
126

Jait Dixit's avatar
Jait Dixit committed
127
    def weight(self, x, power=1, axes=None, inplace=False):
128
129
        nlon = self.nlon
        nlat = self.nlat
130
        vol = pyHealpix.GL_weights(nlat, nlon) ** np.float(power)
131
        weight = np.array(list(itertools.chain.from_iterable(
Theo Steininger's avatar
Theo Steininger committed
132
                          itertools.repeat(x, nlon) for x in vol)))
Jait Dixit's avatar
Jait Dixit committed
133
134
135

        if axes is not None:
            # reshape the weight array to match the input shape
136
            new_shape = np.ones(len(x.shape), dtype=np.int)
137
138
            # we know len(axes) is always 1
            new_shape[axes[0]] = len(weight)
Jait Dixit's avatar
Jait Dixit committed
139
140
141
142
143
            weight = weight.reshape(new_shape)

        if inplace:
            x *= weight
            result_x = x
csongor's avatar
csongor committed
144
        else:
Jait Dixit's avatar
Jait Dixit committed
145
            result_x = x * weight
csongor's avatar
csongor committed
146

Jait Dixit's avatar
Jait Dixit committed
147
        return result_x
148
149
150
151
152

    # ---Added properties and methods---

    @property
    def nlat(self):
Theo Steininger's avatar
Theo Steininger committed
153
154
        """ Number of latitudinal bins (or rings) that are used for this
        pixelization.
155
        """
Theo Steininger's avatar
Theo Steininger committed
156

157
158
159
160
        return self._nlat

    @property
    def nlon(self):
161
        """ Number of longitudinal bins that are used for this pixelization.
162
        """
Theo Steininger's avatar
Theo Steininger committed
163

164
165
166
167
        return self._nlon

    def _parse_nlat(self, nlat):
        nlat = int(nlat)
168
169
170
        if nlat < 1:
            raise ValueError(
                "nlat must be a positive number.")
171
172
173
174
175
176
177
        return nlat

    def _parse_nlon(self, nlon):
        if nlon is None:
            nlon = 2 * self.nlat - 1
        else:
            nlon = int(nlon)
178
179
            if nlon < 1:
                raise ValueError("nlon must be a positive number.")
180
        return nlon
181
182
183
184

    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
Jait Dixit's avatar
Jait Dixit committed
185
186
187
        hdf5_group['nlat'] = self.nlat
        hdf5_group['nlon'] = self.nlon

188
189
190
        return None

    @classmethod
Theo Steininger's avatar
Theo Steininger committed
191
    def _from_hdf5(cls, hdf5_group, repository):
192
        result = cls(
Jait Dixit's avatar
Jait Dixit committed
193
194
195
196
            nlat=hdf5_group['nlat'][()],
            nlon=hdf5_group['nlon'][()],
            )

197
        return result