smooth_linear_amplitude.py 11.9 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
14
# Copyright(C) 2013-2019 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
#
16
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Philipp Arras's avatar
Philipp Arras committed
17

18
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19

Philipp Arras's avatar
Docs  
Philipp Arras committed
20
from ..domain_tuple import DomainTuple
Martin Reinecke's avatar
Martin Reinecke committed
21
from ..domains.power_space import PowerSpace
Philipp Arras's avatar
Philipp Arras committed
22
from ..domains.unstructured_domain import UnstructuredDomain
23
from ..field import Field
Philipp Arras's avatar
Philipp Arras committed
24
from ..operators.adder import Adder
Philipp Arras's avatar
Philipp Arras committed
25
26
from ..operators.distributors import PowerDistributor
from ..operators.endomorphic_operator import EndomorphicOperator
Philipp Arras's avatar
Philipp Arras committed
27
from ..operators.exp_transform import ExpTransform
Philipp Arras's avatar
Philipp Arras committed
28
29
from ..operators.linear_operator import LinearOperator
from ..operators.operator import Operator
Philipp Arras's avatar
Philipp Arras committed
30
from ..operators.qht_operator import QHTOperator
Philipp Arras's avatar
Philipp Arras committed
31
from ..operators.simple_linear_operators import VdotOperator, ducktape
Philipp Arras's avatar
Philipp Arras committed
32
33
from ..operators.slope_operator import SlopeOperator
from ..operators.symmetrizing_operator import SymmetrizingOperator
Philipp Arras's avatar
Philipp Arras committed
34
from ..sugar import from_global_data, full, makeDomain, makeOp
35
36


37
def _ceps_kernel(k, a, k0):
Philipp Arras's avatar
Philipp Arras committed
38
    return (a/(1 + np.sum((k.T/k0)**2, axis=-1).T))**2
39
40


Philipp Arras's avatar
Docs  
Philipp Arras committed
41
def CepstrumOperator(target, a, k0):
42
    """Turns a white Gaussian random field into a smooth field on a LogRGSpace.
43

Philipp Arras's avatar
Docs  
Philipp Arras committed
44
45
46
47
48
49
50
51
    Composed out of three operators:

        sym @ qht @ diag(sqrt_ceps),

    where sym is a :class:`SymmetrizingOperator`, qht is a :class:`QHTOperator`
    and ceps is the so-called cepstrum:

    .. math::
Martin Reinecke's avatar
Martin Reinecke committed
52
        \\mathrm{sqrt\\_ceps}(k) = \\frac{a}{1+(k/k0)^2}
Philipp Arras's avatar
Docs  
Philipp Arras committed
53
54
55
56
57
58
59

    These operators are combined in this fashion in order to generate:

    - A field which is smooth, i.e. second derivatives are punished (note
      that the sqrt-cepstrum is essentially proportional to 1/k**2).

    - A field which is symmetric around the pixel in the middle of the space.
Philipp Arras's avatar
Docs  
Philipp Arras committed
60
61
      This is result of the :class:`SymmetrizingOperator` and needed in order
      to decouple the degrees of freedom at the beginning and the end of the
Philipp Arras's avatar
Docs  
Philipp Arras committed
62
63
64
      amplitude whenever :class:`CepstrumOperator` is used as in
      :class:`SLAmplitude`.

Philipp Arras's avatar
Docs  
Philipp Arras committed
65
66
    The prior on the zero mode (or zero subspaces for more than one dimensions)
    is the integral of the prior over all other modes along the corresponding
67
    axis.
Philipp Arras's avatar
Docs  
Philipp Arras committed
68
69
70
71

    Parameters
    ----------
    target : LogRGSpace
72
        Target domain of the operator, needs to be non-harmonic.
Philipp Arras's avatar
Docs  
Philipp Arras committed
73
    a : float
74
75
76
77
        Cutoff of smoothness prior (positive only). Controls the
        regularization of the inverse laplace operator to be finite at zero.
        Larger values for the cutoff results in a weaker constraining prior.
    k0 : float, list of float
78
        Strength of smoothness prior in quefrency space (positive only) along
79
80
        each axis. If float then the strength is the same along each axis.
        Larger values result in a weaker constraining prior.
81
    """
