Debian Codegraph @

docs/source/examples/untaggedFeature.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
18 import lif
19 import nixio
20 import numpy as np
21 import scipy.signal as signal
22 import matplotlib.pylab as plt
23
24 import docutils
25
26
27 def fake_neuron(stepsize=0.001, offset=.8):
28 stimulus = np.random.randn(82000) * 2.5
29
30 b, a = signal.butter(2, 12.5, fs=1 / stepsize, btype="low")
31 stimulus = signal.filtfilt(b, a, stimulus[:])
32 stimulus = stimulus[1000:-1000]
33 s = np.hstack((np.zeros(10000), stimulus, np.zeros(10000)))
34 lif_model = lif.LIF(stepsize=stepsize, offset=offset)
35 time, v, spike_times = lif_model.run_stimulus(s)
36
37 stimulus_onset = 10000 * stepsize
38 stimulus_duration = len(stimulus) * stepsize
39
40 return time, v, stimulus, stimulus_onset, stimulus_duration
41
42
43 def plot_data(tag):
44 data_array = tag.references[0]
45 voltage = data_array[:]
46
47 x_axis = data_array.dimensions[0]
48 time = x_axis.axis(data_array.data_extent[0])
49
50 stimulus_onset = tag.position
51 stimulus_duration = tag.extent
52
53 stimulus = tag.feature_data(0)
54 stimulus_array = tag.features[0].data
55
56 stim_time_dim = stimulus_array.dimensions[0]
57 stimulus_time = stim_time_dim.axis(stimulus_array.data_extent[0])
58
59 response_axis = plt.subplot2grid((2, 2), (0, 0), rowspan=1, colspan=2)
60 response_axis.tick_params(direction='out')
61 response_axis.spines['top'].set_color('none')
62 response_axis.spines['right'].set_color('none')
63 response_axis.xaxis.set_ticks_position('bottom')
64 response_axis.yaxis.set_ticks_position('left')
65
66 stimulus_axis = plt.subplot2grid((2, 2), (1, 0), rowspan=1, colspan=2)
67 stimulus_axis.tick_params(direction='out')
68 stimulus_axis.spines['top'].set_color('none')
69 stimulus_axis.spines['right'].set_color('none')
70 stimulus_axis.xaxis.set_ticks_position('bottom')
71 stimulus_axis.yaxis.set_ticks_position('left')
72
73 response_axis.plot(time, voltage, color='tab:blue', label=data_array.name, zorder=1)
74 response_axis.set_xlabel(x_axis.label + ((" [" + x_axis.unit + "]") if x_axis.unit else ""))
75 response_axis.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else ""))
76 response_axis.set_xlim(0, np.max(time))
77 response_axis.set_ylim((1.2 * np.min(voltage), 1.2 * np.max(voltage)))
78 response_axis.barh((np.max(voltage) - np.min(voltage)) / 2, stimulus_duration, np.min(voltage) - np.max(voltage),
79 stimulus_onset, color='silver', alpha=0.5, zorder=0, label="stimulus epoch")
80 response_axis.legend(fontsize=9, ncol=2, loc=9)
81
82 stimulus_axis.plot(stimulus_time, stimulus[:], color="slategray", label="stimulus")
83 stimulus_axis.set_xlabel(stim_time_dim.label + ((" [" + stim_time_dim.unit + "]") if stim_time_dim.unit else ""))
84 stimulus_axis.set_ylabel(stimulus_array.label + ((" [" + stimulus_array.unit + "]") if stimulus_array.unit else ""))
85 stimulus_axis.set_xlim(np.min(stimulus_time), np.max(stimulus_time))
86 stimulus_axis.set_ylim(1.2 * np.min(stimulus), 1.2 * np.max(stimulus))
87 stimulus_axis.legend(fontsize=9, loc=1)
88
89 plt.subplots_adjust(left=0.15, top=0.875, bottom=0.1, right=0.98, hspace=0.45, wspace=0.25)
90 plt.gcf().set_size_inches((5.5, 5))
91 if docutils.is_running_under_pytest():
92 plt.close()
93 else:
94 # plt.savefig("../images/untagged_feature.png")
95 plt.show()
96
97
98 if __name__ == '__main__':
99 stepsize = 0.0001 # s
100 time, voltage, stimulus, stim_onset, stim_duration = fake_neuron(stepsize=stepsize)
101
102 # create a new file overwriting any existing content
103 file_name = 'untagged_feature.h5'
104 file = nixio.File.open(file_name, nixio.FileMode.Overwrite)
105
106 # create a 'Block' that represents a grouping object. Here, the recording session.
107 # it gets a name and a type
108 block = file.create_block("block name", "nix.session")
109
110 # create a 'DataArray' to take the membrane voltage
111 data = block.create_data_array("membrane voltage", "nix.sampled.time_series", data=voltage,
112 label="membrane voltage", unit="mV")
113 data.append_sampled_dimension(stepsize, label="time", unit="s")
114
115 # create a stimulus DataArray
116 stim = block.create_data_array("stimulus", "nix.sampled.time_series", data=stimulus,
117 label="current stimulus", unit="nA")
118 stim.append_sampled_dimension(stepsize, label="time", unit="s")
119
120 # create the Tag to highlight the stimulus-on segment
121 tag = block.create_tag("stimulus presentation", "nix.epoch.stimulus_presentation", [stim_onset])
122 tag.extent = [stim_duration]
123 tag.references.append(data)
124
125 # set stimulus as untagged feature of the tag
126 tag.create_feature(stim, nixio.LinkType.Untagged)
127
128 # let's plot the data from the stored information
129 plot_data(tag)
130 file.close()