Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_small(self):
assert len(self.image.files) == self.num_files_in_sample
data = np.asarray([0xff, 0xff, 0x00, 0x60, 0x01, 0x60, 1, 2], dtype=np.uint8)
self.image.write_file("TEST.XEX", None, data)
assert len(self.image.files) == self.num_files_in_sample + 1
data2 = np.frombuffer(self.image.find_file("TEST.XEX"), dtype=np.uint8)
assert np.array_equal(data, data2[0:len(data)])
def _fit_one(self, model_name, data_batched):
# input data should be batched
data = data_batched
loss = None
m = self._models[model_name]
if model_name=='ori+clf':
loss = m.train_on_batch(self._prepare_inputs(data), [data['sense']])
elif model_name=='gen+clf':
loss = m.train_on_batch(self._prepare_inputs(data,add_arg2=0,add_arg2plus=1,drop_arg2plus=self.drop_conn), [data['sense']])
elif model_name=='joint+clf':
loss = m.train_on_batch(self._prepare_inputs(data,add_arg2plus=1,drop_arg2plus=self.drop_conn), [data['sense'], data['sense']])
elif model_name=='discr':
y_0 = np.asarray([0. for i in data['arg1']])
y_1 = np.asarray([self.alpha for i in data['arg1']]) # using alpha for label smoothing
loss = m.train_on_batch(self._prepare_inputs(data,add_arg2plus=1,add_y=self.discr_ydim), [y_0, y_1, data['sense'], data['sense']])
elif model_name=='ori+clf+discr':
y_1 = np.asarray([self.alpha for i in data['arg1']])
loss = m.train_on_batch(self._prepare_inputs(data,add_arg2plus=1,add_y=self.discr_ydim), [data['sense'], y_1, y_1, y_1])
else:
raise("Unkown model %s." % model_name)
return loss
# Find which view each conditional attribute (column_indices) belongs to
views_for_cols = view_assignments[column_indices]
print(views_for_cols)
for viewindx in views_for_cols:
# Find which cluster the target row is in
tgt_cluster = X_D[viewindx][row_index]
# Find every row in this cluster and give them all a point
match_rows = [i for i in range(len(X_D[viewindx])) if X_D[viewindx][i] == tgt_cluster]
score[match_rows] = score[match_rows] + 1
# Normalize between 0 and 1
normfactor = len(column_indices)*len(X_D_list)
normscore = numpy.asarray([float(a)/normfactor for a in score])
# Sort in descending order
argsorted = numpy.argsort(normscore)[::-1]
sortedscore = normscore[argsorted]
return argsorted[0:num_returns], sortedscore[0:num_returns]
nmo = mo.shape[1]
if s is None:
s = mol.intor_symmetric('int1e_ovlp')
mo_s = numpy.dot(mo.T, s)
norm = numpy.zeros((len(irrep_name), nmo))
for i, csym in enumerate(symm_orb):
moso = numpy.dot(mo_s, csym)
ovlpso = reduce(numpy.dot, (csym.T, s, csym))
try:
norm[i] = numpy.einsum('ik,ki->i', moso, lib.cho_solve(ovlpso, moso.T))
except:
ovlpso[numpy.diag_indices(csym.shape[1])] += 1e-12
norm[i] = numpy.einsum('ik,ki->i', moso, lib.cho_solve(ovlpso, moso.T))
norm /= numpy.sum(norm, axis=0) # for orbitals which are not normalized
iridx = numpy.argmax(norm, axis=0)
orbsym = numpy.asarray([irrep_name[i] for i in iridx])
logger.debug(mol, 'irreps of each MO %s', orbsym)
if check:
largest_norm = norm[iridx,numpy.arange(nmo)]
orbidx = numpy.where(largest_norm < 1-tol)[0]
if orbidx.size > 0:
idx = numpy.where(largest_norm < 1-tol*1e2)[0]
if idx.size > 0:
raise ValueError('orbitals %s not symmetrized, norm = %s' %
(idx, largest_norm[idx]))
else:
logger.warn(mol, 'orbitals %s not strictly symmetrized.',
numpy.unique(orbidx))
logger.warn(mol, 'They can be symmetrized with '
'pyscf.symm.symmetrize_space function.')
logger.debug(mol, 'norm = %s', largest_norm[orbidx])
return orbsym
orig = list(map(reproject, orig))
if prep_first:
orig_prepped = list(map(prep, orig))
else:
orig_prepped = orig
transfer = sparse.lil_matrix((len(dest), len(orig)), dtype=np.float)
for i, j in product(range(len(dest)), range(len(orig))):
if orig_prepped[j].intersects(dest[i]):
area = orig[j].intersection(dest[i]).area
transfer[i, j] = area / dest[i].area
# sum of input vectors must be preserved
if normed:
ssum = np.squeeze(np.asarray(transfer.sum(axis=0)), axis=0)
for i, j in zip(*transfer.nonzero()):
transfer[i, j] /= ssum[j]
return transfer
def normalize_by_range(x, nmin = 0, nmax = 1):
x = np.asarray(x)
return (nmax-nmin)*(x-min(x))/(max(x)-min(x)) + nmin
def gpu_sweep_col_div(X, y):
""" X / y = X across the columns """
if type(X) == GPUArray:
gX = X
else:
gX = to_gpu(np.asarray(X, dtype=np.float32))
if type(y) == GPUArray:
gy = y
else:
gy = to_gpu(np.asarray(y, dtype=np.float32))
dims = np.asarray(X.shape, dtype=np.int32)
if devinfo.max_block_threads >= 1024:
blocksize = 32
else:
blocksize = 16
gridsize = int(dims[0] / blocksize) + 1
shared = 4*blocksize
if gX.flags.c_contiguous:
func = CUDA_Kernels.get_function("sweep_columns_div")
else:
func = CUDA_Kernels.get_function("sweep_columns_div_cm")
func(gX, gy, dims[0], dims[1], block=(blocksize, blocksize, 1),
grid = (gridsize, 1), shared = shared)
def get_pascal_labels(self):
return np.asarray([[0,0,0], [128,0,0], [0,128,0], [128,128,0], [0,0,128], [128,0,128],
[0,128,128], [128,128,128], [64,0,0], [192,0,0], [64,128,0], [192,128,0],
[64,0,128], [192,0,128], [64,128,128], [192,128,128], [0, 64,0], [128, 64, 0],
[0,192,0], [128,192,0], [0,64,128]])
Parameters
----------
cmatrix : connection/adjacency matrix
Returns
-------
Min : matching index for incoming connections
"""
m = bct.to_gslm(cmatrix.tolist())
mi = bct.matching_ind_in(m)
minp = bct.from_gsl(mi)
bct.gsl_free(m)
bct.gsl_free(mi)
return np.asarray(minp)
for qid, r in qids_rs:
gold = harvest(Y, qid)
gold_topk = gold[argtopk(gold, k)]
R = np.count_nonzero(gold_topk)
# real ndcg
idcg = rm.dcg_at_k(gold_topk, k)
ndcg = rm.dcg_at_k(r, k) / idcg
values["ndcg"].append(ndcg)
# Verified
# MAP@k
ap = rm.average_precision(r)
values["MAP"].append(ap)
# MRR - compute by hand
ind = np.asarray(r).nonzero()[0]
mrr = (1. / (ind[0] + 1)) if ind.size else 0.
values["MRR"].append(mrr)
# R precision
# R = min(R, k) # ok lets be fair.. you cant get more than k
# we dont need that anymore, since we chop of the remainder
# before computing R
recall = rm.recall(r, R)
values["recall"].append(recall)
# precision = rm.precision_at_k(pad(scored_result, k), k)
precision = rm.precision(r)
values["precision"].append(precision)
f1 = f1_score(precision, recall)
values["f1_score"].append(f1)