roi.py 4.85 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__ = "02/10/2018"

import logging
31

payno's avatar
payno committed
32
33
from Orange.widgets import gui
from Orange.widgets.settings import Setting
34
from Orange.widgets.widget import OWWidget
35
36
from Orange.widgets.widget import Input, Output
import Orange.data
payno's avatar
payno committed
37
from orangecontrib.est.utils import Converter
38
from silx.gui import qt
payno's avatar
payno committed
39
40
41
import est.core.process.roi
from est.core.types import XASObject
from est.gui.roiselector import ROISelector
payno's avatar
payno committed
42
43
44
45

_logger = logging.getLogger(__file__)


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

54
    name = "ROI definition"
55
    id = "orange.widgets.xas.utils.roiselection"
payno's avatar
payno committed
56
57
58
59
60
61
62
63
64
65
    description = "Select data Region Of Interest"
    icon = "icons/image-select-box.svg"

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

    want_main_area = True
    resizing_enabled = True
    compress_signal = False
66
    allows_cycle = False
67
    ewokstaskclass = est.core.process.roi.ROIProcess
payno's avatar
payno committed
68
69
70
71

    _roi_origin = Setting(tuple())
    _roi_size = Setting(tuple())

72
    class Inputs:
payno's avatar
payno committed
73
        xas_obj = Input("xas_obj", XASObject, default=True)
74
75
        # simple compatibility for some Orange widget and especialy the
        # 'spectroscopy add-on'
payno's avatar
payno committed
76
        data_table = Input("Data", Orange.data.Table)
77
78

    class Outputs:
79
        xas_obj = Output("xas_obj", XASObject)
80
81
82
        # by default we want to avoid sending 'Orange.data.Table' to avoid
        # loosing the XASObject flow process and results.

payno's avatar
payno committed
83
84
85
    def __init__(self):
        super().__init__()

payno's avatar
payno committed
86
        self._widget = ROISelector(parent=self)
payno's avatar
payno committed
87
        layout = gui.vBox(self.mainArea, "data selection").layout()
payno's avatar
payno committed
88
89
90
91
92
93
94
95
96
        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()
97
98

        # connect signal / slot
payno's avatar
payno committed
99
100
101
102
        self._buttons.accepted.connect(self.validate)

        # expose API
        self.setROI = self._widget.setROI
payno's avatar
payno committed
103
        self.getROI = self._widget.getROI
payno's avatar
payno committed
104

105
        # self.handleNewSignals()
106

107
108
109
110
111
112
113
    @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
payno's avatar
payno committed
114
115
116
117
118
119
    def process(self, xas_obj):
        if xas_obj is None:
            return
        self._widget.setXasObject(xas_obj=xas_obj)
        self._buttons.show()
        self.show()
payno's avatar
payno committed
120
121
122
123
124

    def validate(self):
        """
        callback when the ROI has been validated
        """
payno's avatar
payno committed
125
        if self._widget.getXasObject() is None:
payno's avatar
payno committed
126
127
128
            return

        try:
payno's avatar
payno committed
129
            roi_process = est.core.process.roi.ROIProcess()
payno's avatar
payno committed
130
131
            xas_roi = est.core.process.roi._ROI.from_silx_def(self.getROI()).to_dict()
            roi_prop = {"roi": xas_roi}
132
            roi_process.set_properties(roi_prop)
payno's avatar
payno committed
133
134
            # we want to keep the raw data in roi object, to be able to resize
            # the roi (especially increase it size)
135
            xas_obj = roi_process.process(xas_obj=self._widget.getXasObject())
136
            self.Outputs.xas_obj.send(xas_obj)
payno's avatar
payno committed
137
138
139
        except Exception as e:
            _logger.error(e)
        else:
140
            OWEwoksWidget.accept(self)
payno's avatar
payno committed
141
142
143
144
145

    def updateProperties(self):
        # as dim are named tuple we have to cast them to dict
        self._roi_origin = tuple(self._widget.getROI().getOrigin())
        self._roi_size = tuple(self._widget.getROI().getSize())