process.py 5.16 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/07/2019"


from silx.gui import qt
32
33
34
35
36
37
from Orange.widgets import gui
import logging

_logger = logging.getLogger(__file__)


38
class _ProcessForOrangeMixIn(object):
39
    """
40
41
    Group processing and progress display in a common class for xasObject
    process.
42
43
44

    If this process own a widget to display the xas object then this one should
    be named '_window'
45
    """
46

47
48
49
50
51
52
53
54
55
    def __init__(self):
        # progress
        self._progress = gui.ProgressBar(self, 100)
        """progress bar"""
        self.__processingThread = None
        """Thread for processing"""
        # progress
        self._progress = gui.ProgressBar(self, 100)

56
    def _startProcess(self):
payno's avatar
payno committed
57
        if hasattr(self, "_window"):
58
            self._window.setEnabled(False)
59
        self._progress.widget.progressBarInit()
60

61
    def _endProcess(self, xas_obj):
payno's avatar
payno committed
62
        if hasattr(self, "_window"):
63
            self._window.setEnabled(True)
64
65
        if self._callback_finish:
            try:
payno's avatar
payno committed
66
67
68
                self.getProcessingThread()._process_obj._advancement.sigProgress.disconnect(
                    self._setProgressValue
                )
69
70
71
72
73
74
75
76
            except ... as e:
                _logger.error(str(e))

            self.getProcessingThread().finished.disconnect(self._callback_finish)
            self._callback_finish = None
        if xas_obj is None:
            return
        else:
payno's avatar
payno committed
77
78
79
80
81
            if hasattr(self, "_window") and hasattr(self._window, "setXASObj"):
                self._window.setXASObj(xas_obj=xas_obj)
            elif hasattr(self, "_window") and hasattr(self._window, "xasObjViewer"):
                if hasattr(self._window.xasObjViewer, "setXASObj"):
                    self._window.xasObjViewer.setXASObj(xas_obj=xas_obj)
82
            # emit signal for the plot
83
            self.Outputs.xas_obj.send(xas_obj)
84

85
    def _canProcess(self):
payno's avatar
payno committed
86
87
88
        return (
            self.__processingThread is None or not self.__processingThread.isRunning()
        )
89
90
91
92
93
94
95
96

    def getProcessingThread(self):
        if self.__processingThread is None:
            self.__processingThread = ProcessQThread(parent=self)
        return self.__processingThread

    def _setProgressValue(self, value):
        self._progress.widget.progressBarSet(value)
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118


class ProcessRunnable(qt.QRunnable):
    """
    qt Runnable for standard process.
    process function should take as input(spectrum, configuration, overwrite)

    :param function pointer fct: process function
    :param :class:`.Spectrum`: spectrum to process
    :param dict configuration: configuration of the process
    :param function pointer callback: optional callback to execute at the end of
                                      the run. Should take no parameter
    """

    def __init__(self, fct, spectrum, configuration, callback=None):
        qt.QRunnable.__init__(self)
        self._spectrum = spectrum
        self._configuration = configuration
        self._callback = callback
        self._function = fct

    def run(self):
119
120
121
122
        try:
            self._configuration, self._spectrum = self._function(
                spectrum=self._spectrum,
                configuration=self._configuration,
payno's avatar
payno committed
123
124
                overwrite=True,
            )
125
126
        except (KeyError, ValueError) as e:
            _logger.error(e)
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        if self._callback:
            self._callback()


class ProcessQThread(qt.QThread):
    """
    Thread dedicated to process execution.
    """

    def __init__(self, parent=None):
        qt.QThread.__init__(self, parent)

    def init(self, xas_obj, process_obj):
        """
        Initialize the thread for processing xas_obj from proces_obj

payno's avatar
payno committed
143
        :param :class:`.XASObject` xas_obj: object to process
144
145
146
147
148
149
        :param :class:`.Process` process_obj: object to process xas_obj
        """
        self._xas_obj = xas_obj
        self._process_obj = process_obj

    def run(self):
150
        self._xas_obj = self._process_obj()