In [1]:
import lmfit

In [2]:
def gaussian(x, amp, cen, wid):
    return amp * exp(-(x-cen)**2 /wid)

In [3]:
from scipy.optimize import curve_fit

In [16]:
x = linspace(-5., 5., 100)
y = gaussian(x, 1., 0., 1.)
yran = y+randn(y.size)*0.05
plot(x,yran, 'o')


Out[16]:
[<matplotlib.lines.Line2D at 0x112243290>]

In [21]:
init_vals = [2,1,2]
best_vals, covar = curve_fit(gaussian, x, yran, p0=init_vals)
print(best_vals, best_vals-[1,0,1])
plot(x, yran, 'o')
plot(x, gaussian(x, best_vals[0], best_vals[1], best_vals[2]))


(array([  9.96957864e-01,  -8.62112108e-04,   9.82723040e-01]), array([-0.00304214, -0.00086211, -0.01727696]))
Out[21]:
[<matplotlib.lines.Line2D at 0x112224e10>]

In [22]:
gmod = lmfit.Model(gaussian)

In [23]:
print(gmod.param_names)


set(['amp', 'wid', 'cen'])

In [25]:
print(gmod.independent_vars)


['x']

In [26]:
params = gmod.make_params()

In [28]:
gmod.eval(x=x, amp=10, cen=6.2, wid=0.75)


Out[28]:
array([  2.30568648e-72,   4.64608219e-71,   9.11081425e-70,
         1.73864600e-68,   3.22885696e-67,   5.83539309e-66,
         1.02630181e-64,   1.75656305e-63,   2.92574223e-62,
         4.74233300e-61,   7.48051849e-60,   1.14829910e-58,
         1.71538661e-57,   2.49374865e-56,   3.52798725e-55,
         4.85718903e-54,   6.50768725e-53,   8.48500318e-52,
         1.07661646e-50,   1.32939392e-49,   1.59746014e-48,
         1.86805665e-47,   2.12585541e-46,   2.35429604e-45,
         2.53730158e-44,   2.66113420e-43,   2.71609610e-42,
         2.69778389e-41,   2.60767132e-40,   2.45291338e-39,
         2.24540789e-38,   2.00028530e-37,   1.73409271e-36,
         1.46297313e-35,   1.20111352e-34,   9.59655664e-34,
         7.46157442e-33,   5.64584808e-32,   4.15730195e-31,
         2.97904881e-30,   2.07743450e-29,   1.40981045e-28,
         9.31060246e-28,   5.98381984e-27,   3.74250944e-26,
         2.27788066e-25,   1.34921978e-24,   7.77710747e-24,
         4.36251738e-23,   2.38144145e-22,   1.26510426e-21,
         6.54028077e-21,   3.29041092e-20,   1.61097037e-19,
         7.67553462e-19,   3.55888034e-18,   1.60583828e-17,
         7.05137637e-17,   3.01321176e-16,   1.25305196e-15,
         5.07098326e-15,   1.99709600e-14,   7.65401616e-14,
         2.85471973e-13,   1.03614670e-12,   3.65984496e-12,
         1.25802073e-11,   4.20820196e-11,   1.36990049e-10,
         4.33975424e-10,   1.33790390e-09,   4.01391686e-09,
         1.17191333e-08,   3.32970904e-08,   9.20663115e-08,
         2.47730154e-07,   6.48695177e-07,   1.65305055e-06,
         4.09935255e-06,   9.89300145e-06,   2.32340300e-05,
         5.31012429e-05,   1.18105058e-04,   2.55632446e-04,
         5.38452159e-04,   1.10372769e-03,   2.20171157e-03,
         4.27407932e-03,   8.07436543e-03,   1.48442374e-02,
         2.65577362e-02,   4.62389414e-02,   7.83444761e-02,
         1.29179187e-01,   2.07281424e-01,   3.23677036e-01,
         4.91866292e-01,   7.27387618e-01,   1.04681133e+00,
         1.46606962e+00])

