예제 #1
0
def threeGaussFit(TR, guess=(3000, 20, 3000, 25, 3000, 30), mode=None):
    x = TR.x
    n = TR.n
    params, success = leastsq(threeGaussErr, guess, args=(x, n))
    A1, mu1, A2, mu2, A3, mu3 = params
    sig1 = ttC.sigofmu(mu1)
    sig2 = ttC.sigofmu(mu2)
    sig3 = ttC.sigofmu(mu3)
    if mode == 'vocal':
        print 'Parameters for triple Gaussian fit to whole histogram:'
        print 'A1: ' + str('%d' % A1)
        print 'sig1: ' + str('%.2f' % sig1)
        print 'mu1: ' + str('%.2f' % mu1) + '\n'
        print 'A2: ' + str('%d' % A2)
        print 'sig2: ' + str('%.2f' % sig2)
        print 'mu2: ' + str('%.2f' % mu2) + '\n'
        print 'A3: ' + str('%d' % A3)
        print 'sig3: ' + str('%.2f' % sig3)
        print 'mu3: ' + str('%.2f' % mu3) + '\n'

    tot = (params[0] + params[2] + params[4])
    #make sure means are in the right order:
    if params[1] > params[3]:
        A1, sig1, mu1, A2, sig2, mu2 = (A2, sig2, mu2, A1, sig1, mu1)
        params = (A1, mu1, A2, mu2, A3, mu3)
    if params[1] > params[5]:
        A1, sig1, mu1, A2, sig2, mu2, A3, sig3, mu3 = (A3, sig3, mu3, A1, sig1,
                                                       mu1, A2, sig2, mu2)
        params = (A1, mu1, A2, mu2, A3, mu3)
    elif params[3] > params[5]:
        A2, sig2, mu2, A3, sig3, mu3 = (A3, sig3, mu3, A2, sig2, mu2)
        params = (A1, mu1, A2, mu2, A3, mu3)
    if params[1] > params[3] or params[1] > params[5] or params[3] > params[5]:
        print 'the means are not in order: you need to alter the code to get this right'
    return params
예제 #2
0
def threeGauss(p,x):
  A1, mu1, A2, mu2, A3, mu3 = p
  sig1=ttC.sigofmu(mu1)
  sig2=ttC.sigofmu(mu2)
  sig3=ttC.sigofmu(mu3)
  G1=(A1/(sig1*np.sqrt(2*np.pi)))*np.exp(-0.5*(x-mu1)*(x-mu1)/(sig1*sig1))
  G2=(A2/(sig2*np.sqrt(2*np.pi)))*np.exp(-0.5*(x-mu2)*(x-mu2)/(sig2*sig2))
  G3=(A3/(sig3*np.sqrt(2*np.pi)))*np.exp(-0.5*(x-mu3)*(x-mu3)/(sig3*sig3))
  return G1+G2+G3
예제 #3
0
def ranges(params):
    A1, mu1, A2, mu2, A3, mu3 = params
    sig1 = ttC.sigofmu(mu1)
    sig2 = ttC.sigofmu(mu2)
    sig3 = ttC.sigofmu(mu3)
    IrangeNEG = (0, mu2 - 2 * sig2)
    IrangeNEU = (mu2 - sig2, mu2 + sig2)
    IrangePOS = (mu2 + 2 * sig2, mu3 + 8 * sig3)
    #NEG integrals
    NEGneg = A1 * (scipy.special.erf(
        (IrangeNEG[1] - mu1) / (np.sqrt(2) * sig1)) - scipy.special.erf(
            (IrangeNEG[0] - mu1) / (np.sqrt(2) * sig1)))
    NEGneu = A2 * (scipy.special.erf(
        (IrangeNEG[1] - mu2) / (np.sqrt(2) * sig2)) - scipy.special.erf(
            (IrangeNEG[0] - mu2) / (np.sqrt(2) * sig2)))
    NEGpos = A3 * (scipy.special.erf(
        (IrangeNEG[1] - mu3) / (np.sqrt(2) * sig3)) - scipy.special.erf(
            (IrangeNEG[0] - mu3) / (np.sqrt(2) * sig3)))
    tot = NEGneg + NEGneu + NEGpos
    PoNEG = [NEGneg / tot, NEGneu / tot, NEGpos / tot]
    #NEU integrals
    NEUneg = A1 * (scipy.special.erf(
        (IrangeNEU[1] - mu1) / (np.sqrt(2) * sig1)) - scipy.special.erf(
            (IrangeNEU[0] - mu1) / (np.sqrt(2) * sig1)))
    NEUneu = A2 * (scipy.special.erf(
        (IrangeNEU[1] - mu2) / (np.sqrt(2) * sig2)) - scipy.special.erf(
            (IrangeNEU[0] - mu2) / (np.sqrt(2) * sig2)))
    NEUpos = A3 * (scipy.special.erf(
        (IrangeNEU[1] - mu3) / (np.sqrt(2) * sig3)) - scipy.special.erf(
            (IrangeNEU[0] - mu3) / (np.sqrt(2) * sig3)))
    tot = NEUneg + NEUneu + NEUpos
    PoNEU = [NEUneg / tot, NEUneu / tot, NEUpos / tot]
    #POS integrals
    POSneg = A1 * (scipy.special.erf(
        (IrangePOS[1] - mu1) / (np.sqrt(2) * sig1)) - scipy.special.erf(
            (IrangePOS[0] - mu1) / (np.sqrt(2) * sig1)))
    POSneu = A2 * (scipy.special.erf(
        (IrangePOS[1] - mu2) / (np.sqrt(2) * sig2)) - scipy.special.erf(
            (IrangePOS[0] - mu2) / (np.sqrt(2) * sig2)))
    POSpos = A3 * (scipy.special.erf(
        (IrangePOS[1] - mu3) / (np.sqrt(2) * sig3)) - scipy.special.erf(
            (IrangePOS[0] - mu3) / (np.sqrt(2) * sig3)))
    tot = POSneg + POSneu + POSpos
    PoPOS = [POSneg / tot, POSneu / tot, POSpos / tot]

    PoALL = (PoNEG, PoNEU, PoPOS)
    return (IrangeNEG, IrangeNEU, IrangePOS, PoALL)
