hp_space.py 5.41 KB
Newer Older
1
2
# NIFTy
# Copyright (C) 2017  Theo Steininger
csongor's avatar
csongor committed
3
#
4
# Author: Theo Steininger
csongor's avatar
csongor committed
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
13
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
csongor's avatar
csongor committed
15
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/>.
csongor's avatar
csongor committed
18
19

import numpy as np
20

21
from nifty.spaces.space import Space
22

Theo Steininger's avatar
Theo Steininger committed
23

Theo Steininger's avatar
Theo Steininger committed
24
class HPSpace(Space):
csongor's avatar
csongor committed
25
26
27
28
29
30
31
32
33
34
35
36
37
    """
        ..        __
        ..      /  /
        ..     /  /___    ______
        ..    /   _   | /   _   |
        ..   /  / /  / /  /_/  /
        ..  /__/ /__/ /   ____/  space class
        ..           /__/

        NIFTY subclass for HEALPix discretizations of the two-sphere [#]_.

        Parameters
        ----------
38
        nside :
csongor's avatar
csongor committed
39
            Resolution parameter for the HEALPix discretization, resulting in
40
            ``12*nside**2`` pixels. Must be positive.
csongor's avatar
csongor committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

        See Also
        --------
        gl_space : A class for the Gauss-Legendre discretization of the
            sphere [#]_.
        lm_space : A class for spherical harmonic components.

        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>`_

    """

59
60
    # ---Overwritten properties and methods---

Martin Reinecke's avatar
Martin Reinecke committed
61
    def __init__(self, nside):
csongor's avatar
csongor committed
62
        """
63
            Sets the attributes for a hp_space class instance.
csongor's avatar
csongor committed
64
65
66
67
68

            Parameters
            ----------
            nside : int
                Resolution parameter for the HEALPix discretization, resulting
69
                in ``12*nside**2`` pixels. Must be positive.
csongor's avatar
csongor committed
70
71
72
73
74
75
76
77

            Returns
            -------
            None

            Raises
            ------
            ValueError
78
                If input `nside` is invalid.
csongor's avatar
csongor committed
79
80
81

        """

Martin Reinecke's avatar
Martin Reinecke committed
82
        super(HPSpace, self).__init__()
csongor's avatar
csongor committed
83

84
        self._nside = self._parse_nside(nside)
csongor's avatar
csongor committed
85

86
87
88
89
90
    # ---Mandatory properties and methods---

    @property
    def harmonic(self):
        return False
csongor's avatar
csongor committed
91
92
93

    @property
    def shape(self):
94
        return (np.int(12 * self.nside ** 2),)
csongor's avatar
csongor committed
95
96

    @property
Jait Dixit's avatar
Jait Dixit committed
97
    def dim(self):
98
        return np.int(12 * self.nside ** 2)
csongor's avatar
csongor committed
99

100
101
102
    @property
    def total_volume(self):
        return 4 * np.pi
103

104
    def copy(self):
105
106
107
108
109
110
        """Returns a copied version of this HPSpace.
			
        Returns
        -------
		HPSpace : A copy of this object.
        """
Martin Reinecke's avatar
Martin Reinecke committed
111
        return self.__class__(nside=self.nside)
112

113
    def weight(self, x, power=1, axes=None, inplace=False):
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
        """ Weights a field living on this space with a specified amount of volume-weights.

		Weights hereby refer to integration weights, as they appear in discretized integrals.
		Per default, this function mutliplies each bin of the field x by its volume, which lets
		it behave like a density (top form). However, different powers of the volume can be applied
		with the power parameter.
        Parameters
        ----------
        x : Field
            A field with this space as domain to be weighted.
        power : int, *optional*
            The power to which the volume-weight is raised.
            (default: 1).
        axes : {int, tuple}, *optional*
            This should not be used. It does nothing.
        inplace : bool, *optional*
            If this is True, the weighting is done on the values of x,
			if it is False, x is not modified and this method returns a 
			weighted copy of x
            (default: False).

        Returns
        -------
		Field
			A weighted version of x, with volume-weights raised to power.
            
        """    

Jait Dixit's avatar
Jait Dixit committed
142
        weight = ((4 * np.pi) / (12 * self.nside**2))**power
143
144
145
146
147
148
149
150

        if inplace:
            x *= weight
            result_x = x
        else:
            result_x = x * weight

        return result_x
151

152
    def get_distance_array(self, distribution_strategy):
153
154
155
156
157
158
159
        """This should not be used, it just raises an error when called.
        
        Raises
        ------
        NotImplementedError
            Always when called.
        """
Theo Steininger's avatar
Theo Steininger committed
160
        raise NotImplementedError
theos's avatar
theos committed
161

162
    def get_fft_smoothing_kernel_function(self, sigma):
163
164
165
166
167
168
169
        """This should not be used, it just raises an error when called.
        
        Raises
        ------
        NotImplementedError
            Always when called.
        """
Theo Steininger's avatar
Theo Steininger committed
170
        raise NotImplementedError
theos's avatar
theos committed
171

172
173
174
175
176
177
178
179
    # ---Added properties and methods---

    @property
    def nside(self):
        return self._nside

    def _parse_nside(self, nside):
        nside = int(nside)
180
181
        if nside < 1:
            raise ValueError("nside must be >=1.")
182
        return nside
183
184
185
186

    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
Jait Dixit's avatar
Jait Dixit committed
187
        hdf5_group['nside'] = self.nside
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
            nside=hdf5_group['nside'][()],
            )
195
        return result