Debian Codegraph @

docs/source/data_handling.rst

raw
1 .. toctree::
2 :maxdepth: 1
3
4 Working with data
5 =================
6
7 Storing data is one thing, but we want to work with it. The following examples illustrate reading of data from *DataArray*, *Tag* and *MultiTag* entities. We will use the dummy dataset already used in the :doc:`tagging <./tagging>` example. The figure below shows what is stored in the dataset.
8
9 .. figure:: ./images/tag1.png
10 :alt: a system's response to a stimulus
11
12 At some instance in time a system was exposed to a stimulus that leads to the system's response. The response has been recorded and stored in a *DataArray*, a *Tag* is used to highlight the "stimulus-on" segment of the data.
13
14 .. literalinclude:: examples/tagging_example.py
15 :caption: :download:`example code <examples/tagging_example.py>`
16 :lines: 78 - 90
17
18 In this example we know the interesting entities by name, i.e. the
19 *DataArray* is called **response** and the *Tag* is called **stimulus**.
20 In cases in which we have no clue about the names, we just have to
21 browse the file or `search <./finding_things.md>`__ by name or type.
22
23 Reading data
24 ------------
25
26 The first and maybe most common problem is to read the data stored in a
27 *DataArray*.
28
29 Reading all data
30 ~~~~~~~~~~~~~~~~
31
32 In *NIX* when you open a *DataArray* the stored data is **not** automatically read from file. This keeps the object lightweight and easy to create. To read the data you can simply access it in a numpy style:
33
34 .. literalinclude:: examples/tagging_example.py
35 :caption: :download:`example code <examples/tagging_example.py>`
36 :lines: 71 - 73
37
38 There are a few noteworthy things:
39
40 - We use some previous knowledge here. For one, we know the names of the entities. Further, we know that the data is 1-D and the single dimension is a ``SampledDimension``. If these things are not known, the NIX library offers the necessary functions to get this information.
41 - ``DataArray.shape`` returns a tuple with the data shape.
42 - ``DataArray.dtype`` returns the data type.
43 - To find out the ``DimensionType``, we need to access the dimension:
44
45 .. code-block:: python
46
47 for dim in data_array.dimensions:
48 print(dim.dimension_type)
49
50
51 Reading partial data
52 ~~~~~~~~~~~~~~~~~~~~
53
54 In other instances it might be wanted to read only parts of the data.
55 Reading slices of the data is straight forward using the numpy style.
56
57 .. literalinclude:: examples/tagging_example.py
58 :caption: :download:`example code <examples/tagging_example.py>`
59 :lines: 98-101
60
61 An alternative approach is to use the ``DataArray.get_slice`` method which by default works with indices but can also work in data coordinates. E.g. we know that the data is 1-D and covers a span of 3.5s and we want to have the data in the interval 0.5s through 1.75s. The method returns a ``nixio.DataView`` object. The actual reading is done by accessing the data.
62
63 .. literalinclude:: examples/tagging_example.py
64 :caption: :download:`example code <examples/tagging_example.py>`
65 :lines: 103-106
66 :emphasize-lines: 3
67
68 The arguments ``positions`` and ``extents`` are passed as lists. There must be one entry for each dimension of the data. In this case, since the data is 1-D, positions and extents are 1-element lists.
69 Note: the slice is defined by the starting point(s) and the *extent(s)*, not with start and end points.
70
71 Reading tagged data
72 ~~~~~~~~~~~~~~~~~~~
73
74 *Tag* and *MultiTag* tag single or multiple points or regions in data stored in the referenced *DataArrays* (see `tagging <./tagging.md>`__ for more information and the example data created in the `example <#mtag_regions>`__ will be used in the following code snippets).
75
76 .. figure:: ./images/multiple_regions.png
77 :alt: tagging multiple segments
78
79 Plot of the data created by this :download:`example <examples/multiple_regions.py>` on tagging multiple regions in data.
80
81 In order to read the data that belongs to the highlighted region(s) *Tag* and *MultiTag* define the ``tagged_data`` methods which return ``nixio.DataView`` objects from which the data is read as shown above. The following code snippet shows how to use these function:
82
83 .. literalinclude:: examples/multiple_regions.py
84 :lines: 86 - 97
85 :emphasize-lines: 10
86 :caption: Reading data segments tagged by the *Tag* or *MultiTag* can be done using the ``tagged_data`` method (:download:`example code <examples/multiple_regions.py>`).
87
88 The *MultiTag* version of the ``tagged_data`` method takes two arguments. The first is the index of the tagged region (0 for the first), the second argument is the name of the referenced *DataArray* (you can also use the index or the id). Since the *Tag* tags only a single region, it takes only one argument, i.e. the name (id, index) of the referenced *DataArray*.
89
90 .. figure:: ./images/reading_tagged_data.png
91 :alt: reading tagged data
92
93
94 Analogously, the feature data attached to the *Tag* or *MultiTag* can be
95 obtained using the ``feature_data`` methods.
96
97 .. literalinclude:: examples/multiple_regions.py
98 :lines: 69 - 76
99 :emphasize-lines: 6, 7
100 :caption: ``feature_data`` works analogously, the first argument is the index of the tagged region, the second the name (or id or index) of the feature. Here the feature stores a single number, i.e. the frequency of the stimulus for each tagged region which is plotted below the highlighted regions in the figure above (:download:`example code <examples/multiple_regions.py>`).