def feat_ann(c=0):
batch_size =700
feats_eeg = scipy.stats.zscore(tools.feat_eeg(data[:,:,0]))
feats_emg = scipy.stats.zscore(tools.feat_emg(data[:,:,1]))
feats_eog = scipy.stats.zscore(tools.feat_eog(data[:,:,2]))
feats_all = np.hstack([feats_eeg, feats_emg, feats_eog])
results = dict()
r = cv(feats_eeg, target, groups, models.ann, name = 'eeg', stop_after=15,batch_size=batch_size, counter=c, plot=plot)
results.update(r)
r = cv(np.hstack([feats_eeg,feats_eog]), target, groups, models.ann, name = 'eeg+eog',batch_size=batch_size, stop_after=15, counter=c, plot=plot)
results.update(r)
r = cv(np.hstack([feats_eeg,feats_emg]), target, groups, models.ann, name = 'eeg+emg',batch_size=batch_size, stop_after=15, counter=c, plot=plot)
results.update(r)
r = cv(feats_all, target, groups, models.ann, name = 'all',batch_size=batch_size, stop_after=15, counter=c, plot=plot)
results.update(r)
with open('results_electrodes_feat.pkl', 'wb') as f: pickle.dump(results, f)
python类hstack()的实例源码
def rotation_from_axes(x_axis, y_axis, z_axis):
"""Convert specification of axis in target frame to
a rotation matrix from source to target frame.
Parameters
----------
x_axis : :obj:`numpy.ndarray` of float
A normalized 3-vector for the target frame's x-axis.
y_axis : :obj:`numpy.ndarray` of float
A normalized 3-vector for the target frame's y-axis.
z_axis : :obj:`numpy.ndarray` of float
A normalized 3-vector for the target frame's z-axis.
Returns
-------
:obj:`numpy.ndarray` of float
A 3x3 rotation matrix that transforms from a source frame to the
given target frame.
"""
return np.hstack((x_axis[:,np.newaxis], y_axis[:,np.newaxis], z_axis[:,np.newaxis]))
def online_em(self, new_labels, w = 0.1, num_it = 3, no_train = False):
if len(new_labels) == 0: return
n = self.lc.n
l = len(new_labels)
self.lc.add_labels(new_labels, l * [None])
self.N = self.lc.n
self.qz = np.hstack((self.qz, np.zeros(l)))
self.maj_lab = np.hstack((self.maj_lab, np.zeros(l)))
self.init_prob(id_range = (n, self.lc.n))
if no_train: return
self.m_step(id_range = (n, self.lc.n), w = w)
for it in range(num_it):
self.e_step(id_range = (n, self.lc.n), w = w)
self.m_step(id_range = (n, self.lc.n), w = w)
def compute_pvalue(self, samples):
samples = self._make_two_dimensional(samples)
self.shape = samples.shape[1]
stein_statistics = []
for f in range(self.number_of_random_frequencies):
# This is a little bit of a bug , but th holds even for this choice
random_frequency = np.random.randn()
matrix_of_stats = self.stein_stat(random_frequency=random_frequency, samples=samples)
stein_statistics.append(matrix_of_stats)
normal_under_null = np.hstack(stein_statistics)
normal_under_null = self._make_two_dimensional(normal_under_null)
return mahalanobis_distance(normal_under_null, normal_under_null.shape[1])
def plotJoints(self, ax, joint, color='nice', jcolor=None):
"""
Plot connected joints
:type ax: axis to plot on
:type joint: joints to connect
:type color: line color
"""
color_index = 0
for i in range(joint.shape[0]):
ax.scatter(joint[i, 0], joint[i, 1], c=(self.jointcolors[color_index % len(self.jointcolors)] if jcolor is None else jcolor), marker='.', s=400)
color_index += 1
for i in range(len(self.jointConnections)):
ax.plot(numpy.hstack((joint[self.jointConnections[i][0], 0], joint[self.jointConnections[i][1], 0])),
numpy.hstack((joint[self.jointConnections[i][0], 1], joint[self.jointConnections[i][1], 1])),
c=(color if color is not 'nice' else self.jointConnectionColors[i]), linewidth=2.0)
def arma_predictor_model(x, y, m, n):
"""
Return matrix and vector relating (*m*, *n*) ARMA model to the
input *x* and output *y*. In other words, construct the max(*m*,
*n*) - 1 by (*m* - 1 + *n* - 1) matrix A such that
y[k] + a_1 y[k - 1] + ... + a_m y[k - m] = b_1 x[k - 1] + ... + b_n x[k - n]
and the vector b corresponds to y[k] for k >= max(*m*, *n*).
"""
assert len(x) == len(y)
k = max(m, n)
A1 = SP.linalg.toeplitz(-y[k:-1], r=-y[(k - m):k][::-1])
A2 = SP.linalg.toeplitz(x[k:-1], r=x[(k - n):k][::-1])
A = NP.hstack((A1, A2))
b = y[k+1:]
return A, b
def tensor_vstack(tensor_list, pad=0):
"""
vertically stack tensors
:param tensor_list: list of tensor to be stacked vertically
:param pad: label to pad with
:return: tensor with max shape
"""
ndim = len(tensor_list[0].shape)
if ndim == 1:
return np.hstack(tensor_list)
dimensions = [0]
for dim in range(1, ndim):
dimensions.append(max([tensor.shape[dim] for tensor in tensor_list]))
for ind, tensor in enumerate(tensor_list):
pad_shape = [(0, 0)]
for dim in range(1, ndim):
pad_shape.append((0, dimensions[dim] - tensor.shape[dim]))
tensor_list[ind] = np.lib.pad(tensor, pad_shape, 'constant', constant_values=pad)
all_tensor = np.vstack(tensor_list)
return all_tensor
def reset(self):
self.cur = 0
if self.shuffle:
if config.TRAIN.ASPECT_GROUPING:
widths = np.array([r['width'] for r in self.roidb])
heights = np.array([r['height'] for r in self.roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((np.random.permutation(horz_inds), np.random.permutation(vert_inds)))
if inds.shape[0] % 2:
inds_ = np.reshape(inds[:-1], (-1, 2))
row_perm = np.random.permutation(np.arange(inds_.shape[0]))
inds[:-1] = np.reshape(inds_[row_perm, :], (-1, ))
else:
inds = np.reshape(inds, (-1, 2))
row_perm = np.random.permutation(np.arange(inds.shape[0]))
inds = np.reshape(inds[row_perm, :], (-1, ))
self.index = inds
else:
np.random.shuffle(self.index)
def reset(self):
self.cur = 0
if self.shuffle:
if config.TRAIN.ASPECT_GROUPING:
widths = np.array([r['width'] for r in self.roidb])
heights = np.array([r['height'] for r in self.roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((np.random.permutation(horz_inds), np.random.permutation(vert_inds)))
if inds.shape[0] % 2:
inds_ = np.reshape(inds[:-1], (-1, 2))
row_perm = np.random.permutation(np.arange(inds_.shape[0]))
inds[:-1] = np.reshape(inds_[row_perm, :], (-1, ))
else:
inds = np.reshape(inds, (-1, 2))
row_perm = np.random.permutation(np.arange(inds.shape[0]))
inds = np.reshape(inds[row_perm, :], (-1, ))
self.index = inds
else:
np.random.shuffle(self.index)
def _shuffle_roidb_inds(self):
"""Randomly permute the training roidb."""
if cfg.TRAIN.ASPECT_GROUPING:
widths = np.array([r['width'] for r in self._roidb])
heights = np.array([r['height'] for r in self._roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((
np.random.permutation(horz_inds),
np.random.permutation(vert_inds)))
inds = np.reshape(inds, (-1, 2))
row_perm = np.random.permutation(np.arange(inds.shape[0]))
inds = np.reshape(inds[row_perm, :], (-1,))
self._perm = inds
else:
self._perm = np.random.permutation(np.arange(len(self._roidb)))
self._cur = 0
def imdb_proposals(net, imdb):
"""Generate RPN proposals on all images in an imdb."""
_t = Timer()
imdb_boxes = [[] for _ in xrange(imdb.num_images)]
for i in xrange(imdb.num_images):
im = cv2.imread(imdb.image_path_at(i))
_t.tic()
imdb_boxes[i], scores = im_proposals(net, im)
_t.toc()
print 'im_proposals: {:d}/{:d} {:.3f}s' \
.format(i + 1, imdb.num_images, _t.average_time)
if 0:
dets = np.hstack((imdb_boxes[i], scores))
# from IPython import embed; embed()
_vis_proposals(im, dets[:3, :], thresh=0.9)
plt.show()
return imdb_boxes
def circumcenter(self, tri):
"""Compute circumcenter and circumradius of a triangle in 2D.
Uses an extension of the method described here:
http://www.ics.uci.edu/~eppstein/junkyard/circumcenter.html
"""
pts = np.asarray([self.coords[v] for v in tri])
pts2 = np.dot(pts, pts.T)
A = np.bmat([[2 * pts2, [[1],
[1],
[1]]],
[[[1, 1, 1, 0]]]])
b = np.hstack((np.sum(pts * pts, axis=1), [1]))
x = np.linalg.solve(A, b)
bary_coords = x[:-1]
center = np.dot(bary_coords, pts)
# radius = np.linalg.norm(pts[0] - center) # euclidean distance
radius = np.sum(np.square(pts[0] - center)) # squared distance
return (center, radius)
def _shuffle_roidb_inds(self):
"""Randomly permute the training roidb."""
if cfg.TRAIN.ASPECT_GROUPING:
widths = np.array([r['width'] for r in self._roidb])
heights = np.array([r['height'] for r in self._roidb])
horz = (widths >= heights)
vert = np.logical_not(horz)
horz_inds = np.where(horz)[0]
vert_inds = np.where(vert)[0]
inds = np.hstack((
np.random.permutation(horz_inds),
np.random.permutation(vert_inds)))
inds = np.reshape(inds, (-1, 2))
row_perm = np.random.permutation(np.arange(inds.shape[0]))
inds = np.reshape(inds[row_perm, :], (-1,))
self._perm = inds
else:
self._perm = np.random.permutation(np.arange(len(self._roidb)))
self._cur = 0
def imdb_proposals(net, imdb):
"""Generate RPN proposals on all images in an imdb."""
_t = Timer()
imdb_boxes = [[] for _ in xrange(imdb.num_images)]
for i in xrange(imdb.num_images):
im = cv2.imread(imdb.image_path_at(i))
_t.tic()
imdb_boxes[i], scores = im_proposals(net, im)
_t.toc()
print 'im_proposals: {:d}/{:d} {:.3f}s' \
.format(i + 1, imdb.num_images, _t.average_time)
if 0:
dets = np.hstack((imdb_boxes[i], scores))
# from IPython import embed; embed()
_vis_proposals(im, dets[:3, :], thresh=0.9)
plt.show()
return imdb_boxes
def test_matrices_l_h_after_func_fill(self, qpOASES_mat_fixtures):
""" Verify qpOASES matrices after filling.
"""
pcs, pp = qpOASES_mat_fixtures
random_fill([pp.l, pp.h])
pp._fill_matrices()
for i in range(pp.N+1):
assert pp.l[i, 0] == -INFTY
assert pp.l[i, 1] == 0
assert pp.h[i, 0] == INFTY
assert pp.h[i, 1] == INFTY
l_expected = np.hstack(map(lambda pc: pc.l[i], pcs))
h_expected = np.hstack(map(lambda pc: pc.h[i], pcs))
assert np.allclose(pp.l[i, 2:], l_expected)
assert np.allclose(pp.h[i, 2:], h_expected)
def test_canonical_mat(self, intp_fixture):
"""
"""
pc, pc_intp = intp_fixture
# number
for i in range(pc_intp.N):
ds = pc_intp.ss[i+1] - pc_intp.ss[i]
ai_new = np.hstack((
pc.a[i],
pc.a[i+1] + 2 * ds * pc.b[i+1]))
bi_new = np.hstack((pc.b[i], pc.b[i+1]))
ci_new = np.hstack((pc.c[i], pc.c[i+1]))
assert np.allclose(ai_new, pc_intp.a[i])
assert np.allclose(bi_new, pc_intp.b[i])
assert np.allclose(ci_new, pc_intp.c[i])
def test_equality_mat(self, intp_fixture):
""" Equality constraint: abar, bbar, cbar, D
"""
pc, pc_intp = intp_fixture
# number
for i in range(pc_intp.N):
ds = pc_intp.ss[i+1] - pc_intp.ss[i]
ai_new = np.hstack((
pc.abar[i],
pc.abar[i+1] + 2 * ds * pc.bbar[i+1]))
bi_new = np.hstack((pc.bbar[i], pc.bbar[i+1]))
ci_new = np.hstack((pc.cbar[i], pc.cbar[i+1]))
Di_new = block_diag(pc.D[i], pc.D[i+1])
li_new = np.hstack((pc.l[i], pc.l[i+1]))
hi_new = np.hstack((pc.h[i], pc.h[i+1]))
assert np.allclose(ai_new, pc_intp.abar[i])
assert np.allclose(bi_new, pc_intp.bbar[i])
assert np.allclose(ci_new, pc_intp.cbar[i])
assert np.allclose(Di_new, pc_intp.D[i], atol=1e-8)
assert np.allclose(li_new, pc_intp.l[i])
assert np.allclose(hi_new, pc_intp.h[i])
def plotFields(layer,fieldShape=None,channel=None,figOffset=1,cmap=None,padding=0.01):
# Receptive Fields Summary
try:
W = layer.W
except:
W = layer
wp = W.eval().transpose();
if len(np.shape(wp)) < 4: # Fully connected layer, has no shape
fields = np.reshape(wp,list(wp.shape[0:-1])+fieldShape)
else: # Convolutional layer already has shape
features, channels, iy, ix = np.shape(wp)
if channel is not None:
fields = wp[:,channel,:,:]
else:
fields = np.reshape(wp,[features*channels,iy,ix])
perRow = int(math.floor(math.sqrt(fields.shape[0])))
perColumn = int(math.ceil(fields.shape[0]/float(perRow)))
fig = mpl.figure(figOffset); mpl.clf()
# Using image grid
from mpl_toolkits.axes_grid1 import ImageGrid
grid = ImageGrid(fig,111,nrows_ncols=(perRow,perColumn),axes_pad=padding,cbar_mode='single')
for i in range(0,np.shape(fields)[0]):
im = grid[i].imshow(fields[i],cmap=cmap);
grid.cbar_axes[0].colorbar(im)
mpl.title('%s Receptive Fields' % layer.name)
# old way
# fields2 = np.vstack([fields,np.zeros([perRow*perColumn-fields.shape[0]] + list(fields.shape[1:]))])
# tiled = []
# for i in range(0,perColumn*perRow,perColumn):
# tiled.append(np.hstack(fields2[i:i+perColumn]))
#
# tiled = np.vstack(tiled)
# mpl.figure(figOffset); mpl.clf(); mpl.imshow(tiled,cmap=cmap); mpl.title('%s Receptive Fields' % layer.name); mpl.colorbar();
mpl.figure(figOffset+1); mpl.clf(); mpl.imshow(np.sum(np.abs(fields),0),cmap=cmap); mpl.title('%s Total Absolute Input Dependency' % layer.name); mpl.colorbar()
def plotOutput(layer,feed_dict,fieldShape=None,channel=None,figOffset=1,cmap=None):
# Output summary
try:
W = layer.output
except:
W = layer
wp = W.eval(feed_dict=feed_dict);
if len(np.shape(wp)) < 4: # Fully connected layer, has no shape
temp = np.zeros(np.product(fieldShape)); temp[0:np.shape(wp.ravel())[0]] = wp.ravel()
fields = np.reshape(temp,[1]+fieldShape)
else: # Convolutional layer already has shape
wp = np.rollaxis(wp,3,0)
features, channels, iy,ix = np.shape(wp)
if channel is not None:
fields = wp[:,channel,:,:]
else:
fields = np.reshape(wp,[features*channels,iy,ix])
perRow = int(math.floor(math.sqrt(fields.shape[0])))
perColumn = int(math.ceil(fields.shape[0]/float(perRow)))
fields2 = np.vstack([fields,np.zeros([perRow*perColumn-fields.shape[0]] + list(fields.shape[1:]))])
tiled = []
for i in range(0,perColumn*perRow,perColumn):
tiled.append(np.hstack(fields2[i:i+perColumn]))
tiled = np.vstack(tiled)
if figOffset is not None:
mpl.figure(figOffset); mpl.clf();
mpl.imshow(tiled,cmap=cmap); mpl.title('%s Output' % layer.name); mpl.colorbar();
def plotFields(layer,fieldShape=None,channel=None,maxFields=25,figName='ReceptiveFields',cmap=None,padding=0.01):
# Receptive Fields Summary
W = layer.W
wp = W.eval().transpose();
if len(np.shape(wp)) < 4: # Fully connected layer, has no shape
fields = np.reshape(wp,list(wp.shape[0:-1])+fieldShape)
else: # Convolutional layer already has shape
features, channels, iy, ix = np.shape(wp)
if channel is not None:
fields = wp[:,channel,:,:]
else:
fields = np.reshape(wp,[features*channels,iy,ix])
fieldsN = min(fields.shape[0],maxFields)
perRow = int(math.floor(math.sqrt(fieldsN)))
perColumn = int(math.ceil(fieldsN/float(perRow)))
fig = mpl.figure(figName); mpl.clf()
# Using image grid
from mpl_toolkits.axes_grid1 import ImageGrid
grid = ImageGrid(fig,111,nrows_ncols=(perRow,perColumn),axes_pad=padding,cbar_mode='single')
for i in range(0,fieldsN):
im = grid[i].imshow(fields[i],cmap=cmap);
grid.cbar_axes[0].colorbar(im)
mpl.title('%s Receptive Fields' % layer.name)
# old way
# fields2 = np.vstack([fields,np.zeros([perRow*perColumn-fields.shape[0]] + list(fields.shape[1:]))])
# tiled = []
# for i in range(0,perColumn*perRow,perColumn):
# tiled.append(np.hstack(fields2[i:i+perColumn]))
#
# tiled = np.vstack(tiled)
# mpl.figure(figOffset); mpl.clf(); mpl.imshow(tiled,cmap=cmap); mpl.title('%s Receptive Fields' % layer.name); mpl.colorbar();
mpl.figure(figName+' Total'); mpl.clf(); mpl.imshow(np.sum(np.abs(fields),0),cmap=cmap); mpl.title('%s Total Absolute Input Dependency' % layer.name); mpl.colorbar()