Tests of the peak finding and peak fitting features of the module on the XRD data of silicon

Runs 2017/1/9


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import sys
import numpy as np

In [2]:
from spectra import getxy, find_peaks, fit_data, output_results

In [3]:
x, y = getxy('../tests/data/xrd.txt')

In [4]:
pks = find_peaks(x, y, 2)

In [5]:
plt.plot(x, y)
plt.plot(x[pks], y[pks], 'ro')


Out[5]:
[<matplotlib.lines.Line2D at 0x7fe773d9d630>]

In [6]:
out1 = fit_data(x, y, pks)


/home/rohan/Cloud/python/spectra/spectra/peaks.py:71: RuntimeWarning: divide by zero encountered in true_divide
  return amp/(1+(2*(x-x0)/fwhm)**2)
/home/rohan/Cloud/python/spectra/spectra/peaks.py:71: RuntimeWarning: invalid value encountered in true_divide
  return amp/(1+(2*(x-x0)/fwhm)**2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-dcf1f511ed9b> in <module>
----> 1 out1 = fit_data(x, y, pks)

~/Cloud/python/spectra/spectra/fitting.py in fit_data(x, y, peak_pos, peak_type, width)
     92         pars['p%s_amp' % i].set(y[p], min=0)
     93 
---> 94     out = model.fit(y, pars, x=x)
     95     return out
     96 

~/anaconda3/lib/python3.7/site-packages/lmfit/model.py in fit(self, data, params, weights, method, iter_cb, scale_covar, verbose, fit_kws, nan_policy, calc_covar, **kwargs)
   1005                              nan_policy=self.nan_policy, calc_covar=calc_covar,
   1006                              **fit_kws)
-> 1007         output.fit(data=data, weights=weights)
   1008         output.components = self.components
   1009         return output

~/anaconda3/lib/python3.7/site-packages/lmfit/model.py in fit(self, data, params, weights, method, nan_policy, **kwargs)
   1353         self.userkws.update(kwargs)
   1354         self.init_fit = self.model.eval(params=self.params, **self.userkws)
-> 1355         _ret = self.minimize(method=self.method)
   1356 
   1357         for attr in dir(_ret):

~/anaconda3/lib/python3.7/site-packages/lmfit/minimizer.py in minimize(self, method, params, **kws)
   1947                         val.lower().startswith(user_method)):
   1948                     kwargs['method'] = val
-> 1949         return function(**kwargs)
   1950 
   1951 

~/anaconda3/lib/python3.7/site-packages/lmfit/minimizer.py in leastsq(self, params, **kws)
   1490 
   1491         try:
-> 1492             lsout = scipy_leastsq(self.__residual, variables, **lskws)
   1493         except AbortFitException:
   1494             pass

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    381     if not isinstance(args, tuple):
    382         args = (args,)
--> 383     shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
    384     m = shape[0]
    385 

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
     24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
     25                 output_shape=None):
---> 26     res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
     27     if (output_shape is not None) and (shape(res) != output_shape):
     28         if (output_shape[0] != 1):

~/anaconda3/lib/python3.7/site-packages/lmfit/minimizer.py in __residual(self, fvars, apply_bounds_transformation)
    536         else:
    537             return _nan_policy(np.asarray(out).ravel(),
--> 538                                nan_policy=self.nan_policy)
    539 
    540     def __jacobian(self, fvars):

~/anaconda3/lib/python3.7/site-packages/lmfit/minimizer.py in _nan_policy(arr, nan_policy, handle_inf)
   2164 
   2165         if contains_nan:
-> 2166             raise ValueError("The input contains nan values")
   2167     return arr
   2168 

ValueError: The input contains nan values

In [7]:
plt.plot(x, y)
plt.plot(x, out1.init_fit)
plt.plot(x, out1.best_fit)


Out[7]:
[<matplotlib.lines.Line2D at 0x10e9209b0>]

In [8]:
print(output_results(out1, pandas=True))


Need pandas to output to file or pandas dataframe
None

In [9]:
# print(output_results(out1, filename='something.txt'))

In [10]:
print(output_results(out1))


[['bg_c0' '0.00891394384238' '0.000116368065484']
 ['bg_c1' '-3.72797826445e-05' '4.7251891488e-06']
 ['bg_c2' '-1.07063720397e-07' '4.20236878473e-08']
 ['p0_x0' '16.7594385313' '0.00523747678644']
 ['p0_amp' '0.0148140191391' '0.000669458517609']
 ['p0_fwhm' '0.231548634761' '0.0151874374284']
 ['p1_x0' '28.5621289075' '0.00154043404604']
 ['p1_amp' '0.0586036612787' '0.000575174923524']
 ['p1_fwhm' '0.313906363136' '0.00441645012981']
 ['p2_x0' '47.4024092356' '0.00155847314152']
 ['p2_amp' '0.0586063405766' '0.000568492915167']
 ['p2_fwhm' '0.321330171086' '0.0044681420804']
 ['p3_x0' '56.2174559392' '0.00205378822795']
 ['p3_amp' '0.044073958697' '0.000573628589862']
 ['p3_fwhm' '0.315604292896' '0.00589239131969']
 ['p4_x0' '69.2035764671' '0.00809813022919']
 ['p4_amp' '0.0110018966971' '0.000582707995921']
 ['p4_fwhm' '0.305945533154' '0.0231874949742']
 ['p5_x0' '76.4071915905' '0.00503886060679']
 ['p5_amp' '0.0198825035752' '0.000518276039288']
 ['p5_fwhm' '0.386634837763' '0.0144699043873']
 ['p6_x0' '88.0620476485' '0.00445882131057']
 ['p6_amp' '0.0247899897598' '0.000469771079879']
 ['p6_fwhm' '0.470627463897' '0.0130167021533']
 ['p7_x0' '94.9489403231' '0.0087911105636']
 ['p7_amp' '0.0144334913303' '0.000409380293138']
 ['p7_fwhm' '0.619999729526' '0.00635568693638']]

In [ ]: