autobk.py 6.28 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 logging
from Orange.widgets import gui
from silx.gui import qt
from silx.gui.plot import LegendSelector
payno's avatar
payno committed
35
36
37
import est.core.process.larch.autobk
from orangecontrib.est.process import _ProcessForOrangeMixIn
from est.gui.XasObjectViewer import XasObjectViewer, ViewType
38
39
40
41
42
43
from est.gui.XasObjectViewer import (
    _plot_bkg,
    _plot_spectrum,
    _plot_knots,
    _plot_chi,
)
payno's avatar
payno committed
44
45
from est.gui.larch.autobk import _AutobkParameters
from orangecontrib.est.widgets.container import _ParameterWindowContainer
46
from ewokscore.hashing import MissingData
47
48
49

_logger = logging.getLogger(__file__)

50
_USE_THREAD = False
51

52
53
54
55
56
57

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

        # xas object viewer
payno's avatar
payno committed
58
        mapKeys = ["mu", "bkg", "chie", "k", "chi", "e0"]
59
60
61
        self.xasObjViewer = XasObjectViewer(
            mapKeys=mapKeys, spectrumPlots=("background", "chi(k)")
        )
payno's avatar
payno committed
62
63
64
        self.xasObjViewer._spectrumViews[0]._plotWidget.getXAxis().setLabel(
            "Energy (eV)"
        )
65
        self.xasObjViewer._spectrumViews[0]._plotWidget.getYAxis().setLabel(
payno's avatar
payno committed
66
67
            "Absorption (a.u.)"
        )
68
        self.setCentralWidget(self.xasObjViewer)
payno's avatar
payno committed
69
70
71
        self._parametersWindow = _ParameterWindowContainer(
            parent=self, parametersWindow=_AutobkParameters
        )
72
73
74
75
76
77
78
79
        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)

80
81
        # bkg legend selector
        self.bkgLegendDockWidget = LegendSelector.LegendsDockWidget(
82
            parent=self, plot=self.xasObjViewer._spectrumViews[0]._plotWidget
payno's avatar
payno committed
83
        )
84
85
86
87
88
89
90
91
92
93
94
        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
95
96
            qt.Qt.RightDockWidgetArea | qt.Qt.LeftDockWidgetArea
        )
97
98
        self._chiLegendDockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
        self.addDockWidget(qt.Qt.RightDockWidgetArea, self._chiLegendDockWidget)
99
100

        # volume key selection
payno's avatar
payno committed
101
102
103
        self.addDockWidget(
            qt.Qt.RightDockWidgetArea, self.xasObjViewer._mapView.keySelectionDocker
        )
104
105

        # plot settings
106
        for ope in (_plot_bkg, _plot_spectrum, _plot_knots):
107
108
            self.xasObjViewer._spectrumViews[0].addCurveOperation(ope)

109
110
        self.xasObjViewer._spectrumViews[1].addCurveOperation(_plot_chi)

111
112
113
114
115
116
117
118
119
        self.setWindowFlags(qt.Qt.Widget)

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

        # set up
        self._updateLegendView()

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

    def _updateLegendView(self):
        index, viewType = self.xasObjViewer.getViewType()
124
125
126
127
128
129
        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
130
131
132
        self.xasObjViewer._mapView.keySelectionDocker.setVisible(
            viewType is ViewType.map
        )
133
134


135
136
137
class AutobkOW(
    _ProcessForOrangeMixIn, ewokstaskclass=est.core.process.larch.autobk.Larch_autobk
):
138
139
140
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
141

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

    want_main_area = True
    resizing_enabled = True

    def __init__(self):
        super().__init__()
        self._window = AutobkWindow(parent=self)
payno's avatar
payno committed
156
        layout = gui.vBox(self.mainArea, "autobk").layout()
157
        layout.addWidget(self._window)
payno's avatar
payno committed
158
        self._window.xasObjViewer.setWindowTitle("spectra")
159
160

        # manage settings
161
162
        larch_settings = self.task_input_values.get("autobk", MissingData)
        if larch_settings is not MissingData:
163
            self._window._parametersWindow.setParameters(larch_settings)
164
165

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

    def _updateProcess(self):
169
        """Update settings keeping current xas obj"""
170
        self.handleNewSignals()
171
172
173
        self.receiveDynamicInputs(
            "autobk", self._window._parametersWindow.getParameters()
        )