def timeIndex(self, slider):
## Return the time and frame index indicated by a slider
if self.image is None:
return (0,0)
t = slider.value()
xv = self.tVals
if xv is None:
ind = int(t)
else:
if len(xv) < 2:
return (0,0)
totTime = xv[-1] + (xv[-1]-xv[-2])
inds = np.argwhere(xv < t)
if len(inds) < 1:
return (0,t)
ind = inds[-1,0]
return ind, t
python类argwhere()的实例源码
def chooseErrorData(self, game, lesson=None):
'''
Choose saved error function data by lesson and game name in
history database.
'''
self.history.setGame(game)
self.load()
if lesson is not None:
self.error_data_training = np.split(self.data[0,:],
np.argwhere(self.data[0,:] == -1))[lesson][1:]
self.error_data_test = np.split(self.data[1,:],
np.argwhere(self.data[1,:] == -1))[lesson][1:]
else:
self.error_data_training = np.delete(self.data[0,:],
np.argwhere(self.data[0,:]==-1))
self.error_data_test = np.delete(self.data[1,:],
np.argwhere(self.data[1,:]==-1))
# ------------------- for test and show reasons only ----------------------
def getSection(self, Address):
# find
idx = numpy.argwhere(self._SectionsFast[:]['Start'] <= Address).flatten()
if len(idx) == 0:
return None
# check
if Address < self._SectionsFast[idx[-1]]['Start'] + \
self._SectionsFast[idx[-1]]['Size']:
return (self._Sections[self._SectionsFast[idx[-1]]['Start']])
else:
return None
##
# Get symbol from given address.
#
# @param Address address within image
# @return the symbol of the address (None if error)
#
def getSymbol(self, Address):
# find
idx = numpy.argwhere(self._SymbolsFast[:]['Start'] <= Address).flatten()
if len(idx) == 0:
return None
# check
if Address < self._SymbolsFast[idx[-1]]['Start'] + \
self._SymbolsFast[idx[-1]]['Size']:
return (self._Symbols[self._SymbolsFast[idx[-1]]['Start']])
else:
return None
##
# Get instruction from given address.
#
# @param Address address within image
# @return size of instr. and assembly code (None if error)
#
def get_antonyms(self,wordA:str, topk:int=10,ispositive:bool=True):
seed=[['??','??'],['??','??'],['??','??'],['??','??'],['??','??']]
proposal={}
for pair in seed:
if ispositive:
result=self.analogy(pair[0],pair[1],wordA,topk)
print(w2v.find_nearest_word((self[pair[0]] + self[pair[1]]) / 2, 3))
else:
result = self.analogy(pair[1], pair[0], wordA, topk)
print(w2v.find_nearest_word((self[pair[0]] + self[pair[1]]) / 2, 3))
for item in result:
term_products = np.argwhere(self[wordA] * self[item[0]] < 0)
#print(item[0] + ':' +wordA + str(term_products))
#print(item[0] + ':' +wordA+'('+str(pair)+') '+ str(len(term_products)))
if len(term_products)>=self.dims/4:
if item[0] not in proposal:
proposal[item[0]] = item[1]
elif item[1]> proposal[item[0]]:
proposal[item[0]] +=item[1]
for k,v in proposal.items():
proposal[k]=v/len(seed)
sortitems=sorted(proposal.items(), key=lambda d: d[1],reverse=True)
return [sortitems[i] for i in range(min(topk,len(sortitems)))]
def prepare_data(seg_img, ins_img):
labels = []
bboxes = []
masks = []
instances = np.unique(ins_img)
for inst in instances[instances != -1]:
mask_inst = ins_img == inst
count = collections.Counter(seg_img[mask_inst].tolist())
instance_class = max(count.items(), key=lambda x: x[1])[0]
assert inst not in [-1]
assert instance_class not in [-1, 0]
where = np.argwhere(mask_inst)
(y1, x1), (y2, x2) = where.min(0), where.max(0) + 1
labels.append(instance_class)
bboxes.append((y1, x1, y2, x2))
masks.append(mask_inst)
labels = np.array(labels)
bboxes = np.array(bboxes)
masks = np.array(masks)
return bboxes, masks, labels
def _step(self, action_n):
observation_n, reward_n, done_n, info = self.env.step(action_n)
# Pass along ID of potentially-done episode
for i, info_i in enumerate(info['n']):
info_i['vectorized.episode_id'] = self.episode_ids[i]
done_i = np.argwhere(done_n).reshape(-1)
if len(done_i):
for i in done_i:
self.extra_done.add(self.episode_ids[i])
# Episode completed, so we bump its value
self.episode_ids[i] += self.n
if self.episode_limit is not None and self.episode_ids[i] > self.episode_limit:
logger.debug('Masking: index=%s episode_id=%s', i, self.episode_ids[i])
self.env.mask(i)
self._set_done_to()
# Pass along the number of contiguous episodes that are now done
info['vectorized.done_to'] = self.done_to
return observation_n, reward_n, done_n, info
def roi_index_to_volume_index(roi_indices, brain_mask):
"""Get the 3d index of a voxel given the linear index in a ROI created with the given brain mask.
This is the inverse function of :func:`volume_index_to_roi_index`.
This function is useful if you, for example, have sampling results of a specific voxel
and you want to locate that voxel in the brain maps.
Please note that this function can be memory intensive for a large list of roi_indices
Args:
roi_indices (int or ndarray): the index in the ROI created by that brain mask
brain_mask (str or 3d array): the brain mask you would like to use
Returns:
ndarray: the 3d voxel location(s) of the indicated voxel(s)
"""
mask = autodetect_brain_mask_loader(brain_mask).get_data()
return np.argwhere(mask)[roi_indices, :]
def _get_Smatrices(self, X, y):
Sb = np.zeros((X.shape[1], X.shape[1]))
S = np.inner(X.T, X.T)
N = len(X)
mu = np.mean(X, axis=0)
classLabels = np.unique(y)
for label in classLabels:
classIdx = np.argwhere(y == label).T[0]
Nl = len(classIdx)
xL = X[classIdx]
muL = np.mean(xL, axis=0)
muLbar = muL - mu
Sb = Sb + Nl * np.outer(muLbar, muLbar)
Sbar = S - N * np.outer(mu, mu)
Sw = Sbar - Sb
self.mean_ = mu
return (Sw, Sb)
def _set_sparse_diagonal(rows, cols, data, preferences):
idx = np.where(rows == cols)
data[idx] = preferences[rows[idx]]
mask = np.ones(preferences.shape, dtype=bool)
mask[rows[idx]] = False
diag_other = np.argwhere(mask).T[0]
rows = np.concatenate((rows, diag_other))
cols = np.concatenate((cols, diag_other))
data = np.concatenate((data, preferences[mask]))
# return data sorted by row
idx_sorted_left_ori = np.lexsort((cols, rows))
rows = rows[idx_sorted_left_ori]
cols = cols[idx_sorted_left_ori]
data = data[idx_sorted_left_ori]
return rows, cols, data
def median_buffer_range(mag, magerr):
"""This function returns the ratio of points that are between plus or minus 10% of the
amplitude value over the mean
:param mag: the time-varying intensity of the lightcurve. Must be an array.
:param magerr: photometric error for the intensity. Must be an array.
:rtype: float
"""
mag, magerr = remove_bad(mag, magerr)
n = np.float(len(mag))
amp = amplitude(mag, magerr)
#mean = meanMag(mag, magerr)
mean = np.median(mag)
a = mean - amp/10.
b = mean + amp/10.
median_buffer_range = len(np.argwhere((mag > a) & (mag < b))) / n
return median_buffer_range
def median_buffer_range2(mag, magerr):
"""This function returns the ratio of points that are more than 20% of the amplitude
value over the mean
:param mag: the time-varying intensity of the lightcurve. Must be an array.
:param magerr: photometric error for the intensity. Must be an array.
:rtype: float
"""
mag, magerr = remove_bad(mag, magerr)
n = np.float(len(mag))
amp = amplitude(mag, magerr)
#mean = meanMag(mag, magerr)
mean = np.median(mag)
a = mean - amp/5.
median_buffer_range = len(np.argwhere((mag < a))) / n
return median_buffer_range
def above1(mag, magerr):
"""This function measures the ratio of data points that are above 1 standard deviation
from the mean magnitude.
:param mag: the time-varying intensity of the lightcurve. Must be an array.
:param magerr: photometric error for the intensity. Must be an array.
:rtype: float
"""
mag, magerr = remove_bad(mag, magerr)
a = meanMag(mag, magerr) - deviation(mag, magerr)
above1 = len(np.argwhere(mag < a) )
return above1
def above3(mag, magerr):
"""This function measures the ratio of data points that are above 3 standard deviations
from the mean magnitude.
:param mag: the time-varying intensity of the lightcurve. Must be an array.
:param magerr: photometric error for the intensity. Must be an array.
:rtype: float
"""
mag, magerr = remove_bad(mag, magerr)
a = meanMag(mag, magerr) - 3*deviation(mag, magerr)
above3 = len(np.argwhere(mag < a) )
return above3
def below1(mag, magerr):
"""This function measures the ratio of data points that are below 1 standard deviations
from the mean magnitude.
:param mag: the time-varying intensity of the lightcurve. Must be an array.
:param magerr: photometric error for the intensity. Must be an array.
:rtype: float
"""
mag, magerr = remove_bad(mag, magerr)
a = meanMag(mag, magerr) + deviation(mag, magerr)
below1 = len(np.argwhere(mag > a))
return below1
def below3(mag, magerr):
"""This function measures the ratio of data points that are below 3 standard deviations
from the mean magnitude.
:param mag: the time-varying intensity of the lightcurve. Must be an array.
:param magerr: photometric error for the intensity. Must be an array.
:rtype: float
"""
mag, magerr = remove_bad(mag, magerr)
a = meanMag(mag, magerr) + 3*deviation(mag, magerr)
below3 = len(np.argwhere(mag > a))
return below3
def below5(mag, magerr):
"""This function measures the ratio of data points that are below 5 standard deviations
from the mean magnitude.
:param mag: the time-varying intensity of the lightcurve. Must be an array.
:param magerr: photometric error for the intensity. Must be an array.
:rtype: float
"""
mag, magerr = remove_bad(mag, magerr)
a = meanMag(mag, magerr) + 5*deviation(mag, magerr)
below5 = len(np.argwhere(mag > a))
return below5
def gen(self, n_samples=1, u=None):
"""
Generate samples from made. Requires as many evaluations as number of inputs.
:param n_samples: number of samples
:param u: random numbers to use in generating samples; if None, new random numbers are drawn
:return: samples
"""
x = np.zeros([n_samples, self.n_inputs], dtype=dtype)
u = rng.randn(n_samples, self.n_inputs).astype(dtype) if u is None else u
for i in xrange(1, self.n_inputs + 1):
m, logp = self.eval_comps(x)
idx = np.argwhere(self.input_order == i)[0, 0]
x[:, idx] = m[:, idx] + np.exp(np.minimum(-0.5 * logp[:, idx], 10.0)) * u[:, idx]
return x
def gen(self, n_samples=1, u=None):
"""
Generate samples from made. Requires as many evaluations as number of inputs.
:param n_samples: number of samples
:param u: random numbers to use in generating samples; if None, new random numbers are drawn
:return: samples
"""
x = np.zeros([n_samples, self.n_inputs], dtype=dtype)
u = rng.randn(n_samples, self.n_inputs).astype(dtype) if u is None else u
for i in xrange(1, self.n_inputs + 1):
m, logp, loga = self.eval_comps(x)
idx = np.argwhere(self.input_order == i)[0, 0]
for n in xrange(n_samples):
z = util.discrete_sample(np.exp(loga[n, idx]))[0]
x[n, idx] = m[n, idx, z] + np.exp(np.minimum(-0.5 * logp[n, idx, z], 10.0)) * u[n, idx]
return x
def gen(self, x, n_samples=1, u=None):
"""
Generate samples from made conditioned on x. Requires as many evaluations as number of outputs.
:param x: input vector
:param n_samples: number of samples
:param u: random numbers to use in generating samples; if None, new random numbers are drawn
:return: samples
"""
y = np.zeros([n_samples, self.n_outputs], dtype=dtype)
u = rng.randn(n_samples, self.n_outputs).astype(dtype) if u is None else u
xy = (np.tile(x, [n_samples, 1]), y)
for i in xrange(1, self.n_outputs + 1):
m, logp = self.eval_comps(xy)
idx = np.argwhere(self.output_order == i)[0, 0]
y[:, idx] = m[:, idx] + np.exp(np.minimum(-0.5 * logp[:, idx], 10.0)) * u[:, idx]
return y
def gen(self, x, n_samples=1, u=None):
"""
Generate samples from made conditioned on x. Requires as many evaluations as number of outputs.
:param x: input vector
:param n_samples: number of samples
:param u: random numbers to use in generating samples; if None, new random numbers are drawn
:return: samples
"""
y = np.zeros([n_samples, self.n_outputs], dtype=dtype)
u = rng.randn(n_samples, self.n_outputs).astype(dtype) if u is None else u
xy = (np.tile(x, [n_samples, 1]), y)
for i in xrange(1, self.n_outputs + 1):
m, logp, loga = self.eval_comps(xy)
idx = np.argwhere(self.output_order == i)[0, 0]
for n in xrange(n_samples):
z = util.discrete_sample(np.exp(loga[n, idx]))[0]
y[n, idx] = m[n, idx, z] + np.exp(np.minimum(-0.5 * logp[n, idx, z], 10.0)) * u[n, idx]
return y
def select_action(self, st, runstep=None):
with self.G.as_default():
if np.random.rand() > self.params['eps']:
#greedy with random tie-breaking
if not self.forward_only:
Q_pred = self.sess.run(self.qnet.y, feed_dict = {self.qnet.x: np.reshape(st, (1,84,84,4))})[0]
else:
Q_pred = runstep(self.sess, self.qnet.y, feed_dict = {self.qnet.x: np.reshape(st, (1,84,84,4))})[0]
a_winner = np.argwhere(Q_pred == np.amax(Q_pred))
if len(a_winner) > 1:
act_idx = a_winner[np.random.randint(0, len(a_winner))][0]
return act_idx,self.engine.legal_actions[act_idx], np.amax(Q_pred)
else:
act_idx = a_winner[0][0]
return act_idx,self.engine.legal_actions[act_idx], np.amax(Q_pred)
else:
#random
act_idx = np.random.randint(0,len(self.engine.legal_actions))
if not self.forward_only:
Q_pred = self.sess.run(self.qnet.y, feed_dict = {self.qnet.x: np.reshape(st, (1,84,84,4))})[0]
else:
Q_pred = runstep(self.sess, self.qnet.y, feed_dict = {self.qnet.x: np.reshape(st, (1,84,84,4))})[0]
return act_idx,self.engine.legal_actions[act_idx], Q_pred[act_idx]
def get_idxs(self, idxs):
"""
Returns the states a the provided indexes.
Args:
idxs (list): the indexes of the states to return.
Returns:
The states at the provided indexes.
"""
if not self._full and np.any(idxs < self._history_length):
idxs[np.argwhere(
idxs < self._history_length).ravel()] += self._history_length
s = self._get_state(idxs - 1)
ss = self._get_state(idxs)
return s, self._actions[idxs - 1, ...], self._rewards[idxs - 1, ...],\
ss, self._absorbing[idxs - 1, ...], self._last[idxs - 1, ...]
def select_episodes(dataset, n_episodes, parse=False):
"""
Return the first `n_episodes` episodes in the provided dataset.
Args:
dataset (list): the dataset to consider;
n_episodes (int): the number of episodes to pick from the dataset;
parse (bool, False): whether to parse the dataset to return.
Returns:
A subset of the dataset containing the first `n_episodes` episodes.
"""
assert n_episodes >= 0, 'Number of episodes must be greater than or equal' \
'to zero.'
if n_episodes == 0:
return np.array([[]])
dataset = np.array(dataset)
last_idxs = np.argwhere(dataset[:, -1] == 1).ravel()
sub_dataset = dataset[:last_idxs[n_episodes - 1] + 1, :]
return sub_dataset if not parse else parse_dataset(sub_dataset)
def fit(self, state, action, q, **fit_params):
"""
Fit the model.
Args:
state (np.ndarray): states;
action (np.ndarray): actions;
q (np.ndarray): target q-values;
**fit_params (dict): other parameters used by the fit method
of each regressor.
"""
state, q = self._preprocess(state, q)
for i in xrange(len(self.model)):
idxs = np.argwhere((action == i)[:, 0]).ravel()
if idxs.size:
self.model[i].fit(state[idxs, :], q[idxs], **fit_params)
def _update(self, state, action, reward, next_state, absorbing):
approximator_idx = 0 if np.random.uniform() < .5 else 1
q_current = self.Q[approximator_idx][state, action]
if not absorbing:
q_ss = self.Q[approximator_idx][next_state, :]
max_q = np.max(q_ss)
a_n = np.array(
[np.random.choice(np.argwhere(q_ss == max_q).ravel())])
q_next = self.Q[1 - approximator_idx][next_state, a_n]
else:
q_next = 0.
q = q_current + self.alpha[approximator_idx](state, action) * (
reward + self.mdp_info.gamma * q_next - q_current)
self.Q[approximator_idx][state, action] = q
def step(self, action):
self._grid = self.convert_to_grid(self._state, *self._grid.shape)
state = np.argwhere(self._grid == self._symbols['S']).ravel()
new_state, reward, absorbing, info = self._step(state, action)
if info['success']:
self._grid[tuple(state)] = self._symbols['.']
self._grid[tuple(new_state)] = self._symbols['S']
self._state = self.convert_to_pixel(self._grid,
self.window_size[1],
self.window_size[0])
return self._state, reward, absorbing, info
def grid_to_adjacency_matrix(grid, neighborhood=8):
"""Convert a boolean grid where 0's express holes and 1's connected pixel
into a sparse adjacency matrix representing the grid-graph.
Neighborhood for each pixel is calculated from its 4 or 8 more immediate
surrounding neighbors (defaults to 8)."""
coords = np.argwhere(grid)
coords_x = coords[:, 0]
coords_y = coords[:, 1]
# lil is the most performance format to build a sparse matrix iteratively
matrix = sparse.lil_matrix((0, coords.shape[0]), dtype=np.uint8)
if neighborhood == 4:
for px, py in coords:
row = (((px == coords_x) & (np.abs(py - coords_y) == 1)) |
((np.abs(px - coords_x) == 1) & (py == coords_y)))
matrix = sparse.vstack([matrix, row])
else:
for px, py in coords:
row = (np.abs(px - coords_x) <= 1) & (np.abs(py - coords_y) <= 1)
matrix = sparse.vstack([matrix, row])
matrix.setdiag(1)
# Once built, we convert it to compressed sparse columns or rows
return matrix.tocsc() # or .tocsr()
def test_contacts_count_contacts(self):
sel = np.array([1, 2, 5, 20], dtype=int)
pairs_expected = np.array([[1, 5], [1, 20], [2, 5], [2, 20], [5, 20]])
pairs = self.feat.pairs(sel, excluded_neighbors=2)
assert(pairs.shape == pairs_expected.shape)
assert(np.all(pairs == pairs_expected))
self.feat.add_contacts(pairs, threshold=0.5, periodic=False, count_contacts=True) # unperiodic distances such that we can compare
# The dimensionality of the feature is now one
assert(self.feat.dimension() == 1)
X = self.traj.xyz[:, pairs_expected[:, 0], :]
Y = self.traj.xyz[:, pairs_expected[:, 1], :]
D = np.sqrt(np.sum((X - Y) ** 2, axis=2))
C = np.zeros(D.shape)
I = np.argwhere(D <= 0.5)
C[I[:, 0], I[:, 1]] = 1.0
# Count the contacts
C = C.sum(1, keepdims=True)
assert(np.allclose(C, self.feat.transform(self.traj)))
def test_Group_Mindist_All_Three_Groups_threshold(self):
threshold = .7
group0 = [0, 20, 30, 0]
group1 = [1, 21, 31, 1]
group2 = [2, 22, 32, 2]
self.feat.add_group_mindist(group_definitions=[group0, group1, group2], threshold=threshold)
D = self.feat.transform(self.traj)
# Now the references, computed separately for each combination of groups
dist_list_01 = np.array(list(product(np.unique(group0), np.unique(group1))))
dist_list_02 = np.array(list(product(np.unique(group0), np.unique(group2))))
dist_list_12 = np.array(list(product(np.unique(group1), np.unique(group2))))
Dref_01 = mdtraj.compute_distances(self.traj, dist_list_01).min(1)
Dref_02 = mdtraj.compute_distances(self.traj, dist_list_02).min(1)
Dref_12 = mdtraj.compute_distances(self.traj, dist_list_12).min(1)
Dref = np.vstack((Dref_01, Dref_02, Dref_12)).T
Dbinary = np.zeros_like(Dref)
I = np.argwhere(Dref <= threshold)
Dbinary[I[:, 0], I[:, 1]] = 1
assert np.allclose(D, Dbinary)
assert len(self.feat.describe())==self.feat.dimension()