smooth_operator.py 3.3 KB
Newer Older
Jait Dixit's avatar
Jait Dixit committed
1
2
3
4
5
6
7
8
9
10
11
import numpy as np

from nifty.config import about
import nifty.nifty_utilities as utilities
from nifty import RGSpace, LMSpace
from nifty.operators.endomorphic_operator import EndomorphicOperator
from nifty.operators.fft_operator import FFTOperator

class SmoothOperator(EndomorphicOperator):

    # ---Overwritten properties and methods---
Jait Dixit's avatar
Jait Dixit committed
12
    def __init__(self, domain=(), field_type=(), inplace=False, sigma=None):
Jait Dixit's avatar
Jait Dixit committed
13
        super(SmoothOperator, self).__init__(domain=domain,
Jait Dixit's avatar
Jait Dixit committed
14
15
16
17
18
19
20
21
                                             field_type=field_type)

        if len(self.domain) != 1:
            raise ValueError(
                about._errors.cstring(
                    'ERROR: SmoothOperator accepts only exactly one '
                    'space as input domain.')
            )
Jait Dixit's avatar
Jait Dixit committed
22
23
24

        if self.field_type != ():
            raise ValueError(about._errors.cstring(
Jait Dixit's avatar
Jait Dixit committed
25
                'ERROR: SmoothOperator field-type must be an '
Jait Dixit's avatar
Jait Dixit committed
26
27
28
29
                'empty tuple.'
            ))

        self._sigma = sigma
Jait Dixit's avatar
Jait Dixit committed
30
31
32
33
        self._inplace = bool(inplace)

    def _inverse_times(self, x, spaces, types):
        return self._smooth_helper(x, spaces, types, inverse=True)
Jait Dixit's avatar
Jait Dixit committed
34
35

    def _times(self, x, spaces, types):
Jait Dixit's avatar
Jait Dixit committed
36
        return self._smooth_helper(x, spaces, types)
Jait Dixit's avatar
Jait Dixit committed
37

Jait Dixit's avatar
Jait Dixit committed
38
39
40
41
    # ---Mandatory properties and methods---
    @property
    def implemented(self):
        return True
Jait Dixit's avatar
Jait Dixit committed
42

Jait Dixit's avatar
Jait Dixit committed
43
44
45
    @property
    def symmetric(self):
        return False
Jait Dixit's avatar
Jait Dixit committed
46

Jait Dixit's avatar
Jait Dixit committed
47
48
49
    @property
    def unitary(self):
        return False
Jait Dixit's avatar
Jait Dixit committed
50
51
52
53
54
55
56
57
58

    # ---Added properties and methods---
    @property
    def sigma(self):
        return self._sigma

    @property
    def inplace(self):
        return self._inplace
Jait Dixit's avatar
Jait Dixit committed
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

    def _smooth_helper(self, x, spaces, types, inverse=False):
        if self.sigma == 0:
            return x if self.inplace else x.copy()

        spaces = utilities.cast_axis_to_tuple(spaces, len(x.domain))

        if spaces is None:
            return x if self.inplace else x.copy()

        # copy for doing the actual smoothing
        smooth_out = x.copy()

        space_obj = x.domain[spaces[0]]
        axes = x.domain_axes[spaces[0]]
        for space_axis, val_axis in zip(range(len(space_obj.shape)), axes):
            transform = FFTOperator(space_obj)
            kernel = space_obj.get_codomain_smoothing_kernel(
                self.sigma, space_axis
            )

            if isinstance(space_obj, RGSpace):
                new_shape = np.ones(len(x.shape), dtype=np.int)
                new_shape[val_axis] = len(kernel)
                kernel = kernel.reshape(new_shape)

                # transform
                smooth_out = transform(smooth_out, spaces=spaces[0])

                # multiply kernel
                if inverse:
                    smooth_out.val /= kernel
                else:
                    smooth_out.val *= kernel

                # inverse transform
                smooth_out = transform.inverse_times(smooth_out,
                                                     spaces=spaces[0])
            elif isinstance(space_obj, LMSpace):
                pass
            else:
                raise ValueError(about._errors.cstring(
                    'ERROR: SmoothOperator cannot smooth space ' +
                    str(space_obj)))

        if self.inplace:
            x.set_val(val=smooth_out.val)
            return x
        else:
            return smooth_out