1
- import neuroml
2
- from six import string_types
1
+ import typing
3
2
3
+ import neuroml
4
+ from neuroml .arraymorph import ArrayMorphology
4
5
5
6
"""Classes to write NeuroML to various formats."""
6
7
@@ -10,8 +11,9 @@ class NeuroMLWriter(object):
10
11
11
12
In future can implement from other types via chain of responsibility pattern.
12
13
"""
14
+
13
15
@classmethod
14
- def write (cls , nmldoc , file , close = True ):
16
+ def write (cls , nmldoc : neuroml . NeuroMLDocument , file : str , close : bool = True ):
15
17
"""Write a NeuroMLDocument to file.
16
18
17
19
:param nmldoc: NeuroML document object to write
@@ -23,8 +25,10 @@ def write(cls, nmldoc, file, close=True):
23
25
:raises AttributeError: if export fails
24
26
"""
25
27
26
- if isinstance (file , string_types ):
27
- file = open (file , "w" )
28
+ if isinstance (file , str ):
29
+ fileh = open (file , "w" )
30
+ else :
31
+ fileh = file
28
32
29
33
# TODO: this should be extracted from the schema:
30
34
namespacedef = 'xmlns="http://www.neuroml.org/schema/neuroml2" '
@@ -37,20 +41,27 @@ def write(cls, nmldoc, file, close=True):
37
41
38
42
try :
39
43
nmldoc .export (
40
- file , 0 , name_ = "neuroml" , namespacedef_ = namespacedef
44
+ fileh , 0 , name_ = "neuroml" , namespacedef_ = namespacedef
41
45
) # name_ param to ensure root element named correctly - generateDS limitation
42
46
except AttributeError as ae :
43
- file .close ()
47
+ fileh .close ()
44
48
raise (ae )
45
49
46
50
if close :
47
- file .close ()
51
+ fileh .close ()
48
52
49
53
50
54
class NeuroMLHdf5Writer (object ):
51
55
"""Exports NeuroML documents to HDF5 format."""
56
+
52
57
@classmethod
53
- def write (cls , nml_doc , h5_file_name , embed_xml = True , compress = True ):
58
+ def write (
59
+ cls ,
60
+ nml_doc : neuroml .NeuroMLDocument ,
61
+ h5_file_name : str ,
62
+ embed_xml : bool = True ,
63
+ compress : bool = True ,
64
+ ):
54
65
"""Write a NeuroMLDocument to HDF5 file
55
66
56
67
:param nmldoc: NeuroML document object to write
@@ -92,9 +103,11 @@ def write(cls, nml_doc, h5_file_name, embed_xml=True, compress=True):
92
103
93
104
try :
94
105
import StringIO
106
+
95
107
sf = StringIO .StringIO ()
96
108
except ImportError :
97
109
import io
110
+
98
111
sf = io .StringIO ()
99
112
100
113
NeuroMLWriter .write (nml_doc , sf , close = False )
@@ -140,13 +153,18 @@ class ArrayMorphWriter(object):
140
153
"""
141
154
142
155
@classmethod
143
- def __write_single_cell (cls , array_morph , fileh , cell_id = None ):
156
+ def __write_single_cell (
157
+ cls ,
158
+ array_morph : ArrayMorphology ,
159
+ fileh ,
160
+ cell_id : typing .Optional [str ] = None ,
161
+ ):
144
162
"""Write a array morphology to a file handler.
145
163
146
164
:param array_morph: a array morph object containing a morphology
147
- :type array_morph: neuroml.arraymorph. ArrayMorphology
148
- :param fileh: file handler of file to write to
149
- :type fileh: file object
165
+ :type array_morph: ArrayMorphology
166
+ :param fileh: pytables file object of file to write to
167
+ :type fileh: pytables file object
150
168
:param cell_id: id of cell
151
169
:type cell_id: str
152
170
"""
@@ -182,7 +200,7 @@ def __write_single_cell(cls, array_morph, fileh, cell_id=None):
182
200
)
183
201
184
202
@classmethod
185
- def __write_neuroml_document (cls , document , fileh ):
203
+ def __write_neuroml_document (cls , document : neuroml . NeuroMLDocument , fileh ):
186
204
"""Write a NeuroMLDocument containing morphology to a file handler
187
205
188
206
:param document: a NeuroML document object containing a morphology
@@ -207,11 +225,15 @@ def __write_neuroml_document(cls, document, fileh):
207
225
cls .__write_single_cell (morphology , fileh , cell_id = cell .id )
208
226
209
227
@classmethod
210
- def write (cls , data , filepath ):
228
+ def write (
229
+ cls ,
230
+ data : typing .Union [neuroml .NeuroMLDocument , ArrayMorphology ],
231
+ filepath : str ,
232
+ ):
211
233
"""Write morphology to file in ArrayMorph format.
212
234
213
235
:param data: data to write
214
- :type data: neuroml.arraymorph. ArrayMorphology or neuroml.NeuroMLDocument
236
+ :type data: ArrayMorphology or neuroml.NeuroMLDocument
215
237
:param filepath: path of file to write to
216
238
:type filepath: str
217
239
@@ -223,7 +245,7 @@ def write(cls, data, filepath):
223
245
# Now instead we should go through a document/cell/morphology
224
246
# hierarchy - this kind of tree traversal should be done recursively
225
247
226
- if isinstance (data , neuroml . arraymorph . ArrayMorphology ):
248
+ if isinstance (data , ArrayMorphology ):
227
249
cls .__write_single_cell (data , fileh )
228
250
229
251
if isinstance (data , neuroml .NeuroMLDocument ):
0 commit comments