energyroi.py 5.31 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
31
32
33
# 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
from Orange.widgets.settings import Setting
34
from Orange.widgets.widget import OWWidget
payno's avatar
payno committed
35
36
37
38
39
40
41
42
43
44
45
from Orange.widgets.widget import Input, Output
import Orange.data
from orangecontrib.est.utils import Converter
from silx.gui import qt
import est.core.process.energyroi
from est.core.types import XASObject
from est.gui.energyroi import EnergyRoiWidget

_logger = logging.getLogger(__file__)


46
class EnergyRoiOW(
47
    OWWidget,
48
):
payno's avatar
payno committed
49
50
51
52
53
54
55
56
57
    """
    Widget used to make the selection of a region of Interest to treat in a
    Dataset.
    """

    name = "energy ROI"
    id = "orange.widgets.xas.utils.energyroi"
    description = "Select the energy ROI to analyse"
    icon = "icons/curveroi.svg"
58
    ewokstaskclass = est.core.process.energyroi.EnergyROIProcess
payno's avatar
payno committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

    priority = 8
    category = "esrfWidgets"
    keywords = [
        "dataset",
        "data",
        "selection",
        "ROI",
        "Region of Interest",
        "energy ROI",
        "energy",
    ]

    want_main_area = True
    resizing_enabled = True
    compress_signal = False
    allows_cycle = False

    _energy_roi = Setting(dict())

    class Inputs:
        xas_obj = Input("xas_obj", XASObject, default=True)
        # simple compatibility for some Orange widget and especialy the
        # 'spectroscopy add-on'
        data_table = Input("Data", Orange.data.Table)

    class Outputs:
86
        xas_obj = Output("xas_obj", XASObject)
payno's avatar
payno committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        # 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 = EnergyRoiWidget(parent=self)
        layout = gui.vBox(self.mainArea, "energy roi").layout()
        layout.addWidget(self._widget)

        # buttons
        types = qt.QDialogButtonBox.Ok
        self._buttons = qt.QDialogButtonBox(parent=self)
        self._buttons.setStandardButtons(types)
        layout.addWidget(self._buttons)

        self._buttons.hide()

        # connect signal / slot
        self._buttons.accepted.connect(self.validate)

        # expose API
        self.setROI = self._widget.setROI
        self.getROI = self._widget.getROI

112
        # self.handleNewSignals()
113

payno's avatar
payno committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    @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

        roi_process = est.core.process.energyroi.EnergyROIProcess()
        minE, maxE = self.getROI()
        roi_prop = {"minE": minE, "maxE": maxE}
        roi_process.set_properties(roi_prop)
        # we want to keep the raw data in roi object, to be able to resize
        # the roi (especially increase it size)
        xas_obj = roi_process.process(xas_obj=self._widget.getXasObject())
142
        self.Outputs.xas_obj.send(xas_obj)
payno's avatar
payno committed
143
144
145
146
147
148
149
150
        try:
            roi_process = est.core.process.energyroi.EnergyROIProcess()
            minE, maxE = self.getROI()
            roi_prop = {"minE": minE, "maxE": maxE}
            roi_process.set_properties(roi_prop)
            # we want to keep the raw data in roi object, to be able to resize
            # the roi (especially increase it size)
            xas_obj = roi_process.process(xas_obj=self._widget.getXasObject())
151
            self.Outputs.xas_obj.send(xas_obj)
payno's avatar
payno committed
152
153
154
        except Exception as e:
            _logger.error(e)
        else:
155
            OWEwoksWidget.accept(self)
payno's avatar
payno committed
156
157
158
159
160
161
162
163

    def updateProperties(self):
        # as dim are named tuple we have to cast them to dict
        minE, maxE = self.getROI()
        self._energy_roi = {
            "minE": minE,
            "maxE": maxE,
        }