Commit b254ad7a authored by payno's avatar payno
Browse files

[core][process] fix the ft process.

parent b9b55f81
Pipeline #12682 failed with stage
in 5 minutes and 17 seconds
......@@ -152,6 +152,7 @@ class QPyMca_k_weight(xas.core.process.k_weight.PyMca_k_weight):
self.pool = qt.QThreadPool()
self.pool.setMaxThreadCount(5)
for spectrum in xas_obj.spectra:
assert 'KWeight' in xas_obj.configuration
runnable = ProcessRunnable(fct=xas.core.process.k_weight.process_spectr_k,
spectrum=spectrum,
configuration=xas_obj.configuration,
......
......@@ -69,25 +69,28 @@ def process_spectr_ft(spectrum, configuration, overwrite=True, callback=None,
'fourier transform')
return None, None
if 'EXAFSNormalized' not in spectrum:
raise ValueError()
_logger.warning('ft window need to be defined first')
return None, None
cleanMu = spectrum['EXAFSSignal']
kValues = spectrum['EXAFSKValues']
exafs = (cleanMu - spectrum["PostEdgeB"]) / spectrum["PostEdgeB"]
if spectrum["KWeight"]:
exafs *= pow(kValues, spectrum["KWeight"])
dataSet = numpy.zeros((cleanMu.size, 2), numpy.float)
dataSet[:, 0] = kValues
dataSet[:, 1] = cleanMu
set2 = dataSet.copy()
set2[:, 1] = exafs
set2[:, 1] = spectrum["EXAFSNormalized"]
# remove points with k<2
goodi = (set2[:, 0] >= spectrum["KMin"]) & (set2[:, 0] <= spectrum["KMax"])
set2 = set2[goodi, :]
ft = pymca_xas.fourierTransform(set2[:,0], set2[:, 1], kMin=spectrum["KMin"], kMax=spectrum["KMax"])
ft = pymca_xas.fourierTransform(set2[:,0], set2[:, 1],
kMin=spectrum["KMin"],
kMax=spectrum["KMax"])
if callback:
callback()
......@@ -97,12 +100,15 @@ def process_spectr_ft(spectrum, configuration, overwrite=True, callback=None,
spectrum_ = Spectrum()
spectrum_.update(spectrum)
assert spectrum_
assert "FTIntensity" in ft
assert ft["FTIntensity"] is not None
spectrum_.ft = ft
assert spectrum_.ft.intensity is not None
if output is not None:
assert output_dict is not None
output[output_dict[spectrum]] = spectrum_
assert spectrum_.ft.intensity is not None
return configuration, spectrum_
......@@ -160,6 +166,7 @@ class PyMca_ft(Process):
configuration=xas_obj.configuration,
callback=self._advancement.increaseAdvancement,
overwrite=True)
assert spectrum.ft.intensity is not None
else:
from multiprocessing import Manager
manager = Manager()
......
......@@ -28,9 +28,11 @@ __license__ = "MIT"
__date__ = "06/11/2019"
from xas.core.types import XASObject, Spectrum
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
from xas.core.process.exafs import process_spectr_exafs
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass, e2k
import multiprocessing
import functools
import numpy
from .process import Process
import logging
......@@ -68,18 +70,27 @@ def process_spectr_k(spectrum, configuration, overwrite=True, callback=None,
else:
spectrum_ = Spectrum.from_dict(spectrum.to_dict())
spectrum_ = Spectrum.from_dict(pymca_xas.processSpectrum())
# we need to update EXAFSNormalized since we are overwriting it
cf, exafs_res = process_spectr_exafs(spectrum=spectrum_,
configuration=configuration)
# update EXAFSNormalized
e0 = pymca_xas.calculateE0()
kValues = e2k(spectrum_.energy - e0)
exafs = exafs_res["EXAFSNormalized"]
if "KWeight" in configuration and configuration["KWeight"] is not None:
configuration['FT']['KWeight'] = configuration["KWeight"]
configuration['EXAFS']['KWeight'] = configuration["KWeight"]
exafs *= pow(kValues, configuration["KWeight"])
spectrum_["EXAFSNormalized"] = exafs
configuration_ = pymca_xas.getConfiguration()
if callback:
callback()
if overwrite:
spectrum.load_frm_dict(spectrum_.to_dict())
spectrum_ = spectrum
if output is not None:
assert output_dict is not None
output[output_dict[spectrum]] = spectrum_
return configuration, spectrum_
return configuration_, spectrum_
def pymca_k_weight(xas_obj):
......@@ -102,7 +113,6 @@ _USE_MULTIPROCESSING_POOL = False
class PyMca_k_weight(Process):
def __init__(self):
Process.__init__(self, name='k weight')
self._k_weight = None
def setProperties(self, properties):
if '_kWeightSetting' in properties:
......@@ -123,8 +133,8 @@ class PyMca_k_weight(Process):
"""
assert xas_obj is not None
_xas_obj = self.getXasObject(xas_obj=xas_obj)
if self._k_weight:
_xas_obj.configuration['SET_KWEIGHT'] = self._k_weight
if self._k_weight() is not None:
_xas_obj.configuration['SET_KWEIGHT'] = self._k_weight()
if 'SET_KWEIGHT' not in _xas_obj.configuration:
_logger.warning(
......@@ -157,11 +167,13 @@ class PyMca_k_weight(Process):
"""process normalization from a pool"""
assert isinstance(xas_obj, XASObject)
if not _USE_MULTIPROCESSING_POOL:
assert 'KWeight' in xas_obj.configuration
for spectrum in xas_obj.spectra:
process_spectr_k(spectrum=spectrum,
configuration=xas_obj.configuration,
callback=self._advancement.increaseAdvancement,
overwrite=True)
assert 'KWeight' in xas_obj.configuration
else:
with multiprocessing.Pool(5) as p:
partial_ = functools.partial(process_spectr_k,
......
......@@ -32,6 +32,7 @@ from xas.core.process.ft import pymca_ft
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from xas.io.utils.pymca import read_spectrum
from xas.core.process.normalization import pymca_normalization
from xas.core.process.k_weight import pymca_k_weight
from xas.core.process.exafs import pymca_exafs
from xas.core.types import XASObject, Spectrum
import os
......@@ -48,6 +49,7 @@ class TestFTSingleSpectrum(unittest.TestCase):
# preproc xas obj
self.xas_obj = pymca_normalization(xas_obj=self.xas_obj)
self.xas_obj = pymca_exafs(xas_obj=self.xas_obj)
self.xas_obj = pymca_k_weight(xas_obj=self.xas_obj)
def testPyMcaXASAsInput(self):
res = pymca_ft(xas_obj=self.xas_obj)
......
......@@ -31,6 +31,7 @@ import unittest
import os
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from xas.core.process.k_weight import pymca_k_weight
from xas.core.process.normalization import pymca_normalization
from xas.core.types import Spectrum, XASObject
from xas.io.utils.pymca import read_spectrum
......@@ -47,9 +48,11 @@ class TestKWeightSingleSpectrum(unittest.TestCase):
configuration=self.config, dim1=1, dim2=1)
def testWithXASObjAsInput(self):
self.xas_obj = pymca_normalization(self.xas_obj)
pymca_k_weight(xas_obj=self.xas_obj)
def testWithDictAsInput(self):
self.xas_obj = pymca_normalization(self.xas_obj)
pymca_k_weight(xas_obj=self.xas_obj.to_dict())
......
......@@ -83,6 +83,8 @@ class TestStreamSingleSpectrum(unittest.TestCase):
out = PyMca_k_weight()(xas_obj=out)
out = PyMca_ft()(xas_obj=out)
assert isinstance(out, XASObject)
self.assertTrue(out.spectra[0].ft is not None)
self.assertTrue(len(out.spectra[0].ft.intensity) > 0)
def test_pymca_with_roi(self):
spectra = spectra_utils.create_dataset(shape=(256, 20, 10))
......@@ -99,7 +101,7 @@ class TestStreamSingleSpectrum(unittest.TestCase):
self.assertEqual(out.dim1, 1)
self.assertEqual(out.dim2, 5)
self.assertTrue(out.spectra[0].ft is not None)
self.assertTrue(len(out.spectra[0].ft['FTIntensity']) > 0)
self.assertTrue(len(out.spectra[0].ft.intensity) > 0)
class TestWorkflowAndH5LinkedFile(unittest.TestCase):
......
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