wiener_process_integrated_amplitude.py 6.43 KB
Newer Older
1
2
3
4
import nifty5 as ift
import numpy as np


Philipp Arras's avatar
Work    
Philipp Arras committed
5
class _TwoLogIntegrations(ift.LinearOperator):
6
7
8
9
10
    def __init__(self, target):
        self._target = ift.makeDomain(target)
        self._domain = ift.makeDomain(
            ift.UnstructuredDomain(self.target.shape[0] - 2))
        self._capability = self.TIMES | self.ADJOINT_TIMES
Philipp Arras's avatar
Work    
Philipp Arras committed
11
12
13
14
        if not isinstance(self._target[0], ift.PowerSpace):
            raise TypeError
        logk_lengths = np.log(self._target[0].k_lengths[1:])
        self._logvol = logk_lengths[1:] - logk_lengths[:-1]
15
16
17
18
19
20
21
22

    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
Philipp Arras's avatar
Work    
Philipp Arras committed
23
24
            res[2:] = np.cumsum(x*self._logvol)
            res[2:] = np.cumsum(res[2:]*self._logvol)
25
26
27
28
            return ift.from_global_data(self._target, res)
        else:
            x = x.to_global_data()
            res = np.empty(self._target.shape)
Philipp Arras's avatar
Work    
Philipp Arras committed
29
30
            res[2:] = np.cumsum(x[2:][::-1])[::-1]*self._logvol
            res[2:] = np.cumsum(res[2:][::-1])[::-1]*self._logvol
31
32
33
            return ift.from_global_data(self._domain, res[2:])


