e0calculator.py 5.35 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
# 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__ = "02/10/2018"

import logging

from Orange.widgets import gui
33
from Orange.widgets.widget import OWWidget
34
35
36
37
38
from Orange.widgets.widget import Input, Output
import Orange.data
from orangecontrib.est.utils import Converter
from silx.gui import qt
import functools
39
import est.core.process.ignoreprocess
40
41
42
43
44
45
46
import est.core.process.setconfig
from est.core.types import XASObject
from est.gui.e0calculator import E0Calculator, E0ComputationMethod

_logger = logging.getLogger(__file__)


47
class E0calculatorOW(
48
    OWWidget,
49
):
50
51
52
53
54
55
56
57
58
59
60
    """
    Widget used to make compute E0 from the dataset.
    """

    name = "e0 calculator"
    id = "orange.widgets.xas.utils.e0calculator"
    description = "Compute E0 from a region of the dataset"
    icon = "icons/e0.svg"

    priority = 25
    category = "esrfWidgets"
payno's avatar
payno committed
61
62
63
64
65
66
67
68
69
    keywords = [
        "E0",
        "Energy",
        "dataset",
        "data",
        "selection",
        "ROI",
        "Region of Interest",
    ]
70
71
72
73

    want_main_area = True
    resizing_enabled = True
    compress_signal = False
74
    allows_cycle = False
75
    ewokstaskclass = est.core.process.ignoreprocess.IgnoreE0Calculation
76
77

    class Inputs:
payno's avatar
payno committed
78
        xas_obj = Input("xas_obj", XASObject, default=True)
79
80
        # simple compatibility for some Orange widget and especialy the
        # 'spectroscopy add-on'
payno's avatar
payno committed
81
        data_table = Input("Data", Orange.data.Table)
82
83

    class Outputs:
84
        xas_obj = Output("xas_obj", XASObject)
85
86
87
88
89
90
91
        # by default we want to avoid sending 'Orange.data.Table' to avoid
        # loosing the XASObject flow process and results.

    def __init__(self):
        super().__init__()

        self._widget = E0Calculator(parent=self)
payno's avatar
payno committed
92
        layout = gui.vBox(self.mainArea, "data selection").layout()
93
94
95
96
97
98
99
        layout.addWidget(self._widget)

        # add the buttons
        style = qt.QApplication.instance().style()
        icon = style.standardIcon(qt.QStyle.SP_DialogApplyButton)

        self._buttons = qt.QDialogButtonBox(parent=self)
payno's avatar
payno committed
100
        self._useMedian = qt.QPushButton(icon, "use median", self)
101
        self._buttons.addButton(self._useMedian, qt.QDialogButtonBox.ActionRole)
payno's avatar
payno committed
102
        self._useMean = qt.QPushButton(icon, "use mean", self)
103
104
105
106
        self._buttons.addButton(self._useMean, qt.QDialogButtonBox.ActionRole)
        layout.addWidget(self._buttons)

        # connect signal / slot
payno's avatar
payno committed
107
108
109
110
111
112
        self._useMean.released.connect(
            functools.partial(self.validateMethodToUse, E0ComputationMethod.MEAN)
        )
        self._useMedian.released.connect(
            functools.partial(self.validateMethodToUse, E0ComputationMethod.MEDIAN)
        )
113
114
115
116

        # set up
        self._buttons.hide()

117
        # self.handleNewSignals()
118

119
120
121
    def validateMethodToUse(self, method):
        """Define the method to use and close the dialog"""
        method = E0ComputationMethod.from_value(method)
payno's avatar
payno committed
122
        assert method in (None, E0ComputationMethod.MEDIAN, E0ComputationMethod.MEAN)
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        self._methodToUse = method
        self.validate()

    def getE0(self):
        if self._methodToUse is None:
            return None
        else:
            return self._widget.getE0(method=self._methodToUse)

    @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
    def process(self, xas_obj):
        if xas_obj is None:
            return
        self._widget.setXasObject(xas_obj=xas_obj)
        self._buttons.show()
        self.show()

    def validate(self):
        """
        callback when the ROI has been validated
        """
        if self._widget.getXasObject() is None:
            return

        try:
            xas_obj = self._widget.getXasObject()
            prop = xas_obj.configuration
payno's avatar
payno committed
156
            prop["e0"] = self.getE0()
157
            xas_obj.configuration = prop
payno's avatar
payno committed
158
            _logger.info("e0 define: {}".format(str(self.getE0())))
159
            self.Outputs.xas_obj.send(xas_obj)
160
161
162
163
        except Exception as e:
            _logger.error(e)
        else:
            OWWidget.accept(self)