|
4 | 4 | import matplotlib.pyplot as plt |
5 | 5 | import pathlib |
6 | 6 | import typing as T |
7 | | -from IPython.display import display |
| 7 | +from IPython.display import display, clear_output |
8 | 8 | from .. import ephys_no_curation as ephys |
9 | 9 | from skimage import io |
10 | 10 |
|
11 | 11 |
|
12 | | -# Widgets |
| 12 | +# Build selection widgets |
13 | 13 | probe_dropdown_wg = widgets.Dropdown( |
14 | 14 | options=ephys.CuratedClustering & ephys_report.ProbeLevelReport.fetch("KEY"), |
15 | 15 | description="Select Probe Insertion : ", |
|
30 | 30 | style={"description_width": "100px"}, |
31 | 31 | ) |
32 | 32 |
|
| 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 | + |
33 | 40 | unit_dropdown_wg = widgets.Dropdown( |
34 | 41 | options=( |
35 | 42 | (ephys_report.UnitLevelReport & probe_dropdown_wg.value) |
|
43 | 50 | style={"description_width": "100px"}, |
44 | 51 | ) |
45 | 52 |
|
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 | | - ) |
52 | 53 |
|
| 54 | +def probe_widget(): |
| 55 | + def plot_probe_figure(probe_key, shank): |
53 | 56 |
|
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") |
62 | 60 |
|
| 61 | + # Constants |
| 62 | + img_width = 2000 |
| 63 | + img_height = 1000 |
| 64 | + scale_factor = 0.5 |
63 | 65 |
|
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) |
66 | 68 |
|
| 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 | + ) |
67 | 78 |
|
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) |
76 | 81 |
|
| 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 | + ) |
77 | 103 |
|
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") |
85 | 105 |
|
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 | + ] |
90 | 112 | ) |
| 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