The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.
Example 1
def _resolve_spec(im1, im2): im = im1.copy() img1 = cv.cvtColor(im1, cv.COLOR_BGR2GRAY) img2 = cv.cvtColor(im2, cv.COLOR_BGR2GRAY) # Best pixel selection criteria # 1. Pixel difference should be more than 20. (just an experimentally value. Free to change!) # 2. Best pixel should have less intensity # 3. pixel should not be pure black. (just an additional constraint # to remove black background created by warping) mask = np.logical_and((img1 - img2) > DIFF_THRESHOLD, img1 > img2) mask = np.logical_and(mask, img2 != 0) im[mask] = im2[mask] return im
Example 2
def action_label_counts(directory, data_loader, n_actions=18, n=None): episode_paths = frame.episode_paths(directory) label_counts = [0, 0] action_label_counts = [[0, 0] for i in range(n_actions)] if n is not None: np.random.shuffle(episode_paths) episode_paths = episode_paths[:n] for episode_path in tqdm.tqdm(episode_paths): try: features, labels = data_loader.load_features_and_labels([episode_path]) except: traceback.print_exc() else: for label in range(len(label_counts)): label_counts[label] += np.count_nonzero(labels == label) for action in range(n_actions): actions = np.reshape(np.array(features["action"]), [-1]) action_label_counts[action][label] += np.count_nonzero( np.logical_and(labels == label, actions == action)) return label_counts, action_label_counts
Example 3
def iall(arrays, axis = -1): """ Test whether all array elements along a given axis evaluate to True Parameters ---------- arrays : iterable Arrays to be reduced. axis : int or None, optional Axis along which a logical AND reduction is performed. The default is to perform a logical AND along the 'stream axis', as if all arrays in ``array`` were stacked along a new dimension. If ``axis = None``, arrays in ``arrays`` are flattened before reduction. Yields ------ all : ndarray, dtype bool """ yield from ireduce_ufunc(arrays, ufunc = np.logical_and, axis = axis)
Example 4
def _classify_gems(counts0, counts1): """ Infer number of distinct transcriptomes present in each GEM (1 or 2) and report cr_constants.GEM_CLASS_GENOME0 for a single cell w/ transcriptome 0, report cr_constants.GEM_CLASS_GENOME1 for a single cell w/ transcriptome 1, report cr_constants.GEM_CLASS_MULTIPLET for multiple transcriptomes """ # Assumes that most of the GEMs are single-cell; model counts independently thresh0, thresh1 = [cr_constants.DEFAULT_MULTIPLET_THRESHOLD] * 2 if sum(counts0 > counts1) >= 1 and sum(counts1 > counts0) >= 1: thresh0 = np.percentile(counts0[counts0 > counts1], cr_constants.MULTIPLET_PROB_THRESHOLD) thresh1 = np.percentile(counts1[counts1 > counts0], cr_constants.MULTIPLET_PROB_THRESHOLD) doublet = np.logical_and(counts0 >= thresh0, counts1 >= thresh1) dtype = np.dtype('|S%d' % max(len(cls) for cls in cr_constants.GEM_CLASSES)) result = np.where(doublet, cr_constants.GEM_CLASS_MULTIPLET, cr_constants.GEM_CLASS_GENOME0).astype(dtype) result[np.logical_and(np.logical_not(result == cr_constants.GEM_CLASS_MULTIPLET), counts1 > counts0)] = cr_constants.GEM_CLASS_GENOME1 return result
Example 5
def reg2bin_vector(begin, end): '''Vectorized tabix reg2bin -- much faster than reg2bin''' result = np.zeros(begin.shape) # Entries filled done = np.zeros(begin.shape, dtype=np.bool) for (bits, bins) in rev_bit_bins: begin_shift = begin >> bits new_done = (begin >> bits) == (end >> bits) mask = np.logical_and(new_done, np.logical_not(done)) offset = ((1 << (29 - bits)) - 1) / 7 result[mask] = offset + begin_shift[mask] done = new_done return result.astype(np.int32)
Example 6
def compute_test_accuracy(X_test, Y_test, model, prediction_type, cellgroup_map_array): prediction = model.predict(X_test) auc = [] if prediction_type=="cellgroup": prediction = np.dot(prediction, cellgroup_map_array) Y_test = np.dot(Y_test, cellgroup_map_array) mask = ~np.logical_or(Y_test.sum(1)==0, Y_test.sum(1)==Y_test.shape[1]) for y,pred in zip(Y_test.T,prediction.T): pos = np.logical_and(mask, y==1) neg = np.logical_and(mask, y==0) try: U = stats.mannwhitneyu(pred[pos], pred[neg])[0] auc.append(1.-U/(np.count_nonzero(pos)*np.count_nonzero(neg))) except ValueError: auc.append(0.5) return auc
Example 7
def ignore_data(self, xmin=None, xmax=None, unit=None): """ Ignore the data points within range [xmin, xmax]. If xmin is None, then xmin=min(xdata); if xmax is None, then xmax=max(xdata). if unit is None, then assume the same unit as `self.xunit'. """ if unit is None: unit = self.xunit if xmin is not None: xmin = self.convert_unit(xmin, unit=unit) else: xmin = np.min(self.xdata) if xmax is not None: xmax = self.convert_unit(xmax, unit=unit) else: xmax = np.max(self.xdata) ignore_idx = np.logical_and(self.xdata >= xmin, self.xdata <= xmax) self.mask[ignore_idx] = False # reset `f_residual' self.f_residual = None
Example 8
def notice_data(self, xmin=None, xmax=None, unit=None): """ Notice the data points within range [xmin, xmax]. If xmin is None, then xmin=min(xdata); if xmax is None, then xmax=max(xdata). if unit is None, then assume the same unit as `self.xunit'. """ if unit is None: unit = self.xunit if xmin is not None: xmin = self.convert_unit(xmin, unit=unit) else: xmin = np.min(self.xdata) if xmax is not None: xmax = self.convert_unit(xmax, unit=unit) else: xmax = np.max(self.xdata) notice_idx = np.logical_and(self.xdata >= xmin, self.xdata <= xmax) self.mask[notice_idx] = True # reset `f_residual' self.f_residual = None
Example 9
def overlap_ratio(boxes1, boxes2): # find intersection bbox x_int_bot = np.maximum(boxes1[:, 0], boxes2[0]) x_int_top = np.minimum(boxes1[:, 0] + boxes1[:, 2], boxes2[0] + boxes2[2]) y_int_bot = np.maximum(boxes1[:, 1], boxes2[1]) y_int_top = np.minimum(boxes1[:, 1] + boxes1[:, 3], boxes2[1] + boxes2[3]) # find intersection area dx = x_int_top - x_int_bot dy = y_int_top - y_int_bot area_int = np.where(np.logical_and(dx>0, dy>0), dx * dy, np.zeros_like(dx)) # find union area_union = boxes1[:,2] * boxes1[:,3] + boxes2[2] * boxes2[3] - area_int # find overlap ratio ratio = np.where(area_union > 0, area_int/area_union, np.zeros_like(area_int)) return ratio ########################################################################### # overlap_ratio of two bboxes # ###########################################################################
Example 10
def overlap_ratio_pair(boxes1, boxes2): # find intersection bbox x_int_bot = np.maximum(boxes1[:, 0], boxes2[:, 0]) x_int_top = np.minimum(boxes1[:, 0] + boxes1[:, 2], boxes2[:, 0] + boxes2[:, 2]) y_int_bot = np.maximum(boxes1[:, 1], boxes2[:, 1]) y_int_top = np.minimum(boxes1[:, 1] + boxes1[:, 3], boxes2[:, 1] + boxes2[:, 3]) # find intersection area dx = x_int_top - x_int_bot dy = y_int_top - y_int_bot area_int = np.where(np.logical_and(dx>0, dy>0), dx * dy, np.zeros_like(dx)) # find union area_union = boxes1[:,2] * boxes1[:,3] + boxes2[:, 2] * boxes2[:, 3] - area_int # find overlap ratio ratio = np.where(area_union > 0, area_int/area_union, np.zeros_like(area_int)) return ratio
Example 11
def _create_drop_path_choices(self): if not self._drop_path: # Drop path was turned off. return np.zeros(shape=[len(self._choices)], dtype='int32') elif np.random.uniform() < self._p_local_drop_path: # Local drop-path (make each choice independantly at random.) choices = np.random.uniform(size=[len(self._choices)]) drop_base = choices < self._p_drop_base_case drop_recursive = np.logical_and( choices < (self._p_drop_base_case + self._p_drop_recursive_case), np.logical_not(drop_base)) return (np.int32(drop_base)*self._JUST_RECURSE + np.int32(drop_recursive)*self._JUST_BASE) else: # Global (pick a single column.) column = np.random.randint(self._fractal_block_depth) return np.array( [self._JUST_RECURSE if len(binary_seq) < column else self._JUST_BASE for _, binary_seq in self._choices], dtype='int32')
Example 12
def __act_manual(self, state_meas): if len(self.__measure_for_manual): # [AMMO2, AMMO3, AMMO4, AMMO5, AMMO6, AMMO7, WEAPON2, # WEAPON3 WEAPON4 WEAPON5 WEAPON6 WEAPON7 SELECTED_WEAPON] assert len(self.__measure_for_manual) == 13 # [SELECT_WEAPON2 SELECT_WEAPON3 SELECT_WEAPON4 SELECT_WEAPON5 SELECT_WEAPON6 SELECT_WEAPON7] curr_action = np.zeros((state_meas.shape[0], self.__num_manual_controls), dtype=np.int) for ns in range(state_meas.shape[0]): curr_ammo = state_meas[ns, self.__measure_for_manual[:6]] curr_weapons = state_meas[ns, self.__measure_for_manual[6:12]] if self.verbose: print 'current ammo:', curr_ammo print 'current weapons:', curr_weapons available_weapons = np.logical_and(curr_ammo >= np.array([1, 2, 1, 1, 1, 40]), curr_weapons) if any(available_weapons): best_weapon = np.nonzero(available_weapons)[0][-1] if not state_meas[ns, self.__measure_for_manual[12]] == best_weapon + 2: curr_action[ns, best_weapon] = 1 return curr_action else: return []
Example 13
def anamVertexRegionSize(vor, L, typical=6): """ Given a set of centroids (voronoi generators), for each vertex in the tessellation, return 1 if the vertex is part of a region that is not of the 'typical' size """ # vertices v = vor.vertices sizeFlag = numpy.zeros(len(v)) # flag for anamolous region size # regSize = [[]]*len(v) # list of region sizes for reg in vor.regions: if len(reg) > 0 and -1 not in reg: # Non-empty and non-open region size = len(reg) if size != typical: for vert in reg: # update size of all vertices that are in the region # regSize[vert].append(size) sizeFlag[vert] = 1 # Choose all in box sizeFlag = sizeFlag[numpy.logical_and(*[numpy.logical_and(v[:, i] >= 0, v[:, i] <= L[i]) for i in range(2)])] sizeFlag = sizeFlag.astype('int') return sizeFlag
Example 14
def anamVertexRegionSize(vor, L, typical=6): """ Given a set of centroids (voronoi generators), for each vertex in the tessellation, return 1 if the vertex is part of a region that is not of the 'typical' size """ # vertices v = vor.vertices sizeFlag = numpy.zeros(len(v)) # flag for anamolous region size #regSize = [[]]*len(v) # list of region sizes for reg in vor.regions: if len(reg) > 0 and -1 not in reg: # Non-empty and non-open region size = len(reg) if size != typical: for vert in reg: # update size of all vertices that are in the region #regSize[vert].append(size) sizeFlag[vert] = 1 # Choose all in box sizeFlag = sizeFlag[ numpy.logical_and(* [ numpy.logical_and(v[:,i]>=0, v[:,i] <= L[i]) for i in range(2) ] )] sizeFlag = sizeFlag.astype('int') return sizeFlag
Example 15
def test_truth_table_logical(self): # 2, 3 and 4 serves as true values input1 = [0, 0, 3, 2] input2 = [0, 4, 0, 2] typecodes = (np.typecodes['AllFloat'] + np.typecodes['AllInteger'] + '?') # boolean for dtype in map(np.dtype, typecodes): arg1 = np.asarray(input1, dtype=dtype) arg2 = np.asarray(input2, dtype=dtype) # OR out = [False, True, True, True] for func in (np.logical_or, np.maximum): assert_equal(func(arg1, arg2).astype(bool), out) # AND out = [False, False, False, True] for func in (np.logical_and, np.minimum): assert_equal(func(arg1, arg2).astype(bool), out) # XOR out = [False, True, True, False] for func in (np.logical_xor, np.not_equal): assert_equal(func(arg1, arg2).astype(bool), out)
Example 16
def test_object_logical(self): a = np.array([3, None, True, False, "test", ""], dtype=object) assert_equal(np.logical_or(a, None), np.array([x or None for x in a], dtype=object)) assert_equal(np.logical_or(a, True), np.array([x or True for x in a], dtype=object)) assert_equal(np.logical_or(a, 12), np.array([x or 12 for x in a], dtype=object)) assert_equal(np.logical_or(a, "blah"), np.array([x or "blah" for x in a], dtype=object)) assert_equal(np.logical_and(a, None), np.array([x and None for x in a], dtype=object)) assert_equal(np.logical_and(a, True), np.array([x and True for x in a], dtype=object)) assert_equal(np.logical_and(a, 12), np.array([x and 12 for x in a], dtype=object)) assert_equal(np.logical_and(a, "blah"), np.array([x and "blah" for x in a], dtype=object)) assert_equal(np.logical_not(a), np.array([not x for x in a], dtype=object)) assert_equal(np.logical_or.reduce(a), 3) assert_equal(np.logical_and.reduce(a), None)
Example 17
def get_metrics(predictions, targets): assert(np.logical_or(predictions == 1, predictions == 0).all()) assert(np.logical_or(targets == 1, targets == 0).all()) TP = np.logical_and(predictions == 1, targets == 1).sum() FP = np.logical_and(predictions == 1, targets == 0).sum() FN = np.logical_and(predictions == 0, targets == 1).sum() TN = np.logical_and(predictions == 0, targets == 0).sum() N = TP + FP + FN + TN PPV = float(TP) / float(TP + FP) if TP != 0.0 else 0.0 FPV = float(TN) / float(TN + FN) if TN != 0.0 else 0.0 ACC = float(TP + TN) / float(N) TPR = float(TP) / float(TP + FN) if TP != 0.0 else 0.0 FPR = float(FP) / float(FP + TN) if FP != 0.0 else 0.0 tp, tn, fp, fn = float(TP) / N, float(TN) / N, float(FP) / N, float(FN) / N MCC = float(tp*tn - fp*fn) / (np.sqrt(tp+fp)*np.sqrt(tp+fn)*np.sqrt(tn+fp)*np.sqrt(tn+fn)) F1 = 2 * TP / float(2 * TP + FP + FN) metrics = { "TP": TP, "FP": FP, "FN": FN, "TN": TN, "N": N, "PPV": PPV, "FPV": FPV, "MCC": MCC, "ACC": ACC, "F1": F1 } return metrics
Example 18
def simOnePrd(self): ''' Simulate one period of the fashion victom model for this type. Each agent receives an idiosyncratic preference shock and chooses whether to change styles (using the optimal decision rule). Parameters ---------- none Returns ------- none ''' pNow = self.pNow sPrev = self.sNow J2Pprob = self.switchFuncJock(pNow) P2Jprob = self.switchFuncPunk(pNow) Shks = self.RNG.rand(self.pop_size) J2P = np.logical_and(sPrev == 0,Shks < J2Pprob) P2J = np.logical_and(sPrev == 1,Shks < P2Jprob) sNow = copy(sPrev) sNow[J2P] = 1 sNow[P2J] = 0 self.sNow = sNow
Example 19
def _shrink(v, gamma): """Soft-shrinkage of an array with parameter gamma. Parameters ---------- v : array Array containing the values to be applied to the shrinkage operator gamma : float Shrinkage parameter. Returns ------- v : array The same input array after the shrinkage operator was applied. """ pos = v > gamma neg = v < -gamma v[pos] -= gamma v[neg] += gamma v[np.logical_and(~pos, ~neg)] = .0 return v
Example 20
def __split_apply(self, func_y_lt_f, func_y_gt_f, func_f_lt_0, y, f): # Make sure all arrays are of a compatible type y, f = np.broadcast_arrays(y, f) y = y.astype(float, copy=False) f = f.astype(float, copy=False) if any(y < 0): raise ValueError("y has to be > 0!") # get indicators of which likelihoods to apply where y_lt_f = np.logical_and(y <= f, f > 0) y_gt_f = np.logical_and(y > f, f > 0) f_lt_0 = f <= 0 result = np.zeros_like(y) result[y_lt_f] = func_y_lt_f(y[y_lt_f], f[y_lt_f]) result[y_gt_f] = func_y_gt_f(y[y_gt_f], f[y_gt_f]) result[f_lt_0] = func_f_lt_0(y[f_lt_0], f[f_lt_0]) return result
Example 21
def _global_lonlat2pix(self, lonlat): x = np.searchsorted(self._coords_x, lonlat[:, 0], side='right') - 1 x = x.astype(int) ycoords = self._coords_y y = np.searchsorted(ycoords, lonlat[:, 1], side='right') - 1 y = y.astype(int) # We want the *closed* interval, which means moving # points on the end back by 1 on_end_x = lonlat[:, 0] == self._coords_x[-1] on_end_y = lonlat[:, 1] == self._coords_y[-1] x[on_end_x] -= 1 y[on_end_y] -= 1 if (not all(np.logical_and(x >= 0, x < self._full_res[0]))) or \ (not all(np.logical_and(y >= 0, y < self._full_res[1]))): raise ValueError("Queried location is not " "in the image {}!".format(self.source._filename)) result = np.concatenate((x[:, np.newaxis], y[:, np.newaxis]), axis=1) return result # @contract(lonlat='array[Nx2](float64),N>0')
Example 22
def load_switching_data(filename_or_fileobject, start_state=None, group="main", failure=False, threshold=None, voltage_scale_factor=1.0, duration_scale_factor=1.0, data_name='voltage', data_filter=None, display=False): data, desc = load_from_HDF5(filename_or_fileobject, reshape=False) # Regular axes states = desc[group].axis("state").points reps = desc[group].axis("attempt").points # Main data array, possibly unstructured dat = data[group][:].reshape((-1, reps.size, states.size)) # Filter data if desired # e.g. filter_func = lambda dat: np.logical_and(dat['field'] == 0.04, dat['temperature'] == 4.0) if data_filter: dat = dat[np.where(data_filter(dat))] Vs = dat[data_name] durs = dat['pulse_duration'][:,0,0] amps = dat['pulse_voltage'][:,0,0] points = np.array([durs, amps]).transpose() if failure: return points, reset_failure(Vs, start_state=start_state) else: return points, switching_phase(Vs, start_state=start_state, threshold=threshold, display=display)
Example 23
def _pfp(pha, amp, phabin, binsize): """Sub prefered phase function """ nbin, nt = len(phabin), pha.shape[1] ampbin = np.zeros((len(phabin), nt), dtype=float) # Binarize amplitude accros all trials : for t in range(nt): curpha, curamp = pha[:, t], amp[:, t] for i, p in enumerate(phabin): idx = np.logical_and(curpha >= p, curpha < p+binsize) if idx.astype(int).sum() != 0: ampbin[i, t] = curamp[idx].mean() else: ampbin[i, t] = 0 ampbin[:, t] /= ampbin[:, t].sum() # Find prefered phase and p-values : pfp = np.array([phabin[k]+binsize/2 for k in ampbin.argmax(axis=0)]) pvalue = circ_rtest(pfp)[0] prf = phabin[ampbin.mean(axis=1).argmax()]+binsize/2 return pfp, prf, pvalue, ampbin
Example 24
def get_snippet_idx(snippet, full_array): """ Find the indices of ``full_array`` where ``snippet`` is present. Assumes both ``snippet`` and ``full_array`` are ordered. Parameters ---------- snippet : np.array Array of ordered time stamps full_array : np.array Array of ordered time stamps Returns ------- idx : np.array Array of booleans indicating where in ``full_array`` ``snippet`` is present. """ idx = np.logical_and( full_array >= snippet[0], full_array <= snippet[-1] ) return idx
Example 25
def get_signal_mask(df): masks = [] # Select waveforms whose DOMs are close to both interaction # vertices and still suficiently separated in time masks.append(df['GeometricalSelection'] == 1) # Apply a light set of cuts on the remaining waveforms masks.append(df['Bins_ToT_Pulse1'] >= 2) masks.append(df['Bins_ToT_Pulse2'] >= 3) masks.append(df['Bins_TbT'] >= 2) masks.append(df['Amplitude_Pulse1'] >= 10) masks.append(df['Amplitude_Pulse2'] >= 10) # Combine all the masks selection_mask = masks[0] for i in range(1, len(masks)): selection_mask = np.logical_and(selection_mask, masks[i]) return selection_mask
Example 26
def get_edge_mask(self, subdomain=None): '''Get faces which are fully in subdomain. ''' if subdomain is None: # http://stackoverflow.com/a/42392791/353337 return numpy.s_[:] if subdomain not in self.subdomains: self._mark_vertices(subdomain) # A face is inside if all its edges are in. # An edge is inside if all its nodes are in. is_in = self.subdomains[subdomain]['vertices'][self.idx_hierarchy] # Take `all()` over the first index is_inside = numpy.all(is_in, axis=tuple(range(1))) if subdomain.is_boundary_only: # Filter for boundary is_inside = numpy.logical_and(is_inside, self.is_boundary_edge) return is_inside
Example 27
def get_face_mask(self, subdomain): '''Get faces which are fully in subdomain. ''' if subdomain is None: # http://stackoverflow.com/a/42392791/353337 return numpy.s_[:] if subdomain not in self.subdomains: self._mark_vertices(subdomain) # A face is inside if all its edges are in. # An edge is inside if all its nodes are in. is_in = self.subdomains[subdomain]['vertices'][self.idx_hierarchy] # Take `all()` over all axes except the last two (face_ids, cell_ids). n = len(is_in.shape) is_inside = numpy.all(is_in, axis=tuple(range(n-2))) if subdomain.is_boundary_only: # Filter for boundary is_inside = numpy.logical_and(is_inside, self.is_boundary_face) return is_inside
Example 28
def _mark_vertices(self, subdomain): '''Mark faces/edges which are fully in subdomain. ''' if subdomain is None: is_inside = numpy.ones(len(self.node_coords), dtype=bool) else: is_inside = subdomain.is_inside(self.node_coords.T).T if subdomain.is_boundary_only: # Filter boundary self.mark_boundary() is_inside = numpy.logical_and(is_inside, self.is_boundary_node) self.subdomains[subdomain] = { 'vertices': is_inside, } return
Example 29
def _filter_image_with_no_gt(self): """ filter images that have no ground-truth labels. use case: when you wish to work only on a subset of pascal classes, you have 2 options: 1. use only the sub-dataset that contains the subset of classes 2. use all images, and images with no ground-truth will count as true-negative images :return: self object with filtered information """ # filter images that do not have any of the specified classes self.labels = [f[np.logical_and(f[:, 0] >= 0, f[:, 0] <= self.num_classes-1), :] for f in self.labels] # find indices of images with ground-truth labels gt_indices = [idx for idx, f in enumerate(self.labels) if not f.size == 0] self.labels = [self.labels[idx] for idx in gt_indices] self.image_set_index = [self.image_set_index[idx] for idx in gt_indices] old_num_images = self.num_images self.num_images = len(self.labels) print ('filtering images with no gt-labels. can abort filtering using *true_negative* flag') print ('... remaining {0}/{1} images. '.format(self.num_images, old_num_images))
Example 30
def test_truth_table_logical(self): # 2, 3 and 4 serves as true values input1 = [0, 0, 3, 2] input2 = [0, 4, 0, 2] typecodes = (np.typecodes['AllFloat'] + np.typecodes['AllInteger'] + '?') # boolean for dtype in map(np.dtype, typecodes): arg1 = np.asarray(input1, dtype=dtype) arg2 = np.asarray(input2, dtype=dtype) # OR out = [False, True, True, True] for func in (np.logical_or, np.maximum): assert_equal(func(arg1, arg2).astype(bool), out) # AND out = [False, False, False, True] for func in (np.logical_and, np.minimum): assert_equal(func(arg1, arg2).astype(bool), out) # XOR out = [False, True, True, False] for func in (np.logical_xor, np.not_equal): assert_equal(func(arg1, arg2).astype(bool), out)
Example 31
def test_object_logical(self): a = np.array([3, None, True, False, "test", ""], dtype=object) assert_equal(np.logical_or(a, None), np.array([x or None for x in a], dtype=object)) assert_equal(np.logical_or(a, True), np.array([x or True for x in a], dtype=object)) assert_equal(np.logical_or(a, 12), np.array([x or 12 for x in a], dtype=object)) assert_equal(np.logical_or(a, "blah"), np.array([x or "blah" for x in a], dtype=object)) assert_equal(np.logical_and(a, None), np.array([x and None for x in a], dtype=object)) assert_equal(np.logical_and(a, True), np.array([x and True for x in a], dtype=object)) assert_equal(np.logical_and(a, 12), np.array([x and 12 for x in a], dtype=object)) assert_equal(np.logical_and(a, "blah"), np.array([x and "blah" for x in a], dtype=object)) assert_equal(np.logical_not(a), np.array([not x for x in a], dtype=object)) assert_equal(np.logical_or.reduce(a), 3) assert_equal(np.logical_and.reduce(a), None)
Example 32
def _make_counts(emin, emax): def _counts(field, data): e = data["event_energy"].in_units("keV") mask = np.logical_and(e >= emin, e < emax) x = data["event_x"][mask] y = data["event_y"][mask] z = np.ones(x.shape) pos = np.array([x,y,z]).transpose() img = data.deposit(pos, method="count") if data.has_field_parameter("sigma"): sigma = data.get_field_parameter("sigma") else: sigma = None if sigma is not None and sigma > 0.0: kern = _astropy.conv.Gaussian2DKernel(stddev=sigma) img[:,:,0] = _astropy.conv.convolve(img[:,:,0], kern) return data.ds.arr(img, "counts/pixel") return _counts
Example 33
def test_particle_filter_dependency(): """ Test dataset add_particle_filter which should automatically add the dependency of the filter. """ @particle_filter(filtered_type='all', requires=['particle_type']) def stars(pfilter, data): filter = data[(pfilter.filtered_type, "particle_type")] == 2 return filter @particle_filter(filtered_type='stars', requires=['creation_time']) def young_stars(pfilter, data): age = data.ds.current_time - data[pfilter.filtered_type, "creation_time"] filter = np.logical_and(age.in_units('Myr') <= 5, age >= 0) return filter ds = yt.load(iso_galaxy) ds.add_particle_filter('young_stars') assert 'young_stars' in ds.particle_types assert 'stars' in ds.particle_types assert ('deposit', 'young_stars_cic') in ds.derived_field_list assert ('deposit', 'stars_cic') in ds.derived_field_list
Example 34
def bbox_filter(left, right, domain_width): def myfilter(chunk, mask=None): pos = np.array([chunk['x'], chunk['y'], chunk['z']]).T # This hurts, but is useful for periodicity. Probably should check # first if it is even needed for a given left/right for i in range(3): pos[:, i] = np.mod(pos[:, i] - left[i], domain_width[i]) + left[i] # Now get all particles that are within the bbox if mask is None: mask = np.all(pos >= left, axis=1) np.logical_and(mask, np.all(pos < right, axis=1), mask) else: np.logical_and(mask, np.all(pos >= left, axis=1), mask) np.logical_and(mask, np.all(pos < right, axis=1), mask) return mask return myfilter
Example 35
def get_transitions(states): """ Computes transitions given a state array Args: states : numpy array States array of the form ...,4,1,1,...,1,2,2,...,2,3,3,....,3,4,...,4,1,... Returns: transitions : numpy array Contains indices of all the transitions in the states array """ states = np.squeeze(states) # Edge cases when starts in 1 and/or ends in 4 if states[0] == 1: states = np.concatenate(([4], states)) if states[-1] == 4: states = np.concatenate((states, [1])) transitions = np.where(np.diff(states) != 0)[0] + 1 first = np.where(states == 1)[0][0] last = np.where(states == 4)[0][-1] + 1 transitions = transitions[np.logical_and(transitions >= first, transitions <= last)] return transitions
Example 36
def get_transitions(states): """ Computes transitions given a state array Args: states : numpy array States array of the form ...,4,1,1,...,1,2,2,...,2,3,3,....,3,4,...,4,1,... Returns: transitions : numpy array Contains indices of all the transitions in the states array """ states = np.squeeze(states) # Edge cases when starts in 1 and/or ends in 4 if states[0] == 1: states = np.concatenate(([4], states)) if states[-1] == 4: states = np.concatenate((states, [1])) transitions = np.where(np.diff(states) != 0)[0] + 1 first = np.where(states == 1)[0][0] last = np.where(states == 4)[0][-1] + 1 transitions = transitions[np.logical_and(transitions >= first, transitions <= last)] return transitions
Example 37
def _compute_count_purity(counts0, counts1): """ Compute fraction of counts in putative single-cell GEMs originating from the non-cell transcriptome """ gem_occupancy = MultiGenomeAnalysis._classify_gems(counts0, counts1) frac0 = counts0.astype(float) / (counts0 + counts1).astype(float) purity0 = frac0[gem_occupancy == cr_constants.GEM_CLASS_GENOME0] purity1 = 1 - frac0[gem_occupancy == cr_constants.GEM_CLASS_GENOME1] overall_purity = np.concatenate([purity0, purity1]) # Compute number of purity outliers threshold0, threshold1 = 1.0, 1.0 fit_purity0 = purity0[np.logical_and(purity0 > 0, purity0 < 1)] fit_purity1 = purity1[np.logical_and(purity1 > 0, purity1 < 1)] if len(fit_purity0) > 1 and len(fit_purity1) > 1: try: alpha0, beta0, _, _ = scipy.stats.beta.fit(fit_purity0, floc=0, fscale=1) alpha1, beta1, _, _ = scipy.stats.beta.fit(fit_purity1, floc=0, fscale=1) threshold0 = scipy.stats.beta.ppf(cr_constants.COUNT_PURITY_OUTLIER_PROB_THRESHOLD, alpha0, beta0) threshold1 = scipy.stats.beta.ppf(cr_constants.COUNT_PURITY_OUTLIER_PROB_THRESHOLD, alpha1, beta1) except scipy.stats._continuous_distns.FitSolverError as e: print >> sys.stderr, e threshold0, threshold1 = 1.0, 1.0 except scipy.stats._continuous_distns.FitDataError as e: print >> sys.stderr, e threshold0, threshold1 = 1.0, 1.0 outlier0 = np.logical_and(gem_occupancy == cr_constants.GEM_CLASS_GENOME0, frac0 < threshold0) outlier1 = np.logical_and(gem_occupancy == cr_constants.GEM_CLASS_GENOME1, (1-frac0) < threshold1) n_outlier0 = sum(outlier0) n_outlier1 = sum(outlier1) frac_outlier0 = tk_stats.robust_divide(n_outlier0, len(purity0)) frac_outlier1 = tk_stats.robust_divide(n_outlier1, len(purity1)) is_outlier = np.logical_or(outlier0, outlier1).astype(int) return (purity0.mean(), purity1.mean(), overall_purity.mean(), n_outlier0, n_outlier1, frac_outlier0, frac_outlier1, is_outlier)
Example 38
def numpy_logical_and_list(list_of_logicals): assert(len(list_of_logicals) >= 2) output = list_of_logicals[0] for i in range(1,len(list_of_logicals)): output = np.logical_and(output, list_of_logicals[i]) return output
Example 39
def clean_contour(in_contour, is_prob=False): if is_prob: pred = (in_contour >= 0.5).astype(np.float32) else: pred = in_contour labels = measure.label(pred) area = [] for l in range(1, np.amax(labels) + 1): area.append(np.sum(labels == l)) out_contour = in_contour out_contour[np.logical_and(labels > 0, labels != np.argmax(area) + 1)] = 0 return out_contour
Example 40
def dice(im1, im2): """ Computes the Dice coefficient, a measure of set similarity. Parameters ---------- im1 : array-like, bool Any array of arbitrary size. If not boolean, will be converted. im2 : array-like, bool Any other array of identical size. If not boolean, will be converted. Returns ------- dice : float Dice coefficient as a float on range [0,1]. Maximum similarity = 1 No similarity = 0 Notes ----- The order of inputs for `dice` is irrelevant. The result will be identical if `im1` and `im2` are switched. """ im1 = np.asarray(im1).astype(np.bool) im2 = np.asarray(im2).astype(np.bool) if im1.shape != im2.shape: raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") # Compute Dice coefficient intersection = np.logical_and(im1, im2) return (2. * intersection.sum() + np.finfo('float').eps) / (im1.sum() + im2.sum() + np.finfo('float').eps)
Example 41
def dice(im1, im2): """ Computes the Dice coefficient, a measure of set similarity. Parameters ---------- im1 : array-like, bool Any array of arbitrary size. If not boolean, will be converted. im2 : array-like, bool Any other array of identical size. If not boolean, will be converted. Returns ------- dice : float Dice coefficient as a float on range [0,1]. Maximum similarity = 1 No similarity = 0 Notes ----- The order of inputs for `dice` is irrelevant. The result will be identical if `im1` and `im2` are switched. """ im1 = np.asarray(im1).astype(np.bool) im2 = np.asarray(im2).astype(np.bool) if im1.shape != im2.shape: raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") # Compute Dice coefficient intersection = np.logical_and(im1, im2) return 2. * (intersection.sum() + np.finfo('float').eps) / (im1.sum() + im2.sum() + 2*np.finfo('float').eps)
Example 42
def dice(im1, im2): """ Computes the Dice coefficient, a measure of set similarity. Parameters ---------- im1 : array-like, bool Any array of arbitrary size. If not boolean, will be converted. im2 : array-like, bool Any other array of identical size. If not boolean, will be converted. Returns ------- dice : float Dice coefficient as a float on range [0,1]. Maximum similarity = 1 No similarity = 0 Notes ----- The order of inputs for `dice` is irrelevant. The result will be identical if `im1` and `im2` are switched. """ im1 = np.asarray(im1).astype(np.bool) im2 = np.asarray(im2).astype(np.bool) if im1.shape != im2.shape: raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") # Compute Dice coefficient intersection = np.logical_and(im1, im2) return 2. * (intersection.sum() + np.finfo('float').eps) / (im1.sum() + im2.sum() + 2*np.finfo('float').eps)
Example 43
def dice(im1, im2): """ Computes the Dice coefficient, a measure of set similarity. Parameters ---------- im1 : array-like, bool Any array of arbitrary size. If not boolean, will be converted. im2 : array-like, bool Any other array of identical size. If not boolean, will be converted. Returns ------- dice : float Dice coefficient as a float on range [0,1]. Maximum similarity = 1 No similarity = 0 Notes ----- The order of inputs for `dice` is irrelevant. The result will be identical if `im1` and `im2` are switched. """ im1 = np.asarray(im1).astype(np.bool) im2 = np.asarray(im2).astype(np.bool) if im1.shape != im2.shape: raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") # Compute Dice coefficient intersection = np.logical_and(im1, im2) return 2. * (intersection.sum() + np.finfo('float').eps) / (im1.sum() + im2.sum() + 2 * np.finfo('float').eps)
Example 44
def dice(im1, im2): """ Computes the Dice coefficient, a measure of set similarity. Parameters ---------- im1 : array-like, bool Any array of arbitrary size. If not boolean, will be converted. im2 : array-like, bool Any other array of identical size. If not boolean, will be converted. Returns ------- dice : float Dice coefficient as a float on range [0,1]. Maximum similarity = 1 No similarity = 0 Notes ----- The order of inputs for `dice` is irrelevant. The result will be identical if `im1` and `im2` are switched. """ im1 = np.asarray(im1).astype(np.bool) im2 = np.asarray(im2).astype(np.bool) if im1.shape != im2.shape: raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") # Compute Dice coefficient intersection = np.logical_and(im1, im2) return 2. * (intersection.sum() + np.finfo('float').eps) / (im1.sum() + im2.sum() + 2*np.finfo('float').eps)
Example 45
def dice(im1, im2): """ Computes the Dice coefficient, a measure of set similarity. Parameters ---------- im1 : array-like, bool Any array of arbitrary size. If not boolean, will be converted. im2 : array-like, bool Any other array of identical size. If not boolean, will be converted. Returns ------- dice : float Dice coefficient as a float on range [0,1]. Maximum similarity = 1 No similarity = 0 Notes ----- The order of inputs for `dice` is irrelevant. The result will be identical if `im1` and `im2` are switched. """ im1 = np.asarray(im1).astype(np.bool) im2 = np.asarray(im2).astype(np.bool) if im1.shape != im2.shape: raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") # Compute Dice coefficient intersection = np.logical_and(im1, im2) return 2. * intersection.sum() / (im1.sum() + im2.sum())
Example 46
def dice(im1, im2): """ Computes the Dice coefficient, a measure of set similarity. Parameters ---------- im1 : array-like, bool Any array of arbitrary size. If not boolean, will be converted. im2 : array-like, bool Any other array of identical size. If not boolean, will be converted. Returns ------- dice : float Dice coefficient as a float on range [0,1]. Maximum similarity = 1 No similarity = 0 Notes ----- The order of inputs for `dice` is irrelevant. The result will be identical if `im1` and `im2` are switched. """ im1 = np.asarray(im1).astype(np.bool) im2 = np.asarray(im2).astype(np.bool) if im1.shape != im2.shape: raise ValueError("Shape mismatch: im1 and im2 must have the same shape.") # Compute Dice coefficient intersection = np.logical_and(im1, im2) return (2. * intersection.sum() + np.finfo('float').eps) / (im1.sum() + im2.sum() + np.finfo('float').eps)
Example 47
def __indexs_select_pk2(self,pk2_roi_pos): x_min = pk2_roi_pos[:,0].min() x_max = pk2_roi_pos[:,0].max() y_min = pk2_roi_pos[:,1].min() y_max = pk2_roi_pos[:,1].max() pca_1,pca_2 = self.PCAusedList.currentText().split("-") pca_1 = np.int(pca_1)-1 pca_2 = np.int(pca_2)-1 x = np.logical_and(self.wavePCAs[:,pca_1]>x_min, \ self.wavePCAs[:,pca_1]<x_max) y = np.logical_and(self.wavePCAs[:,pca_2]>y_min, \ self.wavePCAs[:,pca_2]<y_max) ind_0 = np.logical_and(x, y) ind_0 = np.where(ind_0 == True)[0] ind_0 = np.array(ind_0,dtype=np.int32) if ind_0.shape[0]>0: segments = [] for i in range(pk2_roi_pos.shape[0]-1): segments.append([pk2_roi_pos[i],pk2_roi_pos[i+1]]) segments.append([pk2_roi_pos[-1],pk2_roi_pos[0]]) segments = np.array(segments) temp_pcas = self.wavePCAs[ind_0] temp_pcas = temp_pcas[:,[pca_1,pca_2]] is_intersect = np.apply_along_axis(self.__intersect_roi2,1,temp_pcas,segments,pca_1) return ind_0[is_intersect] else: return np.array([],dtype=np.int32)
Example 48
def __indexs_select_pk2(self,pk2_roi_pos): x_min = pk2_roi_pos[:,0].min() x_max = pk2_roi_pos[:,0].max() y_min = pk2_roi_pos[:,1].min() y_max = pk2_roi_pos[:,1].max() pca_1,pca_2 = self.PCAusedList.currentText().split("-") pca_1 = np.int(pca_1)-1 pca_2 = np.int(pca_2)-1 x = np.logical_and(self.wavePCAs[:,pca_1]>x_min, \ self.wavePCAs[:,pca_1]<x_max) y = np.logical_and(self.wavePCAs[:,pca_2]>y_min, \ self.wavePCAs[:,pca_2]<y_max) ind_0 = np.logical_and(x, y) ind_0 = np.where(ind_0 == True)[0] ind_0 = np.array(ind_0,dtype=np.int32) if ind_0.shape[0]>0: segments = [] for i in range(pk2_roi_pos.shape[0]-1): segments.append([pk2_roi_pos[i],pk2_roi_pos[i+1]]) segments.append([pk2_roi_pos[-1],pk2_roi_pos[0]]) segments = np.array(segments) temp_pcas = self.wavePCAs[ind_0] temp_pcas = temp_pcas[:,[pca_1,pca_2]] is_intersect = np.apply_along_axis(self.__intersect_roi2,1,temp_pcas,segments,pca_1) return ind_0[is_intersect] else: return np.array([],dtype=np.int32)
Example 49
def _diminish(self, prob): # ii,jj = logical_and(self.connected>7, self.grid).nonzero() # self.grid[ii, jj] = False ii,jj = logical_and(self.neigh>self.crowded_limit, self.grid).nonzero() self.grid[ii, jj] = False # diminish_mask = random(size=len(ii))<prob # self.grid[ii[diminish_mask], jj[diminish_mask]] = False # ii,jj = self.grid.nonzero() # diminish_mask = random(size=len(ii))<0.01 # self.grid[ii[diminish_mask], jj[diminish_mask]] = False
Example 50
def generateDXCHANGE(ADNI_DXSUM_table): "Generate DXCHANGE for ADNI1 within DXSUM table: As defined on page 46 of ADNI_data_training_slides_part2.pdf" # Identify ADNI1 Phase and make temporary data frame idx_ADNI1 = np.where(ADNI_DXSUM_table.Phase.values=='ADNI1')[0] DXSUM_table_ADNI1_temp = ADNI_DXSUM_table.iloc[idx_ADNI1] # Initialise DXCHANGE as NaN DXCHANGE_ADNI1 = np.array([np.nan for k in range(0,DXSUM_table_ADNI1_temp.shape[0])]) # Extract relevant DX variables as arrays DXCONV = DXSUM_table_ADNI1_temp.DXCONV.values DXCURREN = DXSUM_table_ADNI1_temp.DXCURREN.values DXCONTYP = DXSUM_table_ADNI1_temp.DXCONTYP.values DXREV = DXSUM_table_ADNI1_temp.DXREV.values # DXCHANGE definitions DXCHANGE_1 = np.logical_and(DXCONV==0,DXCURREN==1) DXCHANGE_2 = np.logical_and(DXCONV==0,DXCURREN==2) DXCHANGE_3 = np.logical_and(DXCONV==0,DXCURREN==3) DXCHANGE_4 = np.logical_and(DXCONV==1,DXCONTYP==1) DXCHANGE_5 = np.logical_and(DXCONV==1,DXCONTYP==3) DXCHANGE_6 = np.logical_and(DXCONV==1,DXCONTYP==2) DXCHANGE_7 = np.logical_and(DXCONV==2,DXREV==1) DXCHANGE_8 = np.logical_and(DXCONV==2,DXREV==2) DXCHANGE_9 = np.logical_and(DXCONV==2,DXREV==3) # Assign appropriate DXCHANGE values DXCHANGE_ADNI1[DXCHANGE_1] = np.array([1 for k in range(0,sum(DXCHANGE_1))]) DXCHANGE_ADNI1[DXCHANGE_2] = np.array([2 for k in range(0,sum(DXCHANGE_2))]) DXCHANGE_ADNI1[DXCHANGE_3] = np.array([3 for k in range(0,sum(DXCHANGE_3))]) DXCHANGE_ADNI1[DXCHANGE_4] = np.array([4 for k in range(0,sum(DXCHANGE_4))]) DXCHANGE_ADNI1[DXCHANGE_5] = np.array([5 for k in range(0,sum(DXCHANGE_5))]) DXCHANGE_ADNI1[DXCHANGE_6] = np.array([6 for k in range(0,sum(DXCHANGE_6))]) DXCHANGE_ADNI1[DXCHANGE_7] = np.array([7 for k in range(0,sum(DXCHANGE_7))]) DXCHANGE_ADNI1[DXCHANGE_8] = np.array([8 for k in range(0,sum(DXCHANGE_8))]) DXCHANGE_ADNI1[DXCHANGE_9] = np.array([9 for k in range(0,sum(DXCHANGE_9))]) # Insert values into original DXSUM table ADNI_DXSUM_table.loc[idx_ADNI1,'DXCHANGE'] = DXCHANGE_ADNI1 return ADNI_DXSUM_table