File: spikeTagging.py

package info (click to toggle)
python-nixio 1.5.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,888 kB
  • sloc: python: 12,527; cpp: 832; makefile: 25
file content (87 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Copyright © 2014 - 2021 German Neuroinformatics Node (G-Node)

 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted under the terms of the BSD License. See
 LICENSE file in the root of the Project.

 Author: Jan Grewe <jan.grewe@g-node.org>

 See https://github.com/G-node/nix/wiki for more information.

"""

import nixio
import lif
import numpy as np
import matplotlib.pylab as plt

import docutils


def fake_neuron():
    lif_model = lif.LIF(offset=1.0)
    t, v, spike_times = lif_model.run_const_stim(5000, 0.00025)
    return t, v, spike_times


def plot_data(tag):
    data_array = tag.references[0]
    voltage = np.zeros(data_array.shape)
    data_array.read_direct(voltage)

    x_axis = data_array.dimensions[0]
    time = x_axis.axis(data_array.shape[0])

    spike_times = np.zeros(tag.positions.data_extent)
    tag.positions.read_direct(spike_times)

    fig = plt.figure(figsize=(5.5, 2.5))
    ax = fig.add_subplot(111)
    ax.plot(time, voltage, color='dodgerblue', label=data_array.name)
    ax.scatter(spike_times, np.ones(spike_times.shape)*np.max(voltage), color='red', label=tag.name)
    ax.set_xlabel(x_axis.label + ((" [" + x_axis.unit + "]") if x_axis.unit else ""))
    ax.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else ""))
    ax.set_xlim(0, np.max(time))
    ax.set_ylim((1.5 * np.min(voltage), 1.5 * np.max(voltage)))
    ax.legend()
    fig.subplots_adjust(bottom=0.175, top=0.975, right=0.975)
    if docutils.is_running_under_pytest():
        plt.close()
    else:
        # fig.savefig("../images/spike_tagging.png")
        plt.show()


if __name__ == '__main__':
    time, voltage, spike_times = fake_neuron()

    # create a new file overwriting any existing content
    file_name = 'spike_tagging.nix'
    file = nixio.File.open(file_name, nixio.FileMode.Overwrite)

    # create a 'Block' that represents a grouping object. Here, the recording session.
    # it gets a name and a type
    block = file.create_block("block name", "nix.session")

    # create a 'DataArray' to take the membrane voltage
    data = block.create_data_array("membrane voltage", "nix.regular_sampled.time_series", data=voltage, label="membrane voltage", unit="mV")

    # add descriptors for time axis
    data.append_sampled_dimension(time[1]-time[0], label="time", unit="s")

    # create the positions DataArray
    positions = block.create_data_array("spike times", "nix.events.spike_times", data=spike_times)
    positions.append_range_dimension_using_self()

    # create a MultiTag
    multi_tag = block.create_multi_tag("spike times", "nix.events.spike_times", positions)
    multi_tag.references.append(data)

    # let's plot the data from the stored information
    plot_data(multi_tag)
    file.close()