def merge(nuclei, peaks2): """ nuclei: a dictionary of RealPoint, representing the average position, vs the number of points averaged. peaks: a list of RealPoint Returns the updated nuclei, with possibly new nuclei, and the existing ones having their coordinates (and counts of points averaged) updated. """ peaks1 = nuclei.keys() search = RadiusNeighborSearchOnKDTree(KDTree(peaks1, peaks1)) for peak2 in peaks2: search.search(peak2, radius, False) n = search.numNeighbors() if 0 == n: # New nuclei not ever seen before nuclei[peak2] = 1 else: # Merge peak with nearest found nuclei, which should only be one given the small radius peak1 = search.getSampler(0).get() count = float(nuclei[peak1]) new_count = count + 1 fraction = count / new_count for d in xrange(3): peak1.setPosition(peak1.getDoublePosition(d) * fraction + peak2.getDoublePosition(d) / new_count, d) nuclei[peak1] = new_count # Check for more if n > 1: print "Ignoring %i additional closeby nuclei" % (n - 1) # Return nuclei to enable a reduce operation over many sets of peaks return nuclei
def makeRadiusSearch(peaks): return RadiusNeighborSearchOnKDTree(KDTree(peaks, peaks))
def makeRadiusSearch(peaks): """ Construct a KDTree-based radius search, for locating points within a given radius of a reference point. """ return RadiusNeighborSearchOnKDTree(KDTree(peaks, peaks))
# Test whether the first nearest neighbor is the point itself from net.imglib2 import RealPoint, KDTree from net.imglib2.neighborsearch import RadiusNeighborSearchOnKDTree points = [RealPoint.wrap([1.0, 1.0, float(i)]) for i in xrange(10)] tree = KDTree(points, points) search = RadiusNeighborSearchOnKDTree(tree) radius = 3 search.search(RealPoint.wrap([1.0, 1.0, 1.0]), 3, False) # unordered for i in xrange(search.numNeighbors()): print "Point " + str(i), search.getSampler(i).get() # Prints: # Point 0 (1.0,1.0,3.0) # Point 1 (1.0,1.0,4.0) # Point 2 (1.0,1.0,0.0) # Point 3 (1.0,1.0,1.0) # Point 4 (1.0,1.0,2.0) # So yes: the first point is always the query point.