Skip to content

Commit a41d7ba

Browse files
committed
update unit & probe widget to be on the same widget
1 parent d2b07d5 commit a41d7ba

File tree

1 file changed

+94
-37
lines changed

1 file changed

+94
-37
lines changed

element_array_ephys/plotting/widget.py

Lines changed: 94 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import matplotlib.pyplot as plt
55
import pathlib
66
import typing as T
7-
from IPython.display import display
7+
from IPython.display import display, clear_output
88
from .. import ephys_no_curation as ephys
99
from skimage import io
1010

1111

12-
# Widgets
12+
# Build selection widgets
1313
probe_dropdown_wg = widgets.Dropdown(
1414
options=ephys.CuratedClustering & ephys_report.ProbeLevelReport.fetch("KEY"),
1515
description="Select Probe Insertion : ",
@@ -30,6 +30,13 @@
3030
style={"description_width": "100px"},
3131
)
3232

33+
shank_toggle_wg = widgets.ToggleButtons(
34+
options=(ephys_report.ProbeLevelReport & probe_dropdown_wg.value).fetch("shank"),
35+
description="Select Shank : ",
36+
layout=widgets.Layout(width="auto"),
37+
style={"button_width": "50px"},
38+
)
39+
3340
unit_dropdown_wg = widgets.Dropdown(
3441
options=(
3542
(ephys_report.UnitLevelReport & probe_dropdown_wg.value)
@@ -43,48 +50,98 @@
4350
style={"description_width": "100px"},
4451
)
4552

46-
# Update shank & unit dropdown according to probe change
47-
def update_shank_options(change):
48-
probe_key = change.new
49-
shank_dropdown_wg.options = (ephys_report.ProbeLevelReport & probe_key.value).fetch(
50-
"shank"
51-
)
5253

54+
def probe_widget():
55+
def plot_probe_figure(probe_key, shank):
5356

54-
def update_unit_dropdown(change):
55-
probe_key = change.new
56-
unit_dropdown_wg.options = (
57-
(
58-
(ephys_report.UnitLevelReport & probe_key.value)
59-
& "cluster_quality_label='good'"
60-
).fetch("unit"),
61-
)
57+
fig_name = (
58+
ephys_report.ProbeLevelReport & probe_key & f"shank={shank}"
59+
).fetch1("drift_map_plot")
6260

61+
# Constants
62+
img_width = 2000
63+
img_height = 1000
64+
scale_factor = 0.5
6365

64-
probe_dropdown_wg.observe(update_shank_options, "value")
65-
probe_dropdown_wg.observe(update_unit_dropdown, "value")
66+
img = io.imread(fig_name)
67+
probe_fig = px.imshow(img)
6668

69+
# Configure other layout
70+
probe_fig.update_layout(
71+
width=img_width * scale_factor,
72+
height=img_height * scale_factor,
73+
margin={"l": 50, "r": 0, "t": 0, "b": 0},
74+
hovermode=False,
75+
xaxis_visible=False,
76+
yaxis_visible=False,
77+
)
6778

68-
def plot_probe_level_report(probe_key: T.Dict[str, T.Any]) -> None:
69-
fig_name = (ephys_report.ProbeLevelReport & probe_key).fetch1("drift_map_plot")
70-
fig, ax = plt.subplots(1, 1, figsize=(10, 5))
71-
img = mpimg.imread(fig_name)
72-
ax.imshow(img)
73-
ax.axis("off")
74-
plt.show()
75-
pathlib.Path(fig_name).unlink()
79+
pathlib.Path(fig_name).unlink()
80+
return go.FigureWidget(probe_fig)
7681

82+
def probe_dropdown_evt(change):
83+
probe_key = change.new
84+
shank_dropdown_wg.options = (
85+
ephys_report.ProbeLevelReport & probe_key.value
86+
).fetch("shank")
87+
unit_dropdown_wg.options = (
88+
(
89+
(ephys_report.UnitLevelReport & probe_key.value)
90+
& "cluster_quality_label='good'"
91+
).fetch("unit"),
92+
)
93+
clear_output()
94+
display(
95+
widgets.VBox(
96+
[
97+
probe_dropdown_wg,
98+
shank_dropdown_wg,
99+
plot_probe_figure(probe_key, shank_dropdown_wg.value),
100+
]
101+
)
102+
)
77103

78-
def plot_unit_level_report(unit_key: T.Dict[str, T.Any]) -> None:
79-
waveform_fig, autocorrelogram_fig, depth_waveform_fig = (
80-
ephys_report.UnitLevelReport & unit_key
81-
).fetch1("waveform_plotly", "autocorrelogram_plotly", "depth_waveform_plotly")
82-
waveform_fig = go.FigureWidget(waveform_fig)
83-
autocorrelogram_fig = go.FigureWidget(autocorrelogram_fig)
84-
depth_waveform_fig = go.FigureWidget(depth_waveform_fig)
104+
probe_dropdown_wg.observe(probe_dropdown_evt, "value")
85105

86-
display(
87-
widgets.HBox(
88-
[widgets.VBox([waveform_fig, autocorrelogram_fig]), depth_waveform_fig]
89-
)
106+
return widgets.VBox(
107+
[
108+
probe_dropdown_wg,
109+
shank_dropdown_wg,
110+
plot_probe_figure(probe_dropdown_wg.value, shank_dropdown_wg.value),
111+
]
90112
)
113+
114+
115+
def unit_widget():
116+
def plot_unit_figure(unit):
117+
# Build a unit widgets
118+
waveform_fig, autocorrelogram_fig, depth_waveform_fig = (
119+
ephys_report.UnitLevelReport & probe_dropdown_wg.value & f"unit={unit}"
120+
).fetch1("waveform_plotly", "autocorrelogram_plotly", "depth_waveform_plotly")
121+
waveform_fig = go.FigureWidget(waveform_fig).update_layout(
122+
width=300, height=300
123+
)
124+
autocorrelogram_fig = go.FigureWidget(autocorrelogram_fig).update_layout(
125+
width=300, height=300
126+
)
127+
depth_waveform_fig = go.FigureWidget(depth_waveform_fig)
128+
depth_waveform_fig.update_layout(
129+
width=300,
130+
height=600,
131+
autosize=False,
132+
margin={"l": 0, "r": 0, "t": 100, "b": 100},
133+
)
134+
135+
unit_fig_wg = widgets.HBox(
136+
[widgets.VBox([waveform_fig, autocorrelogram_fig]), depth_waveform_fig],
137+
layout=widgets.Layout(margin="0 0 0 100px"),
138+
)
139+
return unit_fig_wg
140+
141+
def unit_dropdown_evt(change):
142+
unit = change.new
143+
clear_output()
144+
display(unit_dropdown_wg, plot_unit_figure(unit))
145+
146+
unit_dropdown_wg.observe(unit_dropdown_evt, "value")
147+
return widgets.VBox([unit_dropdown_wg, plot_unit_figure(unit_dropdown_wg.value)])

0 commit comments

Comments
 (0)