-
Notifications
You must be signed in to change notification settings - Fork 22
/
ophys.py
213 lines (180 loc) · 8.1 KB
/
ophys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import numpy as np
import matplotlib.pyplot as plt
from pynwb.ophys import RoiResponseSeries, DfOverF, PlaneSegmentation, TwoPhotonSeries, ImageSegmentation
from pynwb.base import NWBDataInterface
from ndx_grayscalevolume import GrayscaleVolume
from .utils.cmaps import linear_transfer_function
import ipywidgets as widgets
from .base import show_neurodata_base, get_timeseries_dur, get_timeseries_tt, make_trace_selector, make_time_control_panel
from scipy.spatial import ConvexHull
import plotly.graph_objects as go
from bisect import bisect
color_wheel = ['red', 'blue', 'green', 'black', 'magenta', 'yellow']
def show_two_photon_series(indexed_timeseries: TwoPhotonSeries, neurodata_vis_spec: dict):
output = widgets.Output()
if len(indexed_timeseries.data.shape) == 3:
def show_image(index=0):
fig, ax = plt.subplots(subplot_kw={'xticks': [], 'yticks': []})
ax.imshow(indexed_timeseries.data[index], cmap='gray')
output.clear_output(wait=True)
with output:
plt.show(fig)
elif len(indexed_timeseries.data.shape) == 4:
import ipyvolume.pylab as p3
def show_image(index=0):
fig = p3.figure()
p3.volshow(indexed_timeseries.data[index], tf=linear_transfer_function([0, 0, 0], max_opacity=.3))
output.clear_output(wait=True)
with output:
p3.show()
else:
raise NotImplementedError
def on_index_change(change):
show_image(change.new)
slider = widgets.IntSlider(value=0, min=0,
max=indexed_timeseries.data.shape[0] - 1,
orientation='horizontal')
slider.observe(on_index_change, names='value')
show_image()
return widgets.VBox([output, slider])
def show_df_over_f(df_over_f: DfOverF, neurodata_vis_spec: dict):
if len(df_over_f.roi_response_series) == 1:
title, input = list(df_over_f.roi_response_series.items())[0]
return neurodata_vis_spec[RoiResponseSeries](input, neurodata_vis_spec, title=title)
else:
return neurodata_vis_spec[NWBDataInterface](df_over_f, neurodata_vis_spec)
def roi_response_series_widget(node: RoiResponseSeries, neurodata_vis_spec: dict = None,
time_window_controller=None,
roi_controller=None, **kwargs):
if time_window_controller is None:
tmax = get_timeseries_dur(node)
time_window_controller = make_time_control_panel(0, tmax, (0, tmax))
if roi_controller is None:
roi_controller = make_trace_selector(len(node.rois), (0, min(30, len(node.rois))))
controls = {
'roi_response_series': widgets.fixed(node),
'time_window': time_window_controller.children[0],
'roi_window': roi_controller.children[0],
}
controls.update({key: widgets.fixed(val) for key, val in kwargs.items()})
out_fig = widgets.interactive_output(show_roi_response_series, controls)
control_widgets = widgets.HBox(children=(time_window_controller, roi_controller))
vbox = widgets.VBox(children=[control_widgets, out_fig])
return vbox
def show_roi_response_series(roi_response_series: RoiResponseSeries,
neurodata_vis_spec: dict = None,
time_window=None, roi_window=None, title: str = None):
"""
:param roi_response_series: pynwb.ophys.RoiResponseSeries
:param neurodata_vis_spec: OrderedDict
:param time_window: int
:param title: str
:return: matplotlib.pyplot.Figure
"""
if time_window is None:
time_window = [None, None]
if roi_window is None:
roi_window = [0, len(roi_response_series.rois)]
tt = get_timeseries_tt(roi_response_series)
if time_window[0] is None:
t_ind_start = 0
else:
t_ind_start = bisect(tt, time_window[0])
if time_window[1] is None:
t_ind_stop = -1
else:
t_ind_stop = bisect(tt, time_window[1], t_ind_start)
data = roi_response_series.data
tt = tt[t_ind_start: t_ind_stop]
if data.shape[1] == len(tt): # fix of orientation is incorrect
mini_data = data[roi_window[0]:roi_window[1], t_ind_start:t_ind_stop].T
else:
mini_data = data[t_ind_start:t_ind_stop, roi_window[0]:roi_window[1]]
gap = np.median(np.nanstd(mini_data, axis=0)) * 20
offsets = np.arange(roi_window[1] - roi_window[0]) * gap
fig, ax = plt.subplots()
ax.figure.set_size_inches(12, 6)
ax.plot(tt, mini_data + offsets)
ax.set_xlabel('time (s)')
ax.set_ylabel('traces')
if np.isfinite(gap):
ax.set_ylim(-gap, offsets[-1] + gap)
ax.set_xlim(tt[0], tt[-1])
ax.set_yticks(offsets)
ax.set_yticklabels(np.arange(roi_window[0], roi_window[1]))
if title is not None:
ax.set_title(title)
return fig
def show_image_segmentation(img_seg: ImageSegmentation, neurodata_vis_spec: dict):
if len(img_seg.plane_segmentations) == 1:
return show_plane_segmentation(next(iter(img_seg.plane_segmentations.values())), neurodata_vis_spec)
else:
return show_neurodata_base(ImageSegmentation, neurodata_vis_spec)
def show_plane_segmentation(plane_seg: PlaneSegmentation, neurodata_vis_spec: dict):
nrois = len(plane_seg)
if 'voxel_mask' in plane_seg:
import ipyvolume.pylab as p3
dims = np.array([max(max(plane_seg['voxel_mask'][i][dim]) for i in range(nrois))
for dim in ['x', 'y', 'z']]).astype('int') + 1
fig = p3.figure()
for icolor, color in enumerate(color_wheel):
vol = np.zeros(dims)
sel = np.arange(icolor, nrois, len(color_wheel))
for isel in sel:
dat = plane_seg['voxel_mask'][isel]
vol[tuple(dat['x'].astype('int')),
tuple(dat['y'].astype('int')),
tuple(dat['z'].astype('int'))] = 1
p3.volshow(vol, tf=linear_transfer_function(color, max_opacity=.3))
return fig
elif 'image_mask' in plane_seg:
if 'neuron_type' in plane_seg:
neuron_types = np.unique(plane_seg['neuron_type'][:])
data = plane_seg['image_mask'].data
nUnits = data.shape[0]
fig = go.FigureWidget()
aux_leg = []
for i in range(nUnits):
if plane_seg['neuron_type'][i] not in aux_leg:
show_leg = True
aux_leg.append(plane_seg['neuron_type'][i])
else:
show_leg = False
c = color_wheel[np.where(neuron_types==plane_seg['neuron_type'][i])[0][0]]
# hover text
hovertext = '<b>roi_id</b>: '+str(plane_seg['roi_id'][i])
rois_cols = list(plane_seg.colnames)
rois_cols.remove('roi_id')
sec_str = '<br>'.join([col+': '+str(plane_seg[col][i]) for col in rois_cols if isinstance(plane_seg[col][i], (int, float, np.integer, np.float, str))])
hovertext += '<br>'+sec_str
# form cell borders
y, x = np.where(plane_seg['image_mask'][i])
arr = np.vstack((x, y)).T
hull = ConvexHull(arr)
vertices = np.append(hull.vertices, hull.vertices[0])
fig.add_trace(
go.Scatter(
x=arr[vertices, 0],
y=arr[vertices, 1],
fill='toself',
mode='lines',
line_color=c,
name=plane_seg['neuron_type'][i],
legendgroup=plane_seg['neuron_type'][i],
showlegend=show_leg,
text=hovertext,
hovertext='text',
line=dict(width=.5),
)
)
fig.update_layout(
width=700, height=500,
margin=go.layout.Margin(l=60, r=60, b=60, t=60, pad=1),
plot_bgcolor="rgb(245, 245, 245)",
)
return fig
def show_grayscale_volume(vol: GrayscaleVolume, neurodata_vis_spec: dict):
import ipyvolume.pylab as p3
fig = p3.figure()
p3.volshow(vol.data, tf=linear_transfer_function([0, 0, 0], max_opacity=.1))
return fig