lmhptransformation.py 3.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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/>.

Jait Dixit's avatar
Jait Dixit committed
19
import numpy as np
20
from nifty.config import dependency_injector as gdi
Jait Dixit's avatar
Jait Dixit committed
21
from nifty import HPSpace, LMSpace
22
from slicing_transformation import SlicingTransformation
23
import lm_transformation_helper
24

25
pyHealpix = gdi.get('pyHealpix')
Jait Dixit's avatar
Jait Dixit committed
26
27


28
29
30
31
class LMHPTransformation(SlicingTransformation):

    # ---Overwritten properties and methods---

Jait Dixit's avatar
Jait Dixit committed
32
    def __init__(self, domain, codomain=None, module=None):
33
34
35
36
37
38
        if module is None:
            module = 'pyHealpix'

        if module != 'pyHealpix':
            raise ValueError("Unsupported SHT module.")

39
        if gdi.get('pyHealpix') is None:
40
            raise ImportError(
41
                "The module pyHealpix is needed but not available.")
Jait Dixit's avatar
Jait Dixit committed
42

43
        super(LMHPTransformation, self).__init__(domain, codomain, module)
Jait Dixit's avatar
Jait Dixit committed
44

45
46
47
48
    # ---Mandatory properties and methods---

    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        """
            Generates a compatible codomain to which transformations are
            reasonable, i.e.\  a pixelization of the two-sphere.

            Parameters
            ----------
            domain : LMSpace
                Space for which a codomain is to be generated

            Returns
            -------
            codomain : HPSpace
                A compatible codomain.

            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.
        """
        if not isinstance(domain, LMSpace):
70
            raise TypeError("domain needs to be a LMSpace.")
Jait Dixit's avatar
Jait Dixit committed
71

Theo Steininger's avatar
Theo Steininger committed
72
        nside = max((domain.lmax + 1)//2, 1)
Martin Reinecke's avatar
Martin Reinecke committed
73
        result = HPSpace(nside=nside)
74
        return result
Jait Dixit's avatar
Jait Dixit committed
75

76
77
    @classmethod
    def check_codomain(cls, domain, codomain):
Jait Dixit's avatar
Jait Dixit committed
78
        if not isinstance(domain, LMSpace):
79
            raise TypeError("domain is not a LMSpace.")
Jait Dixit's avatar
Jait Dixit committed
80
81

        if not isinstance(codomain, HPSpace):
82
            raise TypeError("codomain must be a HPSpace.")
83

84
85
        nside = codomain.nside
        lmax = domain.lmax
Jait Dixit's avatar
Jait Dixit committed
86

87
        if lmax != 2*nside:
88
            cls.logger.warn("Unrecommended: lmax != 2*nside.")
89
90

        super(LMHPTransformation, cls).check_codomain(domain, codomain)
Jait Dixit's avatar
Jait Dixit committed
91

92
    def _transformation_of_slice(self, inp, **kwargs):
93
94
95
96
97
        if inp.dtype not in (np.float, np.complex):
            self.logger.warn("The input array has dtype: %s. The FFT will "
                             "be performed at double precision." %
                             str(inp.dtype))

98
99
        nside = self.codomain.nside
        lmax = self.domain.lmax
100
        mmax = lmax
Jait Dixit's avatar
Jait Dixit committed
101

102
        if issubclass(inp.dtype.type, np.complexfloating):
103
            [resultReal,
104
             resultImag] = [lm_transformation_helper.buildLm(x, lmax=lmax)
105
                            for x in (inp.real, inp.imag)]
106

Theo Steininger's avatar
Theo Steininger committed
107
            [resultReal, resultImag] = [pyHealpix.alm2map(x, lmax, mmax, nside)
108
109
110
                                        for x in [resultReal, resultImag]]

            result = self._combine_complex_result(resultReal, resultImag)
Jait Dixit's avatar
Jait Dixit committed
111
112

        else:
113
            result = lm_transformation_helper.buildLm(inp, lmax=lmax)
Theo Steininger's avatar
Theo Steininger committed
114
            result = pyHealpix.alm2map(result, lmax, mmax, nside)
Jait Dixit's avatar
Jait Dixit committed
115

116
        return result