Commit 30724e83 authored by payno's avatar payno

[gui] add simple widget to select a spectrum from .dat or urls from .hdf5 for creating a XASObject

parent 2cb99228
# 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__ = "03/07/2019"
from silx.gui.dialog.DataFileDialog import DataFileDialog
from silx.gui import qt
import os
import logging
from xas.io import read_pymca_xas_from_file, InputType
_logger = logging.getLogger(__name__)
class _InputType(qt.QWidget):
def __init__(self, parent=None):
qt.QWidget.__init__(self, parent)
self.setLayout(qt.QHBoxLayout())
self.layout().addWidget(qt.QLabel('input type:'))
self._inputTypeCB = qt.QComboBox(parent=self)
for input_type in InputType.values():
self._inputTypeCB.addItem(input_type)
self.layout().addWidget(self._inputTypeCB)
# expose API
self.currentChanged = self._inputTypeCB.currentIndexChanged
def getCurrentType(self):
"""Return the current input type"""
return InputType.from_value(self._inputTypeCB.currentText())
class XASObjectDialog(qt.QWidget):
"""
Interface used to select inputs for defining a XASObject
"""
def __init__(self, parent=None):
qt.QWidget.__init__(self, parent)
self.setLayout(qt.QGridLayout())
spacer = qt.QWidget(parent=self)
spacer.setSizePolicy(qt.QSizePolicy.Expanding, qt.QSizePolicy.Minimum)
self.layout().addWidget(spacer, 0, 0)
self._inputType = _InputType(parent=self)
self.layout().addWidget(self._inputType, 0, 1)
self._datDialog = _XASObjectFrmDat(parent=self)
self.layout().addWidget(self._datDialog, 1, 0, 1, 2)
self._h5Dialog = _XASObjFrmH5(parent=self)
self.layout().addWidget(self._h5Dialog, 2, 0, 1, 2)
spacer = qt.QWidget(parent=self)
spacer.setSizePolicy(qt.QSizePolicy.Minimum, qt.QSizePolicy.Expanding)
self.layout().addWidget(spacer, 3, 0)
# connect signal / slot
self._inputType.currentChanged.connect(self._updateWidgetVisibility)
# expose API
# default setting
self._updateWidgetVisibility()
def _updateWidgetVisibility(self):
self._datDialog.setVisible(self._inputType.getCurrentType() == InputType.single_spectrum)
self._h5Dialog.setVisible(self._inputType.getCurrentType() == InputType.multiple_spectra)
class _XASObjectFrmDat(qt.QWidget):
"""
Interface used to define a XAS object from a single .dat file
"""
def __init__(self, parent=None):
qt.QWidget.__init__(self, parent)
self.setLayout(qt.QGridLayout())
self.layout().addWidget(qt.QLabel('file', parent=self), 0, 0)
self._inputLe = qt.QLineEdit('', parent=self)
self.layout().addWidget(self._inputLe, 0, 1)
self._selectPB = qt.QPushButton('select', parent=self)
self.layout().addWidget(self._selectPB, 0, 2)
self._startPB = qt.QPushButton('restart', parent=self)
self.layout().addWidget(self._startPB, 1, 1, 1, 2)
spacer = qt.QWidget(parent=self)
spacer.setSizePolicy(qt.QSizePolicy.Minimum, qt.QSizePolicy.Expanding)
self.layout().addWidget(spacer, 2, 0)
# signal / slot connection
self._selectPB.pressed.connect(self._selectFile)
self._inputLe.editingFinished.connect(self._processXASObject)
self._startPB.pressed.connect(self._processXASObject)
# expose api
self.restart = self._processXASObject
def _selectFile(self, *args, **kwargs):
old = self.blockSignals(True)
dialog = qt.QFileDialog(self)
dialog.setFileMode(qt.QFileDialog.ExistingFile)
dialog.setNameFilters(["spectra (*.dat)", ])
if not dialog.exec_():
dialog.close()
return
fileSelected = dialog.selectedFiles()
if len(fileSelected) == 0:
return
else:
assert len(fileSelected) == 1
self.setFileSelected(fileSelected[0])
self.blockSignals(old)
def setFileSelected(self, file_path):
self._inputLe.setText(file_path)
def _processXASObject(self, *args, **kwargs):
self._input_file_setting = self._inputLe.text()
if not os.path.isfile(self._input_file_setting):
mess = self._input_file_setting + 'is not a valid file path'
_logger.warning(mess)
return
else:
try:
xas_obj = read_pymca_xas_from_file(file_path=self._input_file_setting)
except ... as e:
_logger.error(e)
else:
return xas_obj
class _URLSelector(qt.QWidget):
def __init__(self, parent, name, layout=None, position=None):
qt.QWidget.__init__(self, parent)
self.name = name
if layout is None:
layout = self.setLayout(qt.QGridLayout())
position = (0, 0)
layout.addWidget(qt.QLabel(name + ':', parent=self), position[0], position[1])
self._qLineEdit = qt.QLineEdit('', parent=self)
layout.addWidget(self._qLineEdit, position[0], position[1] + 1)
self._qPushButton = qt.QPushButton('select', parent=self)
layout.addWidget(self._qPushButton, position[0], position[1] + 2)
# expose API
self.getUrlPath = self._qLineEdit.text
# connect signal / slot
self._qPushButton.clicked.connect(self._selectFile)
def _selectFile(self, *args, **kwargs):
dialog = DataFileDialog(self)
# dialog.setNameFilters(["".join((self.name, "(*.h5 *.hdf *.hdf5)")),])
if not dialog.exec_():
dialog.close()
return None
if dialog.selectedUrl() is not None:
self._qLineEdit.setText(dialog.selectedUrl())
class _XASObjFrmH5(qt.QWidget):
"""
Interface used to define a XAS object from h5 files and data path
"""
def __init__(self, parent=None):
qt.QWidget.__init__(self, parent)
self.setLayout(qt.QGridLayout())
# spectra url
self._spectraSelector = _URLSelector(parent=self, name='spectra url',
layout=self.layout(), position=(0, 0))
# energy / channel url
self._energySelector = _URLSelector(parent=self, name='energy /channel url',
layout=self.layout(), position=(1, 0))
# configuration url
self._configSelector = _URLSelector(parent=self, name='configuration url',
layout=self.layout(),
position=(2, 0))
if __name__ == '__main__':
app = qt.QApplication([])
dialog = XASObjectDialog()
dialog.show()
app.exec_()
......@@ -33,6 +33,7 @@ from silx.io import utils
from silx.io.url import DataUrl
from xas.core.utils.pymca import read_spectrum
from xas.core.types import XASObject
from silx.utils.enum import Enum
_logger = logging.getLogger(__name__)
......@@ -44,6 +45,11 @@ DEFAULT_CHANNEL_PATH = '/data/NXdata/Channel'
DEFAULT_CONF_PATH = '/configuration'
class InputType(Enum):
single_spectrum = '*.dat'
multiple_spectra = '*.h5'
def read_pymca_xas_from_file(file_path):
"""
......
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