-
Notifications
You must be signed in to change notification settings - Fork 0
/
cx_gen_input.py
executable file
·387 lines (310 loc) · 12.7 KB
/
cx_gen_input.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python
"""
CX input generation script.
"""
import logging
import sys
import h5py
import numpy as np
import matplotlib as mpl
import matplotlib.animation as anim
import matplotlib.image
import matplotlib.cm as cm
import matplotlib.pyplot as plt
try:
from shutil import which
except:
from shutilwhich import which
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,
format='%(asctime)s %(name)s %(levelname)s %(message)s')
logger = logging.getLogger('cx')
class PlaySavedSignal(anim.TimedAnimation):
"""
Animate a sequence of frames saved in an HDF5 file.
The stored signal must be stored in an order-3 tensor
with dimensions `(number_of_frames, frame_rows, frame_cols)`.
"""
def __init__(self, data, interval=100):
fig = plt.figure()
self.data = data
self.ax = plt.imshow(self.data[0, :, :], cmap=cm.get_cmap('gray'))
super(self.__class__, self).__init__(fig, interval=interval,
blit=True, repeat=False)
def _draw_frame(self, framedata):
frame = self.data[framedata, :, :]
self.ax.set_data(frame)
def new_frame_seq(self):
return iter(range(1, self.data.shape[0]))
@classmethod
def from_file(self, file_name, dataset='array', interval=100):
f = h5py.File(file_name, 'r')
data = f[dataset][:]
return PlaySavedSignal(data, interval)
class PlayCXInputSignals(anim.TimedAnimation):
"""
Animate CX input signals.
"""
def __init__(self, input, bu_input_l, bu_input_r, pb_input, interval=100, step=1):
# Frame data step:
self.step = step
assert bu_input_l.shape[0] == pb_input.shape[0]
assert bu_input_r.shape[0] == pb_input.shape[0]
assert bu_input_l.shape[1] == bu_input_r.shape[1]
self.input = input
self.bu_input_l = bu_input_l
self.bu_input_r = bu_input_r
self.pb_input = pb_input
fig = plt.figure()
self.ax = plt.subplot2grid((3, 2), (0, 0), colspan=2)
self.ax_im = self.ax.imshow(input[0],
vmin=np.min(input), vmax=np.max(input),
cmap=cm.get_cmap('gray'))
self.ax.axis('tight')
plt.title('Input')
self.ax_bu_l = plt.subplot2grid((3, 2), (1, 0))
self.ax_bu_l_im = self.ax_bu_l.imshow(bu_input_l[0],
vmin=np.min(bu_input_l), vmax=np.max(bu_input_l),
cmap=cm.get_cmap('gray'))
self.ax_bu_l.axis('tight')
plt.title('BU RF Response')
self.ax_bu_r = plt.subplot2grid((3, 2), (1, 1))
self.ax_bu_r_im = self.ax_bu_r.imshow(bu_input_r[0],
vmin=np.min(bu_input_r), vmax=np.max(bu_input_r),
cmap=cm.get_cmap('gray'))
self.ax_bu_r.axis('tight')
plt.title('bu RF Response')
self.ax_pb = plt.subplot2grid((3, 2), (2, 0), colspan=2)
self.ax_pb_im = self.ax_pb.imshow(pb_input[0][np.newaxis],
vmin=np.min(pb_input), vmax=np.max(pb_input),
cmap=cm.get_cmap('gray'))
self.ax_pb.axis('tight')
plt.title('PB RF Response')
plt.tight_layout()
super(self.__class__, self).__init__(fig, interval=interval,
blit=True, repeat=False)
def _draw_frame(self, framedata):
self.ax_im.set_data(self.input[framedata])
self.ax_bu_l_im.set_data(self.bu_input_l[framedata])
self.ax_bu_r_im.set_data(self.bu_input_r[framedata])
self.ax_pb_im.set_data(self.pb_input[framedata][np.newaxis])
def new_frame_seq(self):
return iter(range(1, self.pb_input.shape[0], self.step))
@classmethod
def from_file(self, input_file, bu_input_l_file, bu_input_r_file, pb_input_file,
dataset='array', interval=100, step=1):
f = h5py.File(input_file, 'r')
input = f[dataset][:]
f = h5py.File(bu_input_l_file, 'r')
bu_input_l = f[dataset][:]
f = h5py.File(bu_input_r_file, 'r')
bu_input_r = f[dataset][:]
f = h5py.File(pb_input_file, 'r')
pb_input = f[dataset][:]
return PlayCXInputSignals(input, bu_input_l, bu_input_r, pb_input,
interval, step)
def save(self, filename):
if which('ffmpeg'):
w = anim.FFMpegFileWriter()
elif which('avconv'):
w = anim.AVConvFileWriter()
else:
raise RuntimeError('avconv or ffmpeg required')
super(self.__class__, self).save(filename, writer=w)
class CreateSignal(object):
"""
Create a test video signal.
"""
def __init__(self, shape, dt, dur):
self.shape = shape
self.dt = dt
self.dur = dur
self.N_t = int(self.dur/self.dt)
def moving_bar_l2r(self, width):
data = np.empty((self.N_t, self.shape[0], self.shape[1]), np.float64)
for i in range(self.N_t):
start = int(np.ceil(i*(self.shape[1]-width)/float(self.N_t)))
frame = np.zeros(self.shape, dtype=np.float64)
frame[:, start:start+width] = 1.0
data[i, :, :] = frame
return data
def moving_bar_r2l(self, width):
data = np.empty((self.N_t, self.shape[0], self.shape[1]), np.float64)
for i in range(self.N_t):
start = int(np.ceil(i*(self.shape[1]-width)/float(self.N_t)))
frame = np.zeros(self.shape, dtype=np.float64)
frame[:, start:start+width] = 1.0
data[i, :, :] = frame
return data[::-1]
@classmethod
def write(cls, data, file_name, dataset='array'):
f = h5py.File(file_name, 'w')
f.create_dataset(dataset, data.shape, data.dtype,
maxshape=(None,)+data.shape[1:])
f[dataset][:] = data
f.close()
class CircularGaussianFilterBank(object):
"""
Create a bank of circular 2D Gaussian filters.
Parameters
----------
shape : tuple
Image dimensions.
sigma : float
Parameter of Gaussian.
n : int
How many blocks should occupy the x-axis.
"""
def __init__(self, shape, sigma, n):
self.shape = shape
self.sigma = sigma
self.n = n
# Compute maximal and minimal response of a centered filter to use for
# normalization:
self.norm_min = np.inner(np.zeros(np.prod(shape)),
self.gaussian_mat(shape, sigma, 0, 0, n).reshape(-1))
self.norm_max = np.inner(np.ones(np.prod(shape)),
self.gaussian_mat(shape, sigma, 0, 0, n).reshape(-1))
self.filters = self.create_filters(shape, sigma, n)
def normalize_output(self, output):
"""
Normalize filter output against range of responses to a centered RF.
"""
return output/(self.norm_max-self.norm_min)
@classmethod
def func_gaussian(cls, x, y, sigma):
"""
2D Gaussian function.
"""
return (1.0/(1*np.pi*(sigma**2)))*np.exp(-(1.0/(2*(sigma**2)))*(x**2+y**2))
@classmethod
def gaussian_mat(cls, shape, sigma, n_x_offset, n_y_offset, n):
"""
Compute offset circular 2D Gaussian.
"""
# Image dimensions in pixels:
N_y, N_x = shape
# Normalized image width and height:
x_max = 1.0
y_max = N_y/float(N_x)
X, Y = np.meshgrid(np.linspace(-x_max/2, x_max/2, N_x)-(n_x_offset/float(n)),
np.linspace(-y_max/2, y_max/2, N_y)-(n_y_offset/float(n)))
return cls.func_gaussian(X, Y, sigma)
@classmethod
def create_filters(cls, shape, sigma, n):
"""
Create filter bank as order-4 tensor.
"""
N_y, N_x = shape
# Normalized image width and height:
x_max = 1.0
y_max = N_y/float(N_x)
# Compute how many blocks to use along the y-axis:
m = n*N_y/N_x
# Construct filters offset by the blocks:
n_x_offsets = np.linspace(np.ceil(-n/2.0), np.floor(n/2.0), n)
n_y_offsets = np.linspace(np.ceil(-m/2.0), np.floor(m/2.0), m)
filters = np.empty((m, n, N_y, N_x), np.float64)
for j, n_x_offset in enumerate(n_x_offsets):
for i, n_y_offset in enumerate(n_y_offsets):
filters[i, j] = cls.gaussian_mat(shape, sigma,
n_x_offset, n_y_offset, n)
return filters
def apply_filters(self, frame, normalize=True):
"""
Compute inner products of computed filters and a video frame.
"""
result = np.tensordot(self.filters, frame)
if normalize:
return self.normalize_output(result)
else:
return result
class RectangularFilterBank(object):
"""
Create a bank of 2D rectangular filters that tile the x-axis.
"""
def __init__(self, shape, n):
self.shape = shape
self.n = n
# Compute maximal and minimal response of a centered filter to use for
# normalization:
self.norm_min = np.inner(np.zeros(np.prod(shape)),
self.rect_mat(shape, 0, n).reshape(-1))
self.norm_max = np.inner(np.ones(np.prod(shape)),
self.rect_mat(shape, 0, n).reshape(-1))
self.filters = self.create_filters(shape, n)
def normalize_output(self, output):
"""
Normalize filter output against range of responses to a centered RF.
"""
return output/(self.norm_max-self.norm_min)
@classmethod
def func_rect(cls, x, y, width):
return np.logical_and(x > -width/2.0, x <= width/2.0).astype(np.float64)
@classmethod
def rect_mat(cls, shape, n_x_offset, n):
N_y, N_x = shape
x_max = 1.0
y_max = N_y/float(N_x)
X, Y = np.meshgrid(np.linspace(-x_max/2, x_max/2, N_x)-(n_x_offset/float(n)),
np.linspace(-y_max/2, y_max/2, N_y))
return cls.func_rect(X, Y, 1.0/n)
@classmethod
def create_filters(cls, shape, n):
N_y, N_x = shape
# Normalized image width and height:
x_max = 1.0
y_max = N_y/float(N_x)
# Construct filters offset by the blocks:
n_x_offsets = np.linspace(np.ceil(-n/2.0), np.floor(n/2.0), n)
filters = np.empty((n, N_y, N_x), np.float64)
for j, n_x_offset in enumerate(n_x_offsets):
filters[j] = cls.rect_mat(shape, n_x_offset, n)
return filters
def apply_filters(self, frame, normalize=True):
"""
Compute inner products of computed filters and a video frame.
"""
result = np.tensordot(self.filters, frame)
if normalize:
return self.normalize_output(result)
else:
return result
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-d', default='l2r', type=str,
help='Direction [l2r, r2l]')
args = parser.parse_args()
logger.info('generating input video signal (%s)' % args.d)
shape = (200, 500)
dt = 1e-4
dur = 0.2
c = CreateSignal(shape, dt, dur)
if args.d == 'l2r':
data = c.moving_bar_l2r(50)
elif args.d == 'r2l':
data = c.moving_bar_r2l(50)
else:
raise RuntimeError('unsupported signal type')
c.write(data, 'moving_bar.h5')
logger.info('generating Gaussian RFs for BU')
fc = CircularGaussianFilterBank((shape[0], shape[1]/2), 0.05, 10)
logger.info('generating rectangular RFs for PB')
fr = RectangularFilterBank(shape, 18)
logger.info('filtering with Gaussian RFs')
BU_input_pre = np.empty((len(data),)+fc.filters.shape[0:2])
bu_input_pre = np.empty((len(data),)+fc.filters.shape[0:2])
for i, frame in enumerate(data):
BU_input_pre[i, :, :] = 3*fc.apply_filters(frame[:, :shape[1]/2])
bu_input_pre[i, :, :] = 3*fc.apply_filters(frame[:, shape[1]/2:])
logger.info('filtering with rectangular RFs')
PB_input_pre = np.empty((len(data), fr.filters.shape[0]))
for i, frame in enumerate(data):
PB_input_pre[i, :] = 3*fr.apply_filters(frame)
logger.info('saving RF responses')
with h5py.File('BU_input_pre.h5', 'w') as f:
f.create_dataset('/array', data=BU_input_pre)
with h5py.File('bu_input_pre.h5', 'w') as f:
f.create_dataset('/array', data=bu_input_pre)
with h5py.File('PB_input_pre.h5', 'w') as f:
f.create_dataset('/array', data=PB_input_pre)