From 08e3b850e50c6eaf62d92b64ed2940880d3dec6d Mon Sep 17 00:00:00 2001 From: linarphy Date: Wed, 30 Aug 2023 00:17:13 +0200 Subject: [PATCH] Update how to find start and stop position of a peak --- classes/science/plate.py | 13 ++++++---- function/utils.py | 56 +++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/classes/science/plate.py b/classes/science/plate.py index c6cb950..bdd0d48 100644 --- a/classes/science/plate.py +++ b/classes/science/plate.py @@ -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 diff --git a/function/utils.py b/function/utils.py index 58dfc0e..e60ef12 100644 --- a/function/utils.py +++ b/function/utils.py @@ -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