<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import scipy . ndimage as scind <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 . settings as cps <EOL> from centrosome . zernike import construct_zernike_polynomials <EOL> from centrosome . zernike import get_zernike_indexes <EOL> from centrosome . cpmorphology import minimum_enclosing_circle <EOL> from centrosome . cpmorphology import fixup_scipy_ndimage_result as fix <EOL> '''<STR_LIT>''' <EOL> C_MEASUREMENT_TEMPLATE = "<STR_LIT>" <EOL> class MeasurementTemplate ( cpm . CPModule ) : <EOL> module_name = "<STR_LIT>" <EOL> category = "<STR_LIT>" <EOL> variable_revision_number = <NUM_LIT:1> <EOL> def create_settings ( self ) : <EOL> self . input_image_name = cps . ImageNameSubscriber ( <EOL> "<STR_LIT>" , <EOL> doc = """<STR_LIT>""" ) <EOL> self . input_object_name = cps . ObjectNameSubscriber ( <EOL> "<STR_LIT>" , <EOL> doc = """<STR_LIT>""" ) <EOL> self . radial_degree = cps . Integer ( <EOL> "<STR_LIT>" , <NUM_LIT:10> , minval = <NUM_LIT:1> , maxval = <NUM_LIT:50> , <EOL> doc = """<STR_LIT>""" ) <EOL> def settings ( self ) : <EOL> return [ self . input_image_name , self . input_object_name , <EOL> self . radial_degree ] <EOL> def run ( self , workspace ) : <EOL> meas = workspace . measurements <EOL> assert isinstance ( meas , cpmeas . Measurements ) <EOL> statistics = [ [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ] <EOL> workspace . display_data . statistics = statistics <EOL> input_image_name = self . input_image_name . value <EOL> input_object_name = self . input_object_name . value <EOL> image_set = workspace . image_set <EOL> input_image = image_set . get_image ( input_image_name , <EOL> must_be_grayscale = True ) <EOL> pixels = input_image . pixel_data <EOL> object_set = workspace . object_set <EOL> assert isinstance ( object_set , cpo . ObjectSet ) <EOL> objects = object_set . get_objects ( input_object_name ) <EOL> labels = objects . segmented <EOL> indexes = objects . get_indices ( ) <EOL> centers , radius = minimum_enclosing_circle ( labels , indexes ) <EOL> for n , m in self . get_zernike_indexes ( ) : <EOL> zr , zi = self . measure_zernike ( <EOL> pixels , labels , indexes , centers , radius , n , m ) <EOL> feature = self . get_measurement_name ( n , m ) <EOL> if m != <NUM_LIT:0> : <EOL> meas . add_measurement ( input_object_name , feature , zr ) <EOL> feature = self . get_measurement_name ( n , - m ) <EOL> meas . add_measurement ( input_object_name , feature , zi ) <EOL> else : <EOL> meas . add_measurement ( input_object_name , feature , zr + zi ) <EOL> zmean = np . mean ( zr ) <EOL> zmedian = np . median ( zr ) <EOL> zsd = np . std ( zr ) <EOL> statistics . append ( [ feature , zmean , zmedian , zsd ] ) <EOL> def display ( self , workspace , figure = None ) : <EOL> statistics = workspace . display_data . statistics <EOL> if figure is None : <EOL> figure = workspace . create_or_find_figure ( subplots = ( <NUM_LIT:1> , <NUM_LIT:1> , ) ) <EOL> else : <EOL> figure . set_subplots ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> figure . subplot_table ( <NUM_LIT:0> , <NUM_LIT:0> , statistics ) <EOL> def get_zernike_indexes ( self , wants_negative = False ) : <EOL> '''<STR_LIT>''' <EOL> zi = get_zernike_indexes ( self . radial_degree . value + <NUM_LIT:1> ) <EOL> if wants_negative : <EOL> zi = np . vstack ( [ zi , zi [ zi [ : , <NUM_LIT:1> ] != <NUM_LIT:0> ] * np . array ( [ <NUM_LIT:1> , - <NUM_LIT:1> ] ) ] ) <EOL> order = np . lexsort ( ( zi [ : , <NUM_LIT:1> ] , zi [ : , <NUM_LIT:0> ] ) ) <EOL> zi = zi [ order , : ] <EOL> return zi <EOL> def measure_zernike ( self , pixels , labels , indexes , centers , radius , n , m ) : <EOL> '''<STR_LIT>''' <EOL> center_x = centers [ : , <NUM_LIT:1> ] <EOL> center_y = centers [ : , <NUM_LIT:0> ] <EOL> center_x = np . hstack ( [ [ <NUM_LIT:0> ] , center_x ] ) <EOL> center_y = np . hstack ( [ [ <NUM_LIT:0> ] , center_y ] ) <EOL> radius = np . hstack ( [ [ <NUM_LIT:1> ] , radius ] ) <EOL> y , x = np . mgrid [ <NUM_LIT:0> : labels . shape [ <NUM_LIT:0> ] , <NUM_LIT:0> : labels . shape [ <NUM_LIT:1> ] ] <EOL> y -= center_y [ labels ] <EOL> x -= center_x [ labels ] <EOL> y = y . astype ( float ) / radius [ labels ] <EOL> x = x . astype ( float ) / radius [ labels ] <EOL> zernike_polynomial = construct_zernike_polynomials ( <EOL> x , y , np . array ( [ [ n , m ] ] ) , labels > <NUM_LIT:0> ) <EOL> zernike_polynomial *= ( <NUM_LIT:2> * n + <NUM_LIT:2> ) / ( <NUM_LIT:2> if m == <NUM_LIT:0> else <NUM_LIT:1> ) / np . pi <EOL> output_pixels = pixels * zernike_polynomial [ : , : , <NUM_LIT:0> ] <EOL> zr = fix ( scind . sum ( output_pixels . real , labels , indexes ) ) <EOL> zi = fix ( scind . sum ( output_pixels . imag , labels , indexes ) ) <EOL> return zr , zi <EOL> def get_feature_name ( self , n , m ) : <EOL> '''<STR_LIT>''' <EOL> if m >= <NUM_LIT:0> : <EOL> return "<STR_LIT>" % ( self . input_image_name . value , n , m ) <EOL> else : <EOL> return "<STR_LIT>" % ( self . input_image_name . value , n , - m ) <EOL> def get_measurement_name ( self , n , m ) : <EOL> '''<STR_LIT>''' <EOL> input_image_name = self . input_image_name . value <EOL> return '<STR_LIT:_>' . join ( [ C_MEASUREMENT_TEMPLATE , <EOL> self . get_feature_name ( n , m ) ] ) <EOL> def get_measurement_columns ( self , pipeline ) : <EOL> input_object_name = self . input_object_name . value <EOL> return [ ( input_object_name , <EOL> self . get_measurement_name ( n , m ) , <EOL> cpmeas . COLTYPE_FLOAT ) <EOL> for n , m in self . get_zernike_indexes ( True ) ] <EOL> def get_categories ( self , pipeline , object_name ) : <EOL> if object_name == self . input_object_name : <EOL> return [ C_MEASUREMENT_TEMPLATE ] <EOL> else : <EOL> return [ ] <EOL> def get_measurements ( self , pipeline , object_name , category ) : <EOL> if ( object_name == self . input_object_name and <EOL> category == C_MEASUREMENT_TEMPLATE ) : <EOL> return [ "<STR_LIT>" ] <EOL> else : <EOL> return [ ] <EOL> def get_measurement_images ( self , pipeline , object_name , category , measurement ) : <EOL> if measurement in self . get_measurements ( <EOL> pipeline , object_name , category ) : <EOL> return [ self . input_image_name . value ] <EOL> else : <EOL> return [ ] <EOL> def get_measurement_scales ( self , pipeline , object_name , category , <EOL> measurement , image_name ) : <EOL> '''<STR_LIT>''' <EOL> if image_name in self . get_measurement_images ( <EOL> pipeline , object_name , category , measurement ) : <EOL> return [ ( "<STR_LIT>" % ( n , m ) ) if m >= <NUM_LIT:0> else <EOL> ( "<STR_LIT>" % ( n , - m ) ) for n , m in <EOL> self . get_zernike_indexes ( True ) ] <EOL> return [ ] <EOL> @ staticmethod <EOL> def get_image_from_features ( radius , feature_dictionary ) : <EOL> '''<STR_LIT>''' <EOL> i , j = np . mgrid [ - radius : ( radius + <NUM_LIT:1> ) , - radius : ( radius + <NUM_LIT:1> ) ] . astype ( float ) / radius <EOL> mask = ( i * i + j * j ) <= <NUM_LIT:1> <EOL> zernike_indexes = np . array ( feature_dictionary . keys ( ) ) <EOL> zernike_features = np . array ( feature_dictionary . values ( ) ) <EOL> z = construct_zernike_polynomials ( <EOL> j , i , np . abs ( zernike_indexes ) , mask = mask ) <EOL> zn = ( <NUM_LIT:2> * zernike_indexes [ : , <NUM_LIT:0> ] + <NUM_LIT:2> ) / ( ( zernike_indexes [ : , <NUM_LIT:1> ] == <NUM_LIT:0> ) + <NUM_LIT:1> ) / np . pi <EOL> z = z * zn [ np . newaxis , np . newaxis , : ] <EOL> z = z . real * ( zernike_indexes [ : , <NUM_LIT:1> ] >= <NUM_LIT:0> ) [ np . newaxis , np . newaxis , : ] + z . imag * ( zernike_indexes [ : , <NUM_LIT:1> ] <= <NUM_LIT:0> ) [ np . newaxis , np . newaxis , : ] <EOL> return np . sum ( z * zernike_features [ np . newaxis , np . newaxis , : ] , <NUM_LIT:2> ) </s>
