lmhptransformation.py 3.68 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
    # ---Mandatory properties and methods---

47
48
49
50
    @property
    def unitary(self):
        return False

51
52
    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        """
            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):
74
            raise TypeError("domain needs to be a LMSpace.")
Jait Dixit's avatar
Jait Dixit committed
75

Theo Steininger's avatar
Theo Steininger committed
76
        nside = max((domain.lmax + 1)//2, 1)
Martin Reinecke's avatar
Martin Reinecke committed
77
        result = HPSpace(nside=nside)
78
        return result
Jait Dixit's avatar
Jait Dixit committed
79

80
81
    @classmethod
    def check_codomain(cls, domain, codomain):
Jait Dixit's avatar
Jait Dixit committed
82
        if not isinstance(domain, LMSpace):
83
            raise TypeError("domain is not a LMSpace.")
Jait Dixit's avatar
Jait Dixit committed
84
85

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

88
89
        nside = codomain.nside
        lmax = domain.lmax
Jait Dixit's avatar
Jait Dixit committed
90

91
        if lmax != 2*nside:
92
            cls.logger.warn("Unrecommended: lmax != 2*nside.")
93
94

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

96
97
98
    def _transformation_of_slice(self, inp, **kwargs):
        nside = self.codomain.nside
        lmax = self.domain.lmax
99
        mmax = lmax
Jait Dixit's avatar
Jait Dixit committed
100

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

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

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

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

115
        return result