2015-11-06 132 views
0

我已經寫了一個函數,對數據集進行一些組平均操作。當我調用它運行的函數時,返回被繪製的數據。在此通話期間,我收到一條警告:Spyder控制檯鎖定引起的「IndexError:數組索引太多」

warnings.warn("Warning: converting a masked element to nan.") 

這看起來不會影響函數調用。然而,函數返回後,發生了一些事情,它們會鎖定運行python的控制檯(python 2.7,spyder 2.3.5.2,windows 7)。在鎖定時沒有錯誤追蹤,但是當我從這個鎖定恢復時,我得到下面給出的錯誤追蹤。這個「索引錯誤」似乎在Python核心發生,我不知道如何追溯到我的代碼。任何人都可以建議如何確定此錯誤的來源。

了一下下,會出現此錯誤的過程的詳細信息:

當我第一次開始的Spyder我跑在Python控制檯我的主要調用模塊。模塊正確完成,控制檯返回到命令提示符。如果我然後單擊變量資源管理器,則IDE將鎖定並進一步點擊,導致IDE變灰(其他操作可能具有相同的效果)。然後我嘗試關閉提示選項恢復的IDE - 我這樣做。 IDE恢復,那是當我在Python控制檯中得到「索引錯誤」跟蹤。然後,我可以從IDE(黃色三角形)中終止控制檯並重新啓動控制檯。如果我再次運行調用模塊,它會正確運行(即所有輸出),但不會返回到重新啓動的python控制檯中的命令提示符。任何對IDE的點擊都會導致它變灰,我需要關閉IDE才能繼續。

>>> Exception in thread Thread-1: 
Traceback (most recent call last): 
    File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner 
    self.run() 
    File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\monitor.py", line 569, in run 
    self.update_remote_view() 
    File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\monitor.py", line 450, in update_remote_view 
    remote_view = make_remote_view(ns, settings, more_excluded_names) 
    File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\monitor.py", line 79, in make_remote_view 
    minmax=settings['minmax']) 
    File "C:\Python27\lib\site-packages\spyderlib\widgets\dicteditorutils.py", line 202, in value_to_display 
    value = repr(value) 
    File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 3667, in __repr__ 
    data=str(self), mask=str(self._mask), 
    File "C:\Python27\lib\site-packages\numpy\ma\core.py", line 3647, in __str__ 
    res.view(ndarray)[m] = f 
IndexError: too many indices for array 

發生此錯誤時被調用的代碼是:

def GroupSpectra(spectral, frequency, u, zmd, grp, covar=[1], prenorm=False): 

    # expand non spectral inputs so they are samwe shape as spectral 
    frequency = expand(frequency,spectral) 
    u = expand(u,spectral) 
    zmd = expand(zmd,spectral) 
    grp = expand(grp,spectral) 
    covar = expand(covar,spectral) 
    scnt = expand(np.array([1]),spectral) 

    # calc normalized freq 
    nfreq = frequency*zmd/u 

    # create frequency bins (freq by grps) 
    grps = np.unique(grp) 
    igrps = grps.size 
    binfreq = np.power(10,np.arange(-5,2,0.1)) 
    iflen = binfreq.size 
    binfreq = np.tile(binfreq,(igrps,1)) 
    binSpecSum = np.zeros(binfreq.shape) 
    binSpecSS = np.zeros(binfreq.shape) 
    binCount = np.ones(binfreq.shape) 
    binCoVar = np.zeros(binfreq.shape) 
    SpecAvg = np.zeros(binfreq.shape) 
    CovAvg = np.zeros(binfreq.shape) 
    SpecStd = np.zeros(binfreq.shape) 

    # pre normalize powers ?? 
    if prenorm == True: 
     spectral = spectral/covar 

    # put powers in bins 
    ig = 0 
    for ig in np.arange(igrps): 
     idg = grp == grps[ig] 
     for ix in np.arange(0,iflen-1): 
      flow = binfreq[0,ix] 
      fhigh = binfreq[0,ix+1] 
      idf = ((nfreq >= flow) & (nfreq < fhigh)) 
      idfg= idg & idf  
      binCount[ig,ix] = np.nansum(scnt[idfg]) 
      binSpecSum[ig,ix] = np.nansum(spectral[idfg]) 
      binSpecSS[ig,ix] = np.nansum(np.power(spectral[idfg],2.0)) 
      binCoVar[ig,ix] = np.nansum(covar[idfg]) 

    # avg spectra 
    idb = binCount > 0.5 
    SpecAvg[idb] = np.divide(binSpecSum[idb],binCount[idb]) 
    FreqAvg = binfreq 
    SpecStd[idb] = np.sqrt(np.divide(binSpecSS[idb],binCount[idb]) - np.square(SpecAvg[idb])) 
    CovAvg[idb] = np.divide(binCoVar[idb],binCount[idb]) 

    # pre normalize powers ?? 
    if prenorm == False: 
     ida = CovAvg != 0.0 
     idb = np.isfinite(CovAvg) 
     idx = ida & idb 
     SpecAvg[idx] = np.divide(SpecAvg[idx],CovAvg[idx]) 
     # SpecStd = SpecStd/CovAvg 


    print(FreqAvg.shape) 
    print(SpecAvg.shape) 
    print(SpecStd.shape) 
    return (FreqAvg,SpecAvg, SpecStd) 
+0

的異常似乎正在發生[在與Spyder的外部Python shell相關的函數中](https://github.com/spyder-ide/spyder/blob/master/spyderlib/widgets/externalshell/monitor.py#L47-L69)。當您在Spyder外部運行相同的代碼時,會發生此錯誤,例如在IPython中還是普通的Python shell? –

+0

嗨,如果我從Spyder的IPython Qt控制檯運行代碼,則會出現同樣的問題。但是,如果我從一個從Windows命令提示符啓動的python解釋器運行該模塊,那麼問題似乎不存在。 – RJCL

+0

另外,如果我從Spyder啓動的IPython shell運行模塊,則問題不會出現。 – RJCL

回答

0

花了一段時間來追蹤下來,但改變了下面的代碼導致Spyder的IDE上完成未鎖定了調用函數。該代碼是用於調整某些數組大小的函數的一部分。

的代碼的原始兩行:

newsmall = np.tile(small, (colb,1)) 
newsmall = np.transpose(newsmall) 

將該代碼作爲更改爲:

newsmall = np.resize(small,(rowb,1)) 
newsmall = np.tile(newsmall, (1,colb)) 

陣列小這兩行之前所具有的尺寸(rowB中,)

相關問題