UPDATE (L'errore vera)
ho erroneamente identificato dove l'errore venisse. Qui è la mia funzione nella sua interezza (scusate se alcune delle linee sono oscure e confuse ...)numpy "Mean of empty slice". avvertimento
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
Ho studiato il problema e trovato la risposta, che viene pubblicato qui di seguito (non in questo post).
L'errore (La sciocca falso errore)
#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])
ha prodotto l'uscita
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.
sembrerebbe che numpy.mean
viene eseguito correttamente quando stampo, ma in modo diverso quando ho assegnarlo a un valore. Qualsiasi feedback sarebbe molto apprezzato. Grazie per aver dedicato del tempo a leggere la mia domanda.
Breve spiegazione
Insomma, sto scrivendo un codice per gestire dati scientifici e parte del codice consiste nel prendere la media di circa 20 valori.
#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])
Questo codice restituisce una "media della porzione vuota" numerica. avvertimento e fastidiosamente lo stampa nella mia preziosa produzione! Ho deciso di provare a rintracciare la fonte della segnalazione come visto here, per esempio, così ho messo
import warnings
warnings.simplefilter("error")
nella parte superiore del mio codice, che poi ha restituito il seguente Traceback snipped:
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.
Ho omesso circa i 2/3 di Traceback perché passa attraverso circa 5 funzioni difficili da spiegare che non influiscono sulla leggibilità o sulla dimensione dei dati.
così ho deciso di stampare l'intera operazione per vedere se davvero right_avg
stava tentando un numpy.mean
di una fetta vuota ... E che quando le cose sono davvero strano.
L'errore sta dicendo che la seconda volta '' numpy.mean (input [right: right + 20]) 'viene chiamato,' input [right: right + 20] 'è vuoto. Ci deve essere un codice tra la prima e la seconda chiamata che sta cambiando il valore di 'input'. – unutbu
I valori per 'input' ** sono ** modificati come hai detto, ma non c'è errore quando stampo lo stesso codice prima ancora che venga chiamato. Pensi che sia un problema con come sto assegnando 'left_avg'? – boof
Avevi ragione! C'è una parte del codice più avanti nella stessa funzione che gestisce 'right_avg'! Ho aggiornato il thread principale per evitare di sprecare il tempo delle persone. Che imbarazzo ... – boof