82
    a = float(a)
Philipp Arras's avatar
Docs  
Philipp Arras committed
83
    target = DomainTuple.make(target)
84
    if a <= 0:
Philipp Arras's avatar
Docs  
Philipp Arras committed
85
        raise ValueError
86
    if len(target) > 1 or target[0].harmonic:
Philipp Arras's avatar
Docs  
Philipp Arras committed
87
        raise TypeError
Philipp Arras's avatar
Docs  
Philipp Arras committed
88
89
90
91
92
    if isinstance(k0, (float, int)):
        k0 = np.array([k0]*len(target.shape))
    else:
        k0 = np.array(k0)
    if len(k0) != len(target.shape):
93
94
95
        raise ValueError
    if np.any(np.array(k0) <= 0):
        raise ValueError
Philipp Arras's avatar
Docs  
Philipp Arras committed
96
97
98
99
100
101
102
103
104

    qht = QHTOperator(target)
    dom = qht.domain[0]
    sym = SymmetrizingOperator(target)

    # Compute cepstrum field
    dim = len(dom.shape)
    shape = dom.shape
    q_array = dom.get_k_array()
105
    # Fill all non-zero modes
Philipp Arras's avatar
Philipp Arras committed
106
107
    no_zero_modes = (slice(1, None),)*dim
    ks = q_array[(slice(None),) + no_zero_modes]
108
    cepstrum_field = np.zeros(shape)
Philipp Arras's avatar
Docs  
Philipp Arras committed
109
    cepstrum_field[no_zero_modes] = _ceps_kernel(ks, a, k0)
110
    # Fill zero-mode subspaces
111
    for i in range(dim):
Philipp Arras's avatar
Philipp Arras committed
112
113
114
        fst_dims = (slice(None),)*i
        sl = fst_dims + (slice(1, None),)
        sl2 = fst_dims + (0,)
115
        cepstrum_field[sl2] = np.sum(cepstrum_field[sl], axis=i)
Philipp Arras's avatar
Docs  
Philipp Arras committed
116
    cepstrum = Field.from_global_data(dom, cepstrum_field)
Philipp Arras's avatar
Philipp Arras committed
117

118
119
    return sym @ qht @ makeOp(cepstrum.sqrt())

Martin Reinecke's avatar
Martin Reinecke committed
120

