Update how to find start and stop position of a peak

This commit is contained in:
linarphy 2023-08-30 00:17:13 +02:00
parent 2a072790ea
commit 08e3b850e5
No known key found for this signature in database
GPG key ID: B914FF3FE69AA363
2 changed files with 35 additions and 34 deletions

View file

@ -4,7 +4,7 @@ from scipy.signal import convolve, find_peaks
from scipy.ndimage import rotate from scipy.ndimage import rotate
from classes.science.border import Border from classes.science.border import Border
from classes.science.calibration_spectrum import CalibrationSpectrum 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 function.fit import linear
from logging import getLogger from logging import getLogger
@ -272,7 +272,7 @@ class Plate:
""" """
self.spectrum = Border() self.spectrum = Border()
indexes = cut_biggest( indexes = find_peak_low_high(
convolve( convolve(
mean( mean(
self.data[ self.border.slice() ], self.data[ self.border.slice() ],
@ -281,12 +281,15 @@ class Plate:
ones( 200 ), ones( 200 ),
'valid' , 'valid' ,
), ),
) )[0]
self.spectrum.y.min = indexes[0] + self.border.y.min + 100 self.spectrum.y.min = indexes[0] + self.border.y.min + 100
self.spectrum.y.max = indexes[1] + 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( convolve(
mean( mean(
self.data[ self.border.slice() ], self.data[ self.border.slice() ],
@ -295,7 +298,7 @@ class Plate:
ones( 200 ), ones( 200 ),
'valid' , 'valid' ,
), ),
) )[0]
self.spectrum.x.min = indexes[0] + self.border.x.min + 100 self.spectrum.x.min = indexes[0] + self.border.x.min + 100
self.spectrum.x.max = indexes[1] + self.border.x.min + 100 self.spectrum.x.max = indexes[1] + self.border.x.min + 100

View file

@ -208,36 +208,34 @@ def near_value( list_ , value ):
index = np.append( index , np.where( list_ == value ) ) index = np.append( index , np.where( list_ == value ) )
return np.round( np.sort( index ) ).astype( int ) # triage 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 Return index of start and end of the biggest peak in a list
""" """
factor = 1 min_diff = ( max( list_ ) - min( list_ ) ) * 0.001
indexes = near_value( max_high_value = max( list_ )
list_ , min_high_value = min( list_ )
np.max( list_ ), max_low_value = max( list_ )
) min_low_value = min( list_ )
if len( indexes ) > 2: while (
import matplotlib.pyplot as plt max_high_value - min_high_value > min_diff or
plt.plot( list_ ) max_low_value - min_low_value > min_diff
plt.show() ):
raise Exception( 'too much peak' ) current_high = min_high_value + (
while len( indexes ) < 2: max_high_value - min_high_value
factor += 1 ) / 2
indexes = near_value( current_low = min_low_value + (
list_ , max_low_value - min_low_value
np.min( list_ ) + ( ) / 2
np.max( list_ ) - np.mean( list_ ) indexes_high = near_value( list_ , current_high )
) / factor, indexes_low = near_value( list_ , current_low )
) if len( indexes_low ) != 2:
factor -= 1 min_low_value = current_low
indexes = near_value( else:
list_ , max_low_value = current_low
np.min( list_ ) + ( if len( indexes_high ) != 2:
np.max( list_ ) - np.mean( list_ ) max_high_value = current_high
) / factor, else:
) min_high_value = current_high
if len( indexes ) == 2:
raise Exception( 'less than two pixel peak' )
return indexes return indexes_low, indexes_high