sugar.py 2.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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/>.
18
19
20
21

from nifty import PowerSpace,\
                  Field,\
                  DiagonalOperator,\
22
23
                  FFTOperator,\
                  sqrt
24
25
26
27

__all__ = ['create_power_operator']


28
def create_power_operator(domain, power_spectrum, power_domain=None, dtype=None,
29
                          distribution_strategy='not'):
30
31
32
    if not domain.harmonic:
        fft = FFTOperator(domain)
        domain = fft.target[0]
33
34
35
36
    if isinstance(power_spectrum, Field):
        power_domain = power_spectrum.domain
    elif power_domain is None:
        power_domain = PowerSpace(domain,
37
                              distribution_strategy=distribution_strategy)
38
39

    fp = Field(power_domain,
40
               val=power_spectrum, dtype=dtype,
41
42
               distribution_strategy=distribution_strategy)

43
    f = fp.power_synthesize(mean=1, std=0, real_signal=False)
44

45
    power_operator = DiagonalOperator(domain, diagonal=f, bare=True)
46

47
48
49
50
51
52
53
    return power_operator


def generate_posterior_sample(mean, covariance):
    S = covariance.S
    R = covariance.R
    N = covariance.N
54
55
56
57
    power = sqrt(S.diagonal().power_analyze())
    mock_signal = power.power_synthesize(real_signal=True)


58
    noise = N.diagonal().val
59

60
61
    mock_noise = Field.from_random(random_type="normal", domain=N.domain,
                                   std = sqrt(noise), dtype = noise.dtype)
62
    mock_data = R.derived_times(mock_signal, mean) + mock_noise
63

64
    mock_j = R.derived_adjoint_times(N.inverse_times(mock_data), mean)
65
66
67
68
    mock_m = covariance.inverse_times(mock_j)
    sample = mock_signal - mock_m + mean
    return sample