pre_edge.py 6.17 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
35
# 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 logging
from Orange.widgets import gui
from Orange.widgets.settings import Setting
from silx.gui import qt
from silx.gui.plot import LegendSelector
payno's avatar
payno committed
36
37
38
import est.core.process.larch.pre_edge
from orangecontrib.est.process import _ProcessForOrangeMixIn
from est.gui.XasObjectViewer import XasObjectViewer, ViewType
payno's avatar
payno committed
39
from est.gui.XasObjectViewer import (
40
    _plot_raw,
payno's avatar
payno committed
41
42
43
44
45
46
    _plot_edge,
    _plot_norm,
    _plot_norm_area,
    _plot_post_edge,
    _plot_pre_edge,
)
payno's avatar
payno committed
47
48
from est.gui.larch.pre_edge import _MPreEdgeParameters
from orangecontrib.est.widgets.container import _ParameterWindowContainer
49
from ewokscore.hashing import MissingData
50
51
52

_logger = logging.getLogger(__file__)

53
_USE_THREAD = False
54
55


56
class _PreEdgeWindow(qt.QMainWindow):
57
58
59
60
    def __init__(self, parent=None):
        qt.QMainWindow.__init__(self, parent)

        # xas object viewer
payno's avatar
payno committed
61
        mapKeys = ["mu", "norm", "norm_area"]
62
        self.xasObjViewer = XasObjectViewer(mapKeys=mapKeys)
payno's avatar
payno committed
63
64
65
        self.xasObjViewer._spectrumViews[0]._plotWidget.getXAxis().setLabel(
            "Energy (eV)"
        )
66
        self.setCentralWidget(self.xasObjViewer)
payno's avatar
payno committed
67
68
69
        self._parametersWindow = _ParameterWindowContainer(
            parent=self, parametersWindow=_MPreEdgeParameters
        )
70
71
72
73
74
75
76
77
78
        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
79
        self.legendDockWidget = LegendSelector.LegendsDockWidget(
80
            parent=self, plot=self.xasObjViewer._spectrumViews[0]._plotWidget
payno's avatar
payno committed
81
82
83
84
        )
        self.legendDockWidget.setAllowedAreas(
            qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea
        )
85
86
87
88
        self.legendDockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, self.legendDockWidget)

        # volume key selection
payno's avatar
payno committed
89
90
91
        self.addDockWidget(
            qt.Qt.RightDockWidgetArea, self.xasObjViewer._mapView.keySelectionDocker
        )
92
93

        # plot settings
payno's avatar
payno committed
94
        for ope in (
95
            _plot_raw,
payno's avatar
payno committed
96
97
98
99
100
101
            _plot_norm,
            _plot_norm_area,
            _plot_pre_edge,
            _plot_post_edge,
            _plot_edge,
        ):
102
103
104
105
106
107
108
109
110
111
112
113
114
            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
115
116
117
        self.xasObjViewer._mapView.keySelectionDocker.setVisible(
            viewType is ViewType.map
        )
118

119
    def getNCurves(self):
120
        return len(self.xasObjViewer._spectrumViews[0]._plotWidget.getAllCurves())
121

122

123
124
class PreEdgeOW(
    _ProcessForOrangeMixIn,
125
    ewokstaskclass=est.core.process.larch.pre_edge.Larch_pre_edge,
126
):
127
128
129
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
130

131
    name = "pre edge"
payno's avatar
payno committed
132
    id = "orange.widgets.est.larch.pre_edge.PreEdgeOW"
133
134
135
136
137
138
139
140
141
142
143
144
    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
145
    keywords = ["spectroscopy", "preedge", "pre", "edge", "normalization"]
146
147
148
149
150

    want_main_area = True
    resizing_enabled = True

    _larchSettings = Setting(dict())
151
152
    # kept for compatibility
    static_input = Setting({"pre_edge": None})
153
154
155
156
    """Store the configuration of the larch configuration"""

    def __init__(self):
        super().__init__()
157
        self._window = _PreEdgeWindow(parent=self)
payno's avatar
payno committed
158
        layout = gui.vBox(self.mainArea, "pre edge").layout()
159
        layout.addWidget(self._window)
payno's avatar
payno committed
160
        self._window.xasObjViewer.setWindowTitle("spectra")
161
162

        # manage settings
163
164
        larch_settings = self.task_input_values.get("pre_edge", MissingData)
        if larch_settings is not MissingData:
165
            self._window._parametersWindow.setParameters(larch_settings)
166
167
168
169
170

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

    def _updateProcess(self):
171
172
173
        self.receiveDynamicInputs(
            "pre_edge", self._window._parametersWindow.getParameters()
        )
174
        self.handleNewSignals()