<s> '''<STR_LIT>''' <EOL> import StringIO <EOL> import base64 <EOL> import unittest <EOL> import numpy as np <EOL> from cellprofiler . preferences import set_headless <EOL> set_headless ( ) <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . measurements as cpmeas <EOL> import cellprofiler . pipeline as cpp <EOL> import cellprofiler . workspace as cpw <EOL> import cellprofiler . cpimage as cpi <EOL> import cellprofiler . objects as cpo <EOL> import cellprofiler . modules . measureimageintensity as M <EOL> class TestMeasureImageIntensity ( unittest . TestCase ) : <EOL> def make_workspace ( self , object_dict = { } , image_dict = { } ) : <EOL> '''<STR_LIT>''' <EOL> module = M . MeasureImageIntensity ( ) <EOL> pipeline = cpp . Pipeline ( ) <EOL> object_set = cpo . ObjectSet ( ) <EOL> image_set_list = cpi . ImageSetList ( ) <EOL> image_set = image_set_list . get_image_set ( <NUM_LIT:0> ) <EOL> workspace = cpw . Workspace ( pipeline , <EOL> module , <EOL> image_set , <EOL> object_set , <EOL> cpmeas . Measurements ( ) , <EOL> image_set_list ) <EOL> for key in image_dict . keys ( ) : <EOL> image_set . add ( key , cpi . Image ( image_dict [ key ] ) ) <EOL> for key in object_dict . keys ( ) : <EOL> o = cpo . Objects ( ) <EOL> o . segmented = object_dict [ key ] <EOL> object_set . add_objects ( o , key ) <EOL> return workspace , module <EOL> def test_00_00_zeros ( self ) : <EOL> '''<STR_LIT>''' <EOL> workspace , module = self . make_workspace ( { } , <EOL> { "<STR_LIT>" : np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) } ) <EOL> image = workspace . image_set . get_image ( "<STR_LIT>" ) <EOL> image . mask = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , bool ) <EOL> module . images [ <NUM_LIT:0> ] . image_name . value = "<STR_LIT>" <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( m . get_object_names ( ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( m . get_object_names ( ) [ <NUM_LIT:0> ] , cpmeas . IMAGE ) <EOL> columns = module . get_measurement_columns ( workspace . pipeline ) <EOL> features = m . get_feature_names ( cpmeas . IMAGE ) <EOL> self . assertEqual ( len ( columns ) , len ( features ) ) <EOL> for column in columns : <EOL> self . assertTrue ( column [ <NUM_LIT:1> ] in features ) <EOL> def test_01_01_image ( self ) : <EOL> '''<STR_LIT>''' <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> pixels = np . random . uniform ( size = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) . astype ( np . float32 ) * <NUM_LIT> <EOL> pixels [ <NUM_LIT:0> : <NUM_LIT:2> , <NUM_LIT:0> : <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> workspace , module = self . make_workspace ( { } , <EOL> { "<STR_LIT>" : pixels } ) <EOL> image = workspace . image_set . get_image ( "<STR_LIT>" ) <EOL> module . images [ <NUM_LIT:0> ] . image_name . value = "<STR_LIT>" <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <NUM_LIT:100> ) <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <EOL> np . sum ( pixels ) ) <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <EOL> np . sum ( pixels ) / <NUM_LIT> ) <EOL> self . assertEqual ( m . get_current_image_measurement ( '<STR_LIT>' ) , <EOL> np . min ( pixels ) ) <EOL> self . assertEqual ( m . get_current_image_measurement ( '<STR_LIT>' ) , <EOL> np . max ( pixels ) ) <EOL> self . assertEqual ( m . get_current_image_measurement ( <EOL> '<STR_LIT>' ) , <NUM_LIT> ) <EOL> def test_01_02_image_and_mask ( self ) : <EOL> '''<STR_LIT>''' <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> pixels = np . random . uniform ( size = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) . astype ( np . float32 ) * <NUM_LIT> <EOL> pixels [ <NUM_LIT:1> : <NUM_LIT:3> , <NUM_LIT:1> : <NUM_LIT:3> ] = <NUM_LIT:1> <EOL> mask = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:9> ] = True <EOL> workspace , module = self . make_workspace ( { } , <EOL> { "<STR_LIT>" : pixels } ) <EOL> image = workspace . image_set . get_image ( "<STR_LIT>" ) <EOL> image . mask = mask <EOL> module . images [ <NUM_LIT:0> ] . image_name . value = "<STR_LIT>" <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <NUM_LIT:64> ) <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <EOL> np . sum ( pixels [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:9> ] ) ) <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <EOL> np . sum ( pixels [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:9> ] ) / <NUM_LIT> ) <EOL> self . assertAlmostEqual ( m . get_current_measurement ( <EOL> cpmeas . IMAGE , "<STR_LIT>" ) , <NUM_LIT> / <NUM_LIT> ) <EOL> def test_01_03_image_and_objects ( self ) : <EOL> '''<STR_LIT>''' <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> pixels = np . random . uniform ( size = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) . astype ( np . float32 ) * <NUM_LIT> <EOL> pixels [ <NUM_LIT:1> : <NUM_LIT:3> , <NUM_LIT:1> : <NUM_LIT:3> ] = <NUM_LIT:1> <EOL> objects = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , int ) <EOL> objects [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> objects [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:5> : <NUM_LIT:9> ] = <NUM_LIT:2> <EOL> workspace , module = self . make_workspace ( { "<STR_LIT>" : objects } , <EOL> { "<STR_LIT>" : pixels } ) <EOL> module . images [ <NUM_LIT:0> ] . image_name . value = "<STR_LIT>" <EOL> module . images [ <NUM_LIT:0> ] . wants_objects . value = True <EOL> module . images [ <NUM_LIT:0> ] . object_name . value = "<STR_LIT>" <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <NUM_LIT:64> ) <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <EOL> np . sum ( pixels [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:9> ] ) ) <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <EOL> np . sum ( pixels [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:9> ] ) / <NUM_LIT> ) <EOL> self . assertAlmostEqual ( m . get_current_measurement ( <EOL> cpmeas . IMAGE , "<STR_LIT>" ) , <NUM_LIT> / <NUM_LIT> ) <EOL> self . assertEqual ( len ( m . get_object_names ( ) ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( m . get_object_names ( ) [ <NUM_LIT:0> ] , cpmeas . IMAGE ) <EOL> columns = module . get_measurement_columns ( workspace . pipeline ) <EOL> features = m . get_feature_names ( cpmeas . IMAGE ) <EOL> self . assertEqual ( len ( columns ) , len ( features ) ) <EOL> for column in columns : <EOL> self . assertTrue ( column [ <NUM_LIT:1> ] in features ) <EOL> def test_01_04_image_and_objects_and_mask ( self ) : <EOL> '''<STR_LIT>''' <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> pixels = np . random . uniform ( size = ( <NUM_LIT:10> , <NUM_LIT:10> ) ) . astype ( np . float32 ) <EOL> objects = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , int ) <EOL> objects [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:5> ] = <NUM_LIT:1> <EOL> objects [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:5> : ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:1> : <NUM_LIT:9> , : <NUM_LIT:9> ] = True <EOL> workspace , module = self . make_workspace ( { "<STR_LIT>" : objects } , <EOL> { "<STR_LIT>" : pixels } ) <EOL> image = workspace . image_set . get_image ( "<STR_LIT>" ) <EOL> image . mask = mask <EOL> module . images [ <NUM_LIT:0> ] . image_name . value = "<STR_LIT>" <EOL> module . images [ <NUM_LIT:0> ] . wants_objects . value = True <EOL> module . images [ <NUM_LIT:0> ] . object_name . value = "<STR_LIT>" <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <NUM_LIT:64> ) <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <EOL> np . sum ( pixels [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:9> ] ) ) <EOL> self . assertEqual ( m . get_current_measurement ( cpmeas . IMAGE , "<STR_LIT>" ) , <EOL> np . sum ( pixels [ <NUM_LIT:1> : <NUM_LIT:9> , <NUM_LIT:1> : <NUM_LIT:9> ] ) / <NUM_LIT> ) <EOL> def test_02_01_load_matlab ( self ) : <EOL> '''<STR_LIT>''' <EOL> data = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> fd = StringIO . StringIO ( base64 . b64decode ( data ) ) <EOL> p = cpp . Pipeline ( ) <EOL> def error_handler ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . LoadExceptionEvent ) ) <EOL> p . add_listener ( error_handler ) <EOL> p . load ( fd ) <EOL> self . assertEqual ( len ( p . modules ( ) ) , <NUM_LIT:2> ) <EOL> module = p . modules ( ) [ <NUM_LIT:1> ] <EOL> self . assertEqual ( len ( module . images ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( module . images [ <NUM_LIT:0> ] . image_name . value , '<STR_LIT>' ) <EOL> self . assertFalse ( module . images [ <NUM_LIT:0> ] . wants_objects . value ) <EOL> def test_02_01_load_v1 ( self ) : <EOL> '''<STR_LIT>''' <EOL> data = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> fd = StringIO . StringIO ( base64 . b64decode ( data ) ) <EOL> p = cpp . Pipeline ( ) <EOL> def error_handler ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . LoadExceptionEvent ) ) <EOL> p . add_listener ( error_handler ) <EOL> p . load ( fd ) <EOL> self . assertEqual ( len ( p . modules ( ) ) , <NUM_LIT:2> ) <EOL> module = p . modules ( ) [ <NUM_LIT:1> ] <EOL> self . assertEqual ( len ( module . images ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( module . images [ <NUM_LIT:0> ] . image_name . value , '<STR_LIT>' ) <EOL> self . assertFalse ( module . images [ <NUM_LIT:0> ] . wants_objects . value ) <EOL> def test_03_01_get_measurement_columns ( self ) : <EOL> module = M . MeasureImageIntensity ( ) <EOL> image_names = [ '<STR_LIT>' % i for i in range ( <NUM_LIT:3> ) ] <EOL> object_names = [ '<STR_LIT>' % i for i in range ( <NUM_LIT:3> ) ] <EOL> first = True <EOL> expected_suffixes = [ ] <EOL> for image_name in image_names : <EOL> if first : <EOL> first = False <EOL> else : <EOL> module . add_image_measurement ( ) <EOL> im = module . images [ - <NUM_LIT:1> ] <EOL> im . image_name . value = image_name <EOL> im . wants_objects . value = False <EOL> expected_suffixes . append ( image_name ) <EOL> for object_name in object_names : <EOL> module . add_image_measurement ( ) <EOL> im = module . images [ - <NUM_LIT:1> ] <EOL> im . image_name . value = image_name <EOL> im . wants_objects . value = True <EOL> im . object_name . value = object_name <EOL> expected_suffixes . append ( "<STR_LIT>" % ( image_name , object_name ) ) <EOL> columns = module . get_measurement_columns ( None ) <EOL> self . assertTrue ( all ( [ column [ <NUM_LIT:0> ] == cpmeas . IMAGE for column in columns ] ) ) <EOL> for expected_suffix in expected_suffixes : <EOL> for feature , coltype in ( ( M . F_TOTAL_INTENSITY , cpmeas . COLTYPE_FLOAT ) , <EOL> ( M . F_MEAN_INTENSITY , cpmeas . COLTYPE_FLOAT ) , <EOL> ( M . F_MIN_INTENSITY , cpmeas . COLTYPE_FLOAT ) , <EOL> ( M . F_MAX_INTENSITY , cpmeas . COLTYPE_FLOAT ) , <EOL> ( M . F_TOTAL_AREA , cpmeas . COLTYPE_INTEGER ) , <EOL> ( M . F_PERCENT_MAXIMAL , cpmeas . COLTYPE_FLOAT ) , <EOL> ( M . F_MAD_INTENSITY , cpmeas . COLTYPE_FLOAT ) , <EOL> ( M . F_LOWER_QUARTILE , cpmeas . COLTYPE_FLOAT ) , <EOL> ( M . F_UPPER_QUARTILE , cpmeas . COLTYPE_FLOAT ) ) : <EOL> feature_name = feature % expected_suffix <EOL> self . assertTrue ( any ( [ ( column [ <NUM_LIT:1> ] == feature_name and <EOL> column [ <NUM_LIT:2> ] == coltype ) <EOL> for column in columns ] ) ) </s>
