Commit 503c6635 authored by payno's avatar payno
Browse files

[orangecontrib][larch] normaize call to larch: from the main thread with some...

[orangecontrib][larch] normaize call to larch: from the main thread with some callback to QApplication.processEvents
parent 253ff75d
......@@ -46,7 +46,7 @@ from xas.gui.XasObjectViewer import _plot_bkg, _plot_spectrum, _plot_knots
from xas.gui.larch.autobk import _AutobkParameters
from orangecontrib.xas.progress import QProgress
from orangecontrib.xas.utils import Converter
from .container import _ParameterWindowContainer
from orangecontrib.xas.widgets.container import _ParameterWindowContainer
_logger = logging.getLogger(__file__)
......@@ -151,6 +151,11 @@ class AutobkOW(_ProcessForOrangeMixIn, OWWidget):
# connect signals / slots
self._window._parametersWindow.sigChanged.connect(self._updateProcess)
# required to display advancement
if _USE_THREAD is False:
self._advancement = QProgress('autobk')
self._advancement.sigProgress.connect(self._setProgressValue)
def _updateProcess(self):
"""Update settings keeping current xas obj"""
self._update_settings()
......@@ -179,12 +184,9 @@ class AutobkOW(_ProcessForOrangeMixIn, OWWidget):
self._startProcess()
# setup the normalization process
if _USE_THREAD is False:
process_obj = xas.core.process.larch.autobk.Larch_autobk()
self._callback_finish = None
process_obj.process(self._latest_xas_obj)
self._endProcess(self._latest_xas_obj)
else:
if _USE_THREAD is True:
# note: for now with larch with cannot do thread computation (see
# PreEdgeOW )
process_obj = QLarch_autobk()
process_obj._advancement.sigProgress.connect(self._setProgressValue)
process_obj.setProperties({
......@@ -197,6 +199,21 @@ class AutobkOW(_ProcessForOrangeMixIn, OWWidget):
thread.finished.connect(self._callback_finish)
# start processing
thread.start(priority=qt.QThread.LowPriority)
else:
# manage advancement
self._advancement.setAdvancement(0)
self._advancement.setMaxSpectrum(self._latest_xas_obj.n_spectrum)
process_obj = xas.core.process.larch.autobk.Larch_autobk()
process_obj.advancement = self._advancement
process_obj.setProperties({
'_larchSettings': self._window._parametersWindow.getParameters()})
# hack (reason explained in the thread part)
# to avoid gui freeze, processevents after each spectrum process
process_obj.addCallback(qt.QApplication.instance().processEvents)
process_obj.addCallback(self._advancement.increaseAdvancement)
process_obj.process(self._latest_xas_obj)
self._callback_finish = None
self._endProcess(self._latest_xas_obj)
class QLarch_autobk(xas.core.process.larch.autobk.Larch_autobk):
......
......@@ -46,7 +46,7 @@ from xas.gui.larch.mback import _MBackParameters
from orangecontrib.xas.progress import QProgress
from xas.gui.XasObjectViewer import _plot_norm, _plot_raw, _plot_fpp, _plot_f2
from orangecontrib.xas.utils import Converter
from .container import _ParameterWindowContainer
from orangecontrib.xas.widgets.container import _ParameterWindowContainer
_logger = logging.getLogger(__file__)
......@@ -151,6 +151,11 @@ class MbackOW(_ProcessForOrangeMixIn, OWWidget):
# connect signals / slots
self._window._parametersWindow.sigChanged.connect(self._updateProcess)
# required to display advancement
if _USE_THREAD is False:
self._advancement = QProgress('mback')
self._advancement.sigProgress.connect(self._setProgressValue)
def _updateProcess(self):
self._update_settings()
if self._latest_xas_obj:
......@@ -178,12 +183,9 @@ class MbackOW(_ProcessForOrangeMixIn, OWWidget):
self._startProcess()
# setup the normalization process
if _USE_THREAD is False:
process_obj = xas.core.process.larch.mback.Larch_mback()
self._callback_finish = None
process_obj.process(xas_obj=self._latest_xas_obj)
self._endProcess(self._latest_xas_obj)
else:
if _USE_THREAD is True:
# note: for now with larch with cannot do thread computation (see
# PreEdgeOW )
process_obj = QLarch_mback()
process_obj._advancement.sigProgress.connect(self._setProgressValue)
process_obj.setProperties({
......@@ -195,6 +197,21 @@ class MbackOW(_ProcessForOrangeMixIn, OWWidget):
thread.finished.connect(self._callback_finish)
# start processing
thread.start(priority=qt.QThread.LowPriority)
else:
# manage advancement
self._advancement.setAdvancement(0)
self._advancement.setMaxSpectrum(self._latest_xas_obj.n_spectrum)
process_obj = xas.core.process.larch.mback.Larch_mback()
process_obj.advancement = self._advancement
process_obj.setProperties({
'_larchSettings': self._window._parametersWindow.getParameters()})
# hack (reason explained in the thread part)
# to avoid gui freeze, processevents after each spectrum process
process_obj.addCallback(qt.QApplication.instance().processEvents)
process_obj.addCallback(self._advancement.increaseAdvancement)
process_obj.process(self._latest_xas_obj)
self._callback_finish = None
self._endProcess(self._latest_xas_obj)
class QLarch_mback(xas.core.process.larch.mback.Larch_mback):
......
......@@ -46,7 +46,7 @@ from xas.gui.XasObjectViewer import _plot_norm, _plot_mback_mu
from xas.gui.larch.mback import _MBackParameters
from orangecontrib.xas.progress import QProgress
from orangecontrib.xas.utils import Converter
from .container import _ParameterWindowContainer
from orangecontrib.xas.widgets.container import _ParameterWindowContainer
_logger = logging.getLogger(__file__)
......@@ -151,6 +151,11 @@ class Mback_normOW(_ProcessForOrangeMixIn, OWWidget):
# connect signals / slots
self._window._parametersWindow.sigChanged.connect(self._updateProcess)
# required to display advancement
if _USE_THREAD is False:
self._advancement = QProgress('mback_norm')
self._advancement.sigProgress.connect(self._setProgressValue)
def _updateProcess(self):
self._update_settings()
if self._latest_xas_obj:
......@@ -178,13 +183,10 @@ class Mback_normOW(_ProcessForOrangeMixIn, OWWidget):
self._startProcess()
# setup the normalization process
if _USE_THREAD is False:
process_obj = xas.core.process.larch.mback_norm.Larch_mback_norm()
self._callback_finish = None
process_obj.process(self._latest_xas_obj)
self._endProcess(self._latest_xas_obj)
else:
process_obj = QLarch_mback()
if _USE_THREAD is True:
# note: for now with larch with cannot do thread computation (see
# PreEdgeOW )
process_obj = QLarch_mback_norm()
process_obj._advancement.sigProgress.connect(self._setProgressValue)
process_obj.setProperties({
'_larchSettings': self._window._parametersWindow.getParameters()})
......@@ -195,9 +197,24 @@ class Mback_normOW(_ProcessForOrangeMixIn, OWWidget):
thread.finished.connect(self._callback_finish)
# start processing
thread.start(priority=qt.QThread.LowPriority)
else:
# manage advancement
self._advancement.setAdvancement(0)
self._advancement.setMaxSpectrum(self._latest_xas_obj.n_spectrum)
process_obj = xas.core.process.larch.mback_norm.Larch_mback_norm()
process_obj.advancement = self._advancement
process_obj.setProperties({
'_larchSettings': self._window._parametersWindow.getParameters()})
# hack (reason explained in the thread part)
# to avoid gui freeze, processevents after each spectrum process
process_obj.addCallback(qt.QApplication.instance().processEvents)
process_obj.addCallback(self._advancement.increaseAdvancement)
process_obj.process(self._latest_xas_obj)
self._callback_finish = None
self._endProcess(self._latest_xas_obj)
class QLarch_mback(xas.core.process.larch.mback_norm.Larch_mback_norm):
class QLarch_mback_norm(xas.core.process.larch.mback_norm.Larch_mback_norm):
"""
Normalization able to give advancement using qt.Signal and QThreadPool
"""
......
......@@ -47,55 +47,13 @@ from xas.gui.XasObjectViewer import (_plot_edge, _plot_norm, _plot_norm_area,
from xas.gui.larch.pre_edge import _MPreEdgeParameters
from orangecontrib.xas.progress import QProgress
from orangecontrib.xas.utils import Converter
from .container import _ParameterWindowContainer
from orangecontrib.xas.widgets.container import _ParameterWindowContainer
_logger = logging.getLogger(__file__)
_USE_THREAD = False
class _PreEdgeParameters(qt.QWidget):
"""
same as _MPreEdgeParameters but add an option to call processing
automatically or when an 'update' button is pressed
"""
sigChanged = qt.Signal()
def __init__(self, parent):
super().__init__(parent=parent)
self.setLayout(qt.QGridLayout())
self._mainwidget = _ParameterWindowContainer(parent=self,
parametersWindow=_MPreEdgeParameters)
self.layout().addWidget(self._mainwidget, 0, 0, 1, 3)
self._autoCB = qt.QCheckBox('manual update', parent=self)
self.layout().addWidget(self._autoCB, 1, 1, 1, 1)
self._autoCB.setToolTip('if activated will wait until you press the '
'"update" button to launch processing. '
'Otherwise executed for each modification in '
'the paramters')
self._manualUpdatePB = qt.QPushButton('update')
self.layout().addWidget(self._manualUpdatePB, 1, 2, 1, 1)
# setup
self._manualUpdatePB.setVisible(False)
# expose API
self.setParameters = self._mainwidget.setParameters
self.getParameters = self._mainwidget.getParameters
# connect signal / slot
self._autoCB.toggled.connect(self._manualUpdatePB.setVisible)
self._manualUpdatePB.pressed.connect(self._update)
self._mainwidget.sigChanged.connect(self._filteredUpdate)
def _filteredUpdate(self, *args, **kwargs):
"""call _update only if in automatic update mode"""
if not self._autoCB.isChecked():
self._update()
def _update(self, *args, **kwargs):
self.sigChanged.emit()
class _PreEdgeWindow(qt.QMainWindow):
def __init__(self, parent=None):
qt.QMainWindow.__init__(self, parent)
......@@ -105,7 +63,8 @@ class _PreEdgeWindow(qt.QMainWindow):
self.xasObjViewer = XasObjectViewer(mapKeys=mapKeys)
self.xasObjViewer._spectrumViews[0]._plot.getXAxis().setLabel("Energy (eV)")
self.setCentralWidget(self.xasObjViewer)
self._parametersWindow = _PreEdgeParameters(parent=self)
self._parametersWindow = _ParameterWindowContainer(parent=self,
parametersWindow=_MPreEdgeParameters)
dockWidget = qt.QDockWidget(parent=self)
# parameters window
......@@ -233,7 +192,7 @@ class PreEdgeOW(_ProcessForOrangeMixIn, OWWidget):
self._startProcess()
# setup the normalization process
if _USE_THREAD:
if _USE_THREAD is True:
# larch is using sqlite3 for database. access to the database
# require a 'connection' and 'cursor'. theoretically we should be
# able to access it from a single thread using something like:
......@@ -272,7 +231,6 @@ class PreEdgeOW(_ProcessForOrangeMixIn, OWWidget):
# manage advancement
self._advancement.setAdvancement(0)
self._advancement.setMaxSpectrum(self._latest_xas_obj.n_spectrum)
self._advancement.increaseAdvancement(1)
process_obj = xas.core.process.larch.pre_edge.Larch_pre_edge()
process_obj.advancement = self._advancement
process_obj.setProperties({
......
......@@ -46,7 +46,7 @@ from xas.gui.XasObjectViewer import XasObjectViewer, ViewType, _plot_chir_mag,\
from xas.gui.larch.xftf import _MXFTFParameters
from orangecontrib.xas.progress import QProgress
from orangecontrib.xas.utils import Converter
from .container import _ParameterWindowContainer
from orangecontrib.xas.widgets.container import _ParameterWindowContainer
_logger = logging.getLogger(__file__)
......@@ -122,7 +122,7 @@ class XFTFWindow(qt.QMainWindow):
return len(self.xasObjViewer._spectrumViews[0]._plot.getAllCurves())
def setKWeight(self, kweight):
self._parametersWindow.setKWeight(kweight)
self._parametersWindow._mainwidget.setKWeight(kweight)
class XFTFOW(_ProcessForOrangeMixIn, OWWidget):
......@@ -172,6 +172,11 @@ class XFTFOW(_ProcessForOrangeMixIn, OWWidget):
# connect signals / slots
self._window._parametersWindow.sigChanged.connect(self._updateProcess)
# required to display advancement
if _USE_THREAD is False:
self._advancement = QProgress('xftf')
self._advancement.sigProgress.connect(self._setProgressValue)
def _updateProcess(self):
self._update_settings()
if self._latest_xas_obj:
......@@ -200,31 +205,40 @@ class XFTFOW(_ProcessForOrangeMixIn, OWWidget):
kweight = xas_obj.configuration['SET_KWEIGHT']
if kweight is not None:
self._window.setKWeight(kweight)
self._window._parametersWindow._kWeightSB.setEnabled(False)
self._window._parametersWindow._mainwidget._kWeightSB.setEnabled(False)
else:
self._window._parametersWindow._kWeightSB.setEnabled(True)
self._window._parametersWindow._mainwidget._kWeightSB.setEnabled(True)
self._latest_xas_obj = xas_obj.copy(create_h5_file=True)
self._startProcess()
# setup the normalization process
if _USE_THREAD is False:
process_obj = xas.core.process.larch.xftf.Larch_xftf()
self._callback_finish = None
process_obj.process(self._latest_xas_obj)
self._endProcess(self._latest_xas_obj)
else:
if _USE_THREAD is True:
process_obj = QLarch_xftf()
process_obj._advancement.sigProgress.connect(self._setProgressValue)
process_obj.setProperties({
'_larchSettings': self._window._parametersWindow.getParameters()})
'_larchSettings': self._window._mainwidget._parametersWindow.getParameters()})
# update the processing thread
thread = self.getProcessingThread()
thread.init(process_obj=process_obj, xas_obj=self._latest_xas_obj)
self._callback_finish = functools.partial(self._endProcess,
self._latest_xas_obj)
self._callback_finish = functools.partial(self._endProcess, self._latest_xas_obj)
thread.finished.connect(self._callback_finish)
# start processing
thread.start(priority=qt.QThread.LowPriority)
else:
# manage advancement
self._advancement.setAdvancement(0)
self._advancement.setMaxSpectrum(self._latest_xas_obj.n_spectrum)
process_obj = xas.core.process.larch.xftf.Larch_xftf()
process_obj.advancement = self._advancement
process_obj.setProperties({
'_larchSettings': self._window._parametersWindow.getParameters()})
# hack (reason explained in the thread part)
# to avoid gui freeze, processevents after each spectrum process
process_obj.addCallback(qt.QApplication.instance().processEvents)
process_obj.addCallback(self._advancement.increaseAdvancement)
process_obj.process(self._latest_xas_obj)
self._callback_finish = None
self._endProcess(self._latest_xas_obj)
class QLarch_xftf(xas.core.process.larch.xftf.Larch_xftf):
......
......@@ -78,7 +78,10 @@ def process_spectr_autobk(spectrum, configuration, overwrite=True,
_spectrum = spectrum
else:
_spectrum = Spectrum().load_frm_dict(spectrum.to_dict())
autobk(_spectrum, group=_spectrum, **opts)
try:
autobk(_spectrum, group=_spectrum, **opts)
except ValueError as e:
_logger.error(e)
if callbacks:
for callback in callbacks:
callback()
......
......@@ -396,7 +396,7 @@ def _plot_norm(obj, **kwargs):
return
assert len(obj.energy) == len(obj.normalized_mu)
return _CurveOperation(x=obj.energy, y=obj.normalized_mu, legend='norm',
color='red')
color='black')
def _plot_norm_area(obj, **kwargs):
......@@ -485,7 +485,7 @@ def _plot_fpp(obj, **kwargs):
_logger.error('fpp has not been computed yet')
return
return _CurveOperation(x=obj.energy, y=obj.fpp, legend='fpp',
color='red')
color='blue')
def _plot_f2(obj, **kwargs):
......
......@@ -78,7 +78,6 @@ class _OptionalQDoubleSpinBox(qt.QWidget):
def _valueChanged(self, *arg, **kwargs):
if self._lastValue != self.getValue():
self._lastValue = self.getValue()
print(self._lastValue)
self.sigChanged.emit()
......
Supports Markdown
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