This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstg.py
349 lines (252 loc) · 8.01 KB
/
stg.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
from tqdm import tqdm
import numpy as np
from bokeh.palettes import Viridis256
from bokeh.layouts import layout
from bokeh.models import ColumnDataSource, CustomJS, Div, HoverTool, Scatter, ColorMapper, ColorBar
from bokeh.plotting import figure, output_notebook, show, output_file
from bokeh.models.tickers import FixedTicker
output_notebook()
output_file("index.html")
# In[2]:
# define some important params
downsample = 13
max_n_spikes = 400
max_T_PD = 2 # seconds, above this is clipper
# In[3]:
# read data
# the reason to use pandas instead of directly reading into a numpy array
# is because numpy's genfromtext is abysmally slow
df_LP = pd.read_csv("/Users/srinivas/Desktop/stg/LP.csv",header=None)
df_PD = pd.read_csv("/Users/srinivas/Desktop/stg/PD.csv",header=None)
nrows = len(df_LP.index)
T_PD = pd.read_csv("/Users/srinivas/Desktop/stg/T_PD.csv",header=None)
T_PD = np.array(T_PD[0])
R = pd.read_csv("/Users/srinivas/Desktop/stg/R.csv",header=None)
Rx = np.array(R[0])
Ry = np.array(R[1])
# In[4]:
# to save space in the final figure, we will only
# plot data points that have < 200 spikes
keep = np.array((df_LP.count(axis=1) < max_n_spikes) & (df_PD.count(axis=1) < max_n_spikes))
# throw away data points with lots of spikes
df_LP = df_LP.iloc[keep,0:max_n_spikes]
df_PD = df_PD.iloc[keep,0:max_n_spikes]
T_PD = T_PD[keep]
Rx = Rx[keep]
Ry = Ry[keep]
# In[5]:
Rx = Rx[::downsample]
Ry = Ry[::downsample]
T_PD = T_PD[::downsample]
# convert into list of arrays
PD = []
LP = []
nrows = len(df_LP.index)
for i in tqdm(np.arange(nrows)[::downsample]):
this_LP = df_LP.iloc[i,:]
#this_LP = this_LP[~np.isnan(this_LP)]
this_PD = df_PD.iloc[i,:]
#this_PD = this_PD[~np.isnan(this_PD)]
offset = np.nanmin([np.nanmin(this_PD),np.nanmin(this_LP)])
this_LP = this_LP - offset
this_PD = this_PD - offset
PD.append(this_PD)
LP.append(this_LP)
# In[6]:
PD_y = np.zeros_like(PD[0])
LP_y = np.zeros_like(PD[0]) + 1
# In[7]:
# make colors
n = len(Rx)
pallete = Viridis256
c_PD = np.copy(T_PD)
# clip PD burst periods > max
c_PD[c_PD>max_T_PD] = max_T_PD
c_PD -= np.nanmin(c_PD)
c_PD /= np.nanmax(c_PD)
c_PD[np.isnan(c_PD)] = -2
idx = np.ceil(255 * c_PD).astype(
int
)
colors = np.repeat("#969696", n)
for i in np.arange(n):
if idx[i] < 0:
continue
colors[i] = pallete[idx[i]]
colors = tuple(colors)
# In[30]:
scatter_size=7
plot_size=700
raster_data = ColumnDataSource(data=dict(
PD = PD[0],
PD_y = PD_y,
LP = LP[0],
LP_y = LP_y,
))
marker_data = ColumnDataSource(data=dict(
x = [Rx[0]],
y = [Ry[0]],
))
tsne_plot = figure(
sizing_mode="stretch_width",
height=plot_size,
width=plot_size,
tools=[],
toolbar_location=None,
x_axis_label="t-SNE 1",
y_axis_label="t-SNE 2",
)
tsne_plot.circle(
Rx,
Ry,
size=scatter_size,
color=colors,
alpha=0.5,
hover_alpha=1,
)
tsne_plot.circle(
"x",
"y",
size=scatter_size * 2,
fill_color=None,
color="red",
source=marker_data,
line_width=3,
)
color_mapper = ColorMapper(palette="Viridis256",
)
color_bar = ColorBar(color_mapper=color_mapper,
label_standoff=12,
location=(0,0),
)
# make another scatter plot showing the rasters
raster_plot = figure(
sizing_mode="stretch_width",
height=100,
max_width=plot_size,
tools=[],
toolbar_location=None,
x_axis_label="Time (s)",
y_axis_label="Neuron",
y_range=(-.5,1.5),
x_range=(0,20),
)
raster_plot.yaxis.major_label_overrides = {
0: "PD",
1: "LP"
}
raster_plot.yaxis.ticker = FixedTicker(ticks=[0,1])
LP_glyph = Scatter(x="LP",
y="LP_y",
size=20,
marker="dash",
line_color="red",
angle=1.57)
PD_glyph = Scatter(x="PD",
y="PD_y",
size=20,
marker="dash",
line_color="blue",
angle=1.57)
raster_plot.add_glyph(raster_data,PD_glyph)
raster_plot.add_glyph(raster_data,LP_glyph)
# add a hover tool that sets the link data for a hovered circle
code = """
if (cb_data.index.indices.length > 0) {
if (cb_data.index.indices[0] > 0) {
// dirty hack to ignore the zero index of the marker
const idx = (cb_data.index.indices[0]);
raster_data.data.PD = PD[idx];
raster_data.data.LP = LP[idx];
//console.log(idx);
raster_data.change.emit();
marker_data.data.x[0] = Rx[idx];
marker_data.data.y[0] = Ry[idx];
marker_data.change.emit();
}
};
"""
callback = CustomJS(args=dict(
raster_data=raster_data,
marker_data=marker_data,
PD=PD,
LP=LP,
Rx=Rx,
Ry=Ry,
),
code=code)
tsne_plot.add_tools(HoverTool(tooltips=[], callback=callback))
#tsne_plot.add_layout(color_bar, 'right')
div = Div(
text=f"""
<h1 style="color: #4d4d4d;;">
Pyloric circuit dynamics explorer 🦀</h1>
<p style="color: #4d4d4d;;">
Scatter plot shows embedding of pyloric circuit
dynamics. Color indicates time period of PD oscillations.
Hover over each point to see the spike pattern from LP and PD
neurons for that point. </p>
""",
width=500,
height=130,
margin=(0, 10, 0, 40),
)
footer = Div(
text="""
<div style="color:#8d8e8f;">
<h3>Details</h3>
<p>
This visualization was created to accompany
<a href = "https://www.biorxiv.org/content/10.1101/2021.07.06.451370v1">
Gorur-Shandilya <i>et al</i>.</a> and full details about the method
are contained there. Briefly, the scatter plot shows
a two-dimensional visualization of a large dataset collected
from the pyloric circuit in the crab. Two identified neurons,
the LP and PD neurons are recorded from and their spikes
are measured. </p>
<p>
The top panel shows a raster plot of spikes from these two neurons.
The rasters shown in this panel correspond to the point currently
highlighted in the scatter plot with a red circle. Hover over
different points in the scatter plot to look at spike patterns
for that data point.</p>
<p>
The colors of the scatter plot correspond to the burst period of
the PD neuron. When the burst period is not defined, because
the PD neuron has no well defined bursts, dots are colored gray.
</p>
<h4>Differences from visualization in the paper</h4>
<p>
The following modifications have been made to create this interactive
visualization:
</p>
<ol>
<li>Only data segments with <400 spikes are shown, to reduce file size.</li>
<li>Only 1 in 13 data points have been shown, to reduce file size.</li>
<li>The colors corresponding to PD neurons with periods above 2s
have been clipped to 2s.</li>
</ol>
<h4>Code</h4>
<p>The code to generate this visualization, together with all code
for this project, is available
<a href = "https://github.com/sg-s/stg-embedding/">here</a>.</p>
</div>
""",
width=500,
margin=(0, 10, 0, 40),
)
show(
layout(
[
[div],
[raster_plot],
[tsne_plot],
[footer],
]
)
)
# In[ ]: