示例#1
0
def fit_peak(plotdict):
    x = plotdict['data']['x']
    y = plotdict['data']['y']
    yerr = plotdict['data']['yerr']
    kernel = findpeak.find_kernel(y)
    npeaks, nlist, plist = findpeak.find_npeaks(x, y, yerr, kernel, nmax=2)
    results = findpeak.findpeak(x, y, npeaks, kernel=kernel)
    fwhm = findpeak.findwidths(x, y, npeaks, results['xpeaks'],
                               results['indices'])
    sigma = fwhm / 2.354
    p0 = [0, 0]
    pb = N.concatenate((results['xpeaks'], fwhm,
                        results['heights'] * N.sqrt(2 * pi * sigma**2)))
    pb = N.array(pb).flatten()
    p0 = N.concatenate((p0, pb)).flatten()
    print 'p0', p0
    #
    fresults = scipy.optimize.leastsq(findpeak.cost_func,
                                      p0,
                                      args=(x, y, yerr),
                                      full_output=1)
    p1 = fresults[0]
    covariance = fresults[1]

    parbase = {'value': 0., 'fixed': 0, 'limited': [0, 0], 'limits': [0., 0.]}
    parinfo = []
    for i in range(len(p0)):
        parinfo.append(copy.deepcopy(parbase))
    for i in range(len(p0)):
        parinfo[i]['value'] = p0[i]
    parinfo[1]['fixed'] = 1  #fix slope
    fa = {'x': x, 'y': y, 'err': yerr}
    m = mpfit(findpeak.myfunctlin, p0, parinfo=parinfo, functkw=fa)
    print 'status = ', m.status
    print 'params = ', m.params
    p1 = m.params
    covariance = m.covar

    dof = len(y) - len(p1)
    fake_dof = len(y)
    chimin = (findpeak.cost_func(p1, x, y, yerr)**2).sum()
    chimin = chimin / dof if dof > 0 else chimin / fake_dof
    covariance = covariance * chimin  #assume our model is good

    area = N.array(N.abs(p1[2 + 2 * npeaks::]))
    area_sig = covariance.diagonal()[2 + 2 * npeaks::]
    fwhm = N.array(N.abs(p1[2 + npeaks:2 + 2 * npeaks]))

    ycalc = findpeak.gen_function(p1, x)
    fitdict = {}
    fitdict['x'] = x
    fitdict['y'] = ycalc
    fitdict['area'] = area.sum()
    fitdict['chi'] = chimin
    fitdict['area_err'] = N.sqrt(area_sig.sum())
    print 'area', fitdict['area']
    #next add the fit results
    print 'chi', chimin
    return fitdict
示例#2
0
def fit_peak(plotdict):
    x=plotdict['data']['x']
    y=plotdict['data']['y']
    yerr=plotdict['data']['yerr']
    kernel=findpeak.find_kernel(y)
    npeaks,nlist,plist=findpeak.find_npeaks(x,y,yerr,kernel,nmax=2)
    results=findpeak.findpeak(x,y,npeaks,kernel=kernel)
    fwhm=findpeak.findwidths(x,y,npeaks,results['xpeaks'],results['indices'])
    sigma=fwhm/2.354
    p0=[0,0]
    pb=N.concatenate((results['xpeaks'], fwhm, results['heights']*N.sqrt(2*pi*sigma**2)))
    pb=N.array(pb).flatten()
    p0=N.concatenate((p0,pb)).flatten()
    print 'p0',p0
    #
    fresults= scipy.optimize.leastsq(findpeak.cost_func, p0, args=(x,y,yerr),full_output=1)
    p1=fresults[0]
    covariance=fresults[1]

    parbase={'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}
    parinfo=[]
    for i in range(len(p0)):
        parinfo.append(copy.deepcopy(parbase))
    for i in range(len(p0)):
        parinfo[i]['value']=p0[i]
    parinfo[1]['fixed']=1 #fix slope
    fa = {'x':x, 'y':y, 'err':yerr}
    m = mpfit(findpeak.myfunctlin, p0, parinfo=parinfo,functkw=fa)
    print 'status = ', m.status
    print 'params = ', m.params
    p1=m.params
    covariance=m.covar

    dof=len(y)-len(p1)
    fake_dof=len(y)
    chimin=(findpeak.cost_func(p1,x,y,yerr)**2).sum()
    chimin=chimin/dof if dof>0 else chimin/fake_dof
    covariance=covariance*chimin #assume our model is good



    area=N.array(N.abs(p1[2+2*npeaks::]))
    area_sig=covariance.diagonal()[2+2*npeaks::]
    fwhm=N.array(N.abs(p1[2+npeaks:2+2*npeaks]))



    ycalc=findpeak.gen_function(p1,x)
    fitdict={}
    fitdict['x']=x
    fitdict['y']=ycalc
    fitdict['area']=area.sum()
    fitdict['chi']=chimin
    fitdict['area_err']=N.sqrt(area_sig.sum())
    print 'area',fitdict['area']
    #next add the fit results
    print 'chi',chimin
    return fitdict
示例#3
0
def fit_peak(plotdict):
    x = plotdict["data"]["x"]
    y = plotdict["data"]["y"]
    yerr = plotdict["data"]["yerr"]
    kernel = findpeak.find_kernel(y)
    npeaks, nlist, plist = findpeak.find_npeaks(x, y, yerr, kernel, nmax=2)
    results = findpeak.findpeak(x, y, npeaks, kernel=kernel)
    fwhm = findpeak.findwidths(x, y, npeaks, results["xpeaks"], results["indices"])
    sigma = fwhm / 2.354
    p0 = [0, 0]
    pb = N.concatenate((results["xpeaks"], fwhm, results["heights"] * N.sqrt(2 * pi * sigma ** 2)))
    pb = N.array(pb).flatten()
    p0 = N.concatenate((p0, pb)).flatten()
    print "p0", p0
    #
    fresults = scipy.optimize.leastsq(findpeak.cost_func, p0, args=(x, y, yerr), full_output=1)
    p1 = fresults[0]
    covariance = fresults[1]

    parbase = {"value": 0.0, "fixed": 0, "limited": [0, 0], "limits": [0.0, 0.0]}
    parinfo = []
    for i in range(len(p0)):
        parinfo.append(copy.deepcopy(parbase))
    for i in range(len(p0)):
        parinfo[i]["value"] = p0[i]
    parinfo[1]["fixed"] = 1  # fix slope
    fa = {"x": x, "y": y, "err": yerr}
    m = mpfit(findpeak.myfunctlin, p0, parinfo=parinfo, functkw=fa)
    print "status = ", m.status
    print "params = ", m.params
    p1 = m.params
    covariance = m.covar

    dof = len(y) - len(p1)
    fake_dof = len(y)
    chimin = (findpeak.cost_func(p1, x, y, yerr) ** 2).sum()
    chimin = chimin / dof if dof > 0 else chimin / fake_dof
    covariance = covariance * chimin  # assume our model is good

    area = N.array(N.abs(p1[2 + 2 * npeaks : :]))
    area_sig = covariance.diagonal()[2 + 2 * npeaks : :]
    fwhm = N.array(N.abs(p1[2 + npeaks : 2 + 2 * npeaks]))

    ycalc = findpeak.gen_function(p1, x)
    fitdict = {}
    fitdict["x"] = x
    fitdict["y"] = ycalc
    fitdict["area"] = area.sum()
    fitdict["chi"] = chimin
    fitdict["area_err"] = N.sqrt(area_sig.sum())
    print "area", fitdict["area"]
    # next add the fit results
    print "chi", chimin
    return fitdict
示例#4
0
文件: refine.py 项目: ourobouros/WRed
    }

    lowerm = [0, 0]
    upperm = [100, pi / 2]
    p0,jmin=anneal(chisq_an,p0,args=(Hr,Kr,Lr,d,q,alphastar,astar,lattice,Hh,Kh,Lh,y,yerr),\
                      schedule='simple',lower=lowerm,upper=upperm,\
                      maxeval=None, maxaccept=None,dwell=500,maxiter=6000,T0=None)
    dof = len(y) - len(p0)
    fake_dof = len(y)
    chimin = (cost_func(p0, Hr, Kr, Lr, d, q, alphastar, astar, lattice, Hh,
                        Kh, Lh, y, yerr)**2).sum()
    chimin = chimin / dof if dof > 0 else chimin / fake_dof
    print 'p0_anneal', p0[0], N.degrees(p0[1])
    print 'chi_anneal', chimin

    m = mpfit(myfunctlin, p0, parinfo=parinfo, functkw=fa)
    print 'status = ', m.status
    print 'params = ', m.params
    p1 = m.params
    covariance = m.covar

    dof = len(y) - len(p1)
    fake_dof = len(y)
    chimin = (cost_func(p1, Hr, Kr, Lr, d, q, alphastar, astar, lattice, Hh,
                        Kh, Lh, y, yerr)**2).sum()
    chimin = chimin / dof if dof > 0 else chimin / fake_dof
    ycalc = calc_struct(p1, Hr, Kr, Lr, d, q, alphastar, astar, lattice, Hh,
                        Kh, Lh)
    print 'chimin', chimin
    print 'p1', p1
    covariance = covariance * chimin  #assume our model is good
