Update how to find start and stop position of a peak
This commit is contained in:
parent
2a072790ea
commit
08e3b850e5
2 changed files with 35 additions and 34 deletions
|
@ -4,7 +4,7 @@ from scipy.signal import convolve, find_peaks
|
|||
from scipy.ndimage import rotate
|
||||
from classes.science.border import Border
|
||||
from classes.science.calibration_spectrum import CalibrationSpectrum
|
||||
from function.utils import find_point, fill, cut_biggest
|
||||
from function.utils import find_point, fill, find_peak_low_high
|
||||
from function.fit import linear
|
||||
|
||||
from logging import getLogger
|
||||
|
@ -272,7 +272,7 @@ class Plate:
|
|||
"""
|
||||
self.spectrum = Border()
|
||||
|
||||
indexes = cut_biggest(
|
||||
indexes = find_peak_low_high(
|
||||
convolve(
|
||||
mean(
|
||||
self.data[ self.border.slice() ],
|
||||
|
@ -281,12 +281,15 @@ class Plate:
|
|||
ones( 200 ),
|
||||
'valid' ,
|
||||
),
|
||||
)
|
||||
)[0]
|
||||
|
||||
self.spectrum.y.min = indexes[0] + self.border.y.min + 100
|
||||
self.spectrum.y.max = indexes[1] + self.border.y.min + 100
|
||||
|
||||
indexes = cut_biggest(
|
||||
import matplotlib.pyplot as plt
|
||||
plt.imshow( self.data[ self.border.slice() ] , aspect = 'auto' )
|
||||
plt.show()
|
||||
indexes = find_peak_low_high(
|
||||
convolve(
|
||||
mean(
|
||||
self.data[ self.border.slice() ],
|
||||
|
@ -295,7 +298,7 @@ class Plate:
|
|||
ones( 200 ),
|
||||
'valid' ,
|
||||
),
|
||||
)
|
||||
)[0]
|
||||
|
||||
self.spectrum.x.min = indexes[0] + self.border.x.min + 100
|
||||
self.spectrum.x.max = indexes[1] + self.border.x.min + 100
|
||||
|
|
|
@ -208,36 +208,34 @@ def near_value( list_ , value ):
|
|||
index = np.append( index , np.where( list_ == value ) )
|
||||
return np.round( np.sort( index ) ).astype( int ) # triage
|
||||
|
||||
def cut_biggest( list_ ):
|
||||
def find_peak_low_high( list_ ):
|
||||
"""
|
||||
Return index of start and end of the biggest peak in a list
|
||||
"""
|
||||
factor = 1
|
||||
indexes = near_value(
|
||||
list_ ,
|
||||
np.max( list_ ),
|
||||
)
|
||||
if len( indexes ) > 2:
|
||||
import matplotlib.pyplot as plt
|
||||
plt.plot( list_ )
|
||||
plt.show()
|
||||
raise Exception( 'too much peak' )
|
||||
while len( indexes ) < 2:
|
||||
factor += 1
|
||||
indexes = near_value(
|
||||
list_ ,
|
||||
np.min( list_ ) + (
|
||||
np.max( list_ ) - np.mean( list_ )
|
||||
) / factor,
|
||||
)
|
||||
factor -= 1
|
||||
indexes = near_value(
|
||||
list_ ,
|
||||
np.min( list_ ) + (
|
||||
np.max( list_ ) - np.mean( list_ )
|
||||
) / factor,
|
||||
)
|
||||
if len( indexes ) == 2:
|
||||
raise Exception( 'less than two pixel peak' )
|
||||
min_diff = ( max( list_ ) - min( list_ ) ) * 0.001
|
||||
max_high_value = max( list_ )
|
||||
min_high_value = min( list_ )
|
||||
max_low_value = max( list_ )
|
||||
min_low_value = min( list_ )
|
||||
while (
|
||||
max_high_value - min_high_value > min_diff or
|
||||
max_low_value - min_low_value > min_diff
|
||||
):
|
||||
current_high = min_high_value + (
|
||||
max_high_value - min_high_value
|
||||
) / 2
|
||||
current_low = min_low_value + (
|
||||
max_low_value - min_low_value
|
||||
) / 2
|
||||
indexes_high = near_value( list_ , current_high )
|
||||
indexes_low = near_value( list_ , current_low )
|
||||
if len( indexes_low ) != 2:
|
||||
min_low_value = current_low
|
||||
else:
|
||||
max_low_value = current_low
|
||||
if len( indexes_high ) != 2:
|
||||
max_high_value = current_high
|
||||
else:
|
||||
min_high_value = current_high
|
||||
|
||||
return indexes
|
||||
return indexes_low, indexes_high
|
||||
|
|
Loading…
Reference in a new issue