| 1 |
import os |
| 2 |
|
| 3 |
import mayavi.mlab as mayavi |
| 4 |
from brian2 import * |
| 5 |
|
| 6 |
def find_max_coordinates(morpho, current_max): |
| 7 |
max_x, max_y, max_z = np.max(np.abs(morpho.coordinates), axis=0) |
| 8 |
new_max = (max([max_x, current_max[0]]), |
| 9 |
max([max_y, current_max[1]]), |
| 10 |
max([max_z, current_max[2]])) |
| 11 |
for child in morpho.children: |
| 12 |
new_max = find_max_coordinates(child, new_max) |
| 13 |
return new_max |
| 14 |
|
| 15 |
DARKRED = (0.5450980392156862, 0.0, 0.0) |
| 16 |
DARKBLUE = (0.0, 0.0, 0.5450980392156862) |
| 17 |
|
| 18 |
def plot_morphology3D(morpho, color_switch=False, show_compartments=False): |
| 19 |
if isinstance(morpho, Soma): |
| 20 |
mayavi.points3d(morpho.x/um, morpho.y/um, morpho.z/um, morpho.diameter/um, |
| 21 |
color=DARKRED, scale_factor=1.0, resolution=16) |
| 22 |
else: |
| 23 |
coords = morpho.coordinates |
| 24 |
if color_switch: |
| 25 |
color = DARKBLUE |
| 26 |
else: |
| 27 |
color = DARKRED |
| 28 |
mayavi.plot3d(coords[:, 0]/um, coords[:, 1]/um, coords[:, 2]/um, color=color, |
| 29 |
tube_radius=1) |
| 30 |
# dots at the center of the compartments |
| 31 |
if show_compartments: |
| 32 |
mayavi.points3d(coords[:, 0]/um, coords[:, 1]/um, coords[:, 2]/um, |
| 33 |
np.ones(coords.shape[0]), color=color, scale_factor=1, |
| 34 |
transparent=0.25) |
| 35 |
|
| 36 |
for child in morpho.children: |
| 37 |
plot_morphology3D(child, color_switch=not color_switch) |
| 38 |
|
| 39 |
def plot_morphology2D(morpho, axis, color_switch=False): |
| 40 |
if isinstance(morpho, Soma): |
| 41 |
ax.plot(morpho.x/um, morpho.y/um, 'o', color='darkred', |
| 42 |
ms=morpho.diameter/um, mec='none') |
| 43 |
else: |
| 44 |
coords = morpho.coordinates |
| 45 |
if color_switch: |
| 46 |
color = 'darkblue' |
| 47 |
else: |
| 48 |
color = 'darkred' |
| 49 |
ax.plot(coords[:, 0]/um, coords[:, 1]/um, color='black', |
| 50 |
lw=2) |
| 51 |
# dots at the center of the compartments |
| 52 |
ax.plot(morpho.x/um, morpho.y/um, 'o', color=color, |
| 53 |
mec='none', alpha=0.75) |
| 54 |
|
| 55 |
for child in morpho.children: |
| 56 |
plot_morphology2D(child, axis=axis, color_switch=not color_switch) |
| 57 |
|
| 58 |
if __name__ == '__main__': |
| 59 |
dirname = os.path.dirname(__file__) |
| 60 |
PATH = os.path.join(dirname, '..', '..', '..', 'docs_sphinx', 'user', 'images') |
| 61 |
|
| 62 |
# Construct binary tree according to Rall's formula |
| 63 |
diameter = 32*um |
| 64 |
length = 80*um |
| 65 |
morpho = Soma(diameter=diameter) |
| 66 |
endpoints = {morpho} |
| 67 |
for depth in range(1, 10): |
| 68 |
diameter /= 2.**(1./3.) |
| 69 |
length /= 2.**(2./3.) |
| 70 |
new_endpoints = set() |
| 71 |
for endpoint in endpoints: |
| 72 |
new_L = Cylinder(n=max([1, int(length/(5*um))]), diameter=diameter, |
| 73 |
length=length) |
| 74 |
new_R = Cylinder(n=max([1, int(length/(5*um))]), diameter=diameter, |
| 75 |
length=length) |
| 76 |
new_endpoints.add(new_L) |
| 77 |
new_endpoints.add(new_R) |
| 78 |
endpoint.L = new_L |
| 79 |
endpoint.R = new_R |
| 80 |
endpoints = new_endpoints |
| 81 |
print('total number of sections and compartments', morpho.n_sections, len(morpho)) |
| 82 |
morpho_with_coords = morpho.generate_coordinates() |
| 83 |
|
| 84 |
ax = plt.subplot(111) |
| 85 |
plot_morphology2D(morpho_with_coords, ax) |
| 86 |
plt.axis('equal') |
| 87 |
plt.xlabel('x ($\mu$ m)') |
| 88 |
plt.ylabel('y ($\mu$ m)') |
| 89 |
|
| 90 |
plt.savefig(os.path.join(PATH, 'morphology_deterministic_coords.png')) |
| 91 |
|
| 92 |
print('be careful, this plotting takes a long time') |
| 93 |
for title, noise_sec, noise_comp in [('section', 25, 0), |
| 94 |
('section_compartment', 25, 15)]: |
| 95 |
|
| 96 |
for idx in range(3): |
| 97 |
fig = mayavi.figure(bgcolor=(0.95, 0.95, 0.95)) |
| 98 |
print(idx) |
| 99 |
morpho_with_coords = morpho.generate_coordinates(section_randomness=noise_sec, |
| 100 |
compartment_randomness=noise_comp) |
| 101 |
plot_morphology3D(morpho_with_coords) |
| 102 |
cam = fig.scene.camera |
| 103 |
cam.zoom(1.1) |
| 104 |
mayavi.draw() |
| 105 |
mayavi.savefig(os.path.join(PATH, 'morphology_random_%s_%d.png' % (title, idx+1))) |
| 106 |
mayavi.close() |