forked from bernhardkaplan/bcpnn-mt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_input_spiketrains.py
executable file
·189 lines (155 loc) · 6.25 KB
/
plot_input_spiketrains.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import simulation_parameters
import pylab
import re
import numpy as np
import matplotlib
import utils
import sys
# load simulation parameters
network_params = simulation_parameters.parameter_storage() # network_params class containing the simulation parameters
params = network_params.load_params() # params stores cell numbers, etc as a dictionary
#params['blur_X'], params['blur_V'] = float(sys.argv[1]), float(sys.argv[2])
folder = params['input_folder']
fn_base = params['input_st_fn_base'].rsplit(folder)[1]
n_cells = params['n_cells']
output_fn_base = params['input_fig_fn_base']
output_fn_movie = params['input_movie'].rsplit('.')[0] + 'blur_%.2e_%.2e.mp4' % (params['blur_X'], params['blur_V'])
bg_color = 'white'
#pylab.rcParams['figure.facecolor'] = bg_color
# parameters
n_frames = 24# number of output figures
n_bins_x, n_bins_y = 20, 20
output_arrays = [np.zeros((n_bins_x, n_bins_y)) for i in xrange(n_frames)]
time_grid = np.linspace(0, params['t_stimulus'], n_frames+1, endpoint=True)
tuning_prop = np.loadtxt(params['tuning_prop_means_fn'])
H, x_edges, y_edges = np.histogram2d(tuning_prop[:,0], tuning_prop[:, 1], bins=(n_bins_x, n_bins_y))
print "x_edges", x_edges, x_edges.size
print "y_edges", y_edges, y_edges.size
z_max = 0
for gid in xrange(n_cells):
fn = params['input_st_fn_base'] + str(gid) + '.npy'
try:
spiketrain = np.load(fn)
binned_spikes, time_bins = np.histogram(spiketrain, time_grid)
x_pos_cell, y_pos_cell = tuning_prop[gid, 0], tuning_prop[gid, 1] # cell properties
x_pos_grid, y_pos_grid = utils.get_grid_pos(x_pos_cell, y_pos_cell, x_edges, y_edges) # cell's position in the grid
# print "%d\t%.3e\t%.3e: x:%d\ty:%d binned_spikes.max: %d" % (gid, x_pos_cell, y_pos_cell, x_pos_grid, y_pos_grid, binned_spikes.max())
z_max = max(binned_spikes.max(), z_max)
for frame in xrange(n_frames): # put activity in right time bin (output figure)
output_arrays[frame][x_pos_grid, y_pos_grid] += binned_spikes[frame]
except:
pass # cell gets no input
for frame in xrange(n_frames):
output_fn_dat = output_fn_base + 'frame%d.dat' % (frame)
output_fn_fig = output_fn_base + 'frame%d.png' % (frame)
print "Saving to file: ", output_fn_dat
np.savetxt(output_fn_dat, output_arrays[frame])
# print "Plotting frame: ", frame
fig = pylab.figure(facecolor=bg_color)
ax = fig.add_subplot(111)
ax.set_ylabel('$y$')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_title('Input spikes clustered by positions\nof target neurons in visual space')
# simple colormap
norm = matplotlib.mpl.colors.Normalize(vmin=0, vmax=z_max)
cax = ax.pcolormesh(output_arrays[frame], norm=norm)#, cmap='bone')#, edgecolor='k', linewidths='1')
# cax = ax.pcolormesh(output_arrays[frame], cmap='bone')#, edgecolor='k', linewidths='1')
pylab.colorbar(cax)#, cmap=pylab.bone())
yticks = ax.get_yticks()
xticks = ax.get_xticks()
yticks_rescaled = []
xticks_rescaled = []
for i in xrange(len(yticks)):
yticks_rescaled.append(yticks[i] / n_bins_y)
for i in xrange(len(xticks)):
xticks_rescaled.append(xticks[i] / n_bins_x)
ax.set_yticklabels(yticks_rescaled)
ax.set_xticklabels(xticks_rescaled)
print "Saving figure: ", output_fn_fig
pylab.savefig(output_fn_fig, facecolor=bg_color)
# let's make a movie!
print 'Creating the movie in file:', output_fn_movie
fps = 6 # frames per second
input_fn = output_fn_base + 'frame%d.png'
command = "avconv -f image2 -r %f -i %s -b 72000 %s" % (fps, input_fn, output_fn_movie)
os.system("rm %s" % output_fn_movie) # remove old one
os.system(command)
show = False
if show:
pylab.show()
#fn_to_plot = []
#gids = []
#for fn in os.listdir(folder):
# m = re.match("%s(\d+)\." % fn_base, fn)
# if m:
# print "Found file:", fn
# try:
# data = numpy.load(folder + fn)
# fn_to_plot.append(fn)
# gid = int(m.groups()[0])
# gids.append(gid)
# spiketrains[gid] = np.load(fn)
# except:
# pass
#fn_to_plot = sorted(fn_to_plot)
#gids = sorted(gids)
#n_plots = len(fn_to_plot)
#n_units = params['n_exc']
#x_max = int(round(numpy.sqrt(n_units)))
#y_max = int(round(numpy.sqrt(n_units)))
#if (n_units > x_max * y_max):
# x_max += 1
#spike_count = numpy.zeros((x_max, y_max))
#fig = pylab.figure()
#ax = fig.add_subplot(111)
#pylab.title('Input spike trains')
#spikes = []
#gid = []
#n_spikes = 0
#for i in xrange(n_plots):
# fn = fn_to_plot[i]
# m = re.match("%s(\d+)\." % fn_base, fn)
# mc_index = int(m.groups()[0])
# data = numpy.load(folder + fn)
# spike_count[mc_index % x_max, mc_index / x_max] = data.size
# ax.plot(data, mc_index * numpy.ones(data.size), 'o', markersize=1, color='k')
#ax.set_ylim(-1, max(gids)+1)
#for gid in xrange(n_cells):
# binned_spikes, time_bins = np.histogram(spiketrains[gid], time_grid)
# x_pos_cell, y_pos_cell = tuning_prop[gid, 0], tuning_prop[gid, 1] # cell properties
# x_pos_grid, y_pos_grid = utils.get_grid_pos(x_pos_cell, y_pos_cell, x_edges, y_edges) # cell's position in the grid
# spike count grid
#fig = pylab.figure()
#ax1 = fig.add_subplot(111)
#ax1.set_title('Input spikes integrated over time')
#pylab.show()
#cax1 = ax1.pcolor(spike_count)#, edgecolor='k', linewidths='1')
#cax1 = ax1.imshow(activity, interpolation='nearest')
#ax1.set_ylim((0, spike_count[:, 0].size))
#ax1.set_xlim((0, spike_count[0, :].size))
# plot histograms
#n_bins = 50
#fig = pylab.figure()
#pylab.subplots_adjust(bottom=0.05, top=0.9, hspace=-0.1)
#x_max = 1000
#xticks = numpy.arange(0, x_max, x_max/10.)
#for i in xrange(n_plots):
# fn = fn_to_plot[i]
# ax = fig.add_subplot(n_plots,1,n_plots-i)
# data = numpy.load(folder + fn)
# print fn, data.size
# count, bins = numpy.histogram(data, n_bins)
# gid = gids[i]
# ax.bar(bins[:-1], count, width=bins[1]-bins[0], facecolor='blue')#, normed=1)
# ax.set_yticks((0, max(count)))
# ax.set_yticklabels(('0', str(max(count))))
# ax.set_xlim((0, x_max))
# ax.set_xticks(xticks)
# ax.set_xticklabels(['' for i in xrange(len(xticks))])
#ax = fig.get_axes()[0]
#ax.set_xticks(xticks)
#ax.set_xticklabels(['%d' % i for i in xticks])
#ax.set_title('Input spike counts')
#pylab.show()