11import os
2+ import sys
23
34import matplotlib .pyplot as plt
45import numpy as np
56import pandas as pd
67
78from flamingo_tools .s3_utils import create_s3_target , BUCKET_NAME
89
10+ sys .path .append ("../figures" )
11+
912
1013def _gaussian_kernel1d (sigma_pix , truncate = 4.0 ):
1114 r = int (truncate * sigma_pix + 0.5 )
@@ -55,6 +58,9 @@ def analyze_cochlea(cochlea, plot=False):
5558 window_size = 200.0
5659 grid , density = density_1d_simple (table ["length[µm]" ], window = window_size ) # window in same units as x
5760
61+ if len (grid ) != len (density ):
62+ breakpoint ()
63+
5864 if plot :
5965 plt .figure (figsize = (6 , 3 ))
6066 plt .plot (grid , density , lw = 2 )
@@ -66,34 +72,87 @@ def analyze_cochlea(cochlea, plot=False):
6672 return grid , density
6773
6874
75+ def get_sgn_counts (cochlea ):
76+ fs = create_s3_target ()
77+ seg_name = "SGN_v2"
78+
79+ table_path = f"tables/{ seg_name } /default.tsv"
80+ table = open_tsv (fs , os .path .join (cochlea , table_path ))
81+ component_ids = [1 ]
82+ table = table [table .component_labels .isin (component_ids )]
83+
84+ frequencies = table ["frequency[kHz]" ].values
85+ values = np .ones_like (frequencies )
86+
87+ return frequencies , values
88+
89+
6990def check_implementation ():
7091 cochlea = "G_EK_000049_L"
7192 analyze_cochlea (cochlea , plot = True )
7293
7394
74- def compare_gerbil_cochleae ():
75- # We need the tonotopic mapping for G_EK_000233_L
76- cochleae = ["G_EK_000233_L" , "G_EK_000049_L" , "G_EK_000049_R" ]
95+ def compare_cochleae (cochleae , animal , plot_density = True , plot_tonotopy = True ):
7796
78- plt .figure (figsize = (6 , 3 ))
79- for cochlea in cochleae :
80- grid , density = analyze_cochlea (cochlea , plot = False )
81- plt .plot (grid , density , lw = 2 , label = cochlea )
97+ if plot_density :
98+ plt .figure (figsize = (6 , 3 ))
99+ for cochlea in cochleae :
100+ grid , density = analyze_cochlea (cochlea , plot = False )
101+ plt .plot (grid , density , lw = 2 , label = cochlea )
102+
103+ plt .xlabel ("Length [µm]" )
104+ plt .ylabel ("Density [SGN/µm]" )
105+ plt .legend ()
106+ plt .tight_layout ()
107+ plt .show ()
82108
83- plt .xlabel ("Length [µm]" )
84- plt .ylabel ("Density [SGN/µm]" )
85- plt .legend ()
86- plt .tight_layout ()
87- plt .show ()
109+ if plot_tonotopy :
110+ from util import frequency_mapping
111+
112+ fig , ax = plt .subplots (figsize = (6 , 3 ))
113+ for cochlea in cochleae :
114+ frequencies , values = get_sgn_counts (cochlea )
115+ sgns_per_band = frequency_mapping (
116+ frequencies , values , animal = animal , aggregation = "sum"
117+ )
118+ bin_labels = sgns_per_band .index
119+ binned_counts = sgns_per_band .values
120+
121+ band_to_x = {band : i for i , band in enumerate (bin_labels )}
122+ x_positions = bin_labels .map (band_to_x )
123+ ax .scatter (x_positions , binned_counts , marker = "o" , label = cochlea , s = 80 )
124+
125+ ax .set_xticks (range (len (bin_labels )))
126+ ax .set_xticklabels (bin_labels )
127+ ax .set_xlabel ("Octave band [kHz]" )
128+ ax .legend ()
129+ plt .show ()
88130
89131
90132# TODO: implement the same for mouse cochleae (healthy vs. opto treatment)
91133# also show this in tonotopic mapping
92134
93135
136+ # The visualization has to be improved to make plots understandable.
94137def main ():
95138 # check_implementation()
96- compare_gerbil_cochleae ()
139+
140+ # Comparison for Gerbil.
141+ # cochleae = ["G_EK_000233_L", "G_EK_000049_L", "G_EK_000049_R"]
142+ # compare_cochleae(cochleae, animal="gerbil", plot_density=True)
143+
144+ # Comparison for Mouse.
145+ # NOTE: There is some problem with M_LR_000143_L and "M_LR_000153_L"
146+ # I have removed the corresponding pairs for now, but we should investigate and add back.
147+ cochleae = [
148+ # Healthy reference cochleae.
149+ "M_LR_000226_L" , "M_LR_000226_R" , "M_LR_000227_L" , "M_LR_000227_R" ,
150+ # Right un-injected cochleae.
151+ "M_LR_000144_R" , "M_LR_000145_R" , "M_LR_000155_R" , "M_LR_000189_R" ,
152+ # Left injected cochleae.
153+ "M_LR_000144_L" , "M_LR_000145_L" , "M_LR_000155_L" , "M_LR_000189_L" ,
154+ ]
155+ compare_cochleae (cochleae , animal = "mouse" )
97156
98157
99158if __name__ == "__main__" :
0 commit comments