{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Analysis of simulated EDX data\n", "\n", "In this notebook we showcase the analysis of the built-in simulated dataset. We use the espm EDXS modelling to simulate the data.\n", "We first perform a KL-NMF decomposition of the data and then plot the results." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "%matplotlib inline\n", "\n", "# Generic imports \n", "import hyperspy.api as hs\n", "import numpy as np\n", "\n", "# espm imports\n", "from espm.estimators import SmoothNMF\n", "import espm.datasets as ds" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Generating artificial datasets and loading them\n", "\n", "If the datasets were already generated, they are not generated again" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Generate the data\n", "# Here `seeds_range` is the number of different samples to generate\n", "ds.generate_built_in_datasets(seeds_range=1)\n", "\n", "# Load the data\n", "spim = ds.load_particules(sample = 0)\n", "# We need to change the data to floats for the decomposition to run\n", "spim.change_dtype('float64')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Building G\n", "\n", "The information in the metadata of the spim object are used to build the G matrix. This matrix contains a model of the characteristic X-rays and the bremsstrahlung." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "spim.build_G()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Problem solving\n", "\n", "### Picking analysis parameters\n", "\n", "- 3 components for 3 phases\n", "- convergence criterions : tol and max_iter. tol is the minimum change of the loss function between two iterations. max_iter is the max number of iterations.\n", "- G : The EDX model is integrated in the decomposition. The algorithm learns the concentration of each chemical element and the bremsstrahlung parameters.\n", "- hspy_comp is a required parameter if you want to use the hyperspy api" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "est = SmoothNMF( n_components = 3, tol = 1e-6, max_iter = 200, G = spim.model,hspy_comp = True)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Calculating the decomposition\n", "\n", "/!\\ It should take a minute to execute in the case of the built-in dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out = spim.decomposition(algorithm = est, return_info=True)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Getting the losses and the results of the decomposition\n", "\n", "- First cell : Printing the resulting concentrations.\n", "- Second cell : Ploting the resulting spectra\n", "- Thrid cell : Ploting the resulting abundances\n", "\n", "Hyperspy is mainly designed to be used with the qt graphical backend of matplotlib. Thus two plots will appear if you are in inline mode." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spim.print_concentration_report()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "spim.plot_decomposition_loadings(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spim.plot_decomposition_factors(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "espm_test", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.11" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }