sugar.py 4.42 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
import numpy as np

Martin Reinecke's avatar
Martin Reinecke committed
21
from . import Space,\
22
                  PowerSpace,\
23
                  Field,\
24
                  ComposedOperator,\
25
                  DiagonalOperator,\
26
                  FFTOperator,\
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
27
                  sqrt
28

29
30
31
__all__ = ['create_power_operator',
           'generate_posterior_sample',
           'create_composed_fft_operator']
32
33


Martin Reinecke's avatar
stage1    
Martin Reinecke committed
34
def create_power_operator(domain, power_spectrum, dtype=None):
Theo Steininger's avatar
Theo Steininger committed
35
    """ Creates a diagonal operator with the given power spectrum.
36

37
    Constructs a diagonal operator that lives over the specified domain.
38

39
40
41
    Parameters
    ----------
    domain : DomainObject
42
        Domain over which the power operator shall live.
Martin Reinecke's avatar
Martin Reinecke committed
43
44
45
    power_spectrum : callable
        A method that implements the square root of a power spectrum as a
        function of k.
Theo Steininger's avatar
Theo Steininger committed
46
    dtype : type *optional*
47
        dtype that the field holding the power spectrum shall use
Theo Steininger's avatar
Theo Steininger committed
48
49
50
        (default : None).
        if dtype == None: the dtype of `power_spectrum` will be used.

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

55
    """
56

Martin Reinecke's avatar
Martin Reinecke committed
57
58
59
    if not callable(power_spectrum):
        raise TypeError("power_spectrum must be callable")
    power_domain = PowerSpace(domain)
Jakob Knollmueller's avatar
Jakob Knollmueller committed
60

Martin Reinecke's avatar
Martin Reinecke committed
61
    fp = Field(power_domain, val=power_spectrum(power_domain.k_lengths),
62
63
               dtype=dtype)
    f = fp.power_synthesize_special()
64
65
66
67

    if not issubclass(fp.dtype.type, np.complexfloating):
        f = f.real

Jakob Knollmueller's avatar
Jakob Knollmueller committed
68
    f **= 2
Martin Reinecke's avatar
Martin Reinecke committed
69
    return DiagonalOperator(Field(domain,f).weight(1))
70

71

72
73
74
def generate_posterior_sample(mean, covariance):
    """ Generates a posterior sample from a Gaussian distribution with given
    mean and covariance
75

76
77
78
    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.
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

    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

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

    """

95
96
97
    S = covariance.S
    R = covariance.R
    N = covariance.N
98

99
    power = sqrt(S.diagonal().power_analyze())
100
101
    mock_signal = power.power_synthesize(real_signal=True)

Martin Reinecke's avatar
Martin Reinecke committed
102
    noise = N.diagonal().weight(-1)
103

104
    mock_noise = Field.from_random(random_type="normal", domain=N.domain,
Martin Reinecke's avatar
Martin Reinecke committed
105
                                   dtype=noise.dtype.type)
106
107
    mock_noise *= sqrt(noise)

Jakob Knollmueller's avatar
Jakob Knollmueller committed
108
    mock_data = R(mock_signal) + mock_noise
109

Jakob Knollmueller's avatar
Jakob Knollmueller committed
110
    mock_j = R.adjoint_times(N.inverse_times(mock_data))
111
112
113
    mock_m = covariance.inverse_times(mock_j)
    sample = mock_signal - mock_m + mean
    return sample
114
115
116
117
118
119
120


def create_composed_fft_operator(domain, codomain=None, all_to='other'):
    fft_op_list = []

    if codomain is None:
        codomain = [None]*len(domain)
121
    interdomain = list(domain.domains)
Martin Reinecke's avatar
Martin Reinecke committed
122
    for i, space in enumerate(domain):
123
124
125
126
127
128
        cospace = codomain[i]
        if not isinstance(space, Space):
            continue
        if (all_to == 'other' or
                (all_to == 'position' and space.harmonic) or
                (all_to == 'harmonic' and not space.harmonic)):
129
130
131
132
133
134
135
136
            if codomain[i] is None:
                interdomain[i] = domain[i].get_default_codomain()
            else:
                interdomain[i] = codomain[i]
            fft_op_list += [FFTOperator(domain=domain, target=interdomain,
                                        space=i)]
        domain = interdomain
    return ComposedOperator(fft_op_list)