def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc. Needs additional handling as
PeriodIndex stores internal data as int dtype
Replace this to __numpy_ufunc__ in future version
"""
if isinstance(context, tuple) and len(context) > 0:
func = context[0]
if (func is np.add):
return self._add_delta(context[1][1])
elif (func is np.subtract):
return self._add_delta(-context[1][1])
elif isinstance(func, np.ufunc):
if 'M->M' not in func.types:
msg = "ufunc '{0}' not supported for the PeriodIndex"
# This should be TypeError, but TypeError cannot be raised
# from here because numpy catches.
raise ValueError(msg.format(func.__name__))
if com.is_bool_dtype(result):
return result
return PeriodIndex(result, freq=self.freq, name=self.name)
python类subtract()的实例源码
period.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def prewhiten(x):
mean = np.mean(x)
std = np.std(x)
std_adj = np.maximum(std, 1.0/np.sqrt(x.size))
y = np.multiply(np.subtract(x, mean), 1/std_adj)
return y
def calculate_roc(thresholds, embeddings1, embeddings2, actual_issame, nrof_folds=10):
assert(embeddings1.shape[0] == embeddings2.shape[0])
assert(embeddings1.shape[1] == embeddings2.shape[1])
nrof_pairs = min(len(actual_issame), embeddings1.shape[0])
nrof_thresholds = len(thresholds)
k_fold = KFold(n_splits=nrof_folds, shuffle=False)
tprs = np.zeros((nrof_folds,nrof_thresholds))
fprs = np.zeros((nrof_folds,nrof_thresholds))
accuracy = np.zeros((nrof_folds))
diff = np.subtract(embeddings1, embeddings2)
dist = np.sum(np.square(diff),1)
indices = np.arange(nrof_pairs)
for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)):
# Find the best threshold for the fold
acc_train = np.zeros((nrof_thresholds))
for threshold_idx, threshold in enumerate(thresholds):
_, _, acc_train[threshold_idx] = calculate_accuracy(threshold, dist[train_set], actual_issame[train_set])
best_threshold_index = np.argmax(acc_train)
for threshold_idx, threshold in enumerate(thresholds):
tprs[fold_idx,threshold_idx], fprs[fold_idx,threshold_idx], _ = calculate_accuracy(threshold, dist[test_set], actual_issame[test_set])
_, _, accuracy[fold_idx] = calculate_accuracy(thresholds[best_threshold_index], dist[test_set], actual_issame[test_set])
tpr = np.mean(tprs,0)
fpr = np.mean(fprs,0)
return tpr, fpr, accuracy
def calculate_val(thresholds, embeddings1, embeddings2, actual_issame, far_target, nrof_folds=10):
assert(embeddings1.shape[0] == embeddings2.shape[0])
assert(embeddings1.shape[1] == embeddings2.shape[1])
nrof_pairs = min(len(actual_issame), embeddings1.shape[0])
nrof_thresholds = len(thresholds)
k_fold = KFold(n_splits=nrof_folds, shuffle=False)
val = np.zeros(nrof_folds)
far = np.zeros(nrof_folds)
diff = np.subtract(embeddings1, embeddings2)
dist = np.sum(np.square(diff),1)
indices = np.arange(nrof_pairs)
for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)):
# Find the threshold that gives FAR = far_target
far_train = np.zeros(nrof_thresholds)
for threshold_idx, threshold in enumerate(thresholds):
_, far_train[threshold_idx] = calculate_val_far(threshold, dist[train_set], actual_issame[train_set])
if np.max(far_train)>=far_target:
f = interpolate.interp1d(far_train, thresholds, kind='slinear')
threshold = f(far_target)
else:
threshold = 0.0
val[fold_idx], far[fold_idx] = calculate_val_far(threshold, dist[test_set], actual_issame[test_set])
val_mean = np.mean(val)
far_mean = np.mean(far)
val_std = np.std(val)
return val_mean, val_std, far_mean
def cancel_offer(top_left_corner, bottom_right_corner, runescape_window):
loc_of_ge_point = pointfrombox.random_point(top_left_corner, bottom_right_corner)
realmouse.move_mouse_to(loc_of_ge_point[0], loc_of_ge_point[1])
pyautogui.click()
# print('now waiting and then trying to locate x button')
# wait_for('Tools/screenshots/abort_x_button.png', runescape_window)
time.sleep(3+random.random())
fail_count = 0
# print('We are about to cancel an offer that we believe to be in the window with coords {}, we are at line 497'.format(runescape_window.bottom_right_corner))
while True:
cancel_loc = pyautogui.locateOnScreen('Tools/screenshots/abort_x_button.png', region=(runescape_window.top_left_corner[0], runescape_window.top_left_corner[1], runescape_window.bottom_right_corner[0]-runescape_window.top_left_corner[0], runescape_window.bottom_right_corner[1]-runescape_window.top_left_corner[1]))
if fail_count > 3:
print('failed 3 times so trying to locate the abort button from the inventory collect button')
to_inv_loc = pyautogui.locateOnScreen('Tools/screenshots/to_inv_button.png', region=(runescape_window.top_left_corner[0], runescape_window.top_left_corner[1], runescape_window.bottom_right_corner[0]-runescape_window.top_left_corner[0], runescape_window.bottom_right_corner[1]-runescape_window.top_left_corner[1]))
cancel_loc = tuple(numpy.subtract(to_inv_loc, (163, 107, 163, 8)))
if cancel_loc == None:
print('Failed to locate the abort button {} times, trying again in 5 seconds'.format(fail_count))
time.sleep(5)
fail_count += 1
else:
break
# print('we have saved the location of the x button ready to click')
# print(cancel_loc)
point_of_cancel_button = pointfrombox.random_point((cancel_loc[0], cancel_loc[1]), (cancel_loc[0]+cancel_loc[2], cancel_loc[1]+cancel_loc[3]))
realmouse.move_mouse_to(point_of_cancel_button[0], point_of_cancel_button[1])
pyautogui.click()
def isub(arrays, axis = -1, dtype = None):
"""
Subtract elements in a reduction fashion. Equivalent to ``numpy.subtract.reduce`` on a dense array.
Parameters
----------
arrays : iterable
Arrays to be multiplied.
axis : int, optional
Reduction axis. Since subtraction is not reorderable (unlike a sum, for example),
`axis` must be specified as an int; full reduction (``axis = None``) will raise an exception.
Default is to subtract the arrays in the stream as if they had been stacked along a new axis,
then subtract along this new axis. If None, arrays are flattened before subtraction.
If `axis` is an int larger that the number of dimensions in the arrays of the stream,
arrays are subtracted along the new axis.
dtype : numpy.dtype, optional
The type of the yielded array and of the accumulator in which the elements
are combined. The dtype of a is used by default unless a has an integer dtype
of less precision than the default platform integer. In that case, if a is
signed then the platform integer is used while if a is unsigned then an
unsigned integer of the same precision as the platform integer is used.
Yields
------
online_sub : ndarray
Raises
------
ValueError
If `axis` is None. Since subtraction is not reorderable (unlike a sum, for example),
`axis` must be specified as an int.
"""
if axis is None:
raise ValueError('Subtraction is not a reorderable operation, and \
therefore a specific axis must be give.')
yield from ireduce_ufunc(arrays, ufunc = np.subtract, axis = axis, dtype = dtype)
def default_dist_funcs(dist_funcs, feature_example):
"""
Fills in default distance metrics for fingerprint analyses
"""
for key in feature_example:
if key in dist_funcs:
pass
elif type(feature_example[key]) is str:
dist_funcs[key] = lambda a, b: int(a!=b)
elif isinstance(feature_example[key], (int, long, float)) or all([isinstance(i, (int, long, float)) for i in feature_example[key]]):
dist_funcs[key] = lambda a, b: np.linalg.norm(np.subtract(a,b))
return dist_funcs
def default_dist_funcs(dist_funcs, feature_example):
"""
Fills in default distance metrics for fingerprint analyses
"""
for key in feature_example:
if key in dist_funcs:
pass
elif type(feature_example[key]) is str:
dist_funcs[key] = lambda a, b: int(a!=b)
elif isinstance(feature_example[key], (int, long, float)) or all([isinstance(i, (int, long, float)) for i in feature_example[key]]):
dist_funcs[key] = lambda a, b: np.linalg.norm(np.subtract(a,b))
return dist_funcs
def MeanSquareError(self,y, y_hat):
'''Calculate the mean square error between the real value (y) and the predicted value (y_hat).'''
'''Return MSE = 1/N \sum^N_{i=1} (y-y_hat)^2.'''
dif = np.subtract(y, y_hat)
sum = np.mean(np.power(dif, 2), axis = 1)
mse = np.mean(sum)
return mse
def crossGenotypeWindows(commonSNPsCHR, commonSNPsPOS, snpsP1, snpsP2, inFile, binLen, outFile, logDebug = True):
## inFile are the SNPs of the sample
(snpCHR, snpPOS, snpGT, snpWEI, DPmean) = snpmatch.parseInput(inFile = inFile, logDebug = logDebug)
# identifying the segregating SNPs between the accessions
# only selecting 0 or 1
segSNPsind = np.where((snpsP1 != snpsP2) & (snpsP1 >= 0) & (snpsP2 >= 0) & (snpsP1 < 2) & (snpsP2 < 2))[0]
log.info("number of segregating snps between parents: %s", len(segSNPsind))
(ChrBins, PosBins) = getBinsSNPs(commonSNPsCHR, commonSNPsPOS, binLen)
log.info("number of bins: %s", len(ChrBins))
outfile = open(outFile, 'w')
for i in range(len(PosBins)):
start = np.sum(PosBins[0:i])
end = start + PosBins[i]
# first snp positions which are segregating and are in this window
reqPOSind = segSNPsind[np.where((segSNPsind < end) & (segSNPsind >= start))[0]]
reqPOS = commonSNPsPOS[reqPOSind]
perchrTarPosind = np.where(snpCHR == ChrBins[i])[0]
perchrTarPos = snpPOS[perchrTarPosind]
matchedAccInd = reqPOSind[np.where(np.in1d(reqPOS, perchrTarPos))[0]]
matchedTarInd = perchrTarPosind[np.where(np.in1d(perchrTarPos, reqPOS))[0]]
matchedTarGTs = snpGT[matchedTarInd]
try:
TarGTBinary = snpmatch.parseGT(matchedTarGTs)
TarGTBinary[np.where(TarGTBinary == 2)[0]] = 4
genP1 = np.subtract(TarGTBinary, snpsP1[matchedAccInd])
genP1no = len(np.where(genP1 == 0)[0])
(geno, pval) = getWindowGenotype(genP1no, len(genP1))
outfile.write("%s\t%s\t%s\t%s\t%s\n" % (i+1, genP1no, len(genP1), geno, pval))
except:
outfile.write("%s\tNA\tNA\tNA\tNA\n" % (i+1))
if i % 40 == 0:
log.info("progress: %s windows", i+10)
log.info("done!")
outfile.close()
def crop_and_concat_layer(inputs, axis=-1):
'''
Layer for cropping and stacking feature maps of different size along a different axis.
Currently, the first feature map in the inputs list defines the output size.
The feature maps can have different numbers of channels.
:param inputs: A list of input tensors of the same dimensionality but can have different sizes
:param axis: Axis along which to concatentate the inputs
:return: The concatentated feature map tensor
'''
output_size = inputs[0].get_shape().as_list()
concat_inputs = [inputs[0]]
for ii in range(1,len(inputs)):
larger_size = inputs[ii].get_shape().as_list()
start_crop = np.subtract(larger_size, output_size) // 2
if len(output_size) == 5: # 3D images
cropped_tensor = inputs[ii][:,
start_crop[1]:start_crop[1] + output_size[1],
start_crop[2]:start_crop[2] + output_size[2],
start_crop[3]:start_crop[3] + output_size[3],...]
elif len(output_size) == 4: # 2D images
cropped_tensor = inputs[ii][:,
start_crop[1]:start_crop[1] + output_size[1],
start_crop[2]:start_crop[2] + output_size[2], ...]
else:
raise ValueError('Unexpected number of dimensions on tensor: %d' % len(output_size))
concat_inputs.append(cropped_tensor)
return tf.concat(concat_inputs, axis=axis)
def pad_to_size(bottom, output_size):
'''
A layer used to pad the tensor bottom to output_size by padding zeros around it
TODO: implement for 3D data
'''
input_size = bottom.get_shape().as_list()
size_diff = np.subtract(output_size, input_size)
pad_size = size_diff // 2
odd_bit = np.mod(size_diff, 2)
if len(input_size) == 4:
padded = tf.pad(bottom, paddings=[[0,0],
[pad_size[1], pad_size[1] + odd_bit[1]],
[pad_size[2], pad_size[2] + odd_bit[2]],
[0,0]])
return padded
elif len(input_size) == 5:
raise NotImplementedError('This layer has not yet been extended to 3D')
else:
raise ValueError('Unexpected input size: %d' % input_size)
def sensor2midi(self):
# Read initial state until it's not empty
initialState = []
while initialState == []:
initialState = self.sensor.getAllImages()
sleep(0.2)
# Observe touch events
while True:
sleep(0.01)
state = self.sensor.getAllImages()
if state:
# Diff the initial state with the new state
diff = np.subtract(initialState[-1]['image'], state[-1]['image'])
self.activeRegions = np.unique(np.where(diff > 100)[1] // self.modulo)
def favour_side(self, mesh, favside):
"""This function weights the size of orientations closer than 45 deg
to a favoured side higher.
Args:
mesh (np.array): with format face_count x 6 x 3.
favside (string): the favoured side "[[0,-1,2.5],3]"
Returns:
a weighted mesh or the original mesh in case of invalid input
"""
if isinstance(favside, str):
try:
restring = r"(-?\d*\.{0,1}\d+)[, []]*(-?\d*\.{0,1}\d+)[, []]*(-?\d*\.{0,1}\d+)\D*(-?\d*\.{0,1}\d+)"
x = float(re.search(restring, favside).group(1))
y = float(re.search(restring, favside).group(2))
z = float(re.search(restring, favside).group(3))
f = float(re.search(restring, favside).group(4))
except AttributeError:
raise AttributeError("Could not parse input: favored side")
else:
raise AttributeError("Could not parse input: favored side")
norm = np.sqrt(np.sum(np.array([x, y, z])**2))
side = np.array([x, y, z])/norm
print("You favour the side {} with a factor of {}".format(
side, f))
diff = np.subtract(mesh[:, 0, :], side)
align = np.sum(diff*diff, axis=1) < 0.7654
mesh_not_align = mesh[np.logical_not(align)]
mesh_align = mesh[align]
mesh_align[:, 5, 0] = f * mesh_align[:, 5, 0] # weight aligning orientations
mesh = np.concatenate((mesh_not_align, mesh_align), axis=0)
return mesh
def rotate_bin_stl(self, rotation_matrix, content):
"""Rotate the object and save as binary STL. This module is currently replaced
by the ascii version. If you want to use binary STL, please do the
following changes in Tweaker.py: Replace "rotatebinSTL" by "rotateSTL"
and set in the write sequence the open outfile option from "w" to "wb".
However, the ascii version is much faster in Python 3."""
mesh = np.array(content, dtype=np.float64)
# prefix area vector, if not already done (e.g. in STL format)
if len(mesh[0]) == 3:
row_number = int(len(content) / 3)
mesh = mesh.reshape(row_number, 3, 3)
# upgrade numpy with: "pip install numpy --upgrade"
rotated_content = np.matmul(mesh, rotation_matrix)
v0 = rotated_content[:, 0, :]
v1 = rotated_content[:, 1, :]
v2 = rotated_content[:, 2, :]
normals = np.cross(np.subtract(v1, v0), np.subtract(v2, v0)
).reshape(int(len(rotated_content)), 1, 3)
rotated_content = np.hstack((normals, rotated_content))
# header = "Tweaked on {}".format(time.strftime("%a %d %b %Y %H:%M:%S")
# ).encode().ljust(79, b" ") + b"\n"
# header = struct.pack("<I", int(len(content) / 3)) # list("solid %s" % filename)
tweaked_array = list(map(self.write_bin_facett, rotated_content))
# return header + b"".join(tweaked_array)
# return b"".join(tweaked_array)
return tweaked_array
def evaluate_model(self, x, w):
if not self.regularization or self.lambd == 0:
edge_weight = x.dot(w)
edge_weight = np.multiply(edge_weight, self.skipped)
else:
edge_weight = np.zeros((1, self.num_edges))
for idx, value in izip(x.indices, x.data):
# edge_weight = np.add(edge_weight, np.multiply(value, np.multiply(np.maximum(np.subtract(np.abs(w[idx, :]), self.lambd), 0), np.sign(w[idx, :]))))
for edge in xrange(self.num_edges):
if w[idx, edge] > self.lambd:
edge_weight[0, edge] += value * (w[idx, edge] - self.lambd)
elif w[idx, edge] < -self.lambd:
edge_weight[0, edge] += value * (w[idx, edge] + self.lambd)
return edge_weight
def test_casting_out_param(self):
# Test that it's possible to do casts on output
a = np.ones((200, 100), np.int64)
b = np.ones((200, 100), np.int64)
c = np.ones((200, 100), np.float64)
np.add(a, b, out=c)
assert_equal(c, 2)
a = np.zeros(65536)
b = np.zeros(65536, dtype=np.float32)
np.subtract(a, 0, out=b)
assert_equal(b, 0)
def test_where_param(self):
# Test that the where= ufunc parameter works with regular arrays
a = np.arange(7)
b = np.ones(7)
c = np.zeros(7)
np.add(a, b, out=c, where=(a % 2 == 1))
assert_equal(c, [0, 2, 0, 4, 0, 6, 0])
a = np.arange(4).reshape(2, 2) + 2
np.power(a, [2, 3], out=a, where=[[0, 1], [1, 0]])
assert_equal(a, [[2, 27], [16, 5]])
# Broadcasting the where= parameter
np.subtract(a, 2, out=a, where=[True, False])
assert_equal(a, [[0, 27], [14, 5]])
def _hist_bin_fd(x):
"""
The Freedman-Diaconis histogram bin estimator.
The Freedman-Diaconis rule uses interquartile range (IQR) to
estimate binwidth. It is considered a variation of the Scott rule
with more robustness as the IQR is less affected by outliers than
the standard deviation. However, the IQR depends on fewer points
than the standard deviation, so it is less accurate, especially for
long tailed distributions.
If the IQR is 0, this function returns 1 for the number of bins.
Binwidth is inversely proportional to the cube root of data size
(asymptotically optimal).
Parameters
----------
x : array_like
Input data that is to be histogrammed, trimmed to range. May not
be empty.
Returns
-------
h : An estimate of the optimal bin width for the given data.
"""
iqr = np.subtract(*np.percentile(x, [75, 25]))
return 2.0 * iqr * x.size ** (-1.0 / 3.0)
def test_testUfuncRegression(self):
# Tests new ufuncs on MaskedArrays.
for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate',
'sin', 'cos', 'tan',
'arcsin', 'arccos', 'arctan',
'sinh', 'cosh', 'tanh',
'arcsinh',
'arccosh',
'arctanh',
'absolute', 'fabs', 'negative',
'floor', 'ceil',
'logical_not',
'add', 'subtract', 'multiply',
'divide', 'true_divide', 'floor_divide',
'remainder', 'fmod', 'hypot', 'arctan2',
'equal', 'not_equal', 'less_equal', 'greater_equal',
'less', 'greater',
'logical_and', 'logical_or', 'logical_xor',
]:
try:
uf = getattr(umath, f)
except AttributeError:
uf = getattr(fromnumeric, f)
mf = getattr(numpy.ma.core, f)
args = self.d[:uf.nin]
ur = uf(*args)
mr = mf(*args)
assert_equal(ur.filled(0), mr.filled(0), f)
assert_mask_equal(ur.mask, mr.mask, err_msg=f)