def _logcdf(self, samples):
lower = np.full(2, -np.inf)
upper = norm.ppf(samples)
limit_flags = np.zeros(2)
if upper.shape[0] > 0:
def func1d(upper1d):
'''
Calculates the multivariate normal cumulative distribution
function of a single sample.
'''
return mvn.mvndst(lower, upper1d, limit_flags, self.theta)[1]
vals = np.apply_along_axis(func1d, -1, upper)
else:
vals = np.empty((0, ))
old_settings = np.seterr(divide='ignore')
vals = np.log(vals)
np.seterr(**old_settings)
vals[np.any(samples == 0.0, axis=1)] = -np.inf
vals[samples[:, 0] == 1.0] = np.log(samples[samples[:, 0] == 1.0, 1])
vals[samples[:, 1] == 1.0] = np.log(samples[samples[:, 1] == 1.0, 0])
return vals
python类apply_along_axis()的实例源码
def _nanmedian(a, axis=None, out=None, overwrite_input=False):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanmedian for parameter usage
"""
if axis is None or a.ndim == 1:
part = a.ravel()
if out is None:
return _nanmedian1d(part, overwrite_input)
else:
out[...] = _nanmedian1d(part, overwrite_input)
return out
else:
# for small medians use sort + indexing which is still faster than
# apply_along_axis
if a.shape[axis] < 400:
return _nanmedian_small(a, axis, out, overwrite_input)
result = np.apply_along_axis(_nanmedian1d, axis, a, overwrite_input)
if out is not None:
out[...] = result
return result
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
interpolation='linear', keepdims=False):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanpercentile for parameter usage
"""
if axis is None:
part = a.ravel()
result = _nanpercentile1d(part, q, overwrite_input, interpolation)
else:
result = np.apply_along_axis(_nanpercentile1d, axis, a, q,
overwrite_input, interpolation)
# apply_along_axis fills in collapsed axis with results.
# Move that axis to the beginning to match percentile's
# convention.
if q.ndim != 0:
result = np.rollaxis(result, axis)
if out is not None:
out[...] = result
return result
def _fix_alpha_channel(self):
# This is a fix for a bug where the Alpha channel was dropped.
colors3to4 = [(c[:3], c[3]) for c in self.names.keys()]
colors3to4 = dict(colors3to4)
assert(len(colors3to4) == len(self.names)) # Dropped alpha channel causes colors to collide :(
for lbl in self.labels:
if lbl is None:
continue # No label file created yet.
img = Image.open(lbl)
size = img.size
img = np.array(img)
if img.shape[2] == 4:
continue # Image has alpha channel, good.
elif img.shape[2] == 3:
# Lookup each (partial) color and find what its alpha should be.
alpha = np.apply_along_axis(lambda c: colors3to4[tuple(c)], 2, img)
data = np.dstack([img, np.array(alpha, dtype=np.uint8)])
new_img = Image.frombuffer("RGBA", size, data, "raw", "RGBA", 0, 1)
new_img.save(lbl)
print("FIXED", lbl)
def plot_cost_to_go_mountain_car(env, estimator, num_tiles=20):
x = np.linspace(env.observation_space.low[0], env.observation_space.high[0], num=num_tiles)
y = np.linspace(env.observation_space.low[1], env.observation_space.high[1], num=num_tiles)
X, Y = np.meshgrid(x, y)
Z = np.apply_along_axis(lambda _: -np.max(estimator.predict(_)), 2, np.dstack([X, Y]))
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap=matplotlib.cm.coolwarm, vmin=-1.0, vmax=1.0)
ax.set_xlabel('Position')
ax.set_ylabel('Velocity')
ax.set_zlabel('Value')
ax.set_title("Mountain \"Cost To Go\" Function")
fig.colorbar(surf)
plt.show()
def boundary_tree_to_image(boundary_tree, size, image_mesh):
arr = array('B')
np.apply_along_axis(lambda c: arr.extend(boundary_tree.query(c)), 1, image_mesh)
return Image.frombytes("RGB", size, arr)
def __query_by_committee(self, clf, X_unlabeled):
num_classes = len(clf[0].classes_)
C = len(clf)
preds = []
if self.strategy == 'vote_entropy':
for model in clf:
y_out = map(int, model.predict(X_unlabeled))
preds.append(np.eye(num_classes)[y_out])
votes = np.apply_along_axis(np.sum, 0, np.stack(preds)) / C
return np.apply_along_axis(entropy, 1, votes)
elif self.strategy == 'average_kl_divergence':
for model in clf:
preds.append(model.predict_proba(X_unlabeled))
consensus = np.mean(np.stack(preds), axis=0)
divergence = []
for y_out in preds:
divergence.append(entropy(consensus.T, y_out.T))
return np.apply_along_axis(np.mean, 0, np.stack(divergence))
def estimate_1hot_cost(X, is_categorical):
"""
Calculate the "memory expansion" after applying one-hot encoding.
:param X: array-like
The input data array
:param is_categorical: boolean array-like
Array of vector form that indicates
whether each features of X is categorical
:return: int
Calculated memory size in byte scale (expansion)
"""
n_columns = 0
count_labels_v = lambda v: np.sum(np.isfinite(np.unique(v))) - 1
n_labels = np.apply_along_axis(count_labels_v, 0, X)
n_columns += np.sum(n_labels[is_categorical])
estimated_memory = n_columns * X.shape[0] * X.dtype.itemsize
return estimated_memory
def load_dataset():
if(not os.path.exists("./dataset/training.csv")):
print("dataset does not exist")
raise Exception
#load dataset
labeled_image = pd.read_csv("./dataset/training.csv")
#preprocessing dataframe
image = np.array(labeled_image["Image"].values).reshape(-1,1)
image = np.apply_along_axis(lambda img: (img[0].split()),1,image)
image = image.astype(np.int32) #because train_img elements are string before preprocessing
image = image.reshape(-1,96*96) # data 96 * 96 size image
label = labeled_image.values[:,:-1]
label = label.astype(np.float32)
#nan value to mean value
col_mean = np.nanmean(label, axis=0)
indices = np.where(np.isnan(label))
label[indices] = np.take(col_mean, indices[1])
return image, label
def get_his_std_qi( data_pixel_qi, max_cts=None):
'''
YG. Dev 16, 2016
Calculate the photon histogram for one q by giving
Parameters:
data_pixel_qi: one-D array, for the photon counts
max_cts: for bin max, bin will be [0,1,2,..., max_cts]
Return:
bins
his
std
'''
if max_cts is None:
max_cts = np.max( data_pixel_qi ) +1
bins = np.arange(max_cts)
dqn, dqm = data_pixel_qi.shape
#get histogram here
H = np.apply_along_axis(np.bincount, 1, np.int_(data_pixel_qi), minlength= max_cts )/dqm
#do average for different frame
his = np.average( H, axis=0)
std = np.std( H, axis=0 )
#cal average photon counts
kmean= np.average(data_pixel_qi )
return bins, his, std, kmean
def _nanmedian(a, axis=None, out=None, overwrite_input=False):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanmedian for parameter usage
"""
if axis is None or a.ndim == 1:
part = a.ravel()
if out is None:
return _nanmedian1d(part, overwrite_input)
else:
out[...] = _nanmedian1d(part, overwrite_input)
return out
else:
# for small medians use sort + indexing which is still faster than
# apply_along_axis
if a.shape[axis] < 400:
return _nanmedian_small(a, axis, out, overwrite_input)
result = np.apply_along_axis(_nanmedian1d, axis, a, overwrite_input)
if out is not None:
out[...] = result
return result
def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
interpolation='linear', keepdims=False):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanpercentile for parameter usage
"""
if axis is None:
part = a.ravel()
result = _nanpercentile1d(part, q, overwrite_input, interpolation)
else:
result = np.apply_along_axis(_nanpercentile1d, axis, a, q,
overwrite_input, interpolation)
# apply_along_axis fills in collapsed axis with results.
# Move that axis to the beginning to match percentile's
# convention.
if q.ndim != 0:
result = np.rollaxis(result, axis)
if out is not None:
out[...] = result
return result
def punion(probs, axis=None):
"""Find the unions of given list of probabilities assuming indepdendence.
Args:
probs: Matrix-like probabilities to union.
axis: Axis along which union will be performed.
Returns:
Matrix of probability unions.
"""
def punion1d(probs):
"""Union for 1d array.
"""
finalp = 0.0
for p in probs:
finalp += p*(1.0-finalp)
return finalp
probs = np.asarray(probs)
if axis is None:
return punion1d(probs.reshape((-1,)))
else:
return np.apply_along_axis(func1d=punion1d, axis=axis, arr=probs)
def addFamily(X):
# Family size: index 8
newCol = np.array(X[:, 1] + X[:, 2], np.newaxis)
newCol = newCol.reshape((len(newCol), 1))
X = np.hstack( (X,newCol) )
# Family category: index 9
def determineFamilyCat(row):
# print('row shape = {}, cont = {}'.format(row.shape, row))
if row[8] == 1:
return 0 # singles
elif 2<=row[8]<=4:
return 1 # normal size
else:
return 2 # large size
newCol = np.apply_along_axis(determineFamilyCat, 1, X)
newCol = newCol.reshape((len(newCol), 1))
X = np.hstack((X,newCol))
return X
# Not used
def update_recover_maps(self):
max_distance = min(self.width // 2, self.height // 2, 15)
#self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
#self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
#self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
#self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
#self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
#self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = min(self.width // 2, self.height // 2, 15)
#self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
#self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
#self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
#self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
#self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
#self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = min(self.width // 2, self.height // 2, 15)
#self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
#self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
#self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
#self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
#self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
#self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = self.width // 2
self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = self.width // 2
self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = min(self.width // 2, self.height // 2, 15)
#self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
#self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
#self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
#self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
#self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
#self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = self.width // 2
self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = self.width // 2
self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = self.width // 2
self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = self.width // 2
self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = self.width // 2
self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def update_recover_maps(self):
max_distance = self.width // 2
self.recover_map = np.zeros((max_distance + 1, self.width, self.height))
self.recover_map[0] = np.divide(self.strength_map, self.production_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map = np.zeros((max_distance + 1, self.width, self.height))
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
new_str_map = np.copy(self.strength_map)
new_str_map[new_str_map == 0] = 2
#self.prod_over_str_map[0] = np.divide(self.production_map, self.strength_map_01) * (self.is_neutral_map - self.combat_zone_map)
self.prod_over_str_map[0] = np.divide(self.production_map, new_str_map) * (self.is_neutral_map - self.combat_zone_map)
self.recover_map[0] = 1 / np.maximum(self.prod_over_str_map[0], 0.01)
for distance in range(1, max_distance + 1):
self.prod_over_str_map[distance] = spread_n(self.prod_over_str_map[distance - 1], 1)
self.prod_over_str_map[distance][self.prod_over_str_map[distance-1] == 0] = 0
self.prod_over_str_map[distance] = self.prod_over_str_map[distance] / 5
self.recover_map[distance] = 1 / np.maximum(self.prod_over_str_map[distance], 0.01)
self.prod_over_str_max_map = np.apply_along_axis(np.max, 0, self.prod_over_str_map)
self.recover_max_map = 1 / np.maximum(self.prod_over_str_max_map, 0.01)
self.prod_over_str_avg_map = np.apply_along_axis(np.mean, 0, self.prod_over_str_map)
self.recover_avg_map = 1 / np.maximum(self.prod_over_str_avg_map, 0.01)
self.prod_over_str_wtd_map = (self.prod_over_str_max_map + self.prod_over_str_avg_map) / 2
self.recover_wtd_map = 1 / np.maximum(self.prod_over_str_wtd_map, 0.01)
def match_matrix(event: Event):
"""Returns a numpy participation matrix for the qualification matches in this event, used for calculating OPR.
Each row in the matrix corresponds to a single alliance in a match, meaning that there will be two rows (one for
red, one for blue) per match. Each column represents a single team, ordered by team number. If a team participated
on a certain alliance, the value at that row and column would be 1, otherwise, it would be 0. For example, an
event with teams 1-7 that featured a match that pitted teams 1, 3, and 5 against 2, 4, and 6 would have a match
matrix that looks like this (sans labels):
#1 #2 #3 #4 #5 #6 #7
qm1_red 1 0 1 0 1 0 0
qm1_blue 0 1 0 1 0 1 0
"""
match_list = []
for match in filter(lambda match: match['comp_level'] == 'qm', event.matches):
matchRow = []
for team in event.teams:
matchRow.append(1 if team['key'] in match['alliances']['red']['teams'] else 0)
match_list.append(matchRow)
matchRow = []
for team in event.teams:
matchRow.append(1 if team['key'] in match['alliances']['blue']['teams'] else 0)
match_list.append(matchRow)
mat = numpy.array(match_list)
sum_matches = numpy.sum(mat, axis=0)
avg_team_matches = sum(sum_matches) / float(len(sum_matches))
return mat[:, numpy.apply_along_axis(numpy.count_nonzero, 0, mat) > avg_team_matches - 2]
def cluster_words(words, service_name, size):
stopwords = ["GET", "POST", "total", "http-requests", service_name, "-", "_"]
cleaned_words = []
for word in words:
for stopword in stopwords:
word = word.replace(stopword, "")
cleaned_words.append(word)
def distance(coord):
i, j = coord
return 1 - jaro_distance(cleaned_words[i], cleaned_words[j])
indices = np.triu_indices(len(words), 1)
distances = np.apply_along_axis(distance, 0, indices)
return cluster_of_size(linkage(distances), size)
def permute_rows(seed, array):
"""
Shuffle each row in ``array`` based on permutations generated by ``seed``.
Parameters
----------
seed : int
Seed for numpy.RandomState
array : np.ndarray[ndim=2]
Array over which to apply permutations.
"""
rand = np.random.RandomState(seed)
return np.apply_along_axis(rand.permutation, 1, array)
def __detect_now(self,spike_waveforms,selectChan,current_page):
if selectChan+"_"+str(current_page) in self.windowsState:
use_shape0 = self.__pk0_roi0_pos(selectChan,current_page)
spk_in_line = np.apply_along_axis(self.__in_select_line,1,spike_waveforms,use_shape0[0],use_shape0[1])
use_shape1 = self.__pk0_roi1_pos(selectChan,current_page)
spk_in_line1 = np.apply_along_axis(self.__in_select_line,1,spike_waveforms,use_shape1[0],use_shape1[1])
detected_mask = spk_in_line & spk_in_line1
else:
detected_mask = np.ones(spike_waveforms.shape[0],dtype=bool)
return detected_mask
# check whether a spike's waveform is intersect with segment widget