Water Teixeira ∗ Resolution with lmfit

Introduction

This example shows how to use the water_teixeira model and fit the data using lmfit.

The data are two sets of water data measured at IN5 (ILL) at 5 Å.

Reference: J. Qvist, H. Schober and B. Halle, J. Chem. Phys. 134, 144508 (2011)

Physical units

For information about unit conversion, please refer to the jupyter notebook called Convert_units.ipynb in the tools folder.

Import libraries

[1]:
import h5py
from scipy.integrate import simps
import numpy as np
import matplotlib.pyplot as plt

# the following line is to remove the warning about too many figures open simultaneously
plt. rcParams.update({'figure.max_open_warning': 0})

import lmfit
from scipy.interpolate import interp1d

%matplotlib widget

Setting of fitting

Import reference data

[2]:
path_to_data = './data/'

with h5py.File(path_to_data + 'H2O_293K_5A.hdf', 'r') as f:
    data_in = f['entry1']
    w = data_in['data1']
    x = w['X'][()]  # energy or time values
    unit_w = w['X'].attrs['long_name']
    unit_q = w['Y'].attrs['long_name']
    y = w['DATA'][()] # intensities
    e = w['errors'][()]  # errors for the intensities
    # Obtain the momentum transfer values
    q = w['Y'][()]
    data_5A = dict(q=q, x=x, y=y, e=e)
[3]:
# number of spectra (i.e. number of different q-values)
nb_q_values = len(data_5A['q'])

Display units of input data

Just for information in order to determine if a conversion of units is required before using the QENSmodels

[4]:
print((f"The names and units of `w` (`x`axis) and `q` are: "
       f"{unit_w[0].decode()} and {unit_q[0].decode()}, respectively."))
The names and units of `w` (`x`axis) and `q` are:  Energy Transfer (meV) and Wavevector Transfer (A!U-1!N), respectively.

Import resolution data and normalize (unit area)

[5]:
path_to_data = './data/'

with h5py.File(path_to_data + 'V_273K_5A.hdf', 'r') as f:
    data = f['entry1']
    w = data['data1']
    res_5A_x = w['X'][()]
    res_5A = np.transpose(w['DATA'][()])

# Force resolution function to have unit area
for i in range(len(data_5A['q'])):
    area = simps(res_5A[:,i], data_5A['x'])
    res_5A[:,i] /= area

Mask data according to energy range and filter negative error

[6]:
# Filter according to energy-range
mask = np.intersect1d(np.where(data_5A['x']>-1.), np.where(data_5A['x']<1.))

f_5A_mask = dict()
f_5A_mask['x'] = np.asarray([data_5A['x'][mask] for i in range(nb_q_values)])
f_5A_mask['y'] = np.asarray([y[mask] for y in data_5A['y']])
f_5A_mask['e'] = np.asarray([e[mask] for e in data_5A['e']])

# Select resolution according to energy range
res_5A_x = res_5A_x[mask]
res_5A = res_5A[mask, :]
[7]:
# Filter according to negative error values
# resolution
selected_indices = np.where(f_5A_mask['e'][i] > 0.0)
resol_5A_x = np.asarray([res_5A_x[selected_indices] for i in range(nb_q_values)])
resol_5A = np.asarray([res_5A[selected_indices, i][0] for i in range(nb_q_values)])

# data
f_5A = dict()
f_5A['x'] = np.asarray([x[selected_indices] for i, x in enumerate(f_5A_mask['x'])])
f_5A['y'] = np.asarray([y[selected_indices] for i, y in enumerate(f_5A_mask['y'])])
f_5A['e'] = np.asarray([e[selected_indices] for i, e in enumerate(f_5A_mask['e'])])
[8]:
# plot experimental data
fig0= plt.figure()
[plt.semilogy(f_5A['x'][i], f_5A['y'][i]) for i in range(nb_q_values)]
plt.xlabel(r'Energy transfer (meV)')
plt.title('Reference data - 5 Angstrom')
plt.grid();
[9]:
# plot experimental data
fig1 = plt.figure()
[plt.semilogy(resol_5A_x[i], resol_5A[i]) for i in range(nb_q_values)]
plt.xlabel(r'Energy transfer (meV)')
plt.title('Resolution function - 5 Angstrom')
plt.grid();

Create function for instrument resolution data (cubic interpolation between tabulated data points)

