def classifyPointCloud(radius, threshold, method): """ :param radius: search radius :param threshold: max slope between points :param method: the data structure the user would like to use :type radius: float :type threshold: deg :type method: string :return: wrapper method - choose a way of searching and get results """ cloud = PointCloud() cloud.initializeData() if method == 'KDTree': kdtree = KDTree() kdtree.initializeKDTree(cloud.pts, cloud.pts.shape[1] - 1) return KDTreeSearch(radius, threshold, cloud.pts, kdtree) elif method == 'Naive': return naiveSearch(radius, threshold, cloud.pts) elif method == 'EqualCells': g = GeoEqualCells() g.initializeGeoEqualCells(cloud.pts, 10, [1, 1]) # terrain, objects, rtime = g.classifyPoints(radius, threshold) return g.classifyPoints(radius, threshold) else: return print( 'method needs to be of the following: Naive, EqualCells or KDTree')
def __init__(self, dataSet, featureCount=None, distanceCalculator=None, isNormalizeFeatures=True): if featureCount is not None and featureCount > dataSet.shape[1]: raise ValueError() if featureCount is None: featureCount = dataSet.shape[1] if distanceCalculator is None: distanceCalculator = ( lambda X, v: DataHelper.calcMinkowskiDistance(X, v)) if isNormalizeFeatures: if featureCount < dataSet.shape[1]: self.__dataSet = dataSet[:, 0:featureCount] self.__dataSet, self.__mu, self.__sigma = DataHelper.normalizeFeatures( self.__dataSet) self.__dataSet = np.hstack( (self.__dataSet, dataSet[:, featureCount:])) else: self.__dataSet, self.__mu, self.__sigma = DataHelper.normalizeFeatures( dataSet) else: self.__dataSet, self.__mu, self.__sigma = dataSet, 0, 1 self.__featureCount = featureCount self.__distanceCalculator = distanceCalculator self.__isNormalizeFeatures = isNormalizeFeatures self.__tree = KDTree.KDTree(self.__dataSet, featureCount)
def initialize(self): if self.world == None: return False # Backup the root if self.root != None: rootBackup = copy.deepcopy(self.root) # Delete all the vertices self.listVertices = [] self.numVertices = 0 self.lowerBoundCost = sys.float_info.max self.lowerBoundVertex = None # Clear the kdtree self.kdtree = KDTree(self.dimension) # Initialize the variables self.root = rootBackup if self.root!= None: self.listVertices.append(self.root) self.insertIntoKDTree(self.root) self.numVertices += 1 self.lowerBoundCost = sys.float_info.max self.lowerBoundVertex = None
def initializeData(self, obstacleSet): # Given set of obstacles then parse the obstacles into line segments segmentList = [] for obstacle in obstacleSet.polys: for segment in obstacle.segments: segmentList.append(segment) if len(obstacleSet) > 15: print "Constructing KDTree" return KDTree(segmentList) else: print "Constructing list" return ObstacleList(segmentList)
def kdTree(self): pin_XList = [] pin_YList = [] for i in self.gridParameters['netInfo']: for j in range(i['numPins']): pin_XList.append(i[str(j + 1)][0]) pin_YList.append(i[str(j + 1)][1]) pinLoc = np.column_stack((pin_XList, pin_YList)) # gridNum = 2 # tree = neighbor.KDTree(pinLoc, leaf_size=int(gridNum)) # # print(tree.idx_array) KDT = kd.KDTree( pinLoc, [np.min(pinLoc[:, 0]), np.min(pinLoc[:, 1])], [np.max(pinLoc[:, 0]), np.max(pinLoc[:, 1])]) # ------------------------------------------------------------ # Plot four different levels of the KD tree fig = plt.figure(figsize=(5, 5)) fig.subplots_adjust(wspace=0.1, hspace=0.15, left=0.1, right=0.9, bottom=0.05, top=0.9) for level in range(1, 10): ax = fig.add_subplot(3, 3, level, xticks=[], yticks=[]) ax.scatter(pinLoc[:, 0], pinLoc[:, 1], s=9) KDT.draw_rectangle(ax, depth=level) # # ax.set_xlim(-1.2, 1.2) # ax.set_ylim(-0.15, 1.15) ax.set_title('level %i' % level) # suptitle() adds a title to the entire figure fig.suptitle('kd-tree Example') plt.show() # # plt.subplot(2,2,1);plt.title('Pin No.8000-9000') # plt.plot(pin_XList[8000:9000],pin_YList[8000:9000],'b.') # plt.subplot(2,2,2);plt.title('Pin No.20000-21000') # plt.plot(pin_XList[20000:21000],pin_YList[20000:21000],'b.') # plt.subplot(2,2,3);plt.title('Pin No.50000-51000') # plt.plot(pin_XList[50000:51000],pin_YList[50000:51000],'b.') # plt.subplot(2,2,4);plt.title('Pin No.100000-101000') # plt.plot(pin_XList[100000:101000],pin_YList[100000:101000],'b.') # plt.show() return
def contact_residues(A, B, cutoff=4.0, pointing=45.0): tree = KDTree([b.co for b in B]) atoms = [] for a in A: #if a.anam in [" C "," N "," O "," CA "]: continue d, co = tree.nearest(a.co) if d < cutoff * cutoff: b = tree.index_of(co) ang = side_chain_angle_to_contact(a, A, B[b]) if ang != -1 and ang < pointing: atoms.append(a) residues = [x.rnum for x in atoms] residues = list(set(residues)) # remove duplicates residues.sort() return residues
import sys, os, math from pdb import * from KDTree import * pdb = sys.argv[1] ch1, ch2 = sys.argv[2], sys.argv[3] chains = read_pdb(pdb) hashchains = {} for chain in chains: if len(chain) > 0: hashchains[chain[0].chn] = chain # based on chn of first Atom A, B = hashchains[ch1], hashchains[ch2] # error if not found tree = KDTree([b.co for b in B]) for a in A: d, co = tree.nearest(a.co) b = tree.index_of(co) cutoff = 4.0 if d < cutoff * cutoff: b = B[b] print a.rnam, a.rnum, a.chn, a.anam, b.rnam, b.rnum, b.chn, b.anam
def __init__(self): self.kdtree = kd.KDTree()