-
Notifications
You must be signed in to change notification settings - Fork 6
/
visualization.py
837 lines (727 loc) · 27.3 KB
/
visualization.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
"""Basic visualization of neurite morphologies using matplotlib."""
import sys,time
import os, sys
from matplotlib.cm import get_cmap
from Crypto.Protocol.AllOrNothing import isInt
sys.setrecursionlimit(10000)
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.cm as cm
import matplotlib.animation as animation
import pylab as pl
from matplotlib import collections as mc
from PIL import Image
from numpy.linalg import inv
import Neuron as Neuron
from Neuron import Node
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.gridspec as gridspec
from numpy import mean,cov,double,cumsum,dot,linalg,array,rank
from pylab import plot,subplot,axis,stem,show,figure, Normalize
import numpy as np
import matplotlib.pyplot as plt
from copy import deepcopy
import pylab as pl
import matplotlib
from matplotlib import collections as mc
from matplotlib.patches import Circle, Wedge, Polygon
from matplotlib.collections import PatchCollection
def get_2d_image(path, size, dpi, background, show_width):
neu = Neuron(file_format = 'swc without attributes', input_file=path)
depth = neu.location[2,:]
p = neu.location[0:2,:]
widths= 5*neu.diameter
widths[0:3] = 0
m = min(depth)
M = max(depth)
depth = background * ((depth - m)/(M-m))
colors = []
lines = []
patches = []
for i in range(neu.n_soma):
x1 = neu.location[0,i]
y1 = neu.location[1,i]
r = 1*neu.diameter[i]
circle = Circle((x1, y1), r, color = str(depth[i]), ec = 'none',fc = 'none')
patches.append(circle)
pa = PatchCollection(patches, cmap=matplotlib.cm.gray)
pa.set_array(depth[0]*np.zeros(neu.n_soma))
for i in range(len(neu.nodes_list)):
colors.append(str(depth[i]))
j = neu.parent_index[i]
lines.append([(p[0,i],p[1,i]),(p[0,j],p[1,j])])
if(show_width):
lc = mc.LineCollection(lines, colors=colors, linewidths = widths)
else:
lc = mc.LineCollection(lines, colors=colors)
fig, ax = plt.subplots()
ax.add_collection(lc)
ax.add_collection(pa)
fig.set_size_inches([size + 1, size + 1])
fig.set_dpi(dpi)
plt.axis('off')
plt.xlim((min(p[0,:]),max(p[0,:])))
plt.ylim((min(p[1,:]),max(p[1,:])))
plt.draw()
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
border = (dpi/2)
return np.squeeze(data[border:-border,border:-border,0])
def projection_on_plane(neuron, normal_vec = np.array([0,0,1]), distance = 10, resolution = np.array([256,256]), gap = 3.0):
"""
Parameters
----------
return
------
dependency
----------
This function needs following data from neuron:
location
diameter
parent_index
"""
# projection all the nodes on the plane and finding the right pixel for their centers
image = np.zeros(resolution)
shift = resolution[0]/2
normal_vec1 = np.array([0,0,1])
normal_vec2 = np.array([0,1,0])
P = project_points(neuron.location, normal_vec1, normal_vec2)
for n in neuron.nodes_list:
if(n.parent != None):
n1, n2, dis = project_point(n, normal_vec1, normal_vec2)
pix1 = np.floor(n1/gap) + shift
pix2 = np.floor(n2/gap) + shift
if(0 <= pix1 and 0 <= pix2 and pix1<resolution[0] and pix2 < resolution[1]):
image[pix1, pix2] = dis
return image
def project_points(location, normal_vectors):
"""
Parameters
----------
normal_vectors : array of shape [2,3]
Each row should a normal vector and both of them should be orthogonal.
location : array of shape [3, n_nodes]
the location of n_nodes number of points
Returns
-------
cordinates: array of shape [2, n_nodes]
The cordinates of the location on the plane defined by the normal vectors.
"""
cordinates = np.dot(normal_vectors, location)
return cordinates
def depth_points(location, orthogonal_vector):
"""
Parameters
----------
orthogonal_vector : array of shape [3]
orthogonal_vector that define the plane
location : array of shape [3, n_nodes]
the location of n_nodes number of points
Returns
-------
depth: array of shape [n_nodes]
The depth of the cordinates when they project on the plane.
"""
depth = np.dot(orthogonal_vector, location)
return depth
def make_image(neuron, A, scale_depth, index_neuron):
normal_vectors = A[0:2,:]
orthogonal_vector = A[2,:]
depth = depth_points(neuron.location, orthogonal_vector)
p = project_points(neuron.location, normal_vectors)
m = min(depth)
M = max(depth)
depth = scale_depth * ((depth - m)/(M-m))
colors = []
lines = []
for i in range(len(neuron.nodes_list)):
colors.append((depth[i],depth[i],depth[i],1))
j = neuron.parent_index[i]
lines.append([(p[0,i],p[1,i]),(p[0,j],p[1,j])])
lc = mc.LineCollection(lines, colors=colors, linewidths=2)
fig, ax = pl.subplots()
ax.add_collection(lc)
pl.axis('off')
pl.xlim((min(p[0,:]),max(p[0,:])))
pl.ylim((min(p[1,:]),max(p[1,:])))
Name = "neuron" + str(index_neuron[0]+1) + "resample" + str(index_neuron[1]+1) + "angle" + str(index_neuron[2]+1) + ".png"
fig.savefig(Name,figsize=(6, 6), dpi=80)
img = Image.open(Name)
img.load()
data = np.asarray( img, dtype="int32" )
data = data[:,:,0]
return data
def random_unitary_basis(kappa):
Ax1 = random_2d_rotation_in_3d('x', kappa)
Ay1 = random_2d_rotation_in_3d('y', kappa)
Az1 = random_2d_rotation_in_3d('z', kappa)
Ax2 = random_2d_rotation_in_3d('x', kappa)
Ay1 = random_2d_rotation_in_3d('y', kappa)
Az1 = random_2d_rotation_in_3d('z', kappa)
A = np.dot(np.dot(Ax1,Ay1),Az1)
B = np.dot(np.dot(Az1,Ay1),Ax1)
return np.dot(A,B)
def random_2d_rotation_in_3d(axis, kappa):
theta = np.random.vonmises(0, kappa, 1)
A = np.eye(3)
if axis is 'z':
A[0,0] = np.cos(theta)
A[1,0] = np.sin(theta)
A[0,1] = - np.sin(theta)
A[1,1] = np.cos(theta)
return A
if axis is 'y':
A[0,0] = np.cos(theta)
A[2,0] = np.sin(theta)
A[0,2] = - np.sin(theta)
A[2,2] = np.cos(theta)
return A
if axis is 'x':
A[1,1] = np.cos(theta)
A[2,1] = np.sin(theta)
A[1,2] = - np.sin(theta)
A[2,2] = np.cos(theta)
return A
def make_six_matrix(A):
six = []
six.append(A[[0,1,2],:])
six.append(A[[0,2,1],:])
six.append(A[[1,2,0],:])
six.append(A[[1,0,2],:])
six.append(A[[2,0,1],:])
six.append(A[[2,1,0],:])
return six
def make_six_images(neuron,scale_depth,neuron_index, kappa):
#A = random_unitary_basis(kappa)
A = np.eye(3)
six = make_six_matrix(A)
D = []
for i in range(6):
a = np.append(neuron_index,i)
D.append(make_image(neuron, six[i], scale_depth, a))
return D
def generate_data(path, scale_depth, n_camrea, kappa):
"""
input
-----
path : list
list of all the pathes of swc. each element of the list should be a string.
scale_depth : float in the interval [0,1]
a value to differentiate between the background and gray level in the image.
n_camera : int
number of different angles to set the six images. For each angle, six images will be generated (up,down and four sides)
kappa : float
The width of the distribution that the angles come from. Large value for kappa results in the angles close to x aixs
kappa = 1 is equvalent to the random angle.
output
------
Data : list of length
"""
Data = []
for i in range(len(path)):
print path[i]
neuron = Neuron(file_format = 'swc without attributes', input_file=path[i])
if(len(neuron.nodes_list) != 0):
for j in range(n_camrea):
D = np.asarray(make_six_images(neuron,scale_depth,np.array([i,j]), kappa))
Data.append(D)
return Data
def get_all_path(directory):
fileSet = []
for root, dirs, files in os.walk(directory):
for fileName in files:
if(fileName[-3:] == 'swc'):
fileSet.append(directory + root.replace(directory, "") + os.sep + fileName)
return fileSet
def plot_2d(neuron, show_depth, line_width):
depth = neuron.location[0,:]
m = min(depth)
M = max(depth)
depth = ((depth - m)/(M-m))
p = neuron.location[0:2,:]
colors = []
lines = []
for i in range(len(neuron.nodes_list)):
colors.append((depth[i],depth[i],depth[i],1))
j = neuron.parent_index[i]
lines.append([(p[0,i],p[1,i]),(p[0,j],p[1,j])])
if(show_depth == False):
lc = mc.LineCollection(lines, colors='k', linewidths=line_width)
else:
lc = mc.LineCollection(lines, colors=colors, linewidths=line_width)
fig, ax = pl.subplots()
ax.add_collection(lc)
pl.axis('off')
pl.xlim((min(p[0,:]),max(p[0,:])))
pl.ylim((min(p[1,:]),max(p[1,:])))
def plot_dendrograph(neuron):
print 1
def plot_2D(neuron,
background = 1,
show_width = False,
show_depth = False,
size = 5,
dpi = 80,
line_width = 1,
show_soma = False,
give_image = False,
red_after = False,
node_red = 0,
translation = (0,0),
scale_on = False,
scale = (1,1),
save = []):
depth = neuron.location[2,:]
p = neuron.location[0:2,:]
if scale_on:
p[0,:] = scale[0] * (p[0,:]-min(p[0,:]))/(max(p[0,:]) - min(p[0,:]) )
p[1,:] = scale[1] * (p[1,:]-min(p[1,:]))/(max(p[1,:]) - min(p[1,:]) )
widths= neuron.diameter
#widths[0:3] = 0
m = min(depth)
M = max(depth)
depth = background * ((depth - m)/(M-m))
colors = []
lines = []
patches = []
for i in range(neuron.n_soma):
x1 = neuron.location[0,i] + translation[0]
y1 = neuron.location[1,i] + translation[1]
r = widths[i]
circle = Circle((x1, y1), r, color = str(depth[i]), ec = 'none',fc = 'none')
patches.append(circle)
pa = PatchCollection(patches, cmap=matplotlib.cm.gray)
pa.set_array(depth[0]*np.zeros(neuron.n_soma))
for i in range(len(neuron.nodes_list)):
colors.append(str(depth[i]))
j = neuron.parent_index[i]
lines.append([(p[0,i] + translation[0],p[1,i] + translation[1]),(p[0,j] + translation[0],p[1,j] + translation[1])])
if(show_width):
if(show_depth):
lc = mc.LineCollection(lines, colors=colors, linewidths = line_width*widths)
else:
lc = mc.LineCollection(lines, linewidths = line_width*widths)
else:
if(show_depth):
lc = mc.LineCollection(lines, colors=colors, linewidths = line_width)
else:
lc = mc.LineCollection(lines, linewidths = line_width, color = 'k')
if(give_image):
if(red_after):
line1 = []
line2 = []
(I1,) = np.where(~np.isnan(neuron.connection[:,node_red]))
(I2,) = np.where(np.isnan(neuron.connection[:,node_red]))
for i in I1:
j = neuron.parent_index[i]
line1.append([(p[0,i],p[1,i]),(p[0,j],p[1,j])])
lc1 = mc.LineCollection(line1, linewidths = 2*line_width, color = 'r')
for i in I2:
j = neuron.parent_index[i]
line2.append([(p[0,i],p[1,i]),(p[0,j],p[1,j])])
lc2 = mc.LineCollection(line2, linewidths = line_width, color = 'k')
return (lc1, lc2, (min(p[0,:]),max(p[0,:])), (min(p[1,:]),max(p[1,:])))
else:
return (lc, (min(p[0,:]),max(p[0,:])), (min(p[1,:]),max(p[1,:])))
else:
fig, ax = plt.subplots()
ax.add_collection(lc)
if(show_soma):
ax.add_collection(pa)
fig.set_size_inches([size + 1, size + 1])
fig.set_dpi(dpi)
plt.axis('off')
plt.xlim((min(p[0,:]),max(p[0,:])))
plt.ylim((min(p[1,:]),max(p[1,:])))
plt.draw()
if(len(save)!=0):
plt.savefig(save, format = "eps")
# def plot_2D(neuron, background = 1, show_width = False, show_depth = False , size = 5, dpi = 80, line_width = 1):
# depth = neuron.location[2,:]
# p = neuron.location[0:2,:]
# widths= neuron.diameter
# m = min(depth)
# M = max(depth)
# depth = background * ((depth - m)/(M-m))
# colors = []
# lines = []
# patches = []
#
# for i in range(neuron.n_soma):
# x1 = neuron.location[0,i]
# y1 = neuron.location[1,i]
# r = neuron.diameter[i]
# circle = Circle((x1, y1), r, color = str(depth[i]), ec = 'none',fc = 'none')
# patches.append(circle)
#
# pa = PatchCollection(patches, cmap=matplotlib.cm.gray)
# pa.set_array(depth[0]*np.zeros(neuron.n_soma))
#
# for i in range(len(neuron.nodes_list)):
# colors.append(str(depth[i]))
# j = neuron.parent_index[i]
# lines.append([(p[0,i],p[1,i]),(p[0,j],p[1,j])])
# if(show_width):
# if(show_depth):
# lc = mc.LineCollection(lines, colors=colors, linewidths = line_width*widths)
# else:
# lc = mc.LineCollection(lines, linewidths = line_width*widths)
# else:
# if(show_depth):
# lc = mc.LineCollection(lines, colors=colors, linewidths = line_width)
# else:
# lc = mc.LineCollection(lines, linewidths = line_width)
#
# fig, ax = plt.subplots()
# ax.add_collection(lc)
# #ax.add_collection(pa)
# fig.set_size_inches([size + 1, size + 1])
# fig.set_dpi(dpi)
# plt.axis('off')
# plt.xlim((min(p[0,:]),max(p[0,:])))
# plt.ylim((min(p[1,:]),max(p[1,:])))
# plt.draw()
# return fig
def plot_3D(neuron, color_scheme="default", color_mapping=None,
synapses=None, save_image="animation",show_radius=True):
"""
3D matplotlib plot of a neuronal morphology. The SWC has to be formatted with a "three point soma".
Colors can be provided and synapse location marked
Parameters
-----------
color_scheme: string
"default" or "neuromorpho". "neuronmorpho" is high contrast
color_mapping: list[float] or list[list[float,float,float]]
Default is None. If present, this is a list[N] of colors
where N is the number of compartments, which roughly corresponds to the
number of lines in the SWC file. If in format of list[float], this list
is normalized and mapped to the jet color map, if in format of
list[list[float,float,float,float]], the 4 floats represt R,G,B,A
respectively and must be between 0-255. When not None, this argument
overrides the color_scheme argument(Note the difference with segments).
synapses : vector of bools
Default is None. If present, draw a circle or dot in a distinct color
at the location of the corresponding compartment. This is a
1xN vector.
save_image: string
Default is None. If present, should be in format "file_name.extension",
and figure produced will be saved as this filename.
show_radius : boolean
True (default) to plot the actual radius. If set to False,
the radius will be taken from `btmorph2\config.py`
"""
if show_radius==False:
plot_radius = config.fake_radius
if color_scheme == 'default':
my_color_list = config.c_scheme_default['neurite']
elif color_scheme == 'neuromorpho':
my_color_list = config.c_scheme_nm['neurite']
else:
raise Exception("Not valid color scheme")
#print 'my_color_list: ', my_color_list
fig, ax = plt.subplots()
if color_mapping is not None:
if isinstance(color_mapping[0], int):
jet = plt.get_cmap('jet')
norm = colors.Normalize(np.min(color_mapping), np.max(color_mapping))
scalarMap = cm.ScalarMappable(norm=norm, cmap=jet)
Z = [[0, 0], [0, 0]]
levels = np.linspace(np.min(color_mapping), np.max(color_mapping), 100)
CS3 = plt.contourf(Z, levels, cmap=jet)
plt.clf()
ax = fig.gca(projection='3d')
index = 0
for node in neuron.nodes_list: # not ordered but that has little importance here
# draw a line segment from parent to current point
c_x = node.xyz[0]
c_y = node.xyz[1]
c_z = node.xyz[2]
c_r = node.r
if index < 3:
pass
else:
parent = node.parent
p_x = parent.xyz[0]
p_y = parent.xyz[1]
p_z = parent.xyz[2]
# p_r = parent.content['p3d'].radius
# print 'index:', index, ', len(cs)=', len(color_mapping)
if show_radius==False:
line_width = plot_radius
else:
line_width = c_r/2.0
if color_mapping is None:
ax.plot([p_x, c_x], [p_y, c_y], [p_z, c_z], my_color_list[node.set_type_from_name() - 1], linewidth=line_width)
else:
if isinstance(color_mapping[0], int):
c = scalarMap.to_rgba(color_mapping[index])
elif isinstance(color_mapping[0], list):
c = [float(x) / 255 for x in color_mapping[index]]
ax.plot([p_x, c_x], [p_y, c_y], [p_z, c_z], c=c, linewidth=c_r/2.0)
# add the synapses
if synapses is not None:
if synapses[index]:
ax.scatter(c_x, c_y, c_z, c='r')
index += 1
#minv, maxv = neuron.get_boundingbox()
#minv = min(minv)
#maxv = max(maxv)
#ax.auto_scale_xyz([minv, maxv], [minv, maxv], [minv, maxv])
index = 0
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
if color_mapping is not None:
if isinstance(color_mapping[0], int):
cb = plt.colorbar(CS3) # bit of a workaround, but it seems to work
ticks_f = np.linspace(np.min(color_mapping)-1, np.max(color_mapping)+1, 5)
ticks_i = map(int, ticks_f)
cb.set_ticks(ticks_i)
# set the bg color
fig = plt.gcf()
ax = fig.gca()
if color_scheme == 'default':
ax.set_axis_bgcolor(config.c_scheme_default['bg'])
elif color_scheme == 'neuromorpho':
ax.set_axis_bgcolor(config.c_scheme_nm['bg'])
if save_image is not None:
plt.savefig(save_image)
plt.show()
return fig
def animate(neuron, color_scheme="default", color_mapping=None,
synapses=None, save_image=None, axis="z"):
"""
3D matplotlib plot of a neuronal morphology. The SWC has to be formatted with a "three point soma".
Colors can be provided and synapse location marked
Parameters
-----------
color_scheme: string
"default" or "neuromorpho". "neuronmorpho" is high contrast
color_mapping: list[float] or list[list[float,float,float]]
Default is None. If present, this is a list[N] of colors
where N is the number of compartments, which roughly corresponds to the
number of lines in the SWC file. If in format of list[float], this list
is normalized and mapped to the jet color map, if in format of
list[list[float,float,float,float]], the 4 floats represt R,G,B,A
respectively and must be between 0-255. When not None, this argument
overrides the color_scheme argument(Note the difference with segments).
synapses : vector of bools
Default is None. If present, draw a circle or dot in a distinct color
at the location of the corresponding compartment. This is a
1xN vector.
save_image: string
Default is None. If present, should be in format "file_name.extension",
and figure produced will be saved as this filename.
"""
if color_scheme == 'default':
my_color_list = config.c_scheme_default['neurite']
elif color_scheme == 'neuromorpho':
my_color_list = config.c_scheme_nm['neurite']
else:
raise Exception("Not valid color scheme")
print 'my_color_list: ', my_color_list
fig, ax = plt.subplots()
if color_mapping is not None:
if isinstance(color_mapping[0], int):
jet = plt.get_cmap('jet')
norm = colors.Normalize(np.min(color_mapping), np.max(color_mapping))
scalarMap = cm.ScalarMappable(norm=norm, cmap=jet)
Z = [[0, 0], [0, 0]]
levels = np.linspace(np.min(color_mapping), np.max(color_mapping), 100)
CS3 = plt.contourf(Z, levels, cmap=jet)
plt.clf()
ax = fig.gca(projection='3d')
index = 0
for node in neuron.nodes_list: # not ordered but that has little importance here
# draw a line segment from parent to current point
c_x = node.xyz[0]
c_y = node.xyz[1]
c_z = node.xyz[2]
c_r = node.r
if index < 3:
pass
else:
parent = node.parent
p_x = parent.xyz[0]
p_y = parent.xyz[1]
p_z = parent.xyz[2]
# p_r = parent.content['p3d'].radius
# print 'index:', index, ', len(cs)=', len(color_mapping)
if color_mapping is None:
ax.plot([p_x, c_x], [p_y, c_y], [p_z, c_z], my_color_list[node.set_type_from_name() - 1], linewidth=c_r/2.0)
else:
if isinstance(color_mapping[0], int):
c = scalarMap.to_rgba(color_mapping[index])
elif isinstance(color_mapping[0], list):
c = [float(x) / 255 for x in color_mapping[index]]
ax.plot([p_x, c_x], [p_y, c_y], [p_z, c_z], c=c, linewidth=c_r/2.0)
# add the synapses
if synapses is not None:
if synapses[index]:
ax.scatter(c_x, c_y, c_z, c='r')
index += 1
#minv, maxv = neuron.get_boundingbox()
#minv = min(minv)
#maxv = max(maxv)
#ax.auto_scale_xyz([minv, maxv], [minv, maxv], [minv, maxv])
index = 0
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
if color_mapping is not None:
if isinstance(color_mapping[0], int):
cb = plt.colorbar(CS3) # bit of a workaround, but it seems to work
ticks_f = np.linspace(np.min(color_mapping)-1, np.max(color_mapping)+1, 5)
ticks_i = map(int, ticks_f)
cb.set_ticks(ticks_i)
# set the bg color
fig = plt.gcf()
ax = fig.gca()
if color_scheme == 'default':
ax.set_axis_bgcolor(config.c_scheme_default['bg'])
elif color_scheme == 'neuromorpho':
ax.set_axis_bgcolor(config.c_scheme_nm['bg'])
anim = animation.FuncAnimation(fig, _animate_rotation,fargs=(ax,), frames=60)
#anim.save(save_image + ".gif", writer='imagemagick', fps=4)
# anim.save(save_image + ".gif", writer='ffmpeg', fps=4)
return fig
def _animate_rotation(nframe,fargs):
fargs.view_init(elev=0, azim=nframe*6)
def plot_3D_Forest(neuron, color_scheme="default", save_image=None):
"""
3D matplotlib plot of a neuronal morphology. The Forest has to be formatted with a "three point soma".
Colors can be provided and synapse location marked
Parameters
-----------
color_scheme: string
"default" or "neuromorpho". "neuronmorpho" is high contrast
save_image: string
Default is None. If present, should be in format "file_name.extension",
and figure produced will be saved as this filename.
"""
my_color_list = ['r','g','b','c','m','y','r--','b--','g--']
# resolve some potentially conflicting arguments
if color_scheme == 'default':
my_color_list = config.c_scheme_default['neurite']
elif color_scheme == 'neuromorpho':
my_color_list = config.c_scheme_nm['neurite']
else:
raise Exception("Not valid color scheme")
print 'my_color_list: ', my_color_list
fig, ax = plt.subplots()
ax = fig.gca(projection='3d')
index = 0
for node in neuron.nodes_list:
c_x = node.xyz[0]
c_y = node.xyz[1]
c_z = node.xyz[2]
c_r = node.r
if index < 3:
pass
else:
parent = node.parent
p_x = parent.xyz[0]
p_y = parent.xyz[1]
p_z = parent.xyz[2]
# p_r = parent.content['p3d'].radius
# print 'index:', index, ', len(cs)=', len(color_mapping)
ax.plot([p_x, c_x], [p_y, c_y], [p_z, c_z], my_color_list[node.set_type_from_name() - 1], linewidth=c_r/2.0)
index += 1
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
if save_image is not None:
plt.savefig(save_image)
return fig
def important_node_full_matrix(neuron):
lines = []
(branch_index,) = np.where(neuron.branch_order==2)
(end_nodes,) = np.where(neuron.branch_order==0)
important_node = np.append(branch_index,end_nodes)
parent_important = neuron.parent_index_for_node_subset(important_node)
important_node = np.append(0, important_node)
L = []
for i in parent_important:
(j,) = np.where(important_node==i)
L = np.append(L,j)
matrix = np.zeros([len(L),len(L)])
for i in range(len(L)):
if(L[i]!=0):
matrix[i,L[i]-1] = 1
B = inv(np.eye(len(L)) - matrix)
return B
def decompose_immediate_children(matrix):
"""
Parameters
----------
matrix : numpy array of shape (n,n)
The matrix of connetion. matrix(i,j) is one is j is a grandparent of i.
Return
------
L : list of numpy array of square shape
L consists of decomposition of matrix to immediate children of root.
"""
a = matrix.sum(axis = 1)
(children,) = np.where(a == 1)
L = []
for ch in children:
(ind,) = np.where(matrix[:,ch]==1)
ind = ind[ind!=ch]
L.append(matrix[np.ix_(ind,ind)])
p = np.zeros(len(L))
for i in range(len(L)):
p[i] = L[i].shape[0]
s = np.argsort(p)
List = []
for i in range(len(L)):
List.append(L[s[i]])
return List
def box(x_min, x_max, y, matrix, line):
"""
The box region for each node in the tree.
"""
L = decompose_immediate_children(matrix)
length = np.zeros(len(L)+1)
for i in range(1,1+len(L)):
length[i] = L[i-1].shape[0] + 1
for i in range(len(L)):
x_left = x_min + (x_max-x_min)*(sum(length[0:i+1])/sum(length))
x_right = x_min + (x_max-x_min)*(sum(length[0:i+2])/sum(length))
line.append([((x_min + x_max)/2., y),((x_left + x_right)/2.,y-1)])
if(L[i].shape[0] > 0):
box(x_left, x_right, y-1, L[i], line)
return line
def plot_dedrite_tree(neuron,
show_all_nodes=False,
save=[]):
"""
Showing the dendogram of the neuron.
Parameters
----------
neuron: Neuron
the input neuron
show_all_nodes: boolean
if Ture, it will show all the nodes, otherwise only the main points are
taking into account.
save: str
the path to save the figure.
"""
B = important_node_full_matrix(neuron)
L = decompose_immediate_children(B)
l = box(0.,1.,0.,B,[])
min_y = 0
for i in l:
min_y = min(min_y, i[1][1])
lc = mc.LineCollection(l)
fig, ax = plt.subplots()
ax.add_collection(lc)
plt.axis('off')
plt.xlim((0,1))
plt.ylim((min_y,0))
plt.draw()
if(len(save)!=0):
plt.savefig(save, format = "eps")