88from flamingo_tools .s3_utils import get_s3_path
99from flamingo_tools .file_utils import read_image_data
1010from flamingo_tools .segmentation .chreef_utils import localize_median_intensities , find_annotations
11- from flamingo_tools .segmentation .sgn_subtype_utils import CUSTOM_THRESHOLDS
11+ from flamingo_tools .segmentation .sgn_subtype_utils import CUSTOM_THRESHOLDS , COCHLEAE
1212
1313MARKER_DIR_SUBTYPE = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/SGN_subtypes"
14- # The cochlea for the CHReef analysis.
15-
16- COCHLEAE = {
17- "M_LR_000098_L" : {"seg_data" : "SGN_v2" , "subtype" : ["CR" , "Ntng1" ], "intensity" : "ratio" },
18- "M_LR_000099_L" : {"seg_data" : "PV_SGN_v2" , "subtype" : ["Calb1" , "Lypd1" ], "intensity" : "ratio" },
19- "M_LR_000184_L" : {"seg_data" : "SGN_v2" , "subtype" : ["Prph" ], "output_seg" : "SGN_v2b" , "intensity" : "ratio" },
20- "M_LR_000184_R" : {"seg_data" : "SGN_v2" , "subtype" : ["Prph" ], "output_seg" : "SGN_v2b" , "intensity" : "ratio" },
21- "M_LR_000214_L" : {"seg_data" : "PV_SGN_v2" , "subtype" : ["Calb1" ], "intensity" : "ratio" },
22- "M_LR_000260_L" : {"seg_data" : "SGN_v2" , "subtype" : ["Prph" , "Tuj1" ], "intensity" : "ratio" },
23- "M_LR_N110_L" : {"seg_data" : "SGN_v2" , "subtype" : ["Calb1" , "Ntng1" ], "intensity" : "ratio" },
24- "M_LR_N110_R" : {"seg_data" : "SGN_v2" , "subtype" : ["Calb1" , "Ntng1" ], "intensity" : "ratio" },
25- "M_LR_N152_L" : {"seg_data" : "SGN_v2" , "subtype" : ["CR" , "Ntng1" ], "intensity" : "ratio" },
26- "M_AMD_N180_L" : {"seg_data" : "SGN_merged" , "subtype" : ["CR" , "Lypd1" , "Ntng1" ], "intensity" : "absolute" },
27- "M_AMD_N180_R" : {"seg_data" : "SGN_merged" , "subtype" : ["CR" , "Ntng1" ], "intensity" : "absolute" },
28- }
2914
3015
3116def get_length_fraction_from_center (table , center_str ):
@@ -160,7 +145,7 @@ def get_annotation_table(annotation_dics, subtype):
160145 annotator = annotator_dir .split ("_" )[1 ]
161146 for center_str in annotation_dic ["center_strings" ]:
162147 row = {"annotator" : annotator }
163- row ["subtype " ] = subtype
148+ row ["subtype_stains " ] = subtype
164149 row ["center_str" ] = center_str
165150 row ["median_intensity" ] = annotation_dic [center_str ]["median_intensity" ]
166151 row ["inbetween_ids" ] = len (annotation_dic [center_str ]["inbetween_ids" ])
@@ -179,13 +164,13 @@ def get_annotation_table(annotation_dics, subtype):
179164 return df
180165
181166
182- def get_object_measures (annotation_dics , intensity_dic , intensity_mode , subtype ):
167+ def get_object_measures (annotation_dics , intensity_dic , intensity_mode , subtype_stain ):
183168 """Get information to create table containing object measure information.
184169 """
185170 om_dic = {}
186171 center_strings = list (intensity_dic .keys ())
187172 om_dic ["center_strings" ] = center_strings
188- om_dic ["subtype " ] = subtype
173+ om_dic ["subtype_stains " ] = subtype_stain
189174 om_dic ["intensity_mode" ] = intensity_mode
190175 for center_str in center_strings :
191176 crop_dic = {}
@@ -234,8 +219,8 @@ def evaluate_marker_annotation(
234219
235220 seg_string = "-" .join (output_seg .split ("_" ))
236221 cochlea_str = "-" .join (cochlea .split ("_" ))
237- subtypes = COCHLEAE [cochlea ]["subtype " ]
238- subtype_str = "_" .join (subtypes )
222+ stains = COCHLEAE [cochlea ]["subtype_stain " ]
223+ subtype_str = "_" .join (stains )
239224 out_path = os .path .join (output_dir , f"{ cochlea_str } _{ subtype_str } _{ seg_string } .tsv" )
240225 annot_out = os .path .join (output_dir , f"{ cochlea_str } _{ subtype_str } _{ seg_string } _annotations.tsv" )
241226 if os .path .exists (out_path ) and os .path .exists (annot_out ) and not force :
@@ -256,13 +241,12 @@ def evaluate_marker_annotation(
256241
257242 # iterate through subtypes
258243 annot_table = None
259- for subtype in subtypes :
260- pattern = subtype
244+ for stain in stains :
261245 if intensity_mode == "ratio" :
262246 table_measurement_path = f"{ cochlea } /tables/{ data_name } /subtype_ratio.tsv"
263- column = f"{ subtype } _ratio_PV"
247+ column = f"{ stain } _ratio_PV"
264248 elif intensity_mode == "absolute" :
265- table_measurement_path = f"{ cochlea } /tables/{ data_name } /{ subtype } _{ seg_string } _object-measures.tsv"
249+ table_measurement_path = f"{ cochlea } /tables/{ data_name } /{ stain } _{ seg_string } _object-measures.tsv"
266250 column = "median"
267251 else :
268252 raise ValueError ("Choose either 'ratio' or 'median' as intensity mode." )
@@ -272,27 +256,27 @@ def evaluate_marker_annotation(
272256 table_measurement = pd .read_csv (f , sep = "\t " )
273257
274258 cochlea_annotations = [a for a in annotation_dirs
275- if len (find_annotations (a , cochlea , subtype )["center_strings" ]) != 0 ]
259+ if len (find_annotations (a , cochlea , stain )["center_strings" ]) != 0 ]
276260 print (f"Evaluating data for cochlea { cochlea } in { cochlea_annotations } ." )
277261
278262 # Find the thresholds from the annotated blocks and save them if specified.
279263 intensity_dic , annot_dic = find_thresholds (cochlea_annotations , cochlea , data_seg ,
280- table_measurement , column = column , pattern = pattern )
264+ table_measurement , column = column , pattern = stain )
281265
282266 if annot_table is None :
283- annot_table = get_annotation_table (annot_dic , subtype )
267+ annot_table = get_annotation_table (annot_dic , stain )
284268 else :
285- annot_table = pd .concat ([annot_table , get_annotation_table (annot_dic , subtype )], ignore_index = True )
269+ annot_table = pd .concat ([annot_table , get_annotation_table (annot_dic , stain )], ignore_index = True )
286270
287271 # create dictionary containing median intensity and segmentation ids for every crop
288- om_dic = get_object_measures (annot_dic , intensity_dic , intensity_mode , subtype )
289- om_out_path = os .path .join (output_dir , f"{ cochlea_str } _{ subtype } _om.json" )
272+ om_dic = get_object_measures (annot_dic , intensity_dic , intensity_mode , stain )
273+ om_out_path = os .path .join (output_dir , f"{ cochlea_str } _{ stain } _om.json" )
290274 with open (om_out_path , "w" ) as f :
291275 json .dump (om_dic , f , sort_keys = True , indent = 4 )
292276
293277 if threshold_save_dir is not None :
294278 os .makedirs (threshold_save_dir , exist_ok = True )
295- threshold_out_path = os .path .join (threshold_save_dir , f"{ cochlea_str } _{ subtype } _{ seg_string } .json" )
279+ threshold_out_path = os .path .join (threshold_save_dir , f"{ cochlea_str } _{ stain } _{ seg_string } .json" )
296280 with open (threshold_out_path , "w" ) as f :
297281 json .dump (intensity_dic , f , sort_keys = True , indent = 4 )
298282
@@ -305,13 +289,13 @@ def evaluate_marker_annotation(
305289 table_measurement = pd .read_csv (f , sep = "\t " )
306290
307291 # Apply the threshold to all SGNs.
308- if CUSTOM_THRESHOLDS .get (cochlea , {}).get (subtype ) is not None :
309- custom_threshold_dic = CUSTOM_THRESHOLDS [cochlea ][subtype ]
292+ if CUSTOM_THRESHOLDS .get (cochlea , {}).get (stain ) is not None :
293+ custom_threshold_dic = CUSTOM_THRESHOLDS [cochlea ][stain ]
310294 else :
311295 custom_threshold_dic = None
312296
313297 table_seg = apply_nearest_threshold (
314- intensity_dic , table_seg , table_measurement , column = column , suffix = subtype ,
298+ intensity_dic , table_seg , table_measurement , column = column , suffix = stain ,
315299 threshold_dic = custom_threshold_dic ,
316300 )
317301
0 commit comments