-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxis3dwithneuron_new3.py
131 lines (116 loc) · 4.71 KB
/
axis3dwithneuron_new3.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
from matplotlib.pyplot import cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from neuron import h
from neuron.gui2.utilities import _segment_3d_pts
class Axis3DWithNEURON(Axes3D):
def auto_aspect(self):
"""sets the x, y, and z range symmetric around the center
Probably needs a square figure to preserve lengths as you rotate."""
bounds = [self.get_xlim(), self.get_ylim(), self.get_zlim()]
half_delta_max = max([(item[1] - item[0]) / 2 for item in bounds])
xmid = sum(bounds[0]) / 2
ymid = sum(bounds[1]) / 2
zmid = sum(bounds[2]) / 2
#self.auto_scale_xyz([xmid - half_delta_max, xmid + half_delta_max],
# [ymid - half_delta_max, ymid + half_delta_max],
# [zmid - half_delta_max, zmid + half_delta_max])
self.auto_scale_xyz([xmid - 160, xmid + 160],
[ymid - 160, ymid + 160],
[zmid - 160, zmid + 160])
def mark(self, segment, marker='or', number=1, **kwargs):
"""plot a marker on a segment
Args:
segment = the segment to mark
marker = matplotlib marker
**kwargs = passed to matplotlib's plot
"""
# TODO: there has to be a better way to do this
sec = segment.sec
n3d = sec.n3d()
arc3d = [sec.arc3d(i) for i in range(n3d)]
x3d = np.array([sec.x3d(i) for i in range(n3d)])
y3d = np.array([sec.y3d(i) for i in range(n3d)])
z3d = np.array([sec.z3d(i) for i in range(n3d)])
seg_l = sec.L * segment.x
x = np.interp(seg_l, arc3d, x3d)
y = np.interp(seg_l, arc3d, y3d)
z = np.interp(seg_l, arc3d, z3d)
self.plot([x], [y], [z], marker)
self.text(x, y, z, str(number), color='red', weight='bold')
def shapeplot(self,
sections=None,
variable=None,
cmap=cm.cool,
**kwargs):
"""
Plots a 3D shapeplot
Args:
sections = list of h.Section() objects to be plotted
**kwargs passes on to matplotlib (e.g. linewidth=2 for thick lines)
Returns:
lines = list of line objects making up shapeplot
Adapted from
https://github.com/ahwillia/PyNeuron-Toolbox/blob/master/PyNeuronToolbox/morphology.py
Accessed 2019-04-11, which had an MIT license
"""
# Default is to plot all sections.
if sections is None:
sections = list(h.allsec())
h.define_shape()
# default color is black
kwargs.setdefault('color', 'black')
# Plot each segement as a line
lines = []
vals = []
diams=[]
for sec in sections:
all_seg_pts = _segment_3d_pts(sec)
for seg, (xs, ys, zs, _, _) in zip(sec, all_seg_pts):
line, = self.plot(xs, ys, zs, '-', **kwargs)
if variable is not None:
try:
if '.' in variable:
mech, var = variable.split('.')
val = getattr(getattr(seg, mech), var)
else:
val = getattr(seg, variable)
except AttributeError:
# leave default color if no variable found
val = None
vals.append(val)
diams.append(seg.diam)
lines.append(line)
if variable is not None:
have_values = True
try:
val_min = min(val for val in vals if val is not None)
val_max = max(val for val in vals if val is not None)
val_range = val_max - val_min
except ValueError:
have_values = False
if have_values and val_range:
for sec in sections:
for line, val, diam in zip(lines, vals, diams):
if val is not None:
#col = cmap(int(255 * (val - val_min) / (val_range)))
line.set_color('k')
line.set_linewidth(diam)
return lines
#if __name__ == '__main__':
# cell = Cell(0)
# h.finitialize(-65)
# h.distance(0, cell.soma[0](0.5))
# for sec in cell.all:
# for seg in sec:
# seg.v = h.distance(seg)
# fig = plt.figure()
# ax = Axis3DWithNEURON(fig)
# ax.grid(False)
# ax.axis('off')
# ax.shapeplot(color='red', variable='v')
# ax.mark(cell.soma[0](0.5))
# ax.mark(cell.axon[53](1))
# ax.mark(cell.apic[33](0.1), marker='ob')
# plt.show()