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

from nifty import PowerSpace,\
                  Field,\
                  DiagonalOperator,\
22
                  sqrt
23
from nifty.minimization.conjugate_gradient import ConjugateGradient
24
25
26
__all__ = ['create_power_operator']


Jakob Knollmueller's avatar
Jakob Knollmueller committed
27
28

def create_power_operator(domain, power_spectrum, dtype=None,
29
                          distribution_strategy='not'):
Theo Steininger's avatar
Theo Steininger committed
30
    """ Creates a diagonal operator with the given power spectrum.
31

32
    Constructs a diagonal operator that lives over the specified domain.
33

34
35
36
    Parameters
    ----------
    domain : DomainObject
37
        Domain over which the power operator shall live.
Theo Steininger's avatar
Theo Steininger committed
38
    power_spectrum : (array-like, method)
39
40
        An array-like object, or a method that implements the square root
        of a power spectrum as a function of k.
Theo Steininger's avatar
Theo Steininger committed
41
    dtype : type *optional*
42
        dtype that the field holding the power spectrum shall use
Theo Steininger's avatar
Theo Steininger committed
43
44
45
        (default : None).
        if dtype == None: the dtype of `power_spectrum` will be used.
    distribution_strategy : string *optional*
46
        Distributed strategy to be used by the underlying d2o objects.
Theo Steininger's avatar
Theo Steininger committed
47
48
        (default : 'not')

49
50
    Returns
    -------
Theo Steininger's avatar
Theo Steininger committed
51
    DiagonalOperator : An operator that implements the given power spectrum.
52

53
    """
54

Jakob Knollmueller's avatar
Jakob Knollmueller committed
55
56
57
58
    if isinstance(power_spectrum, Field):
        power_domain = power_spectrum.domain
    else :
        power_domain = PowerSpace(domain,
59
                              distribution_strategy=distribution_strategy)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
60
61
62



63
    fp = Field(power_domain, val=power_spectrum, dtype=dtype,
64
               distribution_strategy=distribution_strategy)
65
    f = fp.power_synthesize(mean=1, std=0, real_signal=False)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
66
    f **= 2
67
    return DiagonalOperator(domain, diagonal=f, bare=True)
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def generate_posterior_sample(mean, covariance, inverter = None):
    """ Generates a posterior sample from a Gaussian distribution with given mean and covariance

    This method generates samples by setting up the observation and reconstruction of a mock signal
    in order to obtain residuals of the right correlation which are added to the given mean.

    Parameters
    ----------
    mean : Field
        the mean of the posterior Gaussian distribution
    covariance : WienerFilterCurvature
        The posterior correlation structure consisting of a
        response operator, noise covariance and prior signal covariance
    inverter : ConjugateGradient *optional*
        the conjugate gradient used to invert the curvature for the Wiener filter.
        default : None

    Returns
    -------
    sample : Field
        Returns the a sample from the Gaussian of given mean and covariance.

    """

93
94
95
    S = covariance.S
    R = covariance.R
    N = covariance.N
96
97
98
    if inverter is None:
        inverter = ConjugateGradient(preconditioner=S)

Jakob Knollmueller's avatar
Jakob Knollmueller committed
99
    power = S.diagonal().power_analyze()**.5
100
101
102
    mock_signal = power.power_synthesize(real_signal=True)


Jakob Knollmueller's avatar
Jakob Knollmueller committed
103
    noise = N.diagonal(bare=True).val
104

105
106
    mock_noise = Field.from_random(random_type="normal", domain=N.domain,
                                   std = sqrt(noise), dtype = noise.dtype)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
107
    mock_data = R(mock_signal) + mock_noise
108

Jakob Knollmueller's avatar
Jakob Knollmueller committed
109
    mock_j = R.adjoint_times(N.inverse_times(mock_data))
110
111
112
113
    mock_m = covariance.inverse_times(mock_j)
    sample = mock_signal - mock_m + mean
    return sample