22from six import string_types
33
44
5+ """Classes to write NeuroML to various formats."""
6+
7+
58class NeuroMLWriter (object ):
9+ """Writes from NeuroMLDocument to nml file.
10+
11+ In future can implement from other types via chain of responsibility pattern.
12+ """
613 @classmethod
714 def write (cls , nmldoc , file , close = True ):
8- """
9- Writes from NeuroMLDocument to nml file
10- in future can implement from other types
11- via chain of responsibility pattern.
15+ """Write a NeuroMLDocument to file.
16+
17+ :param nmldoc: NeuroML document object to write
18+ :type nmldoc: neuroml.NeuroMLDocument
19+ :param file: file name to write to
20+ :type file: str
21+ :param close: toggle whether file should be closed
22+ :type close: bool
23+ :raises AttributeError: if export fails
1224 """
1325
1426 if isinstance (file , string_types ):
@@ -36,8 +48,21 @@ def write(cls, nmldoc, file, close=True):
3648
3749
3850class NeuroMLHdf5Writer (object ):
51+ """Exports NeuroML documents to HDF5 format."""
3952 @classmethod
4053 def write (cls , nml_doc , h5_file_name , embed_xml = True , compress = True ):
54+ """Write a NeuroMLDocument to HDF5 file
55+
56+ :param nmldoc: NeuroML document object to write
57+ :type nmldoc: neuroml.NeuroMLDocument
58+ :param h5_file_name: file name to write to
59+ :type h5_file_name: str
60+ :param embed_xml: toggle whether XML serialization should be embedded
61+ :type embed_xml: bool
62+ :param compress: toggle compression
63+ :type compress: bool
64+ """
65+
4166 import tables
4267
4368 FILTERS = (
@@ -67,17 +92,13 @@ def write(cls, nml_doc, h5_file_name, embed_xml=True, compress=True):
6792
6893 try :
6994 import StringIO
70-
7195 sf = StringIO .StringIO ()
72- except :
96+ except ImportError :
7397 import io
74-
7598 sf = io .StringIO ()
7699
77100 NeuroMLWriter .write (nml_doc , sf , close = False )
78-
79101 nml2 = sf .getvalue ()
80-
81102 rootGroup ._f_setattr ("neuroml_top_level" , nml2 )
82103
83104 # Put back into previous form...
@@ -89,20 +110,20 @@ def write(cls, nml_doc, h5_file_name, embed_xml=True, compress=True):
89110 """
90111 @classmethod
91112 def write_xml_and_hdf5(cls,nml_doc0,xml_file_name,h5_file_name):
92-
113+
93114 nml_doc_hdf5 = neuroml.NeuroMLDocument(nml_doc0.id)
94-
115+
95116 for n in nml_doc0.networks:
96117 nml_doc_hdf5.networks.append(n)
97-
118+
98119 nml_doc0.networks = []
99-
100- nml_doc0.includes.append(neuroml.IncludeType(h5_file_name))
101-
120+
121+ nml_doc0.includes.append(neuroml.IncludeType(h5_file_name))
122+
102123 NeuroMLWriter.write(nml_doc0,xml_file_name)
103-
124+
104125 NeuroMLHdf5Writer.write(nml_doc_hdf5,h5_file_name,embed_xml=False)
105-
126+
106127 # Put back into previous form...
107128 for n in nml_doc_hdf5.networks:
108129 nml_doc0.networks.append(n)
@@ -113,11 +134,22 @@ def write_xml_and_hdf5(cls,nml_doc0,xml_file_name,h5_file_name):
113134
114135class ArrayMorphWriter (object ):
115136 """
137+ Write morphology to ArrayMorph format.
138+
116139 For now just testing a simple method which can write a morphology, not a NeuroMLDocument.
117140 """
118141
119142 @classmethod
120143 def __write_single_cell (cls , array_morph , fileh , cell_id = None ):
144+ """Write a array morphology to a file handler.
145+
146+ :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
150+ :param cell_id: id of cell
151+ :type cell_id: str
152+ """
121153 vertices = array_morph .vertices
122154 connectivity = array_morph .connectivity
123155 physical_mask = array_morph .physical_mask
@@ -128,12 +160,12 @@ def __write_single_cell(cls, array_morph, fileh, cell_id=None):
128160 # Create the groups:
129161 # can use morphology name in future?
130162
131- if array_morph .id == None :
163+ if array_morph .id is None :
132164 morphology_name = "Morphology"
133165 else :
134166 morphology_name = array_morph .id
135167
136- if cell_id == None :
168+ if cell_id is None :
137169 morphology_group = fileh .create_group (root , morphology_name )
138170 hierarchy_prefix = "/" + morphology_name
139171 else :
@@ -151,26 +183,39 @@ def __write_single_cell(cls, array_morph, fileh, cell_id=None):
151183
152184 @classmethod
153185 def __write_neuroml_document (cls , document , fileh ):
154- document_id = document . id
186+ """Write a NeuroMLDocument containing morphology to a file handler
155187
188+ :param document: a NeuroML document object containing a morphology
189+ :type document: neuroml.NeuroMLDocument
190+ :param fileh: file handler of file to write to
191+ :type fileh: file object
192+ """
156193 for default_id , cell in enumerate (document .cells ):
157194 morphology = cell .morphology
158195
159- if morphology .id == None :
196+ if morphology .id is None :
160197 morphology .id = "Morphology" + str (default_id )
161- if cell .id == None :
198+ if cell .id is None :
162199 cell .id = "Cell" + str (default_id )
163200
164201 cls .__write_single_cell (morphology , fileh , cell_id = cell .id )
165202
166203 for default_id , morphology in enumerate (document .morphology ):
167- if morphology .id == None :
204+ if morphology .id is None :
168205 morphology .id = "Morphology" + str (default_id )
169206
170207 cls .__write_single_cell (morphology , fileh , cell_id = cell .id )
171208
172209 @classmethod
173210 def write (cls , data , filepath ):
211+ """Write morphology to file in ArrayMorph format.
212+
213+ :param data: data to write
214+ :type data: neuroml.arraymorph.ArrayMorphology or neuroml.NeuroMLDocument
215+ :param filepath: path of file to write to
216+ :type filepath: str
217+
218+ """
174219 import tables
175220
176221 fileh = tables .open_file (filepath , mode = "w" )
0 commit comments