xftf.py 6.6 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
import est.core.process.larch.xftf
from orangecontrib.est.process import _ProcessForOrangeMixIn
payno's avatar
payno committed
38
39
40
41
42
43
from est.gui.XasObjectViewer import (
    XasObjectViewer,
    ViewType,
    _plot_chir_mag,
    _plot_chir_re,
    _plot_chir_imag,
44
    _plot_masked_chi_weighted_k,
payno's avatar
payno committed
45
)
payno's avatar
payno committed
46
47
from est.gui.larch.xftf import _MXFTFParameters
from orangecontrib.est.widgets.container import _ParameterWindowContainer
48
from ewokscore.hashing import MissingData
49
50
51
52
53
54
55
56

_logger = logging.getLogger(__file__)


def _get_title(dgroup, title=None):
    """get best title for group"""
    if title is not None:
        return title
payno's avatar
payno committed
57
    data_group = getattr(dgroup, "data", None)
58

payno's avatar
payno committed
59
    for attr in ("title", "plot_title", "filename", "name", "__name__"):
60
61
62
63
64
65
66
67
68
69
70
        t = getattr(dgroup, attr, None)
        if t is not None:
            return t
        if data_group is not None:
            t = getattr(data_group, attr, None)
            if t is not None:
                return t

    return repr(dgroup)


71
class XFTFWindow(qt.QMainWindow):
72
73
74
75
    def __init__(self, parent=None):
        qt.QMainWindow.__init__(self, parent)

        # xas object viewer
76
        mapKeys = ["mu", "chir", "chir_mag", "chir_re", "chir_im", "chir_pha"]
77
78
79
        self.xasObjViewer = XasObjectViewer(
            mapKeys=mapKeys, spectrumPlots=("chi", "masked_chi_weighted_k")
        )
80
81
        self.xasObjViewer._spectrumViews[0]._plotWidget.getXAxis().setLabel(
            "Radial distance (angstrom)"
payno's avatar
payno committed
82
        )
83
84
85
        self.xasObjViewer._spectrumViews[1]._plotWidget.getXAxis().setLabel(
            "wavenumber (angstrom^-1)"
        )
86
        self.setCentralWidget(self.xasObjViewer)
payno's avatar
payno committed
87
88
89
        self._parametersWindow = _ParameterWindowContainer(
            parent=self, parametersWindow=_MXFTFParameters
        )
90
91
92
93
94
95
96
97
        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)

98
99
        # FT legend selector
        self._ftLegendDockWidget = LegendSelector.LegendsDockWidget(
100
            parent=self, plot=self.xasObjViewer._spectrumViews[0]._plotWidget
101
102
        )
        self._ftLegendDockWidget.setAllowedAreas(
payno's avatar
payno committed
103
104
            qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea
        )
105
106
        self._ftLegendDockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, self._ftLegendDockWidget)
107
108

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

        # plot settings
114
        for ope in (_plot_chir_mag, _plot_chir_re, _plot_chir_imag):
115
            self.xasObjViewer._spectrumViews[0].addCurveOperation(ope)
116
117
118
        self.xasObjViewer._spectrumViews[1].addCurveOperation(
            _plot_masked_chi_weighted_k
        )
119
120
121
122
123
124
125
126
127
128
        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()
129
130
131
        self._ftLegendDockWidget.setVisible(
            viewType is ViewType.spectrum and index == 0
        )
payno's avatar
payno committed
132
133
134
        self.xasObjViewer._mapView.keySelectionDocker.setVisible(
            viewType is ViewType.map
        )
135

136
    def getNCurves(self):
137
        return len(self.xasObjViewer._spectrumViews[0]._plotWidget.getAllCurves())
138

139
    def setKWeight(self, kweight):
140
        self._parametersWindow._mainwidget.setRWeight(kweight)
141

142

143
144
class XFTFOW(
    _ProcessForOrangeMixIn,
145
    ewokstaskclass=est.core.process.larch.xftf.Larch_xftf,
146
):
147
148
149
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
150

151
    name = "xftf"
payno's avatar
payno committed
152
    id = "orange.widgets.est.larch.xftf"
payno's avatar
payno committed
153
154
155
156
    description = (
        "forward XAFS Fourier transform, from chi(k) to chi(R), "
        "using common XAFS conventions."
    )
157
158
159
    icon = "icons/xftf.png"
    priority = 2
    category = "esrfWidgets"
payno's avatar
payno committed
160
    keywords = ["spectroscopy", "xftf", "fourier transform"]
161
162
163
164
165

    want_main_area = True
    resizing_enabled = True

    _larchSettings = Setting(dict())
166
167
    # kept for compatibility
    static_input = Setting({"xftf": None, "xas_obj": None})
168
169
170
171
    """Store the configuration of the larch configuration"""

    def __init__(self):
        super().__init__()
172
        self._window = XFTFWindow(parent=self)
payno's avatar
payno committed
173
        layout = gui.vBox(self.mainArea, "xftf").layout()
174
        layout.addWidget(self._window)
payno's avatar
payno committed
175
        self._window.xasObjViewer.setWindowTitle("spectra")
176
177

        # manage settings
178
179
        larch_settings = self.task_input_values.get("xftf", MissingData)
        if larch_settings is not MissingData:
180
            self._window._parametersWindow.setParameters(larch_settings)
181
182
183
184
185

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

    def _updateProcess(self):
186
        self.handleNewSignals()
187
188
189
        self.receiveDynamicInputs(
            "xftf", self._window._parametersWindow.getParameters()
        )