!ls
a.out computational_physics IMG_4489.JPG mq-cap4.pdf quantum_scattering.py Siddharth_ee11b038_classical_scattering.zip Untitled0.ipynb astro_data_projects finitewell.py md_nvt.py num_methods_in_prog rahulporuri.github.io tex class-scatter.pdf fun_with_physics_models Molecular dynamics.py quantum_scattering_cfi.py Rungekutta.pdf try_1.py
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline
import numpy as np
img = mpimg.imread('IMG_4489.JPG')
img
array([[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [1, 1, 1], [1, 1, 1], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [1, 1, 1], [1, 1, 1], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [1, 1, 1], [1, 1, 1], [0, 0, 0]], ..., [[1, 1, 1], [0, 0, 0], [0, 0, 0], ..., [0, 2, 1], [0, 0, 0], [0, 0, 0]], [[1, 1, 1], [0, 0, 0], [0, 0, 0], ..., [1, 1, 1], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [1, 1, 1], [0, 0, 0], [0, 0, 0]]], dtype=uint8)
plt.imshow(img[700:2000,3200:4200,0])
plt.colorbar()
<matplotlib.colorbar.Colorbar instance at 0xb1558a6c>
print len(img), len(img[0]), len(img[0][0])
3456 5184 3
plt.plot(img[1800,3200:4200,0])
[<matplotlib.lines.Line2D at 0xb14823ec>]
def _line(x,*p):
a,b,c,d = p
return a*np.exp(-(x-b)**2/(2*c**2))+d
def _mline(x,*p):
a,b,c,d,e,f,g = p
return a*np.exp(-(x-b)**2/(2*c**2))+d+e*np.exp(-(x-f)**2/(2*g**2))
line = img[1800,3200:4200,0]
from scipy.optimize import curve_fit
x = np.arange(len(line))
p0 = [35,550,150,5]
coeff, var_matrix = curve_fit(_line,x,line,p0=p0)
p0 = [30,525,40,0,35,590,40]
coeff, var_matrix = curve_fit(_mline,x,line,p0=p0)
print coeff
[ 20.40663461 530.09476791 54.24503953 4.57058066 16.22418517 588.28803041 13.22133719]
yfit = [_line(xx,*tuple(coeff)) for xx in x]
plt.hold()
plt.plot(x,line,'*',yfit,'-')
[<matplotlib.lines.Line2D at 0xa115f44c>, <matplotlib.lines.Line2D at 0xa115f9ac>]
multi_yfit = [_mline(xx,*tuple(coeff)) for xx in x]
#plt.hold()
plt.plot(x,line,'*',yfit,'-',multi_yfit,'x')
#plt.plot(line)
#plt.plot(yfit)
#plt.plot(multi_yfit)
[<matplotlib.lines.Line2D at 0xa0e0f54c>, <matplotlib.lines.Line2D at 0xa0de6cec>, <matplotlib.lines.Line2D at 0xa0ded56c>]
plt.plot(img[1350,3200:4200,0])
[<matplotlib.lines.Line2D at 0xa0dc19ec>]
plt.imshow(img[1100:1600,3400:3900,0])
plt.colorbar()
<matplotlib.colorbar.Colorbar instance at 0xa029cd8c>
def _2Dline(x,y,*p):
a,b,c,d,e = p
return a*np.exp(-((x-b)**2+(y-c)**2)/(2*d**2))+e
data = img[1100:1600,3400:3900,0]
p0 = [500,250,250,100,0]
coeff, var_matrix = curve_fit(_2Dline,x,data,p0=p0)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-62-df827390a5b4> in <module>() 1 data = img[1100:1600,3400:3900,0] 2 p0 = [500,250,250,100,0] ----> 3 coeff, var_matrix = curve_fit(_2Dline,x,data,p0=p0) /usr/lib/python2.7/dist-packages/scipy/optimize/minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, **kw) 531 # Remove full_output from kw, otherwise we're passing it in twice. 532 return_full = kw.pop('full_output', False) --> 533 res = leastsq(func, p0, args=args, full_output=1, **kw) 534 (popt, pcov, infodict, errmsg, ier) = res 535 /usr/lib/python2.7/dist-packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag) 366 if not isinstance(args, tuple): 367 args = (args,) --> 368 shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) 369 m = shape[0] 370 if n > m: /usr/lib/python2.7/dist-packages/scipy/optimize/minpack.pyc in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape) 17 def _check_func(checker, argname, thefunc, x0, args, numinputs, 18 output_shape=None): ---> 19 res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) 20 if (output_shape is not None) and (shape(res) != output_shape): 21 if (output_shape[0] != 1): /usr/lib/python2.7/dist-packages/scipy/optimize/minpack.pyc in _general_function(params, xdata, ydata, function) 442 443 def _general_function(params, xdata, ydata, function): --> 444 return function(xdata, *params) - ydata 445 446 <ipython-input-61-89f824156776> in _2Dline(x, y, *p) 1 def _2Dline(x,y,*p): ----> 2 a,b,c,d,e = p 3 return a*np.exp(-((x-b)**2+(y-c)**2)/(2*d**2))+e ValueError: need more than 4 values to unpack