| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Copyright © 2014 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.pyplot as plt |
| 21 |
|
| 22 |
import lif |
| 23 |
import docutils |
| 24 |
|
| 25 |
|
| 26 |
def fake_neuron(stepsize=0.001, offset=.8): |
| 27 |
stimulus = np.random.randn(102000) * 2.5 |
| 28 |
b, a = signal.butter(2, 7.5, fs=1. / stepsize, btype="low") |
| 29 |
stimulus = signal.filtfilt(b, a, stimulus) |
| 30 |
stimulus = stimulus[1000:-1000] |
| 31 |
|
| 32 |
lif_model = lif.LIF(stepsize=stepsize, offset=offset) |
| 33 |
time, v, spike_times = lif_model.run_stimulus(stimulus) |
| 34 |
return time, v, stimulus, spike_times |
| 35 |
|
| 36 |
|
| 37 |
def main(): |
| 38 |
stepsize = 0.0001 |
| 39 |
time, voltage, stimulus, spike_times = fake_neuron(stepsize=0.0001) |
| 40 |
|
| 41 |
|
| 42 |
file_name = 'spike_features.nix' |
| 43 |
file = nixio.File.open(file_name, nixio.FileMode.Overwrite) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
block = file.create_block("block name", "nix.session") |
| 48 |
|
| 49 |
|
| 50 |
data = block.create_data_array("membrane voltage", "nix.regular_sampled.time_series", data=voltage) |
| 51 |
data.label = "membrane voltage" |
| 52 |
data.unit = "mV" |
| 53 |
|
| 54 |
data.append_sampled_dimension(stepsize, label="time", unit="s") |
| 55 |
|
| 56 |
|
| 57 |
positions = block.create_data_array("spike times", "nix.events.spike_times", data=spike_times) |
| 58 |
positions.append_range_dimension_using_self() |
| 59 |
|
| 60 |
|
| 61 |
multi_tag = block.create_multi_tag("spike times", "nix.events.spike_times", positions) |
| 62 |
multi_tag.references.append(data) |
| 63 |
|
| 64 |
|
| 65 |
stimulus_array = block.create_data_array("stimulus", "nix.sampled", data=stimulus, label="stimulus", unit="nA") |
| 66 |
|
| 67 |
stimulus_array.append_sampled_dimension(stepsize, label="time", unit="s") |
| 68 |
|
| 69 |
|
| 70 |
multi_tag.create_feature(stimulus_array, nixio.LinkType.Tagged) |
| 71 |
|
| 72 |
|
| 73 |
plot_data(multi_tag) |
| 74 |
file.close() |
| 75 |
|
| 76 |
|
| 77 |
def plot_data(tag): |
| 78 |
data_array = tag.references[0] |
| 79 |
voltage = data_array[:] |
| 80 |
|
| 81 |
x_axis = data_array.dimensions[0] |
| 82 |
time = x_axis.axis(data_array.data_extent[0]) |
| 83 |
|
| 84 |
spike_times = tag.positions[:] |
| 85 |
|
| 86 |
feature_data_array = tag.features[0].data |
| 87 |
stimulus = feature_data_array[:] |
| 88 |
|
| 89 |
stim_time_dim = feature_data_array.dimensions[0] |
| 90 |
stimulus_time = stim_time_dim.axis(feature_data_array.data_extent[0]) |
| 91 |
|
| 92 |
stim_at_spike_time = np.zeros(len(tag.positions[:])) |
| 93 |
|
| 94 |
for i in range(len(tag.positions)): |
| 95 |
stim_at_spike_time[i] = tag.feature_data(i, 0)[:].item() |
| 96 |
|
| 97 |
response_axis = plt.subplot2grid((2, 3), (0, 0), rowspan=1, colspan=2) |
| 98 |
stimulus_axis = plt.subplot2grid((2, 3), (1, 0), rowspan=1, colspan=2, sharex=response_axis) |
| 99 |
|
| 100 |
hist_axis = plt.subplot2grid((2, 3), (1, 2), rowspan=1, colspan=1) |
| 101 |
hist_axis.hist([stimulus, stim_at_spike_time], color=["tab:blue", "orange"], label=["stimulus", "spike triggered stim"], density=True) |
| 102 |
hist_axis.set_xlabel("%s [%s]" % (feature_data_array.label, feature_data_array.unit)) |
| 103 |
hist_axis.set_ylabel("probability density") |
| 104 |
hist_axis.legend(ncol=1, fontsize=8, loc=(-0.1, 1.025)) |
| 105 |
|
| 106 |
response_axis.plot(time, voltage, color='tab:blue', label=data_array.name, lw=1) |
| 107 |
response_axis.scatter(spike_times, np.ones(spike_times.shape) * np.max(voltage), color='red', label=tag.name) |
| 108 |
response_axis.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else "")) |
| 109 |
response_axis.set_xlim(0, np.max(time)) |
| 110 |
response_axis.set_ylim((1.2 * np.min(voltage), 1.2 * np.max(voltage))) |
| 111 |
response_axis.legend(loc="lower center", ncol=2, fontsize=8) |
| 112 |
|
| 113 |
stimulus_axis.plot(stimulus_time, stimulus, color="darkgray", label="stimulus", lw=1) |
| 114 |
stimulus_axis.scatter(spike_times, np.ones(spike_times.shape) * np.max(stimulus), color='red', label=tag.name) |
| 115 |
stimulus_axis.set_xlabel(stim_time_dim.label + ((" [" + stim_time_dim.unit + "]") if stim_time_dim.unit else "")) |
| 116 |
stimulus_axis.set_ylabel(feature_data_array.label + ((" [" + feature_data_array.unit + "]") if feature_data_array.unit else "")) |
| 117 |
stimulus_axis.set_xlim(np.min(stimulus_time), np.max(stimulus_time)) |
| 118 |
stimulus_axis.set_ylim(1.2 * np.min(stimulus), 1.2 * np.max(stimulus)) |
| 119 |
stimulus_axis.legend(loc="lower center", ncol=2, fontsize=8) |
| 120 |
|
| 121 |
plt.subplots_adjust(left=0.125, top=0.975, bottom=0.1, right=0.98, hspace=0.25, wspace=0.35) |
| 122 |
plt.gcf().set_size_inches((5.5, 4.5)) |
| 123 |
|
| 124 |
if docutils.is_running_under_pytest(): |
| 125 |
plt.close() |
| 126 |
else: |
| 127 |
plt.show() |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == '__main__': |
| 131 |
main() |