Package for fitting XRF spectra. Based on xraylib
Project description
Installation
This package requires xraylib and compwizard. On Windows, xraylib can be installed through the Anaconda interface.
conda install -c conda-forge xraylib=4.0.0
pip instal compwizard
For further information on how to install xraylib on other operational systems, check xraylib wiki.
This module can be installed with:
pip install xfit
Usage
This module provides the "Spectrum" class. It is possible to initialize this class with a numpy nd.array or loading an *.mca or *.spt file, by giving the path.
For the continuum estimation algorithm, refer to the Continuum.py file.
Test.py file provides a common *.mca file to use with the example.py script provided.
Example 1
import xfit
import numpy as np
import matplotlib.pyplot as plt
path = r"./test.mca"
pool_file = r"./pool.txt"
Spec = xfit.Spectrum(file_path=path)
Spec.calibrate() #if no arguments are passed, it gets the parameters from the mca or spt header
Spec.estimate_continuum(30, 11, 11, 3) #iterations, filter window, sav-gol window, sav-gol order
Spec.fit_fano_and_noise()
Spec.create_pool(pool_file)
Spec.fit()
#Plot ------
fig, ax = plt.subplots()
ax.plot(Spec.energyaxis, Spec.data, color="black", label="Data")
ax.plot(Spec.energyaxis, Spec.continuum, color="green", label="Continuum")
for element in Spec.areas.keys():
ax.plot(Spec.energyaxis,
Spec.plots[element],
label=element+" fit result",
color=ElementColors[element],
linestyle="--")
ax.legend(loc=1, fancybox=1)
ax.set_yscale("log")
plt.show()
0.11400000005960464 80.00951851146041 (Fano and Noise values found, respectively)
Example 2
import xfit
import numpy as np
ydata = np.arange(1024)
fit_pool = {}
fit_pool["elements"] = {}
fit_pool["elements"]["Cu"] = ["KA1","KA2","KB1","KB3"]
fit_pool["bg"] = 1 #Forces the use of continuum estimation for the fit
Spec = xfit.Spectrum(array=ydata)
Spec.calibrate(x=channels, y=energies)
Spec.estimate_continuum(30, 11, 11, 3)
Spec.fit_fano_and_noise()
Spec.pool = fit_pool
Spec.fit()
#or simply: Spec.fit(pool=fit_pool)
#Plot ------
fig, ax = plt.subplots()
ax.plot(Spec.energyaxis, Spec.data, color="black", label="Data")
ax.plot(Spec.energyaxis, Spec.continuum, color="green", label="Continuum")
for element in Spec.areas.keys():
ax.plot(Spec.energyaxis,
Spec.plots[element],
label=element+" fit result",
color=ElementColors[element],
linestyle="--")
ax.legend(loc=1, fancybox=1)
ax.set_yscale("log")
plt.show()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.