示例#5
0
 lowerm=[0,0,0,0,0]
 upperm=[100,pi,pi/2,2*pi,pi]
 p0,jmin=anneal(chisq_an,p0,args=(Hr,Kr,Lr,d,q,alphastar,astar,lattice,Hh,Kh,Lh,y,yerr),\
                   schedule='simple',lower=lowerm,upper=upperm,\
                   maxeval=None, maxaccept=None,dwell=50,maxiter=60,T0=None)
 dof=len(y)-len(p0)
 fake_dof=len(y)
 chimin=(cost_func(p0,Hr,Kr,Lr,d,q,alphastar,astar,lattice,Hh,Kh,Lh,y,yerr)**2).sum()
 chimin=chimin/dof if dof>0 else chimin/fake_dof 
 print 'p0_anneal',p0[0],N.degrees(p0[1:])
 print 'chi_anneal', chimin
 
 
 
 
 m = mpfit(myfunctlin, p0, parinfo=parinfo,functkw=fa) 
 print 'status = ', m.status
 print 'params = ', m.params
 p1=m.params
 covariance=m.covar
 
 dof=len(y)-len(p1)
 fake_dof=len(y)
 chimin=(cost_func(p1,Hr,Kr,Lr,d,q,alphastar,astar,lattice,Hh,Kh,Lh,y,yerr)**2).sum()
 chimin=chimin/dof if dof>0 else chimin/fake_dof
 ycalc=calc_struct(p1,Hr,Kr,Lr,d,q,alphastar,astar,lattice,Hh,Kh,Lh)
 print 'chimin',chimin
 print 'p1',p1
 covariance=covariance*chimin #assume our model is good       
 scale=N.abs(p1[0])
 scale_sig=N.sqrt(covariance.diagonal()[0])
示例#6
0
def driver(x,y):
    kernel=31
    if 1:
        kern,DW=optimize_DW(y) #choose the right window size
        #pylab.plot(kern,DW,'s')
        #pylab.show()
        kernel=min(N.abs(kern-2))





    npeaks=2
    nlist=[]
    plist=[]
    for npeaks in range(1,10):
        print 'npeaks',npeaks
        results=findpeak(x,y,npeaks,kernel=31)
        fwhm=findwidths(x,y,npeaks,results['xpeaks'],results['indices'])
        print 'res',results['xpeaks']
        print 'fwhm',fwhm
        print 'heights',results['heights']
        p0=[0,0]

        #results['heights']=[1000,500,1000]
        #fwhm=[.1,.2,.4]
        sigma=fwhm/2.354
        pb=N.concatenate((results['xpeaks'], fwhm, results['heights']*N.sqrt(2*pi*sigma**2)))
        pb=N.array(pb).flatten()
        p0=N.concatenate((p0,pb)).flatten()
        print 'p0',p0
        ycalc=gen_function(p0,x)
        if 1:
            results= scipy.optimize.leastsq(cost_func, p0, args=(x,y,yerr),full_output=1)
            p1=results[0]
            covariance=results[1]
            print 'p1',p1


        if 0:
            parbase={'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}
            parinfo=[]
            for i in range(len(p0)):
                parinfo.append(copy.deepcopy(parbase))
            for i in range(len(p0)):
                parinfo[i]['value']=p0[i]
            fa = {'x':x, 'y':y, 'err':yerr}
            m = mpfit(myfunctlin, p0, parinfo=parinfo,functkw=fa)
            print 'status = ', m.status
            print 'params = ', m.params
            p1=m.params
            covariance=m.covar

        if 1:
            area=max(N.abs(p1[-3::]))
            sigma=p1[-6:-3]/2.354
            amax=max(area/N.sqrt(2*pi*sigma**2))

            chimin=(cost_func(p1,x,y,yerr)**2).sum()
            rangex=max(x)-min(x)
            prob=calc_prob(npeaks,amax,covariance,chimin,rangex)
            print 'prob',prob
            nlist.append(npeaks)
            plist.append(prob)
            #sys.exit()

    pylab.semilogy(nlist,plist,'s')
    pylab.show()
    if 0:
        pylab.plot(x,y,'s')
        pylab.axis([0,2,0,1.4e4])

        for i in range(npeaks):
            pylab.axvline(x=results['xpeaks'][i])
            xcen=results['xpeaks'][i]
            half_height=y[results['indices'][i]]/2
            pylab.plot([(xcen-fwhm[i]/2),(xcen+fwhm[i]/2)],[half_height,half_height])
            ycalc=gen_function(p1,x)
            pylab.plot(x,ycalc)

        pylab.axis([0,2,0,1.4e4])
        pylab.show()
示例#7
0
def driver(x, y):
    kernel = 31
    if 1:
        kern, DW = optimize_DW(y)  #choose the right window size
        #pylab.plot(kern,DW,'s')
        #pylab.show()
        kernel = min(N.abs(kern - 2))

    npeaks = 2
    nlist = []
    plist = []
    for npeaks in range(1, 10):
        print 'npeaks', npeaks
        results = findpeak(x, y, npeaks, kernel=31)
        fwhm = findwidths(x, y, npeaks, results['xpeaks'], results['indices'])
        print 'res', results['xpeaks']
        print 'fwhm', fwhm
        print 'heights', results['heights']
        p0 = [0, 0]

        #results['heights']=[1000,500,1000]
        #fwhm=[.1,.2,.4]
        sigma = fwhm / 2.354
        pb = N.concatenate((results['xpeaks'], fwhm,
                            results['heights'] * N.sqrt(2 * pi * sigma**2)))
        pb = N.array(pb).flatten()
        p0 = N.concatenate((p0, pb)).flatten()
        print 'p0', p0
        ycalc = gen_function(p0, x)
        if 1:
            results = scipy.optimize.leastsq(cost_func,
                                             p0,
                                             args=(x, y, yerr),
                                             full_output=1)
            p1 = results[0]
            covariance = results[1]
            print 'p1', p1

        if 0:
            parbase = {
                'value': 0.,
                'fixed': 0,
                'limited': [0, 0],
                'limits': [0., 0.]
            }
            parinfo = []
            for i in range(len(p0)):
                parinfo.append(copy.deepcopy(parbase))
            for i in range(len(p0)):
                parinfo[i]['value'] = p0[i]
            fa = {'x': x, 'y': y, 'err': yerr}
            m = mpfit(myfunctlin, p0, parinfo=parinfo, functkw=fa)
            print 'status = ', m.status
            print 'params = ', m.params
            p1 = m.params
            covariance = m.covar

        if 1:
            area = max(N.abs(p1[-3::]))
            sigma = p1[-6:-3] / 2.354
            amax = max(area / N.sqrt(2 * pi * sigma**2))

            chimin = (cost_func(p1, x, y, yerr)**2).sum()
            rangex = max(x) - min(x)
            prob = calc_prob(npeaks, amax, covariance, chimin, rangex)
            print 'prob', prob
            nlist.append(npeaks)
            plist.append(prob)
            #sys.exit()

    pylab.semilogy(nlist, plist, 's')
    pylab.show()
    if 0:
        pylab.plot(x, y, 's')
        pylab.axis([0, 2, 0, 1.4e4])

        for i in range(npeaks):
            pylab.axvline(x=results['xpeaks'][i])
            xcen = results['xpeaks'][i]
            half_height = y[results['indices'][i]] / 2
            pylab.plot([(xcen - fwhm[i] / 2), (xcen + fwhm[i] / 2)],
                       [half_height, half_height])
            ycalc = gen_function(p1, x)
            pylab.plot(x, ycalc)

        pylab.axis([0, 2, 0, 1.4e4])
        pylab.show()