response_operator.py 3.46 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
from builtins import range
2
from ... import Field,\
Martin Reinecke's avatar
Martin Reinecke committed
3
4
                FieldArray,\
                DomainTuple
5
from ..linear_operator import LinearOperator
Martin Reinecke's avatar
Martin Reinecke committed
6
from ..smoothing_operator import FFTSmoothingOperator
7
8
from ..composed_operator import ComposedOperator
from ..diagonal_operator import DiagonalOperator
9

10

11
class ResponseOperator(LinearOperator):
Theo Steininger's avatar
Theo Steininger committed
12
    """ NIFTy ResponseOperator (example)
13

14
15
16
17
18
19
20
    This NIFTy ResponseOperator provides the user with an example how a
    ResponseOperator can look like. It smoothes and exposes a field. The
    outcome of the Operator is geometrically not ordered as typical data
    set are.

    Parameters
    ----------
Theo Steininger's avatar
Theo Steininger committed
21
    domain : tuple of DomainObjects, i.e. Spaces and FieldTypes
22
23
24
25
26
27
        The domains on which the operator lives. Either one space or a list
        of spaces
    sigma : list(np.float)
        Defines the smoothing length of the operator for each space it lives on
    exposure : list(np.float)
        Defines the exposure of the operator for each space it lives on
28
29
30
    default_spaces : tuple of ints *optional*
        Defines on which space(s) of a given field the Operator acts by
        default (default: None)
31
32
33

    Attributes
    ----------
34
35
36
    domain : tuple of DomainObjects, i.e. Spaces and FieldTypes
        The domain on which the Operator's input Field lives.
    target : tuple of DomainObjects, i.e. Spaces and FieldTypes
Martin Reinecke's avatar
Martin Reinecke committed
37
        The domain in which the outcome of the operator lives.
38
39
    unitary : boolean
        Indicates whether the Operator is unitary or not.
40
41
42
43
44
45
46
47

    Raises
    ------
    ValueError:
        raised if:
            * len of sigma-list and exposure-list are not equal
    """

48
49
    def __init__(self, domain, sigma=[1.], exposure=[1.], spaces=None):
        super(ResponseOperator, self).__init__()
50

51
        if len(sigma) != len(exposure):
52
53
            raise ValueError("Length of smoothing kernel and length of"
                             "exposure do not match")
Martin Reinecke's avatar
updates    
Martin Reinecke committed
54
        nsigma = len(sigma)
55

Martin Reinecke's avatar
Martin Reinecke committed
56
        self._domain = DomainTuple.make(domain)
Martin Reinecke's avatar
updates    
Martin Reinecke committed
57

58
59
60
61
62
        if spaces is None:
            spaces = range(len(self._domain))

        kernel_smoothing = [FFTSmoothingOperator(self._domain, sigma[x],
                                                 space=spaces[x])
Martin Reinecke's avatar
updates    
Martin Reinecke committed
63
                            for x in range(nsigma)]
64
        kernel_exposure = [DiagonalOperator(Field(self._domain[spaces[x]],
65
                                                  exposure[x]).weight(-1),
66
67
                                            domain=self._domain,
                                            spaces=(spaces[x],))
Martin Reinecke's avatar
Martin Reinecke committed
68
                           for x in range(nsigma)]
69
70
71

        self._composed_kernel = ComposedOperator(kernel_smoothing)
        self._composed_exposure = ComposedOperator(kernel_exposure)
72

73
        target_list = [FieldArray(self._domain[i].shape) for i in spaces]
Martin Reinecke's avatar
Martin Reinecke committed
74
        self._target = DomainTuple.make(target_list)
75

76
77
78
79
80
81
82
83
84
85
    @property
    def domain(self):
        return self._domain

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

    @property
    def unitary(self):
86
        return False
87

88
89
90
    def _times(self, x):
        res = self._composed_kernel.times(x)
        res = self._composed_exposure.times(res)
91
92
        # removing geometric information
        return Field(self._target, val=res.val)
93

94
    def _adjoint_times(self, x):
95
        # setting correct spaces
96
        res = Field(self.domain, val=x.val)
97
        res = self._composed_exposure.adjoint_times(res)
98
        res = res.weight(power=-1)
99
        return self._composed_kernel.adjoint_times(res)