66
77from flamingo_tools .s3_utils import BUCKET_NAME , create_s3_target
88
9+ OUTPUT_FOLDER = "./ihc_counts"
910
10- def check_project ():
11+
12+ def check_project (plot = False , save_ihc_table = False ):
1113 s3 = create_s3_target ()
1214 cochleae = ['M_LR_000226_L' , 'M_LR_000226_R' , 'M_LR_000227_L' , 'M_LR_000227_R' ]
1315
16+ results = {}
1417 for cochlea in cochleae :
1518 content = s3 .open (f"{ BUCKET_NAME } /{ cochlea } /dataset.json" , mode = "r" , encoding = "utf-8" )
1619 info = json .loads (content .read ())
@@ -21,6 +24,7 @@ def check_project():
2124 rel_path = syn ["tableData" ]["tsv" ]["relativePath" ]
2225 table_content = s3 .open (os .path .join (BUCKET_NAME , cochlea , rel_path , "default.tsv" ), mode = "rb" )
2326 syn_table = pd .read_csv (table_content , sep = "\t " )
27+ max_dist = syn_table .distance_to_ihc .max ()
2428
2529 # Load the corresponding ihc table.
2630 ihc = sources ["IHC_v2" ]["segmentation" ]
@@ -30,22 +34,53 @@ def check_project():
3034
3135 # Keep only the synapses that were matched to a valid IHC.
3236 component_id = 2 if cochlea == "M_LR_000226_R" else 1
33- valid_ihcs = ihc_table .label_id [ihc_table .component_labels == component_id ].values
37+ valid_ihcs = ihc_table .label_id [ihc_table .component_labels == component_id ].values . astype ( "int" )
3438
3539 valid_syn_table = syn_table [syn_table .matched_ihc .isin (valid_ihcs )]
3640 n_synapses = len (valid_syn_table )
3741
38- _ , syn_per_ihc = np .unique (valid_syn_table .matched_ihc .values , return_counts = True )
42+ ihc_ids , syn_per_ihc = np .unique (valid_syn_table .matched_ihc .values , return_counts = True )
43+ ihc_ids = ihc_ids .astype ("int" )
44+ results [cochlea ] = syn_per_ihc
3945
4046 print ("Cochlea:" , cochlea )
4147 print ("N-Synapses:" , n_synapses )
4248 print ("Average Syn per IHC:" , np .mean (syn_per_ihc ))
4349 print ("STDEV Syn per IHC:" , np .std (syn_per_ihc ))
50+ print ("@ max dist:" , max_dist )
4451 print ()
4552
53+ if save_ihc_table :
54+ ihc_to_count = {ihc_id : count for ihc_id , count in zip (ihc_ids , syn_per_ihc )}
55+ unmatched_ihcs = np .setdiff1d (valid_ihcs , ihc_ids )
56+ ihc_to_count .update ({ihc_id : 0 for ihc_id in unmatched_ihcs })
57+ ihc_count_table = pd .DataFrame ({
58+ "label_id" : list (ihc_to_count .keys ()),
59+ "synapse_count" : list (ihc_to_count .values ()),
60+ })
61+ os .makedirs (OUTPUT_FOLDER , exist_ok = True )
62+ output_path = os .path .join (OUTPUT_FOLDER , f"ihc_count_{ cochlea } .tsv" )
63+ ihc_count_table .to_csv (output_path , sep = "\t " , index = False )
64+
65+ if plot :
66+ import matplotlib .pyplot as plt
67+ import seaborn as sns
68+
69+ cap = 30
70+
71+ fig , axes = plt .subplots (1 , 4 , figsize = (16 , 4 ), sharex = True , sharey = True )
72+ for i , (cochlea , res ) in enumerate (results .items ()):
73+ sns .histplot (data = np .clip (res , 0 , cap ), bins = 16 , ax = axes [i ])
74+ axes [i ].set_title (cochlea )
75+
76+ fig .suptitle (f"Ribbon Synapses per IHC @ { np .round (max_dist )} micron" )
77+
78+ plt .tight_layout ()
79+ plt .show ()
80+
4681
4782def main ():
48- check_project ()
83+ check_project (plot = False , save_ihc_table = True )
4984
5085
5186if __name__ == "__main__" :
0 commit comments