rgrgtransformation.py 4.83 KB
Newer Older
Jait Dixit's avatar
Jait Dixit committed
1
2
3
4
5
6
7
8
import numpy as np
from transformation import Transformation
from rg_transforms import FFTW, GFFT
from nifty.config import about, dependency_injector as gdi
from nifty import RGSpace, nifty_configuration


class RGRGTransformation(Transformation):
Jait Dixit's avatar
Jait Dixit committed
9
    def __init__(self, domain, codomain=None, module=None):
Jait Dixit's avatar
Jait Dixit committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        if codomain is None:
            codomain = self.get_codomain(domain)
        else:
            if not self.check_codomain(domain, codomain):
                raise ValueError("ERROR: incompatible codomain!")

        if module is None:
            if nifty_configuration['fft_module'] == 'pyfftw':
                self._transform = FFTW(domain, codomain)
            elif nifty_configuration['fft_module'] == 'gfft' or \
                nifty_configuration['fft_module'] == 'gfft_dummy':
                self._transform = \
                    GFFT(domain,
                         codomain,
                         gdi.get(nifty_configuration['fft_module']))
Jait Dixit's avatar
Jait Dixit committed
25
            else:
Jait Dixit's avatar
Jait Dixit committed
26
27
                raise ValueError('ERROR: unknow default FFT module:' +
                                 nifty_configuration['fft_module'])
Jait Dixit's avatar
Jait Dixit committed
28
        else:
Jait Dixit's avatar
Jait Dixit committed
29
30
31
32
33
34
35
36
37
38
            if module == 'pyfftw':
                self._transform = FFTW(domain, codomain)
            elif module == 'gfft':
                self._transform = \
                    GFFT(domain, codomain, gdi.get('gfft'))
            elif module == 'gfft_dummy':
                self._transform = \
                    GFFT(domain, codomain, gdi.get('gfft_dummy'))
            else:
                raise ValueError('ERROR: unknow FFT module:' + module)
Jait Dixit's avatar
Jait Dixit committed
39

Jait Dixit's avatar
Jait Dixit committed
40
    @staticmethod
theos's avatar
theos committed
41
    def get_codomain(domain, dtype=None, zerocenter=None, **kwargs):
Jait Dixit's avatar
Jait Dixit committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
        """
            Generates a compatible codomain to which transformations are
            reasonable, i.e.\  either a shifted grid or a Fourier conjugate
            grid.

            Parameters
            ----------
            domain: RGSpace
                Space for which a codomain is to be generated
            cozerocenter : {bool, numpy.ndarray}, *optional*
                Whether or not the grid is zerocentered for each axis or not
                (default: None).

            Returns
            -------
            codomain : nifty.rg_space
                A compatible codomain.
        """
        if not isinstance(domain, RGSpace):
            raise TypeError('ERROR: domain needs to be a RGSpace')

        # parse the cozerocenter input
theos's avatar
theos committed
64
65
        if zerocenter is None:
            zerocenter = domain.paradict['zerocenter']
Jait Dixit's avatar
Jait Dixit committed
66
67
        # if the input is something scalar, cast it to a boolean
        else:
theos's avatar
theos committed
68
69
            temp = np.empty_like(domain.paradict['zerocenter'])
            temp[:] = zerocenter
70
            zerocenter = temp
Jait Dixit's avatar
Jait Dixit committed
71
72
73

        # calculate the initialization parameters
        distances = 1 / (np.array(domain.paradict['shape']) *
theos's avatar
theos committed
74
75
76
                         np.array(domain.paradict['distances']))
        if dtype is None:
            dtype = np.complex
Jait Dixit's avatar
Jait Dixit committed
77

theos's avatar
theos committed
78
79
80
81
82
        new_space = RGSpace(domain.paradict['shape'],
                            zerocenter=zerocenter,
                            distances=distances,
                            harmonic=(not domain.harmonic),
                            dtype=dtype)
Jait Dixit's avatar
Jait Dixit committed
83
84
85

        return new_space

Jait Dixit's avatar
Jait Dixit committed
86
87
88
89
90
91
92
93
94
    @staticmethod
    def check_codomain(domain, codomain):
        if not isinstance(domain, RGSpace):
            raise TypeError('ERROR: domain must be a RGSpace')

        if codomain is None:
            return False

        if not isinstance(codomain, RGSpace):
theos's avatar
theos committed
95
            return False
Jait Dixit's avatar
Jait Dixit committed
96
97

        if not np.all(np.array(domain.paradict['shape']) ==
theos's avatar
theos committed
98
                      np.array(codomain.paradict['shape'])):
Jait Dixit's avatar
Jait Dixit committed
99
100
101
102
103
104
105
106
            return False

        if domain.harmonic == codomain.harmonic:
            return False

        # Check if the distances match, i.e. dist' = 1 / (num * dist)
        if not np.all(
            np.absolute(np.array(domain.paradict['shape']) *
theos's avatar
theos committed
107
108
109
                        np.array(domain.paradict['distances']) *
                        np.array(codomain.paradict['distances']) - 1) <
                10**-7):
Jait Dixit's avatar
Jait Dixit committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
            return False

        return True

    def transform(self, val, axes=None, **kwargs):
        """
        RG -> RG 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

        """
        if self._transform.codomain.harmonic:
            # correct for forward fft
Jait Dixit's avatar
Jait Dixit committed
129
            val = self._transform.domain.weight(val, power=1, axes=axes)
Jait Dixit's avatar
Jait Dixit committed
130
131
132
133
134
135

        # Perform the transformation
        Tval = self._transform.transform(val, axes, **kwargs)

        if not self._transform.codomain.harmonic:
            # correct for inverse fft
Jait Dixit's avatar
Jait Dixit committed
136
            Tval = self._transform.codomain.weight(Tval, power=-1, axes=axes)
Jait Dixit's avatar
Jait Dixit committed
137
138

        return Tval