<s> '''<STR_LIT>''' <EOL> import base64 <EOL> import os <EOL> import unittest <EOL> import zlib <EOL> from StringIO import StringIO <EOL> import PIL . Image as PILImage <EOL> import numpy as np <EOL> import scipy . ndimage <EOL> from matplotlib . image import pil_to_array <EOL> from cellprofiler . preferences import set_headless <EOL> set_headless ( ) <EOL> import cellprofiler . pipeline as cpp <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . cpimage as cpi <EOL> import cellprofiler . measurements as cpmeas <EOL> import cellprofiler . objects as cpo <EOL> import cellprofiler . workspace as cpw <EOL> import cellprofiler . modules . maskobjects as M <EOL> INPUT_OBJECTS = '<STR_LIT>' <EOL> OUTPUT_OBJECTS = '<STR_LIT>' <EOL> MASKING_OBJECTS = '<STR_LIT>' <EOL> MASKING_IMAGE = '<STR_LIT>' <EOL> OUTPUT_OUTLINES = '<STR_LIT>' <EOL> class TestMaskObjects ( unittest . TestCase ) : <EOL> def test_01_01_load_matlab_exclude ( 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> pipeline = cpp . Pipeline ( ) <EOL> def callback ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . LoadExceptionEvent ) ) <EOL> pipeline . add_listener ( callback ) <EOL> pipeline . load ( StringIO ( zlib . decompress ( base64 . b64decode ( data ) ) ) ) <EOL> self . assertEqual ( len ( pipeline . modules ( ) ) , <NUM_LIT:5> ) <EOL> module = pipeline . modules ( ) [ - <NUM_LIT:2> ] <EOL> self . assertTrue ( isinstance ( module , M . MaskObjects ) ) <EOL> self . assertEqual ( module . object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . mask_choice . value , M . MC_OBJECTS ) <EOL> self . assertEqual ( module . masking_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . remaining_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . retain_or_renumber . value , M . R_RENUMBER ) <EOL> self . assertEqual ( module . overlap_choice . value , M . P_MASK ) <EOL> self . assertTrue ( module . wants_outlines ) <EOL> self . assertEqual ( module . outlines_name . value , "<STR_LIT>" ) <EOL> module = pipeline . modules ( ) [ - <NUM_LIT:1> ] <EOL> self . assertTrue ( isinstance ( module , M . MaskObjects ) ) <EOL> self . assertEqual ( module . object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . mask_choice . value , M . MC_OBJECTS ) <EOL> self . assertEqual ( module . masking_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . remaining_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . retain_or_renumber . value , M . R_RETAIN ) <EOL> self . assertEqual ( module . overlap_choice . value , M . P_REMOVE ) <EOL> self . assertFalse ( module . wants_outlines ) <EOL> def test_01_02_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:4> ) <EOL> module = pipeline . modules ( ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( module , M . MaskObjects ) ) <EOL> self . assertEqual ( module . object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . mask_choice . value , M . MC_OBJECTS ) <EOL> self . assertEqual ( module . masking_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . remaining_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . retain_or_renumber . value , M . R_RENUMBER ) <EOL> self . assertEqual ( module . overlap_choice . value , M . P_MASK ) <EOL> self . assertFalse ( module . wants_outlines ) <EOL> self . assertFalse ( module . wants_inverted_mask ) <EOL> module = pipeline . modules ( ) [ <NUM_LIT:1> ] <EOL> self . assertTrue ( isinstance ( module , M . MaskObjects ) ) <EOL> self . assertEqual ( module . object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . mask_choice . value , M . MC_IMAGE ) <EOL> self . assertEqual ( module . masking_image . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . remaining_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . retain_or_renumber . value , M . R_RETAIN ) <EOL> self . assertEqual ( module . overlap_choice . value , M . P_KEEP ) <EOL> self . assertTrue ( module . wants_outlines ) <EOL> self . assertEqual ( module . outlines_name . value , "<STR_LIT>" ) <EOL> self . assertFalse ( module . wants_inverted_mask ) <EOL> module = pipeline . modules ( ) [ <NUM_LIT:2> ] <EOL> self . assertTrue ( isinstance ( module , M . MaskObjects ) ) <EOL> self . assertEqual ( module . object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . mask_choice . value , M . MC_OBJECTS ) <EOL> self . assertEqual ( module . masking_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . remaining_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . retain_or_renumber . value , M . R_RENUMBER ) <EOL> self . assertEqual ( module . overlap_choice . value , M . P_REMOVE ) <EOL> self . assertFalse ( module . wants_outlines ) <EOL> self . assertFalse ( module . wants_inverted_mask ) <EOL> module = pipeline . modules ( ) [ <NUM_LIT:3> ] <EOL> self . assertTrue ( isinstance ( module , M . MaskObjects ) ) <EOL> self . assertEqual ( module . object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . mask_choice . value , M . MC_OBJECTS ) <EOL> self . assertEqual ( module . masking_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . remaining_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . retain_or_renumber . value , M . R_RENUMBER ) <EOL> self . assertEqual ( module . overlap_choice . value , M . P_REMOVE_PERCENTAGE ) <EOL> self . assertAlmostEqual ( module . overlap_fraction . value , <NUM_LIT> ) <EOL> self . assertFalse ( module . wants_outlines ) <EOL> self . assertFalse ( module . wants_inverted_mask ) <EOL> def test_01_03_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:1> ) <EOL> module = pipeline . modules ( ) [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( module , M . MaskObjects ) ) <EOL> self . assertEqual ( module . object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . mask_choice . value , M . MC_OBJECTS ) <EOL> self . assertEqual ( module . masking_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . remaining_objects . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . retain_or_renumber . value , M . R_RENUMBER ) <EOL> self . assertEqual ( module . overlap_choice . value , M . P_MASK ) <EOL> self . assertFalse ( module . wants_outlines ) <EOL> self . assertTrue ( module . wants_inverted_mask ) <EOL> def make_workspace ( self , labels , overlap_choice , masking_objects = None , <EOL> masking_image = None , renumber = True , <EOL> wants_outlines = False ) : <EOL> module = M . MaskObjects ( ) <EOL> module . module_num = <NUM_LIT:1> <EOL> module . object_name . value = INPUT_OBJECTS <EOL> module . remaining_objects . value = OUTPUT_OBJECTS <EOL> module . mask_choice . value = ( M . MC_OBJECTS if masking_objects is not None <EOL> else M . MC_IMAGE ) <EOL> module . masking_image . value = MASKING_IMAGE <EOL> module . masking_objects . value = MASKING_OBJECTS <EOL> module . retain_or_renumber . value = ( M . R_RENUMBER if renumber <EOL> else M . R_RETAIN ) <EOL> module . overlap_choice . value = overlap_choice <EOL> module . wants_outlines . value = wants_outlines <EOL> module . outlines_name . value = OUTPUT_OUTLINES <EOL> pipeline = cpp . Pipeline ( ) <EOL> def callback ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . RunExceptionEvent ) ) <EOL> pipeline . add_listener ( callback ) <EOL> pipeline . add_module ( module ) <EOL> object_set = cpo . ObjectSet ( ) <EOL> io = cpo . Objects ( ) <EOL> io . segmented = labels <EOL> object_set . add_objects ( io , INPUT_OBJECTS ) <EOL> if masking_objects is not None : <EOL> oo = cpo . Objects ( ) <EOL> oo . segmented = masking_objects <EOL> object_set . add_objects ( oo , MASKING_OBJECTS ) <EOL> image_set_list = cpi . ImageSetList ( ) <EOL> image_set = image_set_list . get_image_set ( <NUM_LIT:0> ) <EOL> if masking_image is not None : <EOL> mi = cpi . Image ( masking_image ) <EOL> image_set . add ( MASKING_IMAGE , mi ) <EOL> workspace = cpw . Workspace ( pipeline , module , image_set , object_set , <EOL> cpmeas . Measurements ( ) , image_set_list ) <EOL> return workspace , module <EOL> def test_02_01_measurement_columns ( self ) : <EOL> '''<STR_LIT>''' <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) , <EOL> M . P_MASK , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) ) <EOL> columns = module . get_measurement_columns ( workspace . pipeline ) <EOL> self . assertEqual ( len ( columns ) , <NUM_LIT:6> ) <EOL> for expected in ( <EOL> ( cpmeas . IMAGE , M . I . FF_COUNT % OUTPUT_OBJECTS , cpmeas . COLTYPE_INTEGER ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_X , cpmeas . COLTYPE_FLOAT ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_Y , cpmeas . COLTYPE_FLOAT ) , <EOL> ( OUTPUT_OBJECTS , M . I . FF_PARENT % INPUT_OBJECTS , cpmeas . COLTYPE_INTEGER ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_NUMBER_OBJECT_NUMBER , cpmeas . COLTYPE_INTEGER ) , <EOL> ( INPUT_OBJECTS , M . I . FF_CHILDREN_COUNT % OUTPUT_OBJECTS , cpmeas . COLTYPE_INTEGER ) ) : <EOL> self . assertTrue ( any ( [ all ( [ c in e for c , e in zip ( column , expected ) ] ) <EOL> for column in columns ] ) ) <EOL> def test_02_02_measurement_categories ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) , <EOL> M . MC_OBJECTS , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) ) <EOL> categories = module . get_categories ( workspace . pipeline , "<STR_LIT>" ) <EOL> self . assertEqual ( len ( categories ) , <NUM_LIT:0> ) <EOL> categories = module . get_categories ( workspace . pipeline , cpmeas . IMAGE ) <EOL> self . assertEqual ( len ( categories ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( categories [ <NUM_LIT:0> ] , M . I . C_COUNT ) <EOL> categories = module . get_categories ( workspace . pipeline , OUTPUT_OBJECTS ) <EOL> self . assertEqual ( len ( categories ) , <NUM_LIT:3> ) <EOL> for category , expected in zip ( sorted ( categories ) , <EOL> ( M . I . C_LOCATION , M . I . C_NUMBER , <EOL> M . I . C_PARENT ) ) : <EOL> self . assertEqual ( category , expected ) <EOL> categories = module . get_categories ( workspace . pipeline , INPUT_OBJECTS ) <EOL> self . assertEqual ( len ( categories ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( categories [ <NUM_LIT:0> ] , M . I . C_CHILDREN ) <EOL> def test_02_03_measurements ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) , <EOL> M . P_MASK , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) ) <EOL> ftr_count = ( M . I . FF_CHILDREN_COUNT % OUTPUT_OBJECTS ) . split ( '<STR_LIT:_>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> d = { "<STR_LIT>" : { } , <EOL> cpmeas . IMAGE : { "<STR_LIT>" : [ ] , M . I . C_COUNT : [ OUTPUT_OBJECTS ] } , <EOL> OUTPUT_OBJECTS : { "<STR_LIT>" : [ ] , <EOL> M . I . C_LOCATION : [ M . I . FTR_CENTER_X , <EOL> M . I . FTR_CENTER_Y ] , <EOL> M . I . C_PARENT : [ INPUT_OBJECTS ] , <EOL> M . I . C_NUMBER : [ M . I . FTR_OBJECT_NUMBER ] } , <EOL> INPUT_OBJECTS : { "<STR_LIT>" : [ ] , <EOL> M . I . C_CHILDREN : [ ftr_count ] } <EOL> } <EOL> for object_name in d . keys ( ) : <EOL> od = d [ object_name ] <EOL> for category in od . keys ( ) : <EOL> features = module . get_measurements ( workspace . pipeline , <EOL> object_name , <EOL> category ) <EOL> expected = od [ category ] <EOL> self . assertEqual ( len ( features ) , len ( expected ) ) <EOL> for feature , e in zip ( sorted ( features ) , sorted ( expected ) ) : <EOL> self . assertEqual ( feature , e ) <EOL> def test_03_01_mask_nothing ( self ) : <EOL> workspace , module = self . make_workspace ( np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) , <EOL> M . MC_OBJECTS , <EOL> np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) ) <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> value = m . get_current_image_measurement ( M . I . FF_COUNT % OUTPUT_OBJECTS ) <EOL> self . assertEqual ( value , <NUM_LIT:0> ) <EOL> for object_name , feature in ( <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_X ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_Y ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_NUMBER_OBJECT_NUMBER ) , <EOL> ( OUTPUT_OBJECTS , M . I . FF_PARENT % INPUT_OBJECTS ) , <EOL> ( INPUT_OBJECTS , M . I . FF_CHILDREN_COUNT % OUTPUT_OBJECTS ) ) : <EOL> data = m . get_current_measurement ( object_name , feature ) <EOL> self . assertEqual ( len ( data ) , <NUM_LIT:0> ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == <NUM_LIT:0> ) ) <EOL> def test_03_02_mask_with_objects ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = <NUM_LIT:1> <EOL> expected = labels . copy ( ) <EOL> expected [ mask == <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> workspace , module = self . make_workspace ( labels , M . P_MASK , mask ) <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> expected_x = np . array ( [ <NUM_LIT:4> , <NUM_LIT:4> ] ) <EOL> expected_y = np . array ( [ <NUM_LIT:5> , <NUM_LIT> ] ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> value = m . get_current_image_measurement ( M . I . FF_COUNT % OUTPUT_OBJECTS ) <EOL> self . assertEqual ( value , <NUM_LIT:2> ) <EOL> for object_name , feature , expected in ( <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_X , expected_x ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_Y , expected_y ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_NUMBER_OBJECT_NUMBER , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) , <EOL> ( OUTPUT_OBJECTS , M . I . FF_PARENT % INPUT_OBJECTS , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) , <EOL> ( INPUT_OBJECTS , M . I . FF_CHILDREN_COUNT % OUTPUT_OBJECTS , np . array ( [ <NUM_LIT:1> , <NUM_LIT:1> ] ) ) ) : <EOL> data = m . get_current_measurement ( object_name , feature ) <EOL> self . assertEqual ( len ( data ) , len ( expected ) ) <EOL> for value , e in zip ( data , expected ) : <EOL> self . assertEqual ( value , e ) <EOL> def test_03_03_mask_with_image ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = True <EOL> expected = labels . copy ( ) <EOL> expected [ ~ mask ] = <NUM_LIT:0> <EOL> workspace , module = self . make_workspace ( labels , M . P_MASK , <EOL> masking_image = mask ) <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> expected_x = np . array ( [ <NUM_LIT:4> , <NUM_LIT:4> ] ) <EOL> expected_y = np . array ( [ <NUM_LIT:5> , <NUM_LIT> ] ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> value = m . get_current_image_measurement ( M . I . FF_COUNT % OUTPUT_OBJECTS ) <EOL> self . assertEqual ( value , <NUM_LIT:2> ) <EOL> for object_name , feature , expected in ( <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_X , expected_x ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_Y , expected_y ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_NUMBER_OBJECT_NUMBER , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) , <EOL> ( OUTPUT_OBJECTS , M . I . FF_PARENT % INPUT_OBJECTS , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) , <EOL> ( INPUT_OBJECTS , M . I . FF_CHILDREN_COUNT % OUTPUT_OBJECTS , np . array ( [ <NUM_LIT:1> , <NUM_LIT:1> ] ) ) ) : <EOL> data = m . get_current_measurement ( object_name , feature ) <EOL> self . assertEqual ( len ( data ) , len ( expected ) ) <EOL> for value , e in zip ( data , expected ) : <EOL> self . assertEqual ( value , e ) <EOL> def test_03_04_mask_renumber ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:30> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:3> <EOL> labels [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:30> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = True <EOL> expected = labels . copy ( ) <EOL> expected [ ~ mask ] = <NUM_LIT:0> <EOL> expected [ expected == <NUM_LIT:3> ] = <NUM_LIT:2> <EOL> workspace , module = self . make_workspace ( labels , M . P_MASK , <EOL> masking_image = mask ) <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> expected_x = np . array ( [ <NUM_LIT:4> , <NUM_LIT:4> ] ) <EOL> expected_y = np . array ( [ <NUM_LIT:5> , <NUM_LIT> ] ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> value = m . get_current_image_measurement ( M . I . FF_COUNT % OUTPUT_OBJECTS ) <EOL> self . assertEqual ( value , <NUM_LIT:2> ) <EOL> for object_name , feature , expected in ( <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_X , expected_x ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_Y , expected_y ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_NUMBER_OBJECT_NUMBER , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> ] ) ) , <EOL> ( OUTPUT_OBJECTS , M . I . FF_PARENT % INPUT_OBJECTS , np . array ( [ <NUM_LIT:1> , <NUM_LIT:3> ] ) ) , <EOL> ( INPUT_OBJECTS , M . I . FF_CHILDREN_COUNT % OUTPUT_OBJECTS , np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) ) ) : <EOL> data = m . get_current_measurement ( object_name , feature ) <EOL> self . assertEqual ( len ( data ) , len ( expected ) ) <EOL> for value , e in zip ( data , expected ) : <EOL> self . assertEqual ( value , e ) <EOL> def test_03_05_mask_retain ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:30> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:3> <EOL> labels [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:30> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = True <EOL> expected = labels . copy ( ) <EOL> expected [ ~ mask ] = <NUM_LIT:0> <EOL> workspace , module = self . make_workspace ( labels , M . P_MASK , <EOL> masking_image = mask , <EOL> renumber = False ) <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> expected_x = np . array ( [ <NUM_LIT:4> , None , <NUM_LIT:4> ] ) <EOL> expected_y = np . array ( [ <NUM_LIT:5> , None , <NUM_LIT> ] ) <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> value = m . get_current_image_measurement ( M . I . FF_COUNT % OUTPUT_OBJECTS ) <EOL> self . assertEqual ( value , <NUM_LIT:3> ) <EOL> for object_name , feature , expected in ( <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_X , expected_x ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_LOCATION_CENTER_Y , expected_y ) , <EOL> ( OUTPUT_OBJECTS , M . I . M_NUMBER_OBJECT_NUMBER , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) , <EOL> ( OUTPUT_OBJECTS , M . I . FF_PARENT % INPUT_OBJECTS , np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) ) , <EOL> ( INPUT_OBJECTS , M . I . FF_CHILDREN_COUNT % OUTPUT_OBJECTS , np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] ) ) ) : <EOL> data = m . get_current_measurement ( object_name , feature ) <EOL> self . assertEqual ( len ( data ) , len ( expected ) ) <EOL> for value , e in zip ( data , expected ) : <EOL> if e is None : <EOL> self . assertTrue ( np . isnan ( value ) ) <EOL> else : <EOL> self . assertEqual ( value , e ) <EOL> def test_03_06_mask_outlines ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = True <EOL> expected = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , bool ) <EOL> expected [ <NUM_LIT:3> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:6> ] = True <EOL> expected [ <NUM_LIT:4> : <NUM_LIT:7> , <NUM_LIT:4> ] = False <EOL> expected [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:6> ] = True <EOL> expected [ <NUM_LIT> : <NUM_LIT:16> , <NUM_LIT:4> ] = False <EOL> workspace , module = self . make_workspace ( labels , M . P_MASK , <EOL> masking_image = mask , <EOL> wants_outlines = True ) <EOL> module . run ( workspace ) <EOL> outlines = workspace . image_set . get_image ( OUTPUT_OUTLINES ) <EOL> self . assertTrue ( np . all ( outlines . pixel_data == expected ) ) <EOL> def test_03_07_mask_invert ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = labels == <NUM_LIT:1> <EOL> mask [ <NUM_LIT:2> , <NUM_LIT:3> ] = False <EOL> mask = ~ mask <EOL> expected = labels <EOL> expected [ labels != <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> expected [ <NUM_LIT:2> , <NUM_LIT:3> ] = <NUM_LIT:0> <EOL> workspace , module = self . make_workspace ( labels , M . P_MASK , <EOL> masking_image = mask ) <EOL> self . assertTrue ( isinstance ( module , M . MaskObjects ) ) <EOL> module . wants_inverted_mask . value = True <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> def test_04_01_keep ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:30> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:3> <EOL> labels [ <NUM_LIT> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:30> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = True <EOL> expected = labels . copy ( ) <EOL> expected [ expected == <NUM_LIT:2> ] = <NUM_LIT:0> <EOL> expected [ expected == <NUM_LIT:3> ] = <NUM_LIT:2> <EOL> workspace , module = self . make_workspace ( labels , M . P_KEEP , <EOL> masking_image = mask ) <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> def test_04_02_remove ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:2> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:7> ] = True <EOL> expected = labels . copy ( ) <EOL> expected [ labels == <NUM_LIT:2> ] = <NUM_LIT:0> <EOL> workspace , module = self . make_workspace ( labels , M . P_REMOVE , <EOL> masking_image = mask ) <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> def test_04_03_remove_percent ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:6> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , bool ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = True <EOL> expected = labels . copy ( ) <EOL> expected [ labels == <NUM_LIT:2> ] = <NUM_LIT:0> <EOL> workspace , module = self . make_workspace ( labels , M . P_REMOVE_PERCENTAGE , <EOL> masking_image = mask ) <EOL> module . overlap_fraction . value = <NUM_LIT> <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> def test_05_01_different_object_sizes ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:30> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:20> ) , int ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = <NUM_LIT:1> <EOL> expected = labels . copy ( ) <EOL> expected [ : <NUM_LIT:20> , : ] [ mask [ : , : <NUM_LIT:10> ] == <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> workspace , module = self . make_workspace ( labels , M . P_MASK , mask ) <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) <EOL> def test_05_02_different_image_sizes ( self ) : <EOL> labels = np . zeros ( ( <NUM_LIT:30> , <NUM_LIT:10> ) , int ) <EOL> labels [ <NUM_LIT:2> : <NUM_LIT:8> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> labels [ <NUM_LIT:12> : <NUM_LIT> , <NUM_LIT:3> : <NUM_LIT:7> ] = <NUM_LIT:2> <EOL> mask = np . zeros ( ( <NUM_LIT:20> , <NUM_LIT:20> ) , bool ) <EOL> mask [ <NUM_LIT:3> : <NUM_LIT> , <NUM_LIT:2> : <NUM_LIT:6> ] = <NUM_LIT:1> <EOL> expected = labels . copy ( ) <EOL> expected [ : <NUM_LIT:20> , : ] [ mask [ : , : <NUM_LIT:10> ] == <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> workspace , module = self . make_workspace ( labels , M . P_MASK , <EOL> masking_image = mask ) <EOL> module . run ( workspace ) <EOL> objects = workspace . object_set . get_objects ( OUTPUT_OBJECTS ) <EOL> self . assertTrue ( np . all ( objects . segmented == expected ) ) </s>
