<s> '''<STR_LIT>''' <EOL> import base64 <EOL> import unittest <EOL> import zlib <EOL> from StringIO import StringIO <EOL> import numpy as np <EOL> from scipy . ndimage import distance_transform_edt <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 . relateobjects as R <EOL> PARENT_OBJECTS = '<STR_LIT>' <EOL> CHILD_OBJECTS = '<STR_LIT>' <EOL> MEASUREMENT = '<STR_LIT>' <EOL> IGNORED_MEASUREMENT = '<STR_LIT>' % R . C_PARENT <EOL> class TestRelateObjects ( unittest . TestCase ) : <EOL> def make_workspace ( self , parents , children , fake_measurement = False ) : <EOL> '''<STR_LIT>''' <EOL> pipeline = cpp . Pipeline ( ) <EOL> if fake_measurement : <EOL> class FakeModule ( cpm . CPModule ) : <EOL> def get_measurement_columns ( self , pipeline ) : <EOL> return [ ( CHILD_OBJECTS , MEASUREMENT , cpmeas . COLTYPE_FLOAT ) , <EOL> ( CHILD_OBJECTS , IGNORED_MEASUREMENT , cpmeas . COLTYPE_INTEGER ) ] <EOL> module = FakeModule ( ) <EOL> module . module_num = <NUM_LIT:1> <EOL> pipeline . add_module ( module ) <EOL> module = R . Relate ( ) <EOL> module . parent_name . value = PARENT_OBJECTS <EOL> module . sub_object_name . value = CHILD_OBJECTS <EOL> module . find_parent_child_distances . value = R . D_NONE <EOL> module . module_num = <NUM_LIT:2> if fake_measurement else <NUM_LIT:1> <EOL> pipeline . add_module ( module ) <EOL> object_set = cpo . ObjectSet ( ) <EOL> image_set_list = cpi . ImageSetList ( ) <EOL> image_set = image_set_list . get_image_set ( <NUM_LIT:0> ) <EOL> m = cpmeas . Measurements ( ) <EOL> m . add_image_measurement ( cpmeas . GROUP_NUMBER , <NUM_LIT:1> ) <EOL> m . add_image_measurement ( cpmeas . GROUP_INDEX , <NUM_LIT:1> ) <EOL> workspace = cpw . Workspace ( pipeline , <EOL> module , <EOL> image_set , <EOL> object_set , <EOL> m , <EOL> image_set_list ) <EOL> o = cpo . Objects ( ) <EOL> if parents . shape [ <NUM_LIT:1> ] == <NUM_LIT:3> : <EOL> o . ijv = parents <EOL> else : <EOL> o . segmented = parents <EOL> object_set . add_objects ( o , PARENT_OBJECTS ) <EOL> o = cpo . Objects ( ) <EOL> if children . shape [ <NUM_LIT:1> ] == <NUM_LIT:3> : <EOL> o . ijv = children <EOL> else : <EOL> o . segmented = children <EOL> object_set . add_objects ( o , CHILD_OBJECTS ) <EOL> return workspace , module <EOL> def features_and_columns_match ( self , workspace ) : <EOL> module = workspace . module <EOL> pipeline = workspace . pipeline <EOL> measurements = workspace . measurements <EOL> object_names = [ x for x in measurements . get_object_names ( ) <EOL> if x != cpmeas . IMAGE ] <EOL> features = [ [ feature <EOL> for feature in measurements . get_feature_names ( object_name ) <EOL> if feature not in ( MEASUREMENT , IGNORED_MEASUREMENT ) ] <EOL> for object_name in object_names ] <EOL> columns = module . get_measurement_columns ( pipeline ) <EOL> self . assertEqual ( sum ( [ len ( f ) for f in features ] ) , len ( columns ) ) <EOL> for column in columns : <EOL> index = object_names . index ( column [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( column [ <NUM_LIT:1> ] in features [ index ] ) <EOL> def test_01_01_load_matlab_v4 ( 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> fd = StringIO ( zlib . decompress ( base64 . b64decode ( data ) ) ) <EOL> pipeline = cpp . Pipeline ( ) <EOL> def callback ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . LoadExceptionEvent ) ) <EOL> pipeline . add_listener ( callback ) <EOL> pipeline . load ( fd ) <EOL> self . assertEqual ( len ( pipeline . modules ( ) ) , <NUM_LIT:4> ) <EOL> module = pipeline . modules ( ) [ <NUM_LIT:3> ] <EOL> self . assertTrue ( isinstance ( module , R . Relate ) ) <EOL> self . assertEqual ( module . sub_object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . parent_name . value , "<STR_LIT>" ) <EOL> self . assertTrue ( module . wants_per_parent_means . value ) <EOL> def test_01_02_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> fd = StringIO ( zlib . decompress ( base64 . b64decode ( data ) ) ) <EOL> pipeline = cpp . Pipeline ( ) <EOL> def callback ( caller , event ) : <EOL> self . assertFalse ( isinstance ( event , cpp . LoadExceptionEvent ) ) <EOL> pipeline . add_listener ( callback ) <EOL> pipeline . load ( fd ) <EOL> self . assertEqual ( len ( pipeline . modules ( ) ) , <NUM_LIT:4> ) <EOL> module = pipeline . modules ( ) [ <NUM_LIT:3> ] <EOL> self . assertTrue ( isinstance ( module , R . Relate ) ) <EOL> self . assertEqual ( module . sub_object_name . value , "<STR_LIT>" ) <EOL> self . assertEqual ( module . parent_name . value , "<STR_LIT>" ) <EOL> self . assertFalse ( module . wants_per_parent_means . value ) <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:8> ) <EOL> module = pipeline . modules ( ) [ - <NUM_LIT:1> ] <EOL> self . assertTrue ( isinstance ( module , R . Relate ) ) <EOL> self . assertEqual ( module . sub_object_name , "<STR_LIT>" ) <EOL> self . assertEqual ( module . parent_name , "<STR_LIT>" ) <EOL> self . assertEqual ( module . find_parent_child_distances , R . D_BOTH ) <EOL> self . assertFalse ( module . wants_per_parent_means ) <EOL> self . assertTrue ( module . wants_step_parent_distances ) <EOL> self . assertEqual ( len ( module . step_parent_names ) , <NUM_LIT:2> ) <EOL> for group , expected in zip ( module . step_parent_names , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> self . assertEqual ( group . step_parent_name , expected ) <EOL> def test_02_01_relate_zeros ( self ) : <EOL> '''<STR_LIT>''' <EOL> labels = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , int ) <EOL> workspace , module = self . make_workspace ( labels , labels ) <EOL> module . wants_per_parent_means . value = False <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> parents_of = m . get_current_measurement ( CHILD_OBJECTS , <EOL> "<STR_LIT>" % PARENT_OBJECTS ) <EOL> self . assertEqual ( np . product ( parents_of . shape ) , <NUM_LIT:0> ) <EOL> child_count = m . get_current_measurement ( PARENT_OBJECTS , <EOL> "<STR_LIT>" % <EOL> CHILD_OBJECTS ) <EOL> self . assertEqual ( np . product ( child_count . shape ) , <NUM_LIT:0> ) <EOL> self . features_and_columns_match ( workspace ) <EOL> def test_02_01_relate_one ( self ) : <EOL> '''<STR_LIT>''' <EOL> parent_labels = np . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , int ) <EOL> child_labels = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:10> ) , int ) <EOL> child_labels [ <NUM_LIT:3> : <NUM_LIT:5> , <NUM_LIT:4> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> workspace , module = self . make_workspace ( parent_labels , child_labels ) <EOL> module . wants_per_parent_means . value = False <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> parents_of = m . get_current_measurement ( CHILD_OBJECTS , <EOL> "<STR_LIT>" % PARENT_OBJECTS ) <EOL> self . assertEqual ( np . product ( parents_of . shape ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( parents_of [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> child_count = m . get_current_measurement ( PARENT_OBJECTS , <EOL> "<STR_LIT>" % <EOL> CHILD_OBJECTS ) <EOL> self . assertEqual ( np . product ( child_count . shape ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( child_count [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> self . features_and_columns_match ( workspace ) <EOL> def test_02_02_relate_wrong_size ( self ) : <EOL> '''<STR_LIT>''' <EOL> parent_labels = np . ones ( ( <NUM_LIT:20> , <NUM_LIT:10> ) , int ) <EOL> parent_labels [ <NUM_LIT:10> : , : ] = <NUM_LIT:0> <EOL> child_labels = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:20> ) , int ) <EOL> child_labels [ <NUM_LIT:3> : <NUM_LIT:5> , <NUM_LIT:4> : <NUM_LIT:7> ] = <NUM_LIT:1> <EOL> workspace , module = self . make_workspace ( parent_labels , child_labels ) <EOL> module . wants_per_parent_means . value = False <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> parents_of = m . get_current_measurement ( CHILD_OBJECTS , <EOL> "<STR_LIT>" % PARENT_OBJECTS ) <EOL> self . assertEqual ( np . product ( parents_of . shape ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( parents_of [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> child_count = m . get_current_measurement ( PARENT_OBJECTS , <EOL> "<STR_LIT>" % <EOL> CHILD_OBJECTS ) <EOL> self . assertEqual ( np . product ( child_count . shape ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( child_count [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> self . features_and_columns_match ( workspace ) <EOL> def test_02_03_relate_ijv ( self ) : <EOL> '''<STR_LIT>''' <EOL> child_ijv = np . array ( [ [ <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:1> ] , [ <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:2> ] , [ <NUM_LIT:20> , <NUM_LIT:15> , <NUM_LIT:3> ] ] ) <EOL> parent_ijv = np . array ( [ [ <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:1> ] , [ <NUM_LIT:20> , <NUM_LIT:15> , <NUM_LIT:2> ] ] ) <EOL> workspace , module = self . make_workspace ( parent_ijv , child_ijv ) <EOL> module . wants_per_parent_means . value = False <EOL> module . run ( workspace ) <EOL> m = workspace . measurements <EOL> parents_of = m . get_current_measurement ( CHILD_OBJECTS , <EOL> "<STR_LIT>" % PARENT_OBJECTS ) <EOL> self . assertEqual ( np . product ( parents_of . shape ) , <NUM_LIT:3> ) <EOL> self . assertTrue ( parents_of [ <NUM_LIT:0> ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( parents_of [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( parents_of [ <NUM_LIT:2> ] , <NUM_LIT:2> ) <EOL> child_count = m . get_current_measurement ( PARENT_OBJECTS , <EOL> "<STR_LIT>" % <EOL> CHILD_OBJECTS ) <EOL> self . assertEqual ( np . product ( child_count . shape ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( child_count [ <NUM_LIT:0> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( child_count [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> def test_03_01_mean ( self ) : <EOL> '''<STR_LIT>''' <EOL> i , j = np . mgrid [ <NUM_LIT:0> : <NUM_LIT:20> , <NUM_LIT:0> : <NUM_LIT:20> ] <EOL> parent_labels = ( i / <NUM_LIT:10> + <NUM_LIT:1> ) . astype ( int ) <EOL> child_labels = ( i / <NUM_LIT:10> ) . astype ( int ) + ( j / <NUM_LIT:10> ) . astype ( int ) * <NUM_LIT:2> + <NUM_LIT:1> <EOL> workspace , module = self . make_workspace ( parent_labels , child_labels , <EOL> fake_measurement = True ) <EOL> module . wants_per_parent_means . value = True <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> m . add_measurement ( CHILD_OBJECTS , MEASUREMENT , <EOL> np . array ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> m . add_measurement ( CHILD_OBJECTS , IGNORED_MEASUREMENT , <EOL> np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) ) <EOL> expected = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> module . run ( workspace ) <EOL> name = "<STR_LIT>" % ( CHILD_OBJECTS , MEASUREMENT ) <EOL> self . assertTrue ( name in m . get_feature_names ( PARENT_OBJECTS ) ) <EOL> data = m . get_current_measurement ( PARENT_OBJECTS , name ) <EOL> self . assertTrue ( np . all ( data == expected ) ) <EOL> self . features_and_columns_match ( workspace ) <EOL> name = "<STR_LIT>" % ( CHILD_OBJECTS , IGNORED_MEASUREMENT ) <EOL> self . assertFalse ( name in m . get_feature_names ( PARENT_OBJECTS ) ) <EOL> def test_03_02_empty_mean ( self ) : <EOL> i , j = np . mgrid [ <NUM_LIT:0> : <NUM_LIT:20> , <NUM_LIT:0> : <NUM_LIT:20> ] <EOL> parent_labels = ( i / <NUM_LIT:10> + <NUM_LIT:1> ) . astype ( int ) <EOL> child_labels = np . zeros ( parent_labels . shape , int ) <EOL> workspace , module = self . make_workspace ( parent_labels , child_labels , <EOL> fake_measurement = True ) <EOL> module . wants_per_parent_means . value = True <EOL> m = workspace . measurements <EOL> self . assertTrue ( isinstance ( m , cpmeas . Measurements ) ) <EOL> m . add_measurement ( CHILD_OBJECTS , MEASUREMENT , np . zeros ( <NUM_LIT:0> ) ) <EOL> m . add_measurement ( CHILD_OBJECTS , IGNORED_MEASUREMENT , np . zeros ( <NUM_LIT:0> , int ) ) <EOL> module . run ( workspace ) <EOL> name = "<STR_LIT>" % ( CHILD_OBJECTS , MEASUREMENT ) <EOL> self . assertTrue ( name in m . get_feature_names ( PARENT_OBJECTS ) ) <EOL> data = m . get_current_measurement ( PARENT_OBJECTS , name ) <EOL> self . assertTrue ( np . all ( np . isnan ( data ) ) ) <EOL> self . features_and_columns_match ( workspace ) <EOL> name = "<STR_LIT>" % ( CHILD_OBJECTS , IGNORED_MEASUREMENT ) <EOL> self . assertFalse ( name in m . get_feature_names ( PARENT_OBJECTS ) ) <EOL> def test_04_00_distance_empty ( self ) : <EOL> '''<STR_LIT>''' <EOL> empty_labels = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:20> ) , int ) <EOL> some_labels = np . zeros ( ( <NUM_LIT:10> , <NUM_LIT:20> ) , int ) <EOL> some_labels [ <NUM_LIT:2> : <NUM_LIT:7> , <NUM_LIT:3> : <NUM_LIT:8> ] = <NUM_LIT:1> <EOL> some_labels [ <NUM_LIT:3> : <NUM_LIT:8> , <NUM_LIT:12> : <NUM_LIT> ] = <NUM_LIT:2> <EOL> for parent_labels , child_labels , n in ( ( empty_labels , empty_labels , <NUM_LIT:0> ) , <EOL> ( some_labels , empty_labels , <NUM_LIT:0> ) , <EOL> ( empty_labels , some_labels , <NUM_LIT:2> ) ) : <EOL> workspace , module = self . make_workspace ( parent_labels , child_labels ) <EOL> self . assertTrue ( isinstance ( module , R . Relate ) ) <EOL> module . find_parent_child_distances . value = R . D_BOTH <EOL> module . run ( workspace ) <EOL> self . features_and_columns_match ( workspace ) <EOL> meas = workspace . measurements <EOL> for feature in ( R . FF_CENTROID , R . FF_MINIMUM ) : <EOL> m = feature % PARENT_OBJECTS <EOL> v = meas . get_current_measurement ( CHILD_OBJECTS , m ) <EOL> self . assertEqual ( len ( v ) , n ) <EOL> if n > <NUM_LIT:0> : <EOL> self . assertTrue ( np . all ( np . isnan ( v ) ) ) <EOL> def test_04_01_distance_centroids ( self ) : <EOL> '''<STR_LIT>''' <EOL> i , j = np . mgrid [ <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:0> : <NUM_LIT:30> ] <EOL> parent_labels = ( i >= <NUM_LIT:7> ) * <NUM_LIT:1> + ( j >= <NUM_LIT:15> ) * <NUM_LIT:2> + <NUM_LIT:1> <EOL> parent_centers = np . array ( [ [ <NUM_LIT:3> , <NUM_LIT:7> ] , [ <NUM_LIT:10> , <NUM_LIT:7> ] , [ <NUM_LIT:3> , <NUM_LIT> ] , [ <NUM_LIT:10> , <NUM_LIT> ] ] , float ) <EOL> child_labels = np . zeros ( i . shape ) <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> child_centers = np . random . permutation ( np . prod ( i . shape ) ) [ : <NUM_LIT:12> ] <EOL> child_centers = np . vstack ( ( i . flatten ( ) [ child_centers ] , <EOL> j . flatten ( ) [ child_centers ] ) ) <EOL> child_labels [ child_centers [ <NUM_LIT:0> ] , child_centers [ <NUM_LIT:1> ] ] = np . arange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> parent_indexes = parent_labels [ child_centers [ <NUM_LIT:0> ] , <EOL> child_centers [ <NUM_LIT:1> ] ] - <NUM_LIT:1> <EOL> expected = np . sqrt ( np . sum ( ( parent_centers [ parent_indexes , : ] - <EOL> child_centers . transpose ( ) ) ** <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> workspace , module = self . make_workspace ( parent_labels , child_labels ) <EOL> self . assertTrue ( isinstance ( module , R . Relate ) ) <EOL> module . find_parent_child_distances . value = R . D_CENTROID <EOL> module . run ( workspace ) <EOL> self . features_and_columns_match ( workspace ) <EOL> meas = workspace . measurements <EOL> v = meas . get_current_measurement ( CHILD_OBJECTS , <EOL> R . FF_CENTROID % PARENT_OBJECTS ) <EOL> self . assertEqual ( v . shape [ <NUM_LIT:0> ] , <NUM_LIT:12> ) <EOL> self . assertTrue ( np . all ( np . abs ( v - expected ) < <NUM_LIT> ) ) <EOL> def test_04_02_distance_minima ( self ) : <EOL> '''<STR_LIT>''' <EOL> i , j = np . mgrid [ <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:0> : <NUM_LIT:30> ] <EOL> parent_labels = ( i >= <NUM_LIT:6> ) * <NUM_LIT:1> + ( j >= <NUM_LIT> ) * <NUM_LIT:2> + <NUM_LIT:1> <EOL> child_labels = np . zeros ( i . shape ) <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> child_centers = np . random . permutation ( np . prod ( i . shape ) ) [ : <NUM_LIT:12> ] <EOL> child_centers = np . vstack ( ( i . flatten ( ) [ child_centers ] , <EOL> j . flatten ( ) [ child_centers ] ) ) <EOL> child_labels [ child_centers [ <NUM_LIT:0> ] , child_centers [ <NUM_LIT:1> ] ] = np . arange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> background = ( ( i != <NUM_LIT:0> ) & ( i != <NUM_LIT:5> ) & ( i != <NUM_LIT:6> ) & ( i != <NUM_LIT> ) & <EOL> ( j != <NUM_LIT:0> ) & ( j != <NUM_LIT> ) & ( j != <NUM_LIT> ) & ( j != <NUM_LIT> ) ) <EOL> d = distance_transform_edt ( background ) <EOL> expected = d [ child_centers [ <NUM_LIT:0> ] , child_centers [ <NUM_LIT:1> ] ] <EOL> workspace , module = self . make_workspace ( parent_labels , child_labels ) <EOL> self . assertTrue ( isinstance ( module , R . Relate ) ) <EOL> module . find_parent_child_distances . value = R . D_MINIMUM <EOL> module . run ( workspace ) <EOL> self . features_and_columns_match ( workspace ) <EOL> meas = workspace . measurements <EOL> v = meas . get_current_measurement ( CHILD_OBJECTS , <EOL> R . FF_MINIMUM % PARENT_OBJECTS ) <EOL> self . assertEqual ( v . shape [ <NUM_LIT:0> ] , <NUM_LIT:12> ) <EOL> self . assertTrue ( np . all ( np . abs ( v - expected ) < <NUM_LIT> ) ) <EOL> def test_04_03_means_of_distances ( self ) : <EOL> i , j = np . mgrid [ <NUM_LIT:0> : <NUM_LIT> , <NUM_LIT:0> : <NUM_LIT:30> ] <EOL> parent_labels = ( i >= <NUM_LIT:7> ) * <NUM_LIT:1> + ( j >= <NUM_LIT:15> ) * <NUM_LIT:2> + <NUM_LIT:1> <EOL> child_labels = np . zeros ( i . shape ) <EOL> np . random . seed ( <NUM_LIT:0> ) <EOL> child_centers = np . random . permutation ( np . prod ( i . shape ) ) [ : <NUM_LIT:12> ] <EOL> child_centers = np . vstack ( ( i . flatten ( ) [ child_centers ] , <EOL> j . flatten ( ) [ child_centers ] ) ) <EOL> child_labels [ child_centers [ <NUM_LIT:0> ] , child_centers [ <NUM_LIT:1> ] ] = np . arange ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> parent_centers = np . array ( [ [ <NUM_LIT:3> , <NUM_LIT:7> ] , [ <NUM_LIT:10> , <NUM_LIT:7> ] , [ <NUM_LIT:3> , <NUM_LIT> ] , [ <NUM_LIT:10> , <NUM_LIT> ] ] , float ) <EOL> parent_indexes = parent_labels [ child_centers [ <NUM_LIT:0> ] , <EOL> child_centers [ <NUM_LIT:1> ] ] - <NUM_LIT:1> <EOL> expected = np . sqrt ( np . sum ( ( parent_centers [ parent_indexes , : ] - <EOL> child_centers . transpose ( ) ) ** <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> workspace , module = self . make_workspace ( parent_labels , child_labels ) <EOL> self . assertTrue ( isinstance ( module , R . Relate ) ) <EOL> module . find_parent_child_distances . value = R . D_CENTROID <EOL> module . wants_per_parent_means . value = True <EOL> mnames = module . get_measurements ( workspace . pipeline , <EOL> PARENT_OBJECTS , <EOL> "<STR_LIT:_>" . join ( ( R . C_MEAN , CHILD_OBJECTS ) ) ) <EOL> self . assertTrue ( R . FF_CENTROID % PARENT_OBJECTS in mnames ) <EOL> feat_mean = R . FF_MEAN % ( CHILD_OBJECTS , R . FF_CENTROID % PARENT_OBJECTS ) <EOL> mcolumns = module . get_measurement_columns ( workspace . pipeline ) <EOL> self . assertTrue ( any ( [ c [ <NUM_LIT:0> ] == PARENT_OBJECTS and c [ <NUM_LIT:1> ] == feat_mean <EOL> for c in mcolumns ] ) ) <EOL> m = workspace . measurements <EOL> m [ CHILD_OBJECTS , R . M_LOCATION_CENTER_X , <NUM_LIT:1> ] = child_centers [ <NUM_LIT:1> ] <EOL> m [ CHILD_OBJECTS , R . M_LOCATION_CENTER_Y , <NUM_LIT:1> ] = child_centers [ <NUM_LIT:0> ] <EOL> module . run ( workspace ) <EOL> v = m [ PARENT_OBJECTS , feat_mean , <NUM_LIT:1> ] <EOL> plabel = m [ CHILD_OBJECTS , "<STR_LIT:_>" . join ( ( R . C_PARENT , PARENT_OBJECTS ) ) , <NUM_LIT:1> ] <EOL> self . assertEqual ( len ( v ) , <NUM_LIT:4> ) <EOL> for idx in range ( <NUM_LIT:4> ) : <EOL> if np . any ( plabel == idx + <NUM_LIT:1> ) : <EOL> self . assertAlmostEqual ( <EOL> v [ idx ] , np . mean ( expected [ plabel == idx + <NUM_LIT:1> ] ) , <NUM_LIT:4> ) </s>