In [1]:
from lmfit.models import GaussianModel, ExponentialModel
data = loadtxt('/Users/Benjamin/PythonLib/lmfit/examples/NIST_Gauss2.dat')
x = data[:, 1]
y = data[:, 0]

In [2]:
exp_mod = ExponentialModel(prefix='exp_')
pars = exp_mod.guess(y, x=x)

gauss1  = GaussianModel(prefix='g1_')
pars.update( gauss1.make_params())

pars['g1_center'].set(105, min=75, max=125)
pars['g1_sigma'].set(15, min=3)
pars['g1_amplitude'].set(2000, min=10)

gauss2  = GaussianModel(prefix='g2_')

pars.update(gauss2.make_params())

pars['g2_center'].set(155, min=125, max=175)
pars['g2_sigma'].set(15, min=3)
pars['g2_amplitude'].set(2000, min=10)

mod = gauss1 + gauss2 + exp_mod


init = mod.eval(pars, x=x)
plt.plot(x, y)
plt.plot(x, init, 'k--')

out = mod.fit(y, pars, x=x)

print(out.fit_report(min_correl=0.5))

plot(x, out.best_fit, 'r-')


[[Model]]
    ((Model(gaussian, prefix='g1_') + Model(gaussian, prefix='g2_')) + Model(exponential, prefix='exp_'))
[[Fit Statistics]]
    # function evals   = 46
    # data points      = 250
    # variables        = 8
    chi-square         = 1247.528
    reduced chi-square = 5.155
[[Variables]]
    exp_amplitude:   99.0183282 +/- 0.537487 (0.54%) (init= 162.2102)
    exp_decay:       90.9508859 +/- 1.103105 (1.21%) (init= 93.24905)
    g1_sigma:        16.6725753 +/- 0.160481 (0.96%) (init= 15)
    g1_center:       107.030954 +/- 0.150067 (0.14%) (init= 105)
    g1_amplitude:    4257.77319 +/- 42.38336 (1.00%) (init= 2000)
    g1_fwhm:         39.2609138 +/- 0.377905 (0.96%)  == '2.3548200*g1_sigma'
    g2_center:       153.270100 +/- 0.194667 (0.13%) (init= 155)
    g2_amplitude:    2493.41770 +/- 36.16947 (1.45%) (init= 2000)
    g2_sigma:        13.8069484 +/- 0.186794 (1.35%) (init= 15)
    g2_fwhm:         32.5128783 +/- 0.439866 (1.35%)  == '2.3548200*g2_sigma'
[[Correlations]] (unreported correlations are <  0.500)
    C(g1_sigma, g1_amplitude)    =  0.824 
    C(g2_amplitude, g2_sigma)    =  0.815 
    C(exp_amplitude, exp_decay)  = -0.695 
    C(g1_sigma, g2_center)       =  0.684 
    C(g1_center, g2_amplitude)   = -0.669 
    C(g1_center, g2_sigma)       = -0.652 
    C(g1_amplitude, g2_center)   =  0.648 
    C(g1_center, g2_center)      =  0.621 
    C(g1_sigma, g1_center)       =  0.507 
    C(exp_decay, g1_amplitude)   = -0.507 
Out[2]:
[<matplotlib.lines.Line2D at 0x111e7f290>]

In [37]:
model = lmfit.models.QuadraticModel()

In [38]:
pars = model.make_params()

In [44]:
data = loadtxt('temp.txt')
wave = data[:,0]
flux = data[:,1]

In [224]:
nlines = 2
lines = zeros(nlines, dtype=[('SIGN', 'i'),('ELEMENT','S20'),('WAVE','f4'),('EW','f4'), ('WAVELEFT', 'f4')])
lines[0] = (-1, 'MgII', 2796.35, 2., 2789.)
lines[1] = (-1, 'MgII', 2803.53, 2., 2798.)
model, pars = ebossspec.make_model(lines)

In [208]:
reload(ebossspec)


Out[208]:
<module 'ebossspec' from 'ebossspec.py'>

In [132]:


In [154]:
init = model.eval(pars, x=log10(wave))
out = model.fit(flux,pars,x=log10(wave), weights=1./power(0.05*ones(wave.size),2))

In [157]:
out.fit_report()


Out[157]:
"[[Model]]\n    ((Model(parabolic, prefix='Quadratic_') + Model(gaussian, prefix='MgII_01_')) + Model(gaussian, prefix='MgII_02_'))\n[[Fit Statistics]]\n    # function evals   = 1072\n    # data points      = 93\n    # variables        = 9\n    chi-square         = 15648.926\n    reduced chi-square = 186.297\n[[Variables]]\n    Quadratic_a:         0.09828356 +/- 0        (0.00%) (init= 0)\n    Quadratic_b:        -0.33970041 +/- 0        (-0.00%) (init= 0)\n    Quadratic_c:         1          +/- 0        (0.00%) (init= 1)\n    MgII_01_sigma:       0.00021328 +/- 0        (0.00%) (init= 0.0002895297)\n    MgII_01_center:      3.44627288 +/- 0        (0.00%) (init= 3.446273)\n    MgII_01_amplitude:  -0.00020439 +/- 0        (-0.00%) (init=-0.0003109544)\n    MgII_01_fwhm:        0.00050224 +/- 0        (0.00%)  == '2.3548200*MgII_01_sigma'\n    MgII_02_amplitude:  -0.00022595 +/- 0        (-0.00%) (init=-0.0003100885)\n    MgII_02_center:      3.44748349 +/- 0        (0.00%) (init= 3.447483)\n    MgII_02_sigma:       0.00020477 +/- 0        (0.00%) (init= 0.0002895297)\n    MgII_02_fwhm:        0.00048219 +/- 0        (0.00%)  == '2.3548200*MgII_02_sigma'\n[[Correlations]] (unreported correlations are <  0.100)"

In [205]:
figsize(10,7)
plot(wave, flux, 'o')
#plot(log10(wave), model.eval(pars,x=log10(wave)))
plot(wave, out.best_fit, lw=2)
xlim(2780, 2810)
plot([2796,2796],[0.5,1.2])


Out[205]:
[<matplotlib.lines.Line2D at 0x1165bded0>]

In [100]:
model


Out[100]:
<lmfit.Model: ((Model(parabolic, prefix='Quadratic_') + Model(gaussian, prefix='MgII_01_')) + Model(gaussian, prefix='MgII_02_'))>

In [101]:
pars


Out[101]:
Parameters([('Quadratic_a', <Parameter 'Quadratic_a', 0.0, bounds=[-0.1:0.1]>), ('Quadratic_b', <Parameter 'Quadratic_b', 0.0, bounds=[-0.5:0.5]>), ('Quadratic_c', <Parameter 'Quadratic_c', 1.0, bounds=[0.9:1.1]>), ('MgII_01_sigma', <Parameter 'MgII_01_sigma', 0.00028952965460216784, bounds=[7.238241365054196e-05:0.0028952965460216787]>), ('MgII_01_center', <Parameter 'MgII_01_center', 3.4464284320994945, bounds=[3.4465836926226077:3.4465837392157339]>), ('MgII_01_amplitude', <Parameter 'MgII_01_amplitude', -0.00015536596226416597, bounds=[-0.0015536596226416598:0]>), ('MgII_01_fwhm', <Parameter 'MgII_01_fwhm', None, bounds=[None:None], expr='2.3548200*MgII_01_sigma'>), ('MgII_02_amplitude', <Parameter 'MgII_02_amplitude', -0.00015498892500896277, bounds=[-0.0015498892500896277:0]>), ('MgII_02_center', <Parameter 'MgII_02_center', 3.4474836452549993, bounds=[3.4476385290639122:3.447638575544008]>), ('MgII_02_sigma', <Parameter 'MgII_02_sigma', 0.00028952965460216784, bounds=[7.238241365054196e-05:0.0028952965460216787]>), ('MgII_02_fwhm', <Parameter 'MgII_02_fwhm', None, bounds=[None:None], expr='2.3548200*MgII_02_sigma'>)])

In [229]:
print((log10(2801.5)-log10(2803.53))/1E-4*69.)


-217.060856909

In [225]:
ew_profile = ebossspec.line_property(log10(wave), flux-1., lines)

In [228]:
x = ravel(ew_profile[1]['WAVE'])
y0 = -ravel(ew_profile[1]['EW'])
y = y0/max(y0)*100.
plot(x, y, '-o')
plot([2798, 2808], [50,50])
plot([2803.53, 2803.53], [0,100])
plot([2801.5, 2801.5], [0,100])


Out[228]:
[<matplotlib.lines.Line2D at 0x11a810d50>]

In [230]:
x = ravel(ew_profile[1]['VEL'])
y0 = -ravel(ew_profile[1]['EW'])
y = y0/max(y0)*100.
plot(x, y, '-o')
plot([-400, 600], [50,50])
plot([-217, -217], [0,100])
plot([0, 0], [0,100])


Out[230]:
[<matplotlib.lines.Line2D at 0x11a947750>]

In [231]:
import fitsio

In [239]:
data = fitsio.read('/Users/Benjamin/AstroData/DEEP2/deep2.ppxf.specdata.dr4_v1.0.fits.gz')

In [269]:
oii1_wave = data['OII_3727_1_WAVE']
oii2_wave = data['OII_3727_2_WAVE']
oii1_flux = data['OII_3727_1_AMP']
oii2_flux = data['OII_3727_2_AMP']
oii1_EW = data['OII_3727_1_EW']
oii2_EW = data['OII_3727_2_EW']
oii_EW = data['OII_3727_EW']

In [273]:
ii = (where(np.logical_and(oii1_EW>0, oii2_EW>0)))[0]
sort_index = argsort(oii_EW[ii])
print(oii1_EW.size, ii.size, oii_EW.size)


(71364, 34431, 71364)

In [276]:
ew_bin = arange(0, 34000, 1000)
ew = zeros(ew_bin.size-1)
ratio = zeros(ew_bin.size-1)
for i in arange(ew.size-1):
    ew[i] = median(oii_EW[ii[sort_index[ew_bin[i]:ew_bin[i+1]+1]]])
    ratio[i] = median(oii2_EW[ii[sort_index[ew_bin[i]:ew_bin[i+1]+1]]]/oii1_EW[ii[sort_index[ew_bin[i]:ew_bin[i+1]+1]]])

#print(ew, ratio)
print(ew, ratio)
fig = figure(figsize=(8,8))
ax = fig.add_subplot(111)
ax.plot(oii_EW[ii], oii2_EW[ii]/oii1_EW[ii], 'o', ms=1)
#ax.plot(ew, ratio, 'o', ms=20)
ax.set_xlim(-5,40)
ax.set_ylim(0,3)
for axis in ['top','bottom','left','right']:
    ax.spines[axis].set_linewidth(2.0)
ax.tick_params(axis='both', which='both', length=7, width=2, labelsize=15)


(array([ 24.6525116,  24.6525116,  24.6525116,  24.6525116,  24.6525116,
        24.6525116,  24.6525116,  24.6525116,  24.6525116,  24.6525116,
        24.6525116,  24.6525116,  24.6525116,  24.6525116,  24.6525116,
        24.6525116,  24.6525116,  24.6525116,  24.6525116,  24.6525116,
        24.6525116,  24.6525116,  24.6525116,  24.6525116,  24.6525116,
        24.6525116,  24.6525116,  24.6525116,  24.6525116,  24.6525116,
        24.6525116,  24.6525116,   0.       ]), array([ 1.43615782,  1.43615782,  1.43615782,  1.43615782,  1.43615782,
        1.43615782,  1.43615782,  1.43615782,  1.43615782,  1.43615782,
        1.43615782,  1.43615782,  1.43615782,  1.43615782,  1.43615782,
        1.43615782,  1.43615782,  1.43615782,  1.43615782,  1.43615782,
        1.43615782,  1.43615782,  1.43615782,  1.43615782,  1.43615782,
        1.43615782,  1.43615782,  1.43615782,  1.43615782,  1.43615782,
        1.43615782,  1.43615782,  0.        ]))

