<s> '''<STR_LIT>''' <EOL> import itertools <EOL> import unittest <EOL> from StringIO import StringIO <EOL> import centrosome . cpmorphology as morph <EOL> import numpy as np <EOL> import cellprofiler . cpimage as cpi <EOL> import cellprofiler . measurements as cpmeas <EOL> import cellprofiler . modules . identify as I <EOL> import cellprofiler . modules . straightenworms as S <EOL> import cellprofiler . objects as cpo <EOL> import cellprofiler . pipeline as cpp <EOL> import cellprofiler . settings as cps <EOL> import cellprofiler . workspace as cpw <EOL> OBJECTS_NAME = "<STR_LIT>" <EOL> STRAIGHTENED_OBJECTS_NAME = "<STR_LIT>" <EOL> IMAGE_NAME = "<STR_LIT>" <EOL> STRAIGHTENED_IMAGE_NAME = "<STR_LIT>" <EOL> AUX_IMAGE_NAME = "<STR_LIT>" <EOL> AUX_STRAIGHTENED_IMAGE_NAME = "<STR_LIT>" <EOL> class TestStraightenWorms ( unittest . TestCase ) : <EOL> def test_01_01_load_v1 ( self ) : <EOL> data = r'''<STR_LIT>''' <EOL> pipeline = cpp . Pipeline ( ) <EOL> def callback ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . LoadExceptionEvent ) ) <EOL> pipeline . add_listener ( callback ) <EOL> pipeline . load ( StringIO ( data ) ) <EOL> self . assertEqual ( len ( pipeline . modules ( ) ) , <NUM_LIT:1> ) <EOL> module = pipeline . modules ( ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> self . assertEqual ( module . objects_name , "<STR_LIT>" ) <EOL> self . assertEqual ( module . straightened_objects_name , "<STR_LIT>" ) <EOL> self . assertEqual ( module . width , <NUM_LIT:20> ) <EOL> self . assertEqual ( module . training_set_directory . dir_choice , <EOL> cps . DEFAULT_OUTPUT_FOLDER_NAME ) <EOL> self . assertEqual ( module . training_set_file_name , "<STR_LIT>" ) <EOL> self . assertEqual ( module . image_count . value , <NUM_LIT:2> ) <EOL> for group , input_name , output_name in ( <EOL> ( module . images [ <NUM_LIT:0> ] , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( module . images [ <NUM_LIT:1> ] , "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> self . assertEqual ( group . image_name , input_name ) <EOL> self . assertEqual ( group . straightened_image_name , output_name ) <EOL> def test_01_02_load_v2 ( self ) : <EOL> data = r'''<STR_LIT>''' <EOL> pipeline = cpp . Pipeline ( ) <EOL> def callback ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . LoadExceptionEvent ) ) <EOL> pipeline . add_listener ( callback ) <EOL> pipeline . load ( StringIO ( data ) ) <EOL> self . assertEqual ( len ( pipeline . modules ( ) ) , <NUM_LIT:3> ) <EOL> for alignment , module in zip ( ( S . FLIP_TOP , S . FLIP_BOTTOM , S . FLIP_NONE ) , <EOL> pipeline . modules ( ) ) : <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> self . assertEqual ( module . objects_name , "<STR_LIT>" ) <EOL> self . assertEqual ( module . straightened_objects_name , "<STR_LIT>" ) <EOL> self . assertEqual ( module . width , <NUM_LIT:20> ) <EOL> self . assertEqual ( module . training_set_directory . dir_choice , <EOL> cps . DEFAULT_INPUT_FOLDER_NAME ) <EOL> self . assertEqual ( module . training_set_file_name , "<STR_LIT>" ) <EOL> self . assertEqual ( len ( module . images ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( module . wants_measurements ) <EOL> self . assertEqual ( module . number_of_segments , <NUM_LIT:4> ) <EOL> self . assertEqual ( module . number_of_stripes , <NUM_LIT:1> ) <EOL> self . assertEqual ( module . flip_worms , alignment ) <EOL> self . assertEqual ( module . flip_image , "<STR_LIT>" ) <EOL> self . assertEqual ( module . images [ <NUM_LIT:0> ] . image_name , "<STR_LIT>" ) <EOL> self . assertEqual ( module . images [ <NUM_LIT:0> ] . straightened_image_name , "<STR_LIT>" ) <EOL> def make_workspace ( self , control_points , lengths , radii , image , <EOL> mask = None , auximage = None ) : <EOL> '''<STR_LIT>''' <EOL> module = S . StraightenWorms ( ) <EOL> module . objects_name . value = OBJECTS_NAME <EOL> module . straightened_objects_name . value = STRAIGHTENED_OBJECTS_NAME <EOL> module . images [ <NUM_LIT:0> ] . image_name . value = IMAGE_NAME <EOL> module . images [ <NUM_LIT:0> ] . straightened_image_name . value = STRAIGHTENED_IMAGE_NAME <EOL> module . flip_image . value = IMAGE_NAME <EOL> module . module_num = <NUM_LIT:1> <EOL> class P : <EOL> def __init__ ( self ) : <EOL> self . radii_from_training = radii <EOL> module . training_set_directory . dir_choice = cps . URL_FOLDER_NAME <EOL> module . training_set_directory . custom_path = "<STR_LIT>" <EOL> module . training_set_file_name . value = "<STR_LIT>" <EOL> module . training_params = { "<STR_LIT>" : ( P ( ) , "<STR_LIT>" ) } <EOL> pipeline = cpp . Pipeline ( ) <EOL> pipeline . add_module ( module ) <EOL> def callback ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . RunExceptionEvent ) ) <EOL> pipeline . add_listener ( callback ) <EOL> m = cpmeas . Measurements ( ) <EOL> for i , ( y , x ) in enumerate ( control_points ) : <EOL> for v , f in ( ( x , S . F_CONTROL_POINT_X ) , <EOL> ( y , S . F_CONTROL_POINT_Y ) ) : <EOL> feature = "<STR_LIT:_>" . join ( ( S . C_WORM , f , str ( i + <NUM_LIT:1> ) ) ) <EOL> m . add_measurement ( OBJECTS_NAME , feature , v ) <EOL> feature = "<STR_LIT:_>" . join ( ( S . C_WORM , S . F_LENGTH ) ) <EOL> m . add_measurement ( OBJECTS_NAME , feature , lengths ) <EOL> image_set_list = cpi . ImageSetList ( ) <EOL> image_set = image_set_list . get_image_set ( <NUM_LIT:0> ) <EOL> image_set . add ( IMAGE_NAME , cpi . Image ( image , mask ) ) <EOL> if auximage is not None : <EOL> image_set . add ( AUX_IMAGE_NAME , cpi . Image ( auximage ) ) <EOL> module . add_image ( ) <EOL> module . images [ <NUM_LIT:1> ] . image_name . value = AUX_IMAGE_NAME <EOL> module . images [ <NUM_LIT:1> ] . straightened_image_name . value = AUX_STRAIGHTENED_IMAGE_NAME <EOL> object_set = cpo . ObjectSet ( ) <EOL> objects = cpo . Objects ( ) <EOL> labels = np . zeros ( image . shape , int ) <EOL> for i in range ( control_points . shape [ <NUM_LIT:2> ] ) : <EOL> if lengths [ i ] == <NUM_LIT:0> : <EOL> continue <EOL> self . rebuild_worm_from_control_points_approx ( <EOL> control_points [ : , : , i ] , radii , labels , i + <NUM_LIT:1> ) <EOL> objects . segmented = labels <EOL> object_set . add_objects ( objects , OBJECTS_NAME ) <EOL> workspace = cpw . Workspace ( pipeline , module , image_set , <EOL> object_set , m , image_set_list ) <EOL> return workspace , module <EOL> def rebuild_worm_from_control_points_approx ( self , control_coords , <EOL> worm_radii , labels , idx ) : <EOL> '''<STR_LIT>''' <EOL> index , count , i , j = morph . get_line_pts ( control_coords [ : - <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> control_coords [ : - <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> control_coords [ <NUM_LIT:1> : , <NUM_LIT:0> ] , <EOL> control_coords [ <NUM_LIT:1> : , <NUM_LIT:1> ] ) <EOL> i = np . delete ( i , index [ <NUM_LIT:1> : ] ) <EOL> j = np . delete ( j , index [ <NUM_LIT:1> : ] ) <EOL> index = index - np . arange ( len ( index ) ) <EOL> count -= <NUM_LIT:1> <EOL> label = np . zeros ( len ( i ) , int ) <EOL> label [ index [ <NUM_LIT:1> : ] ] = <NUM_LIT:1> <EOL> label = np . cumsum ( label ) <EOL> order = np . arange ( len ( i ) ) - index [ label ] <EOL> frac = order . astype ( float ) / count [ label ] . astype ( float ) <EOL> radius = ( worm_radii [ label ] * ( <NUM_LIT:1> - frac ) + <EOL> worm_radii [ label + <NUM_LIT:1> ] * frac ) <EOL> iworm_radius = int ( np . max ( np . ceil ( radius ) ) ) <EOL> ii , jj = np . mgrid [ - iworm_radius : iworm_radius + <NUM_LIT:1> , <EOL> - iworm_radius : iworm_radius + <NUM_LIT:1> ] <EOL> dd = np . sqrt ( ( ii * ii + jj * jj ) . astype ( float ) ) <EOL> mask = ii * ii + jj * jj <= iworm_radius * iworm_radius <EOL> ii = ii [ mask ] <EOL> jj = jj [ mask ] <EOL> dd = dd [ mask ] <EOL> i = ( i [ : , np . newaxis ] + ii [ np . newaxis , : ] ) . flatten ( ) <EOL> j = ( j [ : , np . newaxis ] + jj [ np . newaxis , : ] ) . flatten ( ) <EOL> m = ( radius [ : , np . newaxis ] >= dd [ np . newaxis , : ] ) . flatten ( ) <EOL> i = i [ m ] <EOL> j = j [ m ] <EOL> order = np . lexsort ( ( i , j ) ) <EOL> i = i [ order ] <EOL> j = j [ order ] <EOL> mask = np . hstack ( [ [ True ] , ( i [ : - <NUM_LIT:1> ] != i [ <NUM_LIT:1> : ] ) | ( j [ : - <NUM_LIT:1> ] != j [ <NUM_LIT:1> : ] ) ] ) <EOL> i = i [ mask ] <EOL> j = j [ mask ] <EOL> mask = ( i >= <NUM_LIT:0> ) & ( j >= <NUM_LIT:0> ) & ( i < labels . shape [ <NUM_LIT:0> ] ) & ( j < labels . shape [ <NUM_LIT:1> ] ) <EOL> labels [ i [ mask ] , j [ mask ] ] = idx <EOL> def test_02_01_straighten_nothing ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> np . zeros ( <NUM_LIT:0> ) , <EOL> np . zeros ( <NUM_LIT:5> ) , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) ) <EOL> module . run ( workspace ) <EOL> image = workspace . image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) <EOL> self . assertFalse ( np . any ( image . mask ) ) <EOL> objectset = workspace . object_set <EOL> self . assertTrue ( isinstance ( objectset , cpo . ObjectSet ) ) <EOL> labels = objectset . get_objects ( STRAIGHTENED_OBJECTS_NAME ) . segmented <EOL> self . assertTrue ( np . all ( labels == <NUM_LIT:0> ) ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> self . assertEqual ( m . get_current_image_measurement ( <EOL> "<STR_LIT:_>" . join ( ( I . C_COUNT , STRAIGHTENED_OBJECTS_NAME ) ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( m . get_current_measurement ( <EOL> STRAIGHTENED_OBJECTS_NAME , I . M_LOCATION_CENTER_X ) ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( m . get_current_measurement ( <EOL> STRAIGHTENED_OBJECTS_NAME , I . M_LOCATION_CENTER_Y ) ) , <NUM_LIT:0> ) <EOL> def test_02_02_straighten_straight_worm ( self ) : <EOL> '''<STR_LIT>''' <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT:0> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> control_points = np . array ( [ [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:8> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . wants_measurements . value = False <EOL> module . run ( workspace ) <EOL> image_set = workspace . image_set <EOL> self . assertTrue ( isinstance ( image_set , cpi . ImageSet ) ) <EOL> pixels = image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:1> ] , <NUM_LIT:11> ) <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> np . testing . assert_almost_equal ( pixels , image [ <NUM_LIT:16> : <NUM_LIT> , <NUM_LIT:10> : <NUM_LIT> ] ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> self . assertEqual ( m . get_current_image_measurement ( <EOL> "<STR_LIT:_>" . join ( ( I . C_COUNT , STRAIGHTENED_OBJECTS_NAME ) ) ) , <NUM_LIT:1> ) <EOL> v = m . get_current_measurement ( <EOL> STRAIGHTENED_OBJECTS_NAME , I . M_LOCATION_CENTER_X ) <EOL> self . assertEqual ( len ( v ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( v [ <NUM_LIT:0> ] , <NUM_LIT:5> ) <EOL> v = m . get_current_measurement ( <EOL> STRAIGHTENED_OBJECTS_NAME , I . M_LOCATION_CENTER_Y ) <EOL> self . assertEqual ( len ( v ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( v [ <NUM_LIT:0> ] , <NUM_LIT:9> ) <EOL> object_set = workspace . object_set <EOL> objects = object_set . get_objects ( STRAIGHTENED_OBJECTS_NAME ) <EOL> orig_objects = object_set . get_objects ( OBJECTS_NAME ) <EOL> self . assertTrue ( np . all ( objects . segmented == <EOL> orig_objects . segmented [ <NUM_LIT:16> : <NUM_LIT> , <NUM_LIT:10> : <NUM_LIT> ] ) ) <EOL> def test_02_03_straighten_diagonal_worm ( self ) : <EOL> '''<STR_LIT>''' <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> control_points = np . array ( [ [ [ <NUM_LIT:10> ] , [ <NUM_LIT:10> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT:16> ] , [ <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:20> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . run ( workspace ) <EOL> image_set = workspace . image_set <EOL> self . assertTrue ( isinstance ( image_set , cpi . ImageSet ) ) <EOL> pixels = image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:1> ] , <NUM_LIT:11> ) <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> expected = image [ control_points [ : , <NUM_LIT:0> , <NUM_LIT:0> ] , control_points [ : , <NUM_LIT:1> , <NUM_LIT:0> ] ] <EOL> samples = pixels [ <NUM_LIT:5> : <NUM_LIT> : <NUM_LIT:5> , <NUM_LIT:5> ] <EOL> np . testing . assert_almost_equal ( expected , samples ) <EOL> def test_02_04_straighten_two_worms ( self ) : <EOL> '''<STR_LIT>''' <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT:0> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> control_points = np . array ( [ [ [ <NUM_LIT> , <NUM_LIT:10> ] , [ <NUM_LIT:15> , <NUM_LIT:10> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:15> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT:16> ] , [ <NUM_LIT:15> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:15> , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT:15> , <NUM_LIT> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:8> , <NUM_LIT:20> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . run ( workspace ) <EOL> image_set = workspace . image_set <EOL> self . assertTrue ( isinstance ( image_set , cpi . ImageSet ) ) <EOL> pixels = image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> np . testing . assert_almost_equal ( pixels [ <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:0> : <NUM_LIT:11> ] , image [ <NUM_LIT:16> : <NUM_LIT> , <NUM_LIT:10> : <NUM_LIT> ] ) <EOL> expected = image [ control_points [ : , <NUM_LIT:0> , <NUM_LIT:1> ] , control_points [ : , <NUM_LIT:1> , <NUM_LIT:1> ] ] <EOL> samples = pixels [ <NUM_LIT:5> : <NUM_LIT> : <NUM_LIT:5> , <NUM_LIT:16> ] <EOL> np . testing . assert_almost_equal ( expected , samples ) <EOL> def test_02_05_straighten_missing_worm ( self ) : <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT:0> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> control_points = np . array ( [ [ [ <NUM_LIT> , np . nan , <NUM_LIT:10> ] , [ <NUM_LIT:15> , np . nan , <NUM_LIT:10> ] ] , <EOL> [ [ <NUM_LIT> , np . nan , <NUM_LIT> ] , [ <NUM_LIT:15> , np . nan , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , np . nan , <NUM_LIT:16> ] , [ <NUM_LIT:15> , np . nan , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , np . nan , <NUM_LIT> ] , [ <NUM_LIT:15> , np . nan , <NUM_LIT> ] ] , <EOL> [ [ <NUM_LIT> , np . nan , <NUM_LIT> ] , [ <NUM_LIT:15> , np . nan , <NUM_LIT> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:8> , <NUM_LIT:0> , <NUM_LIT:20> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . run ( workspace ) <EOL> image_set = workspace . image_set <EOL> self . assertTrue ( isinstance ( image_set , cpi . ImageSet ) ) <EOL> pixels = image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> np . testing . assert_almost_equal ( pixels [ <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:0> : <NUM_LIT:11> ] , image [ <NUM_LIT:16> : <NUM_LIT> , <NUM_LIT:10> : <NUM_LIT> ] ) <EOL> expected = image [ control_points [ : , <NUM_LIT:0> , <NUM_LIT:2> ] . astype ( int ) , <EOL> control_points [ : , <NUM_LIT:1> , <NUM_LIT:2> ] . astype ( int ) ] <EOL> samples = pixels [ <NUM_LIT:5> : <NUM_LIT> : <NUM_LIT:5> , <NUM_LIT> ] <EOL> np . testing . assert_almost_equal ( expected , samples ) <EOL> def test_03_01_get_measurement_columns ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> np . zeros ( <NUM_LIT:0> ) , <EOL> np . zeros ( <NUM_LIT:5> ) , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . wants_measurements . value = False <EOL> columns = module . get_measurement_columns ( workspace . pipeline ) <EOL> for object_name , feature_name in ( <EOL> ( cpmeas . IMAGE , "<STR_LIT:_>" . join ( ( I . C_COUNT , STRAIGHTENED_OBJECTS_NAME ) ) ) , <EOL> ( STRAIGHTENED_OBJECTS_NAME , I . M_LOCATION_CENTER_X ) , <EOL> ( STRAIGHTENED_OBJECTS_NAME , I . M_LOCATION_CENTER_Y ) , <EOL> ( STRAIGHTENED_OBJECTS_NAME , I . M_NUMBER_OBJECT_NUMBER ) ) : <EOL> self . assertTrue ( any ( [ ( o == object_name ) and ( f == feature_name ) <EOL> for o , f , t in columns ] ) ) <EOL> categories = module . get_categories ( workspace . pipeline , cpmeas . IMAGE ) <EOL> self . assertEqual ( len ( categories ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( categories [ <NUM_LIT:0> ] , I . C_COUNT ) <EOL> categories = module . get_categories ( workspace . pipeline , STRAIGHTENED_OBJECTS_NAME ) <EOL> self . assertEqual ( len ( categories ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( I . C_LOCATION in categories ) <EOL> self . assertTrue ( I . C_NUMBER in categories ) <EOL> f = module . get_measurements ( workspace . pipeline , cpmeas . IMAGE , I . C_COUNT ) <EOL> self . assertEqual ( len ( f ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( f [ <NUM_LIT:0> ] , STRAIGHTENED_OBJECTS_NAME ) <EOL> f = module . get_measurements ( workspace . pipeline , STRAIGHTENED_OBJECTS_NAME , I . C_NUMBER ) <EOL> self . assertEqual ( len ( f ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( f [ <NUM_LIT:0> ] , I . FTR_OBJECT_NUMBER ) <EOL> f = module . get_measurements ( workspace . pipeline , STRAIGHTENED_OBJECTS_NAME , I . C_LOCATION ) <EOL> self . assertEqual ( len ( f ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( I . FTR_CENTER_X in f ) <EOL> self . assertTrue ( I . FTR_CENTER_Y in f ) <EOL> def test_03_02_get_measurement_columns_wants_images_vertical ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> np . zeros ( <NUM_LIT:0> ) , <EOL> np . zeros ( <NUM_LIT:5> ) , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) , <EOL> auximage = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:5> <EOL> module . number_of_stripes . value = <NUM_LIT:1> <EOL> expected_columns = ( <EOL> [ ( OBJECTS_NAME , <EOL> "<STR_LIT:_>" . join ( ( S . C_WORM , ftr , image , module . get_scale_name ( None , segno ) ) ) , <EOL> cpmeas . COLTYPE_FLOAT ) <EOL> for ftr , image , segno in zip ( <EOL> [ S . FTR_MEAN_INTENSITY ] * <NUM_LIT:10> + [ S . FTR_STD_INTENSITY ] * <NUM_LIT:10> , <EOL> ( [ STRAIGHTENED_IMAGE_NAME ] * <NUM_LIT:5> + [ AUX_STRAIGHTENED_IMAGE_NAME ] * <NUM_LIT:5> ) * <NUM_LIT:2> , <EOL> list ( range ( <NUM_LIT:5> ) ) * <NUM_LIT:4> ) ] ) <EOL> columns = module . get_measurement_columns ( workspace . pipeline ) <EOL> columns = [ column for column in columns if column [ <NUM_LIT:0> ] == OBJECTS_NAME ] <EOL> for expected_column in expected_columns : <EOL> self . assertTrue ( any ( [ all ( [ x == y <EOL> for x , y in zip ( column , expected_column ) ] ) <EOL> for column in columns ] ) ) <EOL> for column in columns : <EOL> self . assertTrue ( any ( [ all ( [ x == y <EOL> for x , y in zip ( column , expected_column ) ] ) <EOL> for expected_column in expected_columns ] ) ) <EOL> categories = module . get_categories ( workspace . pipeline , OBJECTS_NAME ) <EOL> self . assertTrue ( S . C_WORM in categories ) <EOL> features = module . get_measurements ( workspace . pipeline , <EOL> OBJECTS_NAME , S . C_WORM ) <EOL> self . assertEqual ( len ( features ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( S . FTR_MEAN_INTENSITY in features ) <EOL> self . assertTrue ( S . FTR_STD_INTENSITY in features ) <EOL> for ftr in ( S . FTR_MEAN_INTENSITY , S . FTR_STD_INTENSITY ) : <EOL> images = module . get_measurement_images ( workspace . pipeline , <EOL> OBJECTS_NAME , <EOL> S . C_WORM , ftr ) <EOL> self . assertEqual ( len ( images ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( STRAIGHTENED_IMAGE_NAME in images ) <EOL> self . assertTrue ( AUX_STRAIGHTENED_IMAGE_NAME in images ) <EOL> for ftr , image in zip ( [ S . FTR_MEAN_INTENSITY , S . FTR_STD_INTENSITY ] * <NUM_LIT:2> , <EOL> [ STRAIGHTENED_IMAGE_NAME ] * <NUM_LIT:2> + <EOL> [ AUX_STRAIGHTENED_IMAGE_NAME ] * <NUM_LIT:2> ) : <EOL> scales = module . get_measurement_scales ( workspace . pipeline , <EOL> OBJECTS_NAME , <EOL> S . C_WORM , <EOL> ftr , image ) <EOL> self . assertEqual ( len ( scales ) , <NUM_LIT:5> ) <EOL> for expected_scale in range ( <NUM_LIT:5> ) : <EOL> self . assertTrue ( module . get_scale_name ( None , expected_scale ) in scales ) <EOL> def test_03_03_get_measurement_columns_horizontal ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> np . zeros ( <NUM_LIT:0> ) , <EOL> np . zeros ( <NUM_LIT:5> ) , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) , <EOL> auximage = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:1> <EOL> module . number_of_stripes . value = <NUM_LIT:5> <EOL> expected_columns = ( <EOL> [ ( OBJECTS_NAME , <EOL> "<STR_LIT:_>" . join ( ( S . C_WORM , ftr , image , module . get_scale_name ( segno , None ) ) ) , <EOL> cpmeas . COLTYPE_FLOAT ) <EOL> for ftr , image , segno in zip ( <EOL> [ S . FTR_MEAN_INTENSITY ] * <NUM_LIT:10> + [ S . FTR_STD_INTENSITY ] * <NUM_LIT:10> , <EOL> ( [ STRAIGHTENED_IMAGE_NAME ] * <NUM_LIT:5> + [ AUX_STRAIGHTENED_IMAGE_NAME ] * <NUM_LIT:5> ) * <NUM_LIT:2> , <EOL> list ( range ( <NUM_LIT:5> ) ) * <NUM_LIT:4> ) ] ) <EOL> columns = module . get_measurement_columns ( workspace . pipeline ) <EOL> columns = [ column for column in columns if column [ <NUM_LIT:0> ] == OBJECTS_NAME ] <EOL> for expected_column in expected_columns : <EOL> self . assertTrue ( any ( [ all ( [ x == y <EOL> for x , y in zip ( column , expected_column ) ] ) <EOL> for column in columns ] ) ) <EOL> for column in columns : <EOL> self . assertTrue ( any ( [ all ( [ x == y <EOL> for x , y in zip ( column , expected_column ) ] ) <EOL> for expected_column in expected_columns ] ) ) <EOL> categories = module . get_categories ( workspace . pipeline , <EOL> OBJECTS_NAME ) <EOL> self . assertTrue ( S . C_WORM in categories ) <EOL> features = module . get_measurements ( workspace . pipeline , <EOL> OBJECTS_NAME , S . C_WORM ) <EOL> self . assertEqual ( len ( features ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( S . FTR_MEAN_INTENSITY in features ) <EOL> self . assertTrue ( S . FTR_STD_INTENSITY in features ) <EOL> for ftr in ( S . FTR_MEAN_INTENSITY , S . FTR_STD_INTENSITY ) : <EOL> images = module . get_measurement_images ( workspace . pipeline , <EOL> OBJECTS_NAME , <EOL> S . C_WORM , ftr ) <EOL> self . assertEqual ( len ( images ) , <NUM_LIT:2> ) <EOL> self . assertTrue ( STRAIGHTENED_IMAGE_NAME in images ) <EOL> self . assertTrue ( AUX_STRAIGHTENED_IMAGE_NAME in images ) <EOL> for ftr , image in zip ( [ S . FTR_MEAN_INTENSITY , S . FTR_STD_INTENSITY ] * <NUM_LIT:2> , <EOL> [ STRAIGHTENED_IMAGE_NAME ] * <NUM_LIT:2> + <EOL> [ AUX_STRAIGHTENED_IMAGE_NAME ] * <NUM_LIT:2> ) : <EOL> scales = module . get_measurement_scales ( workspace . pipeline , <EOL> OBJECTS_NAME , <EOL> S . C_WORM , <EOL> ftr , image ) <EOL> self . assertEqual ( len ( scales ) , <NUM_LIT:5> ) <EOL> for expected_scale in range ( <NUM_LIT:5> ) : <EOL> self . assertTrue ( module . get_scale_name ( expected_scale , None ) in scales ) <EOL> def test_03_04_get_measurement_columns_both ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> np . zeros ( <NUM_LIT:0> ) , <EOL> np . zeros ( <NUM_LIT:5> ) , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) , <EOL> auximage = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:2> <EOL> module . number_of_stripes . value = <NUM_LIT:3> <EOL> expected_columns = [ ] <EOL> vscales = ( None , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> hscales = ( None , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> for image in ( STRAIGHTENED_IMAGE_NAME , AUX_STRAIGHTENED_IMAGE_NAME ) : <EOL> for ftr in ( S . FTR_MEAN_INTENSITY , S . FTR_STD_INTENSITY ) : <EOL> for vscale in vscales : <EOL> for hscale in hscales : <EOL> if vscale is None and hscale is None : <EOL> continue <EOL> meas = "<STR_LIT:_>" . join ( ( S . C_WORM , ftr , image , <EOL> module . get_scale_name ( hscale , vscale ) ) ) <EOL> expected_columns . append ( <EOL> ( OBJECTS_NAME , meas , cpmeas . COLTYPE_FLOAT ) ) <EOL> columns = module . get_measurement_columns ( workspace . pipeline ) <EOL> columns = [ column for column in columns if column [ <NUM_LIT:0> ] == OBJECTS_NAME ] <EOL> for expected_column in expected_columns : <EOL> self . assertTrue ( any ( [ all ( [ x == y <EOL> for x , y in zip ( column , expected_column ) ] ) <EOL> for column in columns ] ) ) <EOL> for column in columns : <EOL> self . assertTrue ( any ( [ all ( [ x == y <EOL> for x , y in zip ( column , expected_column ) ] ) <EOL> for expected_column in expected_columns ] ) ) <EOL> for ftr in ( S . FTR_MEAN_INTENSITY , S . FTR_STD_INTENSITY ) : <EOL> for image in ( STRAIGHTENED_IMAGE_NAME , AUX_STRAIGHTENED_IMAGE_NAME ) : <EOL> scales = module . get_measurement_scales ( <EOL> workspace . pipeline , OBJECTS_NAME , S . C_WORM , ftr , image ) <EOL> self . assertEqual ( len ( scales ) , <NUM_LIT:11> ) <EOL> for vscale in vscales : <EOL> for hscale in hscales : <EOL> if vscale is None and hscale is None : <EOL> continue <EOL> self . assertTrue ( module . get_scale_name ( hscale , vscale ) in scales ) <EOL> def test_04_00_measure_no_worms ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> np . zeros ( <NUM_LIT:0> ) , <EOL> np . zeros ( <NUM_LIT:5> ) , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) ) <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:5> <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> for ftr , function in ( <EOL> ( S . FTR_MEAN_INTENSITY , np . mean ) , <EOL> ( S . FTR_STD_INTENSITY , np . std ) ) : <EOL> mname = "<STR_LIT:_>" . join ( ( S . C_WORM , ftr , <EOL> STRAIGHTENED_IMAGE_NAME , <EOL> module . get_scale_name ( None , i ) ) ) <EOL> v = m . get_current_measurement ( OBJECTS_NAME , mname ) <EOL> self . assertEqual ( len ( v ) , <NUM_LIT:0> ) <EOL> def test_04_01_measure_one_worm ( self ) : <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT:0> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> control_points = np . array ( [ [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT:30> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:12> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:4> <EOL> module . number_of_stripes . value = <NUM_LIT:3> <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> oo = workspace . object_set . get_objects ( OBJECTS_NAME ) <EOL> segments = [ <EOL> [ ( <NUM_LIT:20> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT:30> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT:32> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) ] ] <EOL> def weighted_mean ( img , segments , mask ) : <EOL> accumulator = <NUM_LIT:0.0> <EOL> weight_accumulator = <NUM_LIT:0.0> <EOL> for i , w in segments : <EOL> piece = img [ i , mask [ i , : ] ] <EOL> accumulator += np . sum ( piece ) * w <EOL> weight_accumulator += w * np . sum ( mask [ i , : ] ) <EOL> return accumulator / weight_accumulator <EOL> def weighted_std ( img , segments , mask ) : <EOL> mean = weighted_mean ( img , segments , mask ) <EOL> accumulator = <NUM_LIT:0.0> <EOL> weight_accumulator = <NUM_LIT:0.0> <EOL> pixel_count = <NUM_LIT:0.0> <EOL> for i , w in segments : <EOL> piece = img [ i , mask [ i , : ] ] <EOL> accumulator += np . sum ( ( piece - mean ) ** <NUM_LIT:2> ) * w <EOL> weight_accumulator += w * np . sum ( mask [ i , : ] ) <EOL> pixel_count += np . sum ( mask [ i , : ] ) <EOL> return np . sqrt ( accumulator / weight_accumulator / <EOL> ( pixel_count - <NUM_LIT:1> ) * pixel_count ) <EOL> for i , segment in enumerate ( segments ) : <EOL> for ftr , function in ( <EOL> ( S . FTR_MEAN_INTENSITY , weighted_mean ) , <EOL> ( S . FTR_STD_INTENSITY , weighted_std ) ) : <EOL> mname = "<STR_LIT:_>" . join ( ( S . C_WORM , ftr , <EOL> STRAIGHTENED_IMAGE_NAME , <EOL> module . get_scale_name ( None , i ) ) ) <EOL> v = m . get_current_measurement ( OBJECTS_NAME , mname ) <EOL> expected = function ( image , segment , oo . segmented == <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( v ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( v [ <NUM_LIT:0> ] , expected ) <EOL> def test_04_02_measure_checkerboarded_worm ( self ) : <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> control_points = np . array ( [ [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT:30> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:12> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:4> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT> <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:4> <EOL> module . number_of_stripes . value = <NUM_LIT:3> <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> oo = workspace . object_set . get_objects ( OBJECTS_NAME ) <EOL> image = workspace . image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> f1 = <NUM_LIT:1.0> / <NUM_LIT> <EOL> f2 = <NUM_LIT> / <NUM_LIT> <EOL> stripes = [ <EOL> ( <NUM_LIT:9> , ( ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:2> , <NUM_LIT:1> ) , ) ) , <EOL> ( <NUM_LIT:10> , ( ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:1> , f2 ) , ( <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:2> , f1 ) , ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:2> , f1 ) , ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:3> , f2 ) , <EOL> ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT:11> , ( ( <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:1> , f1 ) , ( <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:2> , f2 ) , ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:2> , f2 ) , ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:3> , f1 ) , <EOL> ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT:12> , ( ( <NUM_LIT:6> , <NUM_LIT:9> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:9> , <NUM_LIT:12> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:12> , <NUM_LIT:15> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT> , ( ( <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:1> , f2 ) , ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:2> , f1 ) , ( <NUM_LIT:9> , <NUM_LIT:12> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:2> , f1 ) , ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:3> , f2 ) , <EOL> ( <NUM_LIT> , <NUM_LIT:16> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT> , ( ( <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:1> , f1 ) , ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:2> , f2 ) , ( <NUM_LIT:9> , <NUM_LIT:12> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:2> , f2 ) , ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:3> , f1 ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT:15> , ( ( <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT:16> , ( ( <NUM_LIT:3> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT> , ( ( <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT> , ( ( <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:1> , f1 ) , ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:2> , f2 ) , ( <NUM_LIT:9> , <NUM_LIT:12> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:2> , f2 ) , ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:3> , f1 ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT> , ( ( <NUM_LIT:5> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:1> , f2 ) , ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:2> , f1 ) , ( <NUM_LIT:9> , <NUM_LIT:12> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:2> , f1 ) , ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:3> , f2 ) , <EOL> ( <NUM_LIT> , <NUM_LIT:16> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT:20> , ( ( <NUM_LIT:6> , <NUM_LIT:9> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:9> , <NUM_LIT:12> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:12> , <NUM_LIT:15> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT> , ( ( <NUM_LIT:7> , <NUM_LIT:9> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:1> , f1 ) , ( <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:2> , f2 ) , ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:2> , f2 ) , ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:3> , f1 ) , <EOL> ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT> , ( ( <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:1> , f2 ) , ( <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:2> , f1 ) , ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:2> , f1 ) , ( <NUM_LIT:11> , <NUM_LIT:12> , <NUM_LIT:3> , f2 ) , <EOL> ( <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:1> ) ) ) , <EOL> ( <NUM_LIT> , ( ( <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:2> , <NUM_LIT:1> ) , ) ) ] <EOL> segments = [ <EOL> [ ( <NUM_LIT:9> , <NUM_LIT:1.0> ) , ( <NUM_LIT:10> , <NUM_LIT:1.0> ) , ( <NUM_LIT:11> , <NUM_LIT:1.0> ) , ( <NUM_LIT:12> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT:12> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT:15> , <NUM_LIT:1.0> ) , ( <NUM_LIT:16> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT:16> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT:20> , <NUM_LIT> ) ] , <EOL> [ ( <NUM_LIT:20> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) , ( <NUM_LIT> , <NUM_LIT:1.0> ) ] ] <EOL> i_w = np . zeros ( ( image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] , <NUM_LIT:4> ) ) <EOL> j_w = np . zeros ( ( image . shape [ <NUM_LIT:0> ] , image . shape [ <NUM_LIT:1> ] , <NUM_LIT:3> ) ) <EOL> mask = np . zeros ( image . shape , bool ) <EOL> for i , sstripes in stripes : <EOL> for jstart , jend , idx , w in sstripes : <EOL> for j in range ( jstart , jend ) : <EOL> j_w [ i , j , idx - <NUM_LIT:1> ] = w <EOL> mask [ i , j ] = True <EOL> for idx , segment in enumerate ( segments ) : <EOL> for i , w in segment : <EOL> i_w [ i , mask [ i , : ] , idx ] = w <EOL> s2 = lambda x : np . sum ( np . sum ( x , <NUM_LIT:0> ) , <NUM_LIT:0> ) <EOL> weights = s2 ( i_w ) <EOL> expected_means = s2 ( image [ : , : , np . newaxis ] * i_w ) / weights <EOL> counts = s2 ( i_w > <NUM_LIT:0> ) <EOL> expected_sds = np . sqrt ( <EOL> s2 ( i_w * ( image [ : , : , np . newaxis ] - expected_means [ np . newaxis , np . newaxis , : ] ) ** <NUM_LIT:2> ) / <EOL> weights * counts / ( counts - <NUM_LIT:1> ) ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> for ftr , expected in ( ( S . FTR_MEAN_INTENSITY , expected_means ) , <EOL> ( S . FTR_STD_INTENSITY , expected_sds ) ) : <EOL> value = m . get_current_measurement ( <EOL> OBJECTS_NAME , <EOL> "<STR_LIT:_>" . join ( ( S . C_WORM , ftr , STRAIGHTENED_IMAGE_NAME , <EOL> module . get_scale_name ( None , i ) ) ) ) <EOL> self . assertEqual ( len ( value ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( value [ <NUM_LIT:0> ] , expected [ i ] ) <EOL> weights = s2 ( j_w ) <EOL> expected_means = s2 ( image [ : , : , np . newaxis ] * j_w ) / weights <EOL> counts = s2 ( j_w > <NUM_LIT:0> ) <EOL> expected_sds = np . sqrt ( <EOL> s2 ( j_w * ( image [ : , : , np . newaxis ] - <EOL> expected_means [ np . newaxis , np . newaxis , : ] ) ** <NUM_LIT:2> ) / <EOL> weights * counts / ( counts - <NUM_LIT:1> ) ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> for ftr , expected in ( ( S . FTR_MEAN_INTENSITY , expected_means ) , <EOL> ( S . FTR_STD_INTENSITY , expected_sds ) ) : <EOL> value = m . get_current_measurement ( <EOL> OBJECTS_NAME , <EOL> "<STR_LIT:_>" . join ( ( S . C_WORM , ftr , STRAIGHTENED_IMAGE_NAME , <EOL> module . get_scale_name ( i , None ) ) ) ) <EOL> self . assertEqual ( len ( value ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( value [ <NUM_LIT:0> ] , expected [ i ] ) <EOL> ww = i_w [ : , : , : , np . newaxis ] * j_w [ : , : , np . newaxis , : ] <EOL> weights = s2 ( ww ) <EOL> expected_means = s2 ( image [ : , : , np . newaxis , np . newaxis ] * ww ) / weights <EOL> counts = s2 ( ww > <NUM_LIT:0> ) <EOL> expected_sds = np . sqrt ( <EOL> s2 ( ww * ( image [ : , : , np . newaxis , np . newaxis ] - <EOL> expected_means [ np . newaxis , np . newaxis , : , : ] ) ** <NUM_LIT:2> ) / <EOL> weights * counts / ( counts - <NUM_LIT:1> ) ) <EOL> for stripe in range ( <NUM_LIT:3> ) : <EOL> for segment in range ( <NUM_LIT:4> ) : <EOL> for ftr , expected in ( ( S . FTR_MEAN_INTENSITY , expected_means ) , <EOL> ( S . FTR_STD_INTENSITY , expected_sds ) ) : <EOL> mname = "<STR_LIT:_>" . join ( ( S . C_WORM , ftr , STRAIGHTENED_IMAGE_NAME , <EOL> module . get_scale_name ( stripe , segment ) ) ) <EOL> value = m . get_current_measurement ( OBJECTS_NAME , mname ) <EOL> self . assertEqual ( len ( value ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( value [ <NUM_LIT:0> ] , expected [ segment , stripe ] ) <EOL> def test_05_01_flip_no_worms ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:5> , <NUM_LIT:2> , <NUM_LIT:0> ) ) , <EOL> np . zeros ( <NUM_LIT:0> ) , <EOL> np . zeros ( <NUM_LIT:5> ) , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) ) ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:5> <EOL> module . flip_worms . value = S . FLIP_TOP <EOL> module . run ( workspace ) <EOL> def test_05_02_flip_dont_flip_top ( self ) : <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT:0> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> image [ <NUM_LIT> : ] /= <NUM_LIT> <EOL> control_points = np . array ( [ [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:8> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:3> <EOL> module . flip_worms . value = S . FLIP_TOP <EOL> module . run ( workspace ) <EOL> image_set = workspace . image_set <EOL> self . assertTrue ( isinstance ( image_set , cpi . ImageSet ) ) <EOL> pixels = image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:1> ] , <NUM_LIT:11> ) <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> np . testing . assert_almost_equal ( pixels , image [ <NUM_LIT:16> : <NUM_LIT> , <NUM_LIT:10> : <NUM_LIT> ] ) <EOL> def test_05_03_flip_top ( self ) : <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT:0> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> image [ : <NUM_LIT> ] /= <NUM_LIT> <EOL> control_points = np . array ( [ [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:8> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:3> <EOL> module . flip_worms . value = S . FLIP_TOP <EOL> module . run ( workspace ) <EOL> image_set = workspace . image_set <EOL> self . assertTrue ( isinstance ( image_set , cpi . ImageSet ) ) <EOL> pixels = image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:1> ] , <NUM_LIT:11> ) <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> i , j = np . mgrid [ <NUM_LIT> : <NUM_LIT:15> : - <NUM_LIT:1> , <NUM_LIT:20> : <NUM_LIT:9> : - <NUM_LIT:1> ] <EOL> np . testing . assert_almost_equal ( pixels , image [ i , j ] ) <EOL> def test_05_04_flip_dont_flip_bottom ( self ) : <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> image [ : <NUM_LIT> ] /= <NUM_LIT> <EOL> control_points = np . array ( [ [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:8> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:3> <EOL> module . flip_worms . value = S . FLIP_BOTTOM <EOL> module . run ( workspace ) <EOL> image_set = workspace . image_set <EOL> self . assertTrue ( isinstance ( image_set , cpi . ImageSet ) ) <EOL> pixels = image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:1> ] , <NUM_LIT:11> ) <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> np . testing . assert_almost_equal ( pixels , image [ <NUM_LIT:16> : <NUM_LIT> , <NUM_LIT:10> : <NUM_LIT> ] ) <EOL> def test_05_05_flip_bottom ( self ) : <EOL> r = np . random . RandomState ( ) <EOL> r . seed ( <NUM_LIT> ) <EOL> image = r . uniform ( size = ( <NUM_LIT> , <NUM_LIT:30> ) ) <EOL> image [ <NUM_LIT> : ] /= <NUM_LIT> <EOL> control_points = np . array ( [ [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] , <EOL> [ [ <NUM_LIT> ] , [ <NUM_LIT:15> ] ] ] ) <EOL> lengths = np . array ( [ <NUM_LIT:8> ] ) <EOL> radii = np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:3> , <NUM_LIT:1> ] ) <EOL> workspace , module = self . make_workspace ( control_points , lengths , <EOL> radii , image ) <EOL> self . assertTrue ( isinstance ( module , S . StraightenWorms ) ) <EOL> module . width . value = <NUM_LIT:11> <EOL> module . wants_measurements . value = True <EOL> module . number_of_segments . value = <NUM_LIT:3> <EOL> module . flip_worms . value = S . FLIP_BOTTOM <EOL> module . run ( workspace ) <EOL> image_set = workspace . image_set <EOL> self . assertTrue ( isinstance ( image_set , cpi . ImageSet ) ) <EOL> pixels = image_set . get_image ( STRAIGHTENED_IMAGE_NAME ) . pixel_data <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:1> ] , <NUM_LIT:11> ) <EOL> self . assertEqual ( pixels . shape [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> i , j = np . mgrid [ <NUM_LIT> : <NUM_LIT:15> : - <NUM_LIT:1> , <NUM_LIT:20> : <NUM_LIT:9> : - <NUM_LIT:1> ] <EOL> np . testing . assert_almost_equal ( pixels , image [ i , j ] ) </s>
