Analysis of simulated EDX data
In this notebook we showcase the analysis of the built-in simulated dataset. We use the espm EDXS modelling to simulate the data. We first perform a KL-NMF decomposition of the data and then plot the results.
Imports
[1]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
# Generic imports
import hyperspy.api as hs
import numpy as np
# espm imports
from espm.estimators import SmoothNMF
import espm.datasets as ds
Generating artificial datasets and loading them
If the datasets were already generated, they are not generated again
[2]:
# Generate the data
# Here `seeds_range` is the number of different samples to generate
ds.generate_built_in_datasets(seeds_range=1)
# Load the data
spim = ds.load_particules(sample = 0)
# We need to change the data to floats for the decomposition to run
spim.change_dtype('float64')
[3]:
spim.metadata
[3]:
- Acquisition_instrument
- TEM
- Detector
- EDS
- azimuth_angle = 0.0
- elevation_angle = 22.0
- energy_resolution_MnKa = 130.0
- take_off_angle = 22
- type = SDD_efficiency.txt
- width_intercept = 0.065
- width_slope = 0.01
- Stage
- tilt_alpha = 0.0
- beam_energy = 200
- General
- FileIO
- 0
- hyperspy_version = 1.7.5
- io_plugin = hyperspy.io_plugins.hspy
- operation = save
- timestamp = 2023-09-06T12:17:31.626233+00:00
- 1
- hyperspy_version = 1.7.5
- io_plugin = hyperspy.io_plugins.hspy
- operation = load
- timestamp = 2023-09-06T12:18:01.519022+00:00
- title =
- Sample
- density = None
- elements = ['23', '37', '74', '7', '70', '78', '13', '22', '57']
- thickness = 1e-05
- Signal
- signal_type = EDS_espm
- Truth
- Data
- misc_parameters
- N = 500
- data_folder = built_in_particules
- densities = [0.6030107883539217, 0.9870613994765459, 0.8894990661032164]
- model = EDXS
- seed = 91
- shape_2d = [80, 80]
- phases = array([[1.52269863, 1.52045778, 1.51324741, ..., 0.00929776, 0.00928124, ... 51, 1.72210813, 1.71435594, ..., 0.01052453, 0.01050582, 0.01048714]])
- weights = array([[[1., 0., 0.], [1., 0., 0.], [1., 0., 0.], ..., ... ..., [1., 0., 0.], [1., 0., 0.], [1., 0., 0.]]])
- xray_db = 200keV_xrays.json
Building G
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.
[4]:
spim.build_G("bremsstrahlung")
Problem solving
Picking analysis parameters
3 components for 3 phases
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.
hspy_comp is a required parameter if you want to use the hyperspy api
[5]:
est = SmoothNMF( n_components = 3,tol=0.000001, max_iter = 100, G = spim.G,hspy_comp = True)
Calculating the decomposition
/! It should take a minute to execute in the case of the built-in dataset
[6]:
out = spim.decomposition(algorithm = est, return_info=True)
It 10 / 100: loss 2.077484e-01, 1.420 it/s
It 20 / 100: loss 2.068881e-01, 1.455 it/s
It 30 / 100: loss 2.066954e-01, 1.469 it/s
It 40 / 100: loss 2.066230e-01, 1.475 it/s
It 50 / 100: loss 2.065880e-01, 1.479 it/s
It 60 / 100: loss 2.065692e-01, 1.482 it/s
It 70 / 100: loss 2.065575e-01, 1.484 it/s
It 80 / 100: loss 2.065501e-01, 1.484 it/s
It 90 / 100: loss 2.065450e-01, 1.485 it/s
exits because max_iteration was reached
Stopped after 100 iterations in 1.0 minutes and 7.0 seconds.
Decomposition info:
normalize_poissonian_noise=False
algorithm=SmoothNMF()
output_dimension=None
centre=None
scikit-learn estimator:
SmoothNMF()
Getting the losses and the results of the decomposition
First cell : Printing the resulting concentrations.
Second cell : Ploting the resulting spectra
Thrid cell : Ploting the resulting abundances
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.
[7]:
spim.print_concentration_report()
Concentrations report
p0 p1 p2
V : 0.0429 0.0074 0.0265
Rb : 0.8374 0.0129 0.9715
W : 0.0806 0.0009 0.0000
N : 0.0356 0.1186 0.0019
Yb : 0.0008 0.1358 0.0000
Pt : 0.0023 0.0248 0.0002
Al : 0.0004 0.1858 0.0000
Ti : 0.0000 0.1576 0.0000
La : 0.0000 0.3563 0.0000
[8]:
spim.plot_decomposition_loadings(3)
[8]:


[9]:
spim.plot_decomposition_factors(3)
[9]:

