xas_output.py 4.83 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
# 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/11/2019"


from Orange.widgets import gui
32
from Orange.widgets.widget import OWWidget
33
from Orange.widgets.settings import Setting
34
35
from Orange.widgets.widget import Input
from silx.gui import qt
payno's avatar
payno committed
36
from est.core.types import XASObject
37
import est.core.process.io
38
import logging
39
import h5py
payno's avatar
payno committed
40

41
42
43
_logger = logging.getLogger(__file__)


44
class XASOutputOW(
45
    OWWidget,
46
):
47
48
49
    """
    Widget used for signal extraction
    """
payno's avatar
payno committed
50

51
    name = "xas output"
52
    id = "orange.widgets.xas.utils.xas_output"
53
54
55
56
57
58
59
60
    description = "Store process result (configuration)"
    icon = "icons/output.png"
    priority = 5
    category = "esrfWidgets"
    keywords = ["spectroscopy", "signal", "output", "file"]

    want_main_area = True
    resizing_enabled = True
61
    allows_cycle = False
62
    ewokstaskclass = est.core.process.io.DumpXasObject
63
64
65

    _output_file_setting = Setting(str())

66
    class Inputs:
payno's avatar
payno committed
67
        xas_obj = Input("xas_obj", XASObject, default=True)
68

69
70
71
72
73
    def __init__(self):
        super().__init__()
        self._outputWindow = qt.QWidget(parent=self)
        self._outputWindow.setLayout(qt.QGridLayout())

payno's avatar
payno committed
74
75
        self._outputWindow.layout().addWidget(qt.QLabel("file", parent=self))
        self._inputLe = qt.QLineEdit("", parent=self)
76
        self._outputWindow.layout().addWidget(self._inputLe, 0, 0)
payno's avatar
payno committed
77
        self._selectPB = qt.QPushButton("select", parent=self)
78
79
        self._outputWindow.layout().addWidget(self._selectPB, 0, 1)

80
81
82
        spacer = qt.QWidget(parent=self)
        spacer.setSizePolicy(qt.QSizePolicy.Minimum, qt.QSizePolicy.Expanding)
        self._outputWindow.layout().addWidget(spacer, 2, 0)
83

payno's avatar
payno committed
84
        layout = gui.vBox(self.mainArea, "output").layout()
85
86
87
        layout.addWidget(self._outputWindow)

        # deal with settings
88
        self.setFileSelected(self._output_file_setting)
89
90
91
92

        # signal / slot connection
        self._selectPB.pressed.connect(self._selectFile)

93
        # self.handleNewSignals()
94

95
96
97
    def _selectFile(self, *args, **kwargs):
        old = self.blockSignals(True)
        dialog = qt.QFileDialog(self)
98
99
        # dialog.setFileMode(qt.QFileDialog.AnyFile)
        dialog.setAcceptMode(qt.QFileDialog.AcceptSave)
payno's avatar
payno committed
100
        dialog.setNameFilters(["hdf5 files (*.hdf5, *.hdf, *.h5)"])
101
102
103

        if not dialog.exec_():
            dialog.close()
104
            return False
105
106
107

        fileSelected = dialog.selectedFiles()
        if len(fileSelected) == 0:
108
            return False
109
110
        else:
            assert len(fileSelected) == 1
111
112
            file_ = fileSelected[0]
            if not h5py.is_hdf5(file_):
payno's avatar
payno committed
113
                file_ += ".h5"
114
            self.setFileSelected(file_)
115
116

        self.blockSignals(old)
117
        return True
118

119
    def setFileSelected(self, file_path):
120
121
122
        self._output_file_setting = file_path
        self._inputLe.setText(file_path)

123
    def _getFileSelected(self):
124
125
        return self._inputLe.text()

126
    @Inputs.xas_obj
127
    def process(self, xas_obj):
128
129
        if xas_obj is None:
            return
130
        if isinstance(xas_obj, dict):
131
            _xas_obj = XASObject.from_dict(xas_obj)
132
133
134
        else:
            _xas_obj = xas_obj

payno's avatar
payno committed
135
        has_file = self._getFileSelected() != ""
136
        if not has_file:
137
138
            mess = qt.QMessageBox(parent=self)
            mess.setIcon(qt.QMessageBox.Warning)
payno's avatar
payno committed
139
            mess.setText("No output file defined, please give a file path")
140
            mess.exec_()
141
142
            has_file = self._selectFile()
        if not has_file:
payno's avatar
payno committed
143
            _logger.error("no valid output file given, skip save")
144
        else:
145
            _xas_obj.dump(self._getFileSelected())