hplmtransformation.py 3.26 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/>.

19
import numpy as np
20
21

from nifty.config import dependency_injector as gdi
Jait Dixit's avatar
Jait Dixit committed
22
from nifty import HPSpace, LMSpace
23
from slicing_transformation import SlicingTransformation
24

25
import lm_transformation_factory
csongor's avatar
csongor committed
26

27
pyHealpix = gdi.get('pyHealpix')
28
29


30
31
32
33
class HPLMTransformation(SlicingTransformation):

    # ---Overwritten properties and methods---

Jait Dixit's avatar
Jait Dixit committed
34
    def __init__(self, domain, codomain=None, module=None):
35
        if 'pyHealpix' not in gdi:
36
            raise ImportError(
37
                "The module pyHealpix is needed but not available")
Jait Dixit's avatar
Jait Dixit committed
38

39
40
        super(HPLMTransformation, self).__init__(domain, codomain,
                                                 module=module)
41
42
43
44
45

    # ---Mandatory properties and methods---

    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        """
            Generates a compatible codomain to which transformations are
            reasonable, i.e.\  an instance of the :py:class:`lm_space` class.

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

            Returns
            -------
            codomain : LMSpace
                A compatible codomain.
        """

        if not isinstance(domain, HPSpace):
62
            raise TypeError("domain needs to be a HPSpace")
Jait Dixit's avatar
Jait Dixit committed
63

64
        lmax = 2*domain.nside
65

66
        result = LMSpace(lmax=lmax, dtype=domain.dtype)
67
        return result
Jait Dixit's avatar
Jait Dixit committed
68

69
70
    @classmethod
    def check_codomain(cls, domain, codomain):
Jait Dixit's avatar
Jait Dixit committed
71
        if not isinstance(domain, HPSpace):
72
            raise TypeError("domain is not a HPSpace")
Jait Dixit's avatar
Jait Dixit committed
73
74

        if not isinstance(codomain, LMSpace):
75
            raise TypeError("codomain must be a LMSpace.")
Jait Dixit's avatar
Jait Dixit committed
76

77
        lmax = codomain.lmax
78
79
80
81
        nside = domain.nside

        if lmax != 2*nside:
            cls.Logger.warn("Unrecommended: lmax != 2*nside.")
Jait Dixit's avatar
Jait Dixit committed
82

83
        super(HPLMTransformation, cls).check_codomain(domain, codomain)
Jait Dixit's avatar
Jait Dixit committed
84

85
86
    def _transformation_of_slice(self, inp, **kwargs):
        lmax = self.codomain.lmax
87
        mmax = lmax
Jait Dixit's avatar
Jait Dixit committed
88

89
        if issubclass(inp.dtype.type, np.complexfloating):
Theo Steininger's avatar
Theo Steininger committed
90
91
92
            [resultReal,
             resultImag] = [pyHealpix.map2alm_iter(x, lmax, mmax, 3)
                            for x in (inp.real, inp.imag)]
93

94
95
96
            [resultReal,
             resultImag] = [lm_transformation_factory.buildIdx(x, lmax=lmax)
                            for x in [resultReal, resultImag]]
97

98
            result = self._combine_complex_result(resultReal, resultImag)
99
100

        else:
Theo Steininger's avatar
Theo Steininger committed
101
            result = pyHealpix.map2alm_iter(inp, lmax, mmax, 3)
102
            result = lm_transformation_factory.buildIdx(result, lmax=lmax)
103

104
        return result