amplitude_model.py 4.91 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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/>.
#
# Copyright(C) 2013-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

19
from __future__ import absolute_import, division, print_function
Philipp Arras's avatar
Philipp Arras committed
20

21
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
22
23

from ..compat import *
Martin Reinecke's avatar
Martin Reinecke committed
24
from ..domains.power_space import PowerSpace
25
from ..field import Field
Martin Reinecke's avatar
Martin Reinecke committed
26
from ..multi_domain import MultiDomain
Martin Reinecke's avatar
Martin Reinecke committed
27
from ..operators.operator import Operator
Philipp Arras's avatar
Philipp Arras committed
28
from ..sugar import makeOp, sqrt
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


def _ceps_kernel(dof_space, k, a, k0):
    return a**2/(1+(k/(k0*dof_space.bindistances[0]))**2)**2


def create_cepstrum_amplitude_field(domain, cepstrum):
    """Creates a ...
    Writes the sum of all modes into the zero-mode.

    Parameters
    ----------
    domain: ???
        ???
    cepstrum: Callable
        ???
    """

    dim = len(domain.shape)
    dist = domain.bindistances
    shape = domain.shape

    # Prepare q_array
    q_array = np.zeros((dim,) + shape)
53
54
55
56
    for i in range(dim):
        ks = np.zeros(shape[i])
        ks[1:] = np.minimum(shape[i] - 1 - np.arange(shape[i]-1), np.arange(shape[i]-1)) * dist[i]
        q_array[i] += ks.reshape((1,)*i + (shape[i],) + (1,)*(dim-i-1))
57
58
59
60
61
62
63
64
65
66

    # Fill cepstrum field (all non-zero modes)
    no_zero_modes = (slice(1, None),) * dim
    ks = q_array[(slice(None),) + no_zero_modes]
    cepstrum_field = np.zeros(shape)
    cepstrum_field[no_zero_modes] = cepstrum(ks)

    # Fill cepstrum field (zero-mode subspaces)
    for i in range(dim):
        # Prepare indices
Martin Reinecke's avatar
Martin Reinecke committed
67
68
69
        fst_dims = (slice(None),)*i
        sl = fst_dims + (slice(1, None),)
        sl2 = fst_dims + (0,)
70
71
72
73

        # Do summation
        cepstrum_field[sl2] = np.sum(cepstrum_field[sl], axis=i)

74
    return Field.from_global_data(domain, cepstrum_field)
Martin Reinecke's avatar
Martin Reinecke committed
75

Martin Reinecke's avatar
Martin Reinecke committed
76

Martin Reinecke's avatar
Martin Reinecke committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class AmplitudeModel(Operator):
    '''
    Computes a smooth power spectrum.
    Output lives in PowerSpace.

    Parameters
    ----------

    Npixdof : #pix in dof_space

    ceps_a, ceps_k0 : Smoothness parameters in ceps_kernel
                        eg. ceps_kernel(k) = (a/(1+(k/k0)**2))**2
                        a = ceps_a,  k0 = ceps_k0

    sm, sv : slope_mean = expected exponent of power law (e.g. -4),
                slope_variance (default=1)

    im, iv : y-intercept_mean, y-intercept_variance  of power_slope
    '''
    def __init__(self, s_space, Npixdof, ceps_a, ceps_k, sm, sv, im, iv,
Martin Reinecke's avatar
Martin Reinecke committed
97
                 keys=['tau', 'phi']):
Martin Reinecke's avatar
Martin Reinecke committed
98
99
100
101
102
103
        from ..operators.exp_transform import ExpTransform
        from ..operators.qht_operator import QHTOperator
        from ..operators.slope_operator import SlopeOperator
        from ..operators.symmetrizing_operator import SymmetrizingOperator

        h_space = s_space.get_default_codomain()
104
        self._exp_transform = ExpTransform(PowerSpace(h_space), Npixdof)
Martin Reinecke's avatar
Martin Reinecke committed
105
106
107
108
109
        logk_space = self._exp_transform.domain[0]
        qht = QHTOperator(target=logk_space)
        dof_space = qht.domain[0]
        sym = SymmetrizingOperator(logk_space)

110
        phi_mean = np.array([sm, im+sm*logk_space.t_0[0]])
Martin Reinecke's avatar
Martin Reinecke committed
111
112
        phi_sig = np.array([sv, iv])

113
114
115
        self._slope = SlopeOperator(logk_space)
        self._slope = self._slope(makeOp(Field.from_global_data(
                                    self._slope.domain,phi_sig)))
116
        self._norm_phi_mean = Field.from_global_data(self._slope.domain,
Martin Reinecke's avatar
Martin Reinecke committed
117
                                                     phi_mean/phi_sig)
Martin Reinecke's avatar
Martin Reinecke committed
118

Martin Reinecke's avatar
Martin Reinecke committed
119
        self._domain = MultiDomain.make({keys[0]: dof_space,
120
                                         keys[1]: self._slope.domain})
Martin Reinecke's avatar
Martin Reinecke committed
121
        self._target = self._exp_transform.target
Martin Reinecke's avatar
Martin Reinecke committed
122
123
124
125

        kern = lambda k: _ceps_kernel(dof_space, k, ceps_a, ceps_k)
        cepstrum = create_cepstrum_amplitude_field(dof_space, kern)

Martin Reinecke's avatar
Martin Reinecke committed
126
        self._smooth_op = sym(qht(makeOp(sqrt(cepstrum))))
Martin Reinecke's avatar
Martin Reinecke committed
127
128
        self._keys = tuple(keys)

Philipp Arras's avatar
Changes    
Philipp Arras committed
129
130
131
        self._qht = qht
        self._ceps = makeOp(sqrt(cepstrum))

Martin Reinecke's avatar
Martin Reinecke committed
132
    def apply(self, x):
133
        self._check_input(x)
Martin Reinecke's avatar
Martin Reinecke committed
134
135
136
137
        smooth_spec = self._smooth_op(x[self._keys[0]])
        phi = x[self._keys[1]] + self._norm_phi_mean
        linear_spec = self._slope(phi)
        loglog_spec = smooth_spec + linear_spec
Philipp Frank's avatar
Philipp Frank committed
138
        return self._exp_transform((0.5*loglog_spec)).exp()
Philipp Arras's avatar
Changes    
Philipp Arras committed
139
140
141
142
143
144
145
146

    @property
    def qht(self):
        return self._qht

    @property
    def ceps(self):
        return self._ceps
Philipp Arras's avatar
Philipp Arras committed
147
148
149
150

    @property
    def norm_phi_mean(self):
        return self._norm_phi_mean