我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.subtract()。
def profile_score(contour, binary): """ Calculate a score based on the "profile" of the target, basically how closely its geometry matches with the expected geometry of the goal :param contour: :param binary: :return: """ bounding = cv2.boundingRect(contour) pixels = np.zeros((binary.shape[0], binary.shape[1])) cv2.drawContours(pixels, [contour], -1, 255, -1) col_averages = np.mean(pixels, axis=0)[bounding[0]:bounding[0] + bounding[2]] row_averages = np.mean(pixels, axis=1)[bounding[1]:bounding[1] + bounding[3]] # normalize to between 0 and 1 col_averages *= 1.0 / col_averages.max() row_averages *= 1.0 / row_averages.max() col_diff = np.subtract(col_averages, col_profile(col_averages.shape[0], bounding[2])) row_diff = np.subtract(row_averages, row_profile(row_averages.shape[0], bounding[3])) # average difference should be close to 0 avg_diff = np.mean([np.mean(col_diff), np.mean(row_diff)]) return 100 - (avg_diff * 50)
def triplet_loss(anchor, positive, negative, alpha): """Calculate the triplet loss according to the FaceNet paper Args: anchor: the embeddings for the anchor images. positive: the embeddings for the positive images. negative: the embeddings for the negative images. Returns: the triplet loss according to the FaceNet paper as a float tensor. """ with tf.variable_scope('triplet_loss'): pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1) neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1) basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha) loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0) return loss
def predict(image,the_net): inputs = [] try: tmp_input = image tmp_input = cv2.resize(tmp_input,(SIZE,SIZE)) tmp_input = tmp_input[11:11+128,11:11+128]; tmp_input = np.subtract(tmp_input,mean) tmp_input = tmp_input.transpose((2, 0, 1)) tmp_input = np.require(tmp_input, dtype=np.float32) except Exception as e: raise Exception("Image damaged or illegal file format") return the_net.blobs['data'].reshape(1, *tmp_input.shape) the_net.reshape() the_net.blobs['data'].data[...] = tmp_input the_net.forward() scores = the_net.blobs['prob'].data[0] return copy.deepcopy(scores)
def predict(the_net,image): inputs = [] if not os.path.exists(image): raise Exception("Image path not exist") return try: tmp_input = cv2.imread(image) tmp_input = cv2.resize(tmp_input,(SIZE,SIZE)) tmp_input = tmp_input[11:11+128,11:11+128] tmp_input = np.subtract(tmp_input,mean) tmp_input = tmp_input.transpose((2, 0, 1)) tmp_input = np.require(tmp_input, dtype=np.float32) except Exception as e: #raise Exception("Image damaged or illegal file format") return None the_net.blobs['data'].reshape(1, *tmp_input.shape) the_net.reshape() the_net.blobs['data'].data[...] = tmp_input the_net.forward() scores = copy.deepcopy(the_net.blobs['feature'].data) return scores
def predict(image,the_net): inputs = [] try: tmp_input = image tmp_input = cv2.resize(tmp_input,(SIZE,SIZE)) tmp_input = tmp_input[13:13+224,13:13+224]; tmp_input = np.subtract(tmp_input,mean) tmp_input = tmp_input.transpose((2, 0, 1)) tmp_input = np.require(tmp_input, dtype=np.float32) except Exception as e: raise Exception("Image damaged or illegal file format") return the_net.blobs['data'].reshape(1, *tmp_input.shape) the_net.reshape() the_net.blobs['data'].data[...] = tmp_input the_net.forward() scores = the_net.blobs['prob'].data[0] return copy.deepcopy(scores)
def predict(the_net,image): inputs = [] if not os.path.exists(image): raise Exception("Image path not exist") return try: tmp_input = cv2.imread(image) tmp_input = cv2.resize(tmp_input,(SIZE,SIZE)) tmp_input = tmp_input[13:13+224,13:13+224] #tmp_input = np.subtract(tmp_input,mean) tmp_input = tmp_input.transpose((2, 0, 1)) tmp_input = np.require(tmp_input, dtype=np.float32) except Exception as e: #raise Exception("Image damaged or illegal file format") return None the_net.blobs['data'].reshape(1, *tmp_input.shape) the_net.reshape() the_net.blobs['data'].data[...] = tmp_input the_net.forward() scores = copy.deepcopy(the_net.blobs['fc6'].data) return scores
def predict(the_net,image): inputs = [] if not os.path.exists(image): raise Exception("Image path not exist") return try: tmp_input = cv2.imread(image) tmp_input = cv2.resize(tmp_input,(SIZE,SIZE)) tmp_input = tmp_input[13:13+224,13:13+224] tmp_input = np.subtract(tmp_input,mean) tmp_input = tmp_input.transpose((2, 0, 1)) tmp_input = np.require(tmp_input, dtype=np.float32) except Exception as e: #raise Exception("Image damaged or illegal file format") return None the_net.blobs['data'].reshape(1, *tmp_input.shape) the_net.reshape() the_net.blobs['data'].data[...] = tmp_input the_net.forward() scores = copy.deepcopy(the_net.blobs['fc6'].data) return scores
def rotate_ascii_stl(self, rotation_matrix, content, filename): """Rotate the mesh array and save as ASCII STL.""" 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)) tweaked = list("solid %s" % filename) tweaked += list(map(self.write_facett, list(rotated_content))) tweaked.append("\nendsolid %s\n" % filename) tweaked = "".join(tweaked) return tweaked
def load_augment(fname, w, h, aug_params=no_augmentation_params, transform=None, sigma=0.0, color_vec=None): """Load augmented image with output shape (w, h). Default arguments return non augmented image of shape (w, h). To apply a fixed transform (color augmentation) specify transform (color_vec). To generate a random augmentation specify aug_params and sigma. """ img = load_image(fname) img = perturb(img, augmentation_params=aug_params, target_shape=(w, h)) #if transform is None: # img = perturb(img, augmentation_params=aug_params, target_shape=(w, h)) #else: # img = perturb_fixed(img, tform_augment=transform, target_shape=(w, h)) #randString = str(np.random.normal(0,1,1)) #im = Image.fromarray(img.transpose(1,2,0).astype('uint8')) #figName = fname.split("/")[-1] #im.save("imgs/"+figName+randString+".jpg") np.subtract(img, MEAN[:, np.newaxis, np.newaxis], out=img) #np.divide(img, STD[:, np.newaxis, np.newaxis], out=img) #img = augment_color(img, sigma=sigma, color_vec=color_vec) return img
def test_NotImplemented_not_returned(self): # See gh-5964 and gh-2091. Some of these functions are not operator # related and were fixed for other reasons in the past. binary_funcs = [ np.power, np.add, np.subtract, np.multiply, np.divide, np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or, np.bitwise_xor, np.left_shift, np.right_shift, np.fmax, np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2, np.logical_and, np.logical_or, np.logical_xor, np.maximum, np.minimum, np.mod ] # These functions still return NotImplemented. Will be fixed in # future. # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal] a = np.array('1') b = 1 for f in binary_funcs: assert_raises(TypeError, f, a, b)
def k_nearest_approx(self, vec, k): """Get the k nearest neighbors of a vector (in terms of cosine similarity). :param (np.array) vec: query vector :param (int) k: number of top neighbors to return :return (list[tuple[str, float]]): a list of (word, cosine similarity) pairs, in descending order """ if not hasattr(self, 'lshf'): self.lshf = self._init_lsh_forest() # TODO(kelvin): make this inner product score, to be consistent with k_nearest distances, neighbors = self.lshf.kneighbors(vec, n_neighbors=k, return_distance=True) scores = np.subtract(1, distances) nbr_score_pairs = self.score_map(np.squeeze(neighbors), np.squeeze(scores)) return sorted(nbr_score_pairs.items(), key=lambda x: x[1], reverse=True)
def gradient_descent(initial_point, gradient, cost_function, alpha=0.001, max_it=100): current_point = initial_point done = False costs = [cost_function(current_point)] it = 0 while not done: initial_point = current_point #print "Cost = "+str(cost_function(current_point)) delta = gradient(initial_point) #print "Delta: "+str(delta) #print "Alpha x Delta: "+str(np.dot(delta, alpha)) current_point = np.subtract(initial_point, np.dot(delta, alpha)) #print "Current Point:"+str(current_point) costs.append(cost_function(current_point)) if it > max_it: done = True it += 1 print it return current_point, costs
def is_error_still_ok(df1, df2, decimals=2): """Checks to see if the statistics are still within the acceptable bounds Args: df1 (pd.DataFrame): The original data set df2 (pd.DataFrame): The test data set decimals (int): The number of decimals of precision to check Returns: bool: ``True`` if the maximum error is acceptable, ``False`` otherwise """ r1 = get_values(df1) r2 = get_values(df2) # check each of the error values to check if they are the same to the # correct number of decimals r1 = [math.floor(r * 10**decimals) for r in r1] r2 = [math.floor(r * 10**decimals) for r in r2] # we are good if r1 and r2 have the same numbers er = np.subtract(r1, r2) er = [abs(n) for n in er] return np.max(er) == 0
def get_vol(simplex): # Compute the volume via the Cayley-Menger determinant # <http://mathworld.wolfram.com/Cayley-MengerDeterminant.html>. One # advantage is that it can compute the volume of the simplex indenpendent # of the dimension of the space where it's embedded. # compute all edge lengths edges = numpy.subtract(simplex[:, None], simplex[None, :]) ei_dot_ej = numpy.einsum('...k,...k->...', edges, edges) j = simplex.shape[0] - 1 a = numpy.empty((j+2, j+2) + ei_dot_ej.shape[2:]) a[1:, 1:] = ei_dot_ej a[0, 1:] = 1.0 a[1:, 0] = 1.0 a[0, 0] = 0.0 a = numpy.moveaxis(a, (0, 1), (-2, -1)) det = numpy.linalg.det(a) vol = numpy.sqrt((-1.0)**(j+1) / 2**j / math.factorial(j)**2 * det) return vol
def load_augment(fname, w, h, aug_params=no_augmentation_params, transform=None, sigma=0.0, color_vec=None): """Load augmented image with output shape (w, h). Default arguments return non augmented image of shape (w, h). To apply a fixed transform (color augmentation) specify transform (color_vec). To generate a random augmentation specify aug_params and sigma. """ img = load_image(fname) if transform is None: img = perturb(img, augmentation_params=aug_params, target_shape=(w, h)) else: img = perturb_fixed(img, tform_augment=transform, target_shape=(w, h)) np.subtract(img, MEAN[:, np.newaxis, np.newaxis], out=img) np.divide(img, STD[:, np.newaxis, np.newaxis], out=img) img = augment_color(img, sigma=sigma, color_vec=color_vec) return img
def standardize_data(data): """ Standardize the training data X = (X - mu) / (sigma) Parameters: ----------- data: numpy ndarray, where rows are data points and cols are dimensions Returns: -------- X_std (numpy array) \n col_mean (numpy array) \n col_std (numpy array) """ col_mean = np.mean(data, axis=0) col_std = np.std(data, axis=0) sdata = np.subtract(data, col_mean) / col_std return sdata, col_mean, col_std
def __init__(self, dynamics, lqr, constraints, horizon, dt=0.05, FPR=0, error_tol=0.05, erf=np.subtract, min_time=0.5, max_time=1, max_nodes=1E5, goal0=None, sys_time=time.time, printing=True): self.set_system(dynamics, lqr, constraints, erf) self.set_resolution(horizon, dt, FPR, error_tol) self.set_runtime(min_time, max_time, max_nodes, sys_time) self.set_goal(goal0) self.printing = printing self.killed = False #################################################
def convert_to_units(self, units): """ Convert the array and units to the given units. Parameters ---------- units : Unit object or str The units you want to convert to. """ new_units = _unit_repr_check_same(self.units, units) (conversion_factor, offset) = self.units.get_conversion_factor(new_units) self.units = new_units values = self.d values *= conversion_factor if offset: np.subtract(self, offset*self.uq, self) return self
def get_composition_distance(self, comp_vect1, comp_vect2): """ Returns the normalized distance between two composition vectors, which is defined as the L1 norm of their difference, divided by 2 to normalize (so the maximum distance is 1 and the minimum distance is 0). Args: comp_vect1: the first composition vector, as a numpy array comp_vect2: the second composition vector, as a numpy array """ # compute the different between the two composition vectors diff_vector = np.subtract(comp_vect1, comp_vect2) # compute the L1 norm of the difference vector return 0.5*np.linalg.norm(diff_vector, ord=1)
def _get_boll(cls, df): """ Get Bollinger bands. boll_ub means the upper band of the Bollinger bands boll_lb means the lower band of the Bollinger bands boll_ub = MA + K? boll_lb = MA ? K? M = BOLL_PERIOD K = BOLL_STD_TIMES :param df: data :return: None """ moving_avg = df['close_{}_sma'.format(cls.BOLL_PERIOD)] moving_std = df['close_{}_mstd'.format(cls.BOLL_PERIOD)] df['boll'] = moving_avg moving_avg = list(map(np.float64, moving_avg)) moving_std = list(map(np.float64, moving_std)) # noinspection PyTypeChecker df['boll_ub'] = np.add(moving_avg, np.multiply(cls.BOLL_STD_TIMES, moving_std)) # noinspection PyTypeChecker df['boll_lb'] = np.subtract(moving_avg, np.multiply(cls.BOLL_STD_TIMES, moving_std))
def load_sky_subtraction_to_process_stack(self): """ Load sky subtraction into image processing stack If sky image is 3-d ([n,x,y]), then collapse to 2-d ([x,y]) by doing a median on axis=0 """ self.unload_sky_subtraction_from_process_stack() if self.sky_hdulist is not None: if self.sky_hdulist[0].data.ndim == 2: process_fxn = ImageProcessAction(np.subtract, self.sky_hdulist[0].data) elif self.sky_hdulist[0].data.ndim == 3: process_fxn = ImageProcessAction(np.subtract, np.median(self.sky_hdulist[0].data, axis=0)) else: raise UnrecognizedNumberOfDimensions("Tried to load sky image with {} dimensions, " + "when can only handle 2-d or 3-d".format( self.sky_hdulist[0].data.ndim)) # assume that sky subtraction should always be first in processing stack. self.ztv_frame.image_process_functions_to_apply.insert(0, ('sky-subtraction', process_fxn)) wx.CallAfter(pub.sendMessage, 'image-process-functions-to-apply-changed', msg=(self.ztv_frame._pause_redraw_image,)) wx.CallAfter(pub.sendMessage, 'set-window-title', msg=None) self.sky_checkbox.SetValue(True)
def apply_update(self, weights, gradient): """Update the running averages of gradients and weight updates, and compute the Adadelta update for this step.""" if self.running_g2 is None: self.running_g2 = [ np.zeros_like(g) for g in gradient ] if self.running_dx2 is None: self.running_dx2 = [ np.zeros_like(g) for g in gradient ] self.running_g2 = self.running_average_square( self.running_g2, gradient ) new_weights = [] updates = [] for w, g, g2, dx2 in zip(weights, gradient, self.running_g2, self.running_dx2): update = np.multiply( np.divide( self.sqrt_plus_epsilon(dx2), self.sqrt_plus_epsilon(g2) ), g ) new_weights.append( np.subtract( w, update ) ) updates.append(update) self.running_dx2 = self.running_average_square( self.running_dx2, updates ) return new_weights
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)
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 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)