Commit 54086b4f authored by payno's avatar payno

Merge branch 'fix_2' into 'master'

Keep configuration when non gui processing

Closes #2

See merge request workflow/xas!2
parents 055df4b9 c29037ef
Pipeline #12124 passed with stage
in 10 minutes and 2 seconds
......@@ -83,7 +83,7 @@ class ExafsOW(OWWidget):
inputs = [("spectrum", XASBase, "process")]
outputs = [("spectrum", XASBase), ("curves", tuple)]
process_function = xas.core.process.exafs.pymca_exafs
process_function = xas.core.process.exafs.PyMca_exafs
_pymcaSettings = Setting(dict())
"""Store the configuration of the PyMca XASClass"""
......@@ -116,10 +116,10 @@ class ExafsOW(OWWidget):
return
self._latest_xas_obj = xas_obj.copy()
xas_obj.configuration['EXAFS'] = self._window._pymcaWindow.getParameters()
spectrum = ExafsOW.process_function(xas_obj)
self._updatePlot(spectrum)
xas_obj.spectrum = spectrum
process_obj = ExafsOW.process_function()
process_obj.setProperties({'_pymcaSettings': self._window._pymcaWindow.getParameters()})
xas_obj = process_obj.process(xas_obj)
self._updatePlot(xas_obj.spectrum)
# emit signal for the plot
self.send("spectrum", xas_obj)
......
......@@ -83,7 +83,7 @@ class FTOW(OWWidget):
inputs = [("spectrum", XASBase, "process")]
outputs = [("spectrum", XASBase), ("curves", tuple)]
process_function = xas.core.process.ft.pymca_ft
process_function = xas.core.process.ft.PyMca_ft
_pymcaSettings = Setting(dict())
"""Store the configuration of the PyMca XASClass"""
......@@ -115,11 +115,11 @@ class FTOW(OWWidget):
return
self._latest_xas_obj = xas_obj.copy()
xas_obj.configuration['FT'] = self._window._pymcaWindow.getParameters()
process_obj = FTOW.process_function()
process_obj.setProperties({'_pymcaSettings': self._window._pymcaWindow.getParameters()})
spectrum = FTOW.process_function(xas_obj)
self._updatePlot(ddict=spectrum['FT'])
xas_obj.spectrum.update(spectrum)
xas_obj = process_obj.process(xas_obj)
self._updatePlot(ddict=xas_obj.spectrum['FT'])
# emit signal for the plot
self.send("spectrum", xas_obj)
......
......@@ -88,7 +88,7 @@ class KWeightOW(OWWidget):
inputs = [("spectrum", XASBase, "process")]
outputs = [("spectrum", XASBase), ("curves", tuple)]
process_function = xas.core.process.k_weight.pymca_k_weight
process_function = xas.core.process.k_weight.PyMca_k_weight
_kWeightSetting = Setting(int(3))
"""Store the configuration of the PyMca XASClass"""
......@@ -119,15 +119,10 @@ class KWeightOW(OWWidget):
return
self._latest_xas_obj = xas_obj.copy()
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
process_obj = KWeightOW.process_function()
process_obj.setProperties({'_kWeightSetting': self._window._k_spin_box.value()})
xas_obj = process_obj.process(xas_obj)
self._updatePlot(xas_obj.spectrum)
# emit signal for the plot
self.send("spectrum", xas_obj)
......
......@@ -88,7 +88,7 @@ class NormalizationOW(OWWidget):
outputs = [("spectrum", XASBase), ("curves", tuple)]
process_function = xas.core.process.normalization.pymca_normalization
process_function = xas.core.process.normalization.PyMca_normalization
_pymcaSettings = Setting(dict())
"""Store the configuration of the PyMca XASClass"""
......@@ -121,8 +121,9 @@ class NormalizationOW(OWWidget):
return
self._latest_xas_obj = xas_obj.copy()
xas_obj.configuration['Normalization'] = self._window._pymcaWindow.getParameters()
xas_obj.spectrum = NormalizationOW.process_function(xas_obj)
process_class = NormalizationOW.process_function()
process_class.setProperties({'_pymcaSettings': self._window._pymcaWindow.getParameters()})
xas_obj = process_class(xas_obj)
self._updatePlot(xas_obj.spectrum)
# emit signal for the plot
self.send("spectrum", xas_obj)
......
......@@ -57,7 +57,7 @@ class XASOutputOW(OWWidget):
inputs = [("spectrum", XASBase, 'process')]
_output_file_setting = Setting(str())
process_function = xas.core.process.io.writer_pymca_cas
process_function = xas.core.process.io.Writer_pymca_cas
def __init__(self):
super().__init__()
......
......@@ -41,7 +41,7 @@ def pymca_exafs(xas_obj):
:param xas_obj: object containing the configuration and spectrum to process
:type: Union[XASBase, dict]
:return: spectrum dict
:rtype: dict
:rtype: XASBase
"""
if isinstance(xas_obj, dict):
_xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
......@@ -70,4 +70,25 @@ def pymca_exafs(xas_obj):
_xas_obj.spectrum['EXAFSKValues'] = kValues
mu = _xas_obj.spectrum['Mu']
_xas_obj.spectrum['EXAFS'] = pymca_xas.postEdge(k=kValues, mu=mu)
return _xas_obj.spectrum
return _xas_obj
class PyMca_exafs(object):
def __init__(self):
self._settings = None
def setProperties(self, properties):
if '_pymcaSettings' in properties:
self._settings = properties['_pymcaSettings']
def process(self, xas_obj):
if isinstance(xas_obj, dict):
_xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
else:
_xas_obj = xas_obj
if self._settings:
_xas_obj.configuration['EXAFS'] = self._settings
return pymca_exafs(xas_obj=xas_obj)
__call__ = process
......@@ -28,7 +28,6 @@ __license__ = "MIT"
__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
......@@ -56,4 +55,31 @@ def pymca_ft(xas_obj):
pymca_xas.setSpectrum(energy=_xas_obj.spectrum['Energy'],
mu=_xas_obj.spectrum['Mu'])
_xas_obj.spectrum = pymca_xas.processSpectrum()
return _xas_obj.spectrum
return _xas_obj
class PyMca_ft(object):
def __init__(self):
self._settings = None
def setProperties(self, properties):
if '_pymcaSettings' in properties:
self._settings = properties['_pymcaSettings']
def process(self, xas_obj):
"""
: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
if self._settings:
_xas_obj.configuration['FT'] = self._settings
return pymca_ft(xas_obj=xas_obj)
__call__ = process
......@@ -76,7 +76,7 @@ def read_pymca_xas(spectrum_url, config_url=None):
return PyMcaXAS(spectrum=spectrum, configuration=config)
class writer_pymca_cas(object):
class Writer_pymca_cas(object):
"""
class to write the output file. In this case we need a class in order to
setup the output file before
......
......@@ -47,10 +47,12 @@ def pymca_k_weight(xas_obj):
_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')
_logger.warning(
'Missing configuration to know which value we should set '
'to k weight, will be set to 0 by default')
_xas_obj.configuration['SET_KWEIGHT'] = 0
for key in ('FT', 'EXAFS', 'Normalization'):
......@@ -58,16 +60,48 @@ def pymca_k_weight(xas_obj):
_xas_obj.configuration[key] = {}
_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']
_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_obj.spectrum or 'Mu' not in _xas_obj.spectrum:
_logger.error('Energy and or Mu is/are not specified, unable to '
'compute exafs')
return
raise ValueError('Energy and or Mu is/are not specified, unable to '
'compute exafs')
pymca_xas.setSpectrum(energy=_xas_obj.spectrum['Energy'],
mu=_xas_obj.spectrum['Mu'])
pymca_xas.setConfiguration(_xas_obj.configuration)
return pymca_xas.processSpectrum()
_xas_obj.spectrum = pymca_xas.processSpectrum()
return _xas_obj
class PyMca_k_weight(object):
def __init__(self):
self._k_weight = None
def setProperties(self, properties):
if '_kWeightSetting' in properties:
self._k_weight = properties['_kWeightSetting']
def process(self, xas_obj):
"""
:param xas_obj: object containing the configuration and spectrum to process
:type: Union[XASBase, dict]
:return: spectrum dict
:rtype: dict
"""
assert xas_obj is not None
if isinstance(xas_obj, dict):
_xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
else:
_xas_obj = xas_obj
if self._k_weight:
_xas_obj.configuration['SET_KWEIGHT'] = self._k_weight
return pymca_k_weight(xas_obj=xas_obj)
__call__ = process
......@@ -36,7 +36,7 @@ _logger = logging.getLogger(__name__)
def pymca_normalization(xas_obj):
"""
: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
......@@ -55,7 +55,37 @@ def pymca_normalization(xas_obj):
pymca_xas.setSpectrum(energy=_xas_obj.spectrum['Energy'],
mu=_xas_obj.spectrum['Mu'])
pymca_xas.setConfiguration(_xas_obj.configuration)
_xas_obj.configuration = pymca_xas.getConfiguration()
pymca_xas.processSpectrum()
res = pymca_xas.normalize()
_xas_obj.spectrum.update(res)
return _xas_obj.spectrum
return _xas_obj
class PyMca_normalization(object):
def __init__(self):
self._settings = None
def setProperties(self, properties):
if '_pymcaSettings' in properties:
self._settings = properties['_pymcaSettings']
def process(self, xas_obj):
"""
: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
"""
if isinstance(xas_obj, dict):
_xas_obj = PyMcaXAS().load_frm_dict(xas_obj)
else:
_xas_obj = xas_obj
if self._settings:
_xas_obj.configuration['Normalization'] = self._settings
return pymca_normalization(_xas_obj)
__call__ = process
......@@ -29,7 +29,7 @@ __date__ = "06/11/2019"
import unittest
from xas.core.process.exafs import pymca_exafs
from xas.core.types import PyMcaXAS
from xas.core.types import PyMcaXAS, XASBase
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from xas.core.utils.pymca import read_spectrum
import os
......@@ -45,18 +45,18 @@ class TestIO(unittest.TestCase):
def testPyMcaXASAsInput(self):
res = pymca_exafs(self.input_)
self.assertTrue(isinstance(res, dict))
self.assertTrue('PostEdgeB' in res)
self.assertTrue('EXAFSKValues' in res)
self.assertTrue('EXAFSSignal' in res)
self.assertTrue(isinstance(res, XASBase))
self.assertTrue('PostEdgeB' in res.spectrum)
self.assertTrue('EXAFSKValues' in res.spectrum)
self.assertTrue('EXAFSSignal' in res.spectrum)
def testDictAsInput(self):
"""Test succeed if the input is a dict"""
res = pymca_exafs(self.input_.to_dict())
self.assertTrue(isinstance(res, dict))
self.assertTrue('PostEdgeB' in res)
self.assertTrue('EXAFSKValues' in res)
self.assertTrue('EXAFSSignal' in res)
self.assertTrue(isinstance(res, XASBase))
self.assertTrue('PostEdgeB' in res.spectrum)
self.assertTrue('EXAFSKValues' in res.spectrum)
self.assertTrue('EXAFSSignal' in res.spectrum)
def suite():
......
......@@ -31,7 +31,7 @@ import unittest
from xas.core.process.ft import pymca_ft
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from xas.core.utils.pymca import read_spectrum
from xas.core.types import PyMcaXAS
from xas.core.types import PyMcaXAS, XASBase
import os
......@@ -46,17 +46,17 @@ class TestIO(unittest.TestCase):
def testPyMcaXASAsInput(self):
res = pymca_ft(xas_obj=self.xas_obj)
self.assertTrue(isinstance(res, dict))
self.assertTrue('FTRadius' in res['FT'])
self.assertTrue('FTImaginary' in res['FT'])
self.assertTrue('FTIntensity' in res['FT'])
self.assertTrue(isinstance(res, XASBase))
self.assertTrue('FTRadius' in res.spectrum['FT'])
self.assertTrue('FTImaginary' in res.spectrum['FT'])
self.assertTrue('FTIntensity' in res.spectrum['FT'])
def testDictAsInput(self):
res = pymca_ft(xas_obj=self.xas_obj.to_dict())
self.assertTrue(isinstance(res, dict))
self.assertTrue('FTRadius' in res['FT'])
self.assertTrue('FTImaginary' in res['FT'])
self.assertTrue('FTIntensity' in res['FT'])
self.assertTrue(isinstance(res, XASBase))
self.assertTrue('FTRadius' in res.spectrum['FT'])
self.assertTrue('FTImaginary' in res.spectrum['FT'])
self.assertTrue('FTIntensity' in res.spectrum['FT'])
def suite():
......
......@@ -28,15 +28,23 @@ __date__ = "06/11/2019"
import unittest
import os
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from xas.core.process.k_weight import pymca_k_weight
from xas.core.types import PyMcaXAS
from xas.core.utils.pymca import read_spectrum
class TestIO(unittest.TestCase):
"""Make sure the process have valid io"""
def setUp(self):
self.config = {'SET_KWEIGHT': 2.0}
self.xas_obj = PyMcaXAS(configuration=self.config)
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
spectrum = {}
spectrum['Energy'], spectrum['Mu'] = read_spectrum(data_file)
self.spectrum = spectrum
self.xas_obj = PyMcaXAS(spectrum=spectrum, configuration=self.config)
def testWithXASObjAsInput(self):
pymca_k_weight(xas_obj=self.xas_obj)
......
......@@ -29,7 +29,7 @@ __date__ = "06/11/2019"
import unittest
from xas.core.process.normalization import pymca_normalization
from xas.core.types import PyMcaXAS
from xas.core.types import PyMcaXAS, XASBase
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from xas.core.utils.pymca import read_spectrum
import os
......@@ -45,17 +45,17 @@ class TestIO(unittest.TestCase):
def testWithXASObjAsInput(self):
res = pymca_normalization(xas_obj=self.xas_obj)
self.assertTrue(isinstance(res, dict))
self.assertTrue('NormalizedMu' in res)
self.assertTrue('NormalizedEnergy' in res)
self.assertTrue('NormalizedSignal' in res)
self.assertTrue(isinstance(res, XASBase))
self.assertTrue('NormalizedMu' in res.spectrum)
self.assertTrue('NormalizedEnergy' in res.spectrum)
self.assertTrue('NormalizedSignal' in res.spectrum)
def testWithDictAsInput(self):
res = pymca_normalization(xas_obj=self.xas_obj.to_dict())
self.assertTrue(isinstance(res, dict))
self.assertTrue('NormalizedMu' in res)
self.assertTrue('NormalizedEnergy' in res)
self.assertTrue('NormalizedSignal' in res)
self.assertTrue(isinstance(res, XASBase))
self.assertTrue('NormalizedMu' in res.spectrum)
self.assertTrue('NormalizedEnergy' in res.spectrum)
self.assertTrue('NormalizedSignal' in res.spectrum)
def suite():
......
......@@ -31,11 +31,11 @@ import unittest
import os
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from silx.io.url import DataUrl
from xas.core.process.exafs import pymca_exafs
from xas.core.process.ft import pymca_ft
from xas.core.process.exafs import pymca_exafs, PyMca_exafs
from xas.core.process.ft import pymca_ft, PyMca_ft
from xas.core.process.io import read_pymca_xas
from xas.core.process.k_weight import pymca_k_weight
from xas.core.process.normalization import pymca_normalization
from xas.core.process.k_weight import pymca_k_weight, PyMca_k_weight
from xas.core.process.normalization import pymca_normalization, PyMca_normalization
class TestStream(unittest.TestCase):
......@@ -50,6 +50,16 @@ class TestStream(unittest.TestCase):
out = pymca_ft(xas_obj=out)
assert out is not None
def test_pymca_process_with_cls(self):
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
out = read_pymca_xas(spectrum_url=DataUrl(file_path=data_file,
scheme='PyMca'))
out = PyMca_normalization()(xas_obj=out)
out = PyMca_k_weight()(xas_obj=out)
out = PyMca_exafs()(xas_obj=out)
out = PyMca_ft()(xas_obj=out)
assert out is not None
def suite():
test_suite = unittest.TestSuite()
......
......@@ -28,7 +28,7 @@ __date__ = "06/26/2019"
import unittest
from xas.core.process.exafs import pymca_exafs
from xas.core.process.exafs import PyMca_exafs
from xas.core.types import PyMcaXAS, XASFactory
import numpy
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
......
......@@ -33,16 +33,16 @@ from xas.pushworkflow.scheme.node import Node
class PyMcaFTTask(Node):
callback = 'xas.core.process.ft.pymca_ft'
callback = 'xas.core.process.ft.PyMca_ft'
class PyMcaExafsTask(Node):
callback = 'xas.core.process.exafs.pymca_exafs'
callback = 'xas.core.process.exafs.PyMca_exafs'
class PyMcaKWeightTask(Node):
callback = 'xas.core.process.k_weight.pymca_k_weight'
callback = 'xas.core.process.k_weight.PyMca_k_weight'
class PyMcaNormalizationTask(Node):
callback = 'xas.core.process.normalization.pymca_normalization'
callback = 'xas.core.process.normalization.process'
......@@ -28,11 +28,11 @@ __date__ = "06/12/2019"
import unittest
from xas.core.process.io import read_pymca_xas
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from xas.pushworkflow.scheme.node import Node
from xas.pushworkflow.scheme.link import Link
from xas.pushworkflow.scheme.scheme import Scheme
from xas.core.process.io import read_pymca_xas
from xas.app.process import exec_ as exec_workflow
import xas.core.process.normalization
import xas.core.process.k_weight
......@@ -42,7 +42,7 @@ import xas.core.process.io
import os
class TestWorkflow(unittest.TestCase):
class TestWorkflowFunctions(unittest.TestCase):
"""Test some processing workflow"""
def setUp(self):
unittest.TestCase.setUp(self)
......@@ -75,13 +75,52 @@ class TestWorkflow(unittest.TestCase):
def testProcess(self):
out = exec_workflow(scheme=self.scheme, input_=self.data_file)
assert isinstance(out, dict)
assert 'FT' in out
assert 'FTRadius' in out['FT']
assert 'FT' in out['spectrum']
assert 'FTRadius' in out['spectrum']['FT']
class TestWorkflowCallableClasses(unittest.TestCase):
"""Test some processing workflow"""
def setUp(self):
unittest.TestCase.setUp(self)
read_task = Node(callback=xas.core.process.io.read_pymca_xas)
normalization_task = Node(
callback=xas.core.process.normalization.PyMca_normalization)
k_weight_task = Node(callback=xas.core.process.k_weight.PyMca_k_weight)
exafs_task = Node(callback=xas.core.process.exafs.PyMca_exafs)
ft_task = Node(callback=xas.core.process.ft.PyMca_ft)
nodes = (
read_task, normalization_task, k_weight_task, exafs_task, ft_task)
links = [
Link(source_node=read_task, source_channel='spectrum',
sink_node=normalization_task, sink_channel='spectrum'),
Link(source_node=normalization_task, source_channel='spectrum',
sink_node=k_weight_task, sink_channel='spectrum'),
Link(source_node=k_weight_task, source_channel='spectrum',
sink_node=exafs_task, sink_channel='spectrum'),
Link(source_node=exafs_task, source_channel='spectrum',
sink_node=ft_task, sink_channel='spectrum'),
]
self.scheme = Scheme(nodes=nodes, links=links)
self.data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
def testProcess(self):
out = exec_workflow(scheme=self.scheme, input_=self.data_file)
assert isinstance(out, dict)
assert 'FT' in out['spectrum']
assert 'FTRadius' in out['spectrum']['FT']
assert 'FT' in out['configuration']
assert 'Normalization' in out['configuration']
def suite():
test_suite = unittest.TestSuite()
for ui in (TestWorkflow, ):
for ui in (TestWorkflowFunctions, TestWorkflowCallableClasses):
test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui))
return test_suite
......
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