11import argparse
2+ import json
23import os
4+ import pickle
5+
36import imageio .v3 as imageio
47from glob import glob
58from pathlib import Path
1013import pandas as pd
1114from matplotlib import cm , colors
1215
16+ from flamingo_tools .s3_utils import BUCKET_NAME , create_s3_target
1317from util import sliding_runlength_sum , frequency_mapping , SYNAPSE_DIR_ROOT , to_alias
1418
1519# INPUT_ROOT = "/home/pape/Work/my_projects/flamingo-tools/scripts/M_LR_000227_R/scale3"
2327 "Type-II" : "Prph" ,
2428}
2529
30+ FILE_EXTENSION = "png"
31+
2632png_dpi = 300
2733
34+ # The cochlea for the CHReef analysis.
35+ COCHLEAE_DICT = {
36+ "M_LR_000226_L" : {"alias" : "M01L" , "component" : [1 ]},
37+ "M_LR_000226_R" : {"alias" : "M01R" , "component" : [1 ]},
38+ "M_LR_000227_L" : {"alias" : "M02L" , "component" : [1 ]},
39+ "M_LR_000227_R" : {"alias" : "M02R" , "component" : [1 ]},
40+ }
41+
42+
43+ def get_tonotopic_data ():
44+ s3 = create_s3_target ()
45+ source_name = "IHC_v4c"
46+ ihc_version = source_name .split ("_" )[1 ]
47+ cache_path = "./tonotopic_data.pkl"
48+ cochleae = [key for key in COCHLEAE_DICT .keys ()]
49+
50+ if os .path .exists (cache_path ):
51+ with open (cache_path , "rb" ) as f :
52+ return pickle .load (f )
53+
54+ chreef_data = {}
55+ for cochlea in cochleae :
56+ print ("Processsing cochlea:" , cochlea )
57+ content = s3 .open (f"{ BUCKET_NAME } /{ cochlea } /dataset.json" , mode = "r" , encoding = "utf-8" )
58+ info = json .loads (content .read ())
59+ sources = info ["sources" ]
60+
61+ # Load the seg table and filter the compartments.
62+ source = sources [source_name ]["segmentation" ]
63+ rel_path = source ["tableData" ]["tsv" ]["relativePath" ]
64+ table_content = s3 .open (os .path .join (BUCKET_NAME , cochlea , rel_path , "default.tsv" ), mode = "rb" )
65+ table = pd .read_csv (table_content , sep = "\t " )
66+
67+ # May need to be adjusted for some cochleae.
68+ component_labels = COCHLEAE_DICT [cochlea ]["component" ]
69+ print (cochlea , component_labels )
70+ table = table [table .component_labels .isin (component_labels )]
71+ ihc_dir = f"ihc_counts_{ ihc_version } "
72+ synapse_dir = f"/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/predictions/synapses/{ ihc_dir } "
73+ tab_path = os .path .join (synapse_dir , f"ihc_count_{ cochlea } .tsv" )
74+ syn_tab = pd .read_csv (tab_path , sep = "\t " )
75+ syn_ids = syn_tab ["label_id" ].values
76+
77+ syn_per_ihc = [0 for _ in range (len (table ))]
78+ table .loc [:, "syn_per_IHC" ] = syn_per_ihc
79+ for syn_id in syn_ids :
80+ table .loc [table ["label_id" ] == syn_id , "syn_per_IHC" ] = syn_tab .at [syn_tab .index [syn_tab ["label_id" ] == syn_id ][0 ], "synapse_count" ] # noqa
81+
82+ # The relevant values for analysis.
83+ try :
84+ values = table [["label_id" , "length[µm]" , "frequency[kHz]" , "syn_per_IHC" ]]
85+ except KeyError :
86+ print ("Could not find the values for" , cochlea , "it will be skippped." )
87+ continue
88+
89+ chreef_data [cochlea ] = values
90+
91+ with open (cache_path , "wb" ) as f :
92+ pickle .dump (chreef_data , f )
93+ with open (cache_path , "rb" ) as f :
94+ return pickle .load (f )
95+
2896
2997def _plot_colormap (vol , title , plot , save_path ):
3098 # before creating the figure:
@@ -53,7 +121,10 @@ def _plot_colormap(vol, title, plot, save_path):
53121 if plot :
54122 plt .show ()
55123
56- plt .savefig (save_path )
124+ if ".png" in save_path :
125+ plt .savefig (save_path , bbox_inches = "tight" , pad_inches = 0.1 , dpi = png_dpi )
126+ else :
127+ plt .savefig (save_path , bbox_inches = 'tight' , pad_inches = 0 )
57128 plt .close ()
58129
59130
@@ -99,26 +170,31 @@ def fig_03c_rl(save_path, plot=False):
99170 ax .legend (title = "cochlea" )
100171 plt .tight_layout ()
101172
102- plt .savefig (save_path , bbox_inches = "tight" , pad_inches = 0.1 , dpi = png_dpi )
173+ if ".png" in save_path :
174+ plt .savefig (save_path , bbox_inches = "tight" , pad_inches = 0.1 , dpi = png_dpi )
175+ else :
176+ plt .savefig (save_path , bbox_inches = 'tight' , pad_inches = 0 )
177+
103178 if plot :
104179 plt .show ()
105180 else :
106181 plt .close ()
107182
108183
109- def fig_03c_octave (save_path , plot = False ):
184+ def fig_03c_octave (tonotopic_data , save_path , plot = False , use_alias = True ):
110185 ihc_version = "ihc_counts_v4c"
111186 tables = glob (os .path .join (SYNAPSE_DIR_ROOT , ihc_version , "ihc_count_M_LR*.tsv" ))
112187 assert len (tables ) == 4 , len (tables )
113188
114189 result = {"cochlea" : [], "octave_band" : [], "value" : []}
115- for tab_path in tables :
116- cochlea = Path (tab_path ).stem .lstrip ("ihc_count" )
117- alias = to_alias (cochlea )
118- tab = pd .read_csv (tab_path , sep = "\t " )
119- freq = tab ["frequency" ].values
120- syn_count = tab ["synapse_count" ].values
121-
190+ for name , values in tonotopic_data .items ():
191+ if use_alias :
192+ alias = COCHLEAE_DICT [name ]["alias" ]
193+ else :
194+ alias = name .replace ("_" , "" ).replace ("0" , "" )
195+
196+ freq = values ["frequency[kHz]" ].values
197+ syn_count = values ["syn_per_IHC" ].values
122198 octave_binned = frequency_mapping (freq , syn_count , animal = "mouse" )
123199
124200 result ["cochlea" ].extend ([alias ] * len (octave_binned ))
@@ -142,7 +218,11 @@ def fig_03c_octave(save_path, plot=False):
142218 ax .set_title ("Ribbon synapse count per octave band" )
143219 plt .legend (title = "Cochlea" )
144220
145- plt .savefig (save_path , bbox_inches = "tight" , pad_inches = 0.1 , dpi = png_dpi )
221+ if ".png" in save_path :
222+ plt .savefig (save_path , bbox_inches = "tight" , pad_inches = 0.1 , dpi = png_dpi )
223+ else :
224+ plt .savefig (save_path , bbox_inches = 'tight' , pad_inches = 0 )
225+
146226 if plot :
147227 plt .show ()
148228 else :
@@ -200,7 +280,11 @@ def fig_03d_fraction(save_path, plot):
200280 ax .set_xlabel ("Type" )
201281 ax .legend (title = "Cochlea ID" )
202282
203- plt .savefig (save_path , bbox_inches = "tight" , pad_inches = 0.1 , dpi = png_dpi )
283+ if ".png" in save_path :
284+ plt .savefig (save_path , bbox_inches = "tight" , pad_inches = 0.1 , dpi = png_dpi )
285+ else :
286+ plt .savefig (save_path , bbox_inches = 'tight' , pad_inches = 0 )
287+
204288 if plot :
205289 plt .show ()
206290 else :
@@ -219,21 +303,22 @@ def main():
219303 args = parser .parse_args ()
220304
221305 os .makedirs (args .figure_dir , exist_ok = True )
306+ tonotopic_data = get_tonotopic_data ()
222307
223308 # Panel A: Tonotopic mapping of SGNs and IHCs (rendering in napari + heatmap)
224- fig_03a (save_path = os .path .join (args .figure_dir , f"fig_03a_cmap.{ FILE_EXTENSION } " ),
225- plot = args .plot , plot_napari = False )
309+ # fig_03a(save_path=os.path.join(args.figure_dir, f"fig_03a_cmap.{FILE_EXTENSION}"),
310+ # plot=args.plot, plot_napari=True )
226311
227312 # Panel C: Spatial distribution of synapses across the cochlea.
228313 # We have two options: running sum over the runlength or per octave band
229314 # fig_03c_rl(save_path=os.path.join(args.figure_dir, f"fig_03c_runlength.{FILE_EXTENSION}"), plot=args.plot)
230- # fig_03c_octave(tonotopic_data=tonotopic_data,
231- # save_path=os.path.join(args.figure_dir, f"fig_03c_octave.{FILE_EXTENSION}"),
232- # plot=args.plot)
315+ fig_03c_octave (tonotopic_data = tonotopic_data ,
316+ save_path = os .path .join (args .figure_dir , f"fig_03c_octave.{ FILE_EXTENSION } " ),
317+ plot = args .plot )
233318
234319 # Panel D: Spatial distribution of SGN sub-types.
235- fig_03d_fraction (save_path = os .path .join (args .figure_dir , "fig_03d_fraction.png " ), plot = args .plot )
236- fig_03d_octave (save_path = os .path .join (args .figure_dir , "fig_03d_octave.png " ), plot = args .plot )
320+ # fig_03d_fraction(save_path=os.path.join(args.figure_dir, f "fig_03d_fraction.{FILE_EXTENSION} "), plot=args.plot)
321+ # fig_03d_octave(save_path=os.path.join(args.figure_dir, f "fig_03d_octave.{FILE_EXTENSION} "), plot=args.plot)
237322
238323
239324if __name__ == "__main__" :
0 commit comments