exafs.py 7 KB
Newer Older
payno's avatar
payno committed
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
# 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"


31
32
33
import functools
import logging

payno's avatar
payno committed
34
from Orange.widgets import gui
payno's avatar
payno committed
35
from Orange.widgets.settings import Setting
36
from Orange.widgets.widget import OWWidget
37
38
from Orange.widgets.widget import Input, Output
import Orange.data
payno's avatar
payno committed
39
from est.core.types import XASObject
payno's avatar
payno committed
40
from PyMca5.PyMcaGui.physics.xas.XASPostEdgeParameters import XASPostEdgeParameters
41
42
from silx.gui import qt
from silx.gui.plot import LegendSelector
payno's avatar
payno committed
43
import est.core.process.pymca.exafs
44
from orangecontrib.est.process import _ProcessForOrangeMixIn
payno's avatar
payno committed
45
from est.gui.XasObjectViewer import XasObjectViewer, ViewType
payno's avatar
payno committed
46
47
48
49
50
from est.gui.XasObjectViewer import (
    _exafs_signal_plot,
    _exafs_postedge_plot,
    _exafs_knots_plot,
)
payno's avatar
payno committed
51
52
from orangecontrib.est.progress import QProgress
from orangecontrib.est.utils import Converter
payno's avatar
payno committed
53
from orangecontrib.est.widgets.container import _ParameterWindowContainer
54

payno's avatar
payno committed
55

payno's avatar
payno committed
56
57
58
_logger = logging.getLogger(__file__)


payno's avatar
payno committed
59
60
61
class ExafsWindow(qt.QMainWindow):
    def __init__(self, parent=None):
        qt.QMainWindow.__init__(self, parent)
payno's avatar
payno committed
62
        mapKeys = ["mu", "EXAFS.EXAFSKValues", "EXAFS.EXAFSSignal", "EXAFS.PostEdgeB"]
63
        self.xasObjViewer = XasObjectViewer(mapKeys=mapKeys)
64
65
        self.xasObjViewer._spectrumViews[0]._plotWidget.getXAxis().setLabel("K")
        self.xasObjViewer._spectrumViews[0]._plotWidget.getYAxis().setLabel(
payno's avatar
payno committed
66
67
            "Normalized Units"
        )
68
69
70
        self.setCentralWidget(self.xasObjViewer)

        # pymca window
payno's avatar
payno committed
71
72
73
        self._pymcaWindow = _ParameterWindowContainer(
            parent=self, parametersWindow=XASPostEdgeParameters
        )
payno's avatar
payno committed
74
75
76
77
78
79
        dockWidget = qt.QDockWidget(parent=self)
        dockWidget.setWidget(self._pymcaWindow)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, dockWidget)
        dockWidget.setAllowedAreas(qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea)
        dockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
        self.setWindowFlags(qt.Qt.Widget)
80
81

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

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

        # plot settings
payno's avatar
payno committed
97
        for ope in (_exafs_signal_plot, _exafs_postedge_plot, _exafs_knots_plot):
98
            self.xasObjViewer._spectrumViews[0].addCurveOperation(ope)
99

100
101
102
103
104
105
106
107
        self.setWindowFlags(qt.Qt.Widget)

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

        # set up
        self._updateLegendView()

108
    def getNCurves(self):
109
110
111
        return len(self.xasObjViewer.getAllCurves())

    def _updateLegendView(self):
112
113
        index, viewType = self.xasObjViewer.getViewType()
        self.legendDockWidget.setVisible(viewType is ViewType.spectrum)
payno's avatar
payno committed
114
115
116
        self.xasObjViewer._mapView.keySelectionDocker.setVisible(
            viewType is ViewType.map
        )
117

payno's avatar
payno committed
118

119
class ExafsOW(
120
    _ProcessForOrangeMixIn, ewokstaskclass=est.core.process.pymca.exafs.PyMca_exafs
121
):
payno's avatar
payno committed
122
123
124
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
125

payno's avatar
payno committed
126
    name = "exafs"
payno's avatar
payno committed
127
    id = "orange.widgets.est.pymca.exafs"
128
    description = "Progress signal extraction"
payno's avatar
payno committed
129
    icon = "icons/exafs.png"
130
    priority = 3
payno's avatar
payno committed
131
132
133
134
135
    category = "esrfWidgets"
    keywords = ["spectroscopy", "signal"]

    want_main_area = True
    resizing_enabled = True
136
    allows_cycle = False
payno's avatar
payno committed
137

138
    _larchSettings = Setting(dict())
139
140
    # kept for compatibility
    static_input = Setting({"xas_obj": None, "exafs": None})
payno's avatar
payno committed
141
    """Store the configuration of the PyMca XASClass"""
142

payno's avatar
payno committed
143
144
    def __init__(self):
        super().__init__()
145
        self._latest_xas_obj = None
payno's avatar
payno committed
146
        self._window = ExafsWindow()
payno's avatar
payno committed
147
        layout = gui.vBox(self.mainArea, "exafs").layout()
payno's avatar
payno committed
148
        layout.addWidget(self._window)
payno's avatar
payno committed
149

150
151
        self._window.xasObjViewer._spectrumViews[0]._plotWidget.getXAxis().setLabel("K")
        self._window.xasObjViewer._spectrumViews[0]._plotWidget.getYAxis().setLabel(
payno's avatar
payno committed
152
153
            "Normalized Units"
        )
payno's avatar
payno committed
154

payno's avatar
payno committed
155
        # manage settings
156
157
158
159
160
        pymca_settings = self.static_input.get("exafs", None)
        if pymca_settings is None:
            pymca_settings = self._larchSettings
        if pymca_settings != dict():
            self._window._pymcaWindow.setParameters(pymca_settings)
payno's avatar
payno committed
161

payno's avatar
payno committed
162
        # signal / slot connection
163
        self._window._pymcaWindow.sigChanged.connect(self._updateProcess)
payno's avatar
payno committed
164

165
        # self.handleNewSignals()
166

payno's avatar
payno committed
167
    def _updateProcess(self):
payno's avatar
payno committed
168
        self._update_settings()
169
170
        if self._latest_xas_obj:
            self.process(self._latest_xas_obj)
payno's avatar
payno committed
171

172
    def _update_settings(self):
173
        self._larchSettings = self._window._pymcaWindow.getParameters()
174
175
176
177
        self.static_input = {
            "xas_obj": None,
            "exafs": self._window._pymcaWindow.getParameters(),
        }
178

179
180
181
182
183
184
185
186
187
188
189
    def outputsChanged(self):
        if "xas_obj" in self.task_inputs:
            xas_obj = self.output_variables["xas_obj"].value
            if isinstance(xas_obj, dict):
                xas_obj = XASObject.from_dict(xas_obj)
            if hasattr(self, "_window") and hasattr(self._window, "setXASObj"):
                self._window.setXASObj(xas_obj=xas_obj)
            elif hasattr(self, "_window") and hasattr(self._window, "xasObjViewer"):
                if hasattr(self._window.xasObjViewer, "setXASObj"):
                    self._window.xasObjViewer.setXASObj(xas_obj=xas_obj)
            self.Outputs.xas_obj.send(xas_obj)