Fitting 1D dataset

In this example, we find the least square solution of a simple linear equation.


import os

import spectrochempy as scp

Load and prepare an IR spectrum

nd = scp.read_omnic(os.path.join("irdata", "nh4y-activation.spg"))

Select the OH region and mask a noisy interval:

ndOH = nd[54, 3800.0:3300.0]
ndOH[:, 3505.0:3500.0] = scp.MASKED
_ = ndOH.plot()
plot fit

Define the fitting model

Fit parameters are defined in a script with the following syntax:

script = """
#-----------------------------------------------------------
# syntax for parameters definition:
# name: value, low_bound,  high_bound
# available prefix:
#  # for comments
#  * for fixed parameters
#  $ for variable parameters
#  > for reference to a parameter in the COMMON block
#    (> is forbidden in the COMMON block)
# common block parameters should not have a _ in their names
#-----------------------------------------------------------
#

COMMON:
# common parameters ex.
# $ gwidth: 1.0, 0.0, none
$ gratio: 0.1, 0.0, 1.0

MODEL: LINE_1
shape: asymmetricvoigtmodel
    * ampl:  1.1, 0.0, none
    $ pos:   3620, 3400.0, 3700.0
    $ ratio: 0.0147, 0.0, 1.0
    $ asym: 0.1, 0, 1
    $ width: 50, 0, 1000

MODEL: LINE_2
shape: asymmetricvoigtmodel
    $ ampl:  0.8, 0.0, none
    $ pos:   3540, 3400.0, 3700.0
    > ratio: gratio
    $ asym: 0.1, 0, 1
    $ width: 50, 0, 1000

"""

Create the optimizer and inspect the starting model

f1 = scp.Optimize(log_level="INFO")
f1.script = script

# Use dry mode to preview the starting parameters
f1.dry = True
f1.autobase = True
_ = f1.fit(ndOH)

scp.info_(f"numbers of components: {f1.n_components}")
_ = ndOH.plot()
ax = (f1.components[:]).plot(clear=False)
ax.autoscale(enable=True, axis="y")
plot fit
**************************************************
Starting parameters:
**************************************************

COMMON:
       $ gratio:     0.1000, 0.0, 1.0

MODEL: line_1
shape: asymmetricvoigtmodel
       * ampl:     1.1000, 0.0, none
       $ asym:     0.1000, 0.0, 1.0
       $ pos:  3620.0000, 3400.0, 3700.0
       $ ratio:     0.0147, 0.0, 1.0
       $ width:    50.0000, 0.0, 1000.0

MODEL: line_2
shape: asymmetricvoigtmodel
       $ ampl:     0.8000, 0.0, none
       $ asym:     0.1000, 0.0, 1.0
       $ pos:  3540.0000, 3400.0, 3700.0
       > ratio:gratio
       $ width:    50.0000, 0.0, 1000.0

numbers of components: 2

Fit the model

**************************************************
Result:
**************************************************

COMMON:
       $ gratio:     0.3458, 0.0, 1.0

MODEL: line_1
shape: asymmetricvoigtmodel
       * ampl:     1.1000, 0.0, none
       $ asym:     0.7716, 0.0, 1.0
       $ pos:  3623.4044, 3400.0, 3700.0
       $ ratio:     0.4394, 0.0, 1.0
       $ width:    43.5995, 0.0, 1000.0

MODEL: line_2
shape: asymmetricvoigtmodel
       $ ampl:     0.9001, 0.0, none
       $ asym:     1.0000, 0.0, 1.0
       $ pos:  3536.9977, 3400.0, 3700.0
       > ratio:gratio
       $ width:    79.4888, 0.0, 1000.0

Show the result

_ = ndOH.plot()
ax = (f1.components[:]).plot(clear=False)
ax.autoscale(enable=True, axis="y")
plot fit

Evaluate the fit quality

plot fit

This ends the example ! The following line can be uncommented if no plot shows when running the .py script with python

# scp.show()

Total running time of the script: (0 minutes 1.074 seconds)