11import os
2+ import math
23import multiprocessing as mp
34from concurrent import futures
45from typing import List , Tuple
89from tqdm import tqdm
910
1011
11- def find_annotations (annotation_dir ) -> dict :
12+ def find_annotations (annotation_dir , cochleae = None ) -> dict :
1213 """Create dictionary for analysis of ChReef annotations.
1314 Annotations should have format positive-negative_<cochlea>_crop_<coord>_allNegativeExcluded_thr<thr>.tif
1415
@@ -30,16 +31,18 @@ def extract_cochlea_str(name):
3031 return cochlea
3132
3233 file_names = [entry .name for entry in os .scandir (annotation_dir )]
33- cochleae = list (set ([extract_cochlea_str (file_name ) for file_name in file_names ]))
34+ if cochleae is None :
35+ cochleae = list (set ([extract_cochlea_str (file_name ) for file_name in file_names ]))
36+
3437 annotation_dic = {}
3538 for cochlea in cochleae :
3639 cochlea_files = [entry .name for entry in os .scandir (annotation_dir ) if cochlea in entry .name ]
3740 dic = {"cochlea" : cochlea }
3841 dic ["cochlea_files" ] = cochlea_files
3942 center_crops = list (set ([extract_center_crop (cochlea , name = file_name ) for file_name in cochlea_files ]))
4043 dic ["center_coords" ] = center_crops
41- dic ["center_coords_str " ] = [("-" ).join ([str (c ).zfill (4 ) for center_crop in center_crops for c in center_crop ])]
42- for center_str in dic ["center_coords_str " ]:
44+ dic ["center_str " ] = [("-" ).join ([str (c ).zfill (4 ) for center_crop in center_crops for c in center_crop ])]
45+ for center_str in dic ["center_str " ]:
4346 file_neg = [c for c in cochlea_files if all (x in c for x in [cochlea , center_str , "NegativeExcluded" ])][0 ]
4447 file_pos = [c for c in cochlea_files if all (x in c for x in [cochlea , center_str , "WeakPositive" ])][0 ]
4548 dic [center_str ] = {"file_neg" : file_neg , "file_pos" : file_pos }
@@ -141,3 +144,24 @@ def get_median_intensity(file_negexc, file_allweak, center, data_seg, table):
141144 inbetween_ids = find_inbetween_ids (arr_negexc , arr_allweak , roi_seg )
142145 intensities = table .loc [table ["label_id" ].isin (inbetween_ids ), table ["mean" ]]
143146 return np .median (list (intensities ))
147+
148+
149+ def localize_median_intensities (annotation_dir , cochlea , data_seg , table_measure , table_block = None ):
150+ annotation_dic = find_annotations (annotation_dir , cochleae = [cochlea ])
151+ for key in annotation_dic .keys ():
152+ dic = annotation_dic [key ]
153+ for center_coord , center_str in zip (dic ["center_coords" ], dic ["center_str" ]):
154+ file_pos = dic [center_str ["file_pos" ]]
155+ file_neg = dic [center_str ["file_neg" ]]
156+ median_intensity = get_median_intensity (file_neg , file_pos , center_coord , data_seg , table_measure )
157+
158+ annotation_dic [key ][center_str ]["median_intensity" ] = median_intensity
159+ if table_block is not None :
160+ block_centers = table_block ["crop_centers" ]
161+ for num , block_center in enumerate (block_centers ):
162+ dist = math .dist (tuple (block_centers ), center_coord )
163+ if dist < 5 :
164+ annotation_dic [key ][center_str ]["block_index" ] = num
165+ annotation_dic [key ][center_str ]["block_center" ] = block_center
166+
167+ return annotation_dic [cochlea ]
0 commit comments