def build(self): logger.debug( "Building maps for conversion between real and image space") self.imagetree = {} self.imagemap = {} self.realtree = KDTree([x.realcoords for x in self.patches]) self.realmapping = {} for num, patch in enumerate(self.patches): if num % 1000 == 0: logger.debug("Built maps for {0} of {1} patches".format( num, len(self.patches))) resp = {} for _, projection in self.projections.items(): inimage = patch.project(projection) if projection.id not in self.imagetree: self.imagetree[projection.id] = [] self.imagemap[projection.id] = {} self.imagetree[projection.id].append(inimage) self.imagemap[projection.id][tuple(inimage)] = patch.realcoords resp[projection.id] = inimage self.realmapping[tuple(patch.realcoords)] = resp logger.debug("Done building maps for {0} patches".format( len(self.patches))) logger.debug("Building image KD tree") for key, imagecoords in self.imagetree.items(): self.imagetree[key] = KDTree(imagecoords)
def calc_constriction_site(pdbid: str): """Here the assumption is that the constriction site is unique and so is then the KDTree min then.""" pdbid = pdbid.upper() chainL22: str = log.get_struct(pdbid)['uL22'].values[0] chainL4: str = log.get_struct(pdbid)['uL4'].values[0] if ',' in chainL22: chainL22 = chainL22.split(',')[0] if ',' in chainL4: chainL4 = chainL4.split(',')[0] struct = fetchStructure(pdbid)[0] L4: Chain = struct[chainL4] L22: Chain = struct[chainL22] l4res = [*L4.get_atoms()] l22res = [*L22.get_atoms()] kdtreeonl4 = KDTree([*map(lambda x: x.get_coord(), l4res)]) kdtreeonl22 = KDTree([*map(lambda x: x.get_coord(), l22res)]) nbridin22: Atom = None nbridin4: Atom = None atom: Atom distances_indexes = kdtreeonl4.query( [*map(lambda x: x.get_coord(), l22res)]) dist = 99999999 for x in zip([*distances_indexes[0]], [*distances_indexes[1]]): if x[0] < dist: dist = x[0] nbridin4 = x[1] distances_indexes = kdtreeonl22.query( [*map(lambda x: x.get_coord(), l4res)]) dist = 99999999 for x in zip([*distances_indexes[0]], [*distances_indexes[1]]): if x[0] < dist: dist = x[0] nbridin22 = x[1] l4atomcord = l4res[nbridin4].get_coord() l22atomcord = l22res[nbridin22].get_coord() centerline = np.mean([l4atomcord, l22atomcord], axis=0) residueInL4: Residue = l4res[nbridin4].get_parent() residueInL22: Residue = l22res[nbridin22].get_parent() print( """{} {} in chain {}({}) is closest to {} {} in chain{}({}).\n Centerline:{}""" .format(residueInL22.get_resname(), residueInL22.get_id()[1], chainL22, 'uL22', residueInL4.get_resname(), residueInL4.get_id()[1], chainL4, 'uL4', centerline)) return {"uL22": residueInL22, "uL4": residueInL4, "centerline": centerline}
def estimatemeans(self): feature_positions = [] for i in range(len(self.edges[0]) - 1): for j in range(len(self.edges[1]) - 1): if len(self.points[i, j]) > 0: feature_positions.append([i, j]) tree = KDTree(feature_positions) for i in range(len(self.edges[0]) - 1): for j in range(len(self.edges[1]) - 1): if self.points[i, j] == []: radius, neighbour = tree.query(x=np.array([i, j]), k=1) if radius > feature_resolution[0] / 10: self.estmeans[i, j] = np.nan self.means[i, j] = np.nan else: nearby = tree.data[tree.query_ball_point( x=np.array([i, j]), r=radius)] #print([self.trumeans[i] for i in nearby]) estmean = np.nanmean( [self.trumeans[i.astype(int)] for i in nearby]) self.estmeans[i, j] = estmean self.means[i, j] = estmean else: self.means[i, j] = self.trumeans[i, j]
def __init__(self, ax, basemap, lons2d, lats2d, ncVarDict, times, start_date, end_date): """ Plots a vertical profile at the point nearest to the clicked one :type ax: Axes """ assert isinstance(ax, Axes) self.basemap = basemap assert isinstance(self.basemap, Basemap) self.lons_flat = lons2d.flatten() self.lats_flat = lats2d.flatten() self.ncVarDict = ncVarDict self.lons2d = lons2d self.lats2d = lats2d self.counter = 0 self.ax = ax x, y, z = lat_lon.lon_lat_to_cartesian(lons2d.flatten(), lats2d.flatten()) self.kdtree = KDTree(list(zip(x, y, z))) self.sel_time_indices = np.where( [start_date <= t <= end_date for t in times])[0] self.times = times[self.sel_time_indices] ax.figure.canvas.mpl_connect("button_press_event", self)
def add_clusters(df_cells, neighbor_dist=50): """Assigns -1 to clusters with only one cell. """ from scipy.spatial.kdtree import KDTree import networkx as nx x = df_cells[GLOBAL_X] + df_cells[POSITION_J] y = df_cells[GLOBAL_Y] + df_cells[POSITION_I] barcodes = df_cells[BARCODE_0] barcodes = np.array(barcodes) kdt = KDTree(np.array([x, y]).T) num_cells = len(df_cells) print('searching for clusters among %d cells' % num_cells) pairs = kdt.query_pairs(neighbor_dist) pairs = np.array(list(pairs)) x = barcodes[pairs] y = x[:, 0] == x[:, 1] G = nx.Graph() G.add_edges_from(pairs[y]) clusters = list(nx.connected_components(G)) cluster_index = np.zeros(num_cells, dtype=int) - 1 for i, c in enumerate(clusters): cluster_index[list(c)] = i df_cells[CLUSTER] = cluster_index return df_cells
def solvent_surface(molecule, num_points_per_atom=140, delta=0.1): radii = get_solvent_radii(molecule.atomic_numbers) N = len(molecule) grid = load_grid_num_points(num_points_per_atom) num_points_per_atom = grid.shape[0] axes = molecule.axes() positions = molecule.positions_in_molecular_axis_frame() surface = np.empty((N * num_points_per_atom, 4)) atom_idx = np.empty(N * num_points_per_atom, dtype=np.int32) for i in range(N): r = radii[i] + delta l, u = num_points_per_atom * i, num_points_per_atom * (i + 1) surface[l:u, 3] = grid[:, 3] * 4 * np.pi * radii[i] * radii[i] surface[l:u, :3] = grid[:, :3] * r surface[l:u, :3] += positions[i, :] atom_idx[l:u] = i mask = np.ones_like(atom_idx, dtype=bool) tree = KDTree(surface[:, :3]) for i in range(N): p = positions[i] radius = radii[i] + delta idxs = tree.query_ball_point(p, radius - 1e-12) mask[idxs] = False surface = surface[mask, :] surface[:, :3] = (np.dot(surface[:, :3], axes) + molecule.center_of_mass[np.newaxis, :]) atom_idx = atom_idx[mask] return surface
def locally_extreme_points(coords, data, neighbourhood, lookfor='max', p_norm=2.): # Description # Find local maxima of points in a pointcloud. Ties result in both points passing through the filter. # # Not to be used for high-dimensional data. It will be slow. # # coords: A shape (n_points, n_dims) array of point locations # data: A shape (n_points, ) vector of point values # neighbourhood: The (scalar) size of the neighbourhood in which to search. # lookfor: Either 'max', or 'min', depending on whether you want local maxima or minima # p_norm: The p-norm to use for measuring distance (e.g. 1=Manhattan, 2=Euclidian) # # returns # filtered_coords: The coordinates of locally extreme points # filtered_data: The values of these points assert coords.shape[0] == data.shape[ 0], 'You must have one coordinate per data point' extreme_fcn = {'min': np.min, 'max': np.max}[lookfor] kdtree = KDTree(coords) neighbours = kdtree.query_ball_tree(kdtree, r=neighbourhood, p=p_norm) i_am_extreme = [ data[i] == extreme_fcn(data[n]) for i, n in enumerate(neighbours) ] extrema, = np.nonzero( i_am_extreme) # This line just saves time on indexing return coords[extrema], data[extrema]
def __init__(self, ax, basemap, tmin_3d, tmax_3d, lons2d, lats2d, levelheights=None): """ Plots a vertical profile at the point nearest to the clicked one :type ax: Axes """ assert isinstance(ax, Axes) self.basemap = basemap assert isinstance(self.basemap, Basemap) self.tmin_3d = tmin_3d self.tmax_3d = tmax_3d self.lons2d = lons2d self.lats2d = lats2d self.T0 = 273.15 self.lons_flat = lons2d.flatten() self.lats_flat = lats2d.flatten() self.level_heights = levelheights self.counter = 0 self.ax = ax x, y, z = lat_lon.lon_lat_to_cartesian(lons2d.flatten(), lats2d.flatten()) self.kdtree = KDTree(list(zip(x, y, z))) ax.figure.canvas.mpl_connect("button_press_event", self) pass
def _init_kd_tree(self): """ Init KDTree used for interpolation """ x0, y0, z0 = lat_lon.lon_lat_to_cartesian(self.lon2d.flatten(), self.lat2d.flatten()) self.kdtree = KDTree(list(zip(x0, y0, z0)))
def updatemeans(self, newpoint, model, fr): '''For use when self = meanmap Calculates the mean observed value for each niche and populates the current map with those mean values. ''' #Update the new point first new_index = tuple(self.nichefinder(newpoint[2])) i, j = new_index[0], new_index[1] obsmean = self.trumeans[i, j] #estmean = self.estimatemean( i,j) #self.estmeans[ i,j ] = estmean #self.means[i,j] = np.mean([estmean, obsmean]) self.means[i, j] = obsmean ## Now update points that will be affected by the current point feature_positions = [] for i in range(len(self.edges[0]) - 1): for j in range(len(self.edges[1]) - 1): if self.points[i, j] != []: feature_positions.append([i, j]) #print(feature_positions) tree = KDTree(feature_positions) checkindex = [new_index] # Explore all the points around it. end = False checked = [] while not end: newcheck = [] for index in checkindex: newcheck, checked = self.pointsearch( model, index[0], index[1], fr, tree, newpoint, checked) checkindex += newcheck if len(newcheck) == 0: end = True
def compute_graph(samples, weights=None, p_norm=2, max_degree=10, max_distance=INF, approximate_eps=0.): vertices = list(range(len(samples))) edges = set() if not vertices: return vertices, edges if weights is None: weights = np.ones(len(samples[0])) embed_fn = get_embed_fn(weights) embedded = list(map(embed_fn, samples)) kd_tree = KDTree(embedded) for v1 in vertices: # TODO: could dynamically compute distances distances, neighbors = kd_tree.query(embedded[v1], k=max_degree + 1, eps=approximate_eps, p=p_norm, distance_upper_bound=max_distance) for d, v2 in zip(distances, neighbors): if (d < max_distance) and (v1 != v2): edges.update([(v1, v2), (v2, v1)]) # print(time.time() - start_time, len(edges), float(len(edges))/len(samples)) return vertices, edges
def nearest_neighbor_fill(ball, mask): # Fill flag holes with nearest neighbor ff = np.asarray(ball).copy() x, y = np.mgrid[0:ff.shape[0], 0:ff.shape[1]] xygood = np.array((x[~mask], y[~mask])).T xybad = np.array((x[mask], y[mask])).T ff[mask] = ff[~mask][KDTree(xygood).query(xybad)[1]] return Image.fromarray(ff)
def build_tree(laser_file): # build a KD-tree over the point clouds to find nearest neighbors. # input: the point cloud file that is going to be searched. # output: the tree file that includes indices ready to be searched. data_ready = np.vstack([laser_file.X, laser_file.Y, laser_file.Z]).transpose() tree = KDTree(data_ready) return tree
def warp_keypoints(self, keypoints, grid_unnormalized): from scipy.spatial.kdtree import KDTree warp_grid = grid_unnormalized.reshape(-1, 2) regular_grid = self.grid_pixels_unnormalized.reshape(-1, 2) kd = KDTree(warp_grid) dists, idxs = kd.query(keypoints) new_keypoints = regular_grid[idxs] return new_keypoints
def compute_minimax_distance(path1, path2): overall_distance = 0. for path, other in permutations([path1, path2]): tree = KDTree(other) for q1 in path: #closest_distance = min(get_distance(q1, q2) for q2 in other) closest_distance, closest_index = tree.query(q1, k=1, eps=0.) overall_distance = max(overall_distance, closest_distance) return overall_distance
def make_adjacency_matrix(data, k): kt = KDTree(data) out = zeros((len(data),len(data))) for i, points in enumerate(data): distance, neighbors = kt.query_ball_point(points, k) for j in neighbors[1:]: out[i][j] = 1; out[j][i] = 1; #to ensure symmetry, one can imagine the the nearest neighbors is not necessarily associative return out
def __init__(self, filepath): start = time.time() self.file = File(filepath, mode="r") self.scale = self.file.header.scale[0] self.offset = self.file.header.offset[0] self.tree = KDTree( np.vstack([self.file.x, self.file.y, self.file.z]).transpose()) self.time = self.file.header.get_date() end = time.time() - start print("Time Elapsed: {}".format(end))
def mutual_knn(points, n=10, distance=radial_kernel()): knn = {} kt = KDTree(points) for i, point in enumerate(points): # cannot use euclidean distance directly for neighbour in kt.query(point, n + 1)[1]: if i != neighbour: knn.setdefault(i, []).append( (distance(point, points[neighbour]), neighbour)) return knn
def get_kd_tree(self): """ :rtype : KDTree for interpolation purposes """ if self._kdtree is None: x, y, z = lat_lon.lon_lat_to_cartesian(self.lons.flatten(), self.lats.flatten()) self._kdtree = KDTree(list(zip(x, y, z))) return self._kdtree
def __init__(self, xml_annot): self._data = [] self.ref_by_pages = {} idx = 0 for annotation in xml_annot.findall( ".//ANNOTATION/ACTION[@type='goto']/.."): dest = annotation.find("ACTION/DEST") try: page_dest = dest.get("page") x_dest = dest.get("x") y_dest = dest.get("y") except: # Maybe change that, see how much we loose continue page_num = annotation.get("pagenum") page_n = int(page_num) quadpoints = annotation.findall("QUADPOINTS/QUADRILATERAL/POINT") min_h, min_v, max_h, max_v = None, None, None, None for point in quadpoints: h, v = float(point.get("HPOS")), float(point.get("VPOS")) if min_h is None: min_h, max_h = h, h min_v, max_v = v, v else: min_h = min(min_h, h) max_h = max(max_h, h) min_v = min(min_v, v) max_v = max(max_v, v) self._data.append({ "page_dest": page_dest, "x_dest": x_dest, "y_dest": y_dest, "bbx": BBX(page_num, min_h, min_v, max_h, max_v) }) """ Add a new point in the QDTree of the page *page_n* """ if page_n not in self.ref_by_pages: self.ref_by_pages[page_n] = {"idx": [], "pos": []} self.ref_by_pages[page_n]["idx"].append(idx) self.ref_by_pages[page_n]["pos"].append([(min_v + max_v) / 2, (min_h + max_h) / 2]) idx += 1 # Convert list to QDTree for page in self.ref_by_pages: self.ref_by_pages[page]["tree"] = KDTree( self.ref_by_pages[page]["pos"])
def waypoints_cb(self, msg): # self.waypoints = waypoints # rospy.loginfo("TL_DETECTOR: waypoints_cb called...") self.base_waypoints = msg.waypoints if self.base_waypoints is not None: # populate the Lane msg header as well (not really required, but to be consistent) # self.lane_msg_header = msg.header # get the 2d (x, y) waypoints self.base_waypoints_2d = [[wp.pose.pose.position.x, wp.pose.pose.position.y] for wp in self.base_waypoints] # build a KDTree for efficient search of nearest waypoint self.base_waypoints_kdtree = KDTree(self.base_waypoints_2d)
def interpolate_data_to_model_grid(self, model_lons_2d, model_lats_2d, data_obs): x0, y0, z0 = lat_lon.lon_lat_to_cartesian(model_lons_2d.flatten(), model_lats_2d.flatten()) x, y, z = lat_lon.lon_lat_to_cartesian(self.lons2d.flatten(), self.lats2d.flatten()) if self.ktree is None: self.ktree = KDTree(list(zip(x, y, z))) d, i = self.ktree.query(list(zip(x0, y0, z0))) return data_obs.flatten()[i].reshape(model_lons_2d.shape)
def getKDTDict(pdict): kdtdict = {} for l, li in pdict.items(): pnts1 = [lin.p1 for lin in li] #pnts2 = [lin.p2 for lin in li] pnts = [[p.x, p.y] for p in pnts1] #pnts.extend( [ [p.x, p.y] for p in pnts2 ] ) kdtdict[l] = KDTree(pnts) return kdtdict
def __init__(self, data): """Find neigbors Args: data (pd.DataFrame or dict): data with at columns id,pos,and eventually range """ if data is not None: self.data = data # Safety checks assert isinstance(data, pd.DataFrame) assert "pos" in data.columns # Initialize KDTree finder from scipy self.tree = KDTree(self.data["pos"].tolist())
def _merge(self, roi): points = [e[2] for e in roi] tree = KDTree(points) pp = tree.query_pairs(4) #pp = sorted( pp , key=lambda e: max(roi[e[0]] , roi[e[1]]), reverse=True) skips = set() for i, j in pp: if self.Debug: print '%s(%.2f),%s(%.2f)' % (roi[i][0], roi[i][1], roi[j][0], roi[j][1]) if i in skips or j in skips: continue skip = i if roi[i][1] < roi[j][1] else j skips.add(skip) rs = (e for i, e in enumerate(roi) if not i in skips) rs = sorted(rs, key=lambda e: e[1])[-4:] return sorted(rs, key=lambda e: e[2][1])
def test_evol_for_point(): lon = -100 lat = 60 path = "/home/huziy/skynet1_rech3/cordex/for_Samira/b1/tmp_era40_b1.nc" ds = Dataset(path) data = ds.variables["tmp"][:] years = ds.variables["year"][:] coord_file = "/skynet1_rech3/huziy/cordex/CORDEX_DIAG/NorthAmerica_0.44deg_CanESM_B1" coord_file = os.path.join(coord_file, "pmNorthAmerica_0.44deg_CanHisto_B1_195801_moyenne") #coord_file = os.path.join(data_folder, "pmNorthAmerica_0.44deg_ERA40-Int2_195801_moyenne") b, lons2d, lats2d = draw_regions.get_basemap_and_coords(file_path=coord_file) sel_lons = [lon] sel_lats = [lat] xo,yo,zo = lat_lon.lon_lat_to_cartesian(sel_lons, sel_lats) xi, yi, zi = lat_lon.lon_lat_to_cartesian(lons2d.flatten(), lats2d.flatten()) ktree = KDTree(list(zip(xi,yi,zi))) dists, indexes = ktree.query(list(zip(xo,yo,zo))) print(len(indexes)) print(indexes) idx = indexes[0] import matplotlib.pyplot as plt plt.figure() data_to_show = [] for i, y in enumerate(years): data_to_show.append(data[i,:,:].flatten()[idx]) plt.plot(years, data_to_show, "-s", lw = 3) plt.grid() plt.show() pass
def add_clusters(df_cells, barcode_col=BARCODE_0, radius=50, verbose=True, ij=(POSITION_I, POSITION_J)): """Assigns -1 to clusters with only one cell. """ from scipy.spatial.kdtree import KDTree import networkx as nx I, J = ij x = df_cells[GLOBAL_X] + df_cells[J] y = df_cells[GLOBAL_Y] + df_cells[I] barcodes = df_cells[barcode_col] barcodes = np.array(barcodes) kdt = KDTree(np.array([x, y]).T) num_cells = len(df_cells) if verbose: message = 'searching for clusters among {} {} objects' print(message.format(num_cells, barcode_col)) pairs = kdt.query_pairs(radius) pairs = np.array(list(pairs)) x = barcodes[pairs] y = x[:, 0] == x[:, 1] G = nx.Graph() G.add_edges_from(pairs[y]) clusters = list(nx.connected_components(G)) cluster_index = np.zeros(num_cells, dtype=int) - 1 for i, c in enumerate(clusters): cluster_index[list(c)] = i df_cells = df_cells.copy() df_cells[CLUSTER] = cluster_index df_cells[CLUSTER_SIZE] = ( df_cells.groupby(CLUSTER)[barcode_col].transform('size')) df_cells.loc[df_cells[CLUSTER] == -1, CLUSTER_SIZE] = 1 return df_cells
def locally_extreme_points(coords, data, radius, smoothing_radius=None, min_neighbourhood_size=-1, lookfor='max', p_norm=2): ''' Find local maxima of points in a pointcloud. Ties result in both points passing through the filter. Not to be used for high-dimensional data. It will be slow. coords: A shape (n_points, n_dims) array of point locations data: A shape (n_points, ) vector of point values radius: The (scalar) size of the neighbourhood in which to search. min_neighbourhood_size: Extrema whose neighbourhood has less than min_neighbourhood_size points are discarded lookfor: Either 'max', or 'min', depending on whether you want local maxima or minima p_norm: The p-norm to use for measuring distance (e.g. 1=Manhattan, 2=Euclidian) returns filtered_coords: The coordinates of locally extreme points filtered_data: The values of these points ''' assert coords.shape[0] == data.shape[ 0], 'You must have one coordinate per data point' extreme_fcn = {'min': np.min, 'max': np.max}[lookfor] kdtree = KDTree(coords) if smoothing_radius is not None: smoothing_neighbours = kdtree.query_ball_tree(kdtree, r=smoothing_radius, p=p_norm) smooth_data = np.array( [np.mean(data[n]) for n in smoothing_neighbours]) else: smooth_data = data neighbours = kdtree.query_ball_tree(kdtree, r=radius, p=p_norm) i_am_extreme = [ (np.nonzero(np.equal(n, i))[0][0] == np.argmax(smooth_data[n])) & (len(n) > min_neighbourhood_size) for i, n in enumerate(neighbours) ] extrema, = np.nonzero( i_am_extreme) # This line just saves time on indexing return coords[extrema], data[extrema]
def create_main_grid(points, extend, img_size=32, values=[]): """Create grid and caluclate features :param points: Array Vstack [x, y, z, classification] [m] :type points: float :param extend: Array [minX minY, maxX maxY] :type extend: float :param img_size: Spatial size of feature area Default 32. Should be 2 to power of n :type img_size: int :param values: Values for Feature Stats, if non is passed height is used :type values: float """ tree = KDTree(points[:, 0:2]) buff = int(img_size / 2) if values: points[:, 2] = values minX = extend[0, 0] - buff minY = extend[0, 1] - buff maxX = extend[1, 0] + buff maxY = extend[1, 1] + buff gridX = np.linspace(int(minX), int(maxX), int(maxX - minX + 1)) gridY = np.linspace(int(minY), int(maxY), int(maxY - minY + 1)) f1 = np.zeros((len(gridX), len(gridY))) f2 = np.zeros((len(gridX), len(gridY))) f3 = np.zeros((len(gridX), len(gridY))) for x, i in zip(gridX, range(0, len(gridX))): for y, j in zip(gridY, range(0, len(gridY))): list = tree.query_ball_point([x, y], 1.4) cell_ext = np.array([[x - 0.5, y - 0.5], [x + 0.5, y + 0.5]]) cell_points = clip(points[list], cell_ext) if cell_points.any(): f1[i, j] = np.mean(cell_points[:, 2]) f2[i, j] = np.min(cell_points[:, 2]) f3[i, j] = np.max(cell_points[:, 2]) return [f1, f2, f3]
def kdtree_density(points, radius, return_tree=False): """ Returns the density calculated sing a Ball Tree. Higher is denser. Scales according to the dimension of the points. see: https://stackoverflow.com/questions/14070565/calculating-point-density-using-python If `return_tree` is `True` returns a tuple of the density and the KDTree. """ tree = KDTree(points) n = points.shape[-1] ball_trees = tree.query_ball_tree(tree, radius) frequency = np.array([len(neighbours) for neighbours in ball_trees]) density = frequency / radius**n if return_tree: return np.mean(density), tree return np.mean(density)