Commit bdb24bd1 authored by payno's avatar payno
Browse files

[refactoring] create the io module

parent 570c955e
%% Cell type:markdown id: tags:
# XAS process
## pymca functions
The xas process are made from pymca functions. The main class dealing with XAS in pymca is the `XASClass`
Here is the initialization of the class:
%% Cell type:code id: tags:
``` python
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
pymca_xas = XASClass()
```
%% Cell type:markdown id: tags:
The XASClass is associated to a `XASParameters`
%% Cell type:markdown id: tags:
Then we have to set some spectrum
%% Cell type:code id: tags:
``` python
from PyMca5.PyMcaIO import specfilewrapper as specfile
def read_spectrum(spec_file):
"""
:param spec_file: path to the spec file containing the spectrum definition
:return: (energy, mu)
:rtype: tuple
"""
scan = specfile.Specfile(spec_file)[0]
data = scan.data()
if data.shape[0] == 2:
energy = data[0, :]
mu = data[1, :]
else:
energy = None
mu = None
labels = scan.alllabels()
i = 0
for label in labels:
if label.lower() == "energy":
energy = data[i, :]
elif label.lower() in ["counts", "mu", "absorption"]:
mu = data[i, :]
i = i + 1
if (energy is None) or (mu is None):
if len(labels) == 3:
if labels[0].lower() == "point":
energy = data[1, :]
mu = data[2, :]
else:
energy = data[0, :]
mu = data[1, :]
else:
energy = data[0, :]
mu = data[1, :]
return energy, mu
```
%% Cell type:code id: tags:
``` python
import os
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
energy, mu = read_spectrum(data_file)
print(energy)
print(mu)
pymca_xas = XASClass()
pymca_xas.setSpectrum(energy, mu)
```
%% Cell type:markdown id: tags:
### normalization
%% Cell type:code id: tags:
``` python
ddict_norm = pymca_xas.normalize()
ddict_norm
```
%% Cell type:markdown id: tags:
output should be
%% Cell type:code id: tags:
``` python
%gui qt
from silx.gui.plot import Plot1D
plotNorm = Plot1D()
plotNorm.addCurve(ddict_norm["NormalizedEnergy"],
ddict_norm["NormalizedMu"],
legend="Mu",
yaxis="right")
plotNorm.setWindowTitle('normalization')
plotNorm.show()
```
%% Cell type:markdown id: tags:
### EXAFS (signal extraction)
%% Cell type:code id: tags:
``` python
from PyMca5.PyMcaPhysics.xas.XASClass import e2k
params = pymca_xas._configuration["DefaultBackend"]["EXAFS"]
e0 = ddict_norm["Edge"]
kValues = e2k(energy - e0)
ddict_pe = pymca_xas.postEdge(k=kValues, mu=mu, backend=None)
print(ddict_pe)
```
%% Cell type:code id: tags:
``` python
plotPostEdge = Plot1D()
plotPostEdge.addCurve(ddict_pe["NormalizedEnergy"],
ddict_pe["NormalizedSignal"], legend="Mu")
plotPostEdge.setWindowTitle('post edge')
plotPostEdge.show()
```
%% Cell type:markdown id: tags:
### k weight
%% Cell type:markdown id: tags:
will just update k in the EXAFS and Fourier transform classes.
%% Cell type:markdown id: tags:
### Fourier transform
%% Cell type:code id: tags:
``` python
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
pymca_xas = XASClass()
ddict = pymca_xas.fourierTransform(k=kValues, mu=mu, kMin=None, kMax=None)
```
%% Cell type:code id: tags:
``` python
plotFT = Plot1D()
plotFT.addCurve(ddict["FTRadius"],
ddict["FTIntensity"],
legend="FT Intensity",
xlabel="R (Angstrom)",
ylabel="Arbitrary Units")
plotFT.setActiveCurve("FT Intensity")
plotFT.addCurve(ddict["FTRadius"],
ddict["FTImaginary"],
legend="FT Imaginary",
xlabel="R (Angstrom)",
ylabel="Arbitrary Units",
color="red")
plotFT.setWindowTitle('Fourier transform')
plotFT.show()
```
%% Cell type:markdown id: tags:
## xas workflow
To keep compatibility and to normalize the process we defined processes from tomwer which are based on the pymca functions.
Those are simple function to be called with a configuration (as a dict)
%% Cell type:markdown id: tags:
TODO: present XASBase and PyMcaXAS classes
%% Cell type:markdown id: tags:
Reading a spectrum file (and a configuration file)
%% Cell type:code id: tags:
``` python
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
import os
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
```
%% Cell type:code id: tags:
``` python
from xas.core.process.io import read_pymca_xas
from xas.io import read_pymca_xas
from silx.io.url import DataUrl
spec_url = DataUrl(file_path=data_file, scheme='PyMca')
print(spec_url.scheme())
xas_obj = read_pymca_xas(spectrum_url=DataUrl(file_path=data_file, scheme='PyMca'))
assert 'Mu' in xas_obj.spectrum
```
%% Cell type:markdown id: tags:
### normalization
%% Cell type:code id: tags:
``` python
from xas.core.process.normalization import pymca_normalization
spectrum = pymca_normalization(xas_obj.copy())
assert 'NormalizedMu' in spectrum
```
%% Cell type:markdown id: tags:
### exafs
%% Cell type:code id: tags:
``` python
from xas.core.process.exafs import pymca_exafs
spectrum = pymca_exafs(xas_obj.copy())
assert 'PostEdgeB' in spectrum
```
%% Cell type:markdown id: tags:
### k weight
%% Cell type:code id: tags:
``` python
from xas.core.process.k_weight import pymca_k_weight
l_xas_obj = xas_obj.copy()
l_xas_obj.configuration['SET_KWEIGHT'] = 1
spectrum = pymca_k_weight(l_xas_obj)
assert spectrum['KWeight'] == 1
```
%% Cell type:markdown id: tags:
### Fourier transform
%% Cell type:code id: tags:
``` python
from xas.core.process.ft import pymca_ft
spectrum = pymca_ft(xas_obj.copy())
assert 'FTRadius' in spectrum
```
%% Cell type:markdown id: tags:
## Defining a treatment workflow
%% Cell type:code id: tags:
``` python
import xas
from xas.pushworkflow.scheme.node import Node
from xas.pushworkflow.scheme.link import Link
from xas.pushworkflow.scheme.scheme import Scheme
import xas.core.process.normalization
import xas.core.process.k_weight
import xas.core.process.exafs
import xas.core.process.ft
import xas.core.process.io
import xas.io
read_task = Node(callback=xas.core.process.io.read_pymca_xas)
read_task = Node(callback=xas.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'),
]
scheme = Scheme(nodes=nodes, links=links)
```
%% Cell type:markdown id: tags:
Then we can execute the workflow previously defined
%% Cell type:code id: tags:
``` python
from xas.app.process import exec_ as exec_workflow
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from silx.io.url import DataUrl
import os
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
out = exec_workflow(scheme=scheme, input_=data_file)
assert isinstance(out, dict)
assert 'FTRadius' in out['FT']
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
......@@ -37,7 +37,7 @@ from silx.io.url import DataUrl
import h5py
import os
import logging
import xas.core.process.io
import xas.io
_logger = logging.getLogger(__file__)
......@@ -60,7 +60,7 @@ class XASInputOW(OWWidget):
_input_file_setting = Setting(str())
process_function = xas.core.process.io.read_pymca_xas
process_function = xas.io.read_pymca_xas
def __init__(self):
super().__init__()
......@@ -134,15 +134,14 @@ class XASInputOW(OWWidget):
def _interpreteInput(self, file_path):
if file_path.endswith('.dat'):
return xas.core.process.io.read_pymca_xas(DataUrl(file_path=self._input_file_setting,
scheme='PyMca'))
return xas.io.read_pymca_xas(DataUrl(file_path=self._input_file_setting,
scheme='PyMca'))
elif h5py.is_hdf5(file_path):
return xas.core.process.io.read_pymca_xas(
spectrum_url=DataUrl(file_path=file_path,
scheme='PyMca',
data_path='spectrum'),
config_url=DataUrl(file_path=file_path,
scheme='silx',
data_path='configuration'))
return xas.io.read_pymca_xas(spectrum_url=DataUrl(file_path=file_path,
scheme='PyMca',
data_path='spectrum'),
config_url=DataUrl(file_path=file_path,
scheme='silx',
data_path='configuration'))
else:
raise ValueError('file type not managed, unable to load')
......@@ -34,7 +34,7 @@ from silx.gui import qt
from Orange.widgets.settings import Setting
from xas.core.types import XASBase
from xas.core.types import XASFactory
import xas.core.process.io
import xas.io
import logging
_logger = logging.getLogger(__file__)
......@@ -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.io.Writer_pymca_cas
def __init__(self):
super().__init__()
......
......@@ -28,7 +28,7 @@ __date__ = "06/11/2019"
import unittest
from xas.core.process.io import read_pymca_xas
from xas.io import read_pymca_xas
from silx.io.url import DataUrl
from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from xas.core.types import PyMcaXAS, XASFactory
......
......@@ -33,7 +33,7 @@ from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR
from silx.io.url import DataUrl
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.io import read_pymca_xas
from xas.core.process.k_weight import pymca_k_weight, PyMca_k_weight
from xas.core.process.normalization import pymca_normalization, PyMca_normalization
......
from .io import *
......@@ -32,13 +32,12 @@ 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
import xas.core.process.exafs
import xas.core.process.ft
import xas.core.process.io
import xas.io
import os
......@@ -47,7 +46,7 @@ class TestWorkflowFunctions(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
read_task = Node(callback=xas.core.process.io.read_pymca_xas)
read_task = Node(callback=xas.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)
......@@ -84,7 +83,7 @@ class TestWorkflowCallableClasses(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
read_task = Node(callback=xas.core.process.io.read_pymca_xas)
read_task = Node(callback=xas.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)
......
......@@ -36,6 +36,7 @@ def configuration(parent_package='', top_path=None):
config.add_subpackage('app')
config.add_subpackage('core')
config.add_subpackage('gui')
config.add_subpackage('io')
config.add_subpackage('pushworkflow')
config.add_subpackage('pushworkflowactors')
config.add_subpackage('resources')
......
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