| 1 |
.. toctree:: |
| 2 |
:maxdepth: 2 |
| 3 |
|
| 4 |
Storing data |
| 5 |
============ |
| 6 |
|
| 7 |
When storing data, we have two main requirements: 1. We want to be able |
| 8 |
to store **n-dimensional** data structures. 2. The data structures must |
| 9 |
be **self-explanatory**, that is, they must contain sufficient |
| 10 |
information to draw a basic plot of the data. |
| 11 |
|
| 12 |
|sampled_plot| |
| 13 |
.. _sampled_plot: |
| 14 |
|
| 15 |
Considering the simple plot above, we can list all |
| 16 |
information that it shows and by extension, that needs to be stored in |
| 17 |
order to reproduce it. |
| 18 |
|
| 19 |
- the data (voltage measurements) |
| 20 |
- the y-axis labeling, i.e. label (voltage) and unit (mV) |
| 21 |
- the x-axis labeling, i.e. label (time) and unit (s) |
| 22 |
- the x-position for each data point |
| 23 |
- a title/legend |
| 24 |
|
| 25 |
In this, and in most cases, it would be inefficient to store x-, and |
| 26 |
y-position for each plotted point. The voltage measurements have been done |
| 27 |
in regular (time) intervals. Thus, we rather need to store the measured |
| 28 |
values and a definition of the x-axis consisting of an offset, the |
| 29 |
sampling interval, a label, and a unit. |
| 30 |
|
| 31 |
This is exactly the approach chosen in *NIX*. For each dimension of the |
| 32 |
data a **dimension descriptor** must be given. In *NIX* we define three |
| 33 |
dimension descriptors: |
| 34 |
|
| 35 |
1. *SampledDimension*: Used if a dimension is sampled at regular |
| 36 |
intervals. |
| 37 |
2. *RangeDimension*: Used if a dimension is sampled at irregular |
| 38 |
intervals. The instances at which the data has been sampled are stored as *ticks*. These ticks may also be stored in a *DataArray* or *DataFrame*, the Range Dimension can link to it. |
| 39 |
3. *SetDimension*: Used for dimensions that represent categories rather |
| 40 |
than physical quantities. |
| 41 |
|
| 42 |
The DataArray |
| 43 |
------------- |
| 44 |
|
| 45 |
The *DataArray* is the most central entity of the *NIX* data model. As |
| 46 |
almost all other *NIX*-entities it requires a *name* and a *type*. Both |
| 47 |
are not restricted but names must be **unique** within a *Block*. *type* |
| 48 |
information can be used to introduce semantic meaning and |
| 49 |
domain-specificity. Upon creation, a unique ID will be assigned to the |
| 50 |
*DataArray*. |
| 51 |
|
| 52 |
The *DataArray* stores the actual data together with label and unit. In |
| 53 |
addition, the *DataArray* needs a dimension descriptor for each data |
| 54 |
dimension. The following snippet shows how to create a *DataArray* and |
| 55 |
store data in it. |
| 56 |
|
| 57 |
.. literalinclude:: examples/regularlySampledData.py |
| 58 |
:caption: Storing data in a DataArray (:download:`example code <examples/regularlySampledData.py>`). |
| 59 |
:language: python |
| 60 |
:lines: 57 - 61 |
| 61 |
|
| 62 |
As promised, the *DataArray* contains all information to create a basic plot (see the figure above). |
| 63 |
|
| 64 |
.. literalinclude:: examples/regularlySampledData.py |
| 65 |
:language: python |
| 66 |
:lines: 29 - 40 |
| 67 |
:emphasize-lines: 2, 3, 7-8 |
| 68 |
|
| 69 |
The highlighted lines emphasize how information from the dimension descriptor (a *SampledDimension*) and the *DataArray* itself are used for labeling the plot. |
| 70 |
|
| 71 |
In the example shown above, the *NIX* library will figure out the |
| 72 |
dimensionality of the data, the shape of the data and its type. The data |
| 73 |
type and the dimensionality (i.e. the number of dimensions) are fixed |
| 74 |
once the *DataArray* has been created. The actual size of the |
| 75 |
*DataArray* can be changed during the life-time of the entity. |
| 76 |
|
| 77 |
In case you need more control, *DataArrays* can be created empty for |
| 78 |
later filling e.g. during data acquisition. |
| 79 |
|
| 80 |
.. code-block:: python |
| 81 |
|
| 82 |
data_array = block.create_data_array("sinewave", "nix.sampled", dtype=nixio.DataType.Double, shape=(100)); |
| 83 |
|
| 84 |
The resulting *DataArray* will have an initial size (100 elements) which |
| 85 |
will be automatically resized, if required. The data type is set to |
| 86 |
``nixio.DataType.Double``. The *NIX* library will further try to convert passed data to the |
| 87 |
defined data type, if possible. **Note:** Data type and rank (i.e. the number of dimensions) cannot be altered after the *DataArray* has been created. |
| 88 |
|
| 89 |
Data is then set by calling |
| 90 |
|
| 91 |
.. code-block:: python |
| 92 |
|
| 93 |
array.write_direct(voltage) |
| 94 |
|
| 95 |
Data is read from a DataArray by accessing it in numpy style. |
| 96 |
|
| 97 |
.. code-block:: python |
| 98 |
|
| 99 |
# read the full data |
| 100 |
data = array[:] |
| 101 |
|
| 102 |
# reading only the first 100 points |
| 103 |
data = array[:100] |
| 104 |
|
| 105 |
# replacing data |
| 106 |
array[:100] = np.zeros(100) |
| 107 |
|
| 108 |
*DataArrays* can also be extended by ``appending`` data. |
| 109 |
|
| 110 |
.. code-block:: python |
| 111 |
|
| 112 |
array = block.create_data_array("test data", "test", data=np.random.randn(100)) |
| 113 |
print(array.shape) |
| 114 |
array.append(np.ones(100)) |
| 115 |
print(array.shape) |
| 116 |
|
| 117 |
|
| 118 |
Dimensions |
| 119 |
---------- |
| 120 |
|
| 121 |
Within the *DataArray* we can store n-dimensional data. For each |
| 122 |
dimension we must provide a *dimension descriptor*. The following |
| 123 |
introduces the individual descriptors. |
| 124 |
|
| 125 |
SampledDimension |
| 126 |
**************** |
| 127 |
|
| 128 |
.. figure:: ./images/regular_sampled.png |
| 129 |
:alt: 1-D regular sampled data |
| 130 |
|
| 131 |
|
| 132 |
Here we have the same situation as before, the data has been sampled in |
| 133 |
regular intervals. That is, the time between successive data points is |
| 134 |
always the same. The x-axis can be fully described with just a few |
| 135 |
parameters: |
| 136 |
|
| 137 |
1. sampling interval |
| 138 |
2. offset |
| 139 |
3. label |
| 140 |
4. unit |
| 141 |
|
| 142 |
The *SampledDimension* entity is used in such situations and needs to be |
| 143 |
added to the *DataArray* entity upon creation: |
| 144 |
|
| 145 |
.. literalinclude:: examples/regularlySampledData.py |
| 146 |
:language: python |
| 147 |
:lines: 57 - 60 |
| 148 |
:emphasize-lines: 4 |
| 149 |
:caption: A dimension in which the data has been gathered in regular intervals is described using a *SampledDimension* (:download:`example code <examples/regularlySampledData.py>` |
| 150 |
|
| 151 |
|
| 152 |
SetDimension |
| 153 |
************ |
| 154 |
|
| 155 |
.. figure:: ./images/set_dimension.png |
| 156 |
:alt: simple plot with categories |
| 157 |
|
| 158 |
If we need to store data that falls into categories, i.e. the |
| 159 |
x-positions are not numeric or the dimension does not have a natural |
| 160 |
order, a *SetDimension* is used. It stores a label for each entry along |
| 161 |
the described dimension. |
| 162 |
|
| 163 |
.. literalinclude:: examples/categoryData.py |
| 164 |
:language: python |
| 165 |
:lines: 16 - 17 |
| 166 |
:caption: A categorical data dimension is described with a *SetDimension* which (optionally) stores labels for each category (:download:`example code <examples/categoryData.py>`) |
| 167 |
|
| 168 |
|
| 169 |
RangeDimension |
| 170 |
************** |
| 171 |
|
| 172 |
.. figure:: ./images/irregular_sampled.png |
| 173 |
:alt: 1-D irregularly sampled data |
| 174 |
|
| 175 |
|
| 176 |
A signal similar to what we had before is recorded but this time the temporal |
| 177 |
distance between the measurements is not regular. Storing this kind |
| 178 |
of data is not as efficient as in the regularly sampled case since we need to store the time of each measurement and the measured value. The following information needs to be stored to describe the dimension: |
| 179 |
|
| 180 |
1. x-positions of the data points, i.e. *ticks* |
| 181 |
2. label |
| 182 |
3. unit |
| 183 |
|
| 184 |
In this kind of dimension we store a *range* of ticks, therefore the |
| 185 |
name *RangeDimension*. It needs to be added to the *DataArray* when it |
| 186 |
is created. |
| 187 |
|
| 188 |
.. literalinclude:: examples/irregularlySampledData.py |
| 189 |
:caption: Storing irregularly sampled data (:download:`example code <examples/irregularlySampledData.py>`). |
| 190 |
:language: python |
| 191 |
:lines: 45 - 58 |
| 192 |
:emphasize-lines: 14 |
| 193 |
|
| 194 |
**Note:** The *ticks* of a *RangeDimension* must be numeric and ascending. |
| 195 |
|
| 196 |
The *RangeDimension* can do more. Consider the case that the times of an event are stored: |
| 197 |
|
| 198 |
.. figure:: ./images/alias_range.png |
| 199 |
:alt: 1-D event data |
| 200 |
|
| 201 |
For example these might be the times of action potentials (aka spikes) recorded in a nerve. In such a case it is basically the x-values that are of interest. It would be inefficient to store them twice, first as values in the *DataArray* and then again as ticks in the dimension descriptor. |
| 202 |
In such cases the *RangeDimension* is set up to link to the *DataArray* itself. |
| 203 |
|
| 204 |
.. literalinclude:: examples/rangeDimensionLink.py |
| 205 |
:lines: 12 - 17 |
| 206 |
:language: python |
| 207 |
:caption: A *RangeDimension* can link to a *DataArray* as a source of the ticks (:download:`exmaple code <examples/rangeDimensionLink.py>`). |
| 208 |
:emphasize-lines: 6 |
| 209 |
|
| 210 |
In the highlighted line we use a convenience function to establish the link between the dimension descriptor and the data stored via the dimensions of its own DataArray. |
| 211 |
|
| 212 |
This function is a shortcut for: |
| 213 |
|
| 214 |
.. code-block:: python |
| 215 |
|
| 216 |
rdim = data_array.append_range_dimension() |
| 217 |
rdim.create_link(data_array, index=[-1]) |
| 218 |
|
| 219 |
In the same way one can set up the *RangeDimension* to use a *DataFrame*. The ``index`` defines which slice of the data stored in the *DataArray/Frame* holds the ticks. The slice must be a 1-D vector within the linked data. If, for example, the *DataArray* is 2-D and the ticks are in the 3rd column then the index would be ``[-1, 3]``. The ``-1`` marks the dimension along which to look for the ticks. When linking *DataFrames* the index notes the column. |
| 220 |
|
| 221 |
|
| 222 |
Advanced storing |
| 223 |
---------------- |
| 224 |
|
| 225 |
Data compression |
| 226 |
**************** |
| 227 |
|
| 228 |
By default data is stored uncompressed. If you want to use data |
| 229 |
compression this can be enabled by providing the |
| 230 |
``nixio.Compression.DeflateNormal`` flag during file-opening: |
| 231 |
|
| 232 |
.. code-block:: python |
| 233 |
|
| 234 |
import nixio |
| 235 |
f = nixio.File.open("test.nix", nixio.FileMode.Overwrite, compression=nixio.Compression.DeflateNormal) |
| 236 |
|
| 237 |
By doing this, all data will be stored with compression enabled, if |
| 238 |
not explicitly stated otherwise. At any time you can select or deselect |
| 239 |
compression by providing a ``nixio.Compression`` flag during *DataArray* |
| 240 |
creation. Available flags are: |
| 241 |
|
| 242 |
- ``nixio.Compression.Auto``: compression as defined during |
| 243 |
file-opening. |
| 244 |
- ``nixio.Compression.DeflateNormal``: use compression (fixed level). |
| 245 |
- ``nixio.CompressionNo``: no compression. |
| 246 |
|
| 247 |
.. code-block:: python |
| 248 |
|
| 249 |
data_array = b.create_data_array("some data", "nix.sampled", data, compression=nixio.Compression.DeflateNormal); |
| 250 |
|
| 251 |
Note the following: |
| 252 |
|
| 253 |
1. Compression comes with a little cost of read-write performance. |
| 254 |
2. Data compression is fixed once the *DataArray* has been created, it |
| 255 |
cannot be changed afterwards. |
| 256 |
3. Opening and extending a compressed *DataArray* is easily possible |
| 257 |
even if the file has not been opened with the |
| 258 |
``nixio.Compression.DeflateNormal`` flag. |
| 259 |
|
| 260 |
Supported DataTypes |
| 261 |
******************* |
| 262 |
|
| 263 |
*DataArrays* can store a multitude of different data types. The |
| 264 |
supported data types are defined in the ``nixio.DataType`` enumeration: |
| 265 |
|
| 266 |
- ``nixio.DataType.Bool``: 1 bit boolean value. |
| 267 |
- ``nixio.DataType.Char``: 8 bit charater. |
| 268 |
- ``nixio.DataType.Float``: floating point number. |
| 269 |
- ``nixio.DataType.Double``: double precision floating point number. |
| 270 |
- ``nixio.DataType.Int8``: 8 bit integer, signed. |
| 271 |
- ``nixio.DataType.Int16``: 16 bit integer, signed. |
| 272 |
- ``nixio.DataType.Int32``: 32 bit integer, signed. |
| 273 |
- ``nixio.DataType.Int64``: 64 bit integer, signed. |
| 274 |
- ``nixio.DataType.UInt8``: 8 bit unsigned int. |
| 275 |
- ``nixio.DataType.UInt16``: 16 bit unsigned int. |
| 276 |
- ``nixio.DataType.UInt32``: 32 bit unsigned int. |
| 277 |
- ``nixio.DataType.UInt64``: 64 bit unsigned int. |
| 278 |
- ``nixio.DataType.String``: string value. |
| 279 |
- ``nixio.DataType.Opaque``: data type for binary data. |
| 280 |
|
| 281 |
The data type of a *DataArray* must be specified at creation time and |
| 282 |
cannot be changed. In many cases, the *NIX* library will try to handle |
| 283 |
data types transparently and cast data to the data type specified for |
| 284 |
the *DataArray* in which it is supposed to be stored. |
| 285 |
|
| 286 |
|
| 287 |
Extending datasets on the fly |
| 288 |
***************************** |
| 289 |
|
| 290 |
The dimensionality (aka rank) and the stored *DataType* of a *DataArray* |
| 291 |
are fixed. The actual size of the stored dataset, however, can |
| 292 |
changed. This is often used when you acquire data continuously e.g. while |
| 293 |
recording during an experiment. In nixpy the resizing is handled transparently. |
| 294 |
|
| 295 |
The workflow would be: |
| 296 |
|
| 297 |
1. Preparations: Open a nix-file in ``nixio.FileMode.ReadWrite`` or |
| 298 |
``nixio.FileMode.Overwrite``. Create or open the *DataArray*. |
| 299 |
2. Acquire more data. |
| 300 |
3. Append the acquired data to the data array. |
| 301 |
4. Acquire more data. |
| 302 |
|
| 303 |
|
| 304 |
The following code shows how this works. |
| 305 |
|
| 306 |
.. literalinclude:: examples/continuousRecording.py |
| 307 |
:caption: Extending *DataArray* size e.g while recording continuously (:download:`example code <examples/continuousRecording.py>`) |
| 308 |
:lines: 49-65 |
| 309 |
:language: python |
| 310 |
|
| 311 |
|
| 312 |
**Note!** Selecting the initial shape defines the chunk size used to |
| 313 |
write the data to file. Choose it appropriately for the expected size |
| 314 |
increment. Selecting a size that is too small can severly affect |
| 315 |
efficiency. |
| 316 |
|
| 317 |
.. figure:: ./images/multiple_channels.png |
| 318 |
:alt: recording multiple channels |
| 319 |
|
| 320 |
|
| 321 |
.. |sampled_plot| image:: ./images/regular_sampled.png |
| 322 |
|
| 323 |
|
| 324 |
The DataFrame |
| 325 |
------------- |
| 326 |
|
| 327 |
The DataFrame stores tabular data in a rows-and-columns format. Each Column has a name, a data type, and, optionally, a unit and a definition. |
| 328 |
|
| 329 |
.. literalinclude:: examples/tabulardata.py |
| 330 |
:lines: 8 - 25 |
| 331 |
:caption: A DataFrame is created by providing the column definition which consists of a name and the data type that should be stored in each column. Units can be added later on. It is best to use OrderdDicts to make sure, that the column order is as defined (standard dictionaries do not guarantee the order of the items). :download:`example code <examples/tabulardata.py>` |
| 332 |
|
| 333 |
For a quick overview one can use the ``DataFrame.print_table()`` method |
| 334 |
|
| 335 |
.. code-block:: text |
| 336 |
|
| 337 |
column: name id time amplitude frequency |
| 338 |
unit: s mV Hz |
| 339 |
[0]: alpha e28e0735-1 20.18 5.0 100.0 |
| 340 |
[1]: beta 74942839-1 20.09 5.5 101.0 |
| 341 |
[2]: gamma 0886ef1a-2 20.05 5.1 100.0 |
| 342 |
[3]: delta 0ac53fd2-7 20.15 5.3 150.0 |
| 343 |
[4]: epsilon a54b35df-1 20.23 5.7 200.0 |
| 344 |
[5]: fi 70eb952c-0 20.07 5.2 300.0 |
| 345 |
[6]: zeta 130ad527-d 20.12 5.1 39.0 |
| 346 |
[7]: eta 042a460c-e 20.27 5.1 600.0 |
| 347 |
[8]: theta 1f2c1df8-f 20.15 5.6 400.0 |
| 348 |
[9]: iota c4598a1e-b 20.08 5.1 200.0 |
| 349 |
|
| 350 |
The DataFrame offers several methods to get basic information about the table: |
| 351 |
|
| 352 |
.. literalinclude:: examples/tabulardata.py |
| 353 |
:lines: 30-33 |
| 354 |
|
| 355 |
Leading to the following output: |
| 356 |
|
| 357 |
.. code-block:: text |
| 358 |
|
| 359 |
size, aka number of rows: 10 |
| 360 |
column names: ('name', 'id', 'time', 'amplitude', 'frequency') |
| 361 |
column definition: [('name', dtype('O'), None), ('id', dtype('O'), None), ('time', dtype('<f8'), 's'), ('amplitude', dtype('<f8'), 'mV'), ('frequency', dtype('<f8'), 'Hz')] |
| 362 |
column units: [None None 's' 'mV' 'Hz'] |
| 363 |
|
| 364 |
Reading of the table data is possible for individual cells, entire columns, or selected rows |
| 365 |
|
| 366 |
.. literalinclude:: examples/tabulardata.py |
| 367 |
:lines: 35-41 |
| 368 |
|
| 369 |
Which yields the following outputs: |
| 370 |
|
| 371 |
.. code-block:: text |
| 372 |
|
| 373 |
single cell by position: alpha |
| 374 |
single cell by name and row index: alpha |
| 375 |
Entire ID column: ['f0299d50-b' 'c74d3b7c-0' 'e27bb6f7-2' '8bac251b-7' 'aecc18d4-a' |
| 376 |
'268c8dea-f' '88de352a-d' '4bdac82c-b' '5671ed6e-b' '0a7bf5e4-9'] |
| 377 |
Two columns, id and name, joined: [('f0299d50-b', 'alpha') ('c74d3b7c-0', 'beta') ('e27bb6f7-2', 'gamma') |
| 378 |
('8bac251b-7', 'delta') ('aecc18d4-a', 'epsilon') ('268c8dea-f', 'fi') |
| 379 |
('88de352a-d', 'zeta') ('4bdac82c-b', 'eta') ('5671ed6e-b', 'theta') |
| 380 |
('0a7bf5e4-9', 'iota')] |
| 381 |
Entire rows with indices 0 and 2: [('alpha', 'f0299d50-b', 20.18, 5. , 100.) |
| 382 |
('gamma', 'e27bb6f7-2', 20.05, 5.1, 100.)] |