예제 #4
0
def threeGaussFit(TR):
  print len(TR.peak)

  if len(TR.peak)>0:
    if len(TR.peak)==1:
      sig=[ttC.sigofmu(TR.peak[0])]
      popt,pcov=curve_fit(oneGauss2,TR.x,TR.n,p0=[TR.peak[0],TR.amp[0],sig[0]])
      TR.popt.append(popt[0])
      TR.popt.append(popt[1])
      TR.popt.append(popt[2])
      pb.plot(TR.x,oneGauss2(TR.x,popt[0],popt[1],popt[2]),color=col4,linewidth=1)
      pb.show()
      TR.n=TR.n-oneGauss2(TR.x,popt[0],popt[1],popt[2])

    elif len(TR.peak)==2:
      sig=[ttC.sigofmu(TR.peak[0]),ttC.sigofmu(TR.peak[1])]
      popt,pcov=curve_fit(twoGauss2,TR.x,TR.n,p0=[TR.peak[0],TR.amp[0],sig[0],TR.peak[1],TR.amp[1],sig[1]])

      for i in range(6):
        TR.popt.append(popt[i])
  
      pb.plot(TR.x,twoGauss2(TR.x,popt[0],popt[1],popt[2],popt[3],popt[4],popt[5]),color=col4,linewidth=1)
      pb.show()
      TR.n=TR.n-twoGauss2(TR.x,popt[0],popt[1],popt[2],popt[3],popt[4],popt[5])

    else:
      sig=[ttC.sigofmu(TR.peak[0]),ttC.sigofmu(TR.peak[1]),ttC.sigofmu(TR.peak[2])]
      popt,pcov=curve_fit(threeGauss2,TR.x,TR.n,p0=[TR.peak[0],TR.amp[0],sig[0],TR.peak[1],TR.amp[1],sig[1],TR.peak[2],TR.amp[2],sig[2]])
      for i in range(9):
        TR.popt.append(popt[i])
  
      pb.plot(TR.x,threeGauss2(TR.x,popt[0],popt[1],popt[2],popt[3],popt[4],popt[5],popt[6],popt[7],popt[8]),color=col4,linewidth=1)
      pb.show()
      TR.n=TR.n-threeGauss2(TR.x,popt[0],popt[1],popt[2],popt[3],popt[4],popt[5],popt[6],popt[7],popt[8])
예제 #5
0
def hidden_peak(TR,filename):
  window_len=11
  window='hanning'
  s=np.r_[TR.n[window_len-1:0:-1],TR.n,TR.n[-1:-window_len:-1]]
  w=eval('np.'+window+'(window_len)')
  TR.y=np.convolve(w/w.sum(),s,mode='valid')
  TR.y=TR.y[5:-5]
  pk=np.r_[1, TR.y[1:] > TR.y[:-1]] & np.r_[TR.y[:-1] > TR.y[1:],1]

  x=TR.x
  n=TR.n
 
  peak=[]
  amp=[]

  for i in np.where(pk==1)[0]:
    if TR.y[int(i)]>TR.sens:
      peak.append(TR.x[int(i)])
      amp.append(TR.y[int(i)])

  if len(peak)>0:

    if len(peak)==1:
      sig=[ttC.sigofmu(peak[0])]
      popt,pcov=curve_fit(oneGauss2,x,n,p0=[peak[0],amp[0],sig[0]])
      TR.popt.append(popt[0])
      TR.popt.append(popt[1])
      TR.popt.append(popt[2])

    elif len(peak)==2:
      sig=[ttC.sigofmu(peak[0]),ttC.sigofmu(peak[1])]
      popt,pcov=curve_fit(twoGauss2,x,n,p0=[peak[0],amp[0],sig[0],peak[1],amp[1],sig[1]])

      for i in range(6):
        TR.popt.append(popt[i])
    
    else:
      sig=[ttC.sigofmu(peak[0]),ttC.sigofmu(peak[1]),ttC.sigofmu(peak[2])]
      popt,pcov=curve_fit(threeGauss2,x,n,p0=[peak[0],amp[0],sig[0],peak[1],amp[1],sig[1],peak[2],amp[2],sig[2]])
 
      for i in range(9):
        TR.popt.append(popt[i])
#
###
  pb.plot(x,nGauss2(x,TR.popt),color=col3,linewidth=1)
###
  i=re.search('[0-9]+',filename)
  pb.savefig('figures/'+i.group()+'.png')
  pb.clf()

  return TR.popt