-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_spikes.py
97 lines (66 loc) · 2.26 KB
/
plot_spikes.py
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
import numpy as np
import random
import argparse
def raster(times, neurons, color='k'):
import matplotlib.pyplot as plt
ax = plt.gca()
neurons = np.array(neurons)
plt.vlines(times, neurons-0.45, neurons + 0.45, color=color, linewidth=1.5)
plt.xlabel('bio time [s]')
plt.ylabel('neuron index')
return ax
def plot(infilename, outfilename="", show=False, xlim=None, ylim=None):
"""
infilename: first column neuron ids
second column spike times
show: [bool] show plot on screen
outfilename: e.g. result.pdf, result.png
xlim: (xmin, xmax)
ylim: (ymin, ymax)
"""
if not show:
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
ticker.Locator.MAXTICKS *= 10
spikes = np.loadtxt(infilename)
margins={"left":0.11, "right":0.95, "top":0.95, "bottom":0.11}
fig = plt.figure()
times, neurons = spikes[:,1], spikes[:,0]
#mapped_neurons = []
#neuron_idx_map = {0:0, 16:1, 22:2, 31:3}
#for n in neurons:
# mapped_neurons.append(neuron_idx_map[n])
#neurons = mapped_neurons
ax = raster(times, neurons)
minorLocator = ticker.MultipleLocator(0.1)
ax.xaxis.set_minor_locator(minorLocator)
plt.subplots_adjust(**margins)
if xlim:
plt.xlim(*xlim)
if ylim:
plt.ylim(*ylim)
yticks = np.arange(ylim[0], ylim[1], 10.0)
else:
plt.ylim(min(neurons)-0.5, max(neurons)+0.5)
yticks = np.arange(min(neurons), max(neurons)+1, 10.0)
plt.yticks(yticks)
plt.tick_params(axis='x', which='minor', bottom='off', top='off')
#ax.grid(True)
#for y in [11.5+12*n for n in xrange(15)]:
# plt.axhline(y)
if outfilename:
plt.savefig(outfilename)
if show:
plt.show()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('file', type=argparse.FileType('r'))
parser.add_argument('--plotfilename', default="")
parser.add_argument('--xlim', type=float, nargs=2, default=None)
parser.add_argument('--ylim', type=float, nargs=2, default=None)
args = parser.parse_args()
plot(args.file.name, args.plotfilename, True, args.xlim, args.ylim)
if __name__ == "__main__":
main()