hp_space.py 4.32 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
22
import d2o

23
from nifty.spaces.space import Space
24

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

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

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

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

        Attributes
        ----------
        dtype : numpy.dtype
            Data type of the field values, which is always numpy.float64.
    """

64
65
    # ---Overwritten properties and methods---

66
    def __init__(self, nside, dtype=None):
csongor's avatar
csongor committed
67
68
69
70
71
72
73
        """
            Sets the attributes for a hp_space class instance.

            Parameters
            ----------
            nside : int
                Resolution parameter for the HEALPix discretization, resulting
74
                in ``12*nside**2`` pixels. Must be positive.
csongor's avatar
csongor committed
75
76
77
78
79
80
81
82

            Returns
            -------
            None

            Raises
            ------
            ValueError
83
                If input `nside` is invalid.
csongor's avatar
csongor committed
84
85
86

        """

87
        super(HPSpace, self).__init__(dtype)
csongor's avatar
csongor committed
88

89
        self._nside = self._parse_nside(nside)
csongor's avatar
csongor committed
90

91
92
93
94
95
    # ---Mandatory properties and methods---

    @property
    def harmonic(self):
        return False
csongor's avatar
csongor committed
96
97
98

    @property
    def shape(self):
99
        return (np.int(12 * self.nside ** 2),)
csongor's avatar
csongor committed
100
101

    @property
Jait Dixit's avatar
Jait Dixit committed
102
    def dim(self):
103
        return np.int(12 * self.nside ** 2)
csongor's avatar
csongor committed
104

105
106
107
    @property
    def total_volume(self):
        return 4 * np.pi
108

109
110
111
112
    def copy(self):
        return self.__class__(nside=self.nside,
                              dtype=self.dtype)

113
    def weight(self, x, power=1, axes=None, inplace=False):
Jait Dixit's avatar
Jait Dixit committed
114
        weight = ((4 * np.pi) / (12 * self.nside**2))**power
115
116
117
118
119
120
121
122

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

        return result_x
123

124
    def get_distance_array(self, distribution_strategy):
125
126
        raise NotImplementedError \
            ("get_distance_array only works on spaces with a zero point.")
theos's avatar
theos committed
127

128
    def get_fft_smoothing_kernel_function(self, sigma):
129
130
        raise NotImplementedError \
            ("get_fft_smoothing_kernel not supported by this space.")
theos's avatar
theos committed
131

132
133
134
135
136
137
138
139
    # ---Added properties and methods---

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

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

    # ---Serialization---

    def _to_hdf5(self, hdf5_group):
Jait Dixit's avatar
Jait Dixit committed
147
        hdf5_group['nside'] = self.nside
148
        hdf5_group.attrs['dtype'] = self.dtype.name
149
150
151
        return None

    @classmethod
Theo Steininger's avatar
Theo Steininger committed
152
    def _from_hdf5(cls, hdf5_group, repository):
153
        result = cls(
Jait Dixit's avatar
Jait Dixit committed
154
            nside=hdf5_group['nside'][()],
155
            dtype=np.dtype(hdf5_group.attrs['dtype'])
Jait Dixit's avatar
Jait Dixit committed
156
            )
157
        return result