In [277]:
import quicklook

In [283]:
jj = (where(np.logical_and(oii_EW<50, oii_EW>5)))[0]
quicklook.plot_mean(oii_EW, oii2_EW/oii1_EW, xbin=[0,50], ybin=[0,3], statistic='median')


Pearson Coeffiecent = 0.02381
Spearman Coeffiecent = 0.04063
Kendall $\tau$ = 0.02780
Best-fit slope = 0.00090

In [14]:
continuum = lmfit.models.PowerLawModel()
powerlaw = lambda x, amp, index: amp * (x**index)

In [115]:
num_points = 40
amp = 10.0
index = -2.0
#xdata = logspace(log10(1.), log10(10.1), num_points)
xdata = linspace(1.1, 10.1, num_points)
ydata0 = powerlaw(xdata, 10.0, -0.5)
yerr = 0.5*ydata0
ydata = ydata0+randn(num_points)*yerr
ydata


Out[115]:
array([  1.01195718,   7.84382343,   7.00273509,   7.13590693,
         6.16086565,  10.92965526,  10.23797956,   9.10881557,
         6.04784052,   0.43827202,   3.0061936 ,  -2.57669458,
         1.87168575,   7.50633841,   3.4326944 ,   2.52399561,
         4.31967934,   3.98990761,   0.66400301,   3.59867785,
         6.88552861,   0.54613763,   6.99160006,   5.27624941,
         2.33153407,   3.06481235,   3.10759893,   2.99129204,
         3.0562504 ,   7.85514734,   1.73705575,   1.60465816,
         3.10393122,   3.72419949,   2.91321412,   3.59929121,
         4.01464294,   2.60516409,   3.09920053,   3.04489591])

In [113]:
pars = continuum.guess(ydata, x=xdata)
pars['exponent'].set(min=-3, max=-0.1)
pars['amplitude'].set(min=9, max=11.)
out = continuum.fit(ydata, pars, x=xdata, weights=1./yerr)
#out = continuum.fit(ydata, pars, x=xdata)
print(out.fit_report())


[[Model]]
    Model(powerlaw)
[[Fit Statistics]]
    # function evals   = 16
    # data points      = 40
    # variables        = 2
    chi-square         = 30.596
    reduced chi-square = 0.805
[[Variables]]
    exponent:   -0.65816537 +/- 0        (-0.00%) (init=-1.09742)
    amplitude:   11         +/- 0        (0.00%) (init= 11)
[[Correlations]] (unreported correlations are <  0.100)

In [114]:
figsize(15,10)
plot(xdata, out.init_fit, 'k')
plot(xdata, out.best_fit, 'g')
errorbar(xdata, ydata, yerr=yerr, fmt='k^')
xlim(1E0, 1E1)


Out[114]:
(1.0, 10.0)

In [101]:
sum(pow(ydata-ydata0, 2)/pow(yerr,2))/40.


Out[101]:
1.0593696321268324

In [ ]:
def residual(params, x, data, eps_data):
    amp = params['amp'].value
    index = params['index'].value

    model = powerlaw(x, amp, index)

    return (data-model)/eps_data

params = Parameters()
params.add('amp', value=10)
params.add('decay', value=0.007)
params.add('phase', value=0.2)
params.add('frequency', value=3.0)

out = minimize(residual, params, args=(x, data, eps_data))