121
def SLAmplitude(*, target, n_pix, a, k0, sm, sv, im, iv, keys=['tau', 'phi']):
122
123
    '''Operator for parametrizing smooth amplitudes (square roots of power
    spectra).
124
125
126

    The general guideline for setting up generative models in IFT is to
    transform the problem into the eigenbase of the prior and formulate the
127
128
    generative model in this base. This is done here for the case of an
    amplitude which is smooth and has a linear component (both on
129
130
131
    double-logarithmic scale).

    This function assembles an :class:`Operator` which maps two a-priori white
132
    Gaussian random fields to a smooth amplitude which is composed out of
133
134
135
136
137
138
    a linear and a smooth component.

    On double-logarithmic scale, i.e. both x and y-axis on logarithmic scale,
    the output of the generated operator is:

        AmplitudeOperator = 0.5*(smooth_component + linear_component)
Philipp Arras's avatar
Philipp Arras committed
139

140
    This is then exponentiated and exponentially binned (in this order).
141
142
143
144
145
146

    The prior on the linear component is parametrized by four real numbers,
    being expected value and prior variance on the slope and the y-intercept
    of the linear function.

    The prior on the smooth component is parametrized by two real numbers: the
Martin Reinecke's avatar
Martin Reinecke committed
147
148
    strength and the cutoff of the smoothness prior
    (see :class:`CepstrumOperator`).
Martin Reinecke's avatar
Martin Reinecke committed
149
150
151

    Parameters
    ----------
152
153
154
155
156
157
158
    n_pix : int
        Number of pixels of the space in which the .
    target : PowerSpace
        Target of the Operator.
    a : float
        Strength of smoothness prior (see :class:`CepstrumOperator`).
    k0 : float
Philipp Arras's avatar
Docs  
Philipp Arras committed
159
160
        Cutoff of smothness prior in quefrency space (see
        :class:`CepstrumOperator`).
Philipp Arras's avatar
Philipp Arras committed
161
    sm : float
Philipp Arras's avatar
Docs  
Philipp Arras committed
162
        Expected exponent of power law.
Philipp Arras's avatar
Philipp Arras committed
163
    sv : float
164
        Prior standard deviation of exponent of power law.
Philipp Arras's avatar
Philipp Arras committed
165
    im : float
Philipp Arras's avatar
Docs  
Philipp Arras committed
166
167
        Expected y-intercept of power law. This is the value at t_0 of the
        LogRGSpace (see :class:`ExpTransform`).
Philipp Arras's avatar
Philipp Arras committed
168
    iv : float
169
        Prior standard deviation of y-intercept of power law.
170
171
172
173
174
175
176

    Returns
    -------
    Operator
        Operator which is defined on the space of white excitations fields and
        which returns on its target a power spectrum which consists out of a
        smooth and a linear part.
Martin Reinecke's avatar
Martin Reinecke committed
177
    '''
Martin Reinecke's avatar
Martin Reinecke committed
178
179
    return LinearSLAmplitude(target=target, n_pix=n_pix, a=a, k0=k0, sm=sm,
                             sv=sv, im=im, iv=iv, keys=keys).exp()
180
181


Martin Reinecke's avatar
Martin Reinecke committed
182
183
184
185
186
def LinearSLAmplitude(*, target, n_pix, a, k0, sm, sv, im, iv,
                      keys=['tau', 'phi']):
    '''LinearOperator for parametrizing smooth log-amplitudes (square roots of
    power spectra).

187
188
189
    Logarithm of SLAmplitude
    See documentation of SLAmplitude for more details
    '''
190
191
192
193
194
    if not (isinstance(n_pix, int) and isinstance(target, PowerSpace)):
        raise TypeError

    a, k0 = float(a), float(k0)
    sm, sv, im, iv = float(sm), float(sv), float(im), float(iv)
195
196
    if sv <= 0 or iv <= 0:
        raise ValueError
197
198
199
200

    et = ExpTransform(target, n_pix)
    dom = et.domain[0]

201
    # Smooth component
202
203
    dct = {'a': a, 'k0': k0}
    smooth = CepstrumOperator(dom, **dct).ducktape(keys[0])
Martin Reinecke's avatar
Martin Reinecke committed
204

205
    # Linear component
206
207
208
209
210
    sl = SlopeOperator(dom)
    mean = np.array([sm, im + sm*dom.t_0[0]])
    sig = np.array([sv, iv])
    mean = Field.from_global_data(sl.domain, mean)
    sig = Field.from_global_data(sl.domain, sig)
Philipp Arras's avatar
Philipp Arras committed
211
    linear = sl @ Adder(mean) @ makeOp(sig).ducktape(keys[1])
212
213
214

    # Combine linear and smooth component
    loglog_ampl = 0.5*(smooth + linear)
Philipp Arras's avatar
Changes  
Philipp Arras committed
215

216
    # Go from loglog-space to linear-linear-space
217
    return et @ loglog_ampl
Philipp Arras's avatar
Philipp Arras committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328


