<s> import numpy as np <EOL> import scipy . ndimage as ndi <EOL> from . . filters import rank_order <EOL> def peak_local_max ( image , min_distance = <NUM_LIT:1> , threshold_abs = None , <EOL> threshold_rel = None , exclude_border = True , indices = True , <EOL> num_peaks = np . inf , footprint = None , labels = None ) : <EOL> """<STR_LIT>""" <EOL> if type ( exclude_border ) == bool : <EOL> exclude_border = min_distance if exclude_border else <NUM_LIT:0> <EOL> out = np . zeros_like ( image , dtype = np . bool ) <EOL> if labels is not None : <EOL> label_values = np . unique ( labels ) <EOL> if np . any ( np . diff ( label_values ) != <NUM_LIT:1> ) : <EOL> mask = labels >= <NUM_LIT:1> <EOL> labels [ mask ] = <NUM_LIT:1> + rank_order ( labels [ mask ] ) [ <NUM_LIT:0> ] . astype ( labels . dtype ) <EOL> labels = labels . astype ( np . int32 ) <EOL> label_values = np . unique ( labels ) <EOL> for label in label_values [ label_values != <NUM_LIT:0> ] : <EOL> maskim = ( labels == label ) <EOL> out += peak_local_max ( image * maskim , min_distance = min_distance , <EOL> threshold_abs = threshold_abs , <EOL> threshold_rel = threshold_rel , <EOL> exclude_border = exclude_border , <EOL> indices = False , num_peaks = np . inf , <EOL> footprint = footprint , labels = None ) <EOL> if indices is True : <EOL> return np . transpose ( out . nonzero ( ) ) <EOL> else : <EOL> return out . astype ( np . bool ) <EOL> if np . all ( image == image . flat [ <NUM_LIT:0> ] ) : <EOL> if indices is True : <EOL> return np . empty ( ( <NUM_LIT:0> , <NUM_LIT:2> ) , np . int ) <EOL> else : <EOL> return out <EOL> if footprint is not None : <EOL> image_max = ndi . maximum_filter ( image , footprint = footprint , <EOL> mode = '<STR_LIT>' ) <EOL> else : <EOL> size = <NUM_LIT:2> * min_distance + <NUM_LIT:1> <EOL> image_max = ndi . maximum_filter ( image , size = size , mode = '<STR_LIT>' ) <EOL> mask = image == image_max <EOL> if exclude_border : <EOL> for i in range ( mask . ndim ) : <EOL> mask = mask . swapaxes ( <NUM_LIT:0> , i ) <EOL> remove = ( footprint . shape [ i ] if footprint is not None <EOL> else <NUM_LIT:2> * exclude_border ) <EOL> mask [ : remove // <NUM_LIT:2> ] = mask [ - remove // <NUM_LIT:2> : ] = False <EOL> mask = mask . swapaxes ( <NUM_LIT:0> , i ) <EOL> thresholds = [ ] <EOL> if threshold_abs is None : <EOL> threshold_abs = image . min ( ) <EOL> thresholds . append ( threshold_abs ) <EOL> if threshold_rel is not None : <EOL> thresholds . append ( threshold_rel * image . max ( ) ) <EOL> if thresholds : <EOL> mask &= image > max ( thresholds ) <EOL> coordinates = np . transpose ( mask . nonzero ( ) ) <EOL> if coordinates . shape [ <NUM_LIT:0> ] > num_peaks : <EOL> intensities = image . flat [ np . ravel_multi_index ( coordinates . transpose ( ) , <EOL> image . shape ) ] <EOL> idx_maxsort = np . argsort ( intensities ) [ : : - <NUM_LIT:1> ] <EOL> coordinates = coordinates [ idx_maxsort ] [ : num_peaks ] <EOL> if indices is True : <EOL> return coordinates <EOL> else : <EOL> nd_indices = tuple ( coordinates . T ) <EOL> out [ nd_indices ] = True <EOL> return out </s>
