def attribute_category(out, ratios):
''' This function distributes each subject in a 'train' or 'test' category.
Args:
out (pd.DataFrame): a pd.DataFrame that contains the info of all files
by subject.
ratios (list): a list containing the proportions of train/test
subjects. should sum to 1 and supposedly it has been tested before.
Returns:
out (pd.DataFrame): a pd.DataFrame that contains the info of all files
by subject where the 'category' column has been set to either
train or test depending the result of the random draw.
The value of test or train is the same for a given subject.
'''
nSubjects = len(out.subject.unique())
i_train = np.random.choice( np.arange(nSubjects), int(ratios[0] * nSubjects))
train_or_test_by_subject = [
'train' if i in i_train else 'test' for i in range(nSubjects)]
images_per_subject = out.groupby(["subject"]).category.count().values
out.category = list(np.repeat(train_or_test_by_subject,
images_per_subject))
return(out)
python类repeat()的实例源码
def get_op(self):
"""Returns all symmetry operations (including inversions and
subtranslations), but unlike get_symop(), they are returned as
two ndarrays."""
if self.centrosymmetric:
rot = np.tile(np.vstack((self.rotations, -self.rotations)),
(self.nsubtrans, 1, 1))
trans = np.tile(np.vstack((self.translations, -self.translations)),
(self.nsubtrans, 1))
trans += np.repeat(self.subtrans, 2 * len(self.rotations), axis=0)
trans = np.mod(trans, 1)
else:
rot = np.tile(self.rotations, (self.nsubtrans, 1, 1))
trans = np.tile(self.translations, (self.nsubtrans, 1))
trans += np.repeat(self.subtrans, len(self.rotations), axis=0)
trans = np.mod(trans, 1)
return rot, trans
def test_shape_operations(self):
# concatenate
xval = np.random.random((4, 3))
xth = KTH.variable(xval)
xtf = KTF.variable(xval)
yval = np.random.random((4, 2))
yth = KTH.variable(yval)
ytf = KTF.variable(yval)
zth = KTH.eval(KTH.concatenate([xth, yth], axis=-1))
ztf = KTF.eval(KTF.concatenate([xtf, ytf], axis=-1))
assert zth.shape == ztf.shape
assert_allclose(zth, ztf, atol=1e-05)
check_single_tensor_operation('reshape', (4, 2), shape=(8, 1))
check_single_tensor_operation('permute_dimensions', (4, 2, 3),
pattern=(2, 0, 1))
check_single_tensor_operation('repeat', (4, 1), n=3)
check_single_tensor_operation('flatten', (4, 1))
check_single_tensor_operation('expand_dims', (4, 3), dim=-1)
check_single_tensor_operation('expand_dims', (4, 3, 2), dim=1)
check_single_tensor_operation('squeeze', (4, 3, 1), axis=2)
check_single_tensor_operation('squeeze', (4, 1, 1), axis=1)
check_composed_tensor_operations('reshape', {'shape': (4, 3, 1, 1)},
'squeeze', {'axis': 2},
(4, 3, 1, 1))
def __init__(self, name, shape, initial_stdev = 2.0, initial_prec = 5.0, a0 = 1.0, b0 = 1.0):
mean_std = 1.0 / np.sqrt(shape[-1])
with tf.variable_scope(name) as scope:
self.mean = tf.Variable(tf.random_uniform(shape, minval=-mean_std, maxval=mean_std))
self.logvar = tf.Variable(np.log(initial_stdev**2.0) * np.ones(shape), name = "logvar", dtype = tf.float32)
self.prec = np.repeat(initial_prec, shape[-1])
self.prec_ph= tf.placeholder(shape=shape[-1], name="prec", dtype = tf.float32)
self.var = tf.exp(self.logvar, name = "var")
self.a0 = a0
self.b0 = b0
self.shape = shape
# def prec_div(self):
# return - tf.reduce_sum(gammaPrior(self.prec_a, self.prec_b, self.a0, self.b0))
## outputs E_q[ log N( x | 0, prec^-1) ] + Entropy(q(x))
## where x is the normally distributed variable
def supercell(self, scale_mat):
"""
Get the supercell of the origin gcell
scale_mat is similar as H matrix in superlattice generator
"""
# return self.__class__(...)
sarr_lat = np.matmul(scale_mat, self.lattice)
# coor_conv_pos = np.matmul(self.positions, self.lattice)
# o_conv_pos = np.matmul(coor_conv_pos, np.linalg.inv(scale_mat))
o_conv_pos = np.matmul(self.positions, np.linalg.inv(scale_mat))
o_pos = self.get_frac_from_mat(scale_mat)
l_of_positions = [i for i in map(lambda x: x+o_pos, list(o_conv_pos))]
pos = np.concatenate(l_of_positions, axis=0)
n = scale_mat.diagonal().prod()
numbers = np.repeat(self.numbers, n)
return self.__class__(sarr_lat, pos, numbers)
def in_euclidean_discance(self, pos, center, r):
"""
A helper function to return true or false.
Decided whether a position(frac) inside a
distance restriction.
"""
from scipy.spatial.distance import euclidean as euclidean_discance
from itertools import product
cart_cent = self.get_cartesian_from_frac(center)
trans = np.array([i for i in product([-1, 0, 1], repeat=3)])
allpos = pos + trans
for p in allpos:
cart_p = self.get_cartesian_from_frac(p)
if euclidean_discance(cart_p, cart_cent) < r:
return True
break
return False
cochleagram_extractor.py 文件源码
项目:speech_feature_extractor
作者: ZhihaoDU
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def cochleagram_extractor(xx, sr, win_len, shift_len, channel_number, win_type):
fcoefs, f = make_erb_filters(sr, channel_number, 50)
fcoefs = np.flipud(fcoefs)
xf = erb_frilter_bank(xx, fcoefs)
if win_type == 'hanning':
window = np.hanning(channel_number)
elif win_type == 'hamming':
window = np.hamming(channel_number)
elif win_type == 'triangle':
window = (1 - (np.abs(channel_number - 1 - 2 * np.arange(1, channel_number + 1, 1)) / (channel_number + 1)))
else:
window = np.ones(channel_number)
window = window.reshape((channel_number, 1))
xe = np.power(xf, 2.0)
frames = 1 + ((np.size(xe, 1)-win_len) // shift_len)
cochleagram = np.zeros((channel_number, frames))
for i in range(frames):
one_frame = np.multiply(xe[:, i*shift_len:i*shift_len+win_len], np.repeat(window, win_len, 1))
cochleagram[:, i] = np.sqrt(np.mean(one_frame, 1))
cochleagram = np.where(cochleagram == 0.0, np.finfo(float).eps, cochleagram)
return cochleagram
def postaud(x, fmax, fbtype=None):
if fbtype is None:
fbtype = 'bark'
nbands = x.shape[0]
nframes = x.shape[1]
nfpts = nbands
if fbtype == 'bark':
bancfhz = bark2freq(np.linspace(0, freq2bark(fmax), nfpts))
fsq = bancfhz * bancfhz
ftmp = fsq + 1.6e5
eql = ((fsq/ftmp)**2) * ((fsq + 1.44e6)/(fsq + 9.61e6))
eql = eql.reshape(np.size(eql), 1)
z = np.repeat(eql, nframes, axis=1) * x
z = z ** (1./3.)
y = np.vstack((z[1, :], z[1:nbands-1, :], z[nbands-2, :]))
return y
def lpc2cep(a, nout=None):
nin = np.size(a, 0)
ncol = np.size(a, 1)
order = nin - 1
if nout is None:
nout = order + 1
c = np.zeros((nout, ncol))
c[0, :] = -1. * np.log(a[0, :])
renormal_coef = np.reshape(a[0,:], (1, ncol))
renormal_coef = np.repeat(renormal_coef, nin, axis=0)
a = a / renormal_coef
for n in range(1, nout):
sumn = np.zeros(ncol)
for m in range(1, n+1):
sumn = sumn + (n-m) * a[m, :] * c[n-m, :]
c[n, :] = -1. * (a[n, :] + 1. / n * sumn)
return c
def postaud(x, fmax, fbtype=None):
if fbtype is None:
fbtype = 'bark'
nbands = x.shape[0]
nframes = x.shape[1]
nfpts = nbands
if fbtype == 'bark':
bancfhz = bark2freq(np.linspace(0, freq2bark(fmax), nfpts))
fsq = bancfhz * bancfhz
ftmp = fsq + 1.6e5
eql = ((fsq/ftmp)**2) * ((fsq + 1.44e6)/(fsq + 9.61e6))
'''
plt.figure()
plt.plot(eql)
plt.show()
'''
eql = eql.reshape(np.size(eql), 1)
z = np.repeat(eql, nframes, axis=1) * x
z = z ** (1./3.)
y = np.vstack((z[1, :], z[1:nbands-1, :], z[nbands-2, :]))
return y
def lpc2cep(a, nout=None):
nin = np.size(a, 0)
ncol = np.size(a, 1)
order = nin - 1
if nout is None:
nout = order + 1
c = np.zeros((nout, ncol))
c[0, :] = -1. * np.log(a[0, :])
renormal_coef = np.reshape(a[0,:], (1, ncol))
renormal_coef = np.repeat(renormal_coef, nin, axis=0)
a = a / renormal_coef
for n in range(1, nout):
sumn = np.zeros(ncol)
for m in range(1, n+1):
sumn = sumn + (n-m) * a[m, :] * c[n-m, :]
c[n, :] = -1. * (a[n, :] + 1. / n * sumn)
return c
def get_im2col_indices(x_shape, filter_shape, stride, pad):
BS, in_D, in_H, in_W = x_shape
f_H, f_W = filter_shape
pad_H, pad_W = pad
stride_H, stride_W = stride
out_H = int((in_H + 2*pad_H - f_H) / stride_W + 1)
out_W = int((in_W + 2*pad_W - f_W) / stride_W + 1)
i_col = np.repeat(np.arange(f_H), f_W)
i_col = np.tile(i_col, in_D).reshape(-1, 1)
i_row = stride_H * np.repeat(np.arange(out_H), out_W)
i = i_col + i_row #shape=(in_D*f_H*f_W,out_H*out_W)
j_col = np.tile(np.arange(f_W), f_H)
j_col = np.tile(j_col, in_D).reshape(-1, 1)
j_row = stride_W * np.tile(np.arange(out_W), out_H)
j = j_col + j_row #shape=(in_D*f_H*f_W,out_W*out_H)
c = np.repeat(np.arange(in_D), f_H * f_W).reshape(-1, 1) #shape=(in_D*f_H*f_W,1)
return (c, i, j)
def _conform_kernel_to_tensor(kernel, tensor, shape):
""" Re-shape a convolution kernel to match the given tensor's color dimensions. """
l = len(kernel)
channels = shape[-1]
temp = np.repeat(kernel, channels)
temp = tf.reshape(temp, (l, l, channels, 1))
temp = tf.cast(temp, tf.float32)
temp /= tf.maximum(tf.reduce_max(temp), tf.reduce_min(temp) * -1)
return temp
def specular_reflection_matrix(self, frequency, eps_1, mu1, npol, compute_coherent_only):
if npol > 2:
raise NotImplementedError("active model is not yet implemented, need modification for the third compunant")
if self.backscatter_coefficient is not None:
raise NotImplementedError("backscatter_coefficient to be implemented")
if self.specular_reflection is None and self.backscatter_coefficient is None:
self.specular_reflection = 1
if isinstance(self.specular_reflection, dict): # we have a dictionary with polarization
spec_refl_coeff = np.empty(npol*len(mu1))
spec_refl_coeff[0::npol] = self._get_refl(self.specular_reflection['V'], mu1)
spec_refl_coeff[1::npol] = self._get_refl(self.specular_reflection['H'], mu1)
else: # we have a scalar, both polarization are the same
spec_refl_coeff = np.repeat(self._get_refl(self.specular_reflection, mu1), npol)
return scipy.sparse.diags(spec_refl_coeff, 0)
def absorption_matrix(self, frequency, eps_1, mu1, npol, compute_coherent_only):
if self.specular_reflection is None and self.backscatter_coefficient is None:
self.specular_reflection = 1
if npol > 2:
raise NotImplementedError("active model is not yet implemented, need modification for the third compunant")
if isinstance(self.specular_reflection, dict): # we have a dictionary with polarization
abs_coeff = np.empty(npol*len(mu1))
abs_coeff[0::npol] = 1 - self._get_refl(self.specular_reflection['V'], mu1)
abs_coeff[1::npol] = 1 - self._get_refl(self.specular_reflection['H'], mu1)
else: # we have a scalar, both polarization are the same
abs_coeff = 1 - np.repeat(self._get_refl(self.specular_reflection, mu1), npol)
return scipy.sparse.diags(abs_coeff, 0)
def specular_reflection_matrix(self, frequency, eps_1, mu1, npol, compute_coherent_only):
if npol > 2 and not hasattr(self, "stop_pol2_warning"):
print("active model is not yet fully implemented, need modification for the third component") # !!!
self.stop_pol2_warning = True
if self.specular_reflection is None and self.backscattering_coefficient is None:
self.specular_reflection = 1
if isinstance(self.specular_reflection, dict): # we have a dictionary with polarization
spec_refl_coeff = np.empty(npol*len(mu1))
spec_refl_coeff[0::npol] = self._get_refl(self.specular_reflection['V'], mu1)
spec_refl_coeff[1::npol] = self._get_refl(self.specular_reflection['H'], mu1)
else: # we have a scalar, both polarization are the same
spec_refl_coeff = np.repeat(self._get_refl(self.specular_reflection, mu1), npol)
return scipy.sparse.diags(spec_refl_coeff, 0)
def absorption_matrix(self, frequency, eps_1, mu1, npol, compute_coherent_only):
if self.specular_reflection is None and self.backscattering_coefficient is None:
self.specular_reflection = 1
if npol > 2 and not hasattr(self, "stop_pol2_warning"):
print("active model is not yet fully implemented, need modification for the third component") # !!!
self.stop_pol2_warning = True
if isinstance(self.specular_reflection, dict): # we have a dictionary with polarization
abs_coeff = np.empty(npol*len(mu1))
abs_coeff[0::npol] = 1 - self._get_refl(self.specular_reflection['V'], mu1)
abs_coeff[1::npol] = 1 - self._get_refl(self.specular_reflection['H'], mu1)
else: # we have a scalar, both polarization are the same
abs_coeff = 1 - np.repeat(self._get_refl(self.specular_reflection, mu1), npol)
return scipy.sparse.diags(abs_coeff, 0)
def cartesian(arrays, out=None, dtype='f'):
"""http://stackoverflow.com/questions/28684492/numpy-equivalent-of-itertools-product"""
arrays = [np.asarray(x) for x in arrays]
# dtype = arrays[0].dtype
n = np.prod([x.size for x in arrays])
if out is None:
out = np.zeros([n, len(arrays)], dtype=dtype)
m = int(n / arrays[0].size)
out[:,0] = np.repeat(arrays[0], m)
if arrays[1:]:
cartesian(arrays[1:], out=out[0:m,1:])
for j in range(1, arrays[0].size):
out[j*m:(j+1)*m,1:] = out[0:m,1:]
return out
def __call__(self, root, combo):
subject, session = decode_subject_and_session(combo.subject)
path = os.path.join(root,
'subject%d' % subject,
'session%d' % session,
'gest%d.mat' % combo.gesture)
if path not in self.memo:
data = _get_data(path, self.preprocess)
self.memo[path] = data
logger.debug('{}', path)
else:
data = self.memo[path]
assert combo.trial < len(data), str(combo)
data = data[combo.trial].copy()
gesture = np.repeat(combo.gesture, len(data))
subject = np.repeat(combo.subject, len(data))
return Trial(data=data, gesture=gesture, subject=subject)
def add_vibrational_mode(uni, freqdx):
displacements = uni.frequency.displacements(freqdx)
if not all(displacements['symbol'] == uni.atom['symbol']):
print('Mismatch in ordering of atoms and frequencies.')
return
displaced = []
frames = []
# Should these only be absolute values?
factor = np.abs(np.sin(np.linspace(-4*np.pi, 4*np.pi, 200)))
for fac in factor:
moved = uni.atom.copy()
moved['x'] += displacements['dx'].values * fac
moved['y'] += displacements['dy'].values * fac
moved['z'] += displacements['dz'].values * fac
displaced.append(moved)
frames.append(uni.frame)
movie = pd.concat(displaced).reset_index()
movie['frame'] = np.repeat(range(len(factor)), len(uni.atom))
uni.frame = pd.concat(frames).reset_index()
uni.atom = movie