[10]:
f_interp = [interp1d(resol_5A_x[i],
                     resol_5A[i]/np.sum(resol_5A[i]),
                     kind='cubic',
                     bounds_error=False,
                     fill_value='extrapolate') for i in range(nb_q_values)]

def irf_gate(w, spectrum_nb=0):
    """ Function defined from the interpolation of instrument resolution data
    Used to define fitting model and plot """
    return f_interp[spectrum_nb](w)
[11]:
# check interpolation for first spectrum of resolution function: plot tabulated data and interpolated data
indx = 0

fig2 = plt.figure()
plt.plot(resol_5A_x[indx],
         resol_5A[indx]/np.sum(resol_5A[indx]),
         '.',
         label=f"tabulated data. q={data_5A['q'][indx]:.2}")
plt.plot(f_5A['x'][indx],
         irf_gate(f_5A['x'][indx], indx),
         '--',
         label=f"extrapolated data. q={data_5A['q'][indx]:.2}")
plt.legend(bbox_to_anchor=(1.0, .95))
plt.xlabel('w')
plt.title(f'Instrument resolution: tabulated data and interpolated data for spectrum {indx}')
plt.grid();

Create fitting model

[12]:
import QENSmodels
[13]:
# Create convolution function
# code from https://lmfit.github.io/lmfit-py/model.html

def convolve(arr, kernel):
    # simple convolution of two arrays
    npts = min(len(arr), len(kernel))
    pad  = np.ones(npts)
    tmp  = np.concatenate((pad*arr[0], arr, pad*arr[-1]))

    out  = np.convolve(tmp, kernel, mode='valid')
    noff = int((len(out) - npts)/2)
    return out[noff:noff+npts]
[14]:
model = lmfit.CompositeModel(lmfit.Model(irf_gate), lmfit.Model(QENSmodels.sqwWaterTeixeira), convolve)

print(f'Names of parameters: {model.param_names}\nIndependent variable(s): {model.independent_vars}')

# Define boundaries for parameters to be refined
model.set_param_hint('scale', min=0, max=100)
model.set_param_hint('center', min=-0.1, max=0.1)
model.set_param_hint('D', min=0.05, max=0.25)
model.set_param_hint('resTime', min=0, max=1)
model.set_param_hint('radius', min=0.9, max=1.1)
model.set_param_hint('DR', min=0, max=1)

# Fix some of the parameters
model.set_param_hint('q', vary=False)
model.set_param_hint('spectrum_nb', vary=False)

params = model.make_params()
Names of parameters: ['spectrum_nb', 'q', 'scale', 'center', 'D', 'resTime', 'radius', 'DR']
Independent variable(s): ['w']
[15]:
# Plot of the fitting models without and convoluted with the resolution function
# The values of the parameters are specified below. Therefore they could be
#different from those used in the fitting.

fig3, ax3 = plt.subplots(1, 2)
# First subplot
for i in range(nb_q_values):
    xx = f_5A['x'][i]
    ax3[0].plot(xx,
               QENSmodels.sqwWaterTeixeira(xx,
                                           data_5A['q'][i],
                                           scale=1,
                                           center=0,
                                           D=1,
                                           resTime=1,
                                           radius=1,
                                           DR=1),
               label=f"q={data_5A['q'][i]:.2}")

ax3[0].grid(True)
ax3[0].set(xlabel='Omega', ylabel='S(Q,w)', xlim=(-1, 1), title='No resolution')
ax3[0].tick_params()

plt.tight_layout(rect=[0, 0, 1, 0.8])

ax3[0].legend(bbox_to_anchor=(0., 1.1, 2., 0.102),
             loc='lower right',
             ncol=5,
             mode="expand",
             borderaxespad=0.,
             fontsize=8)

# Second subplot
for i in range(nb_q_values):
    params_plot = model.make_params(nb_spectrum=i,
                                    q=data_5A['q'][i],
                                    scale=10.,
                                    center=0.,
                                    D=0.13,
                                    resTime=0.1,
                                    radius=1.,
                                    DR=0.3)
    xx = f_5A['x'][i]
    ax3[1].plot(xx, model.eval(params_plot, w=xx))

ax3[1].grid(True)
ax3[1].set(xlabel='w',
          ylabel='R $\otimes$ S(Q,w)',
          xlim=(-1,1),
          title='Convoluted with resolution')
ax3[1].tick_params();

