Skip to content

Commit bad43ec

Browse files
Update density analysis
1 parent ea4bb5d commit bad43ec

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

scripts/figures/util.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ def _get_mapping(animal):
145145

146146

147147
def frequency_mapping(
148-
frequencies, values, animal="mouse", transduction_efficiency=False, bin_edges=None, bin_labels=None
148+
frequencies, values, animal="mouse", transduction_efficiency=False,
149+
bin_edges=None, bin_labels=None, aggregation="mean",
149150
):
150151
# Get the mapping of frequencies to octave bands for the given species.
151152
if bin_edges is None:
@@ -163,11 +164,8 @@ def frequency_mapping(
163164
num_tot = df[df["value"].isin([1, 2])].groupby("octave_band", observed=False).size()
164165
value_by_band = (num_pos / num_tot).reindex(bin_labels)
165166
else: # Otherwise, aggregate the values over the octave band using the mean.
166-
value_by_band = (
167-
df.groupby("octave_band", observed=True)["value"]
168-
.mean()
169-
.reindex(bin_labels) # keep octave order even if a bin is empty
170-
)
167+
aggregator = getattr(df.groupby("octave_band", observed=True)["value"], aggregation)
168+
value_by_band = aggregator().reindex(bin_labels) # keep octave order even if a bin is empty
171169
return value_by_band
172170

173171

scripts/measurements/density_analysis.py

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import os
2+
import sys
23

34
import matplotlib.pyplot as plt
45
import numpy as np
56
import pandas as pd
67

78
from flamingo_tools.s3_utils import create_s3_target, BUCKET_NAME
89

10+
sys.path.append("../figures")
11+
912

1013
def _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+
6990
def 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.
94137
def 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

99158
if __name__ == "__main__":

0 commit comments

Comments
 (0)