20
20
from pathlib import Path
21
21
22
22
import morph_tool .transform as transformations
23
+ import morphio
23
24
import numpy as np
24
- from morphio .mut import Morphology
25
25
26
26
from bluepysnap .exceptions import BluepySnapError
27
27
from bluepysnap .sonata_constants import Node
@@ -51,8 +51,8 @@ def __init__(self, morph_dir, population, alternate_morphologies=None):
51
51
self ._alternate_morphologies = alternate_morphologies or {}
52
52
self ._population = population
53
53
54
- def get_morphology_dir (self , extension ):
55
- """Return morphology directory based on a given extension ."""
54
+ def _get_morphology_base (self , extension ):
55
+ """Get morphology base path; this will be a directory unless it's a morphology container ."""
56
56
if extension == "swc" :
57
57
if not self ._morph_dir :
58
58
raise BluepySnapError ("'morphologies_dir' is not defined in config" )
@@ -68,16 +68,33 @@ def get_morphology_dir(self, extension):
68
68
69
69
return morph_dir
70
70
71
+ def get_morphology_dir (self , extension = "swc" ):
72
+ """Return morphology directory based on a given extension."""
73
+ morph_dir = self ._get_morphology_base (extension )
74
+
75
+ if extension == "h5" and Path (morph_dir ).is_file ():
76
+ raise BluepySnapError (
77
+ f"'{ morph_dir } ' is a morphology container, so a directory does not exist"
78
+ )
79
+
80
+ return morph_dir
81
+
82
+ def get_name (self , node_id ):
83
+ """Get the morphology name for a `node_id`."""
84
+ if not is_node_id (node_id ):
85
+ raise BluepySnapError ("node_id must be a int or a CircuitNodeId" )
86
+
87
+ name = self ._population .get (node_id , Node .MORPHOLOGY )
88
+ return name
89
+
71
90
def get_filepath (self , node_id , extension = "swc" ):
72
91
"""Return path to SWC morphology file corresponding to `node_id`.
73
92
74
93
Args:
75
94
node_id (int/CircuitNodeId): could be a int or CircuitNodeId.
76
95
extension (str): expected filetype extension of the morph file.
77
96
"""
78
- if not is_node_id (node_id ):
79
- raise BluepySnapError ("node_id must be a int or a CircuitNodeId" )
80
- name = self ._population .get (node_id , Node .MORPHOLOGY )
97
+ name = self .get_name (node_id )
81
98
82
99
return Path (self .get_morphology_dir (extension ), f"{ name } .{ extension } " )
83
100
@@ -90,11 +107,19 @@ def get(self, node_id, transform=False, extension="swc"):
90
107
according to `node_id` position in the circuit.
91
108
extension (str): expected filetype extension of the morph file.
92
109
"""
93
- filepath = self .get_filepath (node_id , extension = extension )
94
- result = Morphology (filepath )
110
+ collection = morphio .Collection (
111
+ self ._get_morphology_base (extension ),
112
+ [
113
+ f".{ extension } " ,
114
+ ],
115
+ )
116
+ name = self .get_name (node_id )
117
+ result = collection .load (name , mutable = True )
118
+
95
119
if transform :
96
120
T = np .eye (4 )
97
121
T [:3 , :3 ] = self ._population .orientations (node_id ) # rotations
98
122
T [:3 , 3 ] = self ._population .positions (node_id ).values # translations
99
123
transformations .transform (result , T )
124
+
100
125
return result .as_immutable ()
0 commit comments