hplmtransformation.py 3.1 KB
Newer Older
1
import numpy as np
2
3

from nifty.config import dependency_injector as gdi
Jait Dixit's avatar
Jait Dixit committed
4
from nifty import HPSpace, LMSpace
5
from slicing_transformation import SlicingTransformation
6

csongor's avatar
csongor committed
7
8
import lm_transformation_factory as ltf

9
10
11
hp = gdi.get('healpy')


12
13
14
15
class HPLMTransformation(SlicingTransformation):

    # ---Overwritten properties and methods---

Jait Dixit's avatar
Jait Dixit committed
16
    def __init__(self, domain, codomain=None, module=None):
17
        if 'healpy' not in gdi:
18
19
            raise ImportError(
                "The module healpy is needed but not available")
Jait Dixit's avatar
Jait Dixit committed
20

21
22
        super(HPLMTransformation, self).__init__(domain, codomain,
                                                 module=module)
23
24
25
26
27

    # ---Mandatory properties and methods---

    @classmethod
    def get_codomain(cls, domain):
Jait Dixit's avatar
Jait Dixit committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
        """
            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):
44
45
            raise TypeError(
                "domain needs to be a HPSpace")
Jait Dixit's avatar
Jait Dixit committed
46

47
        lmax = 3 * domain.nside - 1
48

49
        result = LMSpace(lmax=lmax, dtype=np.dtype('float64'))
50
51
        cls.check_codomain(domain, result)
        return result
Jait Dixit's avatar
Jait Dixit committed
52

Jait Dixit's avatar
Jait Dixit committed
53
54
55
    @staticmethod
    def check_codomain(domain, codomain):
        if not isinstance(domain, HPSpace):
56
57
            raise TypeError(
                'ERROR: domain is not a HPSpace')
Jait Dixit's avatar
Jait Dixit committed
58
59

        if not isinstance(codomain, LMSpace):
60
61
            raise TypeError(
                'ERROR: codomain must be a LMSpace.')
Jait Dixit's avatar
Jait Dixit committed
62

63
64
        nside = domain.nside
        lmax = codomain.lmax
Jait Dixit's avatar
Jait Dixit committed
65

66
        if 3 * nside - 1 != lmax:
67
68
            raise ValueError(
                'ERROR: codomain has 3*nside-1 != lmax.')
Jait Dixit's avatar
Jait Dixit committed
69

70
        return None
Jait Dixit's avatar
Jait Dixit committed
71

72
73
    def _transformation_of_slice(self, inp, **kwargs):
        lmax = self.codomain.lmax
74
        mmax = lmax
Jait Dixit's avatar
Jait Dixit committed
75

76
77
78
79
80
81
82
83
84
        if issubclass(inp.dtype.type, np.complexfloating):
            [resultReal, resultImag] = [hp.map2alm(x.astype(np.float64,
                                                            copy=False),
                                                   lmax=lmax,
                                                   mmax=mmax,
                                                   pol=True,
                                                   use_weights=False,
                                                   **kwargs)
                                        for x in (inp.real, inp.imag)]
85

86
87
            [resultReal, resultImag] = [ltf.buildIdx(x, lmax=lmax)
                                        for x in [resultReal, resultImag]]
88

89
            result = self._combine_complex_result(resultReal, resultImag)
90
91

        else:
92
93
94
95
            result = hp.map2alm(inp.astype(np.float64, copy=False),
                                lmax=lmax, mmax=mmax, pol=True,
                                use_weights=False)
            result = ltf.buildIdx(result, lmax=lmax)
96

97
        return result