Commit e96147ba authored by payno's avatar payno
Browse files

[process] decoupling spectrum and configuration. Should be a class I guess

parent fa381a2a
......@@ -89,7 +89,7 @@ class ExafsOW(OWWidget):
def process(self, process_io):
if process_io is None:
return
res = ExafsOW.process_function(process_io.configuration)
res = ExafsOW.process_function(process_io)
self._updatePlot(res)
# emit signal for the plot
......@@ -98,6 +98,13 @@ class ExafsOW(OWWidget):
def _updatePlot(self, ddict):
plot = self._window.plot
plot.clear()
k = ddict["EXAFSKValues"]
if "KMin" not in ddict:
ddict["KMin"] = k.min()
if "KMax" not in ddict:
ddict["KMax"] = k.max()
idx = (ddict["EXAFSKValues"] >= ddict["KMin"]) & \
(ddict["EXAFSKValues"] <= ddict["KMax"])
......@@ -115,7 +122,6 @@ class ExafsOW(OWWidget):
legend="PostEdge",
xlabel="K",
ylabel="Normalized Units",
color="blue",
resetzoom=False)
plot.addCurve(ddict["KnotsX"],
......@@ -123,5 +129,4 @@ class ExafsOW(OWWidget):
legend="Knots",
linestyle="",
symbol="o",
color="orange",
resetzoom=True)
......@@ -89,7 +89,8 @@ class FTOW(OWWidget):
def process(self, process_io):
if process_io is None:
return
ddict = FTOW.process_function(process_io.configuration)
ddict = FTOW.process_function(process_io)
self._updatePlot(ddict=ddict)
# emit signal for the plot
self.send("spectrum", xas_process_io(spectrum=process_io.spectrum,
......
......@@ -97,7 +97,7 @@ class KWeightOW(OWWidget):
if process_io is None:
return
res = KWeightOW.process_function(process_io.configuration)
res = KWeightOW.process_function(process_io)
self._updatePlot(res)
# emit signal for the plot
......@@ -124,8 +124,7 @@ class KWeightOW(OWWidget):
legend="Normalized EXAFS",
xlabel="K",
ylabel=ylabel,
replace=True,
replot=False)
replace=True)
plot.addCurve(x=ddict["FT"]["K"],
y=ddict["FT"]["WindowWeight"],
......@@ -134,6 +133,5 @@ class KWeightOW(OWWidget):
ylabel="Weight",
yaxis="right",
color="red",
replace=False,
replot=False)
replace=False)
plot.resetZoom()
......@@ -87,6 +87,7 @@ class NormalizationOW(OWWidget):
def __init__(self):
super().__init__()
self._current_process_io = None
self._window = NormalizationWindow(parent=self)
layout = gui.vBox(self.mainArea, 'fourier transform').layout()
layout.addWidget(self._window)
......@@ -94,20 +95,38 @@ class NormalizationOW(OWWidget):
self.pymca_xas = XASClass()
# connect signals / slots
if hasattr(self._window._pymcaWindow, 'sigNormalizationParametersSignal'):
_sig = self._window._pymcaWindow.sigNormalizationParametersSignal
else:
_sig = self._window._pymcaWindow.sigXASNormalizationParametersSignal
_sig.connect(self._updateProcess)
def process(self, process_io):
if process_io is None:
return
ddict = NormalizationOW.process_function(process_io.configuration)
self._current_process_io = process_io
conf = self._window._pymcaWindow.getParameters()
conf.update(process_io.configuration)
process_io = xas_process_io(spectrum=process_io.spectrum,
configuration=conf)
ddict = NormalizationOW.process_function(process_io)
self._updatePlot(ddict)
# emit signal for the plot
self.send("spectrum", xas_process_io(spectrum=process_io.spectrum,
configuration=ddict))
def _updateProcess(self):
if self._current_process_io:
self.process(self._current_process_io)
def _updatePlot(self, ddict):
self._window.plot.clear()
if ddict is None:
return
assert isinstance(ddict, dict)
idx = (ddict["NormalizedEnergy"] >= ddict["NormalizedPlotMin"]) & \
(ddict["NormalizedEnergy"] <= ddict["NormalizedPlotMax"])
......
......@@ -32,7 +32,6 @@ 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.spectrum import Spectrum
from Orange.widgets.settings import Setting
from silx.io.url import DataUrl
import os
......@@ -117,10 +116,10 @@ class ExafsOW(OWWidget):
return
else:
try:
config = xas.core.process.io.read(DataUrl(file_path=self._input_file_setting,
scheme='PyMca'))
spectrum = xas.core.process.io.read(DataUrl(file_path=self._input_file_setting,
scheme='PyMca'))
except ... as e:
_logger.error(e)
else:
self.send("spectrum", xas_process_io(spectrum=Spectrum(config['Energy'], config['Mu']),
configuration=config))
self.send("spectrum", xas_process_io(spectrum=spectrum,
configuration={}))
......@@ -31,10 +31,8 @@ __date__ = "06/11/2019"
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from silx.gui import qt
from xas.core.utils.pymca import read_spectrum
from Orange.widgets.settings import Setting
from xas.core.types import xas_process_io
from xas.core.spectrum import Spectrum
import os
import logging
_logger = logging.getLogger(__file__)
......@@ -106,7 +104,7 @@ class ExafsOW(OWWidget):
self._output_file_setting = file_path
self._inputLe.setText(file_path)
def _getFileSelected(self, file_path):
def _getFileSelected(self):
return self._inputLe.text()
def process(self, process_io):
......
......@@ -34,7 +34,7 @@ import logging
_logger = logging.getLogger(__name__)
def pymca_exafs(configuration):
def pymca_exafs(xas_process_io):
"""
:param dict configuration: should contain the
......@@ -42,26 +42,27 @@ def pymca_exafs(configuration):
:rtype: dict
"""
pymca_xas = XASClass()
if 'Energy' not in configuration or 'Mu' not in configuration:
if 'Energy' not in xas_process_io.spectrum or 'Mu' not in xas_process_io.spectrum:
_logger.error('Energy and or Mu is/are not specified, unable to '
'compute exafs')
return
pymca_xas.setSpectrum(energy=configuration['Energy'],
mu=configuration['Mu'])
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.setConfiguration(configuration)
# TODO: kvalues could be already store.
if 'EXAFSKValues' in configuration and 'Mu' in configuration:
kValues = configuration['EXAFSKValues']
if 'EXAFSKValues' in xas_process_io.spectrum and 'Mu' in xas_process_io.spectrum:
kValues = xas_process_io.spectrum['EXAFSKValues']
else:
if 'Mu' not in configuration:
if 'Mu' not in xas_process_io.spectrum:
print('********************** computing **********************')
configuration = pymca_xas.processSpectrum()
e0 = configuration['Edge']
energy = configuration['Energy']
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']
kValues = e2k(energy - e0)
mu = configuration['Mu']
xas_process_io.spectrum['EXAFSKValues'] = kValues
mu = xas_process_io.spectrum['Mu']
res = pymca_xas.postEdge(k=kValues, mu=mu)
res = pymca_xas.mergeConfigurationDicts(configuration, res)
configuration.update(res)
return configuration
xas_process_io.spectrum.update(res)
return xas_process_io.spectrum
......@@ -34,33 +34,34 @@ import logging
_logger = logging.getLogger(__name__)
def pymca_ft(configuration):
def pymca_ft(xas_process_io):
"""
:param dict configuration: should contain the
:return: configuration dict
"""
pymca_xas = XASClass()
if 'Energy' not in configuration or 'Mu' not in configuration:
if 'Energy' not in xas_process_io.spectrum or 'Mu' not in xas_process_io.spectrum:
_logger.error('Energy and or Mu is/are not specified, unable to '
'compute exafs')
return
pymca_xas.setSpectrum(energy=configuration['Energy'],
mu=configuration['Mu'])
pymca_xas.setSpectrum(energy=xas_process_io.spectrum['Energy'],
mu=xas_process_io.spectrum['Mu'])
pymca_xas.setConfiguration(xas_process_io.configuration)
pymca_xas.processSpectrum()
pymca_xas.setConfiguration(configuration)
mu = configuration['Mu']
if 'EXAFSKValues' in configuration:
kValues = configuration['EXAFSKValues']
mu = xas_process_io.spectrum['Mu']
if 'EXAFSKValues' in xas_process_io.spectrum:
kValues = xas_process_io.spectrum['EXAFSKValues']
spectrum = xas_process_io.spectrum
else:
configuration = pymca_xas.processSpectrum()
e0 = configuration["Edge"]
energy = configuration['Energy']
mu = configuration['Mu']
spectrum = pymca_xas.processSpectrum()
e0 = spectrum["Edge"]
energy = spectrum['Energy']
mu = spectrum['Mu']
kValues = e2k(energy - e0)
res = pymca_xas.fourierTransform(k=kValues, mu=mu, kMin=None, kMax=None)
configuration.update(res)
return configuration
res.update(spectrum)
return res
......@@ -32,17 +32,17 @@ import logging
_logger = logging.getLogger(__name__)
def pymca_k_weight(configuration):
def pymca_k_weight(xas_process_io):
"""
:param dict configuration: should contain the
:return: configuration dict
"""
if 'SET_KWEIGHT' not in configuration:
if 'SET_KWEIGHT' not in xas_process_io.configuration:
_logger.warning('Missing configuration to know which value we should set '
'to k weight, will be set to 0 by default')
configuration['SET_KWEIGHT'] = 0
xas_process_io.configuration['SET_KWEIGHT'] = 0
configuration['KWeight'] = configuration['SET_KWEIGHT']
return configuration
xas_process_io.spectrum['KWeight'] = xas_process_io.configuration['SET_KWEIGHT']
return xas_process_io.spectrum
......@@ -33,21 +33,23 @@ import logging
_logger = logging.getLogger(__name__)
def pymca_normalization(configuration):
def pymca_normalization(xas_process_io):
"""
:param dict configuration: should contain the
:return: configuration dict
"""
pymca_xas = XASClass()
if 'Energy' not in configuration or 'Mu' not in configuration:
if 'Energy' not in xas_process_io.spectrum or 'Mu' not in xas_process_io.spectrum:
_logger.error('Energy and or Mu is/are not specified, unable to '
'normalize')
return
pymca_xas.setSpectrum(energy=configuration['Energy'], mu=configuration['Mu'])
pymca_xas.setConfiguration(configuration)
pymca_xas.setSpectrum(energy=xas_process_io.spectrum['Energy'],
mu=xas_process_io.spectrum['Mu'])
pymca_xas.setConfiguration(xas_process_io.configuration)
pymca_xas.processSpectrum()
res = pymca_xas.normalize()
configuration.update(res)
return configuration
xas_process_io.spectrum.update(res)
return xas_process_io.spectrum
......@@ -42,9 +42,9 @@ class TestIO(unittest.TestCase):
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
config = {}
config['Energy'], config['Mu'] = read_spectrum(data_file)
with TestLogging(pymca_exafs_logger, error=0, warning=0):
with TestLogging(xas_class_logger, error=0, warning=0):
res = pymca_exafs(configuration=config)
# with TestLogging(pymca_exafs_logger, error=0, warning=0):
# with TestLogging(xas_class_logger, error=0, warning=0):
res = pymca_exafs(configuration=config)
self.assertTrue(isinstance(res, dict))
self.assertTrue('PostEdgeB' in res)
self.assertTrue('EXAFSKValues' in res)
......
......@@ -42,9 +42,9 @@ class TestIO(unittest.TestCase):
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
config = {}
config['Energy'], config['Mu'] = read_spectrum(data_file)
with TestLogging(pymca_ft_logger, error=0, warning=0):
with TestLogging(xas_class_logger, error=0, warning=0):
res = pymca_ft(configuration=config)
# with TestLogging(pymca_ft_logger, error=0, warning=0):
# with TestLogging(xas_class_logger, error=0, warning=0):
res = pymca_ft(configuration=config)
self.assertTrue(isinstance(res, dict))
self.assertTrue('FTRadius' in res)
self.assertTrue('FTImaginary' in res)
......
......@@ -40,9 +40,9 @@ class TestInput(unittest.TestCase):
"""Make sure the process have valid io"""
def testWithConfiguration(self):
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
with TestLogging(io_logger, error=0, warning=0):
with TestLogging(xas_class_logger, error=0, warning=0):
res = read(spectrum_url=DataUrl(file_path=data_file, scheme='PyMca'))
# with TestLogging(io_logger, error=0, warning=0):
# with TestLogging(xas_class_logger, error=0, warning=0):
res = read(spectrum_url=DataUrl(file_path=data_file, scheme='PyMca'))
self.assertTrue(isinstance(res, dict))
self.assertTrue('Mu' in res)
self.assertTrue('Energy' in res)
......
......@@ -38,9 +38,9 @@ class TestIO(unittest.TestCase):
"""Make sure the process have valid io"""
def testWithConfiguration(self):
config = {'SET_KWEIGHT': 2.0}
with TestLogging(pymca_k_logger, error=0, warning=0):
with TestLogging(xas_class_logger, error=0, warning=0):
res = pymca_k_weight(configuration=config)
# with TestLogging(pymca_k_logger, error=0, warning=0):
# with TestLogging(xas_class_logger, error=0, warning=0):
res = pymca_k_weight(configuration=config)
self.assertTrue('SET_KWEIGHT' in res)
......
......@@ -41,8 +41,8 @@ class TestIO(unittest.TestCase):
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
config = {}
config['Energy'], config['Mu'] = read_spectrum(data_file)
with TestLogging(pymca_normalization_logger, error=0, warning=0):
res = pymca_normalization(configuration=config)
# with TestLogging(pymca_normalization_logger, error=0, warning=0):
res = pymca_normalization(configuration=config)
self.assertTrue(isinstance(res, dict))
self.assertTrue('NormalizedMu' in res)
self.assertTrue('NormalizedEnergy' in res)
......
......@@ -42,13 +42,13 @@ from xas.core.process.normalization import pymca_normalization
class TestStream(unittest.TestCase):
"""Make sure the process have valid io"""
with TestLogging(xas_class_logger, error=0, warning=0):
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
out = read(spectrum_url=DataUrl(file_path=data_file, scheme='PyMca'))
out = pymca_normalization(configuration=out)
out = pymca_k_weight(configuration=out)
out = pymca_exafs(configuration=out)
out = pymca_ft(configuration=out)
# with TestLogging(xas_class_logger, error=0, warning=0):
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
out = read(spectrum_url=DataUrl(file_path=data_file, scheme='PyMca'))
out = pymca_normalization(configuration=out)
out = pymca_k_weight(configuration=out)
out = pymca_exafs(configuration=out)
out = pymca_ft(configuration=out)
def suite():
......
# 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__ = "06/11/2019"
class Spectrum(object):
def __init__(self, energy, mu):
self.__energy = energy
self.__mu = mu
@property
def energy(self):
return self.__energy
@property
def mu(self):
return self.__mu
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