Debian Codegraph @

brian2/tests/test_morphology.py

raw
1 import os
2 import tempfile
3
4 import pytest
5 from numpy.testing import assert_equal
6
7 from brian2 import numpy as np
8 from brian2.spatialneuron import *
9 from brian2.tests.utils import assert_allclose
10 from brian2.units import DimensionMismatchError, cm, second, um
11 from brian2.utils.logger import catch_logs
12
13
14 @pytest.mark.codegen_independent
15 def test_attributes_soma():
16 soma = Soma(diameter=10 * um)
17 assert isinstance(soma, Morphology)
18 # Single compartment
19 assert soma.n == 1
20 assert soma.total_sections == 1
21 assert soma.total_compartments == 1
22 with pytest.raises(TypeError):
23 len(soma) # ambiguous
24 # Compartment attributes
25 assert_equal(soma.diameter, [10] * um)
26 assert_equal(soma.length, [10] * um)
27 assert_equal(soma.distance, [0] * um)
28 assert_equal(soma.end_distance, 0 * um)
29 assert soma.r_length_1 > 1 * cm
30 assert soma.r_length_2 > 1 * cm
31 assert_equal(soma.area, np.pi * soma.diameter**2)
32 assert_allclose(soma.volume, 1.0 / 6.0 * np.pi * (10 * um) ** 3)
33
34 # No coordinates were specified
35 assert soma.start_x is None
36 assert soma.start_y is None
37 assert soma.start_z is None
38 assert soma.x is None
39 assert soma.y is None
40 assert soma.z is None
41 assert soma.end_x is None
42 assert soma.end_y is None
43 assert soma.end_z is None
44
45
46 @pytest.mark.codegen_independent
47 def test_attributes_soma_coordinates():
48 # Specify only one of the coordinates
49 xyz = {"x", "y", "z"}
50 for coord in xyz:
51 kwds = {coord: 5 * um}
52 soma = Soma(diameter=10 * um, **kwds)
53 # Length shouldn't change (not defined by coordinates but by the diameter)
54 assert_equal(soma.length, [10] * um)
55 assert_equal(soma.distance, [0] * um)
56
57 # Coordinates should be specified now, with 0 values for the other
58 # coordinates
59 for other_coord in xyz - {coord}:
60 assert_equal(getattr(soma, f"start_{other_coord}"), [0] * um)
61 assert_equal(getattr(soma, other_coord), [0] * um)
62 assert_equal(getattr(soma, f"end_{other_coord}"), [0] * um)
63
64 assert_equal(getattr(soma, f"start_{coord}"), [5] * um)
65 assert_equal(getattr(soma, coord), [5] * um)
66 assert_equal(getattr(soma, f"end_{coord}"), [5] * um)
67
68 # Specify all coordinates
69 soma = Soma(diameter=10 * um, x=1 * um, y=2 * um, z=3 * um)
70 # Length shouldn't change (not defined by coordinates but by the diameter)
71 assert_equal(soma.length, [10] * um)
72 assert_equal(soma.distance, [0] * um)
73
74 assert_equal(soma.start_x, 1 * um)
75 assert_equal(soma.x, 1 * um)
76 assert_equal(soma.end_x, 1 * um)
77 assert_equal(soma.start_y, 2 * um)
78 assert_equal(soma.y, 2 * um)
79 assert_equal(soma.end_y, 2 * um)
80 assert_equal(soma.start_z, 3 * um)
81 assert_equal(soma.z, 3 * um)
82 assert_equal(soma.end_z, 3 * um)
83
84
85 @pytest.mark.codegen_independent
86 def test_attributes_cylinder():
87 n = 10
88 cylinder = Cylinder(n=n, diameter=10 * um, length=200 * um)
89 assert isinstance(cylinder, Morphology)
90 # Single section with 10 compartments
91 assert cylinder.n == n
92 assert cylinder.total_sections == 1
93 assert cylinder.total_compartments == n
94 with pytest.raises(TypeError):
95 len(cylinder) # ambiguous
96
97 # Compartment attributes
98 assert_equal(cylinder.diameter, np.ones(n) * 10 * um)
99 assert_equal(cylinder.length, np.ones(n) * 20 * um)
100 assert_equal(cylinder.distance, np.arange(n) * 20 * um + 10 * um)
101 assert_equal(cylinder.end_distance, 200 * um)
102 # TODO: r_length
103 assert_allclose(cylinder.area, np.pi * cylinder.diameter * cylinder.length)
104 assert_allclose(
105 cylinder.volume, 1.0 / 4.0 * np.pi * cylinder.diameter**2 * cylinder.length
106 )
107
108 # No coordinates were specified
109 assert cylinder.start_x is None
110 assert cylinder.start_y is None
111 assert cylinder.start_z is None
112 assert cylinder.x is None
113 assert cylinder.y is None
114 assert cylinder.z is None
115 assert cylinder.end_x is None
116 assert cylinder.end_y is None
117 assert cylinder.end_z is None
118
119
120 @pytest.mark.codegen_independent
121 def test_attributes_cylinder_coordinates():
122 # Specify only the end-point of the section
123 n = 10
124 # Specify only one of the coordinates
125 xyz = {"x", "y", "z"}
126 for coord in xyz:
127 kwds = {coord: [0, 200] * um}
128 cylinder = Cylinder(n=n, diameter=10 * um, **kwds)
129 assert_equal(cylinder.diameter, np.ones(n) * 10 * um)
130 assert_equal(cylinder.length, np.ones(n) * 20 * um)
131 assert_equal(cylinder.distance, np.arange(n) * 20 * um + 10 * um)
132 assert_equal(cylinder.end_distance, 200 * um)
133
134 # Coordinates should be specified now, with 0 values for the other
135 # coordinates
136 for other_coord in xyz - {coord}:
137 assert_equal(getattr(cylinder, f"start_{other_coord}"), np.zeros(n) * um)
138 assert_equal(getattr(cylinder, other_coord), np.zeros(n) * um)
139 assert_equal(getattr(cylinder, f"end_{other_coord}"), np.zeros(n) * um)
140
141 assert_equal(getattr(cylinder, f"start_{coord}"), np.arange(n) * 20 * um)
142 assert_equal(getattr(cylinder, coord), np.arange(n) * 20 * um + 10 * um)
143 assert_equal(
144 getattr(cylinder, f"end_{coord}"), np.arange(n) * 20 * um + 20 * um
145 )
146
147 # Specify all coordinates
148 val = [0, 200.0 / np.sqrt(3.0)] * um
149 cylinder = Cylinder(n=n, diameter=10 * um, x=val, y=val, z=val)
150
151 assert_equal(cylinder.diameter, np.ones(n) * 10 * um)
152 assert_allclose(cylinder.length, np.ones(n) * 20 * um)
153 assert_allclose(cylinder.distance, np.arange(n) * 20 * um + 10 * um)
154 assert_allclose(cylinder.end_distance, 200 * um)
155
156 for coord in ["x", "y", "z"]:
157 assert_allclose(getattr(cylinder, f"start_{coord}"), np.arange(n) * val[1] / n)
158 assert_allclose(
159 getattr(cylinder, coord), np.arange(n) * val[1] / n + 0.5 * val[1] / n
160 )
161 assert_allclose(
162 getattr(cylinder, f"end_{coord}"), np.arange(n) * val[1] / n + val[1] / n
163 )
164
165
166 @pytest.mark.codegen_independent
167 def test_attributes_section():
168 n = 10
169 # No difference to a cylinder
170 sec = Section(n=n, diameter=np.ones(n + 1) * 10 * um, length=np.ones(n) * 20 * um)
171 cyl = Cylinder(n=1, diameter=10 * um, length=0 * um) # dummy cylinder
172 cyl.child = sec
173 assert isinstance(sec, Morphology)
174 # Single section with 10 compartments
175 assert sec.n == n
176 assert sec.total_sections == 1
177 assert sec.total_compartments == n
178 with pytest.raises(TypeError):
179 len(sec) # ambiguous
180
181 # Compartment attributes
182 assert_allclose(sec.diameter, np.ones(n) * 10 * um)
183 assert_allclose(sec.length, np.ones(n) * 20 * um)
184 assert_allclose(sec.distance, np.arange(n) * 20 * um + 10 * um)
185 assert_allclose(sec.end_distance, 200 * um)
186 # TODO: r_length
187 assert_allclose(
188 sec.area, np.pi * 0.5 * (sec.start_diameter + sec.end_diameter) * sec.length
189 )
190 assert_allclose(sec.volume, 1.0 / 4.0 * np.pi * sec.diameter**2 * sec.length)
191
192 # No coordinates were specified
193 assert sec.start_x is None
194 assert sec.start_y is None
195 assert sec.start_z is None
196 assert sec.x is None
197 assert sec.y is None
198 assert sec.z is None
199 assert sec.end_x is None
200 assert sec.end_y is None
201 assert sec.end_z is None
202
203
204 @pytest.mark.codegen_independent
205 def test_attributes_section_coordinates_single():
206 # Specify only the end-point of the section (no difference to cylinder)
207 n = 10
208 # Specify only one of the coordinates
209 xyz = {"x", "y", "z"}
210 for coord in xyz:
211 kwds = {coord: np.linspace(0 * um, 200 * um, n + 1)}
212 sec = Section(n=n, diameter=np.ones(n + 1) * 10 * um, **kwds)
213 cyl = Cylinder(n=1, diameter=10 * um, length=0 * um) # dummy cylinder
214 cyl.child = sec
215 assert_equal(sec.diameter, np.ones(n) * 10 * um)
216 assert_equal(sec.length, np.ones(n) * 20 * um)
217 assert_equal(sec.distance, np.arange(n) * 20 * um + 10 * um)
218 assert_equal(sec.end_distance, 200 * um)
219
220 # Coordinates should be specified now, with 0 values for the other
221 # coordinates
222 for other_coord in xyz - {coord}:
223 assert_equal(getattr(sec, f"start_{other_coord}"), np.zeros(n) * um)
224 assert_equal(getattr(sec, other_coord), np.zeros(n) * um)
225 assert_equal(getattr(sec, f"end_{other_coord}"), np.zeros(n) * um)
226
227 assert_equal(getattr(sec, f"start_{coord}"), np.arange(n) * 20 * um)
228 assert_equal(getattr(sec, coord), np.arange(n) * 20 * um + 10 * um)
229 assert_equal(getattr(sec, f"end_{coord}"), np.arange(n) * 20 * um + 20 * um)
230
231 # Specify all coordinates
232 val = 200.0 / np.sqrt(3.0) * um
233 sec = Section(
234 n=n,
235 diameter=np.ones(n + 1) * 10 * um,
236 x=np.linspace(0 * um, val, n + 1),
237 y=np.linspace(0 * um, val, n + 1),
238 z=np.linspace(0 * um, val, n + 1),
239 )
240 cyl = Cylinder(n=1, diameter=10 * um, length=0 * um)
241 cyl.child = sec
242 assert_equal(sec.diameter, np.ones(n) * 10 * um)
243 assert_allclose(sec.length, np.ones(n) * 20 * um)
244 assert_allclose(sec.distance, np.arange(n) * 20 * um + 10 * um)
245 assert_allclose(sec.end_distance, 200 * um)
246
247 for coord in ["x", "y", "z"]:
248 assert_allclose(getattr(sec, f"start_{coord}"), np.arange(n) * val / n)
249 assert_allclose(getattr(sec, coord), np.arange(n) * val / n + 0.5 * val / n)
250 assert_allclose(getattr(sec, f"end_{coord}"), np.arange(n) * val / n + val / n)
251
252
253 @pytest.mark.codegen_independent
254 def test_attributes_section_coordinates_all():
255 n = 3
256 # Specify all coordinates
257 sec = Section(
258 n=n,
259 diameter=[10, 10, 10, 10] * um,
260 x=[10, 11, 11, 11] * um,
261 y=[100, 100, 101, 101] * um,
262 z=[1000, 1000, 1000, 1001] * um,
263 )
264
265 assert_equal(sec.diameter, np.ones(n) * 10 * um)
266 assert_allclose(sec.length, np.ones(n) * um)
267 assert_allclose(sec.distance, np.arange(n) * um + 0.5 * um)
268 assert_allclose(sec.end_distance, 3 * um)
269
270 assert_allclose(sec.start_x, [10, 11, 11] * um)
271 assert_allclose(sec.x, [10.5, 11, 11] * um)
272 assert_allclose(sec.end_x, [11, 11, 11] * um)
273 assert_allclose(sec.start_y, [100, 100, 101] * um)
274 assert_allclose(sec.y, [100, 100.5, 101] * um)
275 assert_allclose(sec.end_y, [100, 101, 101] * um)
276 assert_allclose(sec.start_z, [1000, 1000, 1000] * um)
277 assert_allclose(sec.z, [1000, 1000, 1000.5] * um)
278 assert_allclose(sec.end_z, [1000, 1000, 1001] * um)
279
280 # Specify varying diameters
281 sec = Section(
282 n=n,
283 diameter=[20, 10, 5, 2.5] * um,
284 x=[0, 1, 1, 1] * um,
285 y=[0, 0, 1, 1] * um,
286 z=[0, 0, 0, 1] * um,
287 )
288 assert_allclose(sec.start_diameter, [20, 10, 5] * um)
289 # diameter at midpoint
290 assert_allclose(sec.diameter, 0.5 * (sec.start_diameter + sec.end_diameter))
291 assert_allclose(sec.end_diameter, [10, 5, 2.5] * um)
292 # TODO: Check area and volume
293
294
295 def _check_tree_cables(morphology, coordinates=False):
296 # number of compartments per section
297 assert morphology.n == 10
298 assert morphology["1"].n == 5
299 assert morphology["2"].n == 5
300 assert morphology["21"].n == 5
301 assert morphology["22"].n == 5
302 # number of compartments per subtree
303 assert morphology.total_compartments == 30
304 assert morphology["1"].total_compartments == 5
305 assert morphology["2"].total_compartments == 15
306 assert morphology["21"].total_compartments == 5
307 assert morphology["22"].total_compartments == 5
308 # number of sections per subtree
309 assert morphology.total_sections == 5
310 assert morphology["1"].total_sections == 1
311 assert morphology["2"].total_sections == 3
312 assert morphology["21"].total_sections == 1
313 assert morphology["22"].total_sections == 1
314 # Check that distances (= distance to root at electrical midpoint)
315 # correctly follow the tree structure
316 assert_allclose(morphology.distance, np.arange(10) * 10 * um + 5 * um)
317 assert_allclose(
318 morphology["2"].distance, 100 * um + np.arange(5) * 10 * um + 5 * um
319 )
320 assert_allclose(
321 morphology["21"].distance, 150 * um + np.arange(5) * 10 * um + 5 * um
322 )
323 assert_allclose(morphology.end_distance, 100 * um)
324 assert_allclose(morphology["1"].end_distance, 200 * um)
325 assert_allclose(morphology["2"].end_distance, 150 * um)
326 assert_allclose(morphology["21"].end_distance, 200 * um)
327 assert_allclose(morphology["22"].end_distance, 200 * um)
328 # Check that section diameters are correctly inherited from the parent
329 # sections
330 assert_allclose(morphology["1"].start_diameter, [10, 8, 6, 4, 2] * um)
331 assert_allclose(morphology["22"].start_diameter, [5, 4, 3, 2, 1] * um)
332
333 if coordinates:
334 # Coordinates should be absolute
335 # section: cable
336 assert_allclose(morphology.start_x, np.arange(10) * 10 * um)
337 assert_allclose(morphology.x, np.arange(10) * 10 * um + 5 * um)
338 assert_allclose(morphology.end_x, np.arange(10) * 10 * um + 10 * um)
339 assert_allclose(morphology.y, np.zeros(10) * um)
340 assert_allclose(morphology.z, np.zeros(10) * um)
341 # section: cable['1']
342 step = 20 / np.sqrt(2) * um
343 assert_allclose(morphology["1"].start_x, 100 * um + np.arange(5) * step)
344 assert_allclose(morphology["1"].x, 100 * um + np.arange(5) * step + step / 2)
345 assert_allclose(morphology["1"].end_x, 100 * um + np.arange(5) * step + step)
346 assert_allclose(morphology["1"].start_y, np.arange(5) * step)
347 assert_allclose(morphology["1"].y, np.arange(5) * step + step / 2)
348 assert_allclose(morphology["1"].end_y, np.arange(5) * step + step)
349 assert_allclose(morphology["1"].z, np.zeros(5) * um)
350 # section: cable['2']
351 step = 10 / np.sqrt(2) * um
352 assert_allclose(morphology["2"].start_x, 100 * um + np.arange(5) * step)
353 assert_allclose(morphology["2"].x, 100 * um + np.arange(5) * step + step / 2)
354 assert_allclose(morphology["2"].end_x, 100 * um + np.arange(5) * step + step)
355 assert_allclose(morphology["2"].start_y, -np.arange(5) * step)
356 assert_allclose(morphology["2"].y, -(np.arange(5) * step + step / 2))
357 assert_allclose(morphology["2"].end_y, -(np.arange(5) * step + step))
358 assert_allclose(morphology["2"].z, np.zeros(5) * um)
359 # section: cable ['21']
360 step = 10 / np.sqrt(2) * um
361 assert_allclose(
362 morphology["21"].start_x,
363 100 * um + 50 / np.sqrt(2) * um + np.arange(5) * step,
364 )
365 assert_allclose(
366 morphology["21"].x,
367 100 * um + 50 / np.sqrt(2) * um + np.arange(5) * step + step / 2,
368 )
369 assert_allclose(
370 morphology["21"].end_x,
371 100 * um + 50 / np.sqrt(2) * um + np.arange(5) * step + step,
372 )
373 assert_allclose(morphology["21"].start_y, -np.ones(5) * 50 / np.sqrt(2) * um)
374 assert_allclose(morphology["21"].y, -np.ones(5) * 50 / np.sqrt(2) * um)
375 assert_allclose(morphology["21"].end_y, -np.ones(5) * 50 / np.sqrt(2) * um)
376 assert_allclose(morphology["21"].start_z, np.arange(5) * step)
377 assert_allclose(morphology["21"].z, np.arange(5) * step + step / 2)
378 assert_allclose(morphology["21"].end_z, np.arange(5) * step + step)
379 # section: cable['22']
380 step = 10 / np.sqrt(2) * um
381 assert_allclose(
382 morphology["22"].start_x,
383 100 * um + 50 / np.sqrt(2) * um + np.arange(5) * step,
384 )
385 assert_allclose(
386 morphology["22"].x,
387 100 * um + 50 / np.sqrt(2) * um + np.arange(5) * step + step / 2,
388 )
389 assert_allclose(
390 morphology["22"].end_x,
391 100 * um + 50 / np.sqrt(2) * um + np.arange(5) * step + step,
392 )
393 assert_allclose(morphology["22"].start_y, -np.ones(5) * 50 / np.sqrt(2) * um)
394 assert_allclose(morphology["22"].y, -np.ones(5) * 50 / np.sqrt(2) * um)
395 assert_allclose(morphology["22"].end_y, -np.ones(5) * 50 / np.sqrt(2) * um)
396 assert_allclose(morphology["22"].start_z, -np.arange(5) * step)
397 assert_allclose(morphology["22"].z, -(np.arange(5) * step + step / 2))
398 assert_allclose(morphology["22"].end_z, -(np.arange(5) * step + step))
399
400
401 @pytest.mark.codegen_independent
402 def test_tree_cables_schematic():
403 cable = Cylinder(n=10, diameter=10 * um, length=100 * um)
404 cable.L = Section(
405 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
406 ) # tapering truncated cones
407 cable.R = Cylinder(n=5, diameter=5 * um, length=50 * um)
408 cable.RL = Cylinder(n=5, diameter=2.5 * um, length=50 * um)
409 cable.RR = Section(
410 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
411 )
412
413 _check_tree_cables(cable)
414
415
416 @pytest.mark.codegen_independent
417 def test_tree_cables_coordinates():
418 # The lengths of the sections should be identical to the previous test
419 cable = Cylinder(n=10, x=[0, 100] * um, diameter=10 * um)
420 cable.L = Section(
421 n=5,
422 diameter=[10, 8, 6, 4, 2, 0] * um,
423 x=np.linspace(0, 100, 6) / np.sqrt(2) * um,
424 y=np.linspace(0, 100, 6) / np.sqrt(2) * um,
425 )
426 cable.R = Cylinder(
427 n=5, diameter=5 * um, x=[0, 50] * um / np.sqrt(2), y=[0, -50] * um / np.sqrt(2)
428 )
429 cable.RL = Cylinder(
430 n=5, diameter=2.5 * um, x=[0, 50] * um / np.sqrt(2), z=[0, 50] * um / np.sqrt(2)
431 )
432 cable.RR = Section(
433 n=5,
434 diameter=[5, 4, 3, 2, 1, 0] * um,
435 x=np.linspace(0, 50, 6) * um / np.sqrt(2),
436 z=np.linspace(0, -50, 6) * um / np.sqrt(2),
437 )
438
439 _check_tree_cables(cable, coordinates=True)
440
441
442 @pytest.mark.codegen_independent
443 def test_tree_cables_from_points():
444 # The coordinates should be identical to the previous test
445 # fmt: off
446 points = [ # cable
447 (1, None, 0, 0, 0, 10, -1),
448 (2, None, 10, 0, 0, 10, 1),
449 (3, None, 20, 0, 0, 10, 2),
450 (4, None, 30, 0, 0, 10, 3),
451 (5, None, 40, 0, 0, 10, 4),
452 (6, None, 50, 0, 0, 10, 5),
453 (7, None, 60, 0, 0, 10, 6),
454 (8, None, 70, 0, 0, 10, 7),
455 (9, None, 80, 0, 0, 10, 8),
456 (10, None, 90, 0, 0, 10, 9),
457 (11, None, 100, 0, 0, 10, 10),
458 # cable.L (using automatic names)
459 (12, None, 100+20/np.sqrt(2), 20/np.sqrt(2), 0, 8 , 11),
460 (13, None, 100+40/np.sqrt(2), 40/np.sqrt(2), 0, 6 , 12),
461 (14, None, 100+60/np.sqrt(2), 60/np.sqrt(2), 0, 4 , 13),
462 (15, None, 100+80/np.sqrt(2), 80/np.sqrt(2), 0, 2 , 14),
463 (16, None, 100+100/np.sqrt(2), 100/np.sqrt(2), 0, 0 , 15),
464 # cable.R (using automatic names)
465 (17, None, 100+10/np.sqrt(2), -10/np.sqrt(2), 0, 5 , 11),
466 (18, None, 100+20/np.sqrt(2), -20/np.sqrt(2), 0, 5 , 17),
467 (19, None, 100+30/np.sqrt(2), -30/np.sqrt(2), 0, 5 , 18),
468 (20, None, 100+40/np.sqrt(2), -40/np.sqrt(2), 0, 5 , 19),
469 (21, None, 100+50/np.sqrt(2), -50/np.sqrt(2), 0, 5 , 20),
470 # cable.RL (using explicit names)
471 (22, 'L' , 100+60/np.sqrt(2), -50/np.sqrt(2), 10/np.sqrt(2), 2.5, 21),
472 (23, 'L' , 100+70/np.sqrt(2), -50/np.sqrt(2), 20/np.sqrt(2), 2.5, 22),
473 (24, 'L' , 100+80/np.sqrt(2), -50/np.sqrt(2), 30/np.sqrt(2), 2.5, 23),
474 (25, 'L' , 100+90/np.sqrt(2), -50/np.sqrt(2), 40/np.sqrt(2), 2.5, 24),
475 (26, 'L' , 100+100/np.sqrt(2), -50/np.sqrt(2), 50/np.sqrt(2), 2.5, 25),
476 # cable.RR (using explicit names)
477 (27, 'R' , 100+60/np.sqrt(2), -50/np.sqrt(2), -10/np.sqrt(2), 4, 21),
478 (28, 'R' , 100+70/np.sqrt(2), -50/np.sqrt(2), -20/np.sqrt(2), 3, 27),
479 (29, 'R' , 100+80/np.sqrt(2), -50/np.sqrt(2), -30/np.sqrt(2), 2, 28),
480 (30, 'R' , 100+90/np.sqrt(2), -50/np.sqrt(2), -40/np.sqrt(2), 1, 29),
481 (31, 'R' , 100+100/np.sqrt(2), -50/np.sqrt(2), -50/np.sqrt(2), 0, 30),
482 ]
483 # fmt: on
484 cable = Morphology.from_points(points)
485
486 # Check that the names are used
487 assert cable.L.n == 5
488 assert cable.R.n == 5
489 assert cable.RL.n == 5
490 assert cable.RR.n == 5
491 _check_tree_cables(cable, coordinates=True)
492
493
494 def test_tree_cables_from_swc():
495 swc_content = """
496 # Test file
497 1 0 0 0 0 5 -1
498 2 0 10 0 0 5 1
499 3 0 20 0 0 5 2
500 4 0 30 0 0 5 3
501 5 0 40 0 0 5 4
502 6 0 50 0 0 5 5
503 7 0 60 0 0 5 6
504 8 0 70 0 0 5 7
505 9 0 80 0 0 5 8
506 10 0 90 0 0 5 9
507 11 0 100 0 0 5 10
508 12 2 114.14213562373095 14.142135623730949 0 4 11
509 13 2 128.2842712474619 28.284271247461898 0 3 12
510 14 2 142.42640687119285 42.426406871192846 0 2 13
511 15 2 156.5685424949238 56.568542494923797 0 1 14
512 16 2 170.71067811865476 70.710678118654741 0 0 15
513 17 2 107.07106781186548 -7.0710678118654746 0 2.5 11
514 18 2 114.14213562373095 -14.142135623730949 0 2.5 17
515 19 2 121.21320343559643 -21.213203435596423 0 2.5 18
516 20 2 128.2842712474619 -28.284271247461898 0 2.5 19
517 21 2 135.35533905932738 -35.35533905932737 0 2.5 20
518 22 2 142.42640687119285 -35.35533905932737 7.0710678118654746 1.25 21
519 23 2 149.49747468305833 -35.35533905932737 14.142135623730949 1.25 22
520 24 2 156.5685424949238 -35.35533905932737 21.213203435596423 1.25 23
521 25 2 163.63961030678928 -35.35533905932737 28.284271247461898 1.25 24
522 26 2 170.71067811865476 -35.35533905932737 35.35533905932737 1.25 25
523 27 2 142.42640687119285 -35.35533905932737 -7.0710678118654746 2 21
524 28 2 149.49747468305833 -35.35533905932737 -14.142135623730949 1.5 27
525 29 2 156.5685424949238 -35.35533905932737 -21.213203435596423 1 28
526 30 2 163.63961030678928 -35.35533905932737 -28.284271247461898 0.5 29
527 31 2 170.71067811865476 -35.35533905932737 -35.35533905932737 0 30
528 """
529 tmp_filename = tempfile.mktemp("cable_morphology.swc")
530 with open(tmp_filename, "w") as f:
531 f.write(swc_content)
532 cable = Morphology.from_file(tmp_filename)
533 os.remove(tmp_filename)
534 _check_tree_cables(cable, coordinates=True)
535
536
537 def _check_tree_soma(morphology, coordinates=False, use_cylinders=True):
538 # number of compartments per section
539 assert morphology.n == 1
540 assert morphology["1"].n == 5
541 assert morphology["2"].n == 5
542
543 # number of compartments per subtree
544 assert morphology.total_compartments == 11
545 assert morphology["1"].total_compartments == 5
546 assert morphology["2"].total_compartments == 5
547
548 # number of sections per subtree
549 assert morphology.total_sections == 3
550 assert morphology["1"].total_sections == 1
551 assert morphology["2"].total_sections == 1
552
553 assert_allclose(morphology.diameter, [30] * um)
554
555 # Check that distances (= distance to root at midpoint)
556 # correctly follow the tree structure
557 # Note that the soma does add nothing to the distance
558 assert_equal(morphology.distance, 0 * um)
559 assert_allclose(morphology["1"].distance, np.arange(5) * 20 * um + 10 * um)
560 assert_allclose(morphology["2"].distance, np.arange(5) * 10 * um + 5 * um)
561 assert_allclose(morphology.end_distance, 0 * um)
562 assert_allclose(morphology["1"].end_distance, 100 * um)
563 assert_allclose(morphology["2"].end_distance, 50 * um)
564
565 assert_allclose(morphology.diameter, 30 * um)
566 assert_allclose(morphology["1"].start_diameter, [8, 8, 6, 4, 2] * um)
567 assert_allclose(morphology["1"].diameter, [8, 7, 5, 3, 1] * um)
568 assert_allclose(morphology["1"].end_diameter, [8, 6, 4, 2, 0] * um)
569 assert_allclose(morphology["2"].start_diameter, np.ones(5) * 5 * um)
570 assert_allclose(morphology["2"].diameter, np.ones(5) * 5 * um)
571 assert_allclose(morphology["2"].end_diameter, np.ones(5) * 5 * um)
572
573 if coordinates:
574 # Coordinates should be absolute
575 # section: soma
576 assert_allclose(morphology.start_x, 100 * um)
577 assert_allclose(morphology.x, 100 * um)
578 assert_allclose(morphology.end_x, 100 * um)
579 assert_allclose(morphology.y, 0 * um)
580 assert_allclose(morphology.z, 0 * um)
581 # section: cable['1']
582 step = 20 / np.sqrt(2) * um
583 assert_allclose(morphology["1"].start_x, 100 * um + np.arange(5) * step)
584 assert_allclose(morphology["1"].x, 100 * um + np.arange(5) * step + step / 2)
585 assert_allclose(morphology["1"].end_x, 100 * um + np.arange(5) * step + step)
586 assert_allclose(morphology["1"].start_y, np.arange(5) * step)
587 assert_allclose(morphology["1"].y, np.arange(5) * step + step / 2)
588 assert_allclose(morphology["1"].end_y, np.arange(5) * step + step)
589 assert_allclose(morphology["1"].z, np.zeros(5) * um)
590 # section: cable['2']
591 step = 10 / np.sqrt(2) * um
592 assert_allclose(morphology["2"].start_x, 100 * um + np.arange(5) * step)
593 if use_cylinders:
594 assert_allclose(
595 morphology["2"].x, 100 * um + np.arange(5) * step + step / 2
596 )
597 assert_allclose(morphology["2"].end_x, 100 * um + np.arange(5) * step + step)
598 assert_allclose(morphology["2"].start_y, -np.arange(5) * step)
599 if use_cylinders:
600 assert_allclose(morphology["2"].y, -(np.arange(5) * step + step / 2))
601 assert_allclose(morphology["2"].end_y, -(np.arange(5) * step + step))
602 if use_cylinders:
603 assert_allclose(morphology["2"].z, np.zeros(5) * um)
604
605
606 @pytest.mark.codegen_independent
607 def test_tree_soma_schematic():
608 soma = Soma(diameter=30 * um)
609 soma.L = Section(
610 n=5, diameter=[8, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
611 ) # tapering truncated cones
612 soma.R = Cylinder(n=5, diameter=5 * um, length=50 * um)
613
614 _check_tree_soma(soma)
615
616
617 @pytest.mark.codegen_independent
618 def test_tree_soma_coordinates():
619 soma = Soma(diameter=30 * um, x=100 * um)
620 soma.L = Section(
621 n=5,
622 diameter=[8, 8, 6, 4, 2, 0] * um,
623 x=np.linspace(0, 100, 6) / np.sqrt(2) * um,
624 y=np.linspace(0, 100, 6) / np.sqrt(2) * um,
625 ) # tapering truncated cones
626 soma.R = Cylinder(
627 n=5, diameter=5 * um, x=[0, 50] * um / np.sqrt(2), y=[0, -50] * um / np.sqrt(2)
628 )
629
630 _check_tree_soma(soma, coordinates=True)
631
632
633 @pytest.mark.codegen_independent
634 def test_tree_soma_from_points():
635 # The coordinates should be identical to the previous test
636 # fmt: on
637 points = [ # soma
638 (1, "soma", 100, 0, 0, 30, -1),
639 # soma.L
640 (2, "L", 100 + 20 / np.sqrt(2), 20 / np.sqrt(2), 0, 8, 1),
641 (3, "L", 100 + 40 / np.sqrt(2), 40 / np.sqrt(2), 0, 6, 2),
642 (4, "L", 100 + 60 / np.sqrt(2), 60 / np.sqrt(2), 0, 4, 3),
643 (5, "L", 100 + 80 / np.sqrt(2), 80 / np.sqrt(2), 0, 2, 4),
644 (6, "L", 100 + 100 / np.sqrt(2), 100 / np.sqrt(2), 0, 0, 5),
645 # soma.R
646 (7, "R", 100 + 10 / np.sqrt(2), -10 / np.sqrt(2), 0, 5, 1),
647 (8, "R", 100 + 20 / np.sqrt(2), -20 / np.sqrt(2), 0, 5, 7),
648 (9, "R", 100 + 30 / np.sqrt(2), -30 / np.sqrt(2), 0, 5, 8),
649 (10, "R", 100 + 40 / np.sqrt(2), -40 / np.sqrt(2), 0, 5, 9),
650 (11, "R", 100 + 50 / np.sqrt(2), -50 / np.sqrt(2), 0, 5, 10),
651 ]
652 # fmt: on
653 cable = Morphology.from_points(points)
654 _check_tree_soma(cable, coordinates=True, use_cylinders=False)
655
656
657 @pytest.mark.codegen_independent
658 def test_tree_soma_from_points_3_point_soma():
659 # The coordinates should be identical to the previous test
660 # fmt: off
661 points = [ # soma
662 (1, 'soma', 100, 0, 0, 30, -1),
663 (2, 'soma', 100, 15, 0, 30, 1),
664 (3, 'soma', 100, -15, 0, 30, 1),
665 # soma.L
666 (4, 'L' , 100+20/np.sqrt(2), 20/np.sqrt(2), 0, 8 , 1),
667 (5, 'L' , 100+40/np.sqrt(2), 40/np.sqrt(2), 0, 6 , 4),
668 (6, 'L' , 100+60/np.sqrt(2), 60/np.sqrt(2), 0, 4 , 5),
669 (7, 'L' , 100+80/np.sqrt(2), 80/np.sqrt(2), 0, 2 , 6),
670 (8, 'L' , 100+100/np.sqrt(2), 100/np.sqrt(2), 0, 0 , 7),
671 # soma.R
672 (9, 'R' , 100+10/np.sqrt(2), -10/np.sqrt(2), 0, 5 , 1),
673 (10, 'R' , 100+20/np.sqrt(2), -20/np.sqrt(2), 0, 5 , 9),
674 (11, 'R' , 100+30/np.sqrt(2), -30/np.sqrt(2), 0, 5 , 10),
675 (12, 'R' , 100+40/np.sqrt(2), -40/np.sqrt(2), 0, 5 , 11),
676 (13, 'R' , 100+50/np.sqrt(2), -50/np.sqrt(2), 0, 5 , 12),
677 ]
678 # fmt: on
679 cable = Morphology.from_points(points)
680 _check_tree_soma(cable, coordinates=True, use_cylinders=False)
681 # The first compartment should be a spherical soma!
682 assert isinstance(cable, Soma)
683
684
685 @pytest.mark.codegen_independent
686 def test_tree_soma_from_points_3_point_soma_incorrect():
687 # Inconsistent diameters
688 # fmt: off
689 points = [ # soma
690 (1, 'soma', 100, 0, 0, 30, -1),
691 (2, 'soma', 100, 15, 0, 28, 1),
692 (3, 'soma', 100, -15, 0, 30, 1),
693 # soma.L
694 (4, 'L' , 100+20/np.sqrt(2), 20/np.sqrt(2), 0, 8 , 1),
695 (5, 'L' , 100+40/np.sqrt(2), 40/np.sqrt(2), 0, 6 , 4),
696 (6, 'L' , 100+60/np.sqrt(2), 60/np.sqrt(2), 0, 4 , 5),
697 (7, 'L' , 100+80/np.sqrt(2), 80/np.sqrt(2), 0, 2 , 6),
698 (8, 'L' , 100+100/np.sqrt(2), 100/np.sqrt(2), 0, 0 , 7)
699 ]
700 # fmt: on
701 with pytest.raises(ValueError):
702 Morphology.from_points(points)
703
704 # Inconsistent coordinates
705 # fmt: off
706 points = [ # soma
707 (1, 'soma', 100, 0, 0, 30, -1),
708 (2, 'soma', 100, 15, 0, 30, 1),
709 (3, 'soma', 100, -16, 0, 30, 1),
710 # soma.L
711 (4, 'L', 100 + 20 / np.sqrt(2), 20 / np.sqrt(2), 0, 8, 1),
712 (5, 'L', 100 + 40 / np.sqrt(2), 40 / np.sqrt(2), 0, 6, 4),
713 (6, 'L', 100 + 60 / np.sqrt(2), 60 / np.sqrt(2), 0, 4, 5),
714 (7, 'L', 100 + 80 / np.sqrt(2), 80 / np.sqrt(2), 0, 2, 6),
715 (8, 'L', 100 + 100 / np.sqrt(2), 100 / np.sqrt(2), 0, 0, 7)
716 ]
717 # fmt: on
718 with pytest.raises(ValueError):
719 Morphology.from_points(points)
720
721
722 @pytest.mark.codegen_independent
723 def test_tree_soma_from_points_somas_not_at_start():
724 # A single soma in the middle (ununusal but possible)
725 points = [ # dendrite
726 (1, "dend", 100, 0, 0, 10, -1),
727 (2, "dend", 100, 0, 0, 10, 1),
728 (3, "dend", 100, 0, 0, 10, 2),
729 (4, "soma", 100, 0, 0, 30, 3),
730 (5, "dend2", 130, 0, 0, 10, 4),
731 (6, "dend2", 160, 0, 0, 10, 5),
732 ]
733 morpho = Morphology.from_points(points)
734 assert morpho.total_sections == 3
735 assert morpho.total_compartments == 5
736
737 # Several somata (probably an error)
738 points = [ # dendrite
739 (1, "dend", 0, 0, 0, 10, -1),
740 (2, "dend", 30, 0, 0, 10, 1),
741 (3, "dend", 60, 0, 0, 10, 2),
742 (4, "soma", 90, 0, 0, 30, 3),
743 (5, "dend2", 120, 70, 0, 10, 4),
744 (6, "dend2", 150, 70, 0, 10, 5),
745 (7, "soma", 180, 40, 0, 30, 6),
746 ]
747 with catch_logs() as logs:
748 morpho = Morphology.from_points(points)
749 assert len(logs) == 1
750 assert logs[0][1].endswith("soma_compartments")
751 assert morpho.total_sections == 4
752 assert morpho.total_compartments == 6
753
754
755 @pytest.mark.codegen_independent
756 def test_tree_soma_from_swc():
757 swc_content = """
758 # Test file
759 1 1 100 0 0 15 -1
760 2 2 114.14213562373095 14.142135623730949 0 4 1
761 3 2 128.2842712474619 28.284271247461898 0 3 2
762 4 2 142.42640687119285 42.426406871192846 0 2 3
763 5 2 156.5685424949238 56.568542494923797 0 1 4
764 6 2 170.71067811865476 70.710678118654741 0 0 5
765 7 2 107.07106781186548 -7.0710678118654746 0 2.5 1
766 8 2 114.14213562373095 -14.142135623730949 0 2.5 7
767 9 2 121.21320343559643 -21.213203435596423 0 2.5 8
768 10 2 128.2842712474619 -28.284271247461898 0 2.5 9
769 11 2 135.35533905932738 -35.35533905932737 0 2.5 10
770 """
771 tmp_filename = tempfile.mktemp("cable_morphology.swc")
772 with open(tmp_filename, "w") as f:
773 f.write(swc_content)
774 soma = Morphology.from_file(tmp_filename)
775 os.remove(tmp_filename)
776 _check_tree_soma(soma, coordinates=True, use_cylinders=False)
777
778
779 @pytest.mark.codegen_independent
780 def test_tree_soma_from_swc_3_point_soma():
781 swc_content = """
782 # Test file
783 1 1 100 0 0 15 -1
784 2 1 100 15 0 15 1
785 3 1 100 -15 0 15 1
786 4 2 114.14213562373095 14.142135623730949 0 4 1
787 5 2 128.2842712474619 28.284271247461898 0 3 4
788 6 2 142.42640687119285 42.426406871192846 0 2 5
789 7 2 156.5685424949238 56.568542494923797 0 1 6
790 8 2 170.71067811865476 70.710678118654741 0 0 7
791 9 2 107.07106781186548 -7.0710678118654746 0 2.5 1
792 10 2 114.14213562373095 -14.142135623730949 0 2.5 9
793 11 2 121.21320343559643 -21.213203435596423 0 2.5 10
794 12 2 128.2842712474619 -28.284271247461898 0 2.5 11
795 13 2 135.35533905932738 -35.35533905932737 0 2.5 12
796 """
797 tmp_filename = tempfile.mktemp("cable_morphology.swc")
798 with open(tmp_filename, "w") as f:
799 f.write(swc_content)
800 soma = Morphology.from_file(tmp_filename)
801 os.remove(tmp_filename)
802 _check_tree_soma(soma, coordinates=True, use_cylinders=False)
803
804
805 @pytest.mark.codegen_independent
806 def test_tree_soma_from_swc_3_point_soma_incorrect_points():
807 swc_content = """
808 # Test file
809 1 1 100 0 0 15 -1
810 2 1 100 15 0 15 1
811 3 1 100 -10 0 15 1
812 4 2 114.14213562373095 14.142135623730949 0 4 1
813 5 2 128.2842712474619 28.284271247461898 0 3 4
814 6 2 142.42640687119285 42.426406871192846 0 2 5
815 7 2 156.5685424949238 56.568542494923797 0 1 6
816 8 2 170.71067811865476 70.710678118654741 0 0 7
817 9 2 107.07106781186548 -7.0710678118654746 0 2.5 1
818 10 2 114.14213562373095 -14.142135623730949 0 2.5 9
819 11 2 121.21320343559643 -21.213203435596423 0 2.5 10
820 12 2 128.2842712474619 -28.284271247461898 0 2.5 11
821 13 2 135.35533905932738 -35.35533905932737 0 2.5 12
822 """
823 tmp_filename = tempfile.mktemp("cable_morphology.swc")
824 with open(tmp_filename, "w") as f:
825 f.write(swc_content)
826 with pytest.raises(ValueError, match=r".*radius is 15.000um.*"):
827 Morphology.from_file(tmp_filename)
828 os.remove(tmp_filename)
829
830
831 @pytest.mark.codegen_independent
832 def test_tree_soma_from_swc_3_point_soma_incorrect_radius():
833 swc_content = """
834 # Test file
835 1 1 100 0 0 15 -1
836 2 1 100 15 0 10 1
837 3 1 100 -15 0 15 1
838 4 2 114.14213562373095 14.142135623730949 0 4 1
839 5 2 128.2842712474619 28.284271247461898 0 3 4
840 6 2 142.42640687119285 42.426406871192846 0 2 5
841 7 2 156.5685424949238 56.568542494923797 0 1 6
842 8 2 170.71067811865476 70.710678118654741 0 0 7
843 9 2 107.07106781186548 -7.0710678118654746 0 2.5 1
844 10 2 114.14213562373095 -14.142135623730949 0 2.5 9
845 11 2 121.21320343559643 -21.213203435596423 0 2.5 10
846 12 2 128.2842712474619 -28.284271247461898 0 2.5 11
847 13 2 135.35533905932738 -35.35533905932737 0 2.5 12
848 """
849 tmp_filename = tempfile.mktemp("cable_morphology.swc")
850 with open(tmp_filename, "w") as f:
851 f.write(swc_content)
852 with pytest.raises(ValueError, match=r".*not all the diameters are identical.*"):
853 Morphology.from_file(tmp_filename)
854 os.remove(tmp_filename)
855
856
857 @pytest.mark.codegen_independent
858 def test_construction_incorrect_arguments():
859 ### Morphology
860 dummy_self = Soma(10 * um) # To allow testing of Morphology.__init__
861 with pytest.raises(TypeError):
862 Morphology.__init__(dummy_self, n=1.5)
863 with pytest.raises(ValueError):
864 Morphology.__init__(dummy_self, n=0)
865 with pytest.raises(TypeError):
866 Morphology.__init__(dummy_self, "filename.swc")
867
868 ### Soma
869 with pytest.raises(DimensionMismatchError):
870 Soma(10)
871 with pytest.raises(TypeError):
872 Soma([10, 20] * um)
873 with pytest.raises(TypeError):
874 Soma(x=[10, 20] * um)
875 with pytest.raises(TypeError):
876 Soma(y=[10, 20] * um)
877 with pytest.raises(TypeError):
878 Soma(z=[10, 20] * um)
879 with pytest.raises(DimensionMismatchError):
880 Soma(x=10)
881 with pytest.raises(DimensionMismatchError):
882 Soma(y=10)
883 with pytest.raises(DimensionMismatchError):
884 Soma(z=10)
885
886 ### Cylinder
887 # Diameter can only be single value
888 with pytest.raises(TypeError):
889 Cylinder(n=3, diameter=[10, 20] * um, length=100 * um)
890 with pytest.raises(TypeError):
891 Cylinder(n=3, diameter=[10, 20, 30] * um, length=100 * um)
892 with pytest.raises(TypeError):
893 Cylinder(n=3, diameter=np.ones(3, 2) * um, length=100 * um)
894 # Length can only be single value
895 with pytest.raises(TypeError):
896 Cylinder(n=3, diameter=10 * um, length=[10, 20] * um)
897 with pytest.raises(TypeError):
898 Cylinder(n=3, diameter=10 * um, length=[10, 20, 30] * um)
899 with pytest.raises(TypeError):
900 Cylinder(n=3, diameter=10 * um, length=np.ones(3, 2) * um)
901 # Coordinates have to be two values
902 with pytest.raises(TypeError):
903 Cylinder(n=3, diameter=10 * um, x=[10] * um)
904 with pytest.raises(TypeError):
905 Cylinder(n=3, diameter=10 * um, x=[10, 20, 30] * um)
906 with pytest.raises(TypeError):
907 Cylinder(n=3, diameter=10 * um, y=[10] * um)
908 with pytest.raises(TypeError):
909 Cylinder(n=3, diameter=10 * um, y=[10, 20, 30] * um)
910 with pytest.raises(TypeError):
911 Cylinder(n=3, diameter=10 * um, z=[10] * um)
912 with pytest.raises(TypeError):
913 Cylinder(n=3, diameter=10 * um, z=[10, 20, 30] * um)
914 # Need either coordinates or lengths
915 with pytest.raises(TypeError):
916 Cylinder(n=3, diameter=10 * um)
917 # But not both
918 with pytest.raises(TypeError):
919 Cylinder(n=3, diameter=10 * um, length=30 * um, x=[0, 30] * um)
920
921 ### Section
922 # Diameter have to be n+1 values
923 with pytest.raises(TypeError):
924 Section(n=3, diameter=10 * um, length=np.ones(3) * 10 * um)
925 with pytest.raises(TypeError):
926 Section(n=3, diameter=[10, 20, 30] * um, length=np.ones(3) * 10 * um)
927 with pytest.raises(TypeError):
928 Section(n=3, diameter=np.ones(4, 2) * um, length=np.ones(3) * 10 * um)
929 # Length have to be n values
930 with pytest.raises(TypeError):
931 Section(n=3, diameter=np.ones(4) * 10 * um, length=10 * um)
932 with pytest.raises(TypeError):
933 Section(n=3, diameter=np.ones(4) * 10 * um, length=[10, 20] * um)
934 with pytest.raises(TypeError):
935 Section(n=3, diameter=np.ones(4) * 10 * um, length=np.ones(3, 2) * um)
936 # Coordinates have to be n+1 values
937 with pytest.raises(TypeError):
938 Section(n=3, diameter=np.ones(4) * 10 * um, x=10 * um)
939 with pytest.raises(TypeError):
940 Section(n=3, diameter=np.ones(4) * 10 * um, x=[10, 20, 30] * um)
941 with pytest.raises(TypeError):
942 Section(n=3, diameter=np.ones(4) * 10 * um, y=10 * um)
943 with pytest.raises(TypeError):
944 Section(n=3, diameter=np.ones(4) * 10 * um, y=[10, 20, 30] * um)
945 with pytest.raises(TypeError):
946 Section(n=3, diameter=np.ones(4) * 10 * um, z=10 * um)
947 with pytest.raises(TypeError):
948 Section(n=3, diameter=np.ones(4) * 10 * um, z=[10, 20, 30] * um)
949 # Need either coordinates or lengths
950 with pytest.raises(TypeError):
951 Section(n=3, diameter=np.ones(4) * 10 * um)
952 # But not both
953 with pytest.raises(TypeError):
954 Section(
955 n=3,
956 diameter=np.ones(4) * 10 * um,
957 length=[10, 20, 30] * um,
958 x=[0, 10, 20, 30] * um,
959 )
960
961
962 @pytest.mark.codegen_independent
963 def test_from_points_long_chain():
964 # in previous versions, this led to a recursion error
965 points = [(1, "soma", 0, 0, 0, 1, -1)]
966 for i in range(2, 10000):
967 points.append((i, "dend", 0 + i * 10, 0, 0, 5, i - 1))
968 morph = Morphology.from_points(points)
969 assert (
970 morph.total_compartments == 10000 - 1
971 ) # compartments are in-between the points
972 assert morph.total_sections == 2
973
974
975 @pytest.mark.codegen_independent
976 def test_from_points_minimal():
977 points = [(1, "soma", 10, 20, 30, 30, -1)]
978 morph = Morphology.from_points(points)
979 assert morph.total_compartments == 1
980 assert_allclose(morph.diameter, 30 * um)
981 assert_allclose(morph.x, 10 * um)
982 assert_allclose(morph.y, 20 * um)
983 assert_allclose(morph.z, 30 * um)
984
985
986 @pytest.mark.codegen_independent
987 def test_from_points_incorrect():
988 # The coordinates should be identical to the previous test
989 # fmt: off
990 points = [
991 (1, None, 0, 0, 0, 10, -1),
992 (2, None, 10, 0, 0, 10, 1),
993 (2, None, 20, 0, 0, 10, 2),
994 ]
995 points2 = [
996 (1, None, 0, 0, 0, 10, -1),
997 (2, None, 10, 0, 0, 10, 1),
998 (3, None, 20, 0, 0, 10, 3),
999 ]
1000 points3 = [
1001 (1, None, 0, 0, 0, 10, -1),
1002 (2, None, 10, 0, 0, 10, 1),
1003 (3, None, 20, 0, 0, 10, 4),
1004 ]
1005 points4 = [
1006 (1, 0, 0, 0, 10, -1),
1007 (2, 10, 0, 0, 10, 1),
1008 (3, 20, 0, 0, 10, 2),
1009 ]
1010 with pytest.raises(ValueError):
1011 Morphology.from_points(points)
1012 with pytest.raises(ValueError):
1013 Morphology.from_points(points2)
1014 with pytest.raises(ValueError):
1015 Morphology.from_points(points3)
1016 with pytest.raises(ValueError):
1017 Morphology.from_points(points4)
1018
1019
1020 @pytest.mark.codegen_independent
1021 def test_subtree_deletion():
1022 soma = Soma(diameter=30 * um)
1023 first_dendrite = Cylinder(n=5, diameter=5 * um, length=50 * um)
1024 second_dendrite = Cylinder(n=5, diameter=5 * um, length=50 * um)
1025 second_dendrite.L = Cylinder(n=5, diameter=5 * um, length=50 * um)
1026 second_dendrite.R = Cylinder(n=5, diameter=5 * um, length=50 * um)
1027 soma.dend1 = first_dendrite
1028 soma.dend2 = second_dendrite
1029 soma.dend3 = Cylinder(n=5, diameter=5 * um, length=50 * um)
1030 soma.dend3.L = Cylinder(n=5, diameter=5 * um, length=50 * um)
1031 soma.dend3.L.L = Cylinder(n=5, diameter=5 * um, length=50 * um)
1032
1033 assert soma.total_compartments == 36
1034
1035 del soma.dend1
1036 assert soma.total_compartments == 31
1037 with pytest.raises(AttributeError):
1038 soma.dend1
1039 with pytest.raises(AttributeError):
1040 delattr(soma, "dend1")
1041 with pytest.raises(AttributeError):
1042 soma.__delitem__("dend1")
1043 assert first_dendrite not in soma.children
1044
1045 del soma["dend2"]
1046 assert soma.total_compartments == 16
1047 with pytest.raises(AttributeError):
1048 soma.dend2
1049 assert second_dendrite not in soma.children
1050
1051 del soma.dend3.LL
1052 assert soma.total_compartments == 11
1053 with pytest.raises(AttributeError):
1054 soma.dend3.LL
1055 with pytest.raises(AttributeError):
1056 soma.dend3.L.L
1057
1058
1059 @pytest.mark.codegen_independent
1060 def test_subgroup_indices():
1061 morpho = Soma(diameter=30 * um)
1062 morpho.L = Cylinder(length=10 * um, diameter=1 * um, n=10)
1063 morpho.LL = Cylinder(length=5 * um, diameter=2 * um, n=5)
1064 morpho.right = Cylinder(length=3 * um, diameter=1 * um, n=7)
1065
1066 assert_equal(morpho.LL.indices[:], [11, 12, 13, 14, 15])
1067 assert_equal(morpho.L.indices[3 * um : 5 * um], [4, 5])
1068 assert_equal(
1069 morpho.L.indices[3 * um : 5 * um], morpho.L[3 * um : 5 * um].indices[:]
1070 )
1071 assert_equal(morpho.L.indices[: 5 * um], [1, 2, 3, 4, 5])
1072 assert_equal(morpho.L.indices[3 * um :], [4, 5, 6, 7, 8, 9, 10])
1073 assert_equal(morpho.L.indices[3.5 * um], 4)
1074 assert_equal(morpho.L.indices[3 * um], 4)
1075 assert_equal(morpho.L.indices[3.9 * um], 4)
1076 assert_equal(morpho.L.indices[3], 4)
1077 assert_equal(morpho.L.indices[-1], 10)
1078 assert_equal(morpho.L.indices[3:5], [4, 5])
1079 assert_equal(morpho.L.indices[3:], [4, 5, 6, 7, 8, 9, 10])
1080 assert_equal(morpho.L.indices[:5], [1, 2, 3, 4, 5])
1081
1082
1083 @pytest.mark.codegen_independent
1084 def test_subgroup_attributes():
1085 morpho = Soma(diameter=30 * um)
1086 morpho.L = Cylinder(length=10 * um, diameter=1 * um, n=10)
1087 morpho.LL = Cylinder(x=[0, 5] * um, diameter=2 * um, n=5)
1088 morpho.right = Cylinder(length=3 * um, diameter=1 * um, n=7)
1089
1090 # # Getting a single compartment by index
1091 assert_allclose(morpho.L[2].area, morpho.L.area[2])
1092 assert_allclose(morpho.L[2].volume, morpho.L.volume[2])
1093 assert_allclose(morpho.L[2].length, morpho.L.length[2])
1094 assert_allclose(morpho.L[2].r_length_1, morpho.L.r_length_1[2])
1095 assert_allclose(morpho.L[2].r_length_2, morpho.L.r_length_2[2])
1096 assert_allclose(morpho.L[2].distance, morpho.L.distance[2])
1097 assert_allclose(morpho.L[2].diameter, morpho.L.diameter[2])
1098 assert morpho.L[2].x is None
1099 assert morpho.L[2].y is None
1100 assert morpho.L[2].z is None
1101 assert morpho.L[2].start_x is None
1102 assert morpho.L[2].start_y is None
1103 assert morpho.L[2].start_z is None
1104 assert morpho.L[2].end_x is None
1105 assert morpho.L[2].end_y is None
1106 assert morpho.L[2].end_z is None
1107
1108 # # Getting a single compartment by position
1109 assert_allclose(morpho.LL[1.5 * um].area, morpho.LL.area[1])
1110 assert_allclose(morpho.LL[1.5 * um].volume, morpho.LL.volume[1])
1111 assert_allclose(morpho.LL[1.5 * um].length, morpho.LL.length[1])
1112 assert_allclose(morpho.LL[1.5 * um].r_length_1, morpho.LL.r_length_1[1])
1113 assert_allclose(morpho.LL[1.5 * um].r_length_2, morpho.LL.r_length_2[1])
1114 assert_allclose(morpho.LL[1.5 * um].distance, morpho.LL.distance[1])
1115 assert_allclose(morpho.LL[1.5 * um].diameter, morpho.LL.diameter[1])
1116 assert_allclose(morpho.LL[1.5 * um].x, morpho.LL.x[1])
1117 assert_allclose(morpho.LL[1.5 * um].y, morpho.LL.y[1])
1118 assert_allclose(morpho.LL[1.5 * um].z, morpho.LL.z[1])
1119 assert_allclose(morpho.LL[1.5 * um].start_x, morpho.LL.start_x[1])
1120 assert_allclose(morpho.LL[1.5 * um].start_y, morpho.LL.start_y[1])
1121 assert_allclose(morpho.LL[1.5 * um].start_z, morpho.LL.start_z[1])
1122 assert_allclose(morpho.LL[1.5 * um].end_x, morpho.LL.end_x[1])
1123 assert_allclose(morpho.LL[1.5 * um].end_y, morpho.LL.end_y[1])
1124 assert_allclose(morpho.LL[1.5 * um].end_z, morpho.LL.end_z[1])
1125
1126 # Getting several compartments by indices
1127 assert_allclose(morpho.right[3:6].area, morpho.right.area[3:6])
1128 assert_allclose(morpho.right[3:6].volume, morpho.right.volume[3:6])
1129 assert_allclose(morpho.right[3:6].length, morpho.right.length[3:6])
1130 assert_allclose(morpho.right[3:6].r_length_1, morpho.right.r_length_1[3:6])
1131 assert_allclose(morpho.right[3:6].r_length_2, morpho.right.r_length_2[3:6])
1132 assert_allclose(morpho.right[3:6].distance, morpho.right.distance[3:6])
1133 assert_allclose(morpho.right[3:6].diameter, morpho.right.diameter[3:6])
1134 assert morpho.right[3:6].x is None
1135 assert morpho.right[3:6].y is None
1136 assert morpho.right[3:6].z is None
1137 assert morpho.right[3:6].start_x is None
1138 assert morpho.right[3:6].start_y is None
1139 assert morpho.right[3:6].start_z is None
1140 assert morpho.right[3:6].end_x is None
1141 assert morpho.right[3:6].end_y is None
1142 assert morpho.right[3:6].end_z is None
1143
1144 # Getting several compartments by position
1145 assert_allclose(morpho.L[3 * um : 5 * um].distance, [3.5, 4.5] * um)
1146 assert_allclose(morpho.L[3.5 * um : 4.5 * um].distance, [3.5, 4.5] * um)
1147
1148
1149 @pytest.mark.codegen_independent
1150 def test_subgroup_incorrect():
1151 # Incorrect indexing
1152 morpho = Soma(diameter=30 * um)
1153 morpho.L = Cylinder(length=10 * um, diameter=1 * um, n=10)
1154 morpho.LL = Cylinder(length=5 * um, diameter=2 * um, n=5)
1155 morpho.right = Cylinder(length=3 * um, diameter=1 * um, n=7)
1156
1157 # Non-existing branch
1158 with pytest.raises(AttributeError):
1159 morpho.axon
1160
1161 # Incorrect indexing
1162 # wrong units or mixing units
1163 with pytest.raises(TypeError):
1164 morpho.L[3 * second : 5 * second]
1165 with pytest.raises(TypeError):
1166 morpho.L[3.4:5.3]
1167 with pytest.raises(TypeError):
1168 morpho.L[3 : 5 * um]
1169 with pytest.raises(TypeError):
1170 morpho.L[3 * um : 5]
1171 # providing a step
1172 with pytest.raises(TypeError):
1173 morpho.L[3 * um : 5 * um : 2 * um]
1174 with pytest.raises(TypeError):
1175 morpho.L[3:5:2]
1176 # incorrect type
1177 with pytest.raises(TypeError):
1178 morpho.L[object()]
1179 # out of range
1180 with pytest.raises(IndexError):
1181 morpho.L[-10 * um]
1182 with pytest.raises(IndexError):
1183 morpho.L[15 * um]
1184 with pytest.raises(IndexError):
1185 morpho.L[10]
1186
1187
1188 @pytest.mark.codegen_independent
1189 def test_topology():
1190 soma = Soma(diameter=30 * um)
1191 soma.L = Section(
1192 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
1193 ) # tapering truncated cones
1194 soma.R = Cylinder(n=10, diameter=5 * um, length=50 * um)
1195 soma.R.left = Cylinder(n=10, diameter=2.5 * um, length=50 * um)
1196 soma.R.right = Section(
1197 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
1198 )
1199
1200 str_topology = str(soma.topology())
1201 lines = [l for l in str_topology.split("\n") if len(l.strip())]
1202 assert len(lines) == 5 # one line for each section
1203 for line, name in zip(lines, ["root", ".L", ".R", ".R.left", "R.right"]):
1204 assert name in line
1205
1206
1207 @pytest.mark.codegen_independent
1208 def test_copy_section_soma():
1209 soma = Soma(diameter=30 * um)
1210 soma_copy = soma.copy_section()
1211 assert soma_copy.diameter[0] == 30 * um
1212 assert soma_copy.x is None
1213 assert soma_copy.y is None
1214 assert soma_copy.z is None
1215 assert soma_copy.type == "soma"
1216
1217 soma = Soma(diameter=30 * um, x=5 * um, z=-10 * um)
1218 soma_copy = soma.copy_section()
1219 assert soma_copy.diameter[0] == 30 * um
1220 assert_allclose(soma_copy.x[0], 5 * um)
1221 assert_allclose(soma_copy.y[0], 0 * um)
1222 assert_allclose(soma_copy.z[0], -10 * um)
1223 assert soma_copy.type == "soma"
1224
1225
1226 @pytest.mark.codegen_independent
1227 def test_copy_section_section():
1228 # No coordinates
1229 sec = Section(
1230 diameter=[10, 5, 4, 3, 2, 1] * um, n=5, length=np.ones(5) * 10 * um, type="dend"
1231 )
1232 sec_copy = sec.copy_section()
1233 assert_allclose(sec_copy.start_diameter, sec.start_diameter)
1234 assert_allclose(sec_copy.end_diameter, sec.end_diameter)
1235 assert_allclose(sec_copy.length, sec.length)
1236 assert sec_copy.n == sec.n
1237 assert sec_copy.x is None
1238 assert sec_copy.y is None
1239 assert sec_copy.z is None
1240 assert sec_copy.type == "dend"
1241
1242 # With coordinates
1243 sec = Section(
1244 diameter=[10, 5, 4, 3, 2, 1] * um,
1245 n=5,
1246 x=[0, 1, 2, 3, 4, 5] * um,
1247 y=[0, -1, -2, -3, -4, -5] * um,
1248 )
1249 sec_copy = sec.copy_section()
1250 assert_allclose(sec_copy.start_diameter, sec.start_diameter)
1251 assert_allclose(sec_copy.end_diameter, sec.end_diameter)
1252 assert_allclose(sec_copy.length, sec.length)
1253 assert sec_copy.n == sec.n
1254 assert_allclose(sec_copy.x, sec.x)
1255 assert_allclose(sec_copy.y, sec.y)
1256 assert_allclose(sec_copy.z, sec.z)
1257
1258 assert sec_copy.type is None
1259
1260
1261 @pytest.mark.codegen_independent
1262 def test_copy_section_cylinder():
1263 # no coordinates
1264 sec = Section(
1265 diameter=[10, 5, 4, 3, 2, 1] * um, n=5, length=np.ones(5) * 20 * um, type="dend"
1266 )
1267 sec_copy = sec.copy_section()
1268 assert_allclose(sec_copy.end_diameter, sec.end_diameter)
1269 assert_allclose(sec_copy.length, sec.length)
1270 assert sec_copy.n == sec.n
1271 assert sec_copy.x is None
1272 assert sec_copy.y is None
1273 assert sec_copy.z is None
1274 assert sec_copy.type == "dend"
1275
1276 # with coordinates
1277 sec = Section(
1278 diameter=[10, 5, 4, 3, 2, 1] * um,
1279 n=5,
1280 x=[0, 1, 2, 3, 4, 5] * um,
1281 y=[0, -1, -2, -3, -4, -5] * um,
1282 )
1283 sec_copy = sec.copy_section()
1284 assert_allclose(sec_copy.end_diameter, sec.end_diameter)
1285 assert_allclose(sec_copy.length, sec.length)
1286 assert sec_copy.n == sec.n
1287 assert_allclose(sec_copy.x, sec.x)
1288 assert_allclose(sec_copy.y, sec.y)
1289 assert_allclose(sec_copy.z, sec.z)
1290
1291 assert sec_copy.type is None
1292
1293
1294 def _check_length_coord_consistency(morph_with_coords):
1295 if not isinstance(morph_with_coords, Soma):
1296 vectors = np.diff(morph_with_coords.coordinates, axis=0)
1297 calculated_length = np.sqrt(np.sum(vectors**2, axis=1))
1298 assert_allclose(calculated_length, morph_with_coords.length)
1299 for child in morph_with_coords.children:
1300 _check_length_coord_consistency(child)
1301
1302
1303 @pytest.mark.codegen_independent
1304 def test_generate_coordinates_deterministic():
1305 morph = Soma(diameter=30 * um)
1306 morph.L = Section(
1307 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
1308 ) # tapering truncated cones
1309 morph.R = Cylinder(n=10, diameter=5 * um, length=50 * um)
1310 morph.R.left = Cylinder(n=10, diameter=2.5 * um, length=50 * um)
1311 morph.R.right = Section(
1312 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
1313 )
1314
1315 morph_with_coords = morph.generate_coordinates()
1316 assert morph_with_coords.total_compartments == morph.total_compartments
1317 assert morph_with_coords.total_sections == morph.total_sections
1318
1319 for new, old in [
1320 (morph_with_coords, morph),
1321 (morph_with_coords.L, morph.L),
1322 (morph_with_coords.R, morph.R),
1323 (morph_with_coords.R.left, morph.R.left),
1324 (morph_with_coords.R.right, morph.R.right),
1325 ]:
1326 assert new.n == old.n
1327 assert_allclose(new.length, old.length)
1328 assert_allclose(new.diameter, old.diameter)
1329 # The morphology should be in the x/y plane
1330 assert_equal(new.z, 0 * um)
1331
1332 _check_length_coord_consistency(morph_with_coords)
1333
1334
1335 @pytest.mark.codegen_independent
1336 def test_generate_coordinates_random_sections():
1337 morph = Soma(diameter=30 * um)
1338 morph.L = Section(
1339 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
1340 ) # tapering truncated cones
1341 morph.R = Cylinder(n=10, diameter=5 * um, length=50 * um)
1342 morph.R.left = Cylinder(n=10, diameter=2.5 * um, length=50 * um)
1343 morph.R.right = Section(
1344 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
1345 )
1346
1347 morph_with_coords = morph.generate_coordinates(section_randomness=25)
1348 assert morph_with_coords.total_compartments == morph.total_compartments
1349 assert morph_with_coords.total_sections == morph.total_sections
1350
1351 for new, old in [
1352 (morph_with_coords, morph),
1353 (morph_with_coords.L, morph.L),
1354 (morph_with_coords.R, morph.R),
1355 (morph_with_coords.R.left, morph.R.left),
1356 (morph_with_coords.R.right, morph.R.right),
1357 ]:
1358 assert new.n == old.n
1359 assert_allclose(new.length, old.length)
1360 assert_allclose(new.diameter, old.diameter)
1361
1362 _check_length_coord_consistency(morph_with_coords)
1363
1364
1365 @pytest.mark.codegen_independent
1366 def test_generate_coordinates_random_compartments():
1367 morph = Soma(diameter=30 * um)
1368 morph.L = Section(
1369 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
1370 ) # tapering truncated cones
1371 morph.R = Cylinder(n=10, diameter=5 * um, length=50 * um)
1372 morph.R.left = Cylinder(n=10, diameter=2.5 * um, length=50 * um)
1373 morph.R.right = Section(
1374 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
1375 )
1376
1377 morph_with_coords = morph.generate_coordinates(compartment_randomness=15)
1378 assert morph_with_coords.total_compartments == morph.total_compartments
1379 assert morph_with_coords.total_sections == morph.total_sections
1380
1381 for new, old in [
1382 (morph_with_coords, morph),
1383 (morph_with_coords.L, morph.L),
1384 (morph_with_coords.R, morph.R),
1385 (morph_with_coords.R.left, morph.R.left),
1386 (morph_with_coords.R.right, morph.R.right),
1387 ]:
1388 assert new.n == old.n
1389 assert_allclose(new.length, old.length)
1390 assert_allclose(new.diameter, old.diameter)
1391
1392 _check_length_coord_consistency(morph_with_coords)
1393
1394
1395 @pytest.mark.codegen_independent
1396 def test_generate_coordinates_random_all():
1397 morph = Soma(diameter=30 * um)
1398 morph.L = Section(
1399 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
1400 ) # tapering truncated cones
1401 morph.R = Cylinder(n=10, diameter=5 * um, length=50 * um)
1402 morph.R.left = Cylinder(n=10, diameter=2.5 * um, length=50 * um)
1403 morph.R.right = Section(
1404 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
1405 )
1406
1407 morph_with_coords = morph.generate_coordinates(
1408 section_randomness=25, compartment_randomness=15
1409 )
1410 assert morph_with_coords.total_compartments == morph.total_compartments
1411 assert morph_with_coords.total_sections == morph.total_sections
1412
1413 for new, old in [
1414 (morph_with_coords, morph),
1415 (morph_with_coords.L, morph.L),
1416 (morph_with_coords.R, morph.R),
1417 (morph_with_coords.R.left, morph.R.left),
1418 (morph_with_coords.R.right, morph.R.right),
1419 ]:
1420 assert new.n == old.n
1421 assert_allclose(new.length, old.length)
1422 assert_allclose(new.diameter, old.diameter)
1423
1424 _check_length_coord_consistency(morph_with_coords)
1425
1426
1427 @pytest.mark.codegen_independent
1428 def test_generate_coordinates_no_overwrite():
1429 morph = Soma(diameter=30 * um)
1430 morph.L = Section(
1431 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
1432 ) # tapering truncated cones
1433 morph.R = Cylinder(n=10, diameter=5 * um, length=50 * um)
1434 morph.R.left = Cylinder(n=10, diameter=2.5 * um, length=50 * um)
1435 morph.R.right = Section(
1436 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
1437 )
1438
1439 morph_with_coords = morph.generate_coordinates(compartment_randomness=15)
1440 # This should not change anything because the morphology already has coordinates!
1441 morph_with_coords2 = morph_with_coords.generate_coordinates(
1442 section_randomness=25, compartment_randomness=15
1443 )
1444
1445 for new, old in [
1446 (morph_with_coords2, morph_with_coords),
1447 (morph_with_coords2.L, morph_with_coords.L),
1448 (morph_with_coords2.R, morph_with_coords.R),
1449 (morph_with_coords2.R.left, morph_with_coords.R.left),
1450 (morph_with_coords2.R.right, morph_with_coords.R.right),
1451 ]:
1452 assert new.n == old.n
1453 assert_allclose(new.length, old.length)
1454 assert_allclose(new.diameter, old.diameter)
1455 assert_allclose(new.x, old.x)
1456 assert_allclose(new.y, old.y)
1457 assert_allclose(new.z, old.z)
1458
1459
1460 @pytest.mark.codegen_independent
1461 def test_generate_coordinates_overwrite():
1462 morph = Soma(diameter=30 * um)
1463 morph.L = Section(
1464 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
1465 ) # tapering truncated cones
1466 morph.R = Cylinder(n=10, diameter=5 * um, length=50 * um)
1467 morph.R.left = Cylinder(n=10, diameter=2.5 * um, length=50 * um)
1468 morph.R.right = Section(
1469 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
1470 )
1471
1472 morph_with_coords = morph.generate_coordinates(compartment_randomness=15)
1473 # This should change things since we explicitly ask for it
1474 morph_with_coords2 = morph_with_coords.generate_coordinates(
1475 section_randomness=25, compartment_randomness=15, overwrite_existing=True
1476 )
1477
1478 for new, old in [ # ignore the root compartment
1479 (morph_with_coords2.L, morph_with_coords.L),
1480 (morph_with_coords2.R, morph_with_coords.R),
1481 (morph_with_coords2.R.left, morph_with_coords.R.left),
1482 (morph_with_coords2.R.right, morph_with_coords.R.right),
1483 ]:
1484 assert new.n == old.n
1485 assert_allclose(new.length, old.length)
1486 assert_allclose(new.diameter, old.diameter)
1487 assert all(np.abs(new.x - old.x) > 0)
1488 assert all(np.abs(new.y - old.y) > 0)
1489 assert all(np.abs(new.z - old.z) > 0)
1490
1491 _check_length_coord_consistency(morph_with_coords2)
1492
1493
1494 @pytest.mark.codegen_independent
1495 def test_generate_coordinates_mixed_overwrite():
1496 morph = Soma(diameter=30 * um)
1497 morph.L = Section(
1498 n=5, diameter=[10, 8, 6, 4, 2, 0] * um, length=np.ones(5) * 20 * um
1499 ) # tapering truncated cones
1500 morph.R = Cylinder(n=10, diameter=5 * um, length=50 * um)
1501 morph_with_coords = morph.generate_coordinates(
1502 section_randomness=25, compartment_randomness=15
1503 )
1504 # The following just returns a copy, as all coordinates are already
1505 # specified
1506 morph_copy = morph_with_coords.generate_coordinates()
1507
1508 # Add new sections that do not yet have coordinates
1509 morph_with_coords.R.left = Cylinder(n=10, diameter=2.5 * um, length=50 * um)
1510 morph_with_coords.R.right = Section(
1511 n=5, diameter=[5, 4, 3, 2, 1, 0] * um, length=np.ones(5) * 10 * um
1512 )
1513
1514 # This should change things since we explicitly ask for it
1515 morph_with_coords2 = morph_with_coords.generate_coordinates(
1516 section_randomness=25, compartment_randomness=15
1517 )
1518
1519 for new, old in [
1520 (morph_with_coords2, morph_with_coords),
1521 (morph_with_coords2.L, morph_with_coords.L),
1522 (morph_with_coords2.R, morph_with_coords.R),
1523 ]:
1524 assert new.n == old.n
1525 assert_allclose(new.length, old.length)
1526 assert_allclose(new.diameter, old.diameter)
1527 assert_allclose(new.x, old.x)
1528 assert_allclose(new.y, old.y)
1529 assert_allclose(new.z, old.z)
1530
1531 assert morph_with_coords.R.left.x is None
1532 assert len(morph_with_coords2.R.left.x) == morph_with_coords2.R.left.n
1533
1534 _check_length_coord_consistency(morph_with_coords2)
1535
1536
1537 @pytest.mark.codegen_independent
1538 def test_str_repr():
1539 # A very basic test, make sure that the str/repr functions return
1540 # something and do not raise an error
1541 for morph in [
1542 Soma(diameter=30 * um),
1543 Soma(diameter=30 * um, x=5 * um, y=10 * um),
1544 Cylinder(n=5, diameter=10 * um, length=50 * um),
1545 Cylinder(n=5, diameter=10 * um, x=[0, 50] * um),
1546 Section(
1547 n=5, diameter=[2.5, 5, 10, 5, 10, 5] * um, length=[10, 20, 5, 5, 10] * um
1548 ),
1549 Section(
1550 n=5, diameter=[2.5, 5, 10, 5, 10, 5] * um, x=[0, 10, 30, 35, 40, 50] * um
1551 ),
1552 ]:
1553 assert len(repr(morph)) > 0
1554 assert len(str(morph)) > 0
1555 morph = Soma(30 * um)
1556 assert len(repr(morph.children)) > 0
1557 assert len(str(morph.children)) > 0
1558 morph.axon = Cylinder(1 * um, n=10, length=100 * um)
1559 morph.dend = Cylinder(1 * um, n=10, length=50 * um)
1560 assert len(repr(morph.children)) > 0
1561 assert len(str(morph.children)) > 0
1562
1563
1564 if __name__ == "__main__":
1565 test_attributes_soma()
1566 test_attributes_soma_coordinates()
1567 test_attributes_cylinder()
1568 test_attributes_cylinder_coordinates()
1569 test_attributes_section()
1570 test_attributes_section_coordinates_single()
1571 test_attributes_section_coordinates_all()
1572 test_tree_cables_schematic()
1573 test_tree_cables_coordinates()
1574 test_tree_cables_from_points()
1575 test_tree_cables_from_swc()
1576 test_tree_soma_schematic()
1577 test_tree_soma_coordinates()
1578 test_tree_soma_from_points()
1579 test_tree_soma_from_points_3_point_soma()
1580 test_tree_soma_from_points_3_point_soma_incorrect()
1581 test_tree_soma_from_swc()
1582 test_tree_soma_from_swc_3_point_soma()
1583 test_construction_incorrect_arguments()
1584 test_from_points_minimal()
1585 test_from_points_incorrect()
1586 test_subtree_deletion()
1587 test_subgroup_indices()
1588 test_subgroup_attributes()
1589 test_subgroup_incorrect()
1590 test_topology()
1591 test_copy_section_soma()
1592 test_copy_section_section()
1593 test_copy_section_cylinder()
1594 test_generate_coordinates_deterministic()
1595 test_generate_coordinates_random_sections()
1596 test_generate_coordinates_random_compartments()
1597 test_generate_coordinates_random_all()
1598 test_generate_coordinates_no_overwrite()
1599 test_generate_coordinates_overwrite()
1600 test_generate_coordinates_mixed_overwrite()
1601 test_str_repr()