Fix and add function to utils.py

- Add point function
- Add find_point function
- Fix type check in some function
This commit is contained in:
linarphy 2023-05-05 17:01:07 +02:00
parent dd526c77ef
commit c26b4782ed
No known key found for this signature in database
GPG key ID: 69BD8A7D97AD643E

View file

@ -1,6 +1,5 @@
from astropy.io.fits import open
from numpy import compress as numpy_compress
from numpy import ndarray
import numpy as np
def load( filename ):
"""
@ -8,7 +7,6 @@ def load( filename ):
"""
if not isinstance( filename , str ):
raise ValueError( 'the filename must be a string, ' + type( filename ) + ' given' )
hdul = open( filename )
data = hdul[0].data
hdul.close()
@ -19,14 +17,14 @@ def compress( data , factor ):
"""
divide the size of the data by 2**factor
"""
if not isinstance( data , ndarray ) and not isinstance( data , list ):
if not isinstance( data , np.ndarray ) and not isinstance( data , list ):
raise ValueError( 'data must be a list, ' + type( data ) + ' given' )
if not isinstance( factor , int ):
raise ValueError( 'factor must be an integer, ' + type( factor ) + ' given' )
for _ in range( factor ):
data = numpy_compress( [ True , False ] * ( data.shape[1] // 2 ) , data , axis = 1 )
data = numpy_compress( [ True , False ] * ( data.shape[0] // 2 ) , data , axis = 0 )
data = np.compress( [ True , False ] * ( data.shape[1] // 2 ) , data , axis = 1 )
data = np.compress( [ True , False ] * ( data.shape[0] // 2 ) , data , axis = 0 )
return data
@ -35,18 +33,16 @@ def big_to_small( indexes , factor ):
convert a coordinate of a point in the non compressed data to the same coordinate
in the compressed one (there is a loss of information here !)
"""
if not isinstance( indexes , int ) and not isinstance( indexes , list ) and not isinstance( indexes , tuple ):
raise ValueError( 'indexes must be an integer or a tuple, ' + type( indexes ) + ' given' )
if not isinstance( indexes , int ) and not isinstance( indexes , list ) and not isinstance( indexes , np.ndarray ):
raise ValueError( 'indexes must be an integer or a list, ' + type( indexes ) + ' given' )
if not isinstance( factor , int ):
raise ValueError( 'factor mus be an integer' )
if isinstance( indexes , int ):
return big_to_small( [ indexes ] , factor )[0]
if isinstance( indexes , tuple ):
return tuple( big_to_small( list( indexes ) , factor ) )
if isinstance( indexes , int ) or isinstance( indexes , np.ndarray ):
return indexes // ( 2 ** factor )
for i in range( len( indexes ) ):
indexes[i] = indexes[i] // ( 2 ** factor )
indexes[i] = big_to_small( indexes[i] , factor )
return indexes
@ -55,18 +51,18 @@ def small_to_big( indexes , factor ):
convert a coordinate of a point n the compressed data to the same coordinate
in the compressed one
"""
if not isinstance( indexes , int ) and not isinstance( indexes , list ) and not isinstance( indexes , tuple ):
raise ValueError( 'indexes must be an integer or a tuple, ' + type( indexes ) + ' given' )
if not isinstance( indexes , int ) and not isinstance( indexes , list ) and not isinstance( indexes , np.ndarray ):
raise ValueError( 'indexes must be an integer or a list, ' + type( indexes ) + ' given' )
if not isinstance( factor , int ):
raise ValueError( 'factor mus be an integer' )
if isinstance( indexes , int ):
return small_to_big( [ indexes ] , factor )[0]
if isinstance( indexes , tuple ):
return tuple( small_to_big( list( indexes ) , factor ) )
return int( indexes * 2 ** factor )
if isinstance( indexes , np.ndarray ):
return ( indexes * 2 ** factor ).astype( int )
for i in range( len( indexes ) ):
indexes[i] = int( indexes[i] * 2 ** factor )
indexes[i] = small_to_big( indexes[i] , factor )
return indexes
@ -75,31 +71,31 @@ def check_side( data , point , tolerance ):
give coordinates of all side point of the given point which have an
intensity difference inferior than tolerance
"""
if not isinstance( data , ndarray ) and not isinstance( data , list ):
if not isinstance( data , np.ndarray ) and not isinstance( data , list ):
raise ValueError( 'data must be a list, ' + type( data ) + ' given' )
if not isinstance( point , ndarray ) and not isinstance( point , tuple ) and not isinstance( point , list ):
if not isinstance( point , np.ndarray ) and not isinstance( point , tuple ) and not isinstance( point , list ):
raise ValueError( 'point must be a tuple, ' + type( point ) + ' given' )
if not isinstance( tolerance , int ) and not isinstance( tolerance , float ):
raise ValueError( 'tolerance must be a number, ' + type( tolerance ) + ' given' )
positions , intensity = [] , data[ point ]
if 0 <= position[0] < data.shape[0] - 1 and intensity - tolerance <= data[ position[0] + 1 , position[1] ] <= intensity + tolerance:
positions.append( ( position[0] + 1 , position[1] ) )
if 0 < position[0] < data.shape[0] and intensity - tolerance <= data[ position[0] - 1 , position[1] ] <= intensity + tolerance:
positions.append( ( position[0] - 1 , position[1] ) )
if 0 <= position[1] < data.shape[1] - 1 and intensity - tolerance <= data[ position[0] , position[1] + 1 ] <= intensity + tolerance:
positions.append( ( position[0] , position[1] + 1 ) )
if 0 < position[1] < data.shape[1] and intensity - tolerance <= data[ position[0] , position[1] - 1 ] <= intensity + tolerance:
positions.append( ( position[0] , position[1] - 1 ) )
positions , intensity = [] , data[ *point ]
if 0 <= point[0] < data.shape[0] - 1 and intensity - tolerance <= data[ point[0] + 1 , point[1] ] <= intensity + tolerance:
positions.append( [ point[0] + 1 , point[1] ] )
if 0 < point[0] < data.shape[0] and intensity - tolerance <= data[ point[0] - 1 , point[1] ] <= intensity + tolerance:
positions.append( [ point[0] - 1 , point[1] ] )
if 0 <= point[1] < data.shape[1] - 1 and intensity - tolerance <= data[ point[0] , point[1] + 1 ] <= intensity + tolerance:
positions.append( [ point[0] , point[1] + 1 ] )
if 0 < point[1] < data.shape[1] and intensity - tolerance <= data[ point[0] , point[1] - 1 ] <= intensity + tolerance:
positions.append( [ point[0] , point[1] - 1 ] )
return positions
def fill( data , point , tolerance , limit = 100000 ):
"""
give the coordinate of all points that fill the area with the given tolerance
"""
if not isinstance( data , ndarray ) and not isinstance( data , list ):
if not isinstance( data , np.ndarray ) and not isinstance( data , list ):
raise ValueError( 'data must be a list, ' + type( data ) + ' given' )
if not isinstance( point , ndarray ) and not isinstance( point , tuple ) and not isinstance( point , list ):
if not isinstance( point , np.ndarray ) and not isinstance( point , tuple ) and not isinstance( point , list ):
raise ValueError( 'point must be a tuple, ' + type( point ) + ' given' )
if not isinstance( tolerance , int ) and not isinstance( tolerance , float ):
raise ValueError( 'tolerance must be a number, ' + type( tolerance ) + ' given' )
@ -117,3 +113,40 @@ def fill( data , point , tolerance , limit = 100000 ):
new_points.append( position )
i += 1
return np.array( taken_point )
def point( index_1 , index_2 , axis = 'x' ):
"""
reorder coordinate
"""
if axis == 'x':
return [ index_2 , index_1 ]
return [ index_1 , index_2 ]
def find_point( list_ , index , axis = 'x' , threshold = 0.5 ):
"""
find the index where to fill in a side
"""
mean = np.mean( list_ )
ampl = np.max( list_ ) - np.min( list_ )
if ampl < mean / 2:
return [ point( index , 0 , axis ) ]
else:
points = []
list_ = np.convolve( list_ , np.ones( 100 ) , 'same' )
list_ -= np.min( list_ )
list_ /= np.max( list_ )
i , inside , size = 0 , False , 0
while i < len( list_ ):
if list_[ i ] > threshold and not inside:
points.append( point( index , i , axis ) )
inside = True
size = 0
elif list_[ i ] < threshold and inside:
size += 1
if size > 0.01 * len( list_ ): # low sensibility
inside = False
i += 1
return points