{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# PyMca XAS treatments\n", "\n", "## pymca functions\n", "\n", "The est processes are made from pymca functions. The main class dealing with XAS in pymca is the `XASClass`\n", "Here is the initialization of the class:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from PyMca5.PyMcaPhysics.xas.XASClass import XASClass\n", "pymca_xas = XASClass()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The XASClass is associated to a `XASParameters`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we have to set some spectrum" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from PyMca5.PyMcaIO import specfilewrapper as specfile\n", "\n", "def read_spectrum(spec_file):\n", " \"\"\"\n", " \n", " :param spec_file: path to the spec file containing the spectrum definition\n", " :return: (energy, mu)\n", " :rtype: tuple\n", " \"\"\"\n", "\n", " scan = specfile.Specfile(spec_file)[0]\n", " data = scan.data()\n", "\n", " if data.shape[0] == 2:\n", " energy = data[0, :]\n", " mu = data[1, :]\n", " else:\n", " energy = None\n", " mu = None\n", " labels = scan.alllabels()\n", " i = 0\n", " for label in labels:\n", " if label.lower() == \"energy\":\n", " energy = data[i, :]\n", " elif label.lower() in [\"counts\", \"mu\", \"absorption\"]:\n", " mu = data[i, :]\n", " i = i + 1\n", " if (energy is None) or (mu is None):\n", " if len(labels) == 3:\n", " if labels[0].lower() == \"point\":\n", " energy = data[1, :]\n", " mu = data[2, :]\n", " else:\n", " energy = data[0, :]\n", " mu = data[1, :]\n", " else:\n", " energy = data[0, :]\n", " mu = data[1, :]\n", " return energy, mu" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from PyMca5.PyMcaPhysics.xas.XASClass import XASClass\n", "\n", "from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR\n", "data_file = os.path.join(PYMCA_DATA_DIR, \"EXAFS_Cu.dat\")\n", "\n", "energy, mu = read_spectrum(data_file)\n", "print(energy)\n", "print(mu)\n", "pymca_xas = XASClass()\n", "\n", "pymca_xas.setSpectrum(energy, mu)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### normalization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ddict_norm = pymca_xas.normalize()\n", "ddict_norm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "output should be" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### EXAFS (signal extraction)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from PyMca5.PyMcaPhysics.xas.XASClass import e2k\n", "params = pymca_xas._configuration[\"DefaultBackend\"][\"EXAFS\"]\n", "e0 = ddict_norm[\"Edge\"]\n", "kValues = e2k(energy - e0)\n", "ddict_pe = pymca_xas.postEdge(k=kValues, mu=mu, backend=None)\n", "print(ddict_pe)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### k weight" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "will just update k in the EXAFS and Fourier transform classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fourier transform" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from PyMca5.PyMcaPhysics.xas.XASClass import XASClass\n", "pymca_xas = XASClass()\n", "ddict = pymca_xas.fourierTransform(k=kValues, mu=mu, kMin=None, kMax=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## est - xas workflow\n", "\n", "To keep compatibility and to normalize the process we defined processes from tomwer which are based on the pymca functions.\n", "Those are simple function to be called with a configuration (as a dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TODO: present XASBase and PyMcaXAS classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reading a spectrum file (and a configuration file)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR\n", "import os\n", "data_file = os.path.join(PYMCA_DATA_DIR, \"EXAFS_Cu.dat\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from est.core.io import read as read_pymca_xas\n", "from silx.io.url import DataUrl\n", "from est.core.types import Dim\n", "spec_url = DataUrl(file_path=data_file, scheme='PyMca')\n", "print(spec_url.scheme())\n", "xas_obj = read_pymca_xas(spectra_url=DataUrl(file_path=data_file, scheme='PyMca', data_path='Column 2'), channel_url=DataUrl(file_path=data_file, scheme='PyMca', data_path='Column 1'))\n", "assert 'Mu' in xas_obj.spectra.data.flat[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### normalization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from est.core.process.pymca.normalization import pymca_normalization\n", "xas_obj = pymca_normalization(xas_obj.copy())\n", "assert 'NormalizedMu' in xas_obj.spectra.data.flat[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### exafs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from est.core.process.pymca.exafs import pymca_exafs\n", "xas_obj = pymca_exafs(xas_obj.copy())\n", "assert 'PostEdgeB' in xas_obj.spectra.data.flat[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### k weight" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from est.core.process.pymca.k_weight import pymca_k_weight\n", "l_xas_obj = xas_obj.copy()\n", "l_xas_obj.configuration['SET_KWEIGHT'] = 1\n", "\n", "xas_obj = pymca_k_weight(l_xas_obj)\n", "assert xas_obj.spectra.data.flat[0]['KWeight'] == 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fourier transform" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from est.core.process.pymca.ft import pymca_ft\n", "xas_obj = pymca_ft(xas_obj.copy())\n", "assert 'FTRadius' in xas_obj.spectra.data.flat[0]['FT']" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3.0 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }