hplmtransformation.py 2.97 KB
Newer Older
1
import numpy as np
Jait Dixit's avatar
Jait Dixit committed
2
from transformation import Transformation
3
4
5
from d2o import distributed_data_object
from nifty.config import dependency_injector as gdi
import nifty.nifty_utilities as utilities
Jait Dixit's avatar
Jait Dixit committed
6
from nifty import HPSpace, LMSpace
7
8
9
10

hp = gdi.get('healpy')


Jait Dixit's avatar
Jait Dixit committed
11
12
class HPLMTransformation(Transformation):
    def __init__(self, domain, codomain, module=None):
13
14
15
        if 'healpy' not in gdi:
            raise ImportError("The module healpy is needed but not available")

Jait Dixit's avatar
Jait Dixit committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
        if self.check_codomain(domain, codomain):
            self.domain = domain
            self.codomain = codomain
        else:
            raise ValueError("ERROR: Incompatible codomain!")

    @staticmethod
    def check_codomain(domain, codomain):
        if not isinstance(domain, HPSpace):
            raise TypeError('ERROR: domain is not a HPSpace')

        if codomain is None:
            return False

        if not isinstance(codomain, LMSpace):
            raise TypeError('ERROR: codomain must be a LMSpace.')

        nside = domain.paradict['nside']
        lmax = codomain.paradict['lmax']
        mmax = codomain.paradict['mmax']

        if (3 * nside - 1 != lmax) or (lmax != mmax):
            return False

        return True

    def transform(self, val, axes=None, **kwargs):
        """
        HP -> LM transform method.

        Parameters
        ----------
        val : np.ndarray or distributed_data_object
            The value array which is to be transformed

        axes : None or tuple
            The axes along which the transformation should take place

        """
55
56
57
58
        # get by number of iterations from kwargs
        niter = kwargs['niter'] if 'niter' in kwargs else 0

        if self.domain.discrete:
59
            val = self.domain.calc_weight(val, power=-0.5)
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

        # shorthands for transform parameters
        lmax = self.codomain.paradict['lmax']
        mmax = self.codomain.paradict['mmax']

        if isinstance(val, distributed_data_object):
            temp_val = val.get_full_data()
        else:
            temp_val = val

        return_val = None

        for slice_list in utilities.get_slice_list(temp_val.shape, axes):
            if slice_list == [slice(None, None)]:
                inp = temp_val
            else:
                if return_val is None:
                    return_val = np.empty_like(temp_val)
                inp = temp_val[slice_list]

            inp = hp.map2alm(inp.astype(np.float64, copy=False),
                             lmax=lmax, mmax=mmax, iter=niter, pol=True,
                             use_weights=False, datapath=None)

            if slice_list == [slice(None, None)]:
                return_val = inp
            else:
                return_val[slice_list] = inp

        if isinstance(val, distributed_data_object):
            new_val = val.copy_empty(dtype=self.codomain.dtype)
            new_val.set_full_data(return_val, copy=False)
        else:
            return_val = return_val.astype(self.codomain.dtype, copy=False)

Jait Dixit's avatar
Jait Dixit committed
95
        return return_val