2929]
3030
3131
32- def  get_length_fraction_from_center (table , center_str ):
32+ def  get_length_fraction_from_center (table , center_str ,  halo_size = 20 ):
3333    """Get 'length_fraction' parameter for center coordinate by averaging nearby segmentation instances. 
3434    """ 
3535    center_coord  =  tuple ([int (c ) for  c  in  center_str .split ("-" )])
3636    (cx , cy , cz ) =  center_coord 
37-     offset  =  20 
3837    subset  =  table [
39-         (cx  -  offset  <  table ["anchor_x" ]) & 
40-         (table ["anchor_x" ] <  cx  +  offset ) & 
41-         (cy  -  offset  <  table ["anchor_y" ]) & 
42-         (table ["anchor_y" ] <  cy  +  offset ) & 
43-         (cz  -  offset  <  table ["anchor_z" ]) & 
44-         (table ["anchor_z" ] <  cz  +  offset )
38+         (cx  -  halo_size  <  table ["anchor_x" ]) & 
39+         (table ["anchor_x" ] <  cx  +  halo_size ) & 
40+         (cy  -  halo_size  <  table ["anchor_y" ]) & 
41+         (table ["anchor_y" ] <  cy  +  halo_size ) & 
42+         (cz  -  halo_size  <  table ["anchor_z" ]) & 
43+         (table ["anchor_z" ] <  cz  +  halo_size )
4544    ]
4645    length_fraction  =  list (subset ["length_fraction" ])
4746    length_fraction  =  float (sum (length_fraction ) /  len (length_fraction ))
4847    return  length_fraction 
4948
5049
51- def  apply_nearest_threshold (intensity_dic , table_seg , table_measurement ):
50+ def  apply_nearest_threshold (intensity_dic , table_seg , table_measurement ,  halo_size = 20 ):
5251    """Apply threshold to nearest segmentation instances. 
5352    Crop centers are transformed into the "length fraction" parameter of the segmentation table. 
5453    This avoids issues with the spiral shape of the cochlea and maps the assignment onto the Rosenthal"s canal. 
5554    """ 
5655    # assign crop centers to length fraction of Rosenthal"s canal 
5756    lf_intensity  =  {}
5857    for  key  in  intensity_dic .keys ():
59-         length_fraction  =  get_length_fraction_from_center (table_seg , key )
58+         length_fraction  =  get_length_fraction_from_center (table_seg , key ,  halo_size = halo_size )
6059        intensity_dic [key ]["length_fraction" ] =  length_fraction 
6160        lf_intensity [length_fraction ] =  {"threshold" : intensity_dic [key ]["median_intensity" ]}
6261
@@ -94,13 +93,14 @@ def apply_nearest_threshold(intensity_dic, table_seg, table_measurement):
9493    return  table_seg 
9594
9695
97- def  find_thresholds (cochlea_annotations , cochlea , data_seg , table_measurement ):
96+ def  find_thresholds (cochlea_annotations , cochlea , data_seg , table_measurement ,  resolution = 0.38 ):
9897    # Find the median intensities by averaging the individual annotations for specific crops 
9998    annotation_dics  =  {}
10099    annotated_centers  =  []
101100    for  annotation_dir  in  cochlea_annotations :
102101        print (f"Localizing threshold with median intensities for { os .path .basename (annotation_dir )}  ." )
103-         annotation_dic  =  localize_median_intensities (annotation_dir , cochlea , data_seg , table_measurement )
102+         annotation_dic  =  localize_median_intensities (annotation_dir , cochlea , data_seg , table_measurement ,
103+                                                      resolution = resolution )
104104        annotated_centers .extend (annotation_dic ["center_strings" ])
105105        annotation_dics [annotation_dir ] =  annotation_dic 
106106
@@ -168,6 +168,13 @@ def evaluate_marker_annotation(
168168    """ 
169169    input_key  =  "s0" 
170170
171+     if  marker_name  ==  "rbOtof" :
172+         halo_size  =  150 
173+         resolution  =  [1.887779 , 1.887779 , 3.0 ]
174+     else :
175+         halo_size  =  20 
176+         resolution  =  0.38 
177+ 
171178    if  annotation_dirs  is  None :
172179        if  "MARKER_DIR"  in  globals ():
173180            marker_dir  =  MARKER_DIR 
@@ -199,16 +206,17 @@ def evaluate_marker_annotation(
199206        with  fs .open (table_path_s3 , "r" ) as  f :
200207            table_measurement  =  pd .read_csv (f , sep = "\t " )
201208
202-         # Find the threholds from the annotated blocks and save it if specified. 
203-         intensity_dic  =  find_thresholds (cochlea_annotations , cochlea , data_seg , table_measurement )
209+         # Find the thresholds from the annotated blocks and save it if specified. 
210+         intensity_dic  =  find_thresholds (cochlea_annotations , cochlea , data_seg , table_measurement ,
211+                                         resolution = resolution )
204212        if  threshold_save_dir  is  not   None :
205213            os .makedirs (threshold_save_dir , exist_ok = True )
206214            threshold_out_path  =  os .path .join (threshold_save_dir , f"{ cochlea_str }  _{ marker_name }  _{ seg_string }  .json" )
207215            with  open (threshold_out_path , "w" ) as  f :
208216                json .dump (intensity_dic , f , sort_keys = True , indent = 4 )
209217
210218        # Apply the threshold to all SGNs. 
211-         table_seg  =  apply_nearest_threshold (intensity_dic , table_seg , table_measurement )
219+         table_seg  =  apply_nearest_threshold (intensity_dic , table_seg , table_measurement ,  halo_size = halo_size )
212220
213221        # Save the table with positives / negatives for all SGNs. 
214222        os .makedirs (output_dir , exist_ok = True )
@@ -224,12 +232,15 @@ def main():
224232    parser .add_argument ("-o" , "--output" , type = str , required = True , help = "Output directory." )
225233    parser .add_argument ("-a" , "--annotation_dirs" , type = str , nargs = "+" , default = None ,
226234                        help = "Directories containing marker annotations." )
227-     parser .add_argument ("--threshold_save_dir" , "-t" )
235+     parser .add_argument ("-t" , "--threshold_save_dir" )
236+     parser .add_argument ("-s" , "--seg_name" , type = str , default = "SGN_v2" )
237+     parser .add_argument ("-m" , "--marker_name" , type = str , default = "GFP" )
228238    parser .add_argument ("-f" , "--force" , action = "store_true" )
229239
230240    args  =  parser .parse_args ()
231241    evaluate_marker_annotation (
232-         args .cochlea , args .output , args .annotation_dirs , threshold_save_dir = args .threshold_save_dir , force = args .force 
242+         args .cochlea , args .output , args .annotation_dirs , threshold_save_dir = args .threshold_save_dir ,
243+         seg_name = args .seg_name , marker_name = args .marker_name , force = args .force 
233244    )
234245
235246
0 commit comments