noise.py 6.66 KB
Newer Older
1
2
3
4
5
6
7
8
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
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "18/01/2021"

import logging

from Orange.widgets import gui
from Orange.widgets.settings import Setting
34
from Orange.widgets.widget import OWWidget
35
36
37
38
39
40
from Orange.widgets.widget import Input, Output
from orangecontrib.est.progress import QProgress
from orangecontrib.est.process import ProcessRunnable
import Orange.data
import functools
from orangecontrib.est.utils import Converter
payno's avatar
payno committed
41
from orangecontrib.est.process import _ProcessForOrangeMixIn
42
43
44
45
46
47
48
49
from silx.gui import qt
import est.core.process.noise
from est.core.types import XASObject
from est.gui.noise import SavitskyGolayNoise

_logger = logging.getLogger(__file__)


50
51
class NoiseOW(
    _ProcessForOrangeMixIn,
52
    OWWidget,
53
):
54
55
56
57
58
    """
    Widget used to make the selection of a region of Interest to treat in a
    Dataset.
    """

59
    name = "noise"
60
61
62
63
64
65
66
67
68
69
70
71
    id = "orange.widgets.xas.utils.noise"
    description = "Compute noise using Savitsky-Golay"
    icon = "icons/noise.svg"

    priority = 40
    category = "esrfWidgets"
    keywords = ["dataset", "data", "noise"]

    want_main_area = True
    resizing_enabled = True
    compress_signal = False
    allows_cycle = False
72
    ewokstaskclass = est.core.process.noise.NoiseProcess
73

payno's avatar
payno committed
74
75
    window_size = Setting(int(-1))
    polynomial_order = Setting(int(-1))
76
77
    e_min = Setting(int(-1))
    e_max = Setting(int(-1))
78
79
80
81
82
83
84
85

    class Inputs:
        xas_obj = Input("xas_obj", XASObject, default=True)
        # simple compatibility for some Orange widget and especialy the
        # 'spectroscopy add-on'
        data_table = Input("Data", Orange.data.Table)

    class Outputs:
86
        xas_obj = Output("xas_obj", XASObject)
87
88
89
90
91
92
93
        # by default we want to avoid sending 'Orange.data.Table' to avoid
        # loosing the XASObject flow process and results.

    def __init__(self):
        super().__init__()
        self._latest_xas_obj = None

payno's avatar
payno committed
94
        self._window = SavitskyGolayNoise(parent=self)
95
        layout = gui.vBox(self.mainArea, "noise").layout()
payno's avatar
payno committed
96
        layout.addWidget(self._window)
97
98
99
100
101
102
103
104
105
106

        # buttons
        types = qt.QDialogButtonBox.Ok
        self._buttons = qt.QDialogButtonBox(parent=self)
        self._buttons.setStandardButtons(types)
        layout.addWidget(self._buttons)

        self._buttons.hide()

        # expose API
payno's avatar
payno committed
107
108
109
110
        self.setWindowSize = self._window.setWindowSize
        self.getWindowSize = self._window.getWindowSize
        self.setPolynomialOrder = self._window.setPolynomialOrder
        self.getPolynomialOrder = self._window.getPolynomialOrder
111
112

        # manage settings
payno's avatar
payno committed
113
        self._window.setParameters(
payno's avatar
payno committed
114
            {
payno's avatar
payno committed
115
116
                "window_size": self.window_size,
                "polynomial_order": self.polynomial_order,
117
118
                "e_min": self.e_min,
                "e_max": self.e_max,
payno's avatar
payno committed
119
120
            }
        )
121
122
123

        # signal / slot connection
        # connect signals / slots
payno's avatar
payno committed
124
        self._window.sigChanged.connect(self._updateProcess)
125

126
        # self.handleNewSignals()
127

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
    def _updateProcess(self, *arv, **kwargs):
        self._update_settings()
        if self._latest_xas_obj:
            self.process(xas_obj=self._latest_xas_obj)

    @Inputs.data_table
    def processFrmDataTable(self, data_table):
        if data_table is None:
            return
        self.process(Converter.toXASObject(data_table=data_table))

    @Inputs.xas_obj
    def process(self, xas_obj):
        if xas_obj is None:
            return

        if not self._canProcess():
            _logger.warning(
                "There is some processing on going already, will"
                "not process the new dataset"
            )

        self._latest_xas_obj = xas_obj
        self._startProcess()

        # setup the ft process
154
        process_obj = QNoise(inputs={"xas_obj": xas_obj})
payno's avatar
payno committed
155
156
        process_obj._advancement.sigProgress.connect(self._setProgressValue)
        process_obj.set_properties({"noise": self._window.getParameters()})
157
158
159
160
161
162
163
164
165
166
167
168
169

        # update the processing thread
        thread = self.getProcessingThread()
        thread.init(process_obj=process_obj, xas_obj=self._latest_xas_obj)
        self._callback_finish = functools.partial(
            self._endProcess, self._latest_xas_obj
        )
        thread.finished.connect(self._callback_finish)

        # start processing
        thread.start(priority=qt.QThread.LowPriority)

    def _update_settings(self):
payno's avatar
payno committed
170
171
        self.window_size = self._window.getWindowSize()
        self.polynomial_order = self._window.getPolynomialOrder()
172
173
        self.e_min = self._window.getEMin()
        self.e_max = self._window.getEMax()
174
175
176
177
178
179
180
181
182
183

    def _setProgressValue(self, value):
        self._progress.widget.progressBarSet(value)


class QNoise(est.core.process.noise.NoiseProcess):
    """
    Normalization able to give advancement using qt.Signal and QThreadPool
    """

184
185
    def __init__(self, *args, **kwargs):
        est.core.process.noise.NoiseProcess.__init__(self, *args, **kwargs)
186
187
188
189
190
191
192
        self._advancement = QProgress("noise")

    def _pool_process(self, xas_obj):
        self.pool = qt.QThreadPool()
        self.pool.setMaxThreadCount(5)
        for spectrum in xas_obj.spectra:
            runnable = ProcessRunnable(
payno's avatar
payno committed
193
                fct=est.core.process.noise.process_noise_savgol,
194
195
196
197
198
199
                spectrum=spectrum,
                configuration=xas_obj.configuration,
                callback=self._advancement.increaseAdvancement,
            )
            self.pool.start(runnable)
        self.pool.waitForDone()