Philipp Arras's avatar
Work    
Philipp Arras committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class _Rest(ift.LinearOperator):
    def __init__(self, target):
        self._target = ift.makeDomain(target)
        self._domain = ift.makeDomain(ift.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 ift.from_global_data(self._tgt(mode), res)


Philipp Arras's avatar
Work    
Philipp Arras committed
56
57
58
def LogIntegratedWienerProcess(target, means, stddevs, wienersigmastddev,
                               wienersigmaprob, keys):
    # means and stddevs: zm, slope, yintercept
Philipp Arras's avatar
Work    
Philipp Arras committed
59
    # keys: rest smooth wienersigma
Philipp Arras's avatar
Work    
Philipp Arras committed
60
    if not (len(means) == 3 and len(stddevs) == 3 and len(keys) == 3):
Philipp Arras's avatar
Work    
Philipp Arras committed
61
62
63
64
65
        raise ValueError
    means = np.array(means)
    stddevs = np.array(stddevs)
    # FIXME More checks
    rest = _Rest(target)
Philipp Arras's avatar
Work    
Philipp Arras committed
66
67
    restmeans = ift.from_global_data(rest.domain, means)
    reststddevs = ift.from_global_data(rest.domain, stddevs)
Philipp Arras's avatar
Work    
Philipp Arras committed
68
69
70
    rest = rest @ ift.Adder(restmeans) @ ift.makeOp(reststddevs)

    expander = ift.VdotOperator(ift.full(target, 1.)).adjoint
Philipp Arras's avatar
Work    
Philipp Arras committed
71
72
73
74
75
76
77
78
79
    m = means[1]
    L = np.log(target.k_lengths[-1]) - np.log(target.k_lengths[1])

    s = np.log(np.abs(m/L))
    from scipy.special import erfinv
    wienermean = s - erfinv(wienersigmaprob)*wienersigmastddev
    print(s, wienermean, wienersigmastddev)
    sigma = ift.Adder(ift.full(expander.domain, wienermean)) @ (
        wienersigmastddev*ift.ducktape(expander.domain, None, keys[2]))
Philipp Arras's avatar
Work    
Philipp Arras committed
80
81
82
83
84
    sigma = expander @ sigma.exp()
    smooth = _TwoLogIntegrations(target).ducktape(keys[1])*sigma
    return rest.ducktape(keys[0]) + smooth


Philipp Arras's avatar
Work    
Philipp Arras committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class Normalization(ift.Operator):
    def __init__(self, domain):
        self._domain = self._target = ift.makeDomain(domain)
        hspace = self._domain[0].harmonic_partner
        pd = ift.PowerDistributor(hspace, power_space=self._domain[0])
        # TODO Does not work on sphere yet
        self._cst = pd.adjoint(ift.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(ift.EndomorphicOperator):
    def __init__(self, domain):
        self._domain = ift.makeDomain(domain)
        self._capability = self.TIMES | self.ADJOINT_TIMES

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


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


def mfplot(fld, A1, A2, name):
    p = ift.Plot()
    dom = fld.domain
    mi, ma = np.min(fld.val), np.max(fld.val)
    for ii in range(dom.shape[-1]):
        extr = ift.DomainTupleFieldInserter(op.target, 1, (ii,)).adjoint
        p.add(extr(fld), zmin=mi, zmax=ma)
    p.add(A1)
    p.add(A2)
    p.add(fld)
    p.output(name=name, xsize=14, ysize=14)


129
130
if __name__ == '__main__':
    np.random.seed(42)
Philipp Arras's avatar
Work    
Philipp Arras committed
131
    from correlated_fields_normalized_amplitude import CorrelatedFieldNormAmplitude
Philipp Arras's avatar
Work    
Philipp Arras committed
132
    sspace = ift.RGSpace((512, 512))
133
    hspace = sspace.get_default_codomain()
Philipp Arras's avatar
Work    
Philipp Arras committed
134
135
136
137
138
139
    target = ift.PowerSpace(hspace,
                            ift.PowerSpace.useful_binbounds(hspace, True))
    test0 = _Rest(target)
    test1 = _TwoLogIntegrations(target)
    ift.extra.consistency_check(test0)
    ift.extra.consistency_check(test1)
Philipp Arras's avatar
Work    
Philipp Arras committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    # A = Amplitude(target, [5, -3, 1, 0], [1, 1, 1, 0.5],
    #               ['rest', 'smooth', 'wienersigma'])
    # fld = ift.from_random('normal', A.domain)
    # ift.extra.check_jacobian_consistency(A, fld)
    # op = CorrelatedFieldNormAmplitude(sspace, A, 3, 2)
    # plts = []
    # p = ift.Plot()
    # for _ in range(25):
    #     fld = ift.from_random('normal', op.domain)
    #     plts.append(A.force(fld))
    #     p.add(op(fld))
    # p.output(name='debug1.png', xsize=20, ysize=20)
    # ift.single_plot(plts, name='debug.png')

    # Multifrequency
    sspace1 = ift.RGSpace((512, 512))
    hspace1 = sspace1.get_default_codomain()
    target1 = ift.PowerSpace(hspace1,
                             ift.PowerSpace.useful_binbounds(hspace1, True))
    A1 = Amplitude(target1, [0, -2, 1], [1, 1, 1], 1, 0.99,
                   ['rest1', 'smooth1', 'wienersigma1'])
    sspace2 = ift.RGSpace((20,), distances=(1e7))
    hspace2 = sspace2.get_default_codomain()
    target2 = ift.PowerSpace(hspace2,
                             ift.PowerSpace.useful_binbounds(hspace2, True))
    A2 = Amplitude(target2, [0, -2, 1], [1, 1, 1], 1, 0.99,
                   ['rest2', 'smooth2', 'wienersigma2'])
    op = CorrelatedFieldNormAmplitude((sspace1, sspace2), (A1, A2), 3, 2)
    for jj in range(10):
Philipp Arras's avatar
Work    
Philipp Arras committed
169
        fld = ift.from_random('normal', op.domain)
Philipp Arras's avatar
Work    
Philipp Arras committed
170
        mfplot(op(fld), A1.force(fld), A2.force(fld), 'debug{}.png'.format(jj))