<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import scipy . ndimage as scind <EOL> import scipy . stats as scistat <EOL> from centrosome . cpmorphology import fixup_scipy_ndimage_result as fix <EOL> from scipy . linalg import lstsq <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . measurements as cpmeas <EOL> import cellprofiler . objects as cpo <EOL> import cellprofiler . settings as cps <EOL> M_IMAGES = "<STR_LIT>" <EOL> M_OBJECTS = "<STR_LIT>" <EOL> M_IMAGES_AND_OBJECTS = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> F_CORRELATION_FORMAT = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> F_SLOPE_FORMAT = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> F_OVERLAP_FORMAT = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> F_K_FORMAT = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> F_KS_FORMAT = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> F_MANDERS_FORMAT = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> F_RWC_FORMAT = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> F_COSTES_FORMAT = "<STR_LIT>" <EOL> class MeasureCorrelation ( cpm . CPModule ) : <EOL> module_name = '<STR_LIT>' <EOL> category = '<STR_LIT>' <EOL> variable_revision_number = <NUM_LIT:3> <EOL> def create_settings ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . image_groups = [ ] <EOL> self . add_image ( can_delete = False ) <EOL> self . spacer_1 = cps . Divider ( ) <EOL> self . add_image ( can_delete = False ) <EOL> self . image_count = cps . HiddenCount ( self . image_groups ) <EOL> self . add_image_button = cps . DoSomething ( "<STR_LIT>" , '<STR_LIT>' , self . add_image ) <EOL> self . spacer_2 = cps . Divider ( ) <EOL> self . thr = cps . Float ( <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:15> , minval = <NUM_LIT:0> , maxval = <NUM_LIT> , doc = '''<STR_LIT>''' ) <EOL> self . images_or_objects = cps . Choice ( <EOL> '<STR_LIT>' , <EOL> [ M_IMAGES , M_OBJECTS , M_IMAGES_AND_OBJECTS ] , doc = '''<STR_LIT>''' % globals ( ) ) <EOL> self . object_groups = [ ] <EOL> self . add_object ( can_delete = False ) <EOL> self . object_count = cps . HiddenCount ( self . object_groups ) <EOL> self . spacer_2 = cps . Divider ( line = True ) <EOL> self . add_object_button = cps . DoSomething ( "<STR_LIT>" , '<STR_LIT>' , self . add_object ) <EOL> def add_image ( self , can_delete = True ) : <EOL> '''<STR_LIT>''' <EOL> group = cps . SettingsGroup ( ) <EOL> if can_delete : <EOL> group . append ( "<STR_LIT>" , cps . Divider ( line = False ) ) <EOL> group . append ( "<STR_LIT>" , cps . ImageNameSubscriber ( <EOL> '<STR_LIT>' , cps . NONE , doc = '''<STR_LIT>''' ) ) <EOL> if len ( self . image_groups ) == <NUM_LIT:0> : <EOL> group . append ( "<STR_LIT>" , cps . Divider ( line = False ) ) <EOL> if can_delete : <EOL> group . append ( "<STR_LIT>" , cps . RemoveSettingButton ( "<STR_LIT>" , "<STR_LIT>" , self . image_groups , group ) ) <EOL> self . image_groups . append ( group ) <EOL> def add_object ( self , can_delete = True ) : <EOL> '''<STR_LIT>''' <EOL> group = cps . SettingsGroup ( ) <EOL> if can_delete : <EOL> group . append ( "<STR_LIT>" , cps . Divider ( line = False ) ) <EOL> group . append ( "<STR_LIT:object_name>" , cps . ObjectNameSubscriber ( <EOL> '<STR_LIT>' , cps . NONE , doc = '''<STR_LIT>''' ) ) <EOL> if can_delete : <EOL> group . append ( "<STR_LIT>" , cps . RemoveSettingButton ( '<STR_LIT>' , '<STR_LIT>' , self . object_groups , group ) ) <EOL> self . object_groups . append ( group ) <EOL> def settings ( self ) : <EOL> '''<STR_LIT>''' <EOL> result = [ self . image_count , self . object_count ] <EOL> result += [ image_group . image_name for image_group in self . image_groups ] <EOL> result += [ self . thr ] <EOL> result += [ self . images_or_objects ] <EOL> result += [ object_group . object_name for object_group in self . object_groups ] <EOL> return result <EOL> def prepare_settings ( self , setting_values ) : <EOL> '''<STR_LIT>''' <EOL> image_count = int ( setting_values [ <NUM_LIT:0> ] ) <EOL> object_count = int ( setting_values [ <NUM_LIT:1> ] ) <EOL> if image_count < <NUM_LIT:2> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % image_count ) <EOL> del self . image_groups [ image_count : ] <EOL> while len ( self . image_groups ) < image_count : <EOL> self . add_image ( ) <EOL> del self . object_groups [ object_count : ] <EOL> while len ( self . object_groups ) < object_count : <EOL> self . add_object ( ) <EOL> def visible_settings ( self ) : <EOL> result = [ ] <EOL> for image_group in self . image_groups : <EOL> result += image_group . visible_settings ( ) <EOL> result += [ self . add_image_button , self . spacer_2 , self . thr , self . images_or_objects ] <EOL> if self . wants_objects ( ) : <EOL> for object_group in self . object_groups : <EOL> result += object_group . visible_settings ( ) <EOL> result += [ self . add_object_button ] <EOL> return result <EOL> def get_image_pairs ( self ) : <EOL> '''<STR_LIT>''' <EOL> for i in range ( self . image_count . value - <NUM_LIT:1> ) : <EOL> for j in range ( i + <NUM_LIT:1> , self . image_count . value ) : <EOL> yield ( self . image_groups [ i ] . image_name . value , <EOL> self . image_groups [ j ] . image_name . value ) <EOL> def wants_images ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . images_or_objects in ( M_IMAGES , M_IMAGES_AND_OBJECTS ) <EOL> def wants_objects ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . images_or_objects in ( M_OBJECTS , M_IMAGES_AND_OBJECTS ) <EOL> def run ( self , workspace ) : <EOL> '''<STR_LIT>''' <EOL> col_labels = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> statistics = [ ] <EOL> for first_image_name , second_image_name in self . get_image_pairs ( ) : <EOL> if self . wants_images ( ) : <EOL> statistics += self . run_image_pair_images ( workspace , <EOL> first_image_name , <EOL> second_image_name ) <EOL> if self . wants_objects ( ) : <EOL> for object_name in [ group . object_name . value for group in self . object_groups ] : <EOL> statistics += self . run_image_pair_objects ( workspace , <EOL> first_image_name , <EOL> second_image_name , <EOL> object_name ) <EOL> if self . show_window : <EOL> workspace . display_data . statistics = statistics <EOL> workspace . display_data . col_labels = col_labels <EOL> def display ( self , workspace , figure ) : <EOL> statistics = workspace . display_data . statistics <EOL> figure . set_subplots ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> figure . subplot_table ( <NUM_LIT:0> , <NUM_LIT:0> , statistics , workspace . display_data . col_labels ) <EOL> def run_image_pair_images ( self , workspace , first_image_name , <EOL> second_image_name ) : <EOL> '''<STR_LIT>''' <EOL> first_image = workspace . image_set . get_image ( first_image_name , <EOL> must_be_grayscale = True ) <EOL> second_image = workspace . image_set . get_image ( second_image_name , <EOL> must_be_grayscale = True ) <EOL> first_pixel_data = first_image . pixel_data <EOL> first_mask = first_image . mask <EOL> first_pixel_count = np . product ( first_pixel_data . shape ) <EOL> second_pixel_data = second_image . pixel_data <EOL> second_mask = second_image . mask <EOL> second_pixel_count = np . product ( second_pixel_data . shape ) <EOL> if first_pixel_count < second_pixel_count : <EOL> second_pixel_data = first_image . crop_image_similarly ( second_pixel_data ) <EOL> second_mask = first_image . crop_image_similarly ( second_mask ) <EOL> elif second_pixel_count < first_pixel_count : <EOL> first_pixel_data = second_image . crop_image_similarly ( first_pixel_data ) <EOL> first_mask = second_image . crop_image_similarly ( first_mask ) <EOL> mask = ( first_mask & second_mask & <EOL> ( ~ np . isnan ( first_pixel_data ) ) & <EOL> ( ~ np . isnan ( second_pixel_data ) ) ) <EOL> result = [ ] <EOL> if np . any ( mask ) : <EOL> fi = first_pixel_data [ mask ] <EOL> si = second_pixel_data [ mask ] <EOL> corr = np . corrcoef ( ( fi , si ) ) [ <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> coeffs = lstsq ( np . array ( ( fi , np . ones_like ( fi ) ) ) . transpose ( ) , si ) [ <NUM_LIT:0> ] <EOL> slope = coeffs [ <NUM_LIT:0> ] <EOL> result += [ [ first_image_name , second_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % corr ] , <EOL> [ first_image_name , second_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % slope ] ] <EOL> nonZero = ( fi > <NUM_LIT:0> ) | ( si > <NUM_LIT:0> ) <EOL> xvar = np . var ( fi [ nonZero ] , axis = <NUM_LIT:0> , ddof = <NUM_LIT:1> ) <EOL> yvar = np . var ( si [ nonZero ] , axis = <NUM_LIT:0> , ddof = <NUM_LIT:1> ) <EOL> xmean = np . mean ( fi [ nonZero ] , axis = <NUM_LIT:0> ) <EOL> ymean = np . mean ( si [ nonZero ] , axis = <NUM_LIT:0> ) <EOL> z = fi [ nonZero ] + si [ nonZero ] <EOL> zvar = np . var ( z , axis = <NUM_LIT:0> , ddof = <NUM_LIT:1> ) <EOL> covar = <NUM_LIT:0.5> * ( zvar - ( xvar + yvar ) ) <EOL> denom = <NUM_LIT:2> * covar <EOL> num = ( yvar - xvar ) + np . sqrt ( ( yvar - xvar ) * ( yvar - xvar ) + <NUM_LIT:4> * ( covar * covar ) ) <EOL> a = ( num / denom ) <EOL> b = ( ymean - a * xmean ) <EOL> i = <NUM_LIT:1> <EOL> while i > <NUM_LIT> : <EOL> Thr_fi_c = i <EOL> Thr_si_c = ( a * i ) + b <EOL> combt = ( fi < Thr_fi_c ) | ( si < Thr_si_c ) <EOL> costReg = scistat . pearsonr ( fi [ combt ] , si [ combt ] ) <EOL> if costReg [ <NUM_LIT:0> ] <= <NUM_LIT:0> : <EOL> break <EOL> i = i - <NUM_LIT> <EOL> combined_thresh_c = ( fi > Thr_fi_c ) & ( si > Thr_si_c ) <EOL> fi_thresh_c = fi [ combined_thresh_c ] <EOL> si_thresh_c = si [ combined_thresh_c ] <EOL> tot_fi_thr_c = fi [ ( fi > Thr_fi_c ) ] . sum ( ) <EOL> tot_si_thr_c = si [ ( si > Thr_si_c ) ] . sum ( ) <EOL> thr_fi = self . thr . value * np . max ( fi ) / <NUM_LIT:100> <EOL> thr_si = self . thr . value * np . max ( si ) / <NUM_LIT:100> <EOL> combined_thresh = ( fi > thr_fi ) & ( si > thr_si ) <EOL> fi_thresh = fi [ combined_thresh ] <EOL> si_thresh = si [ combined_thresh ] <EOL> tot_fi_thr = fi [ ( fi > thr_fi ) ] . sum ( ) <EOL> tot_si_thr = si [ ( si > thr_si ) ] . sum ( ) <EOL> M1 = <NUM_LIT:0> <EOL> M2 = <NUM_LIT:0> <EOL> M1 = fi_thresh . sum ( ) / tot_fi_thr <EOL> M2 = si_thresh . sum ( ) / tot_si_thr <EOL> result += [ [ first_image_name , second_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % M1 ] , <EOL> [ second_image_name , first_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % M2 ] ] <EOL> RWC1 = <NUM_LIT:0> <EOL> RWC2 = <NUM_LIT:0> <EOL> Rank1 = np . lexsort ( [ fi ] ) <EOL> Rank2 = np . lexsort ( [ si ] ) <EOL> Rank1_U = np . hstack ( [ [ False ] , fi [ Rank1 [ : - <NUM_LIT:1> ] ] != fi [ Rank1 [ <NUM_LIT:1> : ] ] ] ) <EOL> Rank2_U = np . hstack ( [ [ False ] , si [ Rank2 [ : - <NUM_LIT:1> ] ] != si [ Rank2 [ <NUM_LIT:1> : ] ] ] ) <EOL> Rank1_S = np . cumsum ( Rank1_U ) <EOL> Rank2_S = np . cumsum ( Rank2_U ) <EOL> Rank_im1 = np . zeros ( fi . shape , dtype = int ) <EOL> Rank_im2 = np . zeros ( si . shape , dtype = int ) <EOL> Rank_im1 [ Rank1 ] = Rank1_S <EOL> Rank_im2 [ Rank2 ] = Rank2_S <EOL> R = max ( Rank_im1 . max ( ) , Rank_im2 . max ( ) ) + <NUM_LIT:1> <EOL> Di = abs ( Rank_im1 - Rank_im2 ) <EOL> weight = ( ( R - Di ) * <NUM_LIT:1.0> ) / R <EOL> weight_thresh = weight [ combined_thresh ] <EOL> RWC1 = ( fi_thresh * weight_thresh ) . sum ( ) / tot_fi_thr <EOL> RWC2 = ( si_thresh * weight_thresh ) . sum ( ) / tot_si_thr <EOL> result += [ [ first_image_name , second_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % RWC1 ] , <EOL> [ second_image_name , first_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % RWC2 ] ] <EOL> C1 = <NUM_LIT:0> <EOL> C2 = <NUM_LIT:0> <EOL> C1 = fi_thresh_c . sum ( ) / tot_fi_thr_c <EOL> C2 = si_thresh_c . sum ( ) / tot_si_thr_c <EOL> result += [ [ first_image_name , second_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % C1 ] , <EOL> [ second_image_name , first_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % C2 ] ] <EOL> overlap = <NUM_LIT:0> <EOL> overlap = ( fi_thresh * si_thresh ) . sum ( ) / np . sqrt ( ( fi_thresh ** <NUM_LIT:2> ) . sum ( ) * ( si_thresh ** <NUM_LIT:2> ) . sum ( ) ) <EOL> K1 = ( fi_thresh * si_thresh ) . sum ( ) / ( fi_thresh ** <NUM_LIT:2> ) . sum ( ) <EOL> K2 = ( fi_thresh * si_thresh ) . sum ( ) / ( si_thresh ** <NUM_LIT:2> ) . sum ( ) <EOL> result += [ [ first_image_name , second_image_name , "<STR_LIT:->" , "<STR_LIT>" , "<STR_LIT>" % overlap ] ] <EOL> else : <EOL> corr = np . NaN <EOL> slope = np . NaN <EOL> C1 = np . NaN <EOL> C2 = np . NaN <EOL> M1 = np . NaN <EOL> M2 = np . NaN <EOL> RWC1 = np . NaN <EOL> RWC2 = np . NaN <EOL> overlap = np . NaN <EOL> K1 = np . NaN <EOL> K2 = np . NaN <EOL> corr_measurement = F_CORRELATION_FORMAT % ( first_image_name , <EOL> second_image_name ) <EOL> slope_measurement = F_SLOPE_FORMAT % ( first_image_name , <EOL> second_image_name ) <EOL> overlap_measurement = F_OVERLAP_FORMAT % ( first_image_name , <EOL> second_image_name ) <EOL> k_measurement_1 = F_K_FORMAT % ( first_image_name , <EOL> second_image_name ) <EOL> k_measurement_2 = F_K_FORMAT % ( second_image_name , <EOL> first_image_name ) <EOL> manders_measurement_1 = F_MANDERS_FORMAT % ( first_image_name , <EOL> second_image_name ) <EOL> manders_measurement_2 = F_MANDERS_FORMAT % ( second_image_name , <EOL> first_image_name ) <EOL> rwc_measurement_1 = F_RWC_FORMAT % ( first_image_name , <EOL> second_image_name ) <EOL> rwc_measurement_2 = F_RWC_FORMAT % ( second_image_name , <EOL> first_image_name ) <EOL> costes_measurement_1 = F_COSTES_FORMAT % ( first_image_name , <EOL> second_image_name ) <EOL> costes_measurement_2 = F_COSTES_FORMAT % ( second_image_name , <EOL> first_image_name ) <EOL> workspace . measurements . add_image_measurement ( corr_measurement , corr ) <EOL> workspace . measurements . add_image_measurement ( slope_measurement , slope ) <EOL> workspace . measurements . add_image_measurement ( overlap_measurement , overlap ) <EOL> workspace . measurements . add_image_measurement ( k_measurement_1 , K1 ) <EOL> workspace . measurements . add_image_measurement ( k_measurement_2 , K2 ) <EOL> workspace . measurements . add_image_measurement ( manders_measurement_1 , M1 ) <EOL> workspace . measurements . add_image_measurement ( manders_measurement_2 , M2 ) <EOL> workspace . measurements . add_image_measurement ( rwc_measurement_1 , RWC1 ) <EOL> workspace . measurements . add_image_measurement ( rwc_measurement_2 , RWC2 ) <EOL> workspace . measurements . add_image_measurement ( costes_measurement_1 , C1 ) <EOL> workspace . measurements . add_image_measurement ( costes_measurement_2 , C2 ) <EOL> return result <EOL> def run_image_pair_objects ( self , workspace , first_image_name , <EOL> second_image_name , object_name ) : <EOL> '''<STR_LIT>''' <EOL> first_image = workspace . image_set . get_image ( first_image_name , <EOL> must_be_grayscale = True ) <EOL> second_image = workspace . image_set . get_image ( second_image_name , <EOL> must_be_grayscale = True ) <EOL> objects = workspace . object_set . get_objects ( object_name ) <EOL> labels = objects . segmented <EOL> try : <EOL> first_pixels = objects . crop_image_similarly ( first_image . pixel_data ) <EOL> first_mask = objects . crop_image_similarly ( first_image . mask ) <EOL> except ValueError : <EOL> first_pixels , m1 = cpo . size_similarly ( labels , first_image . pixel_data ) <EOL> first_mask , m1 = cpo . size_similarly ( labels , first_image . mask ) <EOL> first_mask [ ~ m1 ] = False <EOL> try : <EOL> second_pixels = objects . crop_image_similarly ( second_image . pixel_data ) <EOL> second_mask = objects . crop_image_similarly ( second_image . mask ) <EOL> except ValueError : <EOL> second_pixels , m1 = cpo . size_similarly ( labels , second_image . pixel_data ) <EOL> second_mask , m1 = cpo . size_similarly ( labels , second_image . mask ) <EOL> second_mask [ ~ m1 ] = False <EOL> mask = ( ( labels > <NUM_LIT:0> ) & first_mask & second_mask ) <EOL> first_pixels = first_pixels [ mask ] <EOL> second_pixels = second_pixels [ mask ] <EOL> labels = labels [ mask ] <EOL> result = [ ] <EOL> first_pixel_data = first_image . pixel_data <EOL> first_mask = first_image . mask <EOL> first_pixel_count = np . product ( first_pixel_data . shape ) <EOL> second_pixel_data = second_image . pixel_data <EOL> second_mask = second_image . mask <EOL> second_pixel_count = np . product ( second_pixel_data . shape ) <EOL> if first_pixel_count < second_pixel_count : <EOL> second_pixel_data = first_image . crop_image_similarly ( second_pixel_data ) <EOL> second_mask = first_image . crop_image_similarly ( second_mask ) <EOL> elif second_pixel_count < first_pixel_count : <EOL> first_pixel_data = second_image . crop_image_similarly ( first_pixel_data ) <EOL> first_mask = second_image . crop_image_similarly ( first_mask ) <EOL> mask = ( first_mask & second_mask & <EOL> ( ~ np . isnan ( first_pixel_data ) ) & <EOL> ( ~ np . isnan ( second_pixel_data ) ) ) <EOL> if np . any ( mask ) : <EOL> fi = first_pixel_data [ mask ] <EOL> si = second_pixel_data [ mask ] <EOL> n_objects = objects . count <EOL> if n_objects == <NUM_LIT:0> : <EOL> corr = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> overlap = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> K1 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> K2 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> M1 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> M2 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> RWC1 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> RWC2 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> C1 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> C2 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> elif np . where ( mask ) [ <NUM_LIT:0> ] . __len__ ( ) == <NUM_LIT:0> : <EOL> corr = np . zeros ( ( n_objects , ) ) <EOL> corr [ : ] = np . NaN <EOL> overlap = K1 = K2 = M1 = M2 = RWC1 = RWC2 = C1 = C2 = corr <EOL> else : <EOL> lrange = np . arange ( n_objects , dtype = np . int32 ) + <NUM_LIT:1> <EOL> area = fix ( scind . sum ( np . ones_like ( labels ) , labels , lrange ) ) <EOL> mean1 = fix ( scind . mean ( first_pixels , labels , lrange ) ) <EOL> mean2 = fix ( scind . mean ( second_pixels , labels , lrange ) ) <EOL> std1 = np . sqrt ( fix ( scind . sum ( ( first_pixels - mean1 [ labels - <NUM_LIT:1> ] ) ** <NUM_LIT:2> , <EOL> labels , lrange ) ) ) <EOL> std2 = np . sqrt ( fix ( scind . sum ( ( second_pixels - mean2 [ labels - <NUM_LIT:1> ] ) ** <NUM_LIT:2> , <EOL> labels , lrange ) ) ) <EOL> x = first_pixels - mean1 [ labels - <NUM_LIT:1> ] <EOL> y = second_pixels - mean2 [ labels - <NUM_LIT:1> ] <EOL> corr = fix ( scind . sum ( x * y / ( std1 [ labels - <NUM_LIT:1> ] * std2 [ labels - <NUM_LIT:1> ] ) , <EOL> labels , lrange ) ) <EOL> corr [ scind . sum ( <NUM_LIT:1> , labels , lrange ) == <NUM_LIT:0> ] = np . NaN <EOL> result += [ <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . mean ( corr ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , <EOL> "<STR_LIT>" % np . median ( corr ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . min ( corr ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . max ( corr ) ] ] <EOL> tff = ( self . thr . value / <NUM_LIT:100> ) * fix ( scind . maximum ( first_pixels , labels , lrange ) ) <EOL> tss = ( self . thr . value / <NUM_LIT:100> ) * fix ( scind . maximum ( second_pixels , labels , lrange ) ) <EOL> combined_thresh = ( first_pixels > tff [ labels - <NUM_LIT:1> ] ) & ( second_pixels > tss [ labels - <NUM_LIT:1> ] ) <EOL> fi_thresh = first_pixels [ combined_thresh ] <EOL> si_thresh = second_pixels [ combined_thresh ] <EOL> tot_fi_thr = scind . sum ( first_pixels [ first_pixels > tff [ labels - <NUM_LIT:1> ] ] , labels [ first_pixels > tff [ labels - <NUM_LIT:1> ] ] , <EOL> lrange ) <EOL> tot_si_thr = scind . sum ( second_pixels [ second_pixels > tss [ labels - <NUM_LIT:1> ] ] , <EOL> labels [ second_pixels > tss [ labels - <NUM_LIT:1> ] ] , lrange ) <EOL> nonZero = ( fi > <NUM_LIT:0> ) | ( si > <NUM_LIT:0> ) <EOL> xvar = np . var ( fi [ nonZero ] , axis = <NUM_LIT:0> , ddof = <NUM_LIT:1> ) <EOL> yvar = np . var ( si [ nonZero ] , axis = <NUM_LIT:0> , ddof = <NUM_LIT:1> ) <EOL> xmean = np . mean ( fi [ nonZero ] , axis = <NUM_LIT:0> ) <EOL> ymean = np . mean ( si [ nonZero ] , axis = <NUM_LIT:0> ) <EOL> z = fi [ nonZero ] + si [ nonZero ] <EOL> zvar = np . var ( z , axis = <NUM_LIT:0> , ddof = <NUM_LIT:1> ) <EOL> covar = <NUM_LIT:0.5> * ( zvar - ( xvar + yvar ) ) <EOL> denom = <NUM_LIT:2> * covar <EOL> num = ( yvar - xvar ) + np . sqrt ( ( yvar - xvar ) * ( yvar - xvar ) + <NUM_LIT:4> * ( covar * covar ) ) <EOL> a = ( num / denom ) <EOL> b = ( ymean - a * xmean ) <EOL> i = <NUM_LIT:1> <EOL> while i > <NUM_LIT> : <EOL> thr_fi_c = i <EOL> thr_si_c = ( a * i ) + b <EOL> combt = ( fi < thr_fi_c ) | ( si < thr_si_c ) <EOL> costReg = scistat . pearsonr ( fi [ combt ] , si [ combt ] ) <EOL> if costReg [ <NUM_LIT:0> ] <= <NUM_LIT:0> : <EOL> break <EOL> i = i - <NUM_LIT> <EOL> fi_above_thr = first_pixels > thr_fi_c <EOL> si_above_thr = second_pixels > thr_si_c <EOL> combined_thresh_c = fi_above_thr & si_above_thr <EOL> fi_thresh_c = first_pixels [ combined_thresh_c ] <EOL> si_thresh_c = second_pixels [ combined_thresh_c ] <EOL> if np . any ( fi_above_thr ) : <EOL> tot_fi_thr_c = scind . sum ( first_pixels [ first_pixels > thr_fi_c ] , labels [ first_pixels > thr_fi_c ] , lrange ) <EOL> else : <EOL> tot_fi_thr_c = np . zeros ( len ( lrange ) ) <EOL> if np . any ( si_above_thr ) : <EOL> tot_si_thr_c = scind . sum ( second_pixels [ second_pixels > thr_si_c ] , labels [ second_pixels > thr_si_c ] , <EOL> lrange ) <EOL> else : <EOL> tot_si_thr_c = np . zeros ( len ( lrange ) ) <EOL> M1 = np . zeros ( len ( lrange ) ) <EOL> M2 = np . zeros ( len ( lrange ) ) <EOL> if np . any ( combined_thresh ) : <EOL> M1 = np . array ( scind . sum ( fi_thresh , labels [ combined_thresh ] , lrange ) ) / np . array ( tot_fi_thr ) <EOL> M2 = np . array ( scind . sum ( si_thresh , labels [ combined_thresh ] , lrange ) ) / np . array ( tot_si_thr ) <EOL> result += [ [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . mean ( M1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . median ( M1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . min ( M1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . max ( M1 ) ] ] <EOL> result += [ [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . mean ( M2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . median ( M2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . min ( M2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . max ( M2 ) ] ] <EOL> RWC1 = np . zeros ( len ( lrange ) ) <EOL> RWC2 = np . zeros ( len ( lrange ) ) <EOL> [ Rank1 ] = np . lexsort ( ( [ labels ] , [ first_pixels ] ) ) <EOL> [ Rank2 ] = np . lexsort ( ( [ labels ] , [ second_pixels ] ) ) <EOL> Rank1_U = np . hstack ( [ [ False ] , first_pixels [ Rank1 [ : - <NUM_LIT:1> ] ] != first_pixels [ Rank1 [ <NUM_LIT:1> : ] ] ] ) <EOL> Rank2_U = np . hstack ( [ [ False ] , second_pixels [ Rank2 [ : - <NUM_LIT:1> ] ] != second_pixels [ Rank2 [ <NUM_LIT:1> : ] ] ] ) <EOL> Rank1_S = np . cumsum ( Rank1_U ) <EOL> Rank2_S = np . cumsum ( Rank2_U ) <EOL> Rank_im1 = np . zeros ( first_pixels . shape , dtype = int ) <EOL> Rank_im2 = np . zeros ( second_pixels . shape , dtype = int ) <EOL> Rank_im1 [ Rank1 ] = Rank1_S <EOL> Rank_im2 [ Rank2 ] = Rank2_S <EOL> R = max ( Rank_im1 . max ( ) , Rank_im2 . max ( ) ) + <NUM_LIT:1> <EOL> Di = abs ( Rank_im1 - Rank_im2 ) <EOL> weight = ( R - Di ) * <NUM_LIT:1.0> / R <EOL> weight_thresh = weight [ combined_thresh ] <EOL> if np . any ( combined_thresh_c ) : <EOL> RWC1 = np . array ( scind . sum ( fi_thresh * weight_thresh , labels [ combined_thresh ] , lrange ) ) / np . array ( <EOL> tot_fi_thr ) <EOL> RWC2 = np . array ( scind . sum ( si_thresh * weight_thresh , labels [ combined_thresh ] , lrange ) ) / np . array ( <EOL> tot_si_thr ) <EOL> result += [ [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . mean ( RWC1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . median ( RWC1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . min ( RWC1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . max ( RWC1 ) ] ] <EOL> result += [ [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . mean ( RWC2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . median ( RWC2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . min ( RWC2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . max ( RWC2 ) ] ] <EOL> C1 = np . zeros ( len ( lrange ) ) <EOL> C2 = np . zeros ( len ( lrange ) ) <EOL> if np . any ( combined_thresh_c ) : <EOL> C1 = np . array ( scind . sum ( fi_thresh_c , labels [ combined_thresh_c ] , lrange ) ) / np . array ( tot_fi_thr_c ) <EOL> C2 = np . array ( scind . sum ( si_thresh_c , labels [ combined_thresh_c ] , lrange ) ) / np . array ( tot_si_thr_c ) <EOL> result += [ [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . mean ( C1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . median ( C1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . min ( C1 ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . max ( C1 ) ] <EOL> ] <EOL> result += [ [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . mean ( C2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . median ( C2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . min ( C2 ) ] , <EOL> [ second_image_name , first_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . max ( C2 ) ] <EOL> ] <EOL> fpsq = scind . sum ( first_pixels [ combined_thresh ] ** <NUM_LIT:2> , labels [ combined_thresh ] , lrange ) <EOL> spsq = scind . sum ( second_pixels [ combined_thresh ] ** <NUM_LIT:2> , labels [ combined_thresh ] , lrange ) <EOL> pdt = np . sqrt ( np . array ( fpsq ) * np . array ( spsq ) ) <EOL> if np . any ( combined_thresh ) : <EOL> overlap = fix ( <EOL> scind . sum ( first_pixels [ combined_thresh ] * second_pixels [ combined_thresh ] , <EOL> labels [ combined_thresh ] , <EOL> lrange ) / pdt ) <EOL> K1 = fix ( ( scind . sum ( first_pixels [ combined_thresh ] * second_pixels [ combined_thresh ] , <EOL> labels [ combined_thresh ] , lrange ) ) / ( np . array ( fpsq ) ) ) <EOL> K2 = fix ( <EOL> scind . sum ( first_pixels [ combined_thresh ] * second_pixels [ combined_thresh ] , <EOL> labels [ combined_thresh ] , <EOL> lrange ) / np . array ( spsq ) ) <EOL> else : <EOL> overlap = K1 = K2 = np . zeros ( len ( lrange ) ) <EOL> result += [ <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . mean ( overlap ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . median ( overlap ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . min ( overlap ) ] , <EOL> [ first_image_name , second_image_name , object_name , "<STR_LIT>" , "<STR_LIT>" % np . max ( overlap ) ] ] <EOL> measurement = ( "<STR_LIT>" % <EOL> ( first_image_name , second_image_name ) ) <EOL> overlap_measurement = ( F_OVERLAP_FORMAT % ( first_image_name , <EOL> second_image_name ) ) <EOL> k_measurement_1 = ( F_K_FORMAT % ( first_image_name , <EOL> second_image_name ) ) <EOL> k_measurement_2 = ( F_K_FORMAT % ( second_image_name , <EOL> first_image_name ) ) <EOL> manders_measurement_1 = ( F_MANDERS_FORMAT % ( first_image_name , <EOL> second_image_name ) ) <EOL> manders_measurement_2 = ( F_MANDERS_FORMAT % ( second_image_name , <EOL> first_image_name ) ) <EOL> rwc_measurement_1 = ( F_RWC_FORMAT % ( first_image_name , <EOL> second_image_name ) ) <EOL> rwc_measurement_2 = ( F_RWC_FORMAT % ( second_image_name , <EOL> first_image_name ) ) <EOL> costes_measurement_1 = ( F_COSTES_FORMAT % ( first_image_name , <EOL> second_image_name ) ) <EOL> costes_measurement_2 = ( F_COSTES_FORMAT % ( second_image_name , <EOL> first_image_name ) ) <EOL> workspace . measurements . add_measurement ( object_name , measurement , corr ) <EOL> workspace . measurements . add_measurement ( object_name , overlap_measurement , overlap ) <EOL> workspace . measurements . add_measurement ( object_name , k_measurement_1 , K1 ) <EOL> workspace . measurements . add_measurement ( object_name , k_measurement_2 , K2 ) <EOL> workspace . measurements . add_measurement ( object_name , manders_measurement_1 , M1 ) <EOL> workspace . measurements . add_measurement ( object_name , manders_measurement_2 , M2 ) <EOL> workspace . measurements . add_measurement ( object_name , rwc_measurement_1 , RWC1 ) <EOL> workspace . measurements . add_measurement ( object_name , rwc_measurement_2 , RWC2 ) <EOL> workspace . measurements . add_measurement ( object_name , costes_measurement_1 , C1 ) <EOL> workspace . measurements . add_measurement ( object_name , costes_measurement_2 , C2 ) <EOL> if n_objects == <NUM_LIT:0> : <EOL> return [ [ first_image_name , second_image_name , object_name , <EOL> "<STR_LIT>" , "<STR_LIT:->" ] , <EOL> [ first_image_name , second_image_name , object_name , <EOL> "<STR_LIT>" , "<STR_LIT:->" ] , <EOL> [ first_image_name , second_image_name , object_name , <EOL> "<STR_LIT>" , "<STR_LIT:->" ] , <EOL> [ first_image_name , second_image_name , object_name , <EOL> "<STR_LIT>" , "<STR_LIT:->" ] ] <EOL> else : <EOL> return result <EOL> def get_measurement_columns ( self , pipeline ) : <EOL> '''<STR_LIT>''' <EOL> columns = [ ] <EOL> for first_image , second_image in self . get_image_pairs ( ) : <EOL> if self . wants_images ( ) : <EOL> columns += [ ( cpmeas . IMAGE , <EOL> F_CORRELATION_FORMAT % ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_SLOPE_FORMAT % ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_OVERLAP_FORMAT % ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_K_FORMAT % ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_K_FORMAT % ( second_image , first_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_MANDERS_FORMAT % ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_MANDERS_FORMAT % ( second_image , first_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_RWC_FORMAT % ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_RWC_FORMAT % ( second_image , first_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_COSTES_FORMAT % ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( cpmeas . IMAGE , <EOL> F_COSTES_FORMAT % ( second_image , first_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) ] <EOL> if self . wants_objects ( ) : <EOL> for i in range ( self . object_count . value ) : <EOL> object_name = self . object_groups [ i ] . object_name . value <EOL> columns += [ ( object_name , <EOL> F_CORRELATION_FORMAT % <EOL> ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_OVERLAP_FORMAT % <EOL> ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_K_FORMAT % <EOL> ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_K_FORMAT % <EOL> ( second_image , first_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_MANDERS_FORMAT % <EOL> ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_MANDERS_FORMAT % <EOL> ( second_image , first_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_RWC_FORMAT % <EOL> ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_RWC_FORMAT % <EOL> ( second_image , first_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_COSTES_FORMAT % <EOL> ( first_image , second_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) , <EOL> ( object_name , <EOL> F_COSTES_FORMAT % <EOL> ( second_image , first_image ) , <EOL> cpmeas . COLTYPE_FLOAT ) ] <EOL> return columns <EOL> def get_categories ( self , pipeline , object_name ) : <EOL> '''<STR_LIT>''' <EOL> if ( ( object_name == cpmeas . IMAGE and self . wants_images ( ) ) or <EOL> ( ( object_name != cpmeas . IMAGE ) and self . wants_objects ( ) and <EOL> ( object_name in [ x . object_name . value for x in self . object_groups ] ) ) ) : <EOL> return [ "<STR_LIT>" ] <EOL> return [ ] <EOL> def get_measurements ( self , pipeline , object_name , category ) : <EOL> if self . get_categories ( pipeline , object_name ) == [ category ] : <EOL> if object_name == cpmeas . IMAGE : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> else : <EOL> return [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> return [ ] <EOL> def get_measurement_images ( self , pipeline , object_name , category , <EOL> measurement ) : <EOL> '''<STR_LIT>''' <EOL> result = [ ] <EOL> if measurement in self . get_measurements ( pipeline , object_name , category ) : <EOL> for i1 , i2 in self . get_image_pairs ( ) : <EOL> result . append ( "<STR_LIT>" % ( i1 , i2 ) ) <EOL> if measurement in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> result . append ( "<STR_LIT>" % ( i2 , i1 ) ) <EOL> return result <EOL> def upgrade_settings ( self , setting_values , variable_revision_number , <EOL> module_name , from_matlab ) : <EOL> '''<STR_LIT>''' <EOL> if from_matlab and variable_revision_number == <NUM_LIT:3> : <EOL> image_names = [ x for x in setting_values [ : <NUM_LIT:4> ] <EOL> if x . upper ( ) != cps . DO_NOT_USE . upper ( ) ] <EOL> wants_image_measured = np . any ( [ x == cpmeas . IMAGE <EOL> for x in setting_values [ <NUM_LIT:4> : ] ] ) <EOL> object_names = [ x for x in setting_values [ <NUM_LIT:4> : ] <EOL> if not x in ( cpmeas . IMAGE , cps . DO_NOT_USE ) ] <EOL> if wants_image_measured : <EOL> if len ( object_names ) : <EOL> m = M_IMAGES_AND_OBJECTS <EOL> else : <EOL> m = M_IMAGES <EOL> elif len ( object_names ) : <EOL> m = M_OBJECTS <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if len ( object_names ) == <NUM_LIT:0> : <EOL> object_names = [ cps . NONE ] <EOL> setting_values = ( [ str ( len ( image_names ) ) , str ( len ( object_names ) ) ] + <EOL> image_names + [ m ] + object_names ) <EOL> from_matlab = False <EOL> variable_revision_number = <NUM_LIT:1> <EOL> if variable_revision_number == <NUM_LIT:1> : <EOL> image_count , object_count = [ int ( x ) for x in setting_values [ : <NUM_LIT:2> ] ] <EOL> image_names = setting_values [ <NUM_LIT:2> : ( image_count + <NUM_LIT:2> ) ] <EOL> m = setting_values [ image_count + <NUM_LIT:2> ] <EOL> object_names = setting_values [ ( image_count + <NUM_LIT:3> ) : ] <EOL> if m == "<STR_LIT>" : <EOL> m = M_IMAGES <EOL> elif m == "<STR_LIT>" : <EOL> m = M_OBJECTS <EOL> elif m == "<STR_LIT>" : <EOL> m = M_IMAGES_AND_OBJECTS <EOL> setting_values = ( [ str ( image_count ) , str ( object_count ) ] + <EOL> image_names + [ m ] + object_names ) <EOL> variable_revision_number = <NUM_LIT:2> <EOL> if variable_revision_number == <NUM_LIT:2> : <EOL> image_count = int ( setting_values [ <NUM_LIT:0> ] ) <EOL> idx_thr = image_count + <NUM_LIT:2> <EOL> setting_values = setting_values [ : idx_thr ] + [ "<STR_LIT>" ] + setting_values [ idx_thr : ] <EOL> variable_revision_number = <NUM_LIT:3> <EOL> return setting_values , variable_revision_number , from_matlab </s>