Running the fit using lmfit

[16]:
ini_values = {'scale': 10.,
              'center': 0.,
              'D': 0.13,
              'resTime': 0.1,
              'radius': 1.,
              'DR': 0.3}

result_fit = [None,] * nb_q_values  # store fits for all spectra

for i in range(nb_q_values):
    params = model.make_params(nb_spectrum=i,
                               q=data_5A['q'][i],
                               scale=ini_values['scale'],
                               center=ini_values['center'],
                               D=ini_values['D'],
                               resTime=ini_values['resTime'],
                               radius=ini_values['radius'],
                               DR=ini_values['DR'])

    # Q-independent parameters
    if i==0:
        D_value = params['D'].value
        resTime_value = params['resTime'].value
        radius_value = params['radius'].value
        DR_value = params['DR'].value
    else:
        params['D'].set(value=D_value)
        params['resTime'].set(value=resTime_value)
        params['radius'].set(value=radius_value)
        params['DR'].set(value=DR_value)

    result_fit[i] = model.fit(f_5A['y'][i],
                              params,
                              w=f_5A['x'][i])

Showing the results

using methods implemented in lmfit

[17]:
# display result
for i in range(nb_q_values):
    print(f'Result of fit {i}:\n', result_fit[i].fit_report())
Result of fit 0:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 142
    # data points      = 79
    # variables        = 6
    chi-square         = 6.40050235
    reduced chi-square = 0.08767811
    Akaike info crit   = -186.532638
    Bayesian info crit = -172.315951
    R-squared          = 0.99985207
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            0.5 (fixed)
    scale:        20.5754291 +/- 0.15604089 (0.76%) (init = 10)
    center:       7.2794e-04 +/- 2.3776e-04 (32.66%) (init = 0)
    D:            0.13997445 +/- 0.02009094 (14.35%) (init = 0.13)
    resTime:      0.82179666 +/- 4.37502226 (532.37%) (init = 0.1)
    radius:       1.00174292 +/- 0.05134703 (5.13%) (init = 1)
    DR:           0.20433948 +/- 0.04689643 (22.95%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9979
    C(center, resTime) = +0.8204
    C(center, D)       = +0.8148
    C(scale, DR)       = +0.6371
    C(resTime, radius) = +0.6313
    C(D, radius)       = +0.6093
    C(scale, radius)   = +0.5918
    C(center, radius)  = +0.5535
    C(resTime, DR)     = -0.4158
    C(D, DR)           = -0.3760
    C(center, DR)      = -0.3508
    C(radius, DR)      = -0.1679
    C(scale, D)        = +0.1059
Result of fit 1:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 68
    # data points      = 79
    # variables        = 6
    chi-square         = 2.77298688
    reduced chi-square = 0.03798612
    Akaike info crit   = -252.612302
    Bayesian info crit = -238.395615
    R-squared          = 0.99991373
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            0.6 (fixed)
    scale:        20.7327763 +/- 0.12564769 (0.61%) (init = 10)
    center:       0.00136392 +/- 1.5262e-04 (11.19%) (init = 0)
    D:            0.13686407 +/- 0.00256452 (1.87%) (init = 0.13)
    resTime:      0.40409575 +/- 0.06172693 (15.28%) (init = 0.1)
    radius:       0.91726122 +/- 0.02509503 (2.74%) (init = 1)
    DR:           0.24559980 +/- 0.03923805 (15.98%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9350
    C(center, resTime) = +0.7403
    C(center, D)       = +0.7370
    C(scale, radius)   = +0.7304
    C(D, DR)           = +0.6349
    C(scale, DR)       = +0.5082
    C(D, radius)       = -0.4651
    C(resTime, DR)     = +0.4344
    C(center, DR)      = +0.4060
    C(resTime, radius) = -0.3826
    C(scale, resTime)  = -0.3785
    C(center, radius)  = -0.3059
    C(scale, D)        = -0.2205
    C(scale, center)   = -0.2098
Result of fit 2:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 149
    # data points      = 79
    # variables        = 6
    chi-square         = 2.05616843
    reduced chi-square = 0.02816669
    Akaike info crit   = -276.239684
    Bayesian info crit = -262.022996
    R-squared          = 0.99991429
##  Warning: uncertainties could not be estimated:
    radius:       at boundary
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            0.7 (fixed)
    scale:        20.8780021 (init = 10)
    center:       0.00226834 (init = 0)
    D:            0.13904162 (init = 0.13)
    resTime:      0.99997981 (init = 0.1)
    radius:       0.90000099 (init = 1)
    DR:           0.22366412 (init = 0.3)
Result of fit 3:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 128
    # data points      = 79
    # variables        = 6
    chi-square         = 1.70215336
    reduced chi-square = 0.02331717
    Akaike info crit   = -291.166744
    Bayesian info crit = -276.950057
    R-squared          = 0.99990703
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            0.8 (fixed)
    scale:        20.7436424 +/- 0.24139119 (1.16%) (init = 10)
    center:       0.00358278 +/- 2.4594e-04 (6.86%) (init = 0)
    D:            0.13398821 +/- 0.00296322 (2.21%) (init = 0.13)
    resTime:      0.90643465 +/- 0.21378529 (23.59%) (init = 0.1)
    radius:       0.90000444 +/- 0.00965397 (1.07%) (init = 1)
    DR:           0.13464668 +/- 0.01296077 (9.63%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(scale, radius)   = -0.9617
    C(D, resTime)      = +0.9508
    C(scale, DR)       = +0.9099
    C(center, resTime) = -0.8304
    C(center, D)       = -0.8176
    C(radius, DR)      = -0.7892
    C(D, radius)       = -0.7851
    C(scale, D)        = +0.7812
    C(D, DR)           = +0.6122
    C(resTime, radius) = -0.5828
    C(scale, resTime)  = +0.5685
    C(center, radius)  = +0.5583
    C(scale, center)   = -0.5512
    C(center, DR)      = -0.4158
    C(resTime, DR)     = +0.4153
Result of fit 4:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 136
    # data points      = 79
    # variables        = 6
    chi-square         = 1.33155761
    reduced chi-square = 0.01824052
    Akaike info crit   = -310.564778
    Bayesian info crit = -296.348091
    R-squared          = 0.99990482
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            0.9 (fixed)
    scale:        20.7467769 +/- 0.16774226 (0.81%) (init = 10)
    center:       0.00498467 +/- 7.4046e-04 (14.85%) (init = 0)
    D:            0.13240922 +/- 0.00455292 (3.44%) (init = 0.13)
    resTime:      0.99905605 +/- 0.43942548 (43.98%) (init = 0.1)
    radius:       0.90000686 +/- 0.02949816 (3.28%) (init = 1)
    DR:           0.11987759 +/- 0.01006350 (8.39%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9627
    C(scale, radius)   = +0.9246
    C(center, radius)  = -0.9070
    C(scale, center)   = -0.9031
    C(radius, DR)      = -0.8561
    C(center, DR)      = +0.7865
    C(scale, DR)       = -0.6477
    C(resTime, radius) = +0.4204
    C(resTime, DR)     = -0.3259
    C(scale, resTime)  = +0.2247
    C(center, D)       = +0.1906
    C(D, radius)       = +0.1674
    C(D, DR)           = -0.1073
Result of fit 5:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 179
    # data points      = 79
    # variables        = 6
    chi-square         = 1.40277165
    reduced chi-square = 0.01921605
    Akaike info crit   = -306.448828
    Bayesian info crit = -292.232141
    R-squared          = 0.99986289
##  Warning: uncertainties could not be estimated:
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1 (fixed)
    scale:        20.3477694 (init = 10)
    center:       0.00680244 (init = 0)
    D:            0.12988335 (init = 0.13)
    resTime:      0.99997533 (init = 0.1)
    radius:       0.90003436 (init = 1)
    DR:           0.12272440 (init = 0.3)
Result of fit 6:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 64
    # data points      = 79
    # variables        = 6
    chi-square         = 1.47198606
    reduced chi-square = 0.02016419
    Akaike info crit   = -302.643989
    Bayesian info crit = -288.427302
    R-squared          = 0.99980265
##  Warning: uncertainties could not be estimated:
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.1 (fixed)
    scale:        19.7589539 (init = 10)
    center:       0.00890755 (init = 0)
    D:            0.11805291 (init = 0.13)
    resTime:      0.49110709 (init = 0.1)
    radius:       0.90004043 (init = 1)
    DR:           0.12106184 (init = 0.3)
Result of fit 7:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 105
    # data points      = 79
    # variables        = 6
    chi-square         = 1.81581923
    reduced chi-square = 0.02487424
    Akaike info crit   = -286.059978
    Bayesian info crit = -271.843291
    R-squared          = 0.99966119
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.2 (fixed)
    scale:        19.0288985 +/- 0.16746572 (0.88%) (init = 10)
    center:       0.01210094 +/- 6.5471e-04 (5.41%) (init = 0)
    D:            0.10383887 +/- 0.03507444 (33.78%) (init = 0.13)
    resTime:      0.51606924 +/- 2.30900696 (447.42%) (init = 0.1)
    radius:       1.09705069 +/- 0.08885083 (8.10%) (init = 1)
    DR:           0.09158036 +/- 0.01318764 (14.40%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9923
    C(radius, DR)      = -0.9698
    C(center, D)       = -0.7739
    C(center, resTime) = -0.7707
    C(scale, D)        = +0.7226
    C(scale, resTime)  = +0.6510
    C(scale, DR)       = +0.6490
    C(scale, center)   = -0.5513
    C(scale, radius)   = -0.5469
    C(resTime, radius) = +0.1366
Result of fit 8:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 114
    # data points      = 79
    # variables        = 6
    chi-square         = 2.55955687
    reduced chi-square = 0.03506242
    Akaike info crit   = -258.939483
    Bayesian info crit = -244.722796
    R-squared          = 0.99936489
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.3 (fixed)
    scale:        18.6085165 +/- 0.11139276 (0.60%) (init = 10)
    center:       0.01501980 +/- 6.1613e-04 (4.10%) (init = 0)
    D:            0.08957997 +/- 0.04635124 (51.74%) (init = 0.13)
    resTime:      0.03176703 +/- 3.32053499 (10452.77%) (init = 0.1)
    radius:       1.09988480 +/- 0.09928517 (9.03%) (init = 1)
    DR:           0.09890701 +/- 0.01572324 (15.90%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9950
    C(radius, DR)      = -0.9631
    C(scale, DR)       = +0.7256
    C(scale, radius)   = -0.5626
    C(scale, D)        = +0.3561
    C(D, DR)           = +0.3361
    C(scale, resTime)  = +0.3104
    C(D, radius)       = -0.2558
    C(resTime, DR)     = +0.2480
    C(resTime, radius) = -0.1592
    C(center, resTime) = -0.1045
Result of fit 9:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 140
    # data points      = 79
    # variables        = 6
    chi-square         = 3.67804421
    reduced chi-square = 0.05038417
    Akaike info crit   = -230.298270
    Bayesian info crit = -216.081583
    R-squared          = 0.99880252
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.4 (fixed)
    scale:        18.3388689 +/- 0.17101866 (0.93%) (init = 10)
    center:       0.01877373 +/- 9.8251e-04 (5.23%) (init = 0)
    D:            0.08556382 +/- 0.03475842 (40.62%) (init = 0.13)
    resTime:      0.38721490 +/- 2.42367631 (625.93%) (init = 0.1)
    radius:       1.09999186 +/- 0.01836432 (1.67%) (init = 1)
    DR:           0.11380810 +/- 0.00618001 (5.43%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9989
    C(scale, resTime)  = +0.5442
    C(scale, D)        = +0.5192
    C(radius, DR)      = -0.5065
    C(scale, radius)   = +0.4388
    C(center, D)       = -0.3330
    C(center, resTime) = -0.3255
    C(center, radius)  = +0.2092
    C(resTime, DR)     = -0.2077
    C(D, DR)           = -0.2041
    C(scale, DR)       = +0.1220
Result of fit 10:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 159
    # data points      = 79
    # variables        = 6
    chi-square         = 5.05462801
    reduced chi-square = 0.06924148
    Akaike info crit   = -205.182344
    Bayesian info crit = -190.965657
    R-squared          = 0.99788833
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.5 (fixed)
    scale:        18.2572281 +/- 0.21529681 (1.18%) (init = 10)
    center:       0.02305864 +/- 0.00141278 (6.13%) (init = 0)
    D:            0.08737801 +/- 0.06116842 (70.00%) (init = 0.13)
    resTime:      0.98236637 +/- 3.69550549 (376.18%) (init = 0.1)
    radius:       1.09994276 +/- 0.04211481 (3.83%) (init = 1)
    DR:           0.12458419 +/- 0.01108701 (8.90%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9994
    C(radius, DR)      = -0.7495
    C(scale, radius)   = +0.6953
    C(center, D)       = -0.4634
    C(center, resTime) = -0.4575
    C(D, DR)           = +0.3923
    C(resTime, DR)     = +0.3838
    C(center, DR)      = -0.3395
    C(center, radius)  = +0.2627
    C(scale, center)   = +0.2155
    C(scale, DR)       = -0.1965
    C(scale, D)        = -0.1357
    C(scale, resTime)  = -0.1117
Result of fit 11:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 114
    # data points      = 79
    # variables        = 6
    chi-square         = 5.90367306
    reduced chi-square = 0.08087223
    Akaike info crit   = -192.915978
    Bayesian info crit = -178.699291
    R-squared          = 0.99679153
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.6 (fixed)
    scale:        18.0062204 +/- 0.22698409 (1.26%) (init = 10)
    center:       0.02684103 +/- 0.00191390 (7.13%) (init = 0)
    D:            0.07115321 +/- 0.10627967 (149.37%) (init = 0.13)
    resTime:      0.33237185 +/- 7.97312699 (2398.86%) (init = 0.1)
    radius:       1.09999281 +/- 0.04415944 (4.01%) (init = 1)
    DR:           0.13192062 +/- 0.01206174 (9.14%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9989
    C(scale, DR)       = +0.7834
    C(radius, DR)      = -0.7205
    C(center, resTime) = +0.5871
    C(center, D)       = +0.5799
    C(scale, resTime)  = -0.4965
    C(scale, D)        = -0.4848
    C(scale, center)   = -0.3357
    C(scale, radius)   = -0.2635
    C(center, DR)      = -0.2370
    C(resTime, DR)     = -0.2272
    C(D, DR)           = -0.1982
    C(D, radius)       = -0.1817
    C(resTime, radius) = -0.1363
Result of fit 12:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 187
    # data points      = 79
    # variables        = 6
    chi-square         = 4.39419623
    reduced chi-square = 0.06019447
    Akaike info crit   = -216.243895
    Bayesian info crit = -202.027207
    R-squared          = 0.99668339
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.7 (fixed)
    scale:        17.3847192 +/- 0.23201365 (1.33%) (init = 10)
    center:       0.03034537 +/- 0.00164671 (5.43%) (init = 0)
    D:            0.07392517 +/- 0.12601253 (170.46%) (init = 0.13)
    resTime:      0.73650080 +/- 8.26116878 (1121.68%) (init = 0.1)
    radius:       1.09998921 +/- 0.11201571 (10.18%) (init = 1)
    DR:           0.13399341 +/- 0.02056188 (15.35%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9977
    C(radius, DR)      = -0.9360
    C(scale, DR)       = +0.8129
    C(scale, radius)   = -0.6400
    C(resTime, radius) = +0.5267
    C(scale, resTime)  = -0.4983
    C(scale, D)        = -0.4726
    C(D, radius)       = +0.4689
    C(resTime, DR)     = -0.4534
    C(D, DR)           = -0.4018
    C(center, D)       = -0.1263
    C(center, resTime) = -0.1199
Result of fit 13:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 114
    # data points      = 79
    # variables        = 6
    chi-square         = 3.57782091
    reduced chi-square = 0.04901125
    Akaike info crit   = -232.480820
    Bayesian info crit = -218.264133
    R-squared          = 0.99626607
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.8 (fixed)
    scale:        17.0389322 +/- 2.04610059 (12.01%) (init = 10)
    center:       0.03925425 +/- 0.00196437 (5.00%) (init = 0)
    D:            0.09661003 +/- 0.01541426 (15.96%) (init = 0.13)
    resTime:      0.95960284 +/- 0.56191232 (58.56%) (init = 0.1)
    radius:       0.90000264 +/- 2.30434396 (256.04%) (init = 1)
    DR:           0.19513855 +/- 0.05947845 (30.48%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(scale, radius)   = -0.9941
    C(scale, DR)       = +0.9890
    C(D, resTime)      = +0.9799
    C(radius, DR)      = -0.9719
    C(center, resTime) = +0.2930
    C(center, D)       = +0.2707
    C(center, radius)  = +0.1418
    C(center, DR)      = -0.1401
    C(resTime, DR)     = -0.1379
    C(scale, center)   = -0.1348
    C(resTime, radius) = +0.1284
    C(scale, resTime)  = -0.1092
Result of fit 14:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 126
    # data points      = 79
    # variables        = 6
    chi-square         = 5.97352202
    reduced chi-square = 0.08182907
    Akaike info crit   = -191.986781
    Bayesian info crit = -177.770093
    R-squared          = 0.99175250
##  Warning: uncertainties could not be estimated:
    resTime:      at boundary
    radius:       at boundary
    DR:           at boundary
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            1.9 (fixed)
    scale:        26.4072253 (init = 10)
    center:       0.04917063 (init = 0)
    D:            0.11301801 (init = 0.13)
    resTime:      0.99999998 (init = 0.1)
    radius:       0.90000000 (init = 1)
    DR:           0.99999999 (init = 0.3)
Result of fit 15:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 169
    # data points      = 79
    # variables        = 6
    chi-square         = 2.25825480
    reduced chi-square = 0.03093500
    Akaike info crit   = -268.833589
    Bayesian info crit = -254.616901
    R-squared          = 0.99599690
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            2 (fixed)
    scale:        16.3939552 +/- 0.20280852 (1.24%) (init = 10)
    center:       0.05843368 +/- 0.00213004 (3.65%) (init = 0)
    D:            0.06347529 +/- 0.14311925 (225.47%) (init = 0.13)
    resTime:      0.90678143 +/- 8.73856194 (963.69%) (init = 0.1)
    radius:       1.09998837 +/- 0.10262317 (9.33%) (init = 1)
    DR:           0.14647734 +/- 0.01337402 (9.13%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)     = +0.9951
    C(scale, DR)      = +0.8666
    C(radius, DR)     = -0.7989
    C(scale, radius)  = -0.5265
    C(center, radius) = +0.2379
    C(resTime, DR)    = -0.2294
    C(center, DR)     = -0.1941
    C(scale, resTime) = -0.1708
    C(D, DR)          = -0.1587
    C(scale, D)       = -0.1275
    C(D, radius)      = -0.1182
    C(scale, center)  = -0.1120
Result of fit 16:
 [[Model]]
    (Model(irf_gate) <function convolve at 0x7fe3954dab80> Model(sqwWaterTeixeira))
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 211
    # data points      = 79
    # variables        = 6
    chi-square         = 6.20229687
    reduced chi-square = 0.08496297
    Akaike info crit   = -189.017725
    Bayesian info crit = -174.801038
    R-squared          = 0.98619108
[[Variables]]
    spectrum_nb:  0 (fixed)
    q:            2.1 (fixed)
    scale:        28.0719699 +/- 0.80740775 (2.88%) (init = 10)
    center:       0.06590950 +/- 0.00425994 (6.46%) (init = 0)
    D:            0.06973634 +/- 0.16752266 (240.22%) (init = 0.13)
    resTime:      0.02785974 +/- 7.84681399 (28165.42%) (init = 0.1)
    radius:       0.90000001 +/- 538.141162 (59793.46%) (init = 1)
    DR:           0.99999886 +/- 0.01490956 (1.49%) (init = 0.3)
[[Correlations]] (unreported correlations are < 0.100)
    C(D, resTime)      = +0.9999
    C(scale, radius)   = -0.9297
    C(center, D)       = -0.3864
    C(center, resTime) = -0.3853
    C(radius, DR)      = -0.2960
    C(scale, D)        = +0.1968
    C(scale, center)   = -0.1926
    C(scale, resTime)  = +0.1880
    C(center, radius)  = +0.1632
    C(D, radius)       = -0.1559
    C(resTime, radius) = -0.1484

Plot results using lmfit’s features

[18]:
for i in range(nb_q_values):
    result_fit[i].plot();

Other option to plot experimental data, initial fitting model and fitted model for each spectrum using matplotlib.pyplot

[19]:
for indx in range(nb_q_values):
    fig4 = plt.figure()
    plt.plot(f_5A['x'][indx], f_5A['y'][indx], 'bo',label='exp')
    plt.plot(f_5A['x'][indx], result_fit[indx].init_fit, 'k--',label='ini')
    plt.plot(f_5A['x'][indx], result_fit[indx].best_fit, 'r-', label='fin')
    plt.title(f"q={data_5A['q'][indx]:.2}")
    plt.legend()
    plt.grid()
[ ]:


Generated by nbsphinx from a Jupyter notebook.