| 1 |
.. toctree:: |
| 2 |
:maxdepth: 1 |
| 3 |
|
| 4 |
.. _metadata: |
| 5 |
|
| 6 |
Annotations with arbitrary metadata |
| 7 |
=================================== |
| 8 |
|
| 9 |
The entities of the data model that were discussed so far carry just enough information to get a basic understanding of the stored data. Often much more information than that is required. Storing additional metadata is a central part of the NIX concept. We use a slightly modified version of the *odML* data model for metadata to store additional information. In brief: the model consists of *Sections* that contain *Properties* which in turn carry a list of values. Again, *Sections* can be nested to represent logical dependencies in the hierarchy of a tree. While all data entities discussed above are children of *Block* entities, the metadata lives parallel to the *Blocks*. The idea behind this is that several blocks may refer to the same metadata, or, the other way round the metadata applies to data entities in several blocks. The *types* used for the *Sections* in the following example are defined |
| 10 |
in the `odml terminologies <https://github.com/G-Node/odml-terminologies>`_ |
| 11 |
|
| 12 |
Most of the data entities can link to metadata sections. |
| 13 |
|
| 14 |
.. literalinclude:: examples/annotations.py |
| 15 |
:lines: 5-21 |
| 16 |
:caption: We can add arbitrary metadata in trees of *Sections* and *Properties* (:download:`example code <examples/annotations.py>`). |
| 17 |
|
| 18 |
For a quick view of the metadata tree pretty-print it: |
| 19 |
|
| 20 |
.. literalinclude:: examples/annotations.py |
| 21 |
:lines: 24 |
| 22 |
|
| 23 |
which leads to an output like this. The argument ``max_depth=-1`` notes that the full depth of the tree should be displayed. In the default case (``max_depth=1``) the display will be more compact and will not recursively traverse the whole tree. |
| 24 |
|
| 25 |
.. code-block:: text |
| 26 |
|
| 27 |
recording session [odml.recording] |
| 28 |
|- experimenter: ('John Doe',) |
| 29 |
|- recording date: ('2014-01-01',) |
| 30 |
subject [odml.subject] |
| 31 |
|- id: ('mouse xyz',) |
| 32 |
cell [odml.cell] |
| 33 |
|- resting potential: (-64.5,)mV |
| 34 |
|
| 35 |
The *Sections* add much like dictionaries. To access e.g. the "resting potential" of the cell we may call: |
| 36 |
|
| 37 |
.. literalinclude:: examples/annotations.py |
| 38 |
:lines: 26 |
| 39 |
|
| 40 |
Extending Properties |
| 41 |
-------------------- |
| 42 |
|
| 43 |
Properties can carry multiple values and additional information such as a definition. |
| 44 |
|
| 45 |
.. literalinclude:: examples/annotations.py |
| 46 |
:lines: 33-35 |
| 47 |
|
| 48 |
Reading the values will return a tuple. This has the background that one cannot change a tuple. Changing the values stored in a *Property* can be done e.g. by the ``extend_values`` method |
| 49 |
|
| 50 |
.. literalinclude:: examples/annotations.py |
| 51 |
:lines: 37-38 |
| 52 |
|
| 53 |
One can extend it by single values or by lists of values. |
| 54 |
|
| 55 |
The data type of all values must, however, be the same. Adding a number to the list of strings will fail with a ``TypeError``: |
| 56 |
|
| 57 |
.. literalinclude:: examples/annotations.py |
| 58 |
:lines: 40-43 |
| 59 |
|
| 60 |
.. code-block:: text |
| 61 |
|
| 62 |
New data type '<class 'numpy.int64'>' is inconsistent with the Property's data type '<class 'numpy.str_'>' |
| 63 |
|
| 64 |
**Note:** Once the property has been created, the data type can't be changed. One would need to create a replacement with the desired data type. |
| 65 |
|
| 66 |
Finding annotations |
| 67 |
------------------- |
| 68 |
|
| 69 |
If we do not know the exact path of the *Section* we are looking for, we need to search it by passing a function (in this case a lambda function) to the ``find_section`` method. The following code shows two examples in which we look first for a section with a given name or second a section which contains a property with a certain name. |
| 70 |
|
| 71 |
.. literalinclude:: examples/annotations.py |
| 72 |
:lines: 28 - 31 |
| 73 |
|
| 74 |
The result of the ``find_sections`` will always be a list which may be empty if no match was found. Therefore, the call in the last line is to some extent risky and would lead to an ``OutOfBounds`` exception if the search failed. |