File: taggedFeature.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 (131 lines) | stat: -rw-r--r-- 5,215 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Copyright © 2014 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 numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt

import lif
import docutils


def fake_neuron(stepsize=0.001, offset=.8):
    stimulus = np.random.randn(102000) * 2.5
    b, a = signal.butter(2, 7.5, fs=1. / stepsize, btype="low")
    stimulus = signal.filtfilt(b, a, stimulus)
    stimulus = stimulus[1000:-1000]

    lif_model = lif.LIF(stepsize=stepsize, offset=offset)
    time, v, spike_times = lif_model.run_stimulus(stimulus)
    return time, v, stimulus, spike_times


def main():
    stepsize = 0.0001  # s
    time, voltage, stimulus, spike_times = fake_neuron(stepsize=0.0001)

    # create a new file overwriting any existing content
    file_name = 'spike_features.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)
    data.label = "membrane voltage"
    data.unit = "mV"
    # add descriptors for the time axis
    data.append_sampled_dimension(stepsize, 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)

    # save stimulus snippets in a DataArray
    stimulus_array = block.create_data_array("stimulus", "nix.sampled", data=stimulus, label="stimulus", unit="nA")
    # add a descriptor for the time axis
    stimulus_array.append_sampled_dimension(stepsize, label="time", unit="s")

    # set stimulus as a tagged feature of the multi_tag
    multi_tag.create_feature(stimulus_array, nixio.LinkType.Tagged)

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


def plot_data(tag):
    data_array = tag.references[0]
    voltage = data_array[:]

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

    spike_times = tag.positions[:]

    feature_data_array = tag.features[0].data
    stimulus = feature_data_array[:]

    stim_time_dim = feature_data_array.dimensions[0]
    stimulus_time = stim_time_dim.axis(feature_data_array.data_extent[0])

    stim_at_spike_time = np.zeros(len(tag.positions[:]))

    for i in range(len(tag.positions)):
        stim_at_spike_time[i] = tag.feature_data(i, 0)[:].item()

    response_axis = plt.subplot2grid((2, 3), (0, 0), rowspan=1, colspan=2)
    stimulus_axis = plt.subplot2grid((2, 3), (1, 0), rowspan=1, colspan=2, sharex=response_axis)

    hist_axis = plt.subplot2grid((2, 3), (1, 2), rowspan=1, colspan=1)
    hist_axis.hist([stimulus, stim_at_spike_time], color=["tab:blue", "orange"], label=["stimulus", "spike triggered stim"], density=True)
    hist_axis.set_xlabel("%s [%s]" % (feature_data_array.label, feature_data_array.unit))
    hist_axis.set_ylabel("probability density")
    hist_axis.legend(ncol=1, fontsize=8, loc=(-0.1, 1.025))

    response_axis.plot(time, voltage, color='tab:blue', label=data_array.name, lw=1)
    response_axis.scatter(spike_times, np.ones(spike_times.shape) * np.max(voltage), color='red', label=tag.name)
    response_axis.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else ""))
    response_axis.set_xlim(0, np.max(time))
    response_axis.set_ylim((1.2 * np.min(voltage), 1.2 * np.max(voltage)))
    response_axis.legend(loc="lower center", ncol=2, fontsize=8)

    stimulus_axis.plot(stimulus_time, stimulus, color="darkgray", label="stimulus", lw=1)
    stimulus_axis.scatter(spike_times, np.ones(spike_times.shape) * np.max(stimulus), color='red', label=tag.name)
    stimulus_axis.set_xlabel(stim_time_dim.label + ((" [" + stim_time_dim.unit + "]") if stim_time_dim.unit else ""))
    stimulus_axis.set_ylabel(feature_data_array.label + ((" [" + feature_data_array.unit + "]") if feature_data_array.unit else ""))
    stimulus_axis.set_xlim(np.min(stimulus_time), np.max(stimulus_time))
    stimulus_axis.set_ylim(1.2 * np.min(stimulus), 1.2 * np.max(stimulus))
    stimulus_axis.legend(loc="lower center", ncol=2, fontsize=8)

    plt.subplots_adjust(left=0.125, top=0.975, bottom=0.1, right=0.98, hspace=0.25, wspace=0.35)
    plt.gcf().set_size_inches((5.5, 4.5))
    # plt.savefig('../images/tagged_feature.png')
    if docutils.is_running_under_pytest():
        plt.close()
    else:
        plt.show()


if __name__ == '__main__':
    main()