{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# PyMca XAS treatments\n", "\n", "## pymca functions\n", "\n", "The xas process 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": [ "## 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 xas.core.io import read as read_pymca_xas\n", "from silx.io.url import DataUrl\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'), channel_url=DataUrl(file_path=data_file, scheme='PyMca'))\n", "assert 'Mu' in xas_obj.spectra[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### normalization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from xas.core.process.pymca.normalization import pymca_normalization\n", "xas_obj = pymca_normalization(xas_obj.copy())\n", "assert 'NormalizedMu' in xas_obj.spectra[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### exafs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from xas.core.process.pymca.exafs import pymca_exafs\n", "xas_obj = pymca_exafs(xas_obj.copy())\n", "assert 'PostEdgeB' in xas_obj.spectra[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### k weight" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from xas.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[0]['KWeight'] == 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fourier transform" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from xas.core.process.pymca.ft import pymca_ft\n", "xas_obj = pymca_ft(xas_obj.copy())\n", "assert 'FTRadius' in xas_obj.spectra[0]['FT']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining a treatment workflow" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xas\n", "from xas.pushworkflow.scheme.node import Node\n", "from xas.pushworkflow.scheme.link import Link\n", "from xas.pushworkflow.scheme.scheme import Scheme\n", "import xas.core.process.pymca.normalization\n", "import xas.core.process.pymca.k_weight\n", "import xas.core.process.pymca.exafs\n", "import xas.core.process.pymca.ft\n", "from xas.core.types import XASObject\n", "import xas.core.io\n", "\n", "\n", "read_task = Node(callback=xas.core.io.read_frm_file)\n", "normalization_task = Node(callback=xas.core.process.pymca.normalization.pymca_normalization)\n", "k_weight_task = Node(callback=xas.core.process.pymca.k_weight.pymca_k_weight)\n", "exafs_task = Node(callback=xas.core.process.pymca.exafs.pymca_exafs)\n", "ft_task = Node(callback=xas.core.process.pymca.ft.pymca_ft)\n", "\n", "nodes = (read_task, normalization_task, k_weight_task, exafs_task, ft_task)\n", "\n", "links = [\n", " Link(source_node=read_task, source_channel='spectrum',\n", " sink_node=normalization_task, sink_channel='spectrum'),\n", " Link(source_node=normalization_task, source_channel='spectrum',\n", " sink_node=k_weight_task, sink_channel='spectrum'),\n", " Link(source_node=k_weight_task, source_channel='spectrum',\n", " sink_node=exafs_task, sink_channel='spectrum'),\n", " Link(source_node=exafs_task, source_channel='spectrum',\n", " sink_node=ft_task, sink_channel='spectrum'),\n", "]\n", "\n", "scheme = Scheme(nodes=nodes, links=links)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we can execute the workflow previously defined" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from xas.app.process import exec_ as exec_workflow\n", "from PyMca5.PyMcaDataDir import PYMCA_DATA_DIR\n", "from silx.io.url import DataUrl\n", "import os\n", "data_file = os.path.join(PYMCA_DATA_DIR, \"EXAFS_Cu.dat\")\n", "out = exec_workflow(scheme=scheme, input_=data_file)\n", "assert isinstance(out, dict)\n", "xas_obj_out = XASObject.from_dict(out)\n", "assert 'FTRadius' in xas_obj_out.spectra[0].ft" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "" ] } ], "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 }