2012-04-22 105 views
3

我一直在試圖平滑由於我正在使用的採樣率以及它的計數而產生噪聲的圖。我一直在使用這裏的幫助 - 主要是Plot smooth line with PyPlot(雖然我找不到「樣條」功能,所以我使用UnivarinteSpline代替)用Python平滑圖形的問題

但是,無論我做什麼,我總是收到錯誤的pyplot錯誤"x and y are not of the same length",或者scipi.UnivariateSpline的值爲w,這是不正確的。我不確定如何解決這個問題(不是真正的Python人員!)我已經附上了代碼,儘管它只是最後導致問題的繪圖位。由於

import os.path 
import matplotlib.pyplot as plt 
import scipy.interpolate as sci 
import numpy as np 
def main(): 
    jcc = "0050" 
    dj = "005" 
    l = "060" 
    D = 20 
    hT = 4 * D 
    wT1 = 2 * D 
    wT2 = 5 * D 
    for jcm in ["025","030","035","040","045","050","055","060"]: 
     characteristic = "LeadersOnly/Jcm" + jcm + "/Jcc" + jcc + "/dJ" + dj + "/lambda" + l + "/Seed000" 
     fingertime1 = [] 
     fingertime2 = [] 
     stamp =[] 
     finger=[] 
     for x in range(0,2500,50): 
      if x<10000: 
       z=("00"+str(x)) 
      if x<1000: 
       z=("000"+str(x)) 
      if x<100: 
       z=("0000"+str(x)) 
      if x<10: 
       z=("00000"+str(x)) 
      stamp.append(x) 
      path = "LeadersOnly/Jcm" + jcm + "/Jcc" + jcc + "/dJ" + dj + "/lambda" + l + "/Seed000/profile_" + str(z) + ".txt" 
      if os.path.exists(path): 
       f = open(path, 'r') 
       pr1,pr2=np.genfromtxt(path, delimiter='\t', unpack=True) 
       p1=[] 
       p2=[] 
       h1=[] 
       h2=[] 
       a1=[] 
       a2=[] 
       finger1 = 0 
       finger2 = 0 
       for b in range(len(pr1)): 
        p1.append(pr1[b]) 
        p2.append(pr2[b]) 
       for elem in range(len(pr1)-80): 
        h1.append((p1[elem + (2*D)]-0.5*(p1[elem]+p1[elem + (4*D)]))) 
        h2.append((p2[elem + (2*D)]-0.5*(p2[elem]+p2[elem + (4*D)]))) 
        if h1[elem] >= hT: 
         a1.append(1) 
        else: 
         a1.append(0) 
        if h2[elem]>=hT:   
         a2.append(1) 
        else: 
         a2.append(0) 
       for elem in range(len(a1)-1): 
        if (a1[elem] - a1[elem + 1]) != 0: 
         finger1 = finger1 + 1 
       finger1 = finger1/2 
       for elem in range(len(a2)-1): 
        if (a2[elem] - a2[elem + 1]) != 0: 
         finger2 = finger2 + 1 
       finger2 = finger2/2 
       fingertime1.append(finger1) 
       fingertime2.append(finger2) 
       finger.append((finger1+finger2)/2) 
     namegraph = jcm 
     stampnew = np.linspace(stamp[0],stamp[-1],300) 
     fingernew = sci.UnivariateSpline(stamp, finger, stampnew) 
     plt.plot(stampnew,fingernew,label=namegraph) 
    plt.show()  

main() 

的信息,數據輸入文件是簡單的整數列表(兩份名單由製表符分隔,作爲代碼提示)。

以下是錯誤代碼,我得到的一個:

0-th dimension must be fixed to 50 but got 300 

error          Traceback (most recent call last) 

/group/data/Cara/JCMMOTFingers/fingercount_jcm_smooth.py in <module>() 
    116 
    117 if __name__ == '__main__': 
--> 118  main() 
    119 
    120 

/group/data/Cara/JCMMOTFingers/fingercount_jcm_smooth.py in main() 
    93     #print(len(stamp)) 
    94     stampnew = np.linspace(stamp[0],stamp[-1],300) 
---> 95     fingernew = sci.UnivariateSpline(stamp, finger, stampnew) 
    96     #print(len(stampnew)) 
    97     #print(len(fingernew)) 

/usr/lib/python2.6/dist-packages/scipy/interpolate/fitpack2.pyc in __init__(self, x, y, w, bbox, k, s) 
    86   #_data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier 
    87   data = dfitpack.fpcurf0(x,y,k,w=w, 
---> 88         xb=bbox[0],xe=bbox[1],s=s) 
    89   if data[-1]==1: 
    90    # nest too small, setting to maximum bound 

error: failed in converting 1st keyword `w' of dfitpack.fpcurf0 to C/Fortran array 
+0

如果沒有LeadersOnly文件夾,我們無法複製,但如果您經過了所獲得的回溯的確切副本,我們可能會弄清楚。另外,我對代碼運行的縮進做了一些調整。 – 2012-04-22 16:33:36

+0

不確定解決方案 - 但請將您的0填充更改爲更好!初始化z的行可以替換爲'z ='%06d'%x' – 2012-04-22 16:37:42

+0

已添加到我得到的錯誤鏈之一中。... 如果我通過更改爲stampnew = np .linspace(圖章[0],圖章[-1],50),但隨後會出現更多錯誤,這次是關於pyplot函數 – Cara 2012-04-22 20:31:19

回答

4

我們來分析一下你的代碼了一下,從for x in range(0, 2500, 50):

  • 開始你定義z爲6位數字的字符串填充與0s。你應該使用一些字符串格式,如z = "{0:06d}".format(x)z = "%06d" % x,而不是這些測試。

  • 在循環結束時,stamp將具有​​3210元素。

  • 您檢查您的文件path是否存在,然後打開並閱讀它,但您永遠不會關閉它。更Python的方式是做:

    try: 
        with open(path,"r") as f: 
         do... 
    except IOError: 
        do something else 
    

    隨着with語法,你的文件會自動關閉。

  • pr1 and pr2很可能是1D數組,對不對?你真的可以簡化您的p1p2名單的建設爲:

    p1 = pr1.tolist() 
    p2 = pr2.tolist() 
    
  • 你的列表a1a2具有相同的大小:你可以在一個單一的一個結合您for elem in range(len(a..)-1)循環。您也可以使用np.diff函數。

  • for x in range(...)循環的結束,finger將有50個元素減去丟失的文件的數量。由於您不知道在缺少文件的情況下應採取的措施,因此您的stampfinger列表中可能沒有相同數量的元素,這會使您的scipy.UnivariateSpline崩潰。只有在定義了path文件(這樣,它總是具有與finger相同數量的元素)的情況下,才能更新您的stamp列表。

  • 您的stampnew數組有300個元素,當您的stampfinger最多隻能有50個元素。這是第二個問題,權重數組的大小(stampnew)必須與輸入的大小相同。

  • 你最終想繪製​​vs stamp。問題是​​是而不是陣列,它是UnivariateSpline實例。您仍然需要計算一些實際點數,例如fingernew(stamp),然後在您的plot函數中使用。