Skip to content

Commit 14973f0

Browse files
Merge pull request #185 from NeuralEnsemble/feat/more-docs
More documentation for writers/loaders
2 parents 2d81121 + 4861b60 commit 14973f0

File tree

2 files changed

+98
-30
lines changed

2 files changed

+98
-30
lines changed

neuroml/loaders.py

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1-
from neuroml.nml.nml import parse as nmlparse
2-
3-
from neuroml.nml.nml import parseString as nmlparsestring
4-
5-
import neuroml
6-
from neuroml import (NeuroMLDocument)
7-
import neuroml.utils as utils
8-
91
import os
102
import sys
113
import warnings
12-
134
from typing import Callable, Optional
145

6+
import neuroml
7+
import neuroml.utils as utils
8+
from neuroml import NeuroMLDocument
9+
from neuroml.nml.nml import parse as nmlparse
10+
from neuroml.nml.nml import parseString as nmlparsestring
11+
1512
supressGeneratedsWarnings = True
1613

1714

1815
class NeuroMLLoader(object):
16+
"""Class for loading NeuroML."""
17+
1918
@classmethod
20-
def load(cls, src):
19+
def load(cls, src: str) -> neuroml.NeuroMLDocument:
20+
"""Load a NeuroML file.
21+
22+
:param src: file
23+
:type src: str
24+
:returns: NeuroMLDocument object
25+
:rtype: neuroml.NeuromlDocument
26+
:raises TypeError: if the file is not a valid NeuroML document
27+
"""
2128
doc = cls.__nml2_doc(src)
2229
if isinstance(doc, NeuroMLDocument):
2330
return doc
@@ -29,7 +36,15 @@ def load(cls, src):
2936
)
3037

3138
@classmethod
32-
def __nml2_doc(cls, file_name):
39+
def __nml2_doc(cls, file_name: str) -> neuroml.NeuroMLDocument:
40+
"""Load and parse a NeuroML file.
41+
42+
:param file_name: the file
43+
:type file_name: str
44+
:returns: NeuroMLDocument object
45+
:rtype: neuroml.NeuromlDocument
46+
:raises Exception: if the document is not a valid NeuroML Document
47+
"""
3348
try:
3449
if supressGeneratedsWarnings:
3550
warnings.simplefilter("ignore")
@@ -43,13 +58,43 @@ def __nml2_doc(cls, file_name):
4358

4459

4560
class NeuroMLHdf5Loader(object):
61+
"""Class for loading a NeuroML HDF5 file."""
62+
4663
@classmethod
47-
def load(cls, src, optimized=False):
64+
def load(cls, src: str, optimized: bool = False) -> neuroml.NeuroMLDocument:
65+
"""Load a NeuroML HDF5 file.
66+
67+
:param src: file
68+
:type src: str
69+
:param optimized: load optimized numpy representation
70+
In the optimized representation, instead of the complete Python
71+
object tree being constructed, the various tables in the HDF5 file
72+
are loaded as numpy arrays. This is transparent to the user, who
73+
can continue using the standard methods to access the data.
74+
:type optimized: bool
75+
:returns: NeuroMLDocument object
76+
:rtype: neuroml.NeuromlDocument
77+
"""
4878
doc = cls.__nml2_doc(src, optimized)
4979
return doc
5080

5181
@classmethod
52-
def __nml2_doc(cls, file_name, optimized=False):
82+
def __nml2_doc(
83+
cls, file_name: str, optimized: bool = False
84+
) -> neuroml.NeuroMLDocument:
85+
"""Load and parse a NeuroML HDF5 file.
86+
87+
:param file_name: the file
88+
:type file_name: str
89+
:param optimized: load optimized numpy representation
90+
In the optimized representation, instead of the complete Python
91+
object tree being constructed, the various tables in the HDF5 file
92+
are loaded as numpy arrays. This is transparent to the user, who
93+
can continue using the standard methods to access the data.
94+
:type optimized: bool
95+
:returns: NeuroMLDocument object
96+
:rtype: neuroml.NeuromlDocument
97+
"""
5398
import logging
5499

55100
logging.basicConfig(
@@ -96,6 +141,7 @@ def load_swc_single(cls, src, name=None):
96141
)
97142

98143
import numpy as np
144+
99145
from neuroml import arraymorph
100146

101147
dtype = {

neuroml/writers.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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

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

Comments
 (0)