lmhptransformation.py 4.33 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
24
import lm_transformation_factory as ltf

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


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):
Jait Dixit's avatar
Jait Dixit committed
33
        if gdi.get('healpy') is None:
34
35
            raise ImportError(
                "The module libsharp is needed but not available.")
Jait Dixit's avatar
Jait Dixit committed
36

37
38
        super(LMHPTransformation, self).__init__(domain, codomain,
                                                 module=module)
Jait Dixit's avatar
Jait Dixit committed
39

40
41
42
43
    # ---Mandatory properties and methods---

    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        """
            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):
65
66
            raise TypeError(
                'ERROR: domain needs to be a LMSpace')
Jait Dixit's avatar
Jait Dixit committed
67

68
        nside = (domain.lmax + 1) // 3
69
70
71
        result = HPSpace(nside=nside)
        cls.check_codomain(domain, result)
        return result
Jait Dixit's avatar
Jait Dixit committed
72

Jait Dixit's avatar
Jait Dixit committed
73
74
75
    @staticmethod
    def check_codomain(domain, codomain):
        if not isinstance(domain, LMSpace):
76
77
            raise TypeError(
                'ERROR: domain is not a LMSpace')
Jait Dixit's avatar
Jait Dixit committed
78
79

        if not isinstance(codomain, HPSpace):
80
81
            raise TypeError(
                'ERROR: codomain must be a HPSpace.')
82

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

86
        if 3*nside - 1 != lmax:
87
88
            raise ValueError(
                'ERROR: codomain has 3*nside -1 != lmax.')
Jait Dixit's avatar
Jait Dixit committed
89

90
        return None
Jait Dixit's avatar
Jait Dixit committed
91

92
93
94
    def _transformation_of_slice(self, inp, **kwargs):
        nside = self.codomain.nside
        lmax = self.domain.lmax
95
        mmax = lmax
Jait Dixit's avatar
Jait Dixit committed
96

97
98
99
100
        if issubclass(inp.dtype.type, np.complexfloating):
            [resultReal, resultImag] = [ltf.buildLm(x, lmax=lmax)
                                        for x in (inp.real, inp.imag)]

101
            [resultReal, resultImag] = [hp.alm2map(x.astype(np.complex128,
102
103
104
105
106
107
108
109
110
111
112
113
114
                                                            copy=False),
                                                   nside,
                                                   lmax=lmax,
                                                   mmax=mmax,
                                                   pixwin=False,
                                                   fwhm=0.0,
                                                   sigma=None,
                                                   pol=True,
                                                   inplace=False,
                                                   **kwargs)
                                        for x in [resultReal, resultImag]]

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

        else:
117
118
119
120
            result = ltf.buildLm(inp, lmax=lmax)
            result = hp.alm2map(result.astype(np.complex128, copy=False),
                                nside, lmax=lmax, mmax=mmax, pixwin=False,
                                fwhm=0.0, sigma=None, pol=True, inplace=False)
Jait Dixit's avatar
Jait Dixit committed
121

122
        return result