propagator_operator.py 6.92 KB
Newer Older
1
2
3
4
5
# -*- coding: utf-8 -*-
from nifty.minimization import ConjugateGradient

from nifty.operators.linear_operator import LinearOperator

6
7
8
import logging
logger = logging.getLogger('NIFTy.PropagatorOperator')

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
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

class PropagatorOperator(LinearOperator):
    """
        ..                                                                            __
        ..                                                                          /  /_
        ..      _______   _____   ______    ______    ____ __   ____ __   ____ __  /   _/  ______    _____
        ..    /   _   / /   __/ /   _   | /   _   | /   _   / /   _   / /   _   / /  /   /   _   | /   __/
        ..   /  /_/  / /  /    /  /_/  / /  /_/  / /  /_/  / /  /_/  / /  /_/  / /  /_  /  /_/  / /  /
        ..  /   ____/ /__/     \______/ /   ____/  \______|  \___   /  \______|  \___/  \______/ /__/     operator class
        .. /__/                        /__/                 /______/

        NIFTY subclass for propagator operators (of a certain family)

        The propagator operators :math:`D` implemented here have an inverse
        formulation like :math:`(S^{-1} + M)`, :math:`(S^{-1} + N^{-1})`, or
        :math:`(S^{-1} + R^\dagger N^{-1} R)` as appearing in Wiener filter
        theory.

        Parameters
        ----------
        S : operator
            Covariance of the signal prior.
        M : operator
            Likelihood contribution.
        R : operator
            Response operator translating signal to (noiseless) data.
        N : operator
            Covariance of the noise prior or the likelihood, respectively.

        See Also
        --------
        conjugate_gradient

        Notes
        -----
        The propagator will puzzle the operators `S` and `M` or `R`, `N` or
        only `N` together in the predefined from, a domain is set
        automatically. The application of the inverse is done by invoking a
        conjugate gradient.
        Note that changes to `S`, `M`, `R` or `N` auto-update the propagator.

        Examples
        --------
        >>> f = field(rg_space(4), val=[2, 4, 6, 8])
        >>> S = power_operator(f.target, spec=1)
        >>> N = diagonal_operator(f.domain, diag=1)
        >>> D = propagator_operator(S=S, N=N) # D^{-1} = S^{-1} + N^{-1}
        >>> D(f).val
        array([ 1.,  2.,  3.,  4.])

        Attributes
        ----------
        domain : space
            A space wherein valid arguments live.
        codomain : space
            An alternative space wherein valid arguments live; commonly the
            codomain of the `domain` attribute.
        sym : bool
            Indicates that the operator is self-adjoint.
        uni : bool
            Indicates that the operator is not unitary.
        imp : bool
            Indicates that volume weights are implemented in the `multiply`
            instance methods.
        target : space
            The space wherein the operator output lives.
        _A1 : {operator, function}
            Application of :math:`S^{-1}` to a field.
        _A2 : {operator, function}
            Application of all operations not included in `A1` to a field.
        RN : {2-tuple of operators}, *optional*
            Contains `R` and `N` if given.

    """

    # ---Overwritten properties and methods---

    def __init__(self, S=None, M=None, R=None, N=None, inverter=None):
        """
            Sets the standard operator properties and `codomain`, `_A1`, `_A2`,
            and `RN` if required.

            Parameters
            ----------
            S : operator
                Covariance of the signal prior.
            M : operator
                Likelihood contribution.
            R : operator
                Response operator translating signal to (noiseless) data.
            N : operator
                Covariance of the noise prior or the likelihood, respectively.

        """

        self.S = S
        self.S_inverse_times = self.S.inverse_times

        # build up the likelihood contribution
        (self.M_times,
         M_domain,
         M_field_type,
         M_target,
         M_field_type_target) = self._build_likelihood_contribution(M, R, N)

        # assert that S and M have matching domains
        if not (self.domain == M_domain and
                self.field_type == M_target and
                self.target == M_target and
                self.field_type_target == M_field_type_target):
119
120
            raise ValueError(
                "The domains and targets of the prior " +
121
                "signal covariance and the likelihood contribution must be " +
122
                "the same in the sense of '=='.")
123
124
125
126

        if inverter is not None:
            self.inverter = inverter
        else:
127
            self.inverter = ConjugateGradient()
128
129
130
131
132
133
134
135
136
137
138
139
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
169
170
171
172
173
174

    # ---Mandatory properties and methods---

    @property
    def domain(self):
        return self.S.domain

    @property
    def field_type(self):
        return self.S.field_type

    @property
    def target(self):
        return self.S.target

    @property
    def field_type_target(self):
        return self.S.field_type_target

    @property
    def implemented(self):
        return True

    @property
    def symmetric(self):
        return True

    @property
    def unitary(self):
        return False

    # ---Added properties and methods---

    def _build_likelihood_contribution(self, M, R, N):
        # if a M is given, return its times method and its domains
        # supplier and discard R and N
        if M is not None:
            return (M.times, M.domain, M.field_type, M.target, M.cotarget)

        if N is not None:
            if R is not None:
                return (lambda z: R.adjoint_times(N.inverse_times(R.times(z))),
                        R.domain, R.field_type, R.domain, R.field_type)
            else:
                return (N.inverse_times,
                        N.domain, N.field_type, N.target, N.field_type_target)
        else:
175
176
            raise ValueError(
                "At least M or N must be given.")
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

    def _multiply(self, x, W=None, spam=None, reset=None, note=False,
                  x0=None, tol=1E-4, clevel=1, limii=None, **kwargs):

        if W is None:
            W = self.S
        (result, convergence) = self.inverter(A=self._inverse_multiply,
                                              b=x,
                                              W=W,
                                              spam=spam,
                                              reset=reset,
                                              note=note,
                                              x0=x0,
                                              tol=tol,
                                              clevel=clevel,
                                              limii=limii)
        # evaluate
        if not convergence:
195
            logger.warn("conjugate gradient failed.")
196
197
198
199
200
201
202

        return result

    def _inverse_multiply(self, x, **kwargs):
        result = self.S_inverse_times(x)
        result += self.M_times(x)
        return result