-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_time_plot.py
More file actions
360 lines (290 loc) · 10.9 KB
/
Copy pathreal_time_plot.py
File metadata and controls
360 lines (290 loc) · 10.9 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
'''
Script for generating the GIF in the README
See the documentation for more examples and API descriptions:
http://mouse-connectivity-models.readthedocs.io/en/latest/
'''
#TOP-DOWN view
import os
import logging
from re import X
import subprocess
import time
import math
import sys
from tkinter import TRUE
from PIL.Image import init
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm
from mcmodels.core import Mask, VoxelModelCache
from mcmodels.core.cortical_map import CorticalMap
from matplotlib.widgets import Button
# file path where the data files will be downloaded
MANIFEST_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'../connectivity', 'mcmodels_manifest.json')
mapper = CorticalMap(projection='top_view')
#lookup = mapper.view_lookup.copy().T # transpose for vertical pixel query
global lookup, paths
verbocity = 0
if len(sys.argv) > 3:
verbocity = int(sys.argv[3])
current_overlay = "init"
# check that an argument provided for topview or flatmap version
#if len(sys.argv) != 2:
# print('usage: python interactive_flat_map_animate.py topview | flatmap')
# exit()
top_down_overlay = plt.imread("data/cortical_map_top_down.png")
flat_map_overlay = plt.imread("data/transparent.png")
x_coord = -1
y_coord = -1
toggle_cid = 0
finalPath = [-1,-1,-1]
# takes string of view type to load : topview / flatmap
def load_maps (view):
global lookup
global paths
if (os.path.exists("data/"+ view + '_lookup.npy')):
if verbocity > 1:
print("found lookup.npy")
lookup = np.load("data/"+ view + '_lookup.npy')
else:
lookup = mapper.view_lookup.copy().T
np.save("data/"+ view + '_lookup.npy', lookup)
if (os.path.exists("data/"+ view + '_paths.npy')):
if verbocity > 1:
print("found paths.npy")
paths = np.load("data/"+ view + '_paths.npy')
else:
paths = mapper.paths.copy()
np.save("data/"+ view + '_paths.npy', paths)
def top_to_flat(x,y):
global lookup
global paths
x_flat = 0
y_flat = 0
for i, val in enumerate(lookup[lookup > -1]):
path = paths[val][paths[val].nonzero()]
path = np.vstack([np.unravel_index(x, reference_shape) for x in path])
if (path[0][2] == x and path[0][0] == y):
ind = np.where(lookup == val)
x_flat = ind[0][0]
y_flat = ind[1][0]
break
return x_flat, y_flat
def flat_to_top(x,y):
global lookup
global paths
x_top = 0
y_top = 0
val = lookup[x][y]
path = paths[val][paths[val].nonzero()]
path = np.vstack([np.unravel_index(x, reference_shape) for x in path])
x_top = path[0][2]
y_top = path[0][0]
return x_top, y_top
# return the coordinates of min distance nonnegative points in lookup
def find_neighbors(x, y):
coords = np.where(lookup > -1)
dist_size = len(coords[0])
distance = [-1] * dist_size
coords_x = coords[0]
coords_y = coords[1]
for i in range(dist_size):
distance[i] = math.sqrt(((x - coords_x[i])**2) + ((y - coords_y[i])**2))
min_index = np.where(distance == np.amin(distance))[0][0]
if verbocity > 0:
print("Min distance calulated : ", np.amin(distance))
print("New valid point : ", coords_x[min_index], ", ", coords_y[min_index])
return coords_x[min_index], coords_y[min_index]
# Returns -1 when coord is not in lookup
def plot_image(x, y):
'''
Plots connectivity for an injection (x,y).
Parameters:
x (int)
y (int)
'''
global switch_button
global lookup, paths
global current_overlay
global x_coord, y_coord
if (lookup[x][y] == -1):
if verbocity > 0:
print("Couldn't find: ", x, ", ", y)
return -1
i, val = 0, lookup[x][y]
# get the mean path voxel
if verbocity > 0:
print("view: ", current_overlay,"| val = ", val)
path = paths[val][paths[val].nonzero()]
path = np.vstack([np.unravel_index(x, reference_shape) for x in path])
voxel = tuple(map(int, path.mean(axis=0)))
try:
row_idx = source_mask.get_flattened_voxel_index(voxel)
except ValueError:
if verbocity > 0:
print("voxel %s not in mask", voxel)
else:
plt.clf()
if verbocity > 0:
print("coords = ", x, y)
if verbocity > 1:
print("img_name = ", img_path)
# get voxel expression
volume = target_mask.fill_volume_where_masked(np.zeros(reference_shape),
voxel_array[row_idx])
# map to cortical surface
flat_view = mapper.transform(volume, fill_value=np.nan)
# injection location
pixel = np.ma.masked_where(mapper.view_lookup != val, flat_view,
copy=False)
# plot & params
# plot connectivity
im = plt.pcolormesh(flat_view, zorder=1, cmap=cmap_view, vmin=0,
vmax=vmax)
# plot source voxel
plt.pcolormesh(pixel, zorder=2, cmap=cmap_pixel, vmin=0, vmax=1)
plt.gca().invert_yaxis() # flips yaxis
plt.axis('off')
extent = plt.gca().get_xlim() + plt.gca().get_ylim()
if current_overlay == 'topview':
# plot top down overlay as appropriate
plt.imshow(top_down_overlay, interpolation="nearest", extent=extent, zorder=3)
# print("no overlay")
else:
#empty overlay for flatmap
plt.imshow(flat_map_overlay, interpolation="nearest",
extent=extent, zorder=3)
# add colorbar
cbar = plt.colorbar(im, shrink=0.3, use_gridspec=True, format="%1.1e")
cbar.set_ticks([0, 0.0004, 0.0008, 0.0012])
cbar.ax.tick_params(labelsize=6)
plt.tight_layout()
x_coord = x
y_coord = y
# reinitialize buttons
switch_button = init_buttons()
#plt.savefig(current_overlay + ".svg")
plt.draw()
return 0
# Manage key interactions
# Calls to plot_image do not need to be checked
def on_key(event):
global x_coord, y_coord, toggle_cid
if event.key == 'up':
plot_image(x_coord, y_coord - 1)
elif event.key == 'down':
plot_image(x_coord, y_coord + 1)
elif event.key == 'left':
plot_image(x_coord - 1, y_coord)
elif event.key == 'right':
plot_image(x_coord + 1, y_coord)
elif event.key == 't':
if (toggle_cid == 0):
toggle_cid = fig.canvas.mpl_connect('motion_notify_event', on_press)
else:
fig.canvas.mpl_disconnect(toggle_cid)
toggle_cid = 0
else:
if verbocity > 0:
print(event.key + " is an invalid key")
return
def on_press(event):
'''
Gets injection coordinates (x,y) and calls function to plot projection.
Parameters:
event
'''
x = int(round(event.xdata))
y = int(round(event.ydata))
if (lookup[x][y] in lookup[lookup > -1]):
global x_coord, y_coord
plt.clf()
if verbocity > 1:
print('you pressed', event.button, x, y)
start = time.perf_counter()
plot_image(x, y)
stop = time.perf_counter()
if verbocity > 0:
print("Plot Time : ", stop - start)
x_coord = x
y_coord = y
elif x > 1 or y > 1 and verbocity > 0:
print('you pressed', event.button, x, y, 'which is out of bounds.')
def on_switch(event):
global current_overlay, mapper, lookup, x_coord, y_coord, paths
# clear the old plot
plt.clf()
# handle switching from flatmap to topview
if current_overlay == 'flatmap':
x_top, y_top = flat_to_top(x_coord, y_coord)
current_overlay = 'topview'
mapper = CorticalMap(projection='top_view')
# quick hack to fix bug
mapper.view_lookup[51, 69] = mapper.view_lookup[51, 68]
mapper.view_lookup[51, 44] = mapper.view_lookup[51, 43]
# get the new lookup
load_maps(current_overlay)
lookup[:lookup.shape[0]//2, :] = -1
if (plot_image(x_top, y_top) == -1):
# print("made it to before find neighbors")
x_top, y_top = find_neighbors(x_top, y_top)
if verbocity > 0:
print("neighbor coordinates: ", x_top, ", ", y_top)
plot_image(x_top, y_top)
# handle switching from topview to flatmap
elif current_overlay == 'topview':
current_overlay = 'flatmap'
mapper = CorticalMap(projection='dorsal_flatmap')
mapper.view_lookup[51, 69] = mapper.view_lookup[51, 68]
mapper.view_lookup[51, 44] = mapper.view_lookup[51, 43]
# get the new lookup
load_maps(current_overlay)
lookup[:lookup.shape[0]//2, :] = -1
x_flat, y_flat = top_to_flat(x_coord, y_coord)
plot_image(x_flat, y_flat)
def init_buttons():
global switch_button
ax_switch = plt.axes([0.1, 0.05, 0.1, 0.075])
switch_button = Button(ax_switch, 'Switch')
switch_button.on_clicked(on_switch)
return switch_button
if __name__ == '__main__':
start = time.perf_counter()
# caching object for downloading/loading connectivity/model data
cache = VoxelModelCache(manifest_file=MANIFEST_FILE)
# load voxel model
voxel_array, source_mask, target_mask = cache.get_voxel_connectivity_array()
reference_shape = source_mask.reference_space.annotation.shape
vmax = 1.2 * np.percentile(voxel_array.nodes, 99)
# 2D Cortical Surface Mapper
# projection: can change to "flatmap" if desired
load_maps('topview')
if sys.argv[1] == 'flatmap':
mapper = CorticalMap(projection='dorsal_flatmap')
load_maps('flatmap')
# quick hack to fix bug
mapper.view_lookup[51, 69] = mapper.view_lookup[51, 68]
mapper.view_lookup[51, 44] = mapper.view_lookup[51, 43]
# colormaps
cmap_view = matplotlib.cm.inferno
cmap_pixel = matplotlib.cm.cool
cmap_view.set_bad(alpha=0)
cmap_pixel.set_bad(alpha=0)
# # only want R hemisphere
# lookup = mapper.view_lookup.copy().T # transpose for vertical pixel query
# lookup[:lookup.shape[0]//2, :] = -1
#Begin image plotting and mouse tracking
fig, ax = plt.subplots(figsize=(6, 6), nrows = 2, num="BrainViewer")
if sys.argv[1] == 'topview':
current_overlay = 'topview'
plot_image(84,26)
if sys.argv[1] == 'flatmap':
current_overlay = 'flatmap'
plot_image(200,80)
switch_button = init_buttons()
fig.canvas.mpl_connect('button_press_event', on_press)
fig.canvas.mpl_connect('key_press_event', on_key)
if verbocity > 0:
print("Startup time : ", time.perf_counter() - start)
plt.show()