ó
ŒqIc           @   s˜  d  Z  d d l m Z m Z m Z m Z d d l m Z d d l m	 Z	 d „  Z
 d „  Z d „  Z d d d	 „  ƒ  YZ e d
 k r”d d l m Z d Z d Z d Z d Z d e e e f ƒ Z e e e ƒ Z e j e ƒ e j e ƒ e j ƒ  Z e j ƒ  Z d e e ƒ e f GHx{ e d d ƒ D]g Z e e ƒ Z e j e e ƒ e j  ƒ  Z e j! ƒ  Z e \ Z" Z# Z$ d e e ƒ e e" e# e$ f GHq&Wn  d S(   sÞ  
KD tree data structure for searching N-dimensional vectors.

The KD tree data structure can be used for all kinds of searches that
involve N-dimensional vectors, e.g.  neighbor searches (find all points
within a radius of a given point) or finding all point pairs in a set
that are within a certain radius of each other. See "Computational Geometry: 
Algorithms and Applications" (Mark de Berg, Marc van Kreveld, Mark Overmars, 
Otfried Schwarzkopf). Author: Thomas Hamelryck.
iÿÿÿÿ(   t   sumt   sqrtt   dtypet   array(   t   random(   t   _CKDTreec         C   s   |  | } t  t | | ƒ ƒ S(   N(   R   R    (   t   pt   qt   diff(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt   _dist   s    
c         C   sð   t  j | | ƒ } t |  | f ƒ } | j | ƒ | j | ƒ } g  | D] } | j ^ qG } | d k rq d }	 n t | ƒ }	 | j | ƒ } g  | D] } | j ^ q“ } | d k r½ d }
 n t | ƒ }
 |	 |
 k rÝ d GHn d |	 |
 f GHd S(   s0   Test all fixed radius neighbor search.

    Test all fixed radius neighbor search using the 
    KD tree C module.

    o nr_points - number of points used in test
    o dim - dimension of coords
    o bucket_size - nr of points per tree node
    o radius - radius of search (typically 0.05 or so) 
    i    s   Passed.s   Not passed: %i != %i.N(	   R   t   KDTreeR   t   set_datat   neighbor_searcht   radiust   Nonet   lent   neighbor_simple_search(   t	   nr_pointst   dimt   bucket_sizeR   t   kdtt   coordst	   neighborst   neighbort   rt   l1t   l2(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt   _neighbor_test   s     		c         C   së   t  j | | ƒ } t |  | f ƒ } | d } | j | ƒ | j | | ƒ | j ƒ  } | d k rl d } n t | ƒ } d }	 xC t d |  ƒ D]2 }
 | |
 } t	 | | ƒ | k rŽ |	 d }	 qŽ qŽ W| |	 k rØ d GHn d | |	 f GHd S(   s  Test neighbor search.

    Test neighbor search using the KD tree C module.

    o nr_points - number of points used in test
    o dim - dimension of coords
    o bucket_size - nr of points per tree node
    o radius - radius of search (typically 0.05 or so) 
    i    i   s   Passed.s   Not passed: %i != %i.N(
   R   R
   R   R   t   search_center_radiust   get_indicesR   R   t   rangeR	   (   R   R   R   R   R   R   t   centerR   R   R   t   iR   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt   _test;   s"    
	
R
   c           B   sY   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 RS(
   sÒ  
    KD tree implementation (C++, SWIG python wrapper)

    The KD tree data structure can be used for all kinds of searches that
    involve N-dimensional vectors, e.g.  neighbor searches (find all points
    within a radius of a given point) or finding all point pairs in a set
    that are within a certain radius of each other.

    Reference:

    Computational Geometry: Algorithms and Applications
    Second Edition
    Mark de Berg, Marc van Kreveld, Mark Overmars, Otfried Schwarzkopf
    published by Springer-Verlag
    2nd rev. ed. 2000. 
    ISBN: 3-540-65620-0

    The KD tree data structure is described in chapter 5, pg. 99. 

    The following article made clear to me that the nodes should 
    contain more than one point (this leads to dramatic speed 
    improvements for the "all fixed radius neighbor search", see
    below):

    JL Bentley, "Kd trees for semidynamic point sets," in Sixth Annual ACM
    Symposium on Computational Geometry, vol. 91. San Francisco, 1990

    This KD implementation also performs a "all fixed radius neighbor search",
    i.e. it can find all point pairs in a set that are within a certain radius
    of each other. As far as I know the algorithm has not been published.
    i   c         C   s+   | |  _  t j | | ƒ |  _ d |  _ d  S(   Ni    (   R   R   R
   R   t   built(   t   selfR   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt   __init__|   s    	c         C   s‘   | j  ƒ  d k s$ | j ƒ  d k r3 t d ƒ ‚ n  t | j ƒ d k s^ | j d |  j k rt t d |  j ƒ ‚ n  |  j j | ƒ d |  _ d S(   sÛ   Add the coordinates of the points.

        o coords - two dimensional NumPy array. E.g. if the points
        have dimensionality D and there are N points, the coords 
        array should be NxD dimensional. 
        g    €„.Ág    €„.As&   Points should lie between -1e6 and 1e6i   i   s   Expected a Nx%i NumPy arrayN(	   t   mint   maxt	   ExceptionR   t   shapeR   R   R   R"   (   R#   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt
   set_coordsƒ   s    $+c         C   sZ   |  j  s t d ƒ ‚ n  | j |  j f k rC t d |  j ƒ ‚ n  |  j j | | ƒ d S(   sÜ   Search all points within radius of center.

        o center - one dimensional NumPy array. E.g. if the points have
        dimensionality D, the center array should be D dimensional. 
        o radius - float>0
        s   No point set specifieds%   Expected a %i-dimensional NumPy arrayN(   R"   R'   R(   R   R   R   (   R#   R   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt   search“   s    	c         C   s#   |  j  j ƒ  } | d k r g  S| S(   si   Return radii.

        Return the list of distances from center after
        a neighbor search.
        N(   R   t	   get_radiiR   (   R#   t   a(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyR+   ¡   s    c         C   s#   |  j  j ƒ  } | d k r g  S| S(   s#  Return the list of indices.

        Return the list of indices after a neighbor search.
        The indices refer to the original coords NumPy array. The
        coordinates with these indices were within radius of center.

        For an index pair, the first index<second index. 
        N(   R   R   R   (   R#   R,   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyR   ¬   s    	c         C   s1   |  j  s t d ƒ ‚ n  |  j j | ƒ |  _ d S(   sz   All fixed neighbor search.

        Search all point pairs that are within radius.

        o radius - float (>0)
        s   No point set specifiedN(   R"   R'   R   R   R   (   R#   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt
   all_search½   s    	c         C   s2   t  g  |  j D] } | j | j g ^ q ƒ } | S(   s¼   Return All Fixed Neighbor Search results.

        Return a Nx2 dim NumPy array containing
        the indices of the point pairs, where N
        is the number of neighbor pairs.
        (   R   R   t   index1t   index2(   R#   R   R,   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt   all_get_indicesÈ   s    .c         C   s   g  |  j  D] } | j ^ q
 S(   s½   Return All Fixed Neighbor Search results.

        Return an N-dim array containing the distances
        of all the point pairs, where N is the number 
        of neighbor pairs..
        (   R   R   (   R#   R   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt   all_get_radiiÒ   s    (   t   __name__t
   __module__t   __doc__R$   R)   R*   R+   R   R-   R0   R1   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyR
   [   s   						
t   __main__i † i   i
   iÈ   s&   Found %i point pairs within radius %f.i    s>   Found %i points in radius %f around center (%.2f, %.2f, %.2f).N(    (%   R4   t   numpyR    R   R   R   t   numpy.randomR   t
   Bio.KDTreeR   R	   R   R!   R
   R2   R   R   R   t   query_radiusR   t   kdtreeR)   R-   R0   t   indicesR1   t   radiiR   R   R    R   R*   R   R+   t   xt   yt   z(    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/KDTree/KDTree.pyt   <module>   s6   "		!	 €