pre_edge.py 11.8 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
34
# 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__ = "06/07/2019"


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.pre_edge
from orangecontrib.est.process import _ProcessForOrangeMixIn
from est.core.types import XASObject
from est.gui.XasObjectViewer import XasObjectViewer, ViewType
payno's avatar
payno committed
44
from est.gui.XasObjectViewer import (
45
    _plot_raw,
payno's avatar
payno committed
46
47
48
49
50
51
    _plot_edge,
    _plot_norm,
    _plot_norm_area,
    _plot_post_edge,
    _plot_pre_edge,
)
payno's avatar
payno committed
52
53
54
55
from est.gui.larch.pre_edge import _MPreEdgeParameters
from orangecontrib.est.progress import QProgress
from orangecontrib.est.utils import Converter
from orangecontrib.est.widgets.container import _ParameterWindowContainer
56
57
58

_logger = logging.getLogger(__file__)

59
_USE_THREAD = False
60
61


62
class _PreEdgeWindow(qt.QMainWindow):
63
64
65
66
    def __init__(self, parent=None):
        qt.QMainWindow.__init__(self, parent)

        # xas object viewer
payno's avatar
payno committed
67
        mapKeys = ["mu", "norm", "norm_area"]
68
        self.xasObjViewer = XasObjectViewer(mapKeys=mapKeys)
payno's avatar
payno committed
69
70
71
        self.xasObjViewer._spectrumViews[0]._plotWidget.getXAxis().setLabel(
            "Energy (eV)"
        )
72
        self.setCentralWidget(self.xasObjViewer)
payno's avatar
payno committed
73
74
75
        self._parametersWindow = _ParameterWindowContainer(
            parent=self, parametersWindow=_MPreEdgeParameters
        )
76
77
78
79
80
81
82
83
84
        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)

        # legend selector
payno's avatar
payno committed
85
        self.legendDockWidget = LegendSelector.LegendsDockWidget(
86
            parent=self, plot=self.xasObjViewer._spectrumViews[0]._plotWidget
payno's avatar
payno committed
87
88
89
90
        )
        self.legendDockWidget.setAllowedAreas(
            qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea
        )
91
92
93
94
        self.legendDockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, self.legendDockWidget)

        # volume key selection
payno's avatar
payno committed
95
96
97
        self.addDockWidget(
            qt.Qt.RightDockWidgetArea, self.xasObjViewer._mapView.keySelectionDocker
        )
98
99

        # plot settings
payno's avatar
payno committed
100
        for ope in (
101
            _plot_raw,
payno's avatar
payno committed
102
103
104
105
106
107
            _plot_norm,
            _plot_norm_area,
            _plot_pre_edge,
            _plot_post_edge,
            _plot_edge,
        ):
108
109
110
111
112
113
114
115
116
117
118
119
120
            self.xasObjViewer._spectrumViews[0].addCurveOperation(ope)

        self.setWindowFlags(qt.Qt.Widget)

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

        # set up
        self._updateLegendView()

    def _updateLegendView(self):
        index, viewType = self.xasObjViewer.getViewType()
        self.legendDockWidget.setVisible(viewType is ViewType.spectrum)
payno's avatar
payno committed
121
122
123
        self.xasObjViewer._mapView.keySelectionDocker.setVisible(
            viewType is ViewType.map
        )
124

125
    def getNCurves(self):
126
        return len(self.xasObjViewer._spectrumViews[0]._plotWidget.getAllCurves())
127

128

129
130
class PreEdgeOW(
    _ProcessForOrangeMixIn,
131
    OWWidget,
132
):
133
134
135
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
136

137
    name = "pre edge"
payno's avatar
payno committed
138
    id = "orange.widgets.est.larch.pre_edge.PreEdgeOW"
139
140
141
142
143
144
145
146
147
148
149
150
    description = """
    pre edge subtraction, normalization for XAFS
    This performs a number of steps:
       1. determine E0 (if not supplied) from max of deriv(mu)
       2. fit a line of polynomial to the region below the edge
       3. fit a polynomial to the region above the edge
       4. extrapolate the two curves to E0 to determine the edge jump
       5. estimate area from emin_area to norm2, to get norm_area
    """
    icon = "icons/pre_edge.png"
    priority = 2
    category = "esrfWidgets"
payno's avatar
payno committed
151
    keywords = ["spectroscopy", "preedge", "pre", "edge", "normalization"]
152
153
154

    want_main_area = True
    resizing_enabled = True
155
    allows_cycle = False
156
    ewokstaskclass = est.core.process.larch.pre_edge.Larch_pre_edge
157
158

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

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

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

174
175
176
    def __init__(self):
        super().__init__()
        self._latest_xas_obj = None
177
        self._window = _PreEdgeWindow(parent=self)
payno's avatar
payno committed
178
        layout = gui.vBox(self.mainArea, "pre edge").layout()
