Advertisement
Guest User

Plotting script 21-12-12

a guest
Dec 21st, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. #Fabio Eboli 2012
  2.  
  3. from numpy import arange,array,ones
  4. import pylab
  5. from pylab import *
  6. from scipy import stats
  7. import time
  8.  
  9. #pylab.ion()
  10. fig=pylab.figure(1)
  11. sp1=fig.add_subplot(221)
  12. sp2=fig.add_subplot(222)
  13. sp3=fig.add_subplot(223)
  14. sp4=fig.add_subplot(224)
  15.  
  16. manager=pylab.get_current_fig_manager()
  17.  
  18. data=[0]
  19. xdata=[0]
  20. datapos=[0]
  21. correctionvalues=[0]
  22. dataunstep=[0]
  23. dataundrift=[0]
  24. correctionvalues=[0]
  25. driftslope=0
  26. driftintercept=0
  27. numsamples=0
  28. outliers=0
  29.  
  30. p1=sp1.plot(xdata,data,"r.")
  31. sp1.set_title("Raw Data, outliers removed")
  32. p2=sp2.plot(xdata,correctionvalues,"b")
  33. sp2.set_title("Step correction values")
  34. p3=sp3.plot(xdata,dataunstep,"g")
  35. p31=sp3.plot([0,len(dataunstep)],[driftintercept,driftintercept+driftslope*len(dataunstep)],"g")
  36. sp3.set_title("Steps removed and lin. drift est")
  37. p4=sp4.plot(xdata,dataundrift,"y")
  38. sp4.set_title("Drift corrected")
  39.  
  40. def Calculations(arg):
  41.     global data
  42.     global datapos
  43.     global xdata
  44.     global dataunstep
  45.     global dataundrift
  46.     global driftslope
  47.     global driftintercept
  48.     global correctionvalues
  49.     global numsamples
  50.     global outliers
  51.     data=[]
  52.     xdata=[]
  53.     datapos=[]
  54.     correctionsteps=[]
  55.     correctionvalues=[]
  56.     dataundrift=[]
  57.     dataunstep=[]
  58.     stepvalue=1e-7
  59.     threshold=5e-8
  60.     correctionstep=0
  61.     outliers=0
  62.     logfile=open("..\\TI.txt")
  63.     header=logfile.readline()
  64.     for lines in logfile:
  65.         data.append(float(lines))
  66.     logfile.close()
  67.     numsamples=len(data)
  68.     xdata=arange(0,numsamples)
  69.     print(str(numsamples)+" Samples")
  70.     print("Removing negative values...")
  71.     for i in data:
  72.         if i<0 :
  73.             datapos.append(i+1e-7)
  74.         else :
  75.             datapos.append(i)
  76.     print("Estimating steps correction and removing ouliers...")
  77.     xi=arange(1,len(datapos))
  78.     correctionsteps.append(0)
  79.     correctionvalues.append(0)
  80.     dataunstep.append(datapos[0])
  81.     for i in xi:
  82.         olddata=datapos[i-1]
  83.         newdata=datapos[i]
  84.         diff=newdata-olddata
  85.         correctionstep=0
  86.         if diff>0:
  87.             if diff> 3*threshold:
  88.                 datapos[i]=datapos[i-1] # filetring out values
  89.                 outliers=outliers+1
  90.             else:
  91.                 while diff>threshold:
  92.                     correctionstep=correctionstep-1
  93.                     diff=newdata+correctionstep*stepvalue-olddata
  94.         else :
  95.             if diff< -3*threshold:
  96.                 datapos[i]=datapos[i-1] # filetring out values
  97.                 outliers=outliers+1
  98.             else:
  99.                 while diff<-threshold:
  100.                     correctionstep=correctionstep+1
  101.                     diff=newdata+correctionstep*stepvalue-olddata
  102.         correctionsteps.append(correctionstep)
  103.         correctionvalues.append(correctionvalues[i-1]+correctionstep*stepvalue)
  104.         dataunstep.append(datapos[i]+correctionvalues[i])
  105.     xi=arange(0,len(dataunstep))
  106.     print("Drift estimation:")
  107.     driftline=stats.linregress(xi,dataunstep)
  108.     driftslope=driftline[0]
  109.     driftintercept=driftline[1]
  110.     print("Slope="+str(driftslope)+"; Intercept:"+str(driftintercept))
  111.     print("Drift correction...")
  112.     for i in xi:
  113.         dataundrift.append(dataunstep[i]-driftslope*i)
  114.  
  115. def Plotter(arg):
  116.     global datapos
  117.     global dataunstep
  118.     global dataundrift
  119.     global driftslope
  120.     global driftintercept
  121.     global correctionvalues
  122.     global numsamples
  123.     global outliers
  124.     p1[0].set_data(arange(0,len(datapos)),datapos)
  125.     sp1.axis([0,len(datapos),min(datapos),max(datapos)])
  126.     sp1.set_title("Raw Data: "+str(numsamples)+"samples, "+str(outliers)+" outliers removed")
  127.     p2[0].set_data(arange(0,len(correctionvalues)),correctionvalues)
  128.     sp2.axis([0,len(correctionvalues),min(correctionvalues),max(correctionvalues)])
  129.     sp2.set_title("Step correction values")
  130.     p3[0].set_data(arange(0,len(dataunstep)),dataunstep)
  131.     p31[0].set_data([0,len(dataunstep)],[driftintercept,driftintercept+driftslope*len(dataunstep)])
  132.     sp3.axis([0,len(dataunstep),min(dataunstep),max(dataunstep)])
  133.     sp3.set_title("Steps removed and lin. drift est. : "+str(driftslope))
  134.     p4[0].set_data(arange(0,len(dataundrift)),dataundrift)
  135.     sp4.axis([0,len(dataundrift),min(dataundrift),max(dataundrift)])
  136.     sp4.set_title("Drift Removed")
  137.     manager.canvas.draw()
  138.  
  139. Calculations(0)
  140. Plotter(0)
  141.  
  142. #time.sleep(20)
  143. timer1=fig.canvas.new_timer(interval=50000)
  144. timer1.add_callback(Calculations,())
  145. timer=fig.canvas.new_timer(interval=50000)
  146. timer.add_callback(Plotter,())
  147. timer.start()
  148. timer1.start()
  149. pylab.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement