| 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 |
|
| 18 |
import nixio |
| 19 |
import lif |
| 20 |
import numpy as np |
| 21 |
import matplotlib.pylab as plt |
| 22 |
|
| 23 |
import docutils |
| 24 |
|
| 25 |
|
| 26 |
def fake_neuron(): |
| 27 |
lif_model = lif.LIF(offset=1.0) |
| 28 |
t, v, spike_times = lif_model.run_const_stim(5000, 0.00025) |
| 29 |
return t, v, spike_times |
| 30 |
|
| 31 |
|
| 32 |
def plot_data(tag): |
| 33 |
data_array = tag.references[0] |
| 34 |
voltage = np.zeros(data_array.shape) |
| 35 |
data_array.read_direct(voltage) |
| 36 |
|
| 37 |
x_axis = data_array.dimensions[0] |
| 38 |
time = x_axis.axis(data_array.shape[0]) |
| 39 |
|
| 40 |
spike_times = np.zeros(tag.positions.data_extent) |
| 41 |
tag.positions.read_direct(spike_times) |
| 42 |
|
| 43 |
fig = plt.figure(figsize=(5.5, 2.5)) |
| 44 |
ax = fig.add_subplot(111) |
| 45 |
ax.plot(time, voltage, color='dodgerblue', label=data_array.name) |
| 46 |
ax.scatter(spike_times, np.ones(spike_times.shape)*np.max(voltage), color='red', label=tag.name) |
| 47 |
ax.set_xlabel(x_axis.label + ((" [" + x_axis.unit + "]") if x_axis.unit else "")) |
| 48 |
ax.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else "")) |
| 49 |
ax.set_xlim(0, np.max(time)) |
| 50 |
ax.set_ylim((1.5 * np.min(voltage), 1.5 * np.max(voltage))) |
| 51 |
ax.legend() |
| 52 |
fig.subplots_adjust(bottom=0.175, top=0.975, right=0.975) |
| 53 |
if docutils.is_running_under_pytest(): |
| 54 |
plt.close() |
| 55 |
else: |
| 56 |
|
| 57 |
plt.show() |
| 58 |
|
| 59 |
|
| 60 |
if __name__ == '__main__': |
| 61 |
time, voltage, spike_times = fake_neuron() |
| 62 |
|
| 63 |
|
| 64 |
file_name = 'spike_tagging.nix' |
| 65 |
file = nixio.File.open(file_name, nixio.FileMode.Overwrite) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
block = file.create_block("block name", "nix.session") |
| 70 |
|
| 71 |
|
| 72 |
data = block.create_data_array("membrane voltage", "nix.regular_sampled.time_series", data=voltage, label="membrane voltage", unit="mV") |
| 73 |
|
| 74 |
|
| 75 |
data.append_sampled_dimension(time[1]-time[0], label="time", unit="s") |
| 76 |
|
| 77 |
|
| 78 |
positions = block.create_data_array("spike times", "nix.events.spike_times", data=spike_times) |
| 79 |
positions.append_range_dimension_using_self() |
| 80 |
|
| 81 |
|
| 82 |
multi_tag = block.create_multi_tag("spike times", "nix.events.spike_times", positions) |
| 83 |
multi_tag.references.append(data) |
| 84 |
|
| 85 |
|
| 86 |
plot_data(multi_tag) |
| 87 |
file.close() |