Commit 263fd837 authored by payno's avatar payno
Browse files

[core][dim] rename energy / channel dim Z

parent 46264ec5
Pipeline #39261 passed with stages
in 6 minutes and 43 seconds
%% Cell type:markdown id: tags:
# PyMca XAS treatments
## pymca functions
The est processes 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: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: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:markdown id: tags:
## est - 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 est.core.io import read as read_pymca_xas
from silx.io.url import DataUrl
from est.core.types import Dim
spec_url = DataUrl(file_path=data_file, scheme='PyMca')
print(spec_url.scheme())
xas_obj = read_pymca_xas(spectra_url=DataUrl(file_path=data_file, scheme='PyMca'), channel_url=DataUrl(file_path=data_file, scheme='PyMca'), dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM))
xas_obj = read_pymca_xas(spectra_url=DataUrl(file_path=data_file, scheme='PyMca'), channel_url=DataUrl(file_path=data_file, scheme='PyMca'), dimensions=(Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM))
assert 'Mu' in xas_obj.spectra.data.flat[0]
```
%% Cell type:markdown id: tags:
### normalization
%% Cell type:code id: tags:
``` python
from est.core.process.pymca.normalization import pymca_normalization
xas_obj = pymca_normalization(xas_obj.copy())
assert 'NormalizedMu' in xas_obj.spectra.data.flat[0]
```
%% Cell type:markdown id: tags:
### exafs
%% Cell type:code id: tags:
``` python
from est.core.process.pymca.exafs import pymca_exafs
xas_obj = pymca_exafs(xas_obj.copy())
assert 'PostEdgeB' in xas_obj.spectra.data.flat[0]
```
%% Cell type:markdown id: tags:
### k weight
%% Cell type:code id: tags:
``` python
from est.core.process.pymca.k_weight import pymca_k_weight
l_xas_obj = xas_obj.copy()
l_xas_obj.configuration['SET_KWEIGHT'] = 1
xas_obj = pymca_k_weight(l_xas_obj)
assert xas_obj.spectra.data.flat[0]['KWeight'] == 1
```
%% Cell type:markdown id: tags:
### Fourier transform
%% Cell type:code id: tags:
``` python
from est.core.process.pymca.ft import pymca_ft
xas_obj = pymca_ft(xas_obj.copy())
assert 'FTRadius' in xas_obj.spectra.data.flat[0]['FT']
```
......
......@@ -173,7 +173,7 @@ def main(argv):
"--input-dimensions",
dest="input_dimensions",
default="None",
help="dimension of the input as (channel, Y, X) for example."
help="dimension of the input as (Z,Y,X) for example."
"If None will take default unit according to the input type",
)
# output option
......
......@@ -52,13 +52,13 @@ def convert_spectra_dims(dims):
return None
res = []
for dim in dims:
if dim in ("energy", "channel"):
dim = Dim.CHANNEL_ENERGY_DIM
if isinstance(dim, str):
if dim.lower() == Dim.X_DIM.value.lower():
dim = Dim.X_DIM
elif dim.lower() == Dim.Y_DIM.value.lower():
dim = Dim.Y_DIM
elif dim.lower() == Dim.Z_DIM.value.lower():
dim = Dim.Z_DIM
dim = Dim.from_value(dim)
if dim in res:
......@@ -130,7 +130,7 @@ def get_xas_obj(
raise ValueError("Unable to read spectrum")
else:
if input_dims is None:
input_dims = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.Y_DIM)
input_dims = (Dim.Z_DIM, Dim.Y_DIM, Dim.Y_DIM)
sp, en, conf = read_xas(
spectra_url=input_spectra_url,
channel_url=input_channel_url,
......
......@@ -60,8 +60,8 @@ def main(argv):
parser.add_argument(
"--input-spectra-dims",
dest="input_spectra_dims",
default=(Dim.CHANNEL_ENERGY_DIM.value, Dim.Y_DIM.value, Dim.X_DIM.value),
help="spectra dimension. Should be a tuple of three values: (X,Y,channel)",
default=(Dim.Z_DIM.value, Dim.Y_DIM.value, Dim.X_DIM.value),
help="spectra dimension. Should be a tuple of three values like: (Z,Y,X)",
)
parser.add_argument(
"--input-channel",
......
......@@ -61,7 +61,7 @@ def read(spectra_url, channel_url, dimensions, config_url=None, energy_unit=ur.e
"""
dimensions_ = dimensions
if dimensions_ is None:
dimensions_ = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
dimensions_ = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM)
reader = XASReader()
return reader.read_frm_url(
......@@ -76,7 +76,7 @@ def read(spectra_url, channel_url, dimensions, config_url=None, energy_unit=ur.e
def read_frm_file(
file_path,
energy_unit=ur.eV,
dimensions: tuple = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM),
dimensions: tuple = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM),
):
"""
......@@ -116,7 +116,7 @@ class XASReader(object):
def read_from_file(
file_path,
energy_unit=ur.eV,
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM),
dimensions=(Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM),
):
"""
......
......@@ -64,7 +64,7 @@ class TestReadWrite(unittest.TestCase):
res = read_xas(
spectra_url=DataUrl(file_path=data_file, scheme="PyMca"),
channel_url=DataUrl(file_path=data_file, scheme="PyMca"),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM),
dimensions=(Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM),
)
self.assertTrue(isinstance(res, XASObject))
self.assertEqual(res.n_spectrum, 1)
......
......@@ -101,7 +101,7 @@ class TestNormalizationMultipleSpectrum(unittest.TestCase):
channel_url=DataUrl(
file_path=filename, data_path=channel_path, scheme="silx"
),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM),
dimensions=(Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM),
)
def tearDown(self):
......
......@@ -76,7 +76,7 @@ class TestStreamSingleSpectrum(unittest.TestCase):
out = read_xas(
spectra_url=DataUrl(file_path=data_file, scheme="PyMca"),
channel_url=DataUrl(file_path=data_file, scheme="PyMca"),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM),
dimensions=(Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM),
)
out.configuration = {"EXAFS": self.exafs_configuration, "SET_KWEIGHT": 0}
out = pymca_normalization(xas_obj=out)
......@@ -90,7 +90,7 @@ class TestStreamSingleSpectrum(unittest.TestCase):
out = read_xas(
spectra_url=DataUrl(file_path=data_file, scheme="PyMca"),
channel_url=DataUrl(file_path=data_file, scheme="PyMca"),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM),
dimensions=(Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM),
)
out.configuration = {"EXAFS": self.exafs_configuration, "SET_KWEIGHT": 0}
out = pymca_normalization(xas_obj=out.to_dict())
......@@ -104,7 +104,7 @@ class TestStreamSingleSpectrum(unittest.TestCase):
out = read_xas(
spectra_url=DataUrl(file_path=data_file, scheme="PyMca"),
channel_url=DataUrl(file_path=data_file, scheme="PyMca"),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM),
dimensions=(Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM),
)
out = PyMca_normalization()(xas_obj=out)
exafs_process = PyMca_exafs()
......
......@@ -58,7 +58,7 @@ class TestRoi(unittest.TestCase):
channel_url=DataUrl(
file_path=filename, data_path=channel_path, scheme="silx"
),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM),
dimensions=(Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM),
)
def tearDown(self):
......
......@@ -87,13 +87,13 @@ class TestSpectraDimensions(unittest.TestCase):
channel_url = self.saveChannel(channel=channel)
# if dims are incoherent with energy, should raise an error
dims = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
dims = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM)
with self.assertRaises(ValueError):
XASReader().read_frm_url(
spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
)
dims = (Dim.X_DIM, Dim.Y_DIM, Dim.CHANNEL_ENERGY_DIM)
dims = (Dim.X_DIM, Dim.Y_DIM, Dim.Z_DIM)
xas_obj = XASReader().read_frm_url(
spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
)
......@@ -116,13 +116,13 @@ class TestSpectraDimensions(unittest.TestCase):
channel_url = self.saveChannel(channel=channel)
# if dims are incoherent with energy, should raise an error
dims = (Dim.X_DIM, Dim.Y_DIM, Dim.CHANNEL_ENERGY_DIM)
dims = (Dim.X_DIM, Dim.Y_DIM, Dim.Z_DIM)
with self.assertRaises(ValueError):
XASReader().read_frm_url(
spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
)
dims = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
dims = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM)
xas_obj = XASReader().read_frm_url(
spectra_url=spectra_url, channel_url=channel_url, dimensions=dims
)
......
......@@ -61,7 +61,7 @@ class TestSpectrum(unittest.TestCase):
"""check that we can create a Spectrum from a pymca .dat file"""
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
energy, mu = read_spectrum(
data_file, dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM)
data_file, dimensions=(Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM)
)
spectrum = Spectrum(energy=energy, mu=mu)
self.assertTrue(spectrum.energy is not None)
......@@ -127,7 +127,7 @@ class TestXASObject(unittest.TestCase):
channel_url=DataUrl(
file_path=filename, data_path=channel_path, scheme="silx"
),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM),
dimensions=(Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM),
)
self.assertEqual(self.xas_obj.spectra.shape[0], 20)
self.assertEqual(self.xas_obj.spectra.shape[1], 10)
......@@ -158,7 +158,7 @@ class TestXASObjectSerialization(unittest.TestCase):
f[self.spectra_path] = self.spectra
f[self.channel_path] = self.energy
self.dimensions = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
self.dimensions = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM)
self.url_spectra = DataUrl(
file_path=self.filename, data_path=self.spectra_path, scheme="silx"
......
......@@ -36,4 +36,4 @@ class Dim(Enum):
X_DIM = "X"
Y_DIM = "Y"
CHANNEL_ENERGY_DIM = "channel / energy"
Z_DIM = "Z"
......@@ -60,7 +60,7 @@ class TestSpectra(unittest.TestCase):
"""check that we can create a Spectrum from a pymca .dat file"""
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
energy, mu = read_spectrum(
data_file, dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM)
data_file, dimensions=(Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM)
)
spectrum = Spectrum(energy=energy, mu=mu)
......
......@@ -52,7 +52,7 @@ class TestSpectrum(unittest.TestCase):
"""check that we can create a Spectrum from a pymca .dat file"""
data_file = os.path.join(PYMCA_DATA_DIR, "EXAFS_Cu.dat")
energy, mu = read_spectrum(
data_file, dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM)
data_file, dimensions=(Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM)
)
spectrum = Spectrum(energy=energy, mu=mu)
self.assertTrue(spectrum.energy is not None)
......
......@@ -100,7 +100,7 @@ class TestXASObject(unittest.TestCase):
channel_url=DataUrl(
file_path=filename, data_path=channel_path, scheme="silx"
),
dimensions=(Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM),
dimensions=(Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM),
)
self.assertEqual(self.xas_obj.spectra.shape[0], 20)
self.assertEqual(self.xas_obj.spectra.shape[1], 10)
......@@ -131,7 +131,7 @@ class TestXASObjectSerialization(unittest.TestCase):
f[self.spectra_path] = self.spectra
f[self.channel_path] = self.energy
self.dimensions = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
self.dimensions = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM)
self.url_spectra = DataUrl(
file_path=self.filename, data_path=self.spectra_path, scheme="silx"
......
......@@ -87,7 +87,7 @@ class XASObject(object):
:type: Union[None,str]
"""
DEFAULT_DIMENSIONS = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
DEFAULT_DIMENSIONS = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM)
def __init__(
self,
......@@ -305,7 +305,7 @@ class XASObject(object):
def load_frm_dict(self, ddict: dict):
"""load XAS values from a dict"""
dimensions = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
dimensions = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM)
# default yay of storing data
from est.io import load_data # avoid cyclic import
......
......@@ -241,7 +241,7 @@ class XASObjectDialog(qt.QWidget):
):
spectrumDiag = self._singleSpectrumDialog
dimensions = list(spectrumDiag.getDimensions())
dimensions.insert(0, Dim.CHANNEL_ENERGY_DIM)
dimensions.insert(0, Dim.Z_DIM)
return read_frm_file(
file_path=spectrumDiag.getFileSelected(),
energy_unit=spectrumDiag.getEnergyUnit(),
......@@ -535,11 +535,11 @@ class _QDimComboBox(qt.QComboBox):
self.addItem(Dim.X_DIM.value)
self.addItem(Dim.Y_DIM.value)
if ndim > 2:
self.addItem(Dim.CHANNEL_ENERGY_DIM.value)
self.addItem(Dim.Z_DIM.value)
def setDim(self, dim):
dim = Dim.from_value(dim)
assert dim in (Dim.X_DIM, Dim.Y_DIM, Dim.CHANNEL_ENERGY_DIM)
assert dim in (Dim.X_DIM, Dim.Y_DIM, Dim.Z_DIM)
index = self.findText(dim.value)
assert index >= 0
self.setCurrentIndex(index)
......@@ -564,7 +564,7 @@ class _SpectraDimensions(qt.QWidget):
self.layout().addRow("dim 2", self._dim2)
# set up
self._dim0.setDim(Dim.CHANNEL_ENERGY_DIM)
self._dim0.setDim(Dim.Z_DIM)
self._dim1.setDim(Dim.Y_DIM)
self._dim2.setDim(Dim.X_DIM)
......@@ -587,7 +587,7 @@ class _SpectraDimensions(qt.QWidget):
assert last_modified != get_second
assert last_modified != get_third
assert type(last_modified) == type(get_second) == type(get_third)
value_set = {Dim.CHANNEL_ENERGY_DIM, Dim.X_DIM, Dim.Y_DIM}
value_set = {Dim.Z_DIM, Dim.X_DIM, Dim.Y_DIM}
last_value_set = last_modified.getDim()
value_set.remove(last_value_set)
......
......@@ -204,7 +204,7 @@ def read_xas(
from est.core.types import Dim # avoid cyclic import
if dimensions is None:
dimensions_ = (Dim.CHANNEL_ENERGY_DIM, Dim.Y_DIM, Dim.X_DIM)
dimensions_ = (Dim.Z_DIM, Dim.Y_DIM, Dim.X_DIM)
else:
dimensions_ = []
for dim in dimensions:
......@@ -217,7 +217,7 @@ def read_xas(
raise ValueError(err)
# fit spectra according to dimension
src_axis = (
dimensions_.index(Dim.CHANNEL_ENERGY_DIM),
dimensions_.index(Dim.Z_DIM),
dimensions_.index(Dim.Y_DIM),
dimensions_.index(Dim.X_DIM),
)
......
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