Commit 0abe6418 authored by payno's avatar payno
Browse files

[XASBase] replace the xas_process_io by a XASBase and PyMcaXAS classes

parent b629e782
......@@ -32,9 +32,10 @@ from silx.gui import qt
from silx.gui.plot import Plot2D, LegendSelector
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from xas.core.types import xas_process_io
from xas.core.types import XASBase
from PyMca5.PyMcaGui.physics.xas.XASPostEdgeParameters import XASPostEdgeParameters
import xas.core.process.exafs
import copy
import logging
_logger = logging.getLogger(__file__)
......@@ -75,8 +76,8 @@ class ExafsOW(OWWidget):
want_main_area = True
resizing_enabled = True
inputs = [("spectrum", xas_process_io, "process")]
outputs = [("spectrum", xas_process_io), ("curves", tuple)]
inputs = [("spectrum", XASBase, "process")]
outputs = [("spectrum", XASBase), ("curves", tuple)]
process_function = xas.core.process.exafs.pymca_exafs
......@@ -85,32 +86,28 @@ class ExafsOW(OWWidget):
self._window = ExafsWindow()
layout = gui.vBox(self.mainArea, 'fourier transform').layout()
layout.addWidget(self._window)
self._current_process_io = None
self._latest_xas_obj = None
# signal / slot connection
_sig = self._window._pymcaWindow.sigPostEdgeParametersSignal
_sig.connect(self._updateProcess)
def _updateProcess(self):
if self._current_process_io:
self.process(self._current_process_io)
if self._latest_xas_obj:
self.process(self._latest_xas_obj)
def process(self, process_io):
if process_io is None:
def process(self, xas_obj):
if xas_obj is None:
return
self._current_process_io = process_io
config = process_io.configuration
config['EXAFS'] = self._window._pymcaWindow.getParameters()
process_io = xas_process_io(spectrum=process_io.spectrum,
configuration=config)
spectrum = ExafsOW.process_function(process_io)
self._latest_xas_obj = copy.copy(xas_obj)
xas_obj.configuration['EXAFS'] = self._window._pymcaWindow.getParameters()
spectrum = ExafsOW.process_function(xas_obj)
self._updatePlot(spectrum)
process_io = xas_process_io(spectrum=spectrum,
configuration=process_io.configuration)
xas_obj.spectrum = spectrum
# emit signal for the plot
self.send("spectrum", process_io)
self.send("spectrum", xas_obj)
def _updatePlot(self, ddict):
plot = self._window.plot
......
......@@ -32,10 +32,11 @@ from silx.gui import qt
from silx.gui.plot import Plot2D, LegendSelector
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from xas.core.types import xas_process_io
from xas.core.types import XASBase
from PyMca5.PyMcaGui.physics.xas.XASFourierTransformParameters import XASFourierTransformParameters
import xas.core.process.ft
import logging
import copy
_logger = logging.getLogger(__file__)
......@@ -75,15 +76,15 @@ class FTOW(OWWidget):
want_main_area = True
resizing_enabled = True
inputs = [("spectrum", xas_process_io, "process")]
outputs = [("spectrum", xas_process_io), ("curves", tuple)]
inputs = [("spectrum", XASBase, "process")]
outputs = [("spectrum", XASBase), ("curves", tuple)]
process_function = xas.core.process.ft.pymca_ft
def __init__(self):
super().__init__()
self._window = FTWindow(parent=self)
self._current_process_io = None
self._latest_xas_obj = None
layout = gui.vBox(self.mainArea, 'fourier transform').layout()
layout.addWidget(self._window)
......@@ -91,27 +92,22 @@ class FTOW(OWWidget):
self._window._pymcaWindow.sigFTParametersSignal.connect(self._updateProcess)
def _updateProcess(self, *arv, **kwargs):
if self._current_process_io:
self.process(process_io=self._current_process_io)
if self._latest_xas_obj:
self.process(xas_obj=self._latest_xas_obj)
def process(self, process_io):
if process_io is None:
def process(self, xas_obj):
if xas_obj is None:
return
conf = process_io.configuration
conf['FT'] = self._window._pymcaWindow.getParameters()
process_io = xas_process_io(spectrum=process_io.spectrum,
configuration=conf)
self._current_process_io = process_io
self._latest_xas_obj = copy.copy(xas_obj)
xas_obj.configuration['FT'] = self._window._pymcaWindow.getParameters()
spectrum = FTOW.process_function(process_io)
spectrum = FTOW.process_function(xas_obj)
self._updatePlot(ddict=spectrum)
process_io = xas_process_io(spectrum=spectrum,
configuration=process_io.configuration)
process_io.spectrum.update(spectrum)
xas_obj.spectrum.update(spectrum)
# emit signal for the plot
self.send("spectrum", process_io)
self.send("spectrum", xas_obj)
def _updatePlot(self, ddict):
self._window.plot.clear()
......
......@@ -28,14 +28,14 @@ __license__ = "MIT"
__date__ = "06/07/2019"
from silx.gui import qt
from silx.gui.plot import Plot2D, LegendSelector
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from silx.gui import qt
from xas.core.types import xas_process_io
from xas.core.types import XASBase
import xas.core.process.k_weight
import logging
import copy
_logger = logging.getLogger(__file__)
......@@ -81,8 +81,8 @@ class KWeightOW(OWWidget):
want_main_area = True
resizing_enabled = True
inputs = [("spectrum", xas_process_io, "process")]
outputs = [("spectrum", xas_process_io), ("curves", tuple)]
inputs = [("spectrum", XASBase, "process")]
outputs = [("spectrum", XASBase), ("curves", tuple)]
process_function = xas.core.process.k_weight.pymca_k_weight
......@@ -91,32 +91,32 @@ class KWeightOW(OWWidget):
layout = gui.vBox(self.mainArea, 'fourier transform').layout()
self._window = KWeightWindow(parent=self)
layout.addWidget(self._window)
self._current_process_io = None
self._latest_xas_obj = None
# signal / slot connection
self._window._k_spin_box.valueChanged.connect(self._updateProcess)
def _updateProcess(self, *arv, **kwargs):
if self._current_process_io:
self.process(self._current_process_io)
if self._latest_xas_obj:
self.process(self._latest_xas_obj)
def process(self, process_io):
if process_io is None:
def process(self, xas_obj):
if xas_obj is None:
return
process_io.configuration['SET_KWEIGHT'] = self._window._k_spin_box.value()
self._current_process_io = process_io
process_io.configuration['KWeight'] = self._window._k_spin_box.value()
if 'FT' not in process_io.configuration:
process_io.configuration['FT'] = {}
process_io.configuration['FT']['KWeight'] = self._window._k_spin_box.value()
spectrum = KWeightOW.process_function(process_io)
self._latest_xas_obj = copy.copy(xas_obj)
xas_obj.configuration['SET_KWEIGHT'] = self._window._k_spin_box.value()
xas_obj.configuration['KWeight'] = self._window._k_spin_box.value()
if 'FT' not in xas_obj.configuration:
xas_obj.configuration['FT'] = {}
xas_obj.configuration['FT']['KWeight'] = self._window._k_spin_box.value()
spectrum = KWeightOW.process_function(xas_obj)
self._updatePlot(spectrum)
xas_obj.spectrum = spectrum
# emit signal for the plot
self.send("spectrum", xas_process_io(spectrum=process_io.spectrum,
configuration=process_io.configuration))
self.send("spectrum", xas_obj)
def _updatePlot(self, ddict):
if ddict is None:
......
......@@ -34,8 +34,9 @@ from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from PyMca5.PyMcaGui.physics.xas.XASNormalizationParameters import XASNormalizationParameters
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
from xas.core.types import xas_process_io
from xas.core.types import XASBase
import logging
import copy
import xas.core.process.normalization
_logger = logging.getLogger(__file__)
......@@ -79,15 +80,15 @@ class NormalizationOW(OWWidget):
want_main_area = True
resizing_enabled = True
inputs = [("spectrum", xas_process_io, "process")]
inputs = [("spectrum", XASBase, "process")]
outputs = [("spectrum", xas_process_io), ("curves", tuple)]
outputs = [("spectrum", XASBase), ("curves", tuple)]
process_function = xas.core.process.normalization.pymca_normalization
def __init__(self):
super().__init__()
self._current_process_io = None
self._latest_xas_obj = None
self._window = NormalizationWindow(parent=self)
layout = gui.vBox(self.mainArea, 'fourier transform').layout()
layout.addWidget(self._window)
......@@ -102,26 +103,20 @@ class NormalizationOW(OWWidget):
_sig = self._window._pymcaWindow.sigXASNormalizationParametersSignal
_sig.connect(self._updateProcess)
def process(self, process_io):
if process_io is None:
def process(self, xas_obj):
if xas_obj is None:
return
self._current_process_io = process_io
conf = process_io.configuration
conf['Normalization'] = self._window._pymcaWindow.getParameters()
process_io = xas_process_io(spectrum=process_io.spectrum,
configuration=conf)
spectrum = NormalizationOW.process_function(process_io)
self._updatePlot(spectrum)
process_io = xas_process_io(spectrum=spectrum,
configuration=process_io.configuration)
self._latest_xas_obj = copy.copy(xas_obj)
xas_obj.configuration['Normalization'] = self._window._pymcaWindow.getParameters()
xas_obj.spectrum = NormalizationOW.process_function(xas_obj)
self._updatePlot(xas_obj.spectrum)
# emit signal for the plot
self.send("spectrum", process_io)
self.send("spectrum", xas_obj)
def _updateProcess(self):
if self._current_process_io:
self.process(self._current_process_io)
if self._latest_xas_obj:
self.process(self._latest_xas_obj)
def _updatePlot(self, ddict):
self._window.plot.clear()
......
......@@ -31,7 +31,7 @@ __date__ = "06/11/2019"
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from silx.gui import qt
from xas.core.types import xas_process_io
from xas.core.types import XASBase, PyMcaXAS
from Orange.widgets.settings import Setting
from silx.io.url import DataUrl
import os
......@@ -55,11 +55,11 @@ class ExafsOW(OWWidget):
want_main_area = True
resizing_enabled = True
outputs = [("spectrum", xas_process_io)]
outputs = [("spectrum", XASBase)]
_input_file_setting = Setting(str())
process_function = xas.core.process.io.read
process_function = xas.core.process.io.read_pymca_xas
def __init__(self):
super().__init__()
......@@ -116,10 +116,9 @@ class ExafsOW(OWWidget):
return
else:
try:
spectrum = xas.core.process.io.read(DataUrl(file_path=self._input_file_setting,
scheme='PyMca'))
xas_obj = xas.core.process.io.read_pymca_xas(DataUrl(file_path=self._input_file_setting,
scheme='PyMca'))
except ... as e:
_logger.error(e)
else:
self.send("spectrum", xas_process_io(spectrum=spectrum,
configuration={}))
self.send("spectrum", xas_obj)
......@@ -32,7 +32,7 @@ from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from silx.gui import qt
from Orange.widgets.settings import Setting
from xas.core.types import xas_process_io
from xas.core.types import XASBase
import os
import logging
_logger = logging.getLogger(__file__)
......@@ -53,7 +53,7 @@ class ExafsOW(OWWidget):
want_main_area = True
resizing_enabled = True
inputs = [("spectrum", xas_process_io, 'process')]
inputs = [("spectrum", XASBase, 'process')]
_output_file_setting = Setting(str())
......@@ -107,7 +107,7 @@ class ExafsOW(OWWidget):
def _getFileSelected(self):
return self._inputLe.text()
def process(self, process_io):
def process(self, xas_obj):
if not os.path.isfile(self._getFileSelected()):
qt.QMessageBox.warning(self,
'No output file defined, please give a file path')
......
......@@ -27,6 +27,7 @@ __authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "06/11/2019"
from xas.core.types import PyMcaXAS
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
from PyMca5.PyMcaPhysics.xas.XASClass import e2k
import logging
......@@ -34,35 +35,40 @@ import logging
_logger = logging.getLogger(__name__)
def pymca_exafs(xas_process_io):
def pymca_exafs(xas_obj):
"""
:param dict configuration: should contain the
:return: configuration dict
:param xas_obj: object containing the configuration and spectrum to process
:type: Union[XASBase, dict]
:return: spectrum dict
:rtype: dict
"""
if isinstance(xas_obj, dict):
_xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
else:
_xas_obj = xas_obj
pymca_xas = XASClass()
if 'Energy' not in xas_process_io.spectrum or 'Mu' not in xas_process_io.spectrum:
if 'Energy' not in _xas_obj.spectrum or 'Mu' not in _xas_obj.spectrum:
_logger.error('Energy and or Mu is/are not specified, unable to '
'compute exafs')
return
pymca_xas.setSpectrum(energy=xas_process_io.spectrum['Energy'],
mu=xas_process_io.spectrum['Mu'])
pymca_xas.setConfiguration(xas_process_io.configuration)
xas_process_io.spectrum.update(pymca_xas.processSpectrum())
pymca_xas.setSpectrum(energy=_xas_obj.spectrum['Energy'],
mu=_xas_obj.spectrum['Mu'])
pymca_xas.setConfiguration(_xas_obj.configuration)
_xas_obj.spectrum.update(pymca_xas.processSpectrum())
if 'EXAFSKValues' in xas_process_io.spectrum and 'Mu' in xas_process_io.spectrum:
kValues = xas_process_io.spectrum['EXAFSKValues']
if 'EXAFSKValues' in _xas_obj.spectrum and 'Mu' in _xas_obj.spectrum:
kValues = _xas_obj.spectrum['EXAFSKValues']
else:
if 'Mu' not in xas_process_io.spectrum:
if 'Mu' not in _xas_obj.spectrum:
print('********************** computing **********************')
if 'Edge' not in xas_process_io.spectrum:
xas_process_io.spectrum.update(pymca_xas.processSpectrum())
e0 = xas_process_io.spectrum['Edge']
energy = xas_process_io.spectrum['Energy']
if 'Edge' not in _xas_obj.spectrum:
_xas_obj.spectrum.update(pymca_xas.processSpectrum())
e0 = _xas_obj.spectrum['Edge']
energy = _xas_obj.spectrum['Energy']
kValues = e2k(energy - e0)
xas_process_io.spectrum['EXAFSKValues'] = kValues
mu = xas_process_io.spectrum['Mu']
_xas_obj.spectrum['EXAFSKValues'] = kValues
mu = _xas_obj.spectrum['Mu']
res = pymca_xas.postEdge(k=kValues, mu=mu)
res.update(xas_process_io.spectrum)
res.update(_xas_obj.spectrum)
return res
......@@ -29,30 +29,37 @@ __date__ = "06/11/2019"
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
from PyMca5.PyMcaPhysics.xas.XASClass import e2k
from xas.core.types import PyMcaXAS
import logging
_logger = logging.getLogger(__name__)
def pymca_ft(xas_process_io):
def pymca_ft(xas_obj):
"""
:param dict configuration: should contain the
:return: configuration dict
:param xas_obj: object containing the configuration and spectrum to process
:type: Union[XASBase, dict]
:return: spectrum dict
:rtype: dict
"""
if isinstance(xas_obj, dict):
_xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
else:
_xas_obj = xas_obj
pymca_xas = XASClass()
if 'Energy' not in xas_process_io.spectrum or 'Mu' not in xas_process_io.spectrum:
if 'Energy' not in _xas_obj.spectrum or 'Mu' not in _xas_obj.spectrum:
_logger.error('Energy and or Mu is/are not specified, unable to '
'compute exafs')
return
pymca_xas.setSpectrum(energy=xas_process_io.spectrum['Energy'],
mu=xas_process_io.spectrum['Mu'])
pymca_xas.setConfiguration(xas_process_io.configuration)
pymca_xas.setSpectrum(energy=_xas_obj.spectrum['Energy'],
mu=_xas_obj.spectrum['Mu'])
pymca_xas.setConfiguration(_xas_obj.configuration)
mu = xas_process_io.spectrum['Mu']
if 'EXAFSKValues' in xas_process_io.spectrum:
kValues = xas_process_io.spectrum['EXAFSKValues']
spectrum = xas_process_io.spectrum
mu = _xas_obj.spectrum['Mu']
if 'EXAFSKValues' in _xas_obj.spectrum:
kValues = _xas_obj.spectrum['EXAFSKValues']
spectrum = _xas_obj.spectrum
else:
spectrum = pymca_xas.processSpectrum()
e0 = spectrum["Edge"]
......
......@@ -31,16 +31,18 @@ import logging
from silx.io import utils
from silx.io.url import DataUrl
from xas.core.utils.pymca import read_spectrum
from xas.core.types import PyMcaXAS
_logger = logging.getLogger(__name__)
def read(spectrum_url, config_url=None):
def read_pymca_xas(spectrum_url, config_url=None):
"""
Read the given spectrum url and the config url if any
:param Union[DataUrl, str] spectrum_url:
:param DataUrl config_url:
:return: dict
:return: PyMcaXAS
"""
if type(spectrum_url) is str:
spectrum_url = DataUrl(file_path=spectrum_url, scheme='PyMca')
......@@ -51,9 +53,10 @@ def read(spectrum_url, config_url=None):
if config_url is not None:
config = utils.get_data(config_url)
spectrum = {}
if spectrum_url.scheme() in ('PyMca', 'PyMca5'):
energy, mu = read_spectrum(spectrum_url.file_path())
config['Energy'], config['Mu'] = energy, mu
spectrum['Energy'], spectrum['Mu'] = energy, mu
elif not spectrum_url.is_valid():
raise ValueError('given url is invalid')
else:
......@@ -61,4 +64,4 @@ def read(spectrum_url, config_url=None):
if 'Energy' not in data or 'Mu' not in data:
raise ValueError('Energy and/or Mu keys are not available in given'
'url data', spectrum_url)
return config
return PyMcaXAS(spectrum=spectrum, configuration=config)
......@@ -27,36 +27,48 @@ __authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "06/11/2019"
from xas.core.types import PyMcaXAS
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
import logging
_logger = logging.getLogger(__name__)
def pymca_k_weight(xas_process_io):
def pymca_k_weight(xas_obj):
"""
:param dict configuration: should contain the
:return: configuration dict
:param xas_obj: object containing the configuration and spectrum to process
:type: Union[XASBase, dict]
:return: spectrum dict
:rtype: dict
"""
if 'SET_KWEIGHT' not in xas_process_io.configuration:
assert xas_obj is not None
if isinstance(xas_obj, dict):
_xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
else:
_xas_obj = xas_obj
assert _xas_obj is not None
if 'SET_KWEIGHT' not in _xas_obj.configuration:
_logger.warning('Missing configuration to know which value we should set '
'to k weight, will be set to 0 by default')
xas_process_io.configuration['SET_KWEIGHT'] = 0
_xas_obj.configuration['SET_KWEIGHT'] = 0
for key in ('FT', 'EXAFS', 'Normalization'):
if key not in _xas_obj.configuration:
_xas_obj.configuration[key] = {}
xas_process_io.configuration['KWeight'] = xas_process_io.configuration['SET_KWEIGHT']
xas_process_io.configuration['FT']['KWeight'] = xas_process_io.configuration['SET_KWEIGHT']
xas_process_io.configuration['EXAFS']['KWeight'] = xas_process_io.configuration['SET_KWEIGHT']
xas_process_io.configuration['Normalization']['KWeight'] = xas_process_io.configuration['SET_KWEIGHT']
_xas_obj.configuration['KWeight'] = _xas_obj.configuration['SET_KWEIGHT']
_xas_obj.configuration['FT']['KWeight'] = _xas_obj.configuration['SET_KWEIGHT']
_xas_obj.configuration['EXAFS']['KWeight'] = _xas_obj.configuration['SET_KWEIGHT']
_xas_obj.configuration['Normalization']['KWeight'] = _xas_obj.configuration['SET_KWEIGHT']
pymca_xas = XASClass()
if 'Energy' not in xas_process_io.spectrum or 'Mu' not in xas_process_io.spectrum:
if 'Energy' not in _xas_obj.spectrum or 'Mu' not in _xas_obj.spectrum:
_logger.error('Energy and or Mu is/are not specified, unable to '
'compute exafs')
return
pymca_xas.setSpectrum(energy=xas_process_io.spectrum['Energy'],
mu=xas_process_io.spectrum['Mu'])
pymca_xas.setConfiguration(xas_process_io.configuration)
pymca_xas.setSpectrum(energy=_xas_obj.spectrum['Energy'],
mu=_xas_obj.spectrum['Mu'])
pymca_xas.setConfiguration(_xas_obj.configuration)
res = pymca_xas.processSpectrum()
return res
......@@ -29,27 +29,33 @@ __date__ = "06/11/2019"
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
from xas.core.types import PyMcaXAS
import logging
_logger = logging.getLogger(__name__)
def pymca_normalization(xas_process_io):
def pymca_normalization(xas_obj):
"""
:param dict configuration: should contain the
:return: configuration dict
:param xas_obj: object containing the configuration and spectrum to process
:type: Union[XASBase, dict]. If is a dict, should contain configuration or
spectrum keys. Otherwise is simply the spectrum
:return: spectrum dict
:rtype: dict
"""
pymca_xas = XASClass()
if 'Energy' not in xas_process_io.spectrum or 'Mu' not in xas_process_io.spectrum:
if isinstance(xas_obj, dict):
_xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
else:
_xas_obj = xas_obj
if 'Energy' not in _xas_obj.spectrum or 'Mu' not in _xas_obj.spectrum:
_logger.error('Energy and or Mu is/are not specified, unable to '
'normalize')
return
pymca_xas.setSpectrum(energy=xas_process_io.spectrum['Energy'],
mu=xas_process_io.spectrum['Mu'])
pymca_xas.setConfiguration(xas_process_io.configuration)
pymca_xas.setSpectrum(energy=_xas_obj.spectrum['Energy'],
mu=_xas_