Commit d9e55281 authored by payno's avatar payno
Browse files

[gui][XASObjectViewer] move all plot function in the XasObjectViewer.py and...

[gui][XASObjectViewer] move all plot function in the XasObjectViewer.py and for some function add a "mean", "std" display if possible
parent fccf9413
......@@ -40,8 +40,9 @@ from silx.gui.plot import LegendSelector
import xas.core.process.larch.autobk
from orangecontrib.xas.process import _ProcessForOrangeMixIn
from orangecontrib.xas.process import ProcessRunnable
from xas.core.types import XASObject, Spectrum
from xas.gui.XasObjectViewer import XasObjectViewer, _CurveOperation, ViewType
from xas.core.types import XASObject
from xas.gui.XasObjectViewer import XasObjectViewer, ViewType
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
......@@ -51,31 +52,6 @@ _logger = logging.getLogger(__file__)
_USE_THREAD = True
def _plot_spectrum(spectrum):
assert isinstance(spectrum, Spectrum)
return _CurveOperation(x=spectrum.energy, y=spectrum.mu, legend='spectrum',
yaxis=None, color='blue')
def _plot_bkg(spectrum):
assert isinstance(spectrum, Spectrum)
if not hasattr(spectrum, 'bkg'):
_logger.error('missing bkg parameter, unable to compute bkg plot')
return
return _CurveOperation(x=spectrum.energy, y=spectrum.bkg, legend='bkg',
yaxis=None, color='red')
def _plot_knots(spectrum):
assert isinstance(spectrum, Spectrum)
if not hasattr(spectrum, 'autobk_details'):
_logger.error('missing bkg parameter, unable to compute bkg plot')
return
return _CurveOperation(x=spectrum.autobk_details.knots_e,
y=spectrum.autobk_details.knots_y, legend='knots',
yaxis=None, color='green', linestyle="", symbol="o")
class AutobkWindow(qt.QMainWindow):
def __init__(self, parent=None):
qt.QMainWindow.__init__(self, parent)
......
......@@ -40,8 +40,9 @@ from silx.gui.plot import LegendSelector
import xas.core.process.larch.xftf
from orangecontrib.xas.process import _ProcessForOrangeMixIn
from orangecontrib.xas.process import ProcessRunnable
from xas.core.types import XASObject, Spectrum
from xas.gui.XasObjectViewer import XasObjectViewer, _CurveOperation, ViewType
from xas.core.types import XASObject
from xas.gui.XasObjectViewer import XasObjectViewer, ViewType, _plot_chir_mag,\
_plot_chir_re, _plot_chir_imag
from xas.gui.larch.xftf import _MXFTFParameters
from orangecontrib.xas.progress import QProgress
from orangecontrib.xas.utils import Converter
......@@ -69,37 +70,6 @@ def _get_title(dgroup, title=None):
return repr(dgroup)
def _plot_chir_mag(spectrum):
if not hasattr(spectrum, 'r'):
_logger.error('r not computed, unable to display it')
return
if not hasattr(spectrum, 'chir_mag'):
_logger.error('chir_mag not computed, unable to display it')
return
return _CurveOperation(x=spectrum.r, y=spectrum.chir_mag, legend='chi k (mag)')
def _plot_chir_re(spectrum):
if not hasattr(spectrum, 'r'):
_logger.error('r not computed, unable to display it')
return
if not hasattr(spectrum, 'chir_re'):
_logger.error('chir_re not computed, unable to display it')
return
return _CurveOperation(x=spectrum.r, y=spectrum.chir_re,
legend='chi k (real)')
def _plot_chir_imag(spectrum):
if not hasattr(spectrum, 'r'):
_logger.error('r not computed, unable to display it')
return
if not hasattr(spectrum, 'chir_im'):
_logger.error('chir_im not computed, unable to display it')
return
return _CurveOperation(x=spectrum.r, y=spectrum.chir_im,
legend='chi k (imag)')
class XFTFWindow(qt.QMainWindow):
def __init__(self, parent=None):
qt.QMainWindow.__init__(self, parent)
......
......@@ -45,8 +45,8 @@ from silx.gui.plot import LegendSelector
import xas.core.process.pymca.exafs
from orangecontrib.xas.process import _ProcessForOrangeMixIn, \
ProcessRunnable
from xas.core.types import Spectrum
from xas.gui.XasObjectViewer import XasObjectViewer, _CurveOperation, ViewType
from xas.gui.XasObjectViewer import XasObjectViewer, ViewType
from xas.gui.XasObjectViewer import _exafs_signal_plot, _exafs_postedge_plot, _exafs_knots_plot
from orangecontrib.xas.progress import QProgress
from orangecontrib.xas.utils import Converter
......@@ -54,56 +54,6 @@ from orangecontrib.xas.utils import Converter
_logger = logging.getLogger(__file__)
def _exafs_signal_plot(spectrum):
assert isinstance(spectrum, Spectrum)
missing_keys = spectrum.get_missing_keys(('EXAFSKValues', 'EXAFSSignal'))
if missing_keys:
_logger.error('missing keys:', missing_keys, 'unable to compute exafs signal plot')
return
k = spectrum["EXAFSKValues"]
if "KMin" not in spectrum:
spectrum["KMin"] = k.min()
if "KMax" not in spectrum:
spectrum["KMax"] = k.max()
idx = (spectrum["EXAFSKValues"] >= spectrum["KMin"]) & \
(spectrum["EXAFSKValues"] <= spectrum["KMax"])
x = spectrum["EXAFSKValues"][idx]
y = spectrum["EXAFSSignal"][idx]
return _CurveOperation(x=x, y=y, legend="EXAFSSignal")
def _exafs_postedge_plot(spectrum):
assert isinstance(spectrum, Spectrum)
missing_keys = spectrum.get_missing_keys(('EXAFSKValues', 'PostEdgeB'))
if missing_keys:
_logger.error('missing keys:', missing_keys, 'unable to compute exafs postedge plot')
return
k = spectrum["EXAFSKValues"]
if "KMin" not in spectrum:
spectrum["KMin"] = k.min()
if "KMax" not in spectrum:
spectrum["KMax"] = k.max()
idx = (spectrum["EXAFSKValues"] >= spectrum["KMin"]) & \
(spectrum["EXAFSKValues"] <= spectrum["KMax"])
x = spectrum["EXAFSKValues"][idx]
y = spectrum["PostEdgeB"][idx]
return _CurveOperation(x=x, y=y, legend="PostEdge")
def _exafs_knots_plot(spectrum):
assert isinstance(spectrum, Spectrum)
missing_keys = spectrum.get_missing_keys(('KnotsX', 'KnotsY'))
if missing_keys:
_logger.error('missing keys:', missing_keys, 'unable to compute exafs knot plot')
return
x = spectrum["KnotsX"]
y = spectrum["KnotsY"]
return _CurveOperation(x=x, y=y, legend="Knots", linestyle="", symbol="o")
class ExafsWindow(qt.QMainWindow):
def __init__(self, parent=None):
qt.QMainWindow.__init__(self, parent)
......
......@@ -39,10 +39,21 @@ from silx.gui.widgets.FrameBrowser import HorizontalSliderWithBrowser
from xas.gui import icons
from silx.gui import icons as silx_icons
from silx.gui.colors import Colormap
import numpy
import silx
import logging
_logger = logging.getLogger(__name__)
# median spectrum view
silx_version = silx.version.split('.')
if not (int(silx_version[0]) == 0 and int(silx_version[1]) <= 11):
silx_plot_has_baseline_feature = True
else:
silx_plot_has_baseline_feature = False
_logger.warning('a more recent of silx is required to display '
'mean spectrum (0.12)')
class ViewType(Enum):
map = 0,
......@@ -59,6 +70,9 @@ class _SpectrumViewAction(qt.QAction):
icon = "item-1dim"
elif iView == 1:
icon = "item-1dim-black"
else:
# if necessary: add more icons, this is the only limitation
raise NotImplementedError('Only two spectrum views are maanged')
spectrum_icon = icons.getQIcon(icon)
self.setIcon(spectrum_icon)
self.setCheckable(True)
......@@ -105,7 +119,6 @@ class XasObjectViewer(qt.QMainWindow):
spectrumView = SpectrumViewer(parent=self)
self._mainWidget.layout().addWidget(spectrumView)
self._spectrumViews.append(spectrumView)
# add toolbar
toolbar = qt.QToolBar('')
toolbar.setIconSize(qt.QSize(32, 32))
......@@ -231,7 +244,7 @@ class _ExtendedSliderWithBrowser(HorizontalSliderWithBrowser):
class _CurveOperation(object):
def __init__(self, x, y, legend, yaxis=None, linestyle=None, symbol=None,
color=None, ylabel=None):
color=None, ylabel=None, baseline=None, alpha=1.0):
self.x = x
self.y = y
self.legend = legend
......@@ -240,6 +253,8 @@ class _CurveOperation(object):
self.symbol = symbol
self.color = color
self.ylabel = ylabel
self.baseline = baseline
self.alpha = alpha
class _XMarkerOperation(object):
......@@ -282,9 +297,17 @@ class SpectrumViewer(qt.QMainWindow):
self._dim1FrameBrowser.valueChanged.connect(self._updateSpectrumDisplayed)
self._dim2FrameBrowser.valueChanged.connect(self._updateSpectrumDisplayed)
def addCurveOperation(self, callback):
"""register an curve to display from Xasobject keys, and a legend"""
self._curveOperations.append(callback)
def addCurveOperation(self, callbacks):
"""register an curve to display from Xasobject keys, and a legend
:param callbacks: callback to call when displaying a specific curve
:type: Union[list,tuple,function]
"""
if isinstance(callbacks, (list, tuple)):
for callback in callbacks:
self.addCurveOperation(callback)
else:
self._curveOperations.append(callbacks)
def clearCurveOperations(self):
"""Remove all defined curve operation"""
......@@ -297,9 +320,6 @@ class SpectrumViewer(qt.QMainWindow):
self._dim1FrameBrowser.setRange(0, self.xas_obj.dim1-1)
self._dim2FrameBrowser.setRange(0, self.xas_obj.dim2-1)
def clear(self):
self._plot.clear()
def _updateSpectrumDisplayed(self, *args, **kwargs):
if self.xas_obj is None:
return
......@@ -313,25 +333,33 @@ class SpectrumViewer(qt.QMainWindow):
spectrum = self.xas_obj.getSpectrum(dim1_index, dim2_index)
for operation in self._curveOperations:
res = operation(spectrum)
if res is not None and res.x is not None:
if isinstance(res, _CurveOperation):
self._plot.addCurve(x=res.x,
y=res.y,
legend=res.legend,
yaxis=res.yaxis,
linestyle=res.linestyle,
symbol=res.symbol,
color=res.color,
ylabel=res.ylabel,
)
elif isinstance(res, _XMarkerOperation):
self._plot.addXMarker(x=res.x,
color=res.color,
legend=res.legend)
else:
raise TypeError('this type of operation is not recognized'
'', type(res))
curves = [operation(spectrum),]
if silx_plot_has_baseline_feature is True:
new_curves_op = operation(self.xas_obj, index=dim1_index)
if new_curves_op is not None:
curves += new_curves_op
for res in curves:
if res is not None and res.x is not None:
if isinstance(res, _CurveOperation):
curve = self._plot.addCurve(x=res.x,
y=res.y,
legend=res.legend,
yaxis=res.yaxis,
linestyle=res.linestyle,
symbol=res.symbol,
color=res.color,
ylabel=res.ylabel,
baseline=res.baseline,
)
curve = self._plot.getCurve(curve)
curve.setAlpha(res.alpha)
elif isinstance(res, _XMarkerOperation):
self._plot.addXMarker(x=res.x,
color=res.color,
legend=res.legend)
else:
raise TypeError('this type of operation is not '
'recognized', type(res))
def clear(self):
self._plot.clear()
......@@ -339,86 +367,251 @@ class SpectrumViewer(qt.QMainWindow):
self._dim2FrameBrowser.setMaximum(-1)
def _plot_norm(spectrum):
assert isinstance(spectrum, Spectrum)
assert spectrum.normalized_mu is not None
if spectrum.normalized_mu is None:
_logger.error('norm has not been computed yet')
return
assert len(spectrum.energy) == len(spectrum.normalized_mu)
return _CurveOperation(x=spectrum.energy, y=spectrum.normalized_mu, legend='norm',
color='red')
COLOR_MEAN = 'black'
COLOR_STD = 'grey'
def _plot_norm(obj, **kwargs):
if isinstance(obj, XASObject):
assert 'index' in kwargs
index_dim1 = kwargs['index']
spectra = XASObject._spectra_volume(obj.spectra,
'normalized_mu',
obj.dim1,
obj.dim2,
relative_to='energy')
spectra = spectra[:, index_dim1, :]
mean = numpy.mean(spectra, axis=1)
std = numpy.std(spectra, axis=1)
return (
_CurveOperation(x=obj.energy, y=mean, color=COLOR_MEAN, legend='mean norm',
alpha=0.5),
_CurveOperation(x=obj.energy, y=mean+std, baseline=mean-std, color=COLOR_STD,
legend='std norm', alpha=0.5))
elif isinstance(obj, Spectrum):
assert obj.normalized_mu is not None
if obj.normalized_mu is None:
_logger.error('norm has not been computed yet')
return
assert len(obj.energy) == len(obj.normalized_mu)
return _CurveOperation(x=obj.energy, y=obj.normalized_mu, legend='norm',
color='red')
def _plot_norm_area(spectrum):
assert isinstance(spectrum, Spectrum)
if not hasattr(spectrum, 'norm_area'):
def _plot_norm_area(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'norm_area'):
_logger.error('norm_area has not been computed yet')
return
assert len(spectrum.energy) == len(spectrum.norm_area)
return _CurveOperation(x=spectrum.energy, y=spectrum.norm_area,
assert len(obj.energy) == len(obj.norm_area)
return _CurveOperation(x=obj.energy, y=obj.norm_area,
legend='norm_area', color='orange')
def _plot_mback_mu(spectrum):
assert isinstance(spectrum, Spectrum)
if not hasattr(spectrum, 'mback_mu'):
def _plot_mback_mu(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'mback_mu'):
_logger.error('mback_mu has not been computed yet')
return
return _CurveOperation(x=spectrum.energy, y=spectrum.mback_mu,
return _CurveOperation(x=obj.energy, y=obj.mback_mu,
legend='mback_mu', color='orange')
def _plot_pre_edge(spectrum):
assert isinstance(spectrum, Spectrum)
if spectrum.pre_edge is None:
def _plot_pre_edge(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if obj.pre_edge is None:
_logger.error('pre_edge has not been computed yet')
return
assert len(spectrum.energy) == len(spectrum.pre_edge)
return _CurveOperation(x=spectrum.energy, y=spectrum.pre_edge,
assert len(obj.energy) == len(obj.pre_edge)
return _CurveOperation(x=obj.energy, y=obj.pre_edge,
legend='pre edge', color='green')
def _plot_post_edge(spectrum):
assert isinstance(spectrum, Spectrum)
if spectrum.post_edge is None:
def _plot_post_edge(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if obj.post_edge is None:
_logger.error('post_edge has not been computed yet')
return
assert len(spectrum.energy) == len(spectrum.post_edge)
return _CurveOperation(x=spectrum.energy, y=spectrum.post_edge,
assert len(obj.energy) == len(obj.post_edge)
return _CurveOperation(x=obj.energy, y=obj.post_edge,
legend='post edge', color='black')
def _plot_edge(spectrum):
assert isinstance(spectrum, Spectrum)
if not hasattr(spectrum, 'e0'):
def _plot_edge(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'e0'):
_logger.error('e0 has not been computed yet')
return
return _XMarkerOperation(x=spectrum.e0, legend='edge', color='yellow')
def _plot_raw(spectrum):
assert isinstance(spectrum, Spectrum)
if spectrum.mu is None:
_logger.error('mu not existing')
return
return _CurveOperation(x=spectrum.energy, y=spectrum.mu,
legend='raw spectrum', color='grey')
return _XMarkerOperation(x=obj.e0, legend='edge', color='yellow')
def _plot_fpp(spectrum):
assert isinstance(spectrum, Spectrum)
if not hasattr(spectrum, 'fpp'):
def _plot_raw(obj, **kwargs):
if isinstance(obj, Spectrum):
if obj.mu is None:
_logger.error('mu not existing')
return
return _CurveOperation(x=obj.energy, y=obj.mu,
legend='mu', color='red')
elif isinstance(obj, XASObject):
assert 'index' in kwargs
index_dim1 = kwargs['index']
spectra = XASObject._spectra_volume(obj.spectra,
'normalized_mu',
obj.dim1,
obj.dim2,
relative_to='energy')
spectra = spectra[:, index_dim1, :]
mean = numpy.mean(spectra, axis=1)
std = numpy.std(spectra, axis=1)
return (
_CurveOperation(x=obj.energy, y=mean, color=COLOR_MEAN, legend='mean mu',
alpha=0.5),
_CurveOperation(x=obj.energy, y=mean+std, baseline=mean-std, color=COLOR_STD,
legend='mu std', alpha=0.5))
else:
raise ValueError('input type is not manged')
def _plot_fpp(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'fpp'):
_logger.error('fpp has not been computed yet')
return
return _CurveOperation(x=spectrum.energy, y=spectrum.fpp, legend='fpp',
return _CurveOperation(x=obj.energy, y=obj.fpp, legend='fpp',
color='red')
def _plot_f2(spectrum):
assert isinstance(spectrum, Spectrum)
if not hasattr(spectrum, 'f2'):
def _plot_f2(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'f2'):
_logger.error('f2 has not been computed yet')
return
return _CurveOperation(x=spectrum.energy, y=spectrum.f2, legend='f2',
return _CurveOperation(x=obj.energy, y=obj.f2, legend='f2',
color='orange')
def _plot_chir_mag(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'r'):
_logger.error('r not computed, unable to display it')
return
if not hasattr(obj, 'chir_mag'):
_logger.error('chir_mag not computed, unable to display it')
return
return _CurveOperation(x=obj.r, y=obj.chir_mag, legend='chi k (mag)')
def _plot_chir_re(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'r'):
_logger.error('r not computed, unable to display it')
return
if not hasattr(obj, 'chir_re'):
_logger.error('chir_re not computed, unable to display it')
return
return _CurveOperation(x=obj.r, y=obj.chir_re,
legend='chi k (real)')
def _plot_chir_imag(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'r'):
_logger.error('r not computed, unable to display it')
return
if not hasattr(obj, 'chir_im'):
_logger.error('chir_im not computed, unable to display it')
return
return _CurveOperation(x=obj.r, y=obj.chir_im,
legend='chi k (imag)')
def _plot_spectrum(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
return _CurveOperation(x=obj.energy, y=obj.mu, legend='spectrum',
yaxis=None, color='blue')
def _plot_bkg(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'bkg'):
_logger.error('missing bkg parameter, unable to compute bkg plot')
return
return _CurveOperation(x=obj.energy, y=obj.bkg, legend='bkg',
yaxis=None, color='red')
def _plot_knots(obj, **kwargs):
if not isinstance(obj, Spectrum):
return None
if not hasattr(obj, 'autobk_details'):
_logger.error('missing bkg parameter, unable to compute bkg plot')
return
return _CurveOperation(x=obj.autobk_details.knots_e,
y=obj.autobk_details.knots_y, legend='knots',
yaxis=None, color='green', linestyle="", symbol="o")
def _exafs_signal_plot(spectrum, **kwargs):
if not isinstance(spectrum, Spectrum):
return None
missing_keys = spectrum.get_missing_keys(('EXAFSKValues', 'EXAFSSignal'))
if missing_keys:
_logger.error('missing keys:', missing_keys, 'unable to compute exafs signal plot')
return
k = spectrum["EXAFSKValues"]
if "KMin" not in spectrum:
spectrum["KMin"] = k.min()
if "KMax" not in spectrum:
spectrum["KMax"] = k.max()
idx = (spectrum["EXAFSKValues"] >= spectrum["KMin"]) & \
(spectrum["EXAFSKValues"] <= spectrum["KMax"])
x = spectrum["EXAFSKValues"][idx]
y = spectrum["EXAFSSignal"][idx]
return _CurveOperation(x=x, y=y, legend="EXAFSSignal")
def _exafs_postedge_plot(spectrum, **kwargs):
if not isinstance(spectrum, Spectrum):
return None
missing_keys = spectrum.get_missing_keys(('EXAFSKValues', 'PostEdgeB'))
if missing_keys:
_logger.error('missing keys:', missing_keys, 'unable to compute exafs postedge plot')
return
k = spectrum["EXAFSKValues"]
if "KMin" not in spectrum:
spectrum["KMin"] = k.min()
if "KMax" not in spectrum:
spectrum["KMax"] = k.max()
idx = (spectrum["EXAFSKValues"] >= spectrum["KMin"]) & \
(spectrum["EXAFSKValues"] <= spectrum["KMax"])
x = spectrum["EXAFSKValues"][idx]
y = spectrum["PostEdgeB"][idx]
return _CurveOperation(x=x, y=y, legend="PostEdge")
def _exafs_knots_plot(spectrum, **kwargs):