hp_space.py 4.32 KB
Newer Older
csongor's avatar
csongor committed
1
2
3
4
5
6
7
# 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
8
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
csongor's avatar
csongor committed
10
11
#
# You should have received a copy of the GNU General Public License
12
# 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.
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
39
40
41
        nside : int
            The corresponding HEALPix pixelization. The total number of pixels
            is 12*nside**2.

42
        Attributes
csongor's avatar
csongor committed
43
        ----------
44
45
46
47
48
        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.
        nside : int
49
            The corresponding HEALPix pixelization. The total number of pixels
50
51
52
53
54
            is 12*nside**2.
        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
55

Theo Steininger's avatar
Theo Steininger committed
56
57
58
59
60
        Raises
        ------
        ValueError
            If given `nside` < 1.

csongor's avatar
csongor committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        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>`_

    """

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

Martin Reinecke's avatar
Martin Reinecke committed
80
81
    def __init__(self, nside):
        super(HPSpace, self).__init__()
csongor's avatar
csongor committed
82

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

85
86
    # ---Mandatory properties and methods---

87
88
89
    def __repr__(self):
        return ("HPSpace(nside=%r)" % self.nside)

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

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

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

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

106
    def copy(self):
Martin Reinecke's avatar
Martin Reinecke committed
107
        return self.__class__(nside=self.nside)
108

109
    def weight(self, x, power=1, axes=None, inplace=False):
110

111
        weight = ((4*np.pi) / (12*self.nside*self.nside)) ** np.float(power)
112
113
114
115
116
117
118
119

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

        return result_x
120

121
    def get_distance_array(self, distribution_strategy):
Theo Steininger's avatar
Theo Steininger committed
122
        raise NotImplementedError
theos's avatar
theos committed
123

124
    def get_fft_smoothing_kernel_function(self, sigma):
Theo Steininger's avatar
Theo Steininger committed
125
        raise NotImplementedError
theos's avatar
theos committed
126

127
128
129
130
    # ---Added properties and methods---

    @property
    def nside(self):
Theo Steininger's avatar
Theo Steininger committed
131
132
        """ Returns the nside of the corresponding HEALPix pixelization.
        The total number of pixels is 12*nside**2
133
        """
134
135
136
137
        return self._nside

    def _parse_nside(self, nside):
        nside = int(nside)
138
139
        if nside < 1:
            raise ValueError("nside must be >=1.")
140
        return nside
141
142
143
144

    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
Jait Dixit's avatar
Jait Dixit committed
145
        hdf5_group['nside'] = self.nside
146
147
148
        return None

    @classmethod
Theo Steininger's avatar
Theo Steininger committed
149
    def _from_hdf5(cls, hdf5_group, repository):
150
        result = cls(
Jait Dixit's avatar
Jait Dixit committed
151
152
            nside=hdf5_group['nside'][()],
            )
153
        return result