Debian Codegraph @

docs/source/examples/regularlySampledData.py

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