numpy"空切片的意思".警告

boo*_*oof 6 python numpy suppress-warnings

更新(真正的错误)

我错误地识别了错误的来源.这是我的全部功能(对不起,如果有些线路模糊不清......)

def removeLines(input,CRVAL1,CDELT1): #Masks out the Balmer lines from the spectrum
    #Numbers 4060, 4150, 4300, 4375, 4800, and 4950 obtained from fit_RVs.pro.
    #Other numbers obtained from the Balmer absorption series lines

    for i in range(0,len(lineWindows),2):
        left = toIndex(lineWindows[i],CRVAL1,CDELT1)
        right = toIndex(lineWindows[i+1],CRVAL1,CDELT1)

        print "left = ", left
        print "right = ", right
        print "20 from right =\n", input[right:right+20]
        print "mean of 20 = ", numpy.mean(input[right:right+20])

        #Find the averages on the left and right sides
        left_avg = numpy.mean(input[left-20:left])
        right_avg = numpy.mean(input[right:right+20])   #<--- NOT here

        print "right_avg = ", right_avg

        #Find the slope between the averages
        slope = (left_avg - right_avg)/(left - right)

        #Find the y-intercept of the line conjoining the averages
        bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2

        for j in range(left,right):     #Redefine the data to follow the line conjoining
            input[j] = slope*j + bval   #the sides of the peaks

    left = int(input[0])
    left_avg = int(input[0])
    right = toIndex(lineWindows[0],CRVAL1,CDELT1)
    right_avg = numpy.mean(input[right:right+20])   #<---- THIS IS WHERE IT IS!
    slope = (left_avg - right_avg)/(left - right)
    bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2

    for i in range(left, right):
        input[i] = slope*i + bval
    return input
Run Code Online (Sandbox Code Playgroud)

我已经调查了这个问题并找到了答案,该答案已在下面发布(不在本文中).


错误(愚蠢的错误)

#left  = An index in the data (on the 'left' side)
#right = An index in the data (on the 'right' side)
#input = The data array

print "left = ", left
print "right = ", right
print "20 from right =\n", input[right:right+20]
print "mean of 20 = ", numpy.mean(input[right:right+20])

#Find the averages on the left and right sides
left_avg = numpy.mean(input[left-20:left])
right_avg = numpy.mean(input[right:right+20])
Run Code Online (Sandbox Code Playgroud)

产生了输出

left =  1333
right =  1490
20 from right =
[ 0.14138737  0.14085886  0.14038289  0.14045525  0.14078836  0.14083192
  0.14072289  0.14082283  0.14058594  0.13977806  0.13955595  0.13998236
  0.1400764   0.1399636   0.14025062  0.14074247  0.14094831  0.14078569
  0.14001536  0.13895717]
mean of 20 =  0.140395
Traceback (most recent call last):
...
  File "getRVs.py", line 201, in removeLines
    right_avg = numpy.mean(input[right:right+20])
  File "C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 2735, in mean
    out=out, keepdims=keepdims)
  File "C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\_methods.py", line 59, in _mean
    warnings.warn("Mean of empty slice.", RuntimeWarning)
RuntimeWarning: Mean of empty slice.
Run Code Online (Sandbox Code Playgroud)

numpy.mean当我打印它时,它似乎正确运行,但当我将它分配给一个值时,它会有所不同.任何反馈都将非常感激.感谢您抽出宝贵时间阅读我的问题.


简要说明

简而言之,我正在编写一个代码来处理科学数据,部分代码涉及到大约20个值的平均值.

#left  = An index in the data (on the 'left' side)
#right = An index in the data (on the 'right' side)
#input = The data array

#Find the averages on the left and right sides
left_avg = numpy.mean(input[left-20:left])
right_avg = numpy.mean(input[right:right+20])
Run Code Online (Sandbox Code Playgroud)

此代码返回一个numpy"空切片的平均值".警告并恼人地将它打印在我宝贵的输出中!我决定尝试追查警告的来源,看到这里,例如,所以我把

import warnings
warnings.simplefilter("error")
Run Code Online (Sandbox Code Playgroud)

在我的代码顶部,然后返回以下剪切的Traceback:

  File "getRVs.py", line 201, in removeLines
    right_avg = numpy.mean(input[right:right+20])
  File "C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 2735, in mean
    out=out, keepdims=keepdims)
  File "C:\Users\MyName\Anaconda\lib\site-packages\numpy\core\_methods.py", line 59, in _mean
    warnings.warn("Mean of empty slice.", RuntimeWarning)
RuntimeWarning: Mean of empty slice.
Run Code Online (Sandbox Code Playgroud)

我省略了大约2/3的Traceback,因为它通过了大约5个难以解释的函数,这些函数不会影响数据的可读性或大小.

所以我决定打印出整个操作,看看是right_avg不是真的尝试了numpy.mean一个空片......那就是事情变得非常奇怪.

boo*_*oof 1

我错误地识别了错误所在的代码行。我需要做的是针对特定情况编写代码,在这种情况下,数据中考虑的中心点周围的窗口(left和侧面)太靠近数据数组的边缘right

def removeLines(input,CRVAL1,CDELT1): #Masks out the Balmer lines from the spectrum
    
    for i in range(0,len(lineWindows),2):
        left = toIndex(lineWindows[i],CRVAL1,CDELT1)
        right = toIndex(lineWindows[i+1],CRVAL1,CDELT1)
        
        #Find the averages on the left and right sides
        left_avg = numpy.mean(input[left-20:left])
        right_avg = numpy.mean(input[right:right+20])
        
        #Find the slope between the averages
        slope = (left_avg - right_avg)/(left - right)
        
        #Find the y-intercept of the line conjoining the averages
        bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2
        
        for j in range(left,right):     #Redefine the data to follow the line conjoining
            input[j] = slope*j + bval   #the sides of the peaks

    left = 0
    left_avg = int(input[0])
    
    if toIndex(lineWindows[0],CRVAL1,CDELT1) < 0: right = 0
    else: right = toIndex(lineWindows[0],CRVAL1,CDELT1)

    right_avg = numpy.mean(input[right:right+20])
    slope = (left_avg - right_avg)/(left - right)
    bval = ((left_avg - slope*left) + (right_avg - slope*right)) / 2
    
    for i in range(left, right):
        input[i] = slope*i + bval
    return input
Run Code Online (Sandbox Code Playgroud)

只需改变这个

right = toIndex(lineWindows[0],CRVAL1,CDELT1)    #Error occurs where right = -10
right_avg = numpy.mean(input[right:right+20])    #Index of -10? Yeah, right.
Run Code Online (Sandbox Code Playgroud)

对此

if toIndex(lineWindows[0],CRVAL1,CDELT1) < 0: right = 0    #Index 0, much better!
else: right = toIndex(lineWindows[0],CRVAL1,CDELT1)    #Leave it alone if it isn't a problem.

right_avg = numpy.mean(input[right:right+20])
Run Code Online (Sandbox Code Playgroud)

另外,我错了left = int(input[0]),所以我把它改成了left = 0