Debian Codegraph @

dev/tools/docs/create_multicompartment_diagrams.py

raw
1 import os
2
3 from brian2 import *
4
5 HEADER = '''<?xml version="1.0" encoding="iso-8859-1"?>
6 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20001102//EN"
7 "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd">
8
9 <svg height="3em" viewBox="{minx} {miny} {width} {height}"
10 xmlns="http://www.w3.org/2000/svg"
11 xmlns:xlink="http://www.w3.org/1999/xlink/">
12 '''
13
14 # For now, we do not bother showing the actual size of the Soma
15 SPHERE = '''
16 <defs>
17 <radialGradient id="blueSphere"
18 cx="0" cy="0" r="100%" fx="-50%" fy="-50%">
19 <stop offset="0%" stop-color="white" />
20 <stop offset="75%" stop-color="blue" />
21 <stop offset="100%" stop-color="darkblue" />
22 </radialGradient>
23 </defs>
24 <circle fill="url(#blueSphere)" cx="0" cy="0" r="5" />
25 '''
26
27 CYLINDER = '''
28 <g transform="translate({startx},0)">
29 <ellipse cx="0" cy="{center}"
30 rx="0.5" ry="{radius}"
31 stroke="blue" stroke-width="1"
32 fill="blue"/>
33 <rect x="0" y="{starty}" width="{length}" height="{diameter}" fill="blue"
34 stroke="blue" stroke-width="1"/>
35 <ellipse cx="{length}" cy="{center}"
36 rx="0.5" ry="{radius}"
37 stroke="darkblue" fill="darkblue" stroke-width="1"/>
38 </g>
39 '''
40
41 FRUSTRUM = '''
42 <g transform="translate({startx},0)">
43 <ellipse cx="0" cy="{center}"
44 rx="0.5" ry="{radius1}"
45 stroke="blue" stroke-width="1"
46 fill="blue"/>
47 <polygon points="0 {starty1}, 0 {starty2}, {length} {endy2}, {length} {endy1}"
48 fill="blue" stroke="blue" stroke-width="1"/>
49 <ellipse cx="{length}" cy="{center}"
50 rx="0.5" ry="{radius2}"
51 stroke="darkblue" fill="darkblue" stroke-width="1"/>
52 </g>
53 '''
54
55 PARENT_CYLINDER = '''
56 <defs>
57 <linearGradient id="gradient_to_transparent_grey" x1="0" y1="0" x2="100%" y2="0">
58 <stop offset="0" stop-color="white" stop-opacity="0"/>
59 <stop offset="0.5" stop-color="#222222" stop-opacity="1"/>
60 <stop offset="1" stop-color="#222222" stop-opacity="1"/>
61 </linearGradient>
62 </defs>
63 <g transform="translate(-{length},0)">
64 <rect x="-3" y="{starty}" width="{length}" height="{diameter}" fill="url(#gradient_to_transparent_grey)"
65 stroke="url(#gradient_to_transparent_grey)" stroke-width="1"/>
66 <g transform="translate(-3,0)">
67 <ellipse cx="{length}" cy="{center}"
68 rx="0.5" ry="{radius}"
69 stroke="black" fill="black" stroke-width="1"/>
70 </g>
71 </g>
72 '''
73
74 CONNECTION = '''
75 <line x1="{start}" x2="{end}" y1="{center}" y2="{center}" stroke="black" stroke-width="1"/>
76 '''
77 FOOTER = '''
78 </svg>
79 '''
80
81 def to_svg(morphology):
82 if isinstance(morphology, Soma):
83 return HEADER.format(minx='-5', miny='-5', width='10', height='10') + SPHERE.format(radius=morphology.diameter[0]/2/um) + FOOTER
84 if isinstance(morphology, Cylinder):
85 summed_length = 0*um
86 elements = []
87 center = max([7.5*um, max(morphology.end_diameter)/2])
88 minx = -2.5
89 if morphology.parent is not None:
90 elements.append(PARENT_CYLINDER.format(starty=(center-morphology.parent.end_diameter[-1]/2)/um,
91 length=morphology.parent.length[-1]/um,
92 center=center/um,
93 diameter=morphology.parent.end_diameter[-1]/um,
94 radius=morphology.parent.end_diameter[-1]/2/um))
95 minx -= morphology.parent.length[-1]/um
96 for idx, (diameter, length) in enumerate(zip(morphology.diameter, morphology.length)):
97 elements.append(CONNECTION.format(start=(summed_length-3*um)/um,
98 end=summed_length/um,
99 center=center/um))
100 elements.append(CYLINDER.format(startx=summed_length/um,
101 starty=(center-diameter/2)/um,
102 radius=diameter/2/um,
103 diameter=diameter/um,
104 length=length/um,
105 center=center/um))
106 summed_length += length + 3*um
107 return HEADER.format(minx=minx, miny=-1, width=summed_length/um+0.5-minx, height=center*2/um+2) + ('\n'.join(elements)) + FOOTER
108 elif isinstance(morphology, Section):
109 summed_length = 0*um
110 elements = []
111 center = max([7.5*um, max(morphology.end_diameter)/2])
112 minx = -5
113 if morphology.parent is not None:
114 elements.append(PARENT_CYLINDER.format(starty=(center-morphology.parent.end_diameter[-1]/2)/um,
115 length=morphology.parent.length[-1]/um,
116 center=center/um,
117 diameter=morphology.parent.end_diameter[-1]/um,
118 radius=morphology.parent.end_diameter[-1]/2/um))
119 minx -= morphology.parent.length[-1]/um
120 for idx, (start_diameter, end_diameter, length) in enumerate(zip(morphology.start_diameter, morphology.end_diameter, morphology.length)):
121 elements.append(CONNECTION.format(start=(summed_length-3*um)/um,
122 end=summed_length/um,
123 center=center/um))
124 elements.append(FRUSTRUM.format(startx=summed_length/um,
125 starty1=(center-start_diameter/2)/um,
126 starty2=(center+start_diameter/2)/um,
127 endy1=(center-end_diameter/2)/um,
128 endy2=(center+end_diameter/2)/um,
129 radius1=start_diameter/2/um,
130 radius2=end_diameter/2/um,
131 length=length/um,
132 center=center/um))
133 summed_length += length + 3*um
134 return HEADER.format(minx=minx, miny=-1, width=summed_length/um+1.5-minx,
135 height=center*2/um+2) + ('\n'.join(elements)) + FOOTER
136 else:
137 raise NotImplementedError()
138 if __name__ == '__main__':
139 dirname = os.path.dirname(__file__)
140 PATH = os.path.join(dirname, '..', '..', '..', 'docs_sphinx', 'user', 'images')
141 root = Cylinder(n=1, diameter=15*um, length=10*um)
142 root.cyl = Cylinder(n=5, diameter=10*um, length=50*um)
143 root.sec = Section(n=5, diameter=[15, 5, 10, 5, 10, 5]*um,
144 length=[10, 20, 5, 5, 10]*um)
145 for filename, morpho in [('soma.svg', Soma(diameter=30*um)),
146 ('cylinder.svg', root.cyl),
147 ('section.svg', root.sec)]:
148 with open(os.path.join(PATH, filename), 'w') as f:
149 print(filename)
150 f.write(to_svg(morpho))