Spyder控制台因“IndexError:数组索引太多”而锁定

2024-09-28 01:26:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个函数,它对一个数据集进行分组平均。当我调用这个函数时,它会运行,返回随后绘制的数据。在这次通话中,我得到一个警告:

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

似乎不会影响函数调用。但是,函数返回后,发生了一些事情,锁定了运行python的控制台(python2.7、spyder 2.3.5.2、windows7)。在锁定时没有错误跟踪,但是当我从这个锁定中恢复时,我得到下面给出的错误跟踪。这个“索引错误”似乎发生在python核心中,我不知道如何将它追溯到我的代码中。有人能建议如何确定这个错误的来源吗。你知道吗

有关发生此错误的过程的详细信息:

当我第一次启动Spyder时,我在python控制台中运行我的主调用模块。模块正确完成,控制台返回命令提示符。如果单击变量资源管理器,IDE就会锁定,进一步单击会导致IDE变灰(其他操作可能会产生相同的效果)。然后我尝试关闭IDE,IDE会提示恢复选项—我会这样做。IDE将恢复,这就是我在python控制台中获得“Index error”跟踪的时候。然后,我可以从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)

Tags: inpylibnplineidefileexpand
1条回答
网友
1楼 · 发布于 2024-09-28 01:26:48

跟踪这一点花了一段时间,但更改以下代码导致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,)在这两行之前

相关问题 更多 >

    热门问题