<s> import numpy as np <EOL> def __adjusted_meshgrid ( shape ) : <EOL> """<STR_LIT>""" <EOL> adjust_range = [ ] <EOL> for dim in shape : <EOL> adjust_range . append ( np . linspace ( - <NUM_LIT:0.5> , <NUM_LIT:0.5> , dim ) ) <EOL> return np . meshgrid ( * adjust_range , indexing = '<STR_LIT>' ) <EOL> def __frequency_butterworth_filter ( shape , cutoff , order ) : <EOL> r"""<STR_LIT>""" <EOL> grid = __adjusted_meshgrid ( shape ) <EOL> grid_sq = [ g ** <NUM_LIT:2> for g in grid ] <EOL> grid_sq = sum ( grid_sq ) <EOL> radius = np . sqrt ( grid_sq ) <EOL> return np . fft . ifftshift ( <NUM_LIT:1.0> / ( ( radius / cutoff ) ** ( <NUM_LIT:2> * order ) + <NUM_LIT:1.0> ) ) <EOL> def log_gabor ( image , ** kwargs ) : <EOL> r"""<STR_LIT>""" <EOL> if len ( image . shape ) == <NUM_LIT:2> : <EOL> return __log_gabor_2d ( image , ** kwargs ) <EOL> elif len ( image . shape ) == <NUM_LIT:3> : <EOL> return __log_gabor_3d ( image , ** kwargs ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def __log_gabor_3d ( image , num_scales = <NUM_LIT:4> , num_phi_orientations = <NUM_LIT:6> , <EOL> num_theta_orientations = <NUM_LIT:4> , min_wavelength = <NUM_LIT:3> , <EOL> scaling_constant = <NUM_LIT:2> , center_sigma = <NUM_LIT> , d_theta_sigma = <NUM_LIT> , <EOL> d_phi_sigma = <NUM_LIT> ) : <EOL> theta_sigma = np . pi / num_theta_orientations / d_theta_sigma <EOL> phi_sigma = ( <NUM_LIT:2> * np . pi ) / num_phi_orientations / d_phi_sigma <EOL> bandpass = np . empty ( [ num_scales , image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] , <EOL> image . shape [ <NUM_LIT:2> ] ] , dtype = np . complex ) <EOL> log_gabor = np . empty ( [ num_scales , image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] , <EOL> image . shape [ <NUM_LIT:2> ] ] ) <EOL> S = np . zeros ( image . shape ) <EOL> complex_conv = np . empty ( [ num_scales , num_theta_orientations , <EOL> num_phi_orientations , image . shape [ <NUM_LIT:0> ] , <EOL> image . shape [ <NUM_LIT:1> ] , image . shape [ <NUM_LIT:2> ] ] , dtype = np . complex ) <EOL> tmp_complex_conv = np . empty ( [ num_scales , image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] , <EOL> image . shape [ <NUM_LIT:2> ] ] , dtype = np . complex ) <EOL> image_fft = np . fft . fftn ( image ) <EOL> axis0 , axis1 , axis2 = __adjusted_meshgrid ( image . shape ) <EOL> radius = np . sqrt ( axis0 ** <NUM_LIT:2> + axis1 ** <NUM_LIT:2> + axis2 ** <NUM_LIT:2> ) <EOL> theta = np . arctan2 ( axis0 , axis1 ) <EOL> m_ab = np . abs ( np . mean ( radius ) ) <EOL> phi = np . arccos ( axis2 / ( radius + m_ab ) ) <EOL> radius = np . fft . ifftshift ( radius ) <EOL> radius [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT:1.0> <EOL> theta = np . fft . ifftshift ( theta ) <EOL> phi = np . fft . ifftshift ( phi ) <EOL> sin_theta = np . sin ( theta ) <EOL> cos_theta = np . cos ( theta ) <EOL> sin_phi = np . sin ( phi ) <EOL> cos_phi = np . cos ( phi ) <EOL> butterworth_filter = __frequency_butterworth_filter ( image . shape , <NUM_LIT> , <NUM_LIT:15> ) <EOL> for s in range ( num_scales ) : <EOL> wavelength = min_wavelength * scaling_constant ** s <EOL> fo = <NUM_LIT:1.0> / wavelength <EOL> l = np . exp ( ( - np . log ( radius / fo ) ** <NUM_LIT:2> ) / <EOL> ( <NUM_LIT> * np . log ( center_sigma ) ** <NUM_LIT:2> ) ) <EOL> l = l * butterworth_filter <EOL> l [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT:0.0> <EOL> log_gabor [ s , : , : , : ] = l <EOL> bandpass [ s , : , : , : ] = np . fft . ifft2 ( image_fft * l ) <EOL> for e in range ( num_theta_orientations ) : <EOL> elevation_angle = e * np . pi / num_theta_orientations <EOL> d_theta_sin = ( sin_theta * np . cos ( elevation_angle ) - <EOL> cos_theta * np . sin ( elevation_angle ) ) <EOL> d_theta_cos = ( cos_theta * np . cos ( elevation_angle ) + <EOL> sin_theta * np . sin ( elevation_angle ) ) <EOL> d_theta = np . abs ( np . arctan2 ( d_theta_sin , d_theta_cos ) ) <EOL> for a in range ( num_phi_orientations ) : <EOL> azimuth_angle = a * <NUM_LIT:2> * np . pi / num_phi_orientations <EOL> d_phi_sin = ( sin_phi * np . cos ( azimuth_angle ) - <EOL> cos_phi * np . sin ( azimuth_angle ) ) <EOL> d_phi_cos = ( cos_phi * np . cos ( azimuth_angle ) + <EOL> sin_phi * np . sin ( azimuth_angle ) ) <EOL> d_phi = np . abs ( np . arctan2 ( d_phi_sin , d_phi_cos ) ) <EOL> phi_spread = ( - d_phi ** <NUM_LIT:2> ) / ( <NUM_LIT:2> * phi_sigma ** <NUM_LIT:2> ) <EOL> theta_spread = ( - d_theta ** <NUM_LIT:2> ) / ( <NUM_LIT:2> * theta_sigma ** <NUM_LIT:2> ) <EOL> spread = np . exp ( phi_spread + theta_spread ) <EOL> for s in range ( <NUM_LIT:0> , num_scales ) : <EOL> filter_bank = log_gabor [ s ] * spread <EOL> shifted_filter = np . fft . fftshift ( filter_bank ) <EOL> S += shifted_filter * np . conjugate ( shifted_filter ) <EOL> tmp_complex_conv [ s , : , : ] = np . fft . ifft2 ( image_fft * <EOL> filter_bank ) <EOL> complex_conv [ : , e , a , : , : ] = tmp_complex_conv [ None , None , ... ] <EOL> return complex_conv , bandpass , S <EOL> def __log_gabor_2d ( image , num_scales = <NUM_LIT:4> , num_orientations = <NUM_LIT:6> , <EOL> min_wavelength = <NUM_LIT:3> , scaling_constant = <NUM_LIT:2> , center_sigma = <NUM_LIT> , <EOL> d_phi_sigma = <NUM_LIT> ) : <EOL> bandpass = np . empty ( [ num_scales , image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] ] , <EOL> dtype = np . complex ) <EOL> log_gabor = np . empty ( [ num_scales , image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] ] ) <EOL> S = np . zeros ( image . shape ) <EOL> complex_conv = np . empty ( [ num_scales , num_orientations , image . shape [ <NUM_LIT:0> ] , <EOL> image . shape [ <NUM_LIT:1> ] ] , dtype = np . complex ) <EOL> tmp_complex_conv = np . empty ( [ num_scales , image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] ] , <EOL> dtype = np . complex ) <EOL> phi_sigma = np . pi / num_orientations / d_phi_sigma <EOL> image_fft = np . fft . fft2 ( image ) <EOL> axis0 , axis1 = __adjusted_meshgrid ( image . shape ) <EOL> radius = np . sqrt ( axis0 ** <NUM_LIT:2> + axis1 ** <NUM_LIT:2> ) <EOL> phi = np . arctan2 ( axis0 , axis1 ) <EOL> radius = np . fft . ifftshift ( radius ) <EOL> radius [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = <NUM_LIT:1.0> <EOL> phi = np . fft . ifftshift ( phi ) <EOL> sin_phi = np . sin ( phi ) <EOL> cos_phi = np . cos ( phi ) <EOL> butterworth_filter = __frequency_butterworth_filter ( image . shape , <NUM_LIT> , <NUM_LIT:15> ) <EOL> for s in range ( num_scales ) : <EOL> wavelength = min_wavelength * scaling_constant ** s <EOL> fo = <NUM_LIT:1.0> / wavelength <EOL> l = np . exp ( ( - ( np . log ( radius / fo ) ) ** <NUM_LIT:2> ) / <EOL> ( <NUM_LIT> * np . log ( center_sigma ) ** <NUM_LIT:2> ) ) <EOL> l = l * butterworth_filter <EOL> l [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = <NUM_LIT:0.0> <EOL> log_gabor [ s , : , : ] = l <EOL> bandpass [ s , : , : ] = np . fft . ifft2 ( image_fft * l ) <EOL> for o in range ( num_orientations ) : <EOL> filter_angle = o * np . pi / num_orientations <EOL> ds = ( sin_phi * np . cos ( filter_angle ) - <EOL> cos_phi * np . sin ( filter_angle ) ) <EOL> dc = ( cos_phi * np . cos ( filter_angle ) + <EOL> sin_phi * np . sin ( filter_angle ) ) <EOL> d_phi = np . abs ( np . arctan2 ( ds , dc ) ) <EOL> spread = np . exp ( ( - d_phi ** <NUM_LIT> ) / ( <NUM_LIT> * phi_sigma ** <NUM_LIT:2> ) ) <EOL> for s in range ( <NUM_LIT:0> , num_scales ) : <EOL> filter_bank = log_gabor [ s ] * spread <EOL> shifted_filter = np . fft . fftshift ( filter_bank ) <EOL> S += shifted_filter * np . conjugate ( shifted_filter ) <EOL> tmp_complex_conv [ s , : , : ] = np . fft . ifft2 ( image_fft * filter_bank ) <EOL> complex_conv [ : , o , : , : ] = tmp_complex_conv [ None , ... ] <EOL> return complex_conv , bandpass , np . flipud ( S ) </s>
