1- import neuroml
2- from six import string_types
1+ import typing
32
3+ import neuroml
4+ from neuroml .arraymorph import ArrayMorphology
45
56"""Classes to write NeuroML to various formats."""
67
@@ -10,8 +11,9 @@ class NeuroMLWriter(object):
1011
1112 In future can implement from other types via chain of responsibility pattern.
1213 """
14+
1315 @classmethod
14- def write (cls , nmldoc , file , close = True ):
16+ def write (cls , nmldoc : neuroml . NeuroMLDocument , file : str , close : bool = True ):
1517 """Write a NeuroMLDocument to file.
1618
1719 :param nmldoc: NeuroML document object to write
@@ -23,8 +25,10 @@ def write(cls, nmldoc, file, close=True):
2325 :raises AttributeError: if export fails
2426 """
2527
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
2832
2933 # TODO: this should be extracted from the schema:
3034 namespacedef = 'xmlns="http://www.neuroml.org/schema/neuroml2" '
@@ -37,20 +41,27 @@ def write(cls, nmldoc, file, close=True):
3741
3842 try :
3943 nmldoc .export (
40- file , 0 , name_ = "neuroml" , namespacedef_ = namespacedef
44+ fileh , 0 , name_ = "neuroml" , namespacedef_ = namespacedef
4145 ) # name_ param to ensure root element named correctly - generateDS limitation
4246 except AttributeError as ae :
43- file .close ()
47+ fileh .close ()
4448 raise (ae )
4549
4650 if close :
47- file .close ()
51+ fileh .close ()
4852
4953
5054class NeuroMLHdf5Writer (object ):
5155 """Exports NeuroML documents to HDF5 format."""
56+
5257 @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+ ):
5465 """Write a NeuroMLDocument to HDF5 file
5566
5667 :param nmldoc: NeuroML document object to write
@@ -92,9 +103,11 @@ def write(cls, nml_doc, h5_file_name, embed_xml=True, compress=True):
92103
93104 try :
94105 import StringIO
106+
95107 sf = StringIO .StringIO ()
96108 except ImportError :
97109 import io
110+
98111 sf = io .StringIO ()
99112
100113 NeuroMLWriter .write (nml_doc , sf , close = False )
@@ -140,13 +153,18 @@ class ArrayMorphWriter(object):
140153 """
141154
142155 @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+ ):
144162 """Write a array morphology to a file handler.
145163
146164 :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
150168 :param cell_id: id of cell
151169 :type cell_id: str
152170 """
@@ -182,7 +200,7 @@ def __write_single_cell(cls, array_morph, fileh, cell_id=None):
182200 )
183201
184202 @classmethod
185- def __write_neuroml_document (cls , document , fileh ):
203+ def __write_neuroml_document (cls , document : neuroml . NeuroMLDocument , fileh ):
186204 """Write a NeuroMLDocument containing morphology to a file handler
187205
188206 :param document: a NeuroML document object containing a morphology
@@ -207,11 +225,15 @@ def __write_neuroml_document(cls, document, fileh):
207225 cls .__write_single_cell (morphology , fileh , cell_id = cell .id )
208226
209227 @classmethod
210- def write (cls , data , filepath ):
228+ def write (
229+ cls ,
230+ data : typing .Union [neuroml .NeuroMLDocument , ArrayMorphology ],
231+ filepath : str ,
232+ ):
211233 """Write morphology to file in ArrayMorph format.
212234
213235 :param data: data to write
214- :type data: neuroml.arraymorph. ArrayMorphology or neuroml.NeuroMLDocument
236+ :type data: ArrayMorphology or neuroml.NeuroMLDocument
215237 :param filepath: path of file to write to
216238 :type filepath: str
217239
@@ -223,7 +245,7 @@ def write(cls, data, filepath):
223245 # Now instead we should go through a document/cell/morphology
224246 # hierarchy - this kind of tree traversal should be done recursively
225247
226- if isinstance (data , neuroml . arraymorph . ArrayMorphology ):
248+ if isinstance (data , ArrayMorphology ):
227249 cls .__write_single_cell (data , fileh )
228250
229251 if isinstance (data , neuroml .NeuroMLDocument ):
0 commit comments