Debian Codegraph @

docs/source/extensions.rst

raw
1 .. _extending-standard:
2
3 Extending Standards
4 ===================
5
6 The following page will discuss how to extend a standard using HDMF.
7
8 .. _creating-extensions:
9
10 Creating new Extensions
11 -----------------------
12
13 Standards specified using HDMF are designed to be extended. Extension for a standard can be done so using classes
14 provided in the :py:mod:`hdmf.spec` module. The classes :py:class:`~hdmf.spec.spec.GroupSpec`,
15 :py:class:`~hdmf.spec.spec.DatasetSpec`, :py:class:`~hdmf.spec.spec.AttributeSpec`, and :py:class:`~hdmf.spec.spec.LinkSpec`
16 can be used to define custom types.
17
18 Attribute Specifications
19 ^^^^^^^^^^^^^^^^^^^^^^^^
20
21 Specifying attributes is done with :py:class:`~hdmf.spec.spec.AttributeSpec`.
22
23 .. code-block:: python
24
25 from hdmf.spec import AttributeSpec
26
27 spec = AttributeSpec('bar', 'a value for bar', 'float')
28
29 Dataset Specifications
30 ^^^^^^^^^^^^^^^^^^^^^^
31
32 Specifying datasets is done with :py:class:`~hdmf.spec.spec.DatasetSpec`.
33
34 .. code-block:: python
35
36 from hdmf.spec import DatasetSpec
37
38 spec = DatasetSpec('A custom data type',
39 name='qux',
40 attribute=[
41 AttributeSpec('baz', 'a value for baz', 'str'),
42 ],
43 shape=(None, None))
44
45
46 Using datasets to specify tables
47 ++++++++++++++++++++++++++++++++
48
49 Tables can be specified using :py:class:`~hdmf.spec.spec.DtypeSpec`. To specify a table, provide a
50 list of :py:class:`~hdmf.spec.spec.DtypeSpec` objects to the *dtype* argument.
51
52 .. code-block:: python
53
54 from hdmf.spec import DatasetSpec, DtypeSpec
55
56 spec = DatasetSpec('A custom data type',
57 name='qux',
58 attribute=[
59 AttributeSpec('baz', 'a value for baz', 'str'),
60 ],
61 dtype=[
62 DtypeSpec('foo', 'column for foo', 'int'),
63 DtypeSpec('bar', 'a column for bar', 'float')
64 ])
65
66 Group Specifications
67 ^^^^^^^^^^^^^^^^^^^^
68
69 Specifying groups is done with the :py:class:`~hdmf.spec.spec.GroupSpec` class.
70
71 .. code-block:: python
72
73 from hdmf.spec import GroupSpec
74
75 spec = GroupSpec('A custom data type',
76 name='quux',
77 attributes=[...],
78 datasets=[...],
79 groups=[...])
80
81 Data Type Specifications
82 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
83
84 :py:class:`~hdmf.spec.spec.GroupSpec` and :py:class:`~hdmf.spec.spec.DatasetSpec` use the arguments `data_type_inc` and
85 `data_type_def` for declaring new types and extending existing types. New types are specified by setting the argument
86 `data_type_def`. New types can extend an existing type by specifying the argument `data_type_inc`.
87
88 Create a new type
89
90 .. code-block:: python
91
92 from hdmf.spec import GroupSpec
93
94 # A list of AttributeSpec objects to specify new attributes
95 addl_attributes = [...]
96 # A list of DatasetSpec objects to specify new datasets
97 addl_datasets = [...]
98 # A list of DatasetSpec objects to specify new groups
99 addl_groups = [...]
100 spec = GroupSpec('A custom data type',
101 attributes=addl_attributes,
102 datasets=addl_datasets,
103 groups=addl_groups,
104 data_type_def='MyNewType')
105
106 Extend an existing type
107
108 .. code-block:: python
109
110 from hdmf.spec import GroupSpec
111
112 # A list of AttributeSpec objects to specify additional attributes or attributes to be overridden
113 addl_attributes = [...]
114 # A list of DatasetSpec objects to specify additional datasets or datasets to be overridden
115 addl_datasets = [...]
116 # A list of GroupSpec objects to specify additional groups or groups to be overridden
117 addl_groups = [...]
118 spec = GroupSpec('An extended data type',
119 attributes=addl_attributes,
120 datasets=addl_datasets,
121 groups=addl_groups,
122 data_type_inc='SpikeEventSeries',
123 data_type_def='MyExtendedSpikeEventSeries')
124
125 Existing types can be instantiated by specifying `data_type_inc` alone.
126
127 .. code-block:: python
128
129 from hdmf.spec import GroupSpec
130
131 # use another GroupSpec object to specify that a group of type
132 # ElectricalSeries should be present in the new type defined below
133 addl_groups = [ GroupSpec('An included ElectricalSeries instance',
134 data_type_inc='ElectricalSeries') ]
135
136 spec = GroupSpec('An extended data type',
137 groups=addl_groups,
138 data_type_inc='SpikeEventSeries',
139 data_type_def='MyExtendedSpikeEventSeries')
140
141
142 Datasets can be extended in the same manner (with regard to `data_type_inc` and `data_type_def`,
143 by using the class :py:class:`~hdmf.spec.spec.DatasetSpec`.
144
145 .. _saving-extensions:
146
147 Saving Extensions
148 -----------------
149
150 Extensions are used by including them in a loaded namespace. Namespaces and extensions need to be saved to file
151 for downstream use. The class :py:class:`~hdmf.spec.write.NamespaceBuilder` can be used to create new namespace and
152 specification files.
153
154 Create a new namespace with extensions
155
156 .. code-block:: python
157
158 from hdmf.spec import GroupSpec, NamespaceBuilder
159
160 # create a builder for the namespace
161 ns_builder = NamespaceBuilder("Extension for use in my laboratory", "mylab", ...)
162
163 # create extensions
164 ext1 = GroupSpec('A custom SpikeEventSeries interface',
165 attributes=[...]
166 datasets=[...],
167 groups=[...],
168 data_type_inc='SpikeEventSeries',
169 data_type_def='MyExtendedSpikeEventSeries')
170
171 ext2 = GroupSpec('A custom EventDetection interface',
172 attributes=[...]
173 datasets=[...],
174 groups=[...],
175 data_type_inc='EventDetection',
176 data_type_def='MyExtendedEventDetection')
177
178
179 # add the extension
180 ext_source = 'mylab.specs.yaml'
181 ns_builder.add_spec(ext_source, ext1)
182 ns_builder.add_spec(ext_source, ext2)
183
184 # include an existing namespace - this will include all specifications in that namespace
185 ns_builder.include_namespace('collab_ns')
186
187 # save the namespace and extensions
188 ns_path = 'mylab.namespace.yaml'
189 ns_builder.export(ns_path)
190
191
192 .. tip::
193
194 Using the API to generate extensions (rather than writing YAML sources directly) helps avoid errors in the specification
195 (e.g., due to missing required keys or invalid values) and ensure compliance of the extension definition with the
196 HDMF specification language. It also helps with maintenance of extensions, e.g., if extensions have to be ported to
197 newer versions of the `specification language <https://schema-language.readthedocs.io/en/latest/>`_
198 in the future.
199
200 .. _incorporating-extensions:
201
202 Incorporating extensions
203 ------------------------
204
205 HDMF supports extending existing data types.
206 Extensions must be registered with HDMF to be used for reading and writing of custom data types.
207
208 The following code demonstrates how to load custom namespaces.
209
210 .. code-block:: python
211
212 from hdmf import load_namespaces
213 namespace_path = 'my_namespace.yaml'
214 load_namespaces(namespace_path)
215
216 .. note::
217
218 This will register all namespaces defined in the file ``'my_namespace.yaml'``.
219
220 Container : Representing custom data
221 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
222
223 To read and write custom data, corresponding :py:class:`~hdmf.container.Container` classes must be associated with their
224 respective specifications. :py:class:`~hdmf.container.Container` classes are associated with their respective
225 specification using the decorator :py:func:`~hdmf.common.register_class`.
226
227 The following code demonstrates how to associate a specification with the :py:class:`~hdmf.container.Container` class
228 that represents it.
229
230 .. code-block:: python
231
232 from hdmf.common import register_class
233 from hdmf.container import Container
234
235 @register_class('MyExtension', 'my_namespace')
236 class MyExtensionContainer(Container):
237 ...
238
239 :py:func:`~hdmf.common.register_class` can also be used as a function.
240
241 .. code-block:: python
242
243 from hdmf.common import register_class
244 from hdmf.container import Container
245
246 class MyExtensionContainer(Container):
247 ...
248
249 register_class(data_type='MyExtension', namespace='my_namespace', container_cls=MyExtensionContainer)
250
251 If you do not have an :py:class:`~hdmf.container.Container` subclass to associate with your extension specification,
252 a dynamically created class is created by default.
253
254 To use the dynamic class, you will need to retrieve the class object using the function :py:func:`~hdmf.common.get_class`.
255 Once you have retrieved the class object, you can use it just like you would a statically defined class.
256
257 .. code-block:: python
258
259 from hdmf.common import get_class
260 MyExtensionContainer = get_class('my_namespace', 'MyExtension')
261 my_ext_inst = MyExtensionContainer(...)
262
263
264 If using iPython, you can access documentation for the class's constructor using the help command.
265
266 ObjectMapper : Customizing the mapping between Container and the Spec
267 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
268
269 If your :py:class:`~hdmf.container.Container` extension requires custom mapping of the
270 :py:class:`~hdmf.container.Container` class for reading and writing, you will need to implement and register a custom
271 :py:class:`~hdmf.build.objectmapper.ObjectMapper`.
272
273 :py:class:`~hdmf.build.objectmapper.ObjectMapper` extensions are registered with the decorator
274 :py:func:`~hdmf.common.register_map`.
275
276 .. code-block:: python
277
278 from hdmf.common import register_map
279 from hdmf.build import ObjectMapper
280
281 @register_map(MyExtensionContainer)
282 class MyExtensionMapper(ObjectMapper)
283 ...
284
285 :py:func:`~hdmf.common.register_map` can also be used as a function.
286
287 .. code-block:: python
288
289 from hdmf.common import register_map
290 from hdmf.build import ObjectMapper
291
292 class MyExtensionMapper(ObjectMapper)
293 ...
294
295 register_map(MyExtensionContainer, MyExtensionMapper)
296
297 .. tip::
298
299 ObjectMappers allow you to customize how objects in the spec are mapped to attributes of your Container in
300 Python. This is useful, e.g., in cases where you want to customize the default mapping.
301 For an overview of the concepts of containers, spec, builders, object mappers in HDMF see also
302 :ref:`software-architecture`
303
304
305 .. _documenting-extensions:
306
307 Documenting Extensions
308 ----------------------
309
310 Coming soon!
311
312 Further Reading
313 ---------------
314
315 * **Specification Language:** For a detailed overview of the specification language itself see https://hdmf-schema-language.readthedocs.io/en/latest/index.html