| 1 |
|
| 2 |
|
| 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 scipy.signal as signal |
| 20 |
import matplotlib.pylab as plt |
| 21 |
|
| 22 |
import lif |
| 23 |
import docutils |
| 24 |
|
| 25 |
def fake_neuron(stepsize=0.001, offset=.8, sta_offset=1000): |
| 26 |
stimulus = np.random.randn(102000) * 3.5 |
| 27 |
b, a = signal.butter(1, 12.5, fs=1. / stepsize, btype="low") |
| 28 |
stimulus = signal.filtfilt(b, a, stimulus) |
| 29 |
stimulus = stimulus[1000:-1000] |
| 30 |
lif_model = lif.LIF(stepsize=stepsize, offset=offset) |
| 31 |
time, v, spike_times = lif_model.run_stimulus(stimulus) |
| 32 |
snippets = np.zeros((len(spike_times), 2 * sta_offset)) |
| 33 |
|
| 34 |
for i, t in enumerate(spike_times): |
| 35 |
index = int(round(t / stepsize)) |
| 36 |
if index < sta_offset: |
| 37 |
snip = stimulus[0:index + sta_offset] |
| 38 |
snippets[i, -len(snip):] = snip |
| 39 |
elif (index + sta_offset) > len(stimulus): |
| 40 |
snip = stimulus[index - sta_offset:] |
| 41 |
snippets[i, 0:len(snip)] = snip |
| 42 |
else: |
| 43 |
snippets[i, :] = stimulus[index - sta_offset:index + sta_offset] |
| 44 |
|
| 45 |
return time, v, spike_times, snippets |
| 46 |
|
| 47 |
|
| 48 |
def plot_data(tag): |
| 49 |
data_array = tag.references[0] |
| 50 |
voltage = np.zeros(data_array.shape) |
| 51 |
data_array.read_direct(voltage) |
| 52 |
|
| 53 |
x_axis = data_array.dimensions[0] |
| 54 |
time = x_axis.axis(data_array.data_extent[0]) |
| 55 |
|
| 56 |
spike_times = tag.positions[:] |
| 57 |
|
| 58 |
feature_data_array = tag.features[0].data |
| 59 |
snippets = tag.features[0].data[:] |
| 60 |
|
| 61 |
single_snippet = tag.feature_data(3, 0)[:] |
| 62 |
|
| 63 |
snippet_time_dim = feature_data_array.dimensions[1] |
| 64 |
snippet_time = snippet_time_dim.axis(feature_data_array.data_extent[1]) |
| 65 |
|
| 66 |
response_axis = plt.subplot2grid((2, 2), (0, 0), rowspan=1, colspan=2) |
| 67 |
single_snippet_axis = plt.subplot2grid((2, 2), (1, 0), rowspan=1, colspan=1) |
| 68 |
average_snippet_axis = plt.subplot2grid((2, 2), (1, 1), rowspan=1, colspan=1) |
| 69 |
|
| 70 |
response_axis.plot(time, voltage, color='dodgerblue', label=data_array.name) |
| 71 |
response_axis.scatter(spike_times, np.ones(spike_times.shape) * np.max(voltage), color='red', label=tag.name) |
| 72 |
response_axis.set_xlabel(x_axis.label + ((" [" + x_axis.unit + "]") if x_axis.unit else "")) |
| 73 |
response_axis.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else "")) |
| 74 |
response_axis.set_title(data_array.name) |
| 75 |
response_axis.set_xlim(0, np.max(time)) |
| 76 |
response_axis.set_ylim((1.2 * np.min(voltage), 1.2 * np.max(voltage))) |
| 77 |
response_axis.legend(ncol=2, loc="lower center", fontsize=8) |
| 78 |
|
| 79 |
single_snippet_axis.plot(snippet_time, single_snippet.T, color="red", label=("snippet No 4")) |
| 80 |
single_snippet_axis.set_xlabel(snippet_time_dim.label + ((" [" + snippet_time_dim.unit + "]") if snippet_time_dim.unit else "")) |
| 81 |
single_snippet_axis.set_ylabel(feature_data_array.label + ((" [" + feature_data_array.unit + "]") if feature_data_array.unit else "")) |
| 82 |
single_snippet_axis.set_title("single stimulus snippet") |
| 83 |
single_snippet_axis.set_xlim(np.min(snippet_time), np.max(snippet_time)) |
| 84 |
single_snippet_axis.set_ylim((1.2 * np.min(snippets[3, :]), 1.2 * np.max(snippets[3, :]))) |
| 85 |
single_snippet_axis.legend() |
| 86 |
|
| 87 |
mean_snippet = np.mean(snippets, axis=0) |
| 88 |
std_snippet = np.std(snippets, axis=0) |
| 89 |
average_snippet_axis.fill_between(snippet_time, mean_snippet + std_snippet, mean_snippet - std_snippet, color="tab:red", alpha=0.25) |
| 90 |
average_snippet_axis.plot(snippet_time, mean_snippet, color="red", label=(feature_data_array.name + str(4))) |
| 91 |
average_snippet_axis.set_xlabel(snippet_time_dim.label + ((" [" + snippet_time_dim.unit + "]") if snippet_time_dim.unit else "")) |
| 92 |
average_snippet_axis.set_ylabel(feature_data_array.label + ((" [" + feature_data_array.unit + "]") if feature_data_array.unit else "")) |
| 93 |
average_snippet_axis.set_title("spike-triggered average") |
| 94 |
average_snippet_axis.set_xlim(np.min(snippet_time), np.max(snippet_time)) |
| 95 |
average_snippet_axis.set_ylim((1.2 * np.min(mean_snippet - std_snippet), 1.2 * np.max(mean_snippet + std_snippet))) |
| 96 |
|
| 97 |
plt.subplots_adjust(left=0.15, top=0.875, bottom=0.1, right=0.98, hspace=0.35, wspace=0.25) |
| 98 |
plt.gcf().set_size_inches((5.5, 4.5)) |
| 99 |
|
| 100 |
if docutils.is_running_under_pytest(): |
| 101 |
plt.close() |
| 102 |
else: |
| 103 |
plt.show() |
| 104 |
|
| 105 |
|
| 106 |
def main(): |
| 107 |
stepsize = 0.0001 |
| 108 |
sta_offset = 1000 |
| 109 |
time, voltage, spike_times, sts = fake_neuron(stepsize=0.0001, sta_offset=sta_offset) |
| 110 |
|
| 111 |
|
| 112 |
file_name = 'spike_features.h5' |
| 113 |
file = nixio.File.open(file_name, nixio.FileMode.Overwrite) |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
block = file.create_block("block name", "nix.session") |
| 118 |
|
| 119 |
|
| 120 |
data = block.create_data_array("membrane voltage", "nix.regular_sampled.time_series", data=voltage, label="membrane voltage") |
| 121 |
|
| 122 |
data.append_sampled_dimension(stepsize, label="time", unit="s") |
| 123 |
|
| 124 |
|
| 125 |
positions = block.create_data_array("times", "nix.events.spike_times", data=spike_times) |
| 126 |
positions.append_range_dimension_using_self() |
| 127 |
|
| 128 |
|
| 129 |
multi_tag = block.create_multi_tag("spike times", "nix.events.spike_times", positions) |
| 130 |
multi_tag.references.append(data) |
| 131 |
|
| 132 |
|
| 133 |
snippets = block.create_data_array("spike triggered stimulus", "nix.regular_sampled.multiple_series", data=sts, label="stimulus", unit="nA") |
| 134 |
snippets.append_set_dimension() |
| 135 |
snippets.append_sampled_dimension(stepsize, offset=-sta_offset * stepsize, label="time", unit="s") |
| 136 |
|
| 137 |
|
| 138 |
multi_tag.create_feature(snippets, nixio.LinkType.Indexed) |
| 139 |
|
| 140 |
|
| 141 |
plot_data(multi_tag) |
| 142 |
file.close() |
| 143 |
|
| 144 |
|
| 145 |
if __name__ == '__main__': |
| 146 |
main() |