<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> from centrosome . cpmorphology import opening , closing , white_tophat <EOL> from centrosome . filter import enhance_dark_holes , circular_hough <EOL> from centrosome . filter import hessian , median_filter <EOL> from centrosome . filter import variance_transform , line_integration <EOL> from scipy . ndimage import gaussian_filter <EOL> import cellprofiler . cpimage as cpi <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . settings as cps <EOL> from cellprofiler . gui . help import HELP_ON_MEASURING_DISTANCES , PROTIP_AVOID_ICON <EOL> ENHANCE = '<STR_LIT>' <EOL> SUPPRESS = '<STR_LIT>' <EOL> E_SPECKLES = '<STR_LIT>' <EOL> E_NEURITES = '<STR_LIT>' <EOL> E_DARK_HOLES = '<STR_LIT>' <EOL> E_CIRCLES = '<STR_LIT>' <EOL> E_TEXTURE = '<STR_LIT>' <EOL> E_DIC = '<STR_LIT>' <EOL> S_FAST = "<STR_LIT>" <EOL> S_SLOW = "<STR_LIT>" <EOL> N_GRADIENT = "<STR_LIT>" <EOL> N_TUBENESS = "<STR_LIT>" <EOL> class EnhanceOrSuppressFeatures ( cpm . CPModule ) : <EOL> module_name = '<STR_LIT>' <EOL> category = "<STR_LIT>" <EOL> variable_revision_number = <NUM_LIT:5> <EOL> def create_settings ( self ) : <EOL> self . image_name = cps . ImageNameSubscriber ( <EOL> '<STR_LIT>' , <EOL> cps . NONE , doc = """<STR_LIT>""" ) <EOL> self . filtered_image_name = cps . ImageNameProvider ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> self . method = cps . Choice ( <EOL> '<STR_LIT>' , <EOL> [ ENHANCE , SUPPRESS ] , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . enhance_method = cps . Choice ( <EOL> '<STR_LIT>' , <EOL> [ E_SPECKLES , E_NEURITES , E_DARK_HOLES , E_CIRCLES , E_TEXTURE , E_DIC ] , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . object_size = cps . Integer ( <EOL> '<STR_LIT>' , <NUM_LIT:10> , <NUM_LIT:2> , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . hole_size = cps . IntegerRange ( <EOL> '<STR_LIT>' , value = ( <NUM_LIT:1> , <NUM_LIT:10> ) , minval = <NUM_LIT:1> , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . smoothing = cps . Float ( <EOL> '<STR_LIT>' , value = <NUM_LIT> , minval = <NUM_LIT:0> , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . angle = cps . Float ( <EOL> '<STR_LIT>' , value = <NUM_LIT:0> , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . decay = cps . Float ( <EOL> '<STR_LIT>' , value = <NUM_LIT> , minval = <NUM_LIT:0.1> , maxval = <NUM_LIT:1> , doc = <EOL> """<STR_LIT>""" % globals ( ) ) <EOL> self . neurite_choice = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> [ N_TUBENESS , N_GRADIENT ] , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . speckle_accuracy = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> choices = [ S_FAST , S_SLOW ] , <EOL> doc = """<STR_LIT>""" % globals ( ) ) <EOL> def settings ( self ) : <EOL> return [ self . image_name , self . filtered_image_name , <EOL> self . method , self . object_size , self . enhance_method , <EOL> self . hole_size , self . smoothing , self . angle , self . decay , <EOL> self . neurite_choice , self . speckle_accuracy ] <EOL> def visible_settings ( self ) : <EOL> result = [ self . image_name , self . filtered_image_name , <EOL> self . method ] <EOL> if self . method == ENHANCE : <EOL> result += [ self . enhance_method ] <EOL> self . object_size . min_value = <NUM_LIT:2> <EOL> if self . enhance_method == E_DARK_HOLES : <EOL> result += [ self . hole_size ] <EOL> elif self . enhance_method == E_TEXTURE : <EOL> result += [ self . smoothing ] <EOL> elif self . enhance_method == E_DIC : <EOL> result += [ self . smoothing , self . angle , self . decay ] <EOL> elif self . enhance_method == E_NEURITES : <EOL> result += [ self . neurite_choice ] <EOL> if self . neurite_choice == N_GRADIENT : <EOL> result += [ self . object_size ] <EOL> else : <EOL> result += [ self . smoothing ] <EOL> elif self . enhance_method == E_SPECKLES : <EOL> result += [ self . object_size , self . speckle_accuracy ] <EOL> self . object_size . min_value = <NUM_LIT:3> <EOL> else : <EOL> result += [ self . object_size ] <EOL> else : <EOL> result += [ self . object_size ] <EOL> return result <EOL> def run ( self , workspace ) : <EOL> image = workspace . image_set . get_image ( self . image_name . value , <EOL> must_be_grayscale = True ) <EOL> radius = ( float ( self . object_size . value ) - <NUM_LIT:1.0> ) / <NUM_LIT> <EOL> mask = image . mask if image . has_mask else None <EOL> pixel_data = image . pixel_data <EOL> if self . method == ENHANCE : <EOL> if self . enhance_method == E_SPECKLES : <EOL> if self . speckle_accuracy == S_SLOW or radius <= <NUM_LIT:3> : <EOL> result = white_tophat ( pixel_data , radius , mask ) <EOL> else : <EOL> result = pixel_data - median_filter ( <EOL> median_filter ( pixel_data , mask , radius , percent = <NUM_LIT:0> ) , <EOL> mask , radius , percent = <NUM_LIT:100> ) <EOL> if mask is not None : <EOL> result [ ~ mask ] = pixel_data [ ~ mask ] <EOL> elif self . enhance_method == E_NEURITES : <EOL> if self . neurite_choice == N_GRADIENT : <EOL> result = ( <NUM_LIT:3> * pixel_data - <EOL> opening ( pixel_data , radius , mask ) - <EOL> closing ( pixel_data , radius , mask ) ) <EOL> result [ result > <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> result [ result < <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> else : <EOL> sigma = self . smoothing . value <EOL> smoothed = gaussian_filter ( pixel_data , sigma ) <EOL> L = hessian ( smoothed , return_hessian = False , <EOL> return_eigenvectors = False ) <EOL> result = - L [ : , : , <NUM_LIT:0> ] * ( L [ : , : , <NUM_LIT:0> ] < <NUM_LIT:0> ) * sigma * sigma <EOL> if image . has_mask : <EOL> result [ ~ mask ] = pixel_data [ ~ mask ] <EOL> elif self . enhance_method == E_DARK_HOLES : <EOL> min_radius = max ( <NUM_LIT:1> , int ( self . hole_size . min / <NUM_LIT:2> ) ) <EOL> max_radius = int ( ( self . hole_size . max + <NUM_LIT:1> ) / <NUM_LIT:2> ) <EOL> result = enhance_dark_holes ( pixel_data , min_radius , <EOL> max_radius , mask ) <EOL> elif self . enhance_method == E_CIRCLES : <EOL> result = circular_hough ( pixel_data , radius + <NUM_LIT> , mask = mask ) <EOL> elif self . enhance_method == E_TEXTURE : <EOL> result = variance_transform ( pixel_data , <EOL> self . smoothing . value , <EOL> mask = mask ) <EOL> elif self . enhance_method == E_DIC : <EOL> result = line_integration ( pixel_data , <EOL> self . angle . value , <EOL> self . decay . value , <EOL> self . smoothing . value ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % <EOL> self . enhance_method . value ) <EOL> elif self . method == SUPPRESS : <EOL> if image . has_mask : <EOL> result = opening ( image . pixel_data , radius , image . mask ) <EOL> else : <EOL> result = opening ( image . pixel_data , radius ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % self . method ) <EOL> result_image = cpi . Image ( result , parent_image = image ) <EOL> workspace . image_set . add ( self . filtered_image_name . value , result_image ) <EOL> if self . show_window : <EOL> workspace . display_data . image = image . pixel_data <EOL> workspace . display_data . result = result <EOL> def display ( self , workspace , figure ) : <EOL> image = workspace . display_data . image <EOL> result = workspace . display_data . result <EOL> figure . set_subplots ( ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> figure . subplot_imshow_grayscale ( <NUM_LIT:0> , <NUM_LIT:0> , image , <EOL> "<STR_LIT>" % self . image_name . value ) <EOL> figure . subplot_imshow_grayscale ( <NUM_LIT:1> , <NUM_LIT:0> , result , <EOL> "<STR_LIT>" % self . filtered_image_name . value , <EOL> sharexy = figure . subplot ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> def upgrade_settings ( self , setting_values , variable_revision_number , <EOL> module_name , from_matlab ) : <EOL> '''<STR_LIT>''' <EOL> if not from_matlab and variable_revision_number == <NUM_LIT:1> : <EOL> setting_values = setting_values + [ E_SPECKLES , "<STR_LIT>" ] <EOL> variable_revision_number = <NUM_LIT:2> <EOL> if not from_matlab and variable_revision_number == <NUM_LIT:2> : <EOL> setting_values = setting_values + [ "<STR_LIT>" , "<STR_LIT:0>" , "<STR_LIT>" ] <EOL> variable_revision_number = <NUM_LIT:3> <EOL> if not from_matlab and variable_revision_number == <NUM_LIT:3> : <EOL> setting_values = setting_values + [ N_GRADIENT ] <EOL> variable_revision_number = <NUM_LIT:4> <EOL> if not from_matlab and variable_revision_number == <NUM_LIT:4> : <EOL> setting_values = setting_values + [ S_SLOW ] <EOL> variable_revision_number = <NUM_LIT:5> <EOL> return setting_values , variable_revision_number , from_matlab <EOL> EnhanceOrSuppressSpeckles = EnhanceOrSuppressFeatures </s>
