def get_source_index(source_name, feature_database):
return np.where(feature_database["sources"] == source_name)[0]
python类where()的实例源码
def get_feature_index(feature_name, feature_database):
return np.where(feature_database["feats"] == feature_name)[0][0]
def get(languages, feature_set_str, header=False, random=False, minimal=False):
lang_codes = languages.split()
feature_names, feature_values = get_concatenated_sets(lang_codes, feature_set_str)
feature_names = np.array([ f.replace(" ","_") for f in feature_names ])
feats = {}
if minimal:
mask = np.all(feature_values == 0.0, axis=0)
mask |= np.all(feature_values == 1.0, axis=0)
mask |= np.all(feature_values == -1.0, axis=0)
unmasked_indices = np.where(np.logical_not(mask))
else:
unmasked_indices = np.where(np.ones(feature_values.shape[1]))
if random:
feature_values = np.random.random(feature_values.shape) >= 0.5
if header:
print("\t".join(['CODE']+list(feature_names[unmasked_indices])))
feat_names = feature_names[unmasked_indices]
for i, lang_code in enumerate(lang_codes):
values = feature_values[i,unmasked_indices].ravel()
#values = [ '--' if f == -1 else ("%0.4f"%f).rstrip("0").rstrip(".") for f in values ]
feats[lang_code] = values
#print("\t".join([lang_code]+values))
return feats, feat_names
#if __name__ == '__main__':
# argparser = argparse.ArgumentParser()
# argparser.add_argument("languages", default='', help="The languages of interest, in ISO 639-3 codes, separated by spaces (e.g., \"deu eng fra swe\")")
# argparser.add_argument("feature_set", default='', help="The feature set or sets of interest (e.g., \"syntax_knn\" or \"fam\"), joined by concatenation (+) or element-wise union (|).")
# argparser.add_argument("-f", "--fields", default=False, action="store_true", help="Print feature names as the first row of data.")
# argparser.add_argument("-r", "--random", default=False, action="store_true", help="Randomize all feature values (e.g., to make a control group).")
# argparser.add_argument("-m", "--minimal", default=False, action="store_true", help="Suppress columns that are all 0, all 1, or all nulls.")
# args = argparser.parse_args()
# get(args.languages, args.feature_set, args.fields, args.random, args.minimal)
def run_numpy():
x = topk.dot(users)
x = np.where(users>0, 0, x)
return x.argmax(axis=0)
def run_dask():
x = t.dot(u)
x = da.where(u>0, 0, x)
r = x.argmax(axis=0)
return r.compute()
def _solve_equation_least_squares(self, A, B):
"""Solve system of linear equations A X = B.
Currently using Pseudo-inverse because it also allows for singular matrices.
Args:
A (numpy.ndarray): Left-hand side of equation.
B (numpy.ndarray): Right-hand side of equation.
Returns:
X (numpy.ndarray): Solution of equation.
"""
# Pseudo-inverse
X = np.dot(np.linalg.pinv(A), B)
# LU decomposition
# lu, piv = scipy.linalg.lu_factor(A)
# X = scipy.linalg.lu_solve((lu, piv), B)
# Vanilla least-squares from numpy
# X, _, _, _ = np.linalg.lstsq(A, B)
# QR decomposition
# Q, R, P = scipy.linalg.qr(A, mode='economic', pivoting=True)
# # Find first zero element in R
# out = np.where(np.diag(R) == 0)[0]
# if out.size == 0:
# i = R.shape[0]
# else:
# i = out[0]
# B_prime = np.dot(Q.T, B)
# X = np.zeros((A.shape[1], B.shape[1]), dtype=A.dtype)
# X[P[:i], :] = scipy.linalg.solve_triangular(R[:i, :i], B_prime[:i, :])
return X
def get_index(self, value):
"""Returns the index of a given value.
Args:
value: Value the index requested for.
Returns:
Index.
"""
index, = np.where(np.abs(self.vector - value) <= self.snap_radius)
assert len(index) < 2, "Multiple points found within snap radius of given value."
assert len(index) > 0, "No point found within snap radius of given value."
return int(index)
def gen_minibatch(tokens, features, labels, mini_batch_size, shuffle= True):
tokens = np.asarray(tokens)[np.where(labels!=0.5)[0]]
if type(features) is np.ndarray:
features = np.asarray(features)[np.where(labels!=0.5)[0]]
else:
features = np.asarray(features.todense())[np.where(labels!=0.5)[0]]
labels = np.asarray(labels)[np.where(labels!=0.5)[0]]
# print tokens.shape
# print tokens[0]
for token, feature, label in iterate_minibatches(tokens, features, labels, mini_batch_size, shuffle = shuffle):
# print 'token', type(token)
# print token
token = [_ for _ in pad_batch(token)]
# print len(token), token[0].size(), token[1].size()
yield token, Variable(torch.from_numpy(feature)) , Variable(torch.FloatTensor(label), requires_grad= False)
def gen_minibatch(tokens, features, labels, mini_batch_size, shuffle= True):
tokens = np.asarray(tokens)[np.where(labels!=0.5)[0]]
features = np.asarray(features.todense())[np.where(labels!=0.5)[0]]
labels = np.asarray(labels)[np.where(labels!=0.5)[0]]
# print tokens.shape
# print tokens[0]
for token, feature, label in iterate_minibatches(tokens, features, labels, mini_batch_size, shuffle = shuffle):
# print 'token', type(token)
# print token
token = [_ for _ in pad_batch(token)]
# print len(token), token[0].size(), token[1].size()
yield token, Variable(torch.from_numpy(feature)) , Variable(torch.FloatTensor(label), requires_grad= False)
def view_waveforms_clusters(data, halo, threshold, templates, amps_lim, n_curves=200, save=False):
nb_templates = templates.shape[1]
n_panels = numpy.ceil(numpy.sqrt(nb_templates))
mask = numpy.where(halo > -1)[0]
clust_idx = numpy.unique(halo[mask])
fig = pylab.figure()
square = True
center = len(data[0] - 1)//2
for count, i in enumerate(xrange(nb_templates)):
if square:
pylab.subplot(n_panels, n_panels, count + 1)
if (numpy.mod(count, n_panels) != 0):
pylab.setp(pylab.gca(), yticks=[])
if (count < n_panels*(n_panels - 1)):
pylab.setp(pylab.gca(), xticks=[])
subcurves = numpy.where(halo == clust_idx[count])[0]
for k in numpy.random.permutation(subcurves)[:n_curves]:
pylab.plot(data[k], '0.5')
pylab.plot(templates[:, count], 'r')
pylab.plot(amps_lim[count][0]*templates[:, count], 'b', alpha=0.5)
pylab.plot(amps_lim[count][1]*templates[:, count], 'b', alpha=0.5)
xmin, xmax = pylab.xlim()
pylab.plot([xmin, xmax], [-threshold, -threshold], 'k--')
pylab.plot([xmin, xmax], [threshold, threshold], 'k--')
#pylab.ylim(-1.5*threshold, 1.5*threshold)
ymin, ymax = pylab.ylim()
pylab.plot([center, center], [ymin, ymax], 'k--')
pylab.title('Cluster %d' %i)
if nb_templates > 0:
pylab.tight_layout()
if save:
pylab.savefig(os.path.join(save[0], 'waveforms_%s' %save[1]))
pylab.close()
else:
pylab.show()
del fig
def view_performance(file_name, triggers, lims=(150,150)):
params = CircusParser(file_name)
N_e = params.getint('data', 'N_e')
N_total = params.getint('data', 'N_total')
sampling_rate = params.getint('data', 'sampling_rate')
do_temporal_whitening = params.getboolean('whitening', 'temporal')
do_spatial_whitening = params.getboolean('whitening', 'spatial')
spike_thresh = params.getfloat('detection', 'spike_thresh')
file_out_suff = params.get('data', 'file_out_suff')
N_t = params.getint('detection', 'N_t')
nodes, edges = get_nodes_and_edges(params)
chunk_size = N_t
if do_spatial_whitening:
spatial_whitening = load_data(params, 'spatial_whitening')
if do_temporal_whitening:
temporal_whitening = load_data(params, 'temporal_whitening')
thresholds = load_data(params, 'thresholds')
try:
result = load_data(params, 'results')
except Exception:
result = {'spiketimes' : {}, 'amplitudes' : {}}
curve = numpy.zeros((len(triggers), len(result['spiketimes'].keys()), lims[1]+lims[0]), dtype=numpy.int32)
count = 0
for count, t_spike in enumerate(triggers):
for key in result['spiketimes'].keys():
elec = int(key.split('_')[1])
idx = numpy.where((result['spiketimes'][key] > t_spike - lims[0]) & (result['spiketimes'][key] < t_spike + lims[0]))
curve[count, elec, t_spike - result['spiketimes'][key][idx]] += 1
pylab.subplot(111)
pylab.imshow(numpy.mean(curve, 0), aspect='auto')
return curve
def slice_result(result, times):
sub_results = []
nb_temp = len(result['spiketimes'])
for t in times:
sub_result = {'spiketimes' : {}, 'amplitudes' : {}}
for key in result['spiketimes'].keys():
idx = numpy.where((result['spiketimes'][key] >= t[0]) & (result['spiketimes'][key] <= t[1]))[0]
sub_result['spiketimes'][key] = result['spiketimes'][key][idx] - t[0]
sub_result['amplitudes'][key] = result['amplitudes'][key][idx]
sub_results += [sub_result]
return sub_results
def encode_segmap(self, mask):
mask = mask.astype(int)
label_mask = np.zeros((mask.shape[0], mask.shape[1]), dtype=np.int16)
for i, label in enumerate(self.get_pascal_labels()):
label_mask[np.where(np.all(mask == label, axis=-1))[:2]] = i
label_mask = label_mask.astype(int)
return label_mask
def voc_ap(rec, prec, use_07_metric=False):
""" ap = voc_ap(rec, prec, [use_07_metric])
Compute VOC AP given precision and recall.
If use_07_metric is true, uses the
VOC 07 11 point method (default:False).
"""
if use_07_metric:
# 11 point metric
ap = 0.
for t in np.arange(0., 1.1, 0.1):
if np.sum(rec >= t) == 0:
p = 0
else:
p = np.max(prec[rec >= t])
ap = ap + p / 11.
else:
# correct AP calculation
# first append sentinel values at the end
mrec = np.concatenate(([0.], rec, [1.]))
mpre = np.concatenate(([0.], prec, [0.]))
# compute the precision envelope
for i in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
# to calculate area under PR curve, look for points
# where X axis (recall) changes value
i = np.where(mrec[1:] != mrec[:-1])[0]
# and sum (\Delta recall) * prec
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
return ap
def chol_inv(B, lower=True):
"""
Returns the inverse of matrix A, where A = B*B.T,
ie B is the Cholesky decomposition of A.
Solves Ax = I
given B is the cholesky factorization of A.
"""
return cho_solve((B, lower), np.eye(B.shape[0]))
def active_set_Lam(self, fixed, vary):
grad = self.grad_wrt_Lam(fixed, vary)
assert np.allclose(grad, grad.T, 1e-3)
return np.where((np.abs(np.triu(grad)) > self.lamL) | (self.Lam != 0))
# return np.where((np.abs(grad) > self.lamL) | (~np.isclose(self.Lam, 0)))
def active_set_Theta(self, fixed, vary):
grad = self.grad_wrt_Theta(fixed, vary)
return np.where((np.abs(grad) > self.lamT) | (self.Theta != 0))
# return np.where((np.abs(grad) > self.lamT) | (~np.isclose(self.Theta, 0)))
def paddle_top(observation, paddle="right"):
column = observation[:, PADDLE_COLUMN[paddle], :] - PADDLE_COLOR[paddle]
found = (np.sum(np.abs(column), axis=1) < TOLERANCE).astype(np.int)
r = np.argmax(found)
if not found[r]:
return None
else:
return r
# def ball_center(observation):
# w = np.where(np.abs(observation[:,6:36] - 0.30457518) > TOLERANCE)[:2]
# if len(w[0]) == 0 or len(w[0]) > 4:
# return None
# w = np.mean(w, axis=1)
# return w[0], w[1] + 6
#
# def ball_on_left(observation):
# w = np.where(np.abs(observation[:,6:21] - 0.30457518) > TOLERANCE)[:2]
# return(len(w[0]) > 0)
def paddle_top(observation, paddle="right"):
column = observation[:, PADDLE_COLUMN[paddle], :] - PADDLE_COLOR[paddle]
found = (np.sum(np.abs(column), axis=1) < TOLERANCE).astype(np.int)
r = np.argmax(found)
if not found[r]:
return None
else:
return r
# def ball_center(observation):
# w = np.where(np.abs(observation[:,6:36] - 0.30457518) > TOLERANCE)[:2]
# if len(w[0]) == 0 or len(w[0]) > 4:
# return None
# w = np.mean(w, axis=1)
# return w[0], w[1] + 6
#
# def ball_on_left(observation):
# w = np.where(np.abs(observation[:,6:21] - 0.30457518) > TOLERANCE)[:2]
# return(len(w[0]) > 0)
def paddle_top(observation, paddle="right"):
column = observation[:, PADDLE_COLUMN[paddle], :] - PADDLE_COLOR[paddle]
found = (np.sum(np.abs(column), axis=1) < TOLERANCE).astype(np.int)
r = np.argmax(found)
if not found[r]:
return None
else:
return r
# def ball_center(observation):
# w = np.where(np.abs(observation[:,6:36] - 0.30457518) > TOLERANCE)[:2]
# if len(w[0]) == 0 or len(w[0]) > 4:
# return None
# w = np.mean(w, axis=1)
# return w[0], w[1] + 6
#
# def ball_on_left(observation):
# w = np.where(np.abs(observation[:,6:21] - 0.30457518) > TOLERANCE)[:2]
# return(len(w[0]) > 0)