Debian Codegraph @

docs/source/examples/irregularlySampledData.py

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