Numpy "The average value of an empty slice." a warning

UPDATE (real error)

I incorrectly identified where the error came from. Here is my whole function (sorry if some of the lines are unclear and confusing ...)

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 

I investigated the problem and found the answer that is posted below (not in this post).


Error (stupid false error)

 #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]) 

made a conclusion

 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. 

It seems that numpy.mean works correctly when I print it, but differently when I assign it a value. Any feedback would be greatly appreciated. Thanks for taking the time to read my question.


Short description

In short, I am writing code to process scientific data, and part of the code involves taking a value of about 20 values.

 #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]) 

This code returns numpy "Mean empty slice". warning and annoyingly prints it in my precious product! For example, I decided to try to track the source of the warning, for example, here , so I posted

 import warnings warnings.simplefilter("error") 

at the top of my code, which then returned the following traceback cut off:

  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. 

I missed about 2/3 of Traceback because it moves through about 5 complex functions that do not affect the readability or size of the data.

So, I decided to print out the whole operation to see if right_avg is right_avg trying to numpy.mean empty slice ... And this is when everything gets really weird.

+6
source share
2 answers

You dingbat! The answer is obvious, right? You obviously mistakenly determined which line of code your error went into. What you need to do is write code for the specific case when the window ( left and right sides) around the center point considered in the data is too close to the edge of the data array .

 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 

Just change it

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

to that

 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]) 

Also, you were completely wrong regarding left = int(input[0]) , so I changed it to left = 0 for you. Who knows what other simple errors this messy, messy code gives? Look a little harder before posting to Stack Overflow, please!

+3
source

I could not reproduce your error. Are you using the latest numpy version? However, you can suppress warnings by removing the ignore keyword (see https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings )

This error usually means that an empty list has been passed to the function.

 >>> a = [] >>> import numpy >>> numpy.mean(a) /shahlab/pipelines/apps_centos6/Python-2.7.10/lib/python2.7/site-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice. warnings.warn("Mean of empty slice.", RuntimeWarning) /shahlab/pipelines/apps_centos6/Python-2.7.10/lib/python2.7/site-packages/numpy/core/_methods.py:71: RuntimeWarning: invalid value encountered in double_scalars ret = ret.dtype.type(ret / rcount) nan >>> print numpy.mean(a) nan >>> import warnings >>> warnings.simplefilter("ignore") >>> numpy.mean(a) nan >>> a=[ 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] >>> numpy.mean(a) 0.140394615 >>> x = numpy.mean(a) >>> print x 0.140394615 >>> numpy.__version__ '1.9.2' 

Hope this helps.

+1
source

All Articles