Workflow

All the spectra fitting operations can be realized both through the GUI or by python scripts.

However, although python scripts can be very practical when working with repetitive actions (like batches in the case of parametric studies for instance), from a practical point of view, it is easier:

  • 1/ to use the GUI to define a Fitspy model visually, then

  • 2/ to apply it to new data sets.

GUI Mode

../_images/workflow.png

To create a `Fitspy` model:

  • (1) Select file(s) from Select Files or Select Dir

  • (2) Define the X-range

  • (3) Click on the Baseline panel to activate it (if not)

  • (4) Select baseline points on the main figure (*)

  • (5) Subtract Selec. the baseline to the selected spectra or Subtract All the baseline(s)

  • (6) Click on the Fitting panel to activate it (if not)

  • (7) Select a Peak model

  • (8) Select a peak point on the main figure (*)

  • (9) Add a background (BKG model) to be fitted

  • (10) Use Parameters to see the results and to set bounds and constraints for a new fitting

  • (13) Save Select or Save All the `Models` in a `.json` file (to be replayed later)

(*) use left/right click on the figure to add/delete a baseline or a peak point

Once saved, a Fitspy model enables to recover a previous state (as-it, if all the spectra defined in the model can be loaded again) as follows:

  • (14) Reload the `Fitspy` model (`.json` file)

  • (11) Fit Selec. or Fit All the spectra

  • (12) Save Results (fitted parameters and statistics)

Or, after removing all spectra in the file selector widget (Remove All), the Fitspy model can be apply to another data set as follows:

  • (1) Select file(s) from Select Files or Select Dir

  • (15) Load Model *(associated to the first spectra if several)

  • (16) Apply to Sel. or Apply to All

  • (12) Save Results (fitted parameters and statistics)

Scripting Mode

Although it is more recommended to use the GUI to define a Fitspy model visually , here is a partial example of how to do it by script:

from fitspy.spectrum import Spectrum

spectrum = Spectrum()

# load a spectrum to create the model
spectrum.load_profile(fname=r"C:\Users\...\H-000.txt", xmin=150, xmax=650)

# baseline definition and subtract
spectrum.baseline.points = [[160, 600], [52, 28]] # (x, y) baseline points coordinates
spectrum.subtract_baseline()

# peak models creation (based on 2 peaks)
spectrum.add_peak_model('Lorentzian', x0=322)
spectrum.add_peak_model('Gaussian', x0=402)

# model saving
spectrum.save(fname_json=r"C:\Users\...\model.json")

Once defined, a Fitspy model saved in a ‘.json’ file can be applied to a more consequent data set as follows:

from pathlib import Path
from fitspy.spectra import Spectra
from fitspy.spectrum import Spectrum


# list of the spectra pathnames to handle
dirname = Path(r"C:\Users\...")
fnames = dirname.glob('*.txt')

# Spectra object creation
spectra = Spectra()
for fname in fnames:
    spectrum = Spectrum()
    spectrum.load_profile(fname)
    spectra.append(spectrum)

# Fitspy model loading and application
model = Spectra.load_model(fname_json=r"C:\Users\...\model.json")
spectra.apply_model(model, ncpus=16)

# save the calculated fitting parameters
spectra.save_results(dirname_results=r"C:\Users\...\results")