autobk.py 10.9 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
# 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"
28
__date__ = "04/10/2021"
29
30
31
32
33
34


import functools
import logging
from Orange.widgets import gui
from Orange.widgets.settings import Setting
35
from Orange.widgets.widget import OWWidget
36
37
from Orange.widgets.widget import Input, Output
import Orange.data
38
39
from silx.gui import qt
from silx.gui.plot import LegendSelector
payno's avatar
payno committed
40
41
42
43
import est.core.process.larch.autobk
from orangecontrib.est.process import _ProcessForOrangeMixIn
from est.core.types import XASObject
from est.gui.XasObjectViewer import XasObjectViewer, ViewType
44
45
46
47
48
49
from est.gui.XasObjectViewer import (
    _plot_bkg,
    _plot_spectrum,
    _plot_knots,
    _plot_chi,
)
payno's avatar
payno committed
50
51
52
53
from est.gui.larch.autobk import _AutobkParameters
from orangecontrib.est.progress import QProgress
from orangecontrib.est.utils import Converter
from orangecontrib.est.widgets.container import _ParameterWindowContainer
54
55
56

_logger = logging.getLogger(__file__)

57
_USE_THREAD = False
58

59
60
61
62
63
64

class AutobkWindow(qt.QMainWindow):
    def __init__(self, parent=None):
        qt.QMainWindow.__init__(self, parent)

        # xas object viewer
payno's avatar
payno committed
65
        mapKeys = ["mu", "bkg", "chie", "k", "chi", "e0"]
66
67
68
        self.xasObjViewer = XasObjectViewer(
            mapKeys=mapKeys, spectrumPlots=("background", "chi(k)")
        )
payno's avatar
payno committed
69
70
71
        self.xasObjViewer._spectrumViews[0]._plotWidget.getXAxis().setLabel(
            "Energy (eV)"
        )
72
        self.xasObjViewer._spectrumViews[0]._plotWidget.getYAxis().setLabel(
payno's avatar
payno committed
73
74
            "Absorption (a.u.)"
        )
75
        self.setCentralWidget(self.xasObjViewer)
payno's avatar
payno committed
76
77
78
        self._parametersWindow = _ParameterWindowContainer(
            parent=self, parametersWindow=_AutobkParameters
        )
79
80
81
82
83
84
85
86
        dockWidget = qt.QDockWidget(parent=self)

        # parameters window
        dockWidget.setWidget(self._parametersWindow)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, dockWidget)
        dockWidget.setAllowedAreas(qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea)
        dockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)

87
88
        # bkg legend selector
        self.bkgLegendDockWidget = LegendSelector.LegendsDockWidget(
89
            parent=self, plot=self.xasObjViewer._spectrumViews[0]._plotWidget
payno's avatar
payno committed
90
        )
91
92
93
94
95
96
97
98
99
100
101
        self.bkgLegendDockWidget.setAllowedAreas(
            qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea
        )
        self.bkgLegendDockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, self.bkgLegendDockWidget)

        # chi legend selector
        self._chiLegendDockWidget = LegendSelector.LegendsDockWidget(
            parent=self, plot=self.xasObjViewer._spectrumViews[1]._plotWidget
        )
        self._chiLegendDockWidget.setAllowedAreas(
payno's avatar
payno committed
102
103
            qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea
        )
104
105
        self._chiLegendDockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, self._chiLegendDockWidget)
106
107

        # volume key selection
payno's avatar
payno committed
108
109
110
        self.addDockWidget(
            qt.Qt.RightDockWidgetArea, self.xasObjViewer._mapView.keySelectionDocker
        )
111
112

        # plot settings
113
        for ope in (_plot_bkg, _plot_spectrum, _plot_knots):
114
115
            self.xasObjViewer._spectrumViews[0].addCurveOperation(ope)

116
117
        self.xasObjViewer._spectrumViews[1].addCurveOperation(_plot_chi)

118
119
120
121
122
123
124
125
126
        self.setWindowFlags(qt.Qt.Widget)

        # connect signal / slot
        self.xasObjViewer.viewTypeChanged.connect(self._updateLegendView)

        # set up
        self._updateLegendView()

    def getNCurves(self):
127
        return len(self.xasObjViewer._spectrumViews[0]._plotWidget.getAllCurves())
128
129
130

    def _updateLegendView(self):
        index, viewType = self.xasObjViewer.getViewType()
131
132
133
134
135
136
        self.bkgLegendDockWidget.setVisible(
            viewType is ViewType.spectrum and index == 0
        )
        self._chiLegendDockWidget.setVisible(
            viewType is ViewType.spectrum and index == 1
        )
payno's avatar
payno committed
137
138
139
        self.xasObjViewer._mapView.keySelectionDocker.setVisible(
            viewType is ViewType.map
        )
140
141


142
class AutobkOW(_ProcessForOrangeMixIn, OWWidget):
143
144
145
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
146

147
    name = "autobk"
payno's avatar
payno committed
148
    id = "orange.widgets.est.larch.autobk"
149
150
151
152
    description = "background removal"
    icon = "icons/autobk.png"
    priority = 1
    category = "esrfWidgets"
payno's avatar
payno committed
153
    keywords = ["spectroscopy", "autobk", "background"]
154
155
156

    want_main_area = True
    resizing_enabled = True
157
    allows_cycle = False
158

159
    _larchSettings = Setting(dict())
160
161
162
    # kept for compatibility
    static_input = Setting({"xas_obj": None, "autobk": None})
    """Store the configuration of the larch configuration"""
163

164
165
    ewokstaskclass = est.core.process.larch.autobk.Larch_autobk

166
    class Inputs:
payno's avatar
payno committed
167
        xas_obj = Input("xas_obj", XASObject, default=True)
168
169
        # simple compatibility for some Orange widget and especialy the
        # 'spectroscopy add-on'
payno's avatar
payno committed
170
        data_table = Input("Data", Orange.data.Table)
171
172

    class Outputs:
173
        xas_obj = Output("xas_obj", XASObject)
174
175
176
        # by default we want to avoid sending 'Orange.data.Table' to avoid
        # loosing the XASObject flow process and results.

177
178
179
180
    def __init__(self):
        super().__init__()
        self._latest_xas_obj = None
        self._window = AutobkWindow(parent=self)
payno's avatar
payno committed
181
        layout = gui.vBox(self.mainArea, "autobk").layout()
182
        layout.addWidget(self._window)
payno's avatar
payno committed
183
        self._window.xasObjViewer.setWindowTitle("spectra")
184
185

        # manage settings
186
        larch_settings = self.static_input.get("autobk", None)
187
188
189
190
        if larch_settings is None:
            larch_settings = self._larchSettings
        if larch_settings != dict():
            self._window._parametersWindow.setParameters(larch_settings)
191
192

        # connect signals / slots
193
        self._window._parametersWindow.sigChanged.connect(self._updateProcess)
194

195
196
        # required to display advancement
        if _USE_THREAD is False:
payno's avatar
payno committed
197
            self._advancement = QProgress("autobk")
198
199
            self._advancement.sigProgress.connect(self._setProgressValue)

200
        # set up (insure settings will be store
201
        self._update_settings()
202

203
        # self.handleNewSignals()
204

205
    def _updateProcess(self):
206
        """Update settings keeping current xas obj"""
207
208
209
210
211
        self._update_settings()
        if self._latest_xas_obj:
            self.process(self._latest_xas_obj)

    def _update_settings(self):
212
        self._larchSettings = self._window._parametersWindow.getParameters()
213
214
215
216
        self.static_input = {
            "autobk": self._window._parametersWindow.getParameters(),
            "xas_obj": None,
        }
217

218
219
220
221
222
223
224
    @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
225
226
227
228
229
    def process(self, xas_obj):
        if xas_obj is None:
            return

        if not self._canProcess():
payno's avatar
payno committed
230
231
232
233
            _logger.warning(
                "There is some processing on going already, will"
                "not process the new dataset"
            )
234

235
        self._latest_xas_obj = xas_obj
236
237
238
        self._startProcess()

        # setup the normalization process
239
        inputs = {"xas_obj": xas_obj}
240
241
242
        if _USE_THREAD is True:
            # note: for now with larch with cannot do thread computation (see
            # PreEdgeOW )
243
            process_obj = QLarch_autobk(inputs=inputs)
244
            process_obj._advancement.sigProgress.connect(self._setProgressValue)
245
            process_obj.set_properties(
payno's avatar
payno committed
246
247
                {"_larchSettings": self._window._parametersWindow.getParameters()}
            )
248
249
250
251

            # update the processing thread
            thread = self.getProcessingThread()
            thread.init(process_obj=process_obj, xas_obj=self._latest_xas_obj)
payno's avatar
payno committed
252
253
254
            self._callback_finish = functools.partial(
                self._endProcess, self._latest_xas_obj
            )
255
256
257
            thread.finished.connect(self._callback_finish)
            # start processing
            thread.start(priority=qt.QThread.LowPriority)
258
259
260
261
        else:
            # manage advancement
            self._advancement.setAdvancement(0)
            self._advancement.setMaxSpectrum(self._latest_xas_obj.n_spectrum)
262
            process_obj = est.core.process.larch.autobk.Larch_autobk(inputs=inputs)
263
            process_obj.advancement = self._advancement
264
            process_obj.set_properties(
payno's avatar
payno committed
265
266
                {"_larchSettings": self._window._parametersWindow.getParameters()}
            )
267
268
269
270
            # hack (reason explained in the thread part)
            # to avoid gui freeze, processevents after each spectrum process
            process_obj.addCallback(qt.QApplication.instance().processEvents)
            process_obj.addCallback(self._advancement.increaseAdvancement)
271
            process_obj.run()
272
273
            self._callback_finish = None
            self._endProcess(self._latest_xas_obj)
274
275


payno's avatar
payno committed
276
class QLarch_autobk(est.core.process.larch.autobk.Larch_autobk):
277
278
279
280
    """
    Normalization able to give advancement using qt.Signal and QThreadPool
    """

281
282
    def __init__(self, *args, **kwargs):
        est.core.process.larch.autobk.Larch_autobk.__init__(self, *args, **kwargs)
payno's avatar
payno committed
283
        self._advancement = QProgress("autobk")
284
285
286
287
288

    def _pool_process(self, xas_obj):
        self.pool = qt.QThreadPool()
        self.pool.setMaxThreadCount(5)
        for spectrum in xas_obj.spectra:
payno's avatar
payno committed
289
290
291
292
293
294
            runnable = ProcessRunnable(
                fct=est.core.process.larch.autobk.process_spectr_autobk,
                spectrum=spectrum,
                configuration=xas_obj.configuration,
                callback=self._advancement.increaseAdvancement,
            )
295
296
            self.pool.start(runnable)
        self.pool.waitForDone()