class _TwoLogIntegrations(LinearOperator):
    def __init__(self, target):
        self._target = makeDomain(target)
        self._domain = makeDomain(
            UnstructuredDomain(self.target.shape[0] - 2))
        self._capability = self.TIMES | self.ADJOINT_TIMES
        if not isinstance(self._target[0], PowerSpace):
            raise TypeError
        logk_lengths = np.log(self._target[0].k_lengths[1:])
        self._logvol = logk_lengths[1:] - logk_lengths[:-1]

    def apply(self, x, mode):
        self._check_input(x, mode)
        if mode == self.TIMES:
            x = x.to_global_data()
            res = np.empty(self._target.shape)
            res[0] = 0
            res[1] = 0
            res[2:] = np.cumsum(x*self._logvol)
            res[2:] = np.cumsum(res[2:]*self._logvol)
            return from_global_data(self._target, res)
        else:
            x = x.to_global_data()
            res = np.empty(self._target.shape)
            res[2:] = np.cumsum(x[2:][::-1])[::-1]*self._logvol
            res[2:] = np.cumsum(res[2:][::-1])[::-1]*self._logvol
            return from_global_data(self._domain, res[2:])


class _Rest(LinearOperator):
    def __init__(self, target):
        self._target = makeDomain(target)
        self._domain = makeDomain(UnstructuredDomain(3))
        self._logk_lengths = np.log(self._target[0].k_lengths[1:])
        self._logk_lengths -= self._logk_lengths[0]
        self._capability = self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
        self._check_input(x, mode)
        x = x.to_global_data()
        res = np.empty(self._tgt(mode).shape)
        if mode == self.TIMES:
            res[0] = x[0]
            res[1:] = x[1]*self._logk_lengths + x[2]
        else:
            res[0] = x[0]
            res[1] = np.vdot(self._logk_lengths, x[1:])
            res[2] = np.sum(x[1:])
        return from_global_data(self._tgt(mode), res)


def LogIntegratedWienerProcess(target, means, stddevs, wienersigmastddev,
                               wienersigmaprob, keys):
    # means and stddevs: zm, slope, yintercept
    # keys: rest smooth wienersigma
    if not (len(means) == 3 and len(stddevs) == 3 and len(keys) == 3):
        raise ValueError
    means = np.array(means)
    stddevs = np.array(stddevs)
    # FIXME More checks
    rest = _Rest(target)
    restmeans = from_global_data(rest.domain, means)
    reststddevs = from_global_data(rest.domain, stddevs)
    rest = rest @ Adder(restmeans) @ makeOp(reststddevs)

    expander = VdotOperator(full(target, 1.)).adjoint
    m = means[1]
    L = np.log(target.k_lengths[-1]) - np.log(target.k_lengths[1])

    from scipy.stats import norm
    wienermean = np.sqrt(3/L)*np.abs(m)/norm.ppf(wienersigmaprob)
    wienermean = np.log(wienermean)

    sigma = Adder(full(expander.domain, wienermean)) @ (
        wienersigmastddev*ducktape(expander.domain, None, keys[2]))
    sigma = expander @ sigma.exp()
    smooth = _TwoLogIntegrations(target).ducktape(keys[1])*sigma
    return rest.ducktape(keys[0]) + smooth


class Normalization(Operator):
    def __init__(self, domain):
        self._domain = self._target = makeDomain(domain)
        hspace = self._domain[0].harmonic_partner
        pd = PowerDistributor(hspace, power_space=self._domain[0])
        # TODO Does not work on sphere yet
        self._cst = pd.adjoint(full(pd.target, hspace.scalar_dvol))
        self._specsum = SpecialSum(self._domain)

    def apply(self, x):
        self._check_input(x)
        return self._specsum(self._cst*x).one_over()*x


class SpecialSum(EndomorphicOperator):
    def __init__(self, domain):
        self._domain = makeDomain(domain)
        self._capability = self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
        self._check_input(x, mode)
        return full(self._tgt(mode), x.sum())


def WPAmplitude(target, means, stddevs, wienersigmastddev, wienersigmaprob,
              keys):
    op = LogIntegratedWienerProcess(target, means, stddevs, wienersigmastddev,
                                    wienersigmaprob, keys)
    return Normalization(target) @ op.exp()