Commit 03d83988 authored by payno's avatar payno

[gui] add first brick for the XASObject viewer.

Enable to display a map or the spectrum
Spectrums are only displyed with energy and mu at the moment but should evolve with time.
Maybe we would like to do the same for the map ???
parent 35f81d24
......@@ -83,6 +83,13 @@ class XASObject(object):
self.addSpectrum(spectrum)
self.energy = energy
def getSpectrum(self, dim1_idx, dim2_idx):
"""Util function to access the spectrum at dim1_idx, dim2_idx"""
assert dim1_idx < self.dim1
assert dim2_idx < self.dim2
global_idx = dim1_idx * self.dim2 + dim2_idx
return self.spectra[global_idx]
def addSpectrum(self, spectrum):
self.__spectra.append(spectrum)
......@@ -210,6 +217,10 @@ class XASObject(object):
return len(self.__spectra)
# TODO: add the spectra class. Would speed up and simplify stuff probably
class Spectra(object):
pass
class Spectrum(object):
_MU_KEY = 'Mu'
......
# 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.
#
# ###########################################################################*/
"""Tools to visualize spectra"""
__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "04/07/2019"
from xas.core.types import XASObject
from xas.io import read_pymca_xas
from silx.gui import qt
from silx.gui.plot import Plot1D
from silx.gui.plot.StackView import StackViewMainWindow
from silx.utils.enum import Enum
from silx.gui.widgets.FrameBrowser import HorizontalSliderWithBrowser
from silx.gui import icons as silx_icons
class ViewType(Enum):
map = 0,
spectrum = 1,
class _SpectrumViewAction(qt.QAction):
def __init__(self, parent=None):
qt.QAction.__init__(self, 'spectrum view', parent=parent)
spectrum_icon = silx_icons.getQIcon("item-1dim")
self.setIcon(spectrum_icon)
self.setCheckable(True)
class _MapViewAction(qt.QAction):
def __init__(self, parent=None):
qt.QAction.__init__(self, 'map view', parent=parent)
map_icon = silx_icons.getQIcon("image")
self.setIcon(map_icon)
self.setCheckable(True)
class XasObjectViewer(qt.QMainWindow):
"""Viewer dedicated to view a XAS object"""
def __init__(self, parent=None):
qt.QMainWindow.__init__(self, parent)
self.setWindowFlags(qt.Qt.Widget)
# main stack widget
self._mainWidget = qt.QWidget(parent=self)
self._mainWidget.setLayout(qt.QVBoxLayout())
self.setCentralWidget(self._mainWidget)
# map view
self._mapView = MapViewer(parent=self)
self._mainWidget.layout().addWidget(self._mapView)
# spectrum view
self._spectrumView = SpectrumViewer(parent=self)
self._mainWidget.layout().addWidget(self._spectrumView)
# add toolbar
toolbar = qt.QToolBar('')
toolbar.setIconSize(qt.QSize(32, 32))
self.view_actions = qt.QActionGroup(self)
self._spectrumViewAction = _SpectrumViewAction()
self.view_actions.addAction(self._spectrumViewAction)
self._mapViewAction = _MapViewAction()
self.view_actions.addAction(self._mapViewAction)
toolbar.addAction(self._spectrumViewAction)
toolbar.addAction(self._mapViewAction)
self.addToolBar(qt.Qt.LeftToolBarArea, toolbar)
toolbar.setMovable(False)
# connect signal / Slot
self._mapViewAction.triggered.connect(self._updateView)
self._spectrumViewAction.triggered.connect(self._updateView)
# initialize
self._spectrumViewAction.setChecked(True)
self._updateView()
def _updateView(self, *arg, **kwargs):
self._mapView.setVisible(self.getViewType() is ViewType.map)
self._spectrumView.setVisible(self.getViewType() is ViewType.spectrum)
def getViewType(self):
if self._mapViewAction.isChecked():
return ViewType.map
elif self._spectrumViewAction.isChecked():
return ViewType.spectrum
def setXASObj(self, xas_obj):
self._mapView.clear()
self._spectrumView.clear()
# set the map view
spectra_volume = XASObject._spectra_volume(xas_obj.spectra,
xas_obj.dim1,
xas_obj.dim2)
self._mapView.setStack(spectra_volume)
# set spectrum view
self._spectrumView.setXasObject(xas_obj)
class MapViewer(StackViewMainWindow):
pass
class _ExtendedSliderWithBrowser(HorizontalSliderWithBrowser):
def __init__(self, parent=None, name=None):
HorizontalSliderWithBrowser.__init__(self, parent)
self.layout().insertWidget(0, qt.QLabel(str(name + ':')))
class SpectrumViewer(qt.QMainWindow):
def __init__(self, parent=None):
qt.QMainWindow.__init__(self, parent)
self.xas_obj = None
self._plot = Plot1D(parent=self)
self.setCentralWidget(self._plot)
dockWidget = qt.QDockWidget(parent=self)
frameBrowsers = qt.QWidget(parent=self)
frameBrowsers.setLayout(qt.QVBoxLayout())
frameBrowsers.layout().setContentsMargins(0, 0, 0, 0)
self._dim1FrameBrowser = _ExtendedSliderWithBrowser(parent=self,
name='dim 1')
frameBrowsers.layout().addWidget(self._dim1FrameBrowser)
self._dim2FrameBrowser = _ExtendedSliderWithBrowser(parent=self,
name='dim 2')
frameBrowsers.layout().addWidget(self._dim2FrameBrowser)
dockWidget.setWidget(frameBrowsers)
self.addDockWidget(qt.Qt.BottomDockWidgetArea, dockWidget)
dockWidget.setAllowedAreas(qt.Qt.BottomDockWidgetArea)
dockWidget.setFeatures(qt.QDockWidget.NoDockWidgetFeatures)
# connect signal / slot
self._dim1FrameBrowser.valueChanged.connect(self._updateSpectrumDisplayed)
self._dim2FrameBrowser.valueChanged.connect(self._updateSpectrumDisplayed)
def setXasObject(self, xas_obj):
self.xas_obj = xas_obj
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
dim1_index = self._dim1FrameBrowser.value()
dim2_index = self._dim2FrameBrowser.value()
spectrum = self.xas_obj.getSpectrum(dim1_index, dim2_index)
self._plot.addCurve(x=spectrum.energy, y=spectrum.mu)
def clear(self):
self._plot.clear()
self._dim1FrameBrowser.setMaximum(-1)
self._dim2FrameBrowser.setMaximum(-1)
if __name__ == '__main__':
from silx.io.url import DataUrl
file_name = "S5_nuit1_merge_new_crop.h5"
url_spectra = DataUrl(file_path=file_name,
data_path='/data/NXdata/data',
scheme='silx')
url_channel = DataUrl(file_path=file_name,
data_path='/data/NXdata/Channel',
scheme='silx')
xas_obj = read_pymca_xas(spectra_url=url_spectra, channel_url=url_channel)
# import numpy
# from xas.core.utils import spectra as spectra_utils
# spectra = spectra_utils.create_dataset(shape=(256, 32, 64))
# energy = numpy.linspace(start=3.25, stop=3.69, num=256)
# xas_obj = XASObject(spectra=spectra, energy=energy, configuration=None)
app = qt.QApplication([])
viewer = XasObjectViewer()
viewer.setXASObj(xas_obj=xas_obj)
viewer.show()
app.exec_()
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