def my_dbscan(dots, epsilon, minSamples): # v0.1版本,未考虑核心点和边界点的区别 visit_points = [] # 将已访问的点的下标保存起来 unvisit_points = [i for i in range(0, len(dots))] # 将未访问的点的下标保存起来 tag = default_tag = -1 # 默认全为噪声点 tags = [default_tag] * len(dots) # 建立一个KD树,这种树采用类似平衡二叉树的算法,对多维数组建立二叉树,搜索时间复杂度为o(logn) kt = KDTree(dots) while unvisit_points.__len__() > 0: p_idx = unvisit_points[0] visit_points.append(p_idx) unvisit_points.remove(p_idx) neigh_idx = kt.query_ball_point(dots[p_idx], epsilon) if len(neigh_idx) > minSamples + 1: tag += 1 tags[p_idx] = tag while neigh_idx.__len__() > 0: n_idx = neigh_idx[0] neigh_idx.remove(n_idx) if n_idx not in unvisit_points: continue visit_points.append(n_idx) unvisit_points.remove(n_idx) next_neigh_idx = kt.query_ball_point(dots[n_idx], epsilon) if len(next_neigh_idx) > minSamples + 1: neigh_idx = list( set(neigh_idx) | set(next_neigh_idx) & set(unvisit_points)) tags[n_idx] = tag else: tags[p_idx] = -1 return tags
def jiggle_points(self): max_y, min_y = self.y + self.max_height / 2, self.y - self.max_height / 2 more_to_do = (len(self.points) > 0) while more_to_do: for i in range(len(self.points)): current = self.points.pop(0) points_array = numpy.array([[p.x, p.y] for p in self.points]) kdtree = KDTree(points_array) ids = kdtree.query_ball_point([current.x, current.y], self.min_dist) # If there are any neighbours too near counter = 0 while not ids == []: rand = random.uniform(-self.min_dist, self.min_dist) current.y = min(max_y, max(min_y, current.y + rand)) ids = kdtree.query_ball_point([current.x, current.y], self.min_dist) counter += 1 if counter > 100: break self.points.append(current) for i in range(len(self.points)): current = self.points.pop(0) points_array = numpy.array([[p.x, p.y] for p in self.points]) kdtree = KDTree(points_array) ids = kdtree.query_ball_point([current.x, current.y], self.min_dist) self.points.append(current) more_to_do = (not ids == []) if more_to_do: break
def getProteinAtomsAroundHotspot(self, hotspot, pdb, fromCentroid=True, radius=6, includeH=True, returnmask=False): """ Get pdb indices for the atoms neighbouring the hotspot around a given distance. Distance will be calculated from the hotspot centroid or considering all points insed depending on fromCentroid value. Also it is optional to include the hydrogens (default) or remove them from the returned list. Args: hotspot (HotSpot or HotSpotSet) Fetch atoms around this hotspots pdb (PDBModel or string) What PDB to return atoms from. Should be a Biskit.PDBModel or a string pointing to a correct pdb file to read. fromCentroid (bool) Calculate from centroid point or considering all points in the hotspot? radius (float) Radius around we consider an atom as neighbour includeH (bool) Include hydrogens in returen indices? returnMask (bool) Return as mask instead of list of indices? Returns: indicesList (list of ints) Indices in the pdb corresponding to neighbouring atoms or indicesMask (npy.ndarray of bools) Mask for the PDBModel """ import Biskit as bi import scipy if scipy.__version__ > "0.11.0": from scipy.spatial import KDTree as KDTree else: from scipy.spatial import KDTree # Load PDB if string # and get Hydrogens mask if isinstance(pdb, str): pdb = bi.PDBModel(pdb) hids = npy.where(pdb.maskH())[0] # First construct a KDTree with protein coordinates. tree = KDTree(pdb.xyz) # 3dims, 1 atom per bucket # Find neighbours # check first if hotspot has multiple coords or just one if hotspot.coordList.ndim == 1: dims = 1 else: dims = 2 if fromCentroid or dims == 1: # Calculate n'bours to one point rawids = tree.query_ball_point(hotspot.coord, radius) if not includeH: ids = [ri for ri in rawids if ri not in hids] # check index is not a hydrogen atom else: ids = rawids else: # Do a search for each point and get a unique set of n'bours rawids = tree.query_ball_point(hotspot.coordList, radius) if not includeH: ids = [ri for ri in rawids if ri not in hids] # check index is not a hydrogen atom else: ids = rawids if returnmask: m = [i in ids for i in range(len(pdb.xyz))] return m else: return ids
def jiggle_points(self): max_y, min_y = self.y + self.max_height/2, self.y - self.max_height/2 more_to_do = (len(self.points) > 0) while more_to_do: for i in range(len(self.points)): current = self.points.pop(0) points_array = numpy.array([[p.x, p.y] for p in self.points]) kdtree = KDTree(points_array) ids = kdtree.query_ball_point([current.x, current.y], self.min_dist) # If there are any neighbours too near counter = 0 while not ids == []: rand = random.uniform(-self.min_dist, self.min_dist) current.y = min(max_y, max(min_y, current.y + rand)) ids = kdtree.query_ball_point([current.x, current.y], self.min_dist) counter += 1 if counter > 100: break self.points.append(current) for i in range(len(self.points)): current = self.points.pop(0) points_array = numpy.array([[p.x, p.y] for p in self.points]) kdtree = KDTree(points_array) ids = kdtree.query_ball_point([current.x, current.y], self.min_dist) self.points.append(current) more_to_do = (not ids == []) if more_to_do: break
def extraCredit(tripLocations, startPolygon, endPolygon): #indices is a list that should contain the indices of the trips in the tripLocations list #which start in the startPolygon region and end in the endPolygon region indices = [] #TODO: insert your code here. You should build the kdtree and use it to query the closest # intersection for each trip startpoint = [] endpoint = [] for item in tripLocations: startpoint.append([item[0],item[1]]) endpoint.append([item[2],item[3]]) #print start startTime = time.time() rs = ((startRectangle[0][0] - startRectangle[0][1])**2 + (startRectangle[1][0] - startRectangle[1][1])**2)**0.5/float(2) re = ((endRectangle[0][0] - endRectangle[0][1])**2 + (endRectangle[1][0] - endRectangle[1][1])**2)**0.5/float(2) os = [(startRectangle[0][0] + startRectangle[0][1])/2,(startRectangle[1][0] + startRectangle[1][1])/2] oe = [(endRectangle[0][0] + endRectangle[0][1])/2,(endRectangle[1][0] + endRectangle[1][1])/2] Stree = KDTree(startpoint) Etree = KDTree(endpoint) startpoint = set(Stree.query_ball_point(os, rs)) endpoint = set(Etree.query_ball_point(oe, re)) s_e = list(startpoint.intersection(endpoint)) for point in s_e: if point_inside_polygon(tripLocations[point][0],tripLocations[point][1],startPolygon) and point_inside_polygon(tripLocations[point][2],tripLocations[point][3],endPolygon): indices.append(i) return indices
def optics(self, eps=0.1, min_pts=5): self.eps = eps self.reach_dist = [inf for i in range(self.n)] # 可达距离 self.core_dist = [inf for i in range(self.n)] # 核心距离 kd = KDTree(self.dataset) while (self.unvisited): # 随机选取一个点 i = random.choice(self.unvisited) self.visit(i) # 获取i的邻域 neighbors_i = kd.query_ball_point(self.dataset[i], eps) # 如果i是核心点 if len(neighbors_i) >= min_pts: # 计算核心距离 self.core_dist[i] = self.cal_core_dist(i, neighbors_i, min_pts) seed_list = list() self.insert_list(i, neighbors_i, seed_list) while (seed_list): seed_list.sort(key=lambda x: self.reach_dist[x]) j = seed_list.pop(0) self.visit(j) neighbors_j = kd.query_ball_point(self.dataset[j], eps) if len(neighbors_j) >= min_pts: self.core_dist[j] = self.cal_core_dist( j, neighbors_j, min_pts) self.insert_list(j, neighbors_j, seed_list) return self.order_list, self.reach_dist
def blue_noise_sample_elimination(point_list, mesh_surface_area, sample_count): # Parameters alpha = 8 rmax = numpy.sqrt(mesh_surface_area / ((2 * sample_count) * numpy.sqrt(3.))) # Compute a KD-tree of the input point list kdtree = KDTree(point_list) # Compute the weight for each sample D = numpy.minimum(squareform(pdist(point_list)), 2 * rmax) D = (1. - (D / (2 * rmax))) ** alpha W = numpy.zeros(point_list.shape[0]) for i in range(point_list.shape[0]): W[i] = sum(D[i, j] for j in kdtree.query_ball_point(point_list[i], 2 * rmax) if i != j) # Pick the samples we need heap = sorted((w, i) for i, w in enumerate(W)) id_set = set(range(point_list.shape[0])) while len(id_set) > sample_count: # Pick the sample with the highest weight w, i = heap.pop() id_set.remove(i) neighbor_set = set(kdtree.query_ball_point(point_list[i], 2 * rmax)) neighbor_set.remove(i) heap = [(w - D[i, j], j) if j in neighbor_set else (w, j) for w, j in heap] heap.sort() # Job done return point_list[sorted(id_set)]
def dbscan(dataSet, eps, minPts): """ @brief 基于kd-tree的DBScan算法 @param dataSet 输入数据集,numpy格式 @param eps 最短距离 @param minPts 最小簇点数 @return 分类标签 """ nPoints = dataSet.shape[0] vPoints = visitlist(count=nPoints) # 初始化簇标记列表C,簇标记为 k k = -1 C = [-1 for i in range(nPoints)] # 构建KD-Tree,并生成所有距离<=eps的点集合 kd = KDTree(dataSet) while (vPoints.unvisitednum > 0): p = random.choice(vPoints.unvisitedlist) vPoints.visit(p) N = kd.query_ball_point(dataSet[p], eps) if len(N) >= minPts: k += 1 C[p] = k for p1 in N: if p1 in vPoints.unvisitedlist: vPoints.visit(p1) M = kd.query_ball_point(dataSet[p1], eps) if len(M) >= minPts: for i in M: if i not in N: N.append(i) if C[p1] == -1: C[p1] = k else: C[p] = -1 return C
def dbscan(data, eps, min_pts): m = data.shape[0] points = VisitRecord(m) group_index = -1 group = np.zeros((m, 1))-1 kd_tree = KDTree(data) while points.unvisited_num > 0: current_visit = choice(points.unvisited) points.visit(current_visit) print("unvisited", points.unvisited_num) round_points = kd_tree.query_ball_point(data[current_visit], eps) if len(round_points) >= min_pts: group_index += 1 group[current_visit] = group_index for p_index in round_points: print("unvisited", points.unvisited_num, group_index) if p_index in points.unvisited: points.visit(p_index) round_points_next = kd_tree.query_ball_point(data[p_index], eps) if len(round_points_next) >= min_pts: for i_index in round_points_next: if i_index not in round_points: round_points.append(i_index) if group[p_index] == -1: group[p_index] = group_index else: group[current_visit] = -1 return group, group_index
def infect_opt(self, q): """ When there are many infectious individuals, the tree query by I becomes time consuming We want to optimize this fuction in such cases. We can query by susceptible individuals instead, whose population size is smaller If a susceptible person's neighbors include any I, then he becomes infected. """ X = np.vstack((self.s_info,self.i_info)) if X.size == 0: # print('all individuals are recovered') return tree = KDTree(X[:,1:]) # pos of S, I if len(self.s_info) > len(self.i_info): # query by I is more efficient contact_lists = tree.query_ball_point(self.i_info[:,1:], q) # a list of lists of indices contact_tree_inds = sum(contact_lists, []) # flatten a list of lists contact_inds = X[:,0][contact_tree_inds] s_contact_slice = np.isin(self.s_info[:,0], contact_inds) new_i_info = self.s_info[s_contact_slice] self.s_info = self.s_info[~s_contact_slice] else: # query by S is more efficient # print('optimizing') contact_lists = tree.query_ball_point(self.s_info[:,1:], q) # a list of lists of indices i_tree_inds = range(len(self.s_info), len(X)) new_i_tree_inds = [] for s_ind, contact_list in enumerate(contact_lists): # for each S if np.any(np.isin(i_tree_inds, contact_list)): # if any I is in an S's neighbor new_i_tree_inds.append(s_ind) # then this S becomes infected new_i_inds = X[:,0][new_i_tree_inds] s_contact_slice = np.isin(self.s_info[:,0], new_i_inds, True) new_i_info = self.s_info[s_contact_slice] self.s_info = self.s_info[~s_contact_slice] self.i_info = np.concatenate((self.i_info, new_i_info))
def DBSCAN(X: np.ndarray, r: float, minPts: int): pointnum = X.shape[0] v = visitlist(pointnum) clustersSet = list() noise = cluster(-1) tree = KDTree(X) k = 0 while v.unvisitednum > 0: randid = random.choice(v.unvisitedlist) v.visit(randid) N = tree.query_ball_point(X[randid], r) if len(N) < minPts: noise.points.append(randid) else: clus = cluster(k) clus.points.append(randid) N.remove(randid) while len(N) > 0: p = N.pop() if p in v.unvisitedlist: v.visit(p) clus.points.append(p) pN = tree.query_ball_point(X[p], r) if len(pN) >= minPts: pN.remove(p) N = N + pN clustersSet.append(clus) clustersSet.append(noise) return clustersSet
def sub_dbscan(dataSet, eps, minPts): nPoints = dataSet.shape[0] vPoints = visitlist(count=nPoints) # 初始化簇标记列表C,簇标记为 k k = -1 C = [-1 for i in range(nPoints)] # 构建KD-Tree,并生成所有距离<=eps的点集合 kd = KDTree(dataSet) while (vPoints.unvisitednum > 0): p = random.choice(vPoints.unvisitedlist) vPoints.visit(p) N = kd.query_ball_point(dataSet[p], eps) if len(N) >= minPts: k += 1 C[p] = k for p1 in N: if p1 in vPoints.unvisitedlist: vPoints.visit(p1) M = kd.query_ball_point(dataSet[p1], eps) new_C = C.copy() C_list.append(new_C) if len(M) >= minPts: for i in M: if i not in N: N.append(i) if C[p1] == -1: C[p1] = k else: C[p] = -1 return C_list
def dbscan(dataSet, eps, minPts): nPoints = dataSet.shape[0] vPoints = visitlist(count=nPoints) k = -1 C = [-1 for i in range(nPoints)] kd = KDTree(dataSet) #corrcet X->dataSet while (vPoints.unvisitednum > 0): p = random.choice(vPoints.unvisitedlist) vPoints.visit(p) N = kd.query_ball_point(dataSet[p], eps) if len(N) >= minPts: k += 1 C[p] = k for p1 in N: if p1 in vPoints.unvisitedlist: vPoints.visit(p1) M = kd.query_ball_point(dataSet[p1], eps) if len(M) >= minPts: for i in M: if i not in N: N.append(i) if C[p1] == -1: C[p1] = k else: C[p] = -1 #corrcet p1->p return C
def get_current_tote_content_cloud(self, cur_camera_R, cur_camera_t, scene_cloud=None, threshold=0.02, fit=False): ''' subtract tote model from current point cloud of tote (with object in it) if fit is True, transform the model to align with the scene first. it will take some time return the point cloud of the remaining scene in global frame ''' if scene_cloud is None: _, scene_cloud, _, _ = self.read_once(unit='meter', Nx3_cloud=True, clean=True) model_cloud = self.load_model_tote_cloud(downsample=True) assert model_cloud is not None, 'Error loading tote model' model_xform_R, model_xform_t = self.load_R_t(self.get_tote_viewing_camera_xform_path(), nparray=True) model_cloud = model_cloud.dot(model_xform_R) + model_xform_t scene_cloud = scene_cloud[::downsample_rate, :] cur_camera_R = np.array(cur_camera_R).reshape(3,3).T cur_camera_t = np.array(cur_camera_t) scene_cloud = scene_cloud.dot(cur_camera_R.T) + cur_camera_t # transform scene cloud print "Making scene cloud with %i points"%scene_cloud.shape[0] scene_tree = KDTree(scene_cloud) print "Done" if fit: R, t = icp.match(model_cloud, scene_tree) model_cloud = model_cloud.dot(R.T) + t keep_idxs = self.subtract_model(model_cloud, scene_tree=scene_tree, threshold=threshold) content_cloud = scene_cloud[keep_idxs, :] # print 'Dirty content cloud has %i points'%content_cloud.shape[0] num_content_cloud_pts = content_cloud.shape[0] print "Making content cloud with %i points"%content_cloud.shape[0] content_tree = KDTree(content_cloud) print "Done. Querying neighbors within 0.03 m" idxs = content_tree.query_ball_point(content_cloud, 0.03) unisolated_flag = map(lambda x: len(x)>=3, idxs) content_cloud = content_cloud[np.where(unisolated_flag)[0], :] print "Clean cloud has %i points"%content_cloud.shape[0] print "Making content cloud with %i points"%content_cloud.shape[0] content_tree = KDTree(content_cloud) print "Done. Querying neighbors within 0.03 m" idxs = content_tree.query_ball_point(content_cloud, 0.03) unisolated_flag = map(lambda x: len(x)>=4, idxs) content_cloud = content_cloud[np.where(unisolated_flag)[0], :] print "Clean cloud has %i points"%content_cloud.shape[0] print "Making content cloud with %i points"%content_cloud.shape[0] content_tree = KDTree(content_cloud) print "Done. Querying neighbors within 0.03 m" idxs = content_tree.query_ball_point(content_cloud, 0.03) unisolated_flag = map(lambda x: len(x)>=5, idxs) content_cloud = content_cloud[np.where(unisolated_flag)[0], :] print "Clean cloud has %i points"%content_cloud.shape[0] return content_cloud
def test_ball_point_ints(): """Regression test for #1373.""" x, y = np.mgrid[0:4, 0:4] points = zip(x.ravel(), y.ravel()) tree = KDTree(points) assert_equal([4, 8, 9, 12], tree.query_ball_point((2, 0), 1)) points = np.asarray(points, dtype=np.float) tree = KDTree(points) assert_equal([4, 8, 9, 12], tree.query_ball_point((2, 0), 1))
class SearchRoutes(object): def __init__(self): self.stations = pd.read_csv(FN, index_col=0) self.stations['horizontal'] = self.stations.longitude * 52.32 self.stations['vertical'] = self.stations.latitude * 69.135 self.tree = KDTree(self.stations[['vertical', 'horizontal']]) self.__get_routes__() print('Finished Initializing.') def __get_routes__(self): self.routes = pd.read_csv(ROUTES_FN, sep='\t') self.routes.dock_id1 = self.routes.dock_id1.astype(int) self.routes.dock_id2 = self.routes.dock_id2.astype(int) self.routes.set_index(['dock_id1', 'dock_id2'], inplace=True) self.routes.sort_index(inplace=True) def start_to_end(self, start, end): """ Produce a graph of time duration then find the path which minimizes the total duration. start: [latitude, longitude] end: [latitude, longitude] """ # Find stations within walking distance from start and finish. start_miles = [start[0]*69.135, start[1]*52.32] end_miles = [end[0]*69.135, end[1]*52.32] inds = self.tree.query_ball_point(start_miles, 0.5) start_stations = self.stations.iloc[inds] print(start_stations.stationName) inds = self.tree.query_ball_point(end_miles, 0.5) end_stations = self.stations.iloc[inds] # Add walking duration from start to each of those stations. nodes = {} for _, start_row in start_stations.iterrows(): r = get_directions(start[::-1], start_row[['longitude', 'latitude']], 'foot') nodes.update({start_row.id:r['duration']}) graph = {'start': nodes} for _, end_row in end_stations.iterrows(): r = get_directions(end[::-1], end_row[['longitude', 'latitude']], 'foot') graph.update({end_row.id:{'end':r['duration']}}) # Add cycling directions from each "start" citibike to each "end" citibike. for _, start_row in start_stations.iterrows(): nodes = {} for _, end_row in end_stations.iterrows(): nodes.update({end_row.id : self.routes.loc[start_row.id, end_row.id]['duration']}) graph.update({start_row.id:nodes}) return get_shortest_path(graph, 'start', 'end')
def test_ball_point_ints(): # Regression test for #1373. x, y = np.mgrid[0:4, 0:4] points = list(zip(x.ravel(), y.ravel())) tree = KDTree(points) assert_equal(sorted([4, 8, 9, 12]), sorted(tree.query_ball_point((2, 0), 1))) points = np.asarray(points, dtype=float) tree = KDTree(points) assert_equal(sorted([4, 8, 9, 12]), sorted(tree.query_ball_point((2, 0), 1)))
def fit(self, X): n_samples = len(X) kd_tree = KDTree(X) # 构造KD树 density_arr = np.array([len(kd_tree.query_ball_point(x, self.eps)) for x in X]) # 密度数组 visited_arr = [False for _ in range(n_samples)] # 访问标记数组 k = -1 # 初始类别 self.labels_ = np.array([-1 for _ in range(n_samples)]) for sample_idx in range(n_samples): if visited_arr[sample_idx]: # 跳过已访问样本 continue visited_arr[sample_idx] = True # 跳过噪声样本与边界样本 if density_arr[sample_idx] == 1 or density_arr[sample_idx] < self.min_samples: continue # 核心对象 else: # 找出邻域中的所有核心对象,包括自身 cores = [idx for idx in kd_tree.query_ball_point(X[sample_idx], self.eps) if density_arr[idx] >= self.min_samples] k += 1 self.labels_[sample_idx] = k self.core_sample_indices_.append(sample_idx) while cores: cur_core = cores.pop(0) if not visited_arr[cur_core]: self.core_sample_indices_.append(cur_core) visited_arr[cur_core] = True self.labels_[cur_core] = k neighbors = kd_tree.query_ball_point(X[cur_core], self.eps) neighbor_cores = [idx for idx in neighbors if idx not in cores and density_arr[idx] >= self.min_samples] neighbor_boards = [idx for idx in neighbors if density_arr[idx] < self.min_samples] cores.extend(neighbor_cores) for idx in neighbor_boards: if self.labels_[idx] == -1: self.labels_[idx] = k # 更新类属性 self.core_sample_indices_ = np.sort(np.array(self.core_sample_indices_)) self.components_ = X[self.core_sample_indices_]
def predict(self): """ @brief 基于kd-tree的DBScan算法 @param data_set 输入数据集 @param eps 最短距离 @param minPts 最小簇点数 @return 分类标签 """ # 输入数据点的个数 nPoints = len(self.data_set) # (1) 标记所有对象为unvisited vPoints = visitlist(count=nPoints) # (2) 初始化簇标记列表C,簇标记为 k k = -1 C = [-1 for i in range(nPoints)] # 构建KD-Tree,并生成所有距离<=eps的点集合 kd = KDTree(self.data_set) while (vPoints.unvisitednum > 0): # (3) 随机选择一个unvisited对象p p = random.choice(vPoints.unvisitedlist) # (4) 标t己p为visited vPoints.visit(p) # (5) 如果 p 的邻域至少有MinPts个对象 N = kd.query_ball_point(self.data_set[p], self.eps) # 邻域 if len(N) >= self.minPts: # (6) 创建个一个新簇C,并把p添加到C k += 1 C[p] = k # (7) 令N为p的邻域中的对象的集合 # (8) for N中的每个点p' for p1 in N: # (9) if p'是unvisited if p1 in vPoints.unvisitedlist: # (10) 标记p'为visited vPoints.visit(p1) # (11) if p'的$\varepsilon$-邻域至少有MinPts个点,把这些点添加到N # 找出p'的邻域点,并将这些点去重新添加到N M = kd.query_ball_point(self.data_set[p1], self.eps) if len(M) >= self.minPts: for i in M: if i not in N: N.append(i) # (12) if p'还不是任何簇的成员,把p'添加到c if C[p1] == -1: C[p1] = k # (13) else标记p为噪声 else: C[p1] = -1 # (14) until没有标记为unvisited的对象 return C
def dbscan_kd(dataSet, eps, minPts, X): # numpy.ndarray的 shape属性表示矩阵的行数与列数 # 行数即表小所有点的个数 nPoints = dataSet.shape[0] # (1) 标记所有对象为unvisited # 在这里用一个类vPoints进行实现 vPoints = vs.visitlist(count=nPoints) # 初始化簇标记列表C,簇标记为 k k = -1 C = [-1 for i in range(nPoints)] # 构建KD-Tree,并生成所有距离<=eps的点集合 kd = KDTree(X) while (vPoints.unvisitednum > 0): # (3) 随机选择一个unvisited对象p p = random.choice(vPoints.unvisitedlist) # (4) 标t己p为visited vPoints.visit(p) # (5) if p 的$\varepsilon$-邻域至少有MinPts个对象 # N是p的$\varepsilon$-邻域点列表 N = kd.query_ball_point(dataSet[p], eps) if len(N) >= minPts: # (6) 创建个一个新簇C,并把p添加到C # 这里的C是一个标记列表,直接对第p个结点进行赋值 k += 1 C[p] = k # (7) 令N为p的$\varepsilon$-邻域中的对象的集合 # N是p的$\varepsilon$-邻域点集合 # (8) for N中的每个点p' for p1 in N: # (9) if p'是unvisited if p1 in vPoints.unvisitedlist: # (10) 标记p'为visited vPoints.visit(p1) # (11) if p'的$\varepsilon$-邻域至少有MinPts个点,把这些点添加到N # 找出p'的$\varepsilon$-邻域点,并将这些点去重新添加到N M = kd.query_ball_point(dataSet[p1], eps) if len(M) >= minPts: for i in M: if i not in N: N.append(i) # (12) if p'还不是任何簇的成员,把p'添加到c # C是标记列表,直接把p'分到对应的簇里即可 if C[p1] == -1: C[p1] = k # (15) else标记p为噪声 else: C[p1] = -1 # (16) until没有标记为unvisited的对象 return C
def test_kdtree_complex_data(): # Test that KDTree rejects complex input points (gh-9108) points = np.random.rand(10, 2).view(complex) with pytest.raises(TypeError, match="complex data"): t = KDTree(points) t = KDTree(points.real) with pytest.raises(TypeError, match="complex data"): t.query(points) with pytest.raises(TypeError, match="complex data"): t.query_ball_point(points, r=1)
def features_by_centers(base, features): try: from scipy.spatial import KDTree except ImportError: from PathScripts.kdtree import KDTree features = sorted(features, key=lambda feature: getattr(base.Shape, feature).Surface.Radius, reverse=True) coordinates = [(cylinder.Surface.Center.x, cylinder.Surface.Center.y) for cylinder in [getattr(base.Shape, feature) for feature in features]] tree = KDTree(coordinates) seen = {} by_centers = {} for n, feature in enumerate(features): if n in seen: continue seen[n] = True cylinder = getattr(base.Shape, feature) xc, yc, _ = cylinder.Surface.Center by_centers[xc, yc] = {cylinder.Surface.Radius: feature} for coord in tree.query_ball_point((xc, yc), cylinder.Surface.Radius): seen[coord] = True cylinder = getattr(base.Shape, features[coord]) by_centers[xc, yc][cylinder.Surface.Radius] = features[coord] return by_centers
class SupervoxelTree(object): def __init__(self, env, fname, transparency=0., dx=0.24, dz=1.38, pitch=0.44): """ dx -- X translation of the camera frame dz -- Z translation of the camera frame pitch -- pitch rotation of the camera frame """ self.boxes = self.read_centers(fname, dx, dz, pitch) self.tree = KDTree([box.p[:2] for box in self.boxes]) def read_centers(self, fname, dx, dz, pitch): boxes = [] with open(fname, 'r') as f: for line in f: v = map(float, line.split(',')) with env: name = 'sv%d' % len(env.GetBodies()) x = -cos(pitch) * v[1] + sin(pitch) * v[2] + dx y = v[0] z = -sin(pitch) * v[1] - cos(pitch) * v[2] + dz boxes.append(Box(env, name, dims=[0.05, 0.05, 0.05], pose=[1., 0., 0., 0., x, y, z], color='r')) return boxes def query(self, p, radius): indexes = self.tree.query_ball_point(p[0:2], radius) return [self.boxes[i] for i in indexes]
def parallelPoints(self, photos, eps=0.005, d=300): parallel = [] if len(photos) > 0: tree = KDTree([p.center for p in photos]) for point in self.excluded: index = self.tree.query(point)[1] if index > 0: m1 = Duct._lineEquation(point, self.points[index - 1])[0] if index < len(self.points) - 1: m2 = Duct._lineEquation(point, self.points[index + 1])[0] if 'm1' in locals() and 'm2' in locals(): # Pendiente de recta perpendicular en el punto m = -2 / (m1 + m2) elif 'm1' in locals(): m = -1 / m1 elif 'm2' in locals(): m = -1 / m2 n = point[0] - m * point[1] indexes = tree.query_ball_point(point, eps) fewPhotos = [photos[i] for i in indexes] for photo in fewPhotos: if (Duct.intersects((m, n), photo.corners) and Duct.distance(point, photo.center) < d): parallel.append(point) break self.parallel = parallel
def get_picking_position_for_single_item_bin(self, bin_letter, cur_camera_R, cur_camera_t, limb, colorful=True, fit=False, threshold=0.01, crop=False, bin_bound=None, perturb_xform=None, return_bin_content_cloud=False, return_normal=False): ''' physical pre-condition: place the camera to the hard-coded tote-viewing position. return (x,y,z) coordinate in global frame of position that has something to suck. ''' bin_content_cloud = self.get_current_bin_content_cloud(bin_letter, cur_camera_R, cur_camera_t, limb, colorful=colorful, fit=fit, threshold=threshold, crop=crop, bin_bound=bin_bound, perturb_xform=perturb_xform) pos = self.find_max_density_3d(bin_content_cloud) bin_content_tree = KDTree(bin_content_cloud[:,0:3]) neighbor_idxs = bin_content_tree.query_ball_point(pos, r=0.002) print 'neighbor_idxs is of type', neighbor_idxs.__class__ if len(neighbor_idxs)==0: print 'no neighbors!' normal = [-1,0,0] else: pts = bin_content_tree[neighbor_idxs,0:3] normal = get_normal(pts) return_list = [pos] if return_bin_content_cloud: return_list += [bin_content_cloud] if return_normal: return_list += [normal] if len(return_list)==1: return return_list[0] else: return return_list
def addCommercialLabelToObjectStructure(self, confidenceImage, return_sum=False): if not self.objectStructure.empty: """ IDENTIFY USING CONNECTED COMPONENT SIZE """ objAreas = self.objectStructure['area'] isCommercialSize = objAreas >= self.commercialAreaThreshold """ IDENTIFY USING PANEL PIXEL DENSITY """ neighborhoodFilter = np.ones([2 * self.commercialNeighborhoodRadius + 1] * 2) dummyImage = np.zeros(confidenceImage.shape) pixelList = np.vstack(np.array(self.objectStructure['pixelList'])).transpose() dummyImage[pixelList[0], pixelList[1]] = 1 panelNeighborhoodCount = np.real( np.fft.ifft2(np.fft.fft2(dummyImage) * np.fft.fft2(neighborhoodFilter, dummyImage.shape))) # imfilt countThreshold = int(self.commercialPanelDensityThreshold * np.prod(neighborhoodFilter.shape)) commercialPanelMap = panelNeighborhoodCount > countThreshold commercialCenters = np.vstack(np.nonzero(commercialPanelMap)).transpose() if commercialCenters.any(): panelCenters = np.vstack( (self.objectStructure['iLocation'], self.objectStructure['jLocation'])).transpose() Mdl = KDTree(commercialCenters) # Search for panels with neighborhood radius of a commercial center out = Mdl.query_ball_point(panelCenters, self.commercialNeighborhoodRadius) isCommercialDensity = [bool(i) for i in out] else: isCommercialDensity = np.zeros(objAreas.shape, dtype=bool) self.objectStructure.isCommercial = isCommercialSize & isCommercialDensity if return_sum: non_commercial = self.objectStructure.loc[self.objectStructure['isCommercial'] is False]
def filter_obj_info(OBJ_INFO): #OBJ_info.append( [ X1[i], Y1[i], # cRA10[i], cDEC10[i], # MAG1[i], # X2[j], Y2[j], # cRA20[j], cDEC20[j], # MAG2[j], SN1, SN2, NAME ] ) ### filter on X1, X2, sn orig_obj = OBJ_INFO[:] OBJ_INFO = numpy.asarray(numpy.asarray(OBJ_INFO).T[0:-1], dtype='float') print OBJ_INFO[10] print print OBJ_INFO[11] snv = OBJ_INFO[10]**2 + OBJ_INFO[11]**2 X = OBJ_INFO[0] Y = OBJ_INFO[1] tree = KDTree(numpy.asarray([X, Y]).T) keep = [] for i in range(0, len(X)): match_inds = tree.query_ball_point([X[i], Y[i]], 3.0) if numpy.all(snv[i] >= snv[match_inds]): keep.append(orig_obj[i]) print ' Original length: %d' % (len(orig_obj)) print 'Collapsed length: %d' % (len(keep)) return keep
class StarCollection: def __init__(self, list): self._list = list self._tree_array = [[star.coords.x, star.coords.y, star.coords.z] for star in list] self._tree = KDTree(self._tree_array) def get_by_id(self, id): return [x for x in self._list if x.id == id][0] def get_neighbors(self, current, min_dist, max_dist, goal): # return [x for x in self._list if min_dist**2 < current.coords.dist_squared(x.coords) < max_dist**2 and x is not current] indexes = self._tree.query_ball_point( [current.coords.x, current.coords.y, current.coords.z], max_dist) stars = [self._list[i] for i in indexes] dist_current_to_goal = current.coords.dist(goal.coords) results = [] for star in stars: dist_goal_to_star = goal.coords.dist(star.coords) dist_current_to_star = current.coords.dist(star.coords) if dist_current_to_star < min_dist: continue if dist_current_to_goal < dist_goal_to_star: continue results.append(star) return results
def filter_obj_info( OBJ_INFO ): #OBJ_info.append( [ X1[i], Y1[i], # cRA10[i], cDEC10[i], # MAG1[i], # X2[j], Y2[j], # cRA20[j], cDEC20[j], # MAG2[j], SN1, SN2, NAME ] ) ### filter on X1, X2, sn orig_obj = OBJ_INFO[:] OBJ_INFO = numpy.asarray( numpy.asarray( OBJ_INFO ).T[0:-1], dtype='float') print OBJ_INFO[10] print print OBJ_INFO[11] snv = OBJ_INFO[10]**2 + OBJ_INFO[11]**2 X = OBJ_INFO[0] Y = OBJ_INFO[1] tree = KDTree( numpy.asarray( [X,Y] ).T ) keep = [] for i in range(0, len(X)): match_inds = tree.query_ball_point( [X[i],Y[i]], 3.0 ) if numpy.all( snv[i] >= snv[ match_inds ] ): keep.append( orig_obj[i] ) print ' Original length: %d'%( len(orig_obj) ) print 'Collapsed length: %d'%( len(keep) ) return keep
def compare_image(the_image): """Return the fraction of sources found in the original image""" # pixel comparison is not good, doesn't work. Compare catalogs. if isinstance(the_image, np.ma.MaskedArray): full_algn = the_image.filled(fill_value=np.median(the_image))\ .astype('float32') else: full_algn = the_image.astype('float32') full_algn[the_image == 0] = np.median(the_image) import sep bkg = sep.Background(full_algn) thresh = 3.0 * bkg.globalrms allobjs = sep.extract(full_algn - bkg.back(), thresh) allxy = np.array([[obj['x'], obj['y']] for obj in allobjs]) from scipy.spatial import KDTree ref_coordtree = KDTree(self.star_new_pos) # Compare here srcs list with self.star_ref_pos num_sources = 0 for asrc in allxy: found_source = ref_coordtree.query_ball_point(asrc, 3) if found_source: num_sources += 1 fraction_found = float(num_sources) / float(len(allxy)) return fraction_found
def PRM(self): if self.num_points is 0: print("\nNo points sampled\n") return self.calculate_prm_parameter() # set up KD tree from scipy.spatial import KDTree T = KDTree(self.points) # calculate edges for pt1 in self.points: # idx := list of points in ball around pt1 idx = T.query_ball_point(pt1, r=self.r) for indx2 in idx: print("Debug", indx2, pt1) self.edges[pt1][self.points[indx2]] = True # print("In neighborhood of ", pt, ": ", idx) # go through edges for pt1, list2 in d.items(): for pt2, true in list2.items(): # TO DO check edge if self.connect_two_points(pt1, pt2) is True: # TO DO add to plot self.lines.append([pt1, pt2]) try: # TO DO remove itself del self.edges[pt1][pt2] except: print("Error A1") try: # TO DO remove identical one del self.edges[pt2][pt1] except: print("Error A2, not symmetric???1!?")
def explore_neighbours(train_df, test_df): r = 0.001 df = train_df[[LATITUDE, LONGITUDE, TARGET]] df = pd.get_dummies(df, columns=[TARGET]) tree = KDTree(df[[LATITUDE, LONGITUDE]].values) t = test_df[[LATITUDE, LONGITUDE]] t['res'] = tree.query_ball_point(t.values, r=r)
def purge_zoverlap(df, z_dist=2): zidxes = df.z.unique() for z_i in range(len(zidxes) - 1): subdf = df[(df.z == zidxes[z_i]) | (df.z == zidxes[z_i + 1])] yx = subdf.centroid.values yx = np.stack(yx, axis=0) tree = KDTree(yx) dclust = DBSCAN(eps=2, min_samples=2) dclust.fit(yx) skip_list = set(np.where(dclust.labels_ == -1)[0]) nomatches = [] drop_list = [] for idx, i in enumerate(yx): if idx % 10000 == 0: print(idx) if idx in skip_list: continue m = tree.query_ball_point(i, 2) m = [j for j in m if j != idx] row_query = subdf.iloc[idx] for j in m: row_match = subdf.iloc[j] if row_match.cword_idx != row_query.cword_idx: continue if row_match.npixels >= row_query.npixels: drop_list.append((idx, j)) else: drop_list.append((j, idx)) break if len(drop_list) > 0: droppers, keepers = zip(*drop_list) df.drop(index=subdf.iloc[list(droppers)].index, inplace=True) return df
def compute_matches_parallel(blocks, features, begin_idx, end_idx, matches_indices_queue): sys.setrecursionlimit(1000000) kd_tree = KDTree(features) matches_indices = list() previous_percentage = -1 pos = begin_idx while pos < end_idx: found_matches = kd_tree.query_ball_point(features[pos], config.similarity_threshold, eps=1e-6) # Exclude nearby blocks filtered_matches = filter( lambda x: euclidean_distance( np.array([blocks[x].center_row, blocks[x].center_col]), np.array([blocks[pos].center_row, blocks[pos].center_col])) > 2 * config.block_radius, found_matches) if len(filtered_matches) > 0: #Check if there are matches matches_indices.extend(filtered_matches) matches_indices.append(pos) pos += 1 percentage = int(100 * float(pos - begin_idx) / (end_idx - begin_idx)) if percentage > previous_percentage: previous_percentage = percentage print( str(' Process id: ' + str(os.getpid()) + ' - Percentage: ' + str(percentage) + '%')) matches_indices_queue.put(matches_indices)
def radius_ball_search_o3d_radii(pc, kpt_indices, radii, search_radius, input_num=None, msg=None): # radius-ball search keypoints = pc[kpt_indices] search = KDTree(pc) all_pc = [] for idx, kpt in enumerate(kpt_indices): r = search_radius * 2 indices = search.query_ball_point(pc[kpt], r) if len(indices) == 0: i = 1024 if input_num is None else input_num all_pc.append(np.zeros([i, 3], dtype=np.float32)) else: searched_thresh = search_radius * radii[indices] / 0.025 candidates = pc[indices] dists = np.sqrt( np.sum((candidates - keypoints[idx][None])**2, axis=1)) select = np.argwhere(dists <= searched_thresh).reshape(-1) subsampled = smoothing(candidates[select]) all_pc.append(subsampled) return all_pc
def radius_ball_search_o3d(pcd, kpt, search_radius, voxel_size=0.015, return_normals=False, input_num=None, name=None): # radius-ball search normals_at_kpt = None from_o3d = lambda pcd: np.asarray(pcd.points) keypoints = from_o3d(pcd)[kpt] pcd_down = pcd.voxel_down_sample(voxel_size=voxel_size) #0.015 # pcd_down = pcd pc = from_o3d(pcd_down) if return_normals: if len(pcd.normals) != len(pcd.points): raise RuntimeError('[!] The point cloud needs normals.') normals_at_kpt = np.asarray(pcd.normals)[kpt] search = KDTree(pc) results = search.query_ball_point(keypoints, search_radius) all_pc = [] for indices in results: # print(len(indices)) if len(indices) <= 1: i = 1024 if input_num is None else input_num all_pc.append(np.zeros([i,3],dtype=np.float32)) else: if input_num is not None: resample_indices, patch = pctk.uniform_resample_np(pc[indices], input_num) else: patch = pc[indices] all_pc.append(patch) if return_normals: all_pc = transform_with_normals(all_pc, normals_at_kpt) return all_pc, pcd_down, normals_at_kpt
def compare_image(the_image): """Return the fraction of sources found in the reference image""" # pixel comparison is not good, doesn't work. Compare catalogs. if isinstance(the_image, np.ma.MaskedArray): full_algn = the_image.filled(fill_value=np.median(the_image))\ .astype('float32') else: full_algn = the_image.astype('float32') # full_algn[the_image == 0] = np.median(the_image) import sep bkg = sep.Background(full_algn) thresh = 3.0 * bkg.globalrms allobjs = sep.extract(full_algn - bkg.back(), thresh) allxy = np.array([[obj['x'], obj['y']] for obj in allobjs]) from scipy.spatial import KDTree ref_coordtree = KDTree(self.star_ref_pos) # Compare here srcs list with self.star_ref_pos num_sources = 0 for asrc in allxy: found_source = ref_coordtree.query_ball_point(asrc, 3) if found_source: num_sources += 1 fraction_found = float(num_sources) / float(len(allxy)) return fraction_found
def kdtreeApproach(tripLocations, startRectangle, endRectangle): #indices is a list that should contain the indices of the trips in the tripLocations list #which start in the startRectangle region and end in the endRectangle region indices = [] trip1 = [] trip2 = [] for i,j,k,l in tripLocations: trip1.append([i,j]) trip2.append([k,l]) #print trip1 startTime = time.time() srad = sqrt((startRectangle[0][0] - startRectangle[0][1])**2 + (startRectangle[1][0] - startRectangle[1][1])**2)/float(2) erad = sqrt((endRectangle[0][0] - endRectangle[0][1])**2 + (endRectangle[1][0] - endRectangle[1][1])**2)/float(2) startx = (startRectangle[0][0] + startRectangle[0][1])/2 starty = (startRectangle[1][0] + startRectangle[1][1])/2 endx = (endRectangle[0][0] + endRectangle[0][1])/2 endy = (endRectangle[1][0] + endRectangle[1][1])/2 tree1 = KDTree(trip1) tree2 = KDTree(trip2) start = set(tree1.query_ball_point([startx,starty], srad)) end = set(tree2.query_ball_point([endx, endy], erad)) in_both = list(start.intersection(end)) print len(in_both) for i in in_both: if startRectangle[0][0]<=tripLocations[i][0] and startRectangle[0][1]>=tripLocations[i][0] and startRectangle[1][0]<=tripLocations[i][1] and startRectangle[1][1]>=tripLocations[i][1] and endRectangle[0][0]<=tripLocations[i][2] and endRectangle[0][1]>=tripLocations[i][2] and endRectangle[1][0]<=tripLocations[i][3] and endRectangle[1][1]>=tripLocations[i][3]: indices.append(i) print indices #indices.append(start) #indices.append(end) #print indices #TODO: insert your code here. You should build the kdtree and use it to query the closest # intersection for each trip # endTime = time.time() print 'The kdtree computation took', (endTime - startTime), 'seconds' return indices
def test_random_ball_vectorized(): n = 20 m = 5 T = KDTree(np.random.randn(n,m)) r = T.query_ball_point(np.random.randn(2,3,m),1) assert_equal(r.shape,(2,3)) assert_(isinstance(r[0,0],list))
def __init__(self,datos,r): self._datos = datos _arbol =KDTree(datos) _vecinos = _arbol.query_ball_point(self._datos,r,2) self._datos = datos self._links = lil_matrix((datos.shape[0], datos.shape[0])) for i in range(0,self._datos.shape[0]): N = _vecinos[i] for j in range(0,len(N)-1): for k in range(j+1,len(N)): self._links[N[j],N[k]]+=1
def isolated_index(x, y, radius=6.): logger.info('Filtering with an isolation radius: %s', radius) tree = KDTree(np.vstack([x, y]).T) index = np.zeros_like(x, dtype=bool) for i in np.arange(x.size): within = tree.query_ball_point((x[i], y[i]), radius) # There should be only one star (the target) within `radius` pixels if len(within) == 1: index[i] = True return index
def is_indexed(q, reflections, tolerance): """Check if a q vector is indexed by a set of reflections :param q: the q vector to check :param reflections: the list of reflections to find if this matches :param tolerance: the tolerance on the difference between q and the nearest reflection. :return: whether this q is indexed by this list of reflections """ kdtree = KDTree(reflections) result = kdtree.query_ball_point(q, r=tolerance) return len(result) > 0
class Fast_DBSCAN: ''' classdocs ''' def __init__(self, data, minPts=5, eps=.1): ''' Constructor ''' self.minPts = minPts self.eps = eps self.data = data #necessary for KDTree queries self.num_points = data.shape[0] self.num_features = data.shape[1] #self.dist_mat = pairwise_distances(data) #self.kd_tree = KDTree(data) #consider changing the default "leafsize" parameter self.kd_tree = KDTree(data, self.num_points/8)#self.num_points**.5) self.visited = np.zeros(self.num_points) self.noise = np.zeros(self.num_points) #what's the point of this? self.cluster_assignment = np.zeros(self.num_points) #cluster==0 --> cluster unassigned self.current_cluster = 1 def _region_query(self, this_point): # Needs to to be a list vs an array so that I can extend it while looping in "_expand_cluster" #return np.where(self.dist_mat[this_point,:] < self.eps)[0].tolist() return self.kd_tree.query_ball_point(self.data[this_point,:],self.eps) def _expand_cluster(self,base_point,neighbors): self.cluster_assignment[base_point] = self.current_cluster #needs it's own case since it was already visited for this_point in neighbors: if not self.visited[this_point]: self.visited[this_point] = 1 new_neighbors = self._region_query(this_point) if len(new_neighbors) >= self.minPts: neighbors += new_neighbors if self.cluster_assignment[this_point] == 0: self.cluster_assignment[this_point] = self.current_cluster def cluster(self): for this_point in range(self.num_points): if not self.visited[this_point]: self.visited[this_point] = 1 neighbors = self._region_query(this_point) if len(neighbors) < self.minPts: self.noise[this_point] = 1 else: self._expand_cluster(this_point,neighbors) self.current_cluster += 1
def test_find_sources(self): srcs = astroalign.find_sources(self.image_ref) from scipy.spatial import KDTree ref_coordtree = KDTree(self.star_ref_pos) # Compare here srcs list with self.star_ref_pos num_sources = 0 for asrc in srcs: found_source = ref_coordtree.query_ball_point(asrc, 3) if found_source: num_sources += 1 fraction_found = float(num_sources) / float(len(srcs)) self.assertGreater(fraction_found, 0.85)
class SupervoxelTree(PointSet): def __init__(self, env, fname): super(SupervoxelTree, self).__init__(env, fname) dims = [0.05] * 3 if 'super' in fname else [0.02] * 3 self.boxes = [] for (x, y, z) in self.points: name = 'sv%d' % len(self.env.GetBodies()) self.boxes.append(Box(self.env, name, dims=dims, pose=[1., 0., 0., 0., x, y, z], color='r')) self.tree = KDTree([box.p[:2] for box in self.boxes]) def query(self, p, radius): indexes = self.tree.query_ball_point(p[0:2], radius) return [self.boxes[i] for i in indexes]
def kdtreeApproach(tripLocations, startRectangle, endRectangle): #indices is a list that should contain the indices of the trips in the tripLocations list #which start in the startRectangle region and end in the endRectangle region indices = [] startTime = time.time() #TODO: insert your code here. You should build the kdtree and use it to query the closest # intersection for each trip startpoint = [] endpoint = [] for item in tripLocations: startpoint.append([item[0],item[1]]) endpoint.append([item[2],item[3]]) #print start startTime = time.time() rs = ((startRectangle[0][0] - startRectangle[0][1])**2 + (startRectangle[1][0] - startRectangle[1][1])**2)**0.5/float(2) re = ((endRectangle[0][0] - endRectangle[0][1])**2 + (endRectangle[1][0] - endRectangle[1][1])**2)**0.5/float(2) os = [(startRectangle[0][0] + startRectangle[0][1])/2,(startRectangle[1][0] + startRectangle[1][1])/2] oe = [(endRectangle[0][0] + endRectangle[0][1])/2,(endRectangle[1][0] + endRectangle[1][1])/2] Stree = KDTree(startpoint) Etree = KDTree(endpoint) startpoint = set(Stree.query_ball_point(os, rs)) endpoint = set(Etree.query_ball_point(oe, re)) s_e = list(startpoint.intersection(endpoint)) for point in s_e: if startRectangle[0][1]>=tripLocations[point][0]>=startRectangle[0][0]and startRectangle[1][1]>=tripLocations[point][1]>=startRectangle[1][0] and startRectangle[0][1]>=tripLocations[point][2]>=startRectangle[0][0]and startRectangle[1][1]>=tripLocations[point][3]>=startRectangle[1][0]: indices.append(i) # endTime = time.time() print 'The kdtree computation took', (endTime - startTime), 'seconds' return indices
def kdtreeApproach(intersections, tripLocations, distanceThreshold): #counts is a dictionary that has as keys the intersection index in the intersections list #and as values the number of trips in the tripLocation list which are within a distance of #distanceThreshold from the intersection counts = {} startTime = time.time() #TODO: insert your code here. You should build the kdtree and use it to query the closest # intersection for each trip tree = KDTree(intersections) pointlist = tree.query_ball_point(tripLocations,distanceThreshold) for i in xrange(len(pointlist)): counts[i] = len(pointlist[i]) # endTime = time.time() print 'The kdtree computation took', (endTime - startTime), 'seconds' return counts
def normal_correction(approachrays,r=0.05): T = KDTree(approachrays[:,0:3]) rays = [] N = len(approachrays) t=time.time() initialTime = t for i in range(0,len(approachrays)): rays.append(approachrays[i]) idx = T.query_ball_point(approachrays[i,0:3],r) for ind in idx: if dot(approachrays[i,3:6],approachrays[ind,3:6])<0: approachrays[ind,3:6]=-approachrays[ind,3:6] if i%10000==0: print str(i)+'/'+str(N) print 'elapsed time= '+str(time.time()-t) t=time.time() return rays
def subtract_model(model_cloud, scene_cloud=None, scene_tree=None, threshold=0.01): ''' subtract model from scene. return indices of scene points that should be kept (beyond a threshold of any points in the model) ''' assert scene_cloud is not None or scene_tree is not None, 'Either scene_model or scene_tree must be provided' model_cloud = np.array(model_cloud) if scene_tree is None: scene_cloud = np.array(scene_cloud) assert scene_cloud.shape[1]==3, 'scene_cloud dimension must be Nx3' print "Making KDTree with %d points"%scene_cloud.shape[0] scene_tree = KDTree(scene_cloud) print "Querying neighbors within %f of %d points"%(threshold, model_cloud.shape[0]) idxs = scene_tree.query_ball_point(model_cloud, threshold) print "Done" idxs = [i for group in idxs for i in group] # flattened indices discard_idxs = set(idxs) all_idxs = set(range(scene_cloud.shape[0])) keep_idxs = all_idxs - discard_idxs return list(keep_idxs)
def find_nearest_hkl(qs, reflections, hkls, tolerance): """Find the nearest HKL value that indexes each of the q vectors :param qs: the q vectors for satellite peaks to index :param reflections: the list of reflections to search :param hkls: the list of hkl values mapping to each reflection :param tolerance: the tolerance on the difference between a given q and the nearest reflection :return: MxN matrix of integers indexing the q vectors. Unindexed vectors will be returned as all zeros. """ kdtree = KDTree(reflections) final_indexing = [] for i, q in enumerate(qs): result = kdtree.query_ball_point(q, r=tolerance) if len(result) > 0: smallest = sort_vectors_by_norm(hkls[result]) final_indexing.append(smallest[0]) else: final_indexing.append(np.zeros_like(hkls.shape[1])) return np.vstack(final_indexing)
def kdtreeApproach(intersections, tripLocations, distanceThreshold): # counts is a dictionary that has as keys the intersection index in the intersections list # and as values the number of trips in the tripLocation list which are within a distance of # distanceThreshold from the intersection counts = {} startTime = time.time() # TODO: insert your code here. You should build the kdtree and use it to query the closest # intersection for each trip intersections = KDTree(intersections) idx_start = intersections.query_ball_point(tripLocations, distanceThreshold) for item in idx_start: for i in item: if i in counts: counts[i] += 1 else: counts[i] = 1 # endTime = time.time() print "The kdtree computation took", (endTime - startTime), "seconds" return counts
def match( xv1, xv2 ): ##-- a cross-correlation matcher that assumes ##-- that there is no rotation or scale variation ##-- between images. ilist = ifilter_mag( itertools.product( xv1, xv2 ) ) dxy = map( lambda x: [ x[0][0] - x[1][0], x[0][1] - x[1][1] ], ilist ) dxy = numpy.asarray( dxy ).T tree = KDTree( dxy.T ) tree_xy = KDTree( xv1[:,0:2] ) #print 'trees built - correlating...' N = map( lambda x: len( tree.query_ball_point( x, 5.0 ) ), dxy.T ) x = numpy.argmax(N) xv1ret, xv2ret = [],[] for k in xv2[:,0:2]: r, i = tree_xy.query( [ k[0] + dxy.T[x][0], k[1] + dxy.T[x][1] ] ) if r < 5: xv1ret.append( xv1[i][0:2] ) xv2ret.append( k ) return numpy.asarray(xv1ret), numpy.asarray(xv2ret)
sums = sums + sum(diff,0) index.remove(index[0]) return sums rR = concatenate((reachableRays,AllreachableRays)) T = KDTree(rR[:,0:3]) iI = concatenate((iksolList,AlliksolList)) initialsols = [] for i in iI: initialsols.append(i[0]) joints = [3,4] stopcriteria(T,initialsols,joints) while True: Index = range(0,len(iI)) while len(Index)>0: k = random.choice(Index) idxs = T.query_ball_point(reachableRays[k,0:3],r=0.05)[1:] possibleSols = iI[k] diffs = [] for sol in possibleSols: diff = [] for idx in idxs: diff.append((sol-initialsols[idx])**2) diffs.append(diff) initialsols[k]=possibleSols[criteria(diffs,joints)] Index.remove(k) sums = stopcriteria(T,initialsols,joints) print(sums)
class Grid(dict): """ """ def __init__(self, spaxels, *args, **keywords): lines = spaxels[:,3].astype(int) fibers = spaxels[:,2].astype(int) x_pos = spaxels[:,0] y_pos = spaxels[:,1] neighbours = zip(x_pos.ravel(), y_pos.ravel()) self.neighbours = KDTree(neighbours) diccionario= dict((k, []) for k in np.unique(fibers)) for fib in np.unique(fibers): aux = {} aux['real_fiber'] = fib aux['trace'] = lines[fibers==fib].tolist() aux['x_pos'] = x_pos[fibers==fib].tolist() aux['y_pos'] = y_pos[fibers==fib].tolist() diccionario[fib] = aux super(Grid, self).__init__(diccionario) # matriz = self.matrix_generation(spaxels) # matriz = self.delete_ceroes(matriz) # # matriz = np.flipud(matriz) # # self.default = matriz def __getitem__(self, key): return super(Grid, self).get(key, self.default) def matrix_generation(self, spaxels): spaxels = spaxels[np.where((spaxels[:,0] >= -20) & (spaxels[:,0] <=20) & (spaxels[:,1] >= -20) & (spaxels[:,1] <=20))] ox = np.around(spaxels[:,0], decimals=3) oy = np.around(spaxels[:,1], decimals=3) fiber_number = spaxels[:,3].astype(int) paso_ox = 0.465 paso_oy = 0.268 col_x = np.around(ox/paso_ox, decimals=1) + 13 col_y = np.around(oy/paso_oy, decimals=1) + 21 #20 si usamos LCB_spaxel_centers.dat col_x = col_x.astype(int) col_y = col_y.astype(int) coordenadas = zip(col_x, col_y, fiber_number) matriz = np.zeros((42,27)) for elem in coordenadas: matriz[elem[1],elem[0]] = elem[2] return matriz def delete_ceroes(self, matriz): # pintar_matriz(matriz) nueva_matriz = np.zeros((22,27)) for indice, row in enumerate(matriz): nueva_fila = row if indice not in [matriz.shape[0]-1,0]: if indice%2 ==0: nueva_fila = row + matriz[indice-1,:] nueva_matriz[indice/2,:] = nueva_fila else: nueva_matriz[(indice//2)+1,:] = nueva_fila elif indice in [0]: nueva_matriz[indice,:] = nueva_fila else: nueva_matriz[-1,:] = nueva_fila for cont in xrange(1,27,2): nueva_matriz[:,cont] = np.roll(nueva_matriz[:,cont].T, 1, axis=0).T # nueva_matriz = np.delete(nueva_matriz, -1, 0) return nueva_matriz def dump_matriz(self, matriz): import pandas df = pandas.DataFrame(matriz) # Papel # df = pandas.DataFrame(matriz) print(df.to_string()) def find(self, item): """ A fast lookup by value implementation """ for pos, value in self.items(): if item == value or item in value: return pos return None def get_from_trace(self, trace): for pos, value in self.items(): if trace in value['trace']: indice = np.where(np.in1d(value['trace'], trace)==True)[0][0] bun = value['real_fiber'] x_pos = value['x_pos'][indice] y_pos = value['y_pos'][indice] return trace, bun, x_pos, y_pos return None def get_neighbours(self, points, radius=0.5): ind = np.array(self.neighbours.query_ball_point(points, r=radius))+1 # We add +1 because .dat file starts in 1 return ind def get_fiber(self, points): ind = np.array(self.neighbours.query(points, k=1))[1] return int(ind)
i = 0 for x, y, r in list(map(lambda x: tuple(map(lambda x: int(x), x.split())), open(sys.argv[1]).read().split("\n")[1:-1])): if not (x, y) in points or points[(x, y)][0] <= r: points[(x, y)] = (r, i) i += 1 #def dist(a, b): # return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 result = [] tree = KDTree(list(points.keys())) for x, y in sorted(list(points.keys()), key = lambda x: -points[x][0]): if (x, y) in blown: continue result.append(str(points[(x, y)][1])) blowing = [] blowing.append((x, y)) while len(blowing): x, y = blowing.pop() if (x, y) in blown: continue blown.add((x, y)) #blowing.extend(filter(lambda z: not z in blown and dist(z, (x, y)) <= points[(x, y)][0] ** 2, list(points.keys()))) blowing.extend(map(lambda x: tree.data[x], filter(lambda x: not x in blown, tree.query_ball_point((x, y), points[(x, y)][0])))) print(' '.join(result))
# features which are created when the community goes # down a road away from the main group. has_tail = True # guilty until proven innocent while has_tail: hull_outline = LineString(shape.exterior) characteristic_size = math.sqrt(shape.area) outline_pts = hull_outline.coords[:] # find the projections along the line lenght for each point projections = [hull_outline.project(Point(x)) for x in outline_pts] # so we can find neighbors close to each other outline_kdtree = KDTree(outline_pts) biggest_tail = None for idx, point in enumerate(outline_pts): proj = projections[idx] # find nearest neighbors, within 5% of total size neighbors = outline_kdtree.query_ball_point( point, r=characteristic_size * args.tail_pinch) for neighbor in neighbors: neighbor_point = outline_pts[neighbor] if neighbor_point == point: continue neighbor_proj = projections[neighbor] # Find distance along edge in both directions. # Must wrap around at proj = 0. # The smaller of the two is the relevant one. distance_along_edge = abs(neighbor_proj - proj) distance_along_edge = min( distance_along_edge, hull_outline.length - distance_along_edge ) distance_as_crow_flies = math.hypot( point[0] - neighbor_point[0],
print "KDTree %d lookups:\t%g" % (r, time.time()-t) del w t = time.time() w = T2.query(queries) print "cKDTree %d lookups:\t%g" % (r, time.time()-t) del w T3 = cKDTree(data,leafsize=n) t = time.time() w = T3.query(queries) print "flat cKDTree %d lookups:\t%g" % (r, time.time()-t) del w t = time.time() w1 = T1.query_ball_point(queries, 0.2) print "KDTree %d ball lookups:\t%g" % (r, time.time()-t) t = time.time() w2 = T2.query_ball_point(queries, 0.2) print "cKDTree %d ball lookups:\t%g" % (r, time.time()-t) t = time.time() w3 = T3.query_ball_point(queries, 0.2) print "flat cKDTree %d ball lookups:\t%g" % (r, time.time()-t) all_good = True for a, b in zip(w1, w2): if sorted(a) != sorted(b): all_good = False for a, b in zip(w1, w3):