Commit dc4002ff authored by payno's avatar payno

[orangecontrib][normalization][proto] clean and generalize qt.Runnable and ProcessThread

parent 82247dad
Pipeline #12430 failed with stage
in 60 minutes and 1 second
# 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
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):
self._configuration, self._spectrum = self._function(
spectrum=self._spectrum,
configuration=self._configuration,
overwrite=True)
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
:param :class:`.XASObject` xas_obj: object to process
:param :class:`.Process` process_obj: object to process xas_obj
"""
self._xas_obj = xas_obj
self._process_obj = process_obj
def run(self):
self._xas_obj = self._process_obj.process(self._xas_obj)
......@@ -38,6 +38,7 @@ from ..progress import QProgress
from xas.core.types import XASObject, Spectrum
from xas.gui.XasObjectViewer import XasObjectViewer, _CurveOperation, ViewType
import xas.core.process.normalization
from orangecontrib.xas.process import ProcessQThread, ProcessRunnable
import functools
import logging
......@@ -228,7 +229,7 @@ class NormalizationOW(OWWidget):
def getProcessingThread(self):
if self.__processingThread is None:
self.__processingThread = NormalizationQThread(parent=self)
self.__processingThread = ProcessQThread(parent=self)
return self.__processingThread
def _updateProcess(self):
......@@ -259,15 +260,15 @@ class NormalizationOW(OWWidget):
self.send("spectra", xas_obj)
from xas.core.process.normalization import _process_spectrum
from xas.core.process.normalization import _process_spectr_norm
# TODO: use directly the pymca_normalization, just use a different pool_process
class QPyMca_normalization(xas.core.process.normalization.PyMca_normalization, qt.QObject):
class QPyMca_normalization(xas.core.process.normalization.PyMca_normalization):
"""
Normalization able to give advancement using qt.Signal and QThreadPool
"""
# TODO: if possible remove the inheritance from qt.QObject
def __init__(self, parent=None):
qt.QObject.__init__(self, parent)
def __init__(self):
xas.core.process.normalization.PyMca_normalization.__init__(self)
self._advancement = QProgress('normalization')
......@@ -275,41 +276,9 @@ class QPyMca_normalization(xas.core.process.normalization.PyMca_normalization, q
self.pool = qt.QThreadPool()
self.pool.setMaxThreadCount(5)
for spectrum in xas_obj.spectra:
runnable = NormalizationRunnable(spectrum=spectrum,
configuration=xas_obj.configuration,
callback=self._advancement_callback)
runnable = ProcessRunnable(fct=_process_spectr_norm,
spectrum=spectrum,
configuration=xas_obj.configuration,
callback=self._advancement.increaseAdvancement)
self.pool.start(runnable)
self.pool.waitForDone()
# TODO: this should be removed
def _advancement_callback(self):
self._advancement.increaseAdvancement()
# TODO: this runnable can be generalized.
class NormalizationRunnable(qt.QRunnable):
def __init__(self, spectrum, configuration, callback=None):
qt.QRunnable.__init__(self)
self._spectrum = spectrum
self._configuration = configuration
self._callback = callback
def run(self):
self._configuration, self._spectrum = _process_spectrum(spectrum=self._spectrum,
configuration=self._configuration,
overwrite=True)
if self._callback:
self._callback()
# TODO: this process thread can be generalized to.
class NormalizationQThread(qt.QThread):
def __init__(self, parent=None):
qt.QThread.__init__(self, parent)
def init(self, xas_obj, process_obj):
self._xas_obj = xas_obj
self._process_obj = process_obj
def run(self):
self._xas_obj = self._process_obj.process(self._xas_obj)
......@@ -37,7 +37,7 @@ import logging
_logger = logging.getLogger(__name__)
def _process_spectrum(spectrum, configuration, overwrite=True):
def _process_spectr_norm(spectrum, configuration, overwrite=True):
if spectrum.mu is None:
_logger.error('Mu is not specified, unable to normalize')
return None, None
......@@ -114,7 +114,7 @@ class PyMca_normalization(Process):
def _pool_process(self, xas_obj):
"""process normalization from a pool"""
with multiprocessing.Pool(5) as p:
partial_ = functools.partial(_process_spectrum,
partial_ = functools.partial(_process_spectr_norm,
configuration=xas_obj.configuration,
callback=self._advancement.increaseAdvancement,
overwrite=True)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment