Skip to content

Commit a7e13f8

Browse files
Merge pull request #184 from NeuralEnsemble/feat/document-writers
docs(writers): document classes and methods
2 parents 5b66349 + 74c442f commit a7e13f8

File tree

3 files changed

+127
-33
lines changed

3 files changed

+127
-33
lines changed

neuroml/nml/helper_methods.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2931,7 +2931,14 @@ def __str__(self):
29312931
source='''\
29322932
29332933
def exportHdf5(self, h5file, h5Group):
2934-
"""Export to HDF5 file. """
2934+
"""Export to HDF5 file.
2935+
2936+
:param h5file: HDF5 file handler
2937+
:type h5file: file object
2938+
:param h5Group: the tables Group object to write
2939+
:type h5Group: tables.Group
2940+
2941+
"""
29352942
#print("Exporting %s: "+str(self.id)+" as HDF5")
29362943
%s
29372944
'''

neuroml/nml/nml.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# -*- coding: utf-8 -*-
33

44
#
5-
# Generated Wed Sep 20 19:12:47 2023 by generateDS.py version 2.43.1.
6-
# Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]
5+
# Generated Tue Dec 5 10:44:41 2023 by generateDS.py version 2.43.3.
6+
# Python 3.11.6 (main, Oct 3 2023, 00:00:00) [GCC 13.2.1 20230918 (Red Hat 13.2.1-3)]
77
#
88
# Command line options:
99
# ('-o', 'nml.py')
@@ -16,7 +16,7 @@
1616
# NeuroML_v2.3.xsd
1717
#
1818
# Command line:
19-
# /usr/local/bin/generateDS -o "nml.py" --use-getter-setter="none" --user-methods="helper_methods.py" --export="write validate" --custom-imports-template="gds_imports-template.py" NeuroML_v2.3.xsd
19+
# /home/asinha/.local/share/virtualenvs/neuroml-311-dev/bin/generateDS -o "nml.py" --use-getter-setter="none" --user-methods="helper_methods.py" --export="write validate" --custom-imports-template="gds_imports-template.py" NeuroML_v2.3.xsd
2020
#
2121
# Current working directory (os.getcwd()):
2222
# nml
@@ -7370,7 +7370,14 @@ def _buildChildren(
73707370
super(InputList, self)._buildChildren(child_, node, nodeName_, True)
73717371

73727372
def exportHdf5(self, h5file, h5Group):
7373-
"""Export to HDF5 file."""
7373+
"""Export to HDF5 file.
7374+
7375+
:param h5file: HDF5 file handler
7376+
:type h5file: file object
7377+
:param h5Group: the tables Group object to write
7378+
:type h5Group: tables.Group
7379+
7380+
"""
73747381
# print("Exporting InputList: "+str(self.id)+" as HDF5")
73757382

73767383
ilGroup = h5file.create_group(h5Group, "inputList_" + self.id)
@@ -10840,7 +10847,14 @@ def _buildChildren(
1084010847
super(Population, self)._buildChildren(child_, node, nodeName_, True)
1084110848

1084210849
def exportHdf5(self, h5file, h5Group):
10843-
"""Export to HDF5 file."""
10850+
"""Export to HDF5 file.
10851+
10852+
:param h5file: HDF5 file handler
10853+
:type h5file: file object
10854+
:param h5Group: the tables Group object to write
10855+
:type h5Group: tables.Group
10856+
10857+
"""
1084410858
# print("Exporting Population: "+str(self.id)+" as HDF5")
1084510859

1084610860
popGroup = h5file.create_group(h5Group, "population_" + self.id)
@@ -12504,7 +12518,14 @@ def __str__(self):
1250412518
)
1250512519

1250612520
def exportHdf5(self, h5file, h5Group):
12507-
"""Export to HDF5 file."""
12521+
"""Export to HDF5 file.
12522+
12523+
:param h5file: HDF5 file handler
12524+
:type h5file: file object
12525+
:param h5Group: the tables Group object to write
12526+
:type h5Group: tables.Group
12527+
12528+
"""
1250812529
# print("Exporting Network: "+str(self.id)+" as HDF5")
1250912530

1251012531
netGroup = h5file.create_group(h5Group, "network")
@@ -44993,7 +45014,14 @@ def _buildChildren(
4499345014
super(ContinuousProjection, self)._buildChildren(child_, node, nodeName_, True)
4499445015

4499545016
def exportHdf5(self, h5file, h5Group):
44996-
"""Export to HDF5 file."""
45017+
"""Export to HDF5 file.
45018+
45019+
:param h5file: HDF5 file handler
45020+
:type h5file: file object
45021+
:param h5Group: the tables Group object to write
45022+
:type h5Group: tables.Group
45023+
45024+
"""
4499745025
# print("Exporting ContinuousProjection: "+str(self.id)+" as HDF5")
4499845026

4499945027
projGroup = h5file.create_group(h5Group, "projection_" + self.id)
@@ -45401,7 +45429,14 @@ def _buildChildren(
4540145429
super(ElectricalProjection, self)._buildChildren(child_, node, nodeName_, True)
4540245430

4540345431
def exportHdf5(self, h5file, h5Group):
45404-
"""Export to HDF5 file."""
45432+
"""Export to HDF5 file.
45433+
45434+
:param h5file: HDF5 file handler
45435+
:type h5file: file object
45436+
:param h5Group: the tables Group object to write
45437+
:type h5Group: tables.Group
45438+
45439+
"""
4540545440
# print("Exporting ElectricalProjection: "+str(self.id)+" as HDF5")
4540645441

4540745442
projGroup = h5file.create_group(h5Group, "projection_" + self.id)
@@ -46678,7 +46713,14 @@ def _buildChildren(
4667846713
super(Projection, self)._buildChildren(child_, node, nodeName_, True)
4667946714

4668046715
def exportHdf5(self, h5file, h5Group):
46681-
"""Export to HDF5 file."""
46716+
"""Export to HDF5 file.
46717+
46718+
:param h5file: HDF5 file handler
46719+
:type h5file: file object
46720+
:param h5Group: the tables Group object to write
46721+
:type h5Group: tables.Group
46722+
46723+
"""
4668246724
# print("Exporting Projection: "+str(self.id)+" as HDF5")
4668346725

4668446726
projGroup = h5file.create_group(h5Group, "projection_" + self.id)

neuroml/writers.py

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22
from six import string_types
33

44

5+
"""Classes to write NeuroML to various formats."""
6+
7+
58
class 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

3850
class 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

114135
class 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

Comments
 (0)