<s> '''<STR_LIT>''' <EOL> import matplotlib . cm <EOL> import numpy as np <EOL> import scipy . ndimage as scind <EOL> from centrosome . cpmorphology import fixup_scipy_ndimage_result as fix <EOL> from centrosome . cpmorphology import strel_disk , centers_of_labels <EOL> from centrosome . outline import outline <EOL> import cellprofiler . cpimage as cpi <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . measurements as cpmeas <EOL> import cellprofiler . objects as cpo <EOL> import cellprofiler . preferences as cpprefs <EOL> import cellprofiler . settings as cps <EOL> import cellprofiler . workspace as cpw <EOL> from cellprofiler . settings import YES , NO <EOL> D_ADJACENT = '<STR_LIT>' <EOL> D_EXPAND = '<STR_LIT>' <EOL> D_WITHIN = '<STR_LIT>' <EOL> D_ALL = [ D_ADJACENT , D_EXPAND , D_WITHIN ] <EOL> M_NUMBER_OF_NEIGHBORS = '<STR_LIT>' <EOL> M_PERCENT_TOUCHING = '<STR_LIT>' <EOL> M_FIRST_CLOSEST_OBJECT_NUMBER = '<STR_LIT>' <EOL> M_FIRST_CLOSEST_DISTANCE = '<STR_LIT>' <EOL> M_SECOND_CLOSEST_OBJECT_NUMBER = '<STR_LIT>' <EOL> M_SECOND_CLOSEST_DISTANCE = '<STR_LIT>' <EOL> M_ANGLE_BETWEEN_NEIGHBORS = '<STR_LIT>' <EOL> M_ALL = [ M_NUMBER_OF_NEIGHBORS , M_PERCENT_TOUCHING , <EOL> M_FIRST_CLOSEST_OBJECT_NUMBER , M_FIRST_CLOSEST_DISTANCE , <EOL> M_SECOND_CLOSEST_OBJECT_NUMBER , M_SECOND_CLOSEST_DISTANCE , <EOL> M_ANGLE_BETWEEN_NEIGHBORS ] <EOL> C_NEIGHBORS = '<STR_LIT>' <EOL> S_EXPANDED = '<STR_LIT>' <EOL> S_ADJACENT = '<STR_LIT>' <EOL> class MeasureObjectNeighbors ( cpm . CPModule ) : <EOL> module_name = '<STR_LIT>' <EOL> category = "<STR_LIT>" <EOL> variable_revision_number = <NUM_LIT:2> <EOL> def create_settings ( self ) : <EOL> self . object_name = cps . ObjectNameSubscriber ( <EOL> '<STR_LIT>' , cps . NONE , doc = """<STR_LIT>""" ) <EOL> self . neighbors_name = cps . ObjectNameSubscriber ( <EOL> '<STR_LIT>' , cps . NONE , doc = """<STR_LIT>""" ) <EOL> self . distance_method = cps . Choice ( <EOL> '<STR_LIT>' , <EOL> D_ALL , D_EXPAND , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . distance = cps . Integer ( <EOL> '<STR_LIT>' , <NUM_LIT:5> , <NUM_LIT:1> , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . wants_count_image = cps . Binary ( <EOL> '<STR_LIT>' , <EOL> False , doc = """<STR_LIT>""" ) <EOL> self . count_image_name = cps . ImageNameProvider ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> self . count_colormap = cps . Colormap ( <EOL> '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> self . wants_percent_touching_image = cps . Binary ( <EOL> '<STR_LIT>' , <EOL> False , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . touching_image_name = cps . ImageNameProvider ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> self . touching_colormap = cps . Colormap ( <EOL> '<STR_LIT>' , doc = """<STR_LIT>""" ) <EOL> def settings ( self ) : <EOL> return [ self . object_name , self . neighbors_name , <EOL> self . distance_method , self . distance , <EOL> self . wants_count_image , self . count_image_name , <EOL> self . count_colormap , self . wants_percent_touching_image , <EOL> self . touching_image_name , self . touching_colormap ] <EOL> def visible_settings ( self ) : <EOL> result = [ self . object_name , self . neighbors_name , self . distance_method ] <EOL> if self . distance_method == D_WITHIN : <EOL> result += [ self . distance ] <EOL> result += [ self . wants_count_image ] <EOL> if self . wants_count_image . value : <EOL> result += [ self . count_image_name , self . count_colormap ] <EOL> if self . neighbors_are_objects : <EOL> result += [ self . wants_percent_touching_image ] <EOL> if self . wants_percent_touching_image . value : <EOL> result += [ self . touching_image_name , self . touching_colormap ] <EOL> return result <EOL> @ property <EOL> def neighbors_are_objects ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . object_name . value == self . neighbors_name . value <EOL> def run ( self , workspace ) : <EOL> objects = workspace . object_set . get_objects ( self . object_name . value ) <EOL> assert isinstance ( objects , cpo . Objects ) <EOL> has_pixels = objects . areas > <NUM_LIT:0> <EOL> labels = objects . small_removed_segmented <EOL> kept_labels = objects . segmented <EOL> neighbor_objects = workspace . object_set . get_objects ( <EOL> self . neighbors_name . value ) <EOL> assert isinstance ( neighbor_objects , cpo . Objects ) <EOL> neighbor_labels = neighbor_objects . small_removed_segmented <EOL> unedited_segmented = neighbor_objects . unedited_segmented <EOL> touching_border = np . zeros ( np . max ( unedited_segmented ) + <NUM_LIT:1> , bool ) <EOL> touching_border [ unedited_segmented [ <NUM_LIT:0> , : ] ] = True <EOL> touching_border [ unedited_segmented [ - <NUM_LIT:1> , : ] ] = True <EOL> touching_border [ unedited_segmented [ : , <NUM_LIT:0> ] ] = True <EOL> touching_border [ unedited_segmented [ : , - <NUM_LIT:1> ] ] = True <EOL> touching_border [ <NUM_LIT:0> ] = False <EOL> touching_border_mask = touching_border [ unedited_segmented ] <EOL> nobjects = np . max ( labels ) <EOL> nkept_objects = objects . count <EOL> nneighbors = np . max ( neighbor_labels ) <EOL> if np . any ( touching_border ) and np . all ( ~ touching_border_mask [ neighbor_labels != <NUM_LIT:0> ] ) : <EOL> touching_border_object_number = np . cumsum ( touching_border ) + np . max ( neighbor_labels ) <EOL> touching_border_mask = touching_border_mask & ( neighbor_labels == <NUM_LIT:0> ) <EOL> neighbor_labels = neighbor_labels . copy ( ) . astype ( np . int32 ) <EOL> neighbor_labels [ touching_border_mask ] = touching_border_object_number [ <EOL> unedited_segmented [ touching_border_mask ] ] <EOL> _ , object_numbers = objects . relate_labels ( labels , kept_labels ) <EOL> if self . neighbors_are_objects : <EOL> neighbor_numbers = object_numbers <EOL> neighbor_has_pixels = has_pixels <EOL> else : <EOL> _ , neighbor_numbers = neighbor_objects . relate_labels ( <EOL> neighbor_labels , neighbor_objects . segmented ) <EOL> neighbor_has_pixels = np . bincount ( neighbor_labels . ravel ( ) ) [ <NUM_LIT:1> : ] > <NUM_LIT:0> <EOL> neighbor_count = np . zeros ( ( nobjects , ) ) <EOL> pixel_count = np . zeros ( ( nobjects , ) ) <EOL> first_object_number = np . zeros ( ( nobjects , ) , int ) <EOL> second_object_number = np . zeros ( ( nobjects , ) , int ) <EOL> first_x_vector = np . zeros ( ( nobjects , ) ) <EOL> second_x_vector = np . zeros ( ( nobjects , ) ) <EOL> first_y_vector = np . zeros ( ( nobjects , ) ) <EOL> second_y_vector = np . zeros ( ( nobjects , ) ) <EOL> angle = np . zeros ( ( nobjects , ) ) <EOL> percent_touching = np . zeros ( ( nobjects , ) ) <EOL> expanded_labels = None <EOL> if self . distance_method == D_EXPAND : <EOL> i , j = scind . distance_transform_edt ( labels == <NUM_LIT:0> , <EOL> return_distances = False , <EOL> return_indices = True ) <EOL> labels = labels [ i , j ] <EOL> expanded_labels = labels <EOL> distance = <NUM_LIT:1> <EOL> scale = S_EXPANDED <EOL> if self . neighbors_are_objects : <EOL> neighbor_labels = labels . copy ( ) <EOL> elif self . distance_method == D_WITHIN : <EOL> distance = self . distance . value <EOL> scale = str ( distance ) <EOL> elif self . distance_method == D_ADJACENT : <EOL> distance = <NUM_LIT:1> <EOL> scale = S_ADJACENT <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . distance_method . value ) <EOL> if nneighbors > ( <NUM_LIT:1> if self . neighbors_are_objects else <NUM_LIT:0> ) : <EOL> first_objects = [ ] <EOL> second_objects = [ ] <EOL> object_indexes = np . arange ( nobjects , dtype = np . int32 ) + <NUM_LIT:1> <EOL> ocenters = centers_of_labels ( <EOL> objects . small_removed_segmented ) . transpose ( ) <EOL> ncenters = centers_of_labels ( <EOL> neighbor_objects . small_removed_segmented ) . transpose ( ) <EOL> areas = fix ( scind . sum ( np . ones ( labels . shape ) , labels , object_indexes ) ) <EOL> perimeter_outlines = outline ( labels ) <EOL> perimeters = fix ( scind . sum ( <EOL> np . ones ( labels . shape ) , perimeter_outlines , object_indexes ) ) <EOL> i , j = np . mgrid [ <NUM_LIT:0> : nobjects , <NUM_LIT:0> : nneighbors ] <EOL> distance_matrix = np . sqrt ( ( ocenters [ i , <NUM_LIT:0> ] - ncenters [ j , <NUM_LIT:0> ] ) ** <NUM_LIT:2> + <EOL> ( ocenters [ i , <NUM_LIT:1> ] - ncenters [ j , <NUM_LIT:1> ] ) ** <NUM_LIT:2> ) <EOL> if distance_matrix . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> order = np . zeros ( distance_matrix . shape , int ) <EOL> else : <EOL> order = np . lexsort ( [ distance_matrix ] ) <EOL> first_neighbor = <NUM_LIT:1> if self . neighbors_are_objects else <NUM_LIT:0> <EOL> first_object_index = order [ : , first_neighbor ] <EOL> first_x_vector = ncenters [ first_object_index , <NUM_LIT:1> ] - ocenters [ : , <NUM_LIT:1> ] <EOL> first_y_vector = ncenters [ first_object_index , <NUM_LIT:0> ] - ocenters [ : , <NUM_LIT:0> ] <EOL> if nneighbors > first_neighbor + <NUM_LIT:1> : <EOL> second_object_index = order [ : , first_neighbor + <NUM_LIT:1> ] <EOL> second_x_vector = ncenters [ second_object_index , <NUM_LIT:1> ] - ocenters [ : , <NUM_LIT:1> ] <EOL> second_y_vector = ncenters [ second_object_index , <NUM_LIT:0> ] - ocenters [ : , <NUM_LIT:0> ] <EOL> v1 = np . array ( ( first_x_vector , first_y_vector ) ) <EOL> v2 = np . array ( ( second_x_vector , second_y_vector ) ) <EOL> dot = ( np . sum ( v1 * v2 , <NUM_LIT:0> ) / <EOL> np . sqrt ( np . sum ( v1 ** <NUM_LIT:2> , <NUM_LIT:0> ) * np . sum ( v2 ** <NUM_LIT:2> , <NUM_LIT:0> ) ) ) <EOL> angle = np . arccos ( dot ) * <NUM_LIT> / np . pi <EOL> strel = strel_disk ( distance ) <EOL> strel_touching = strel_disk ( distance + <NUM_LIT> ) <EOL> i , j = np . mgrid [ <NUM_LIT:0> : labels . shape [ <NUM_LIT:0> ] , <NUM_LIT:0> : labels . shape [ <NUM_LIT:1> ] ] <EOL> min_i , max_i , min_i_pos , max_i_pos = scind . extrema ( i , labels , object_indexes ) <EOL> min_j , max_j , min_j_pos , max_j_pos = scind . extrema ( j , labels , object_indexes ) <EOL> min_i = np . maximum ( fix ( min_i ) - distance , <NUM_LIT:0> ) . astype ( int ) <EOL> max_i = np . minimum ( fix ( max_i ) + distance + <NUM_LIT:1> , labels . shape [ <NUM_LIT:0> ] ) . astype ( int ) <EOL> min_j = np . maximum ( fix ( min_j ) - distance , <NUM_LIT:0> ) . astype ( int ) <EOL> max_j = np . minimum ( fix ( max_j ) + distance + <NUM_LIT:1> , labels . shape [ <NUM_LIT:1> ] ) . astype ( int ) <EOL> for object_number in object_numbers : <EOL> if object_number == <NUM_LIT:0> : <EOL> continue <EOL> index = object_number - <NUM_LIT:1> <EOL> patch = labels [ min_i [ index ] : max_i [ index ] , <EOL> min_j [ index ] : max_j [ index ] ] <EOL> npatch = neighbor_labels [ min_i [ index ] : max_i [ index ] , <EOL> min_j [ index ] : max_j [ index ] ] <EOL> patch_mask = patch == ( index + <NUM_LIT:1> ) <EOL> extended = scind . binary_dilation ( patch_mask , strel ) <EOL> neighbors = np . unique ( npatch [ extended ] ) <EOL> neighbors = neighbors [ neighbors != <NUM_LIT:0> ] <EOL> if self . neighbors_are_objects : <EOL> neighbors = neighbors [ neighbors != object_number ] <EOL> nc = len ( neighbors ) <EOL> neighbor_count [ index ] = nc <EOL> if nc > <NUM_LIT:0> : <EOL> first_objects . append ( np . ones ( nc , int ) * object_number ) <EOL> second_objects . append ( neighbors ) <EOL> if self . neighbors_are_objects : <EOL> outline_patch = perimeter_outlines [ <EOL> min_i [ index ] : max_i [ index ] , <EOL> min_j [ index ] : max_j [ index ] ] == object_number <EOL> extended = scind . binary_dilation ( <EOL> ( patch != <NUM_LIT:0> ) & ( patch != object_number ) , strel_touching ) <EOL> overlap = np . sum ( outline_patch & extended ) <EOL> pixel_count [ index ] = overlap <EOL> if sum ( [ len ( x ) for x in first_objects ] ) > <NUM_LIT:0> : <EOL> first_objects = np . hstack ( first_objects ) <EOL> reverse_object_numbers = np . zeros ( <EOL> max ( np . max ( object_numbers ) , np . max ( first_objects ) ) + <NUM_LIT:1> , int ) <EOL> reverse_object_numbers [ object_numbers ] = np . arange ( len ( object_numbers ) ) + <NUM_LIT:1> <EOL> first_objects = reverse_object_numbers [ first_objects ] <EOL> second_objects = np . hstack ( second_objects ) <EOL> reverse_neighbor_numbers = np . zeros ( <EOL> max ( np . max ( neighbor_numbers ) , np . max ( second_objects ) ) + <NUM_LIT:1> , int ) <EOL> reverse_neighbor_numbers [ neighbor_numbers ] = np . arange ( len ( neighbor_numbers ) ) + <NUM_LIT:1> <EOL> second_objects = reverse_neighbor_numbers [ second_objects ] <EOL> to_keep = ( first_objects > <NUM_LIT:0> ) & ( second_objects > <NUM_LIT:0> ) <EOL> first_objects = first_objects [ to_keep ] <EOL> second_objects = second_objects [ to_keep ] <EOL> else : <EOL> first_objects = np . zeros ( <NUM_LIT:0> , int ) <EOL> second_objects = np . zeros ( <NUM_LIT:0> , int ) <EOL> if self . neighbors_are_objects : <EOL> percent_touching = pixel_count * <NUM_LIT:100> / perimeters <EOL> else : <EOL> percent_touching = pixel_count * <NUM_LIT> / areas <EOL> object_indexes = object_numbers - <NUM_LIT:1> <EOL> neighbor_indexes = neighbor_numbers - <NUM_LIT:1> <EOL> first_object_number = np . zeros ( nkept_objects , int ) <EOL> second_object_number = np . zeros ( nkept_objects , int ) <EOL> if nkept_objects > ( <NUM_LIT:1> if self . neighbors_are_objects else <NUM_LIT:0> ) : <EOL> di = ( ocenters [ object_indexes [ : , np . newaxis ] , <NUM_LIT:0> ] - <EOL> ncenters [ neighbor_indexes [ np . newaxis , : ] , <NUM_LIT:0> ] ) <EOL> dj = ( ocenters [ object_indexes [ : , np . newaxis ] , <NUM_LIT:1> ] - <EOL> ncenters [ neighbor_indexes [ np . newaxis , : ] , <NUM_LIT:1> ] ) <EOL> distance_matrix = np . sqrt ( di * di + dj * dj ) <EOL> distance_matrix [ ~ has_pixels , : ] = np . inf <EOL> distance_matrix [ : , ~ neighbor_has_pixels ] = np . inf <EOL> order = np . lexsort ( [ distance_matrix ] ) . astype ( <EOL> first_object_number . dtype ) <EOL> if self . neighbors_are_objects : <EOL> first_object_number [ has_pixels ] = order [ has_pixels , <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> if nkept_objects > <NUM_LIT:2> : <EOL> second_object_number [ has_pixels ] = order [ has_pixels , <NUM_LIT:2> ] + <NUM_LIT:1> <EOL> else : <EOL> first_object_number [ has_pixels ] = order [ has_pixels , <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> if order . shape [ <NUM_LIT:1> ] > <NUM_LIT:1> : <EOL> second_object_number [ has_pixels ] = order [ has_pixels , <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> else : <EOL> object_indexes = object_numbers - <NUM_LIT:1> <EOL> neighbor_indexes = neighbor_numbers - <NUM_LIT:1> <EOL> first_objects = np . zeros ( <NUM_LIT:0> , int ) <EOL> second_objects = np . zeros ( <NUM_LIT:0> , int ) <EOL> neighbor_count = neighbor_count [ object_indexes ] <EOL> neighbor_count [ ~ has_pixels ] = <NUM_LIT:0> <EOL> percent_touching = percent_touching [ object_indexes ] <EOL> percent_touching [ ~ has_pixels ] = <NUM_LIT:0> <EOL> first_x_vector = first_x_vector [ object_indexes ] <EOL> second_x_vector = second_x_vector [ object_indexes ] <EOL> first_y_vector = first_y_vector [ object_indexes ] <EOL> second_y_vector = second_y_vector [ object_indexes ] <EOL> angle = angle [ object_indexes ] <EOL> assert ( isinstance ( workspace , cpw . Workspace ) ) <EOL> m = workspace . measurements <EOL> assert ( isinstance ( m , cpmeas . Measurements ) ) <EOL> image_set = workspace . image_set <EOL> features_and_data = [ <EOL> ( M_NUMBER_OF_NEIGHBORS , neighbor_count ) , <EOL> ( M_FIRST_CLOSEST_OBJECT_NUMBER , first_object_number ) , <EOL> ( M_FIRST_CLOSEST_DISTANCE , np . sqrt ( first_x_vector ** <NUM_LIT:2> + first_y_vector ** <NUM_LIT:2> ) ) , <EOL> ( M_SECOND_CLOSEST_OBJECT_NUMBER , second_object_number ) , <EOL> ( M_SECOND_CLOSEST_DISTANCE , np . sqrt ( second_x_vector ** <NUM_LIT:2> + second_y_vector ** <NUM_LIT:2> ) ) , <EOL> ( M_ANGLE_BETWEEN_NEIGHBORS , angle ) ] <EOL> if self . neighbors_are_objects : <EOL> features_and_data . append ( ( M_PERCENT_TOUCHING , percent_touching ) ) <EOL> for feature_name , data in features_and_data : <EOL> m . add_measurement ( self . object_name . value , <EOL> self . get_measurement_name ( feature_name ) , <EOL> data ) <EOL> if len ( first_objects ) > <NUM_LIT:0> : <EOL> m . add_relate_measurement ( <EOL> self . module_num , <EOL> cpmeas . NEIGHBORS , <EOL> self . object_name . value , <EOL> self . object_name . value if self . neighbors_are_objects <EOL> else self . neighbors_name . value , <EOL> m . image_set_number * np . ones ( first_objects . shape , int ) , <EOL> first_objects , <EOL> m . image_set_number * np . ones ( second_objects . shape , int ) , <EOL> second_objects ) <EOL> labels = kept_labels <EOL> neighbor_count_image = np . zeros ( labels . shape , int ) <EOL> object_mask = objects . segmented != <NUM_LIT:0> <EOL> object_indexes = objects . segmented [ object_mask ] - <NUM_LIT:1> <EOL> neighbor_count_image [ object_mask ] = neighbor_count [ object_indexes ] <EOL> workspace . display_data . neighbor_count_image = neighbor_count_image <EOL> if self . neighbors_are_objects : <EOL> percent_touching_image = np . zeros ( labels . shape ) <EOL> percent_touching_image [ object_mask ] = percent_touching [ object_indexes ] <EOL> workspace . display_data . percent_touching_image = percent_touching_image <EOL> image_set = workspace . image_set <EOL> if self . wants_count_image . value : <EOL> neighbor_cm_name = self . count_colormap . value <EOL> neighbor_cm = get_colormap ( neighbor_cm_name ) <EOL> sm = matplotlib . cm . ScalarMappable ( cmap = neighbor_cm ) <EOL> img = sm . to_rgba ( neighbor_count_image ) [ : , : , : <NUM_LIT:3> ] <EOL> img [ : , : , <NUM_LIT:0> ] [ ~ object_mask ] = <NUM_LIT:0> <EOL> img [ : , : , <NUM_LIT:1> ] [ ~ object_mask ] = <NUM_LIT:0> <EOL> img [ : , : , <NUM_LIT:2> ] [ ~ object_mask ] = <NUM_LIT:0> <EOL> count_image = cpi . Image ( img , masking_objects = objects ) <EOL> image_set . add ( self . count_image_name . value , count_image ) <EOL> else : <EOL> neighbor_cm_name = cpprefs . get_default_colormap ( ) <EOL> neighbor_cm = matplotlib . cm . get_cmap ( neighbor_cm_name ) <EOL> if self . neighbors_are_objects and self . wants_percent_touching_image : <EOL> percent_touching_cm_name = self . touching_colormap . value <EOL> percent_touching_cm = get_colormap ( percent_touching_cm_name ) <EOL> sm = matplotlib . cm . ScalarMappable ( cmap = percent_touching_cm ) <EOL> img = sm . to_rgba ( percent_touching_image ) [ : , : , : <NUM_LIT:3> ] <EOL> img [ : , : , <NUM_LIT:0> ] [ ~ object_mask ] = <NUM_LIT:0> <EOL> img [ : , : , <NUM_LIT:1> ] [ ~ object_mask ] = <NUM_LIT:0> <EOL> img [ : , : , <NUM_LIT:2> ] [ ~ object_mask ] = <NUM_LIT:0> <EOL> touching_image = cpi . Image ( img , masking_objects = objects ) <EOL> image_set . add ( self . touching_image_name . value , <EOL> touching_image ) <EOL> else : <EOL> percent_touching_cm_name = cpprefs . get_default_colormap ( ) <EOL> percent_touching_cm = matplotlib . cm . get_cmap ( percent_touching_cm_name ) <EOL> if self . show_window : <EOL> workspace . display_data . neighbor_cm_name = neighbor_cm_name <EOL> workspace . display_data . percent_touching_cm_name = percent_touching_cm_name <EOL> workspace . display_data . orig_labels = objects . segmented <EOL> workspace . display_data . expanded_labels = expanded_labels <EOL> workspace . display_data . object_mask = object_mask <EOL> def display ( self , workspace , figure ) : <EOL> figure . set_subplots ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> figure . subplot_imshow_labels ( <NUM_LIT:0> , <NUM_LIT:0> , workspace . display_data . orig_labels , <EOL> "<STR_LIT>" % self . object_name . value ) <EOL> object_mask = workspace . display_data . object_mask <EOL> expanded_labels = workspace . display_data . expanded_labels <EOL> neighbor_count_image = workspace . display_data . neighbor_count_image <EOL> neighbor_count_image [ ~ object_mask ] = - <NUM_LIT:1> <EOL> neighbor_cm = get_colormap ( workspace . display_data . neighbor_cm_name ) <EOL> neighbor_cm . set_under ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> neighbor_cm = matplotlib . cm . ScalarMappable ( cmap = neighbor_cm ) <EOL> if self . neighbors_are_objects : <EOL> percent_touching_cm = get_colormap ( workspace . display_data . percent_touching_cm_name ) <EOL> percent_touching_cm . set_under ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> percent_touching_image = workspace . display_data . percent_touching_image <EOL> percent_touching_image [ ~ object_mask ] = - <NUM_LIT:1> <EOL> percent_touching_cm = matplotlib . cm . ScalarMappable ( cmap = percent_touching_cm ) <EOL> if np . any ( object_mask ) : <EOL> figure . subplot_imshow ( <NUM_LIT:0> , <NUM_LIT:1> , neighbor_count_image , <EOL> "<STR_LIT>" % <EOL> self . object_name . value , <EOL> colormap = neighbor_cm , <EOL> colorbar = True , vmin = <NUM_LIT:0> , <EOL> vmax = max ( neighbor_count_image . max ( ) , <NUM_LIT:1> ) , <EOL> normalize = False , <EOL> sharexy = figure . subplot ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> if self . neighbors_are_objects : <EOL> figure . subplot_imshow ( <NUM_LIT:1> , <NUM_LIT:1> , percent_touching_image , <EOL> "<STR_LIT>" % <EOL> self . object_name . value , <EOL> colormap = percent_touching_cm , <EOL> colorbar = True , vmin = <NUM_LIT:0> , <EOL> vmax = max ( percent_touching_image . max ( ) , <NUM_LIT:1> ) , <EOL> normalize = False , <EOL> sharexy = figure . subplot ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> else : <EOL> figure . subplot_imshow ( <NUM_LIT:0> , <NUM_LIT:1> , neighbor_count_image , <EOL> "<STR_LIT>" % <EOL> self . object_name . value , <EOL> colormap = neighbor_cm , <EOL> vmin = <NUM_LIT:0> , <EOL> vmax = max ( neighbor_count_image . max ( ) , <NUM_LIT:1> ) , <EOL> sharexy = figure . subplot ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> if self . neighbors_are_objects : <EOL> figure . subplot_imshow ( <NUM_LIT:1> , <NUM_LIT:1> , percent_touching_image , <EOL> "<STR_LIT>" % <EOL> self . object_name . value , <EOL> colormap = percent_touching_cm , <EOL> vmin = <NUM_LIT:0> , <EOL> vmax = max ( neighbor_count_image . max ( ) , <NUM_LIT:1> ) , <EOL> sharexy = figure . subplot ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> if self . distance_method == D_EXPAND : <EOL> figure . subplot_imshow_labels ( <NUM_LIT:1> , <NUM_LIT:0> , expanded_labels , <EOL> "<STR_LIT>" % <EOL> self . object_name . value , <EOL> sharexy = figure . subplot ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> @ property <EOL> def all_features ( self ) : <EOL> if self . neighbors_are_objects : <EOL> return M_ALL <EOL> else : <EOL> return filter ( lambda x : x != M_PERCENT_TOUCHING , M_ALL ) <EOL> def get_measurement_name ( self , feature ) : <EOL> if self . distance_method == D_EXPAND : <EOL> scale = S_EXPANDED <EOL> elif self . distance_method == D_WITHIN : <EOL> scale = str ( self . distance . value ) <EOL> elif self . distance_method == D_ADJACENT : <EOL> scale = S_ADJACENT <EOL> if self . neighbors_are_objects : <EOL> return "<STR_LIT:_>" . join ( ( C_NEIGHBORS , feature , scale ) ) <EOL> else : <EOL> return "<STR_LIT:_>" . join ( ( C_NEIGHBORS , feature , <EOL> self . neighbors_name . value , scale ) ) <EOL> def get_measurement_columns ( self , pipeline ) : <EOL> '''<STR_LIT>''' <EOL> coltypes = dict ( [ ( feature , <EOL> cpmeas . COLTYPE_INTEGER <EOL> if feature in ( M_NUMBER_OF_NEIGHBORS , <EOL> M_FIRST_CLOSEST_OBJECT_NUMBER , <EOL> M_SECOND_CLOSEST_OBJECT_NUMBER ) <EOL> else cpmeas . COLTYPE_FLOAT ) <EOL> for feature in self . all_features ] ) <EOL> return [ ( self . object_name . value , <EOL> self . get_measurement_name ( feature_name ) , <EOL> coltypes [ feature_name ] ) <EOL> for feature_name in self . all_features ] <EOL> def get_object_relationships ( self , pipeline ) : <EOL> '''<STR_LIT>''' <EOL> objects_name = self . object_name . value <EOL> if self . neighbors_are_objects : <EOL> neighbors_name = objects_name <EOL> else : <EOL> neighbors_name = self . neighbors_name . value <EOL> return [ ( cpmeas . NEIGHBORS , objects_name , neighbors_name , <EOL> cpmeas . MCA_AVAILABLE_EACH_CYCLE ) ] <EOL> def get_categories ( self , pipeline , object_name ) : <EOL> if object_name == self . object_name : <EOL> return [ C_NEIGHBORS ] <EOL> return [ ] <EOL> def get_measurements ( self , pipeline , object_name , category ) : <EOL> if object_name == self . object_name and category == C_NEIGHBORS : <EOL> return filter ( lambda x : ( x is not M_PERCENT_TOUCHING <EOL> or self . neighbors_are_objects ) , M_ALL ) <EOL> return [ ] <EOL> def get_measurement_objects ( self , pipeline , object_name , category , <EOL> measurement ) : <EOL> if ( self . neighbors_are_objects or <EOL> measurement not in self . get_measurements ( pipeline , object_name , category ) ) : <EOL> return [ ] <EOL> return [ self . neighbors_name . value ] <EOL> def get_measurement_scales ( self , pipeline , object_name , category , measurement , image_name ) : <EOL> if measurement in self . get_measurements ( pipeline , object_name , category ) : <EOL> if self . distance_method == D_EXPAND : <EOL> return [ S_EXPANDED ] <EOL> elif self . distance_method == D_ADJACENT : <EOL> return [ S_ADJACENT ] <EOL> elif self . distance_method == D_WITHIN : <EOL> return [ str ( self . distance . value ) ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . distance_method . value ) <EOL> return [ ] <EOL> def upgrade_settings ( self , setting_values , variable_revision_number , module_name , from_matlab ) : <EOL> if from_matlab and variable_revision_number == <NUM_LIT:5> : <EOL> wants_image = setting_values [ <NUM_LIT:2> ] != cps . DO_NOT_USE <EOL> distance_method = D_EXPAND if setting_values [ <NUM_LIT:1> ] == "<STR_LIT:0>" else D_WITHIN <EOL> setting_values = [ setting_values [ <NUM_LIT:0> ] , <EOL> distance_method , <EOL> setting_values [ <NUM_LIT:1> ] , <EOL> cps . YES if wants_image else cps . NO , <EOL> setting_values [ <NUM_LIT:2> ] , <EOL> cps . DEFAULT , <EOL> cps . NO , <EOL> "<STR_LIT>" , <EOL> cps . DEFAULT ] <EOL> from_matlab = False <EOL> variable_revision_number = <NUM_LIT:1> <EOL> if variable_revision_number == <NUM_LIT:1> : <EOL> setting_values = setting_values [ : <NUM_LIT:1> ] * <NUM_LIT:2> + setting_values [ <NUM_LIT:1> : ] <EOL> variable_revision_number = <NUM_LIT:2> <EOL> return setting_values , variable_revision_number , from_matlab <EOL> def get_colormap ( name ) : <EOL> '''<STR_LIT>''' <EOL> if name == cps . DEFAULT : <EOL> name = cpprefs . get_default_colormap ( ) <EOL> return matplotlib . cm . get_cmap ( name ) </s>
