Debian Codegraph @

docs/source/examples/multipleTimeSeries.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """Copyright © 2014 German Neuroinformatics Node (G-Node)
4
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted under the terms of the BSD License. See
9 LICENSE file in the root of the Project.
10
11 Author: Jan Grewe <[email protected]>
12
13 See https://github.com/G-node/nix/wiki for more information.
14
15 """
16
17 import nixio as nix
18 import numpy as np
19 import matplotlib.pylab as plt
20
21 import docutils
22
23
24 def create_data(duration=1, freq=10, stepsize=0.01):
25 x = np.arange(0, duration * 2 * np.pi, stepsize)
26 sine = np.sin(freq * x)
27 cosine = np.cos(freq * x)
28 return x, sine, cosine
29
30
31 def plot_data(data_array):
32 set_dim = data_array.dimensions[0]
33 x_axis = data_array.dimensions[1]
34 x = np.arange(0, data_array.shape[1])
35 x = x * x_axis.sampling_interval + x_axis.offset
36 y = np.zeros(data_array.shape)
37 data_array.read_direct(y)
38 for i, label in enumerate(set_dim.labels):
39 plt.plot(x, y[i, :], label=label)
40
41 plt.xlabel(x_axis.label + " [" + x_axis.unit + "]")
42 plt.ylabel(data_array.label + " [" + data_array.unit + "]")
43 plt.title(data_array.name)
44 plt.xlim(0, np.max(x))
45 plt.ylim((1.1 * np.min(y), 1.1 * np.max(y)))
46 plt.legend()
47 if docutils.is_running_under_pytest():
48 plt.close()
49 else:
50 plt.savefig('multiple_time_series.png')
51 plt.show()
52
53 def main():
54 # fake some data
55 duration = 2
56 frequency = 2
57 stepsize = 0.02
58 _, sine, cosine = create_data(duration, frequency, stepsize)
59
60 # create a new file overwriting any existing content
61 file_name = 'multiple_regular_data_example.h5'
62 file = nix.File.open(file_name, nix.FileMode.Overwrite)
63
64 # create a 'Block' that represents a grouping object. Here, the recording session.
65 # it gets a name and a type
66 block = file.create_block("block name", "nix.session")
67 # create a 'DataArray' to take the data, add some information about the signal
68 y = np.vstack((sine, cosine))
69 data = block.create_data_array("waveforms", "nix.regular_sampled.multiple_series", data=y, label="voltage", unit="mV")
70 # descriptor for first dimension is a set
71 data.append_set_dimension(labels=['sin', 'cos'])
72 # add a descriptor for the xaxis
73 data.append_sampled_dimension(stepsize, offset=0.0, label="time", unit="s")
74
75 # let's plot the data from the stored information
76 plot_data(data)
77 file.close()
78
79
80 if __name__ == "__main__":
81 main()