179
        layout.addWidget(self._window)
payno's avatar
payno committed
180
        self._window.xasObjViewer.setWindowTitle("spectra")
181
182

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

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

192
193
        # required to display advancement
        if _USE_THREAD is False:
payno's avatar
payno committed
194
            self._advancement = QProgress("pre_edge")
195
196
            self._advancement.sigProgress.connect(self._setProgressValue)

197
        # set up (insure settings will be store
198
        self._update_settings()
199
        # self.handleNewSignals()
200

201
202
203
204
205
206
207
    def _updateProcess(self):
        self._update_settings()
        if self._latest_xas_obj:
            self.process(self._latest_xas_obj)

    def _update_settings(self):
        self._larchSettings = self._window._parametersWindow.getParameters()
208
209
210
211
        self.static_input = {
            "pre_edge": self._window._parametersWindow.getParameters(),
            "xas_obj": None,
        }
212

213
214
215
216
217
218
219
    @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
220
221
222
223
224
    def process(self, xas_obj):
        if xas_obj is None:
            return

        if not self._canProcess():
payno's avatar
payno committed
225
226
227
228
            _logger.warning(
                "There is some processing on going already, will"
                "not process the new dataset"
            )
229

230
        self._latest_xas_obj = xas_obj
231
232

        # e0 can be defined from the flow previously
payno's avatar
payno committed
233
234
        if "e0" in xas_obj.configuration:
            conf = {"e0": xas_obj.configuration["e0"]}
235
            self._window._parametersWindow.setParameters(conf)
236
237
238
        self._startProcess()

        # setup the normalization process
239
        inputs = {"xas_obj": xas_obj}
240
        if _USE_THREAD is True:
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
            # larch is using sqlite3 for database. access to the database
            # require a 'connection' and 'cursor'. theoretically we should be
            # able to access it from a single thread using something like:
            #
            # import sqlite3
            # from larch.xray import xraydb
            # import os
            #
            # dbname = 'xraydata.db'
            # parent, _ = os.path.split(xraydb.__file__)
            # file_ = os.path.join(parent, dbname)
            # if not os.path.exists(file_):
            #     raise IOError('%s does not exists' % file_)
            # self.connection = sqlite3.connect(file_)
            # cursor = self.connection.cursor()
            # self._processingThread = PreEdgeThread(advancement=self._advancement,
            #                                        args=(self.connection, cursor))
            # working until at one point (in time) the database is closed by larch
            # or there is some concurrency failling ?
            # so fo rnow we are making the processing in the main thread that
            # larch is happy about and splitting it in a way to avoid gui
            # freeze
263
            process_obj = QLarch_pre_edge(inputs=inputs)
264
            process_obj._advancement.sigProgress.connect(self._setProgressValue)
265
            process_obj.set_properties(
payno's avatar
payno committed
266
267
                {"_larchSettings": self._window._parametersWindow.getParameters()}
            )
268
269
270
            # update the processing thread
            thread = self.getProcessingThread()
            thread.init(process_obj=process_obj, xas_obj=self._latest_xas_obj)
payno's avatar
payno committed
271
272
273
            self._callback_finish = functools.partial(
                self._endProcess, self._latest_xas_obj
            )
274
275
276
277
            thread.finished.connect(self._callback_finish)
            # start processing
            thread.start(priority=qt.QThread.LowPriority)
        else:
278
279
280
            # manage advancement
            self._advancement.setAdvancement(0)
            self._advancement.setMaxSpectrum(self._latest_xas_obj.n_spectrum)
281
            process_obj = est.core.process.larch.pre_edge.Larch_pre_edge(inputs=inputs)
282
            process_obj.advancement = self._advancement
283
            process_obj.set_properties(
payno's avatar
payno committed
284
285
                {"_larchSettings": self._window._parametersWindow.getParameters()}
            )
286
287
288
289
            # 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)
290
            process_obj.run()
291
292
            self._callback_finish = None
            self._endProcess(self._latest_xas_obj)
293
294


payno's avatar
payno committed
295
class QLarch_pre_edge(est.core.process.larch.pre_edge.Larch_pre_edge):
296
297
298
299
    """
    Normalization able to give advancement using qt.Signal and QThreadPool
    """

300
301
    def __init__(self, *args, **kwargs):
        est.core.process.larch.pre_edge.Larch_pre_edge.__init__(self, *args, **kwargs)
payno's avatar
payno committed
302
        self._advancement = QProgress("pre_edge")
303
304
305
306
307

    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
308
309
310
311
312
313
            runnable = ProcessRunnable(
                fct=est.core.process.larch.pre_edge.process_spectr_pre_edge,
                spectrum=spectrum,
                configuration=xas_obj.configuration,
                callback=self._advancement.increaseAdvancement,
            )
314
315
            self.pool.start(runnable)
        self.pool.waitForDone()