ó
ä™xTc           @   sì   d  Z  d d l m Z d d l m Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z	 d	 d
 „ Z
 e d d „ Z d „  Z d „  Z d „  Z e d „ Z d d d „  ƒ  YZ d d d „  ƒ  YZ d e f d „  ƒ  YZ d d d „  ƒ  YZ d S(   s-   
http://pypi.python.org/pypi/cluster/1.1.1b3
iÿÿÿÿ(   t   division(   t	   TupleTypet   ClusteringErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyR      s   c         C   sK   t  |  ƒ t  g  ƒ k r |  g S|  g  k r/ |  St |  d ƒ t |  d ƒ S(   sR   
   Flattens a list.
   Example:
   flatten([a,b,[c,d,[e,f]]]) = [a,b,c,d,e,f]
   i    i   (   t   typet   flatten(   t   L(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyR      s
      c         C   sW   t  |  ƒ } |  } | j ƒ  | d @r3 | | d S| | d d | | d d Sd S(   s~   Return the median of the list of numbers.

   found at: http://mail.python.org/pipermail/python-list/2004-December/253517.htmli   i   g       @N(   t   lent   sort(   t   numberst   nt   copy(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   median&   s    

c         C   s    t  t |  ƒ ƒ t  t |  ƒ ƒ S(   sƒ   Returns the arithmetic mean of a numeric list.

   found at: http://mail.python.org/pipermail/python-list/2004-December/253517.html(   t   floatt   sumR   (   R
   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   mean3   s    i   c         C   sš   d d l  m } t | ƒ t |  ƒ k s. t ‚ |  d k s@ t ‚ d } x: t t |  ƒ ƒ D]& } | t |  | | | ƒ | 7} qY W| | d t | ƒ ƒ S(   så  
   Calculates the minkowski distance between two points.

   PARAMETERS
      x - the first point
      y - the second point
      p - the order of the minkowski algorithm.
          Default = 2. This is equal to the euclidian distance.
                       If the order is 1, it is equal to the manhatten
                       distance.
                       The higher the order, the closer it converges to the
                       Chebyshev distance, which has p=infinity
   iÿÿÿÿ(   t   powi   i    g      ð?(   t   mathR   R   t   AssertionErrort   ranget   absR   (   t   xt   yt   pR   R   t   i(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   minkowski_distance9   s    $c   
      C   sÇ   g  } d } x´ |  D]¬ } g  } d } x€ |  D]x }	 | d k	 rZ | | k rZ | j | ƒ n@ | r„ | | k  r„ | j | | | ƒ n | j | | |	 ƒ ƒ | d 7} q, W| j | ƒ | d 7} q W| S(   sÇ  
   Takes a list and generates a 2D-matrix using the supplied combination
   function to calculate the values.

   PARAMETERS
      list        - the list of items
      combinfunc  - the function that is used to calculate teh value in a cell.
                    It has to cope with two arguments.
      symmetric   - Whether it will be a symmetric matrix along the diagonal.
                    For example, it the list contains integers, and the
                    combination function is abs(x-y), then the matrix will be
                    symmetric.
                    Default: False
      diagonal    - The value to be put into the diagonal. For some functions,
                    the diagonal will stay constant. An example could be the
                    function "x-y". Then each diagonal cell will be "0".
                    If this value is set to None, then the diagonal will be
                    calculated.
                    Default: None
   i    i   N(   t   Nonet   append(
   t   listt
   combinfunct	   symmetrict   diagonalt   matrixt	   row_indext   itemt   rowt	   col_indext   item2(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt	   genmatrixO   s    c         C   s�   d } t  |  d ƒ } x: |  D]2 } x) | D]! } t t  t | ƒ ƒ | ƒ } q* Wq Wd | } d | | } x |  D] } | t | ƒ GHqr Wd S(   s†   
   Prints out a 2-dimensional list cleanly.
   This is useful for debugging.

   PARAMETERS
      list  -  the 2D-list to display
   i    s    %%%is |t   |N(   R   t   maxt   strt   tuple(   R   t   maxlent   colcountt   colt   cellt   formatR$   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   printmatrixx   s    	#
c         C   s?   d d l  m } d } x |  D] } | | d 7} q W| | ƒ S(   s#   calculates the magnitude of a vecoriÿÿÿÿ(   t   sqrti    i   (   R   R2   (   t   aR2   R   t   coord(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt	   magnitudeŒ   s
    c         C   s[   t  |  ƒ t  | ƒ k s t ‚ d } x0 t t  |  ƒ ƒ D] } | |  | | | 7} q7 W| S(   s,   Calculates the dotproduct between two vecorsi    (   R   R   R   (   R3   t   bt   outR   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt
   dotproduct”   s
    c         C   s[   g  } xH t  t |  d ƒ ƒ D]0 } | j | g  |  D] } | | ^ q3 ƒ ƒ q Wt | ƒ S(   s/   returns the central vector of a list of vectorsi    (   R   R   R   R+   (   R   t   methodR7   R   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   centroidœ   s    .t   Clusterc           B   sn   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z	 d „  Z
 d d	 „ Z d
 „  Z d „  Z RS(   s  
   A collection of items. This is internally used to detect clustered items in
   the data so we could distinguish other collection types (lists, dicts, ...)
   from the actual clusters. This means that you could also create clusters of
   lists with this class.
   c         C   s   d |  j  |  j f S(   Ns   <Cluster@%s(%s)>(   t   _Cluster__levelt   _Cluster__items(   t   self(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   __str__«   s    c         C   s
   |  j  ƒ  S(   N(   R?   (   R>   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   __repr__®   s    c         G   s:   | |  _  t | ƒ d k r' g  |  _ n t | ƒ |  _ d S(   sX  
      Constructor

      PARAMETERS
         level - The level of this cluster. This is used in hierarchical
                 clustering to retrieve a specific set of clusters. The higher
                 the level, the smaller the count of clusters returned. The
                 level depends on the difference function used.
         *args - every additional argument passed following the level value
                 will get added as item to the cluster. You could also pass a
                 list as second parameter to initialise the cluster with that
                 list as content
      i    N(   R<   R   R=   R   (   R>   t   levelt   args(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   __init__±   s    	 c         C   s   |  j  j | ƒ d S(   ss   
      Appends a new item to the cluster

      PARAMETERS
         item  -  The item that is to be appended
      N(   R=   R   (   R>   R#   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyR   Ã   s    c         C   s    | d k r |  j S| |  _ d S(   sÉ   
      Sets or gets the items of the cluster

      PARAMETERS
         newItems (optional) - if set, the items of the cluster will be
                               replaced with that argument.
      N(   R   R=   (   R>   t   newItems(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   itemsÌ   s     c         G   s{   g  } t  | ƒ d k r$ |  j } n | d j ƒ  } x@ | D]8 } t | t ƒ rf | |  j | ƒ } q; | j | ƒ q; W| S(   s8  
      Completely flattens out this cluster and returns a one-dimensional list
      containing the cluster's items. This is useful in cases where some items
      of the cluster are clusters in their own right and you only want the
      items.

      PARAMETERS
         *args - only used for recursion.
      i    (   R   R=   RE   t
   isinstanceR;   t   fullyflattenR   (   R>   RB   t   flattened_itemst
   collectionR#   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRG   ×   s    
 c         C   s   |  j  S(   s<   
      Returns the level associated with this cluster
      (   R<   (   R>   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRA   í   s    i    c         C   s`   | d d |  j  GHxE |  j D]: } t | t ƒ rG | j | d ƒ q | d d | GHq Wd S(   s=   
      Pretty-prints this cluster. Useful for debuging
      s      s
   [level %s]i   s   %sN(   R<   R=   RF   R;   t   display(   R>   t   depthR#   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRJ   ó   s
    c         C   sl   |  j  d } |  j  d } t | t ƒ r8 | j ƒ  } n | } t | t ƒ r\ | j ƒ  } n | } | | f S(   së  
      Returns the structure (topology) of the cluster as tuples.

      Output from cl.data:
          [<Cluster@0.833333333333(['CVS', <Cluster@0.818181818182(['34.xls',
          <Cluster@0.789473684211([<Cluster@0.555555555556(['0.txt',
          <Cluster@0.181818181818(['ChangeLog', 'ChangeLog.txt'])>])>,
          <Cluster@0.684210526316(['20060730.py',
          <Cluster@0.684210526316(['.cvsignore',
          <Cluster@0.647058823529(['About.py',
          <Cluster@0.625(['.idlerc', '.pylint.d'])>])>])>])>])>])>])>]

      Corresponding output from cl.topo():
          ('CVS', ('34.xls', (('0.txt', ('ChangeLog', 'ChangeLog.txt')),
          ('20060730.py', ('.cvsignore', ('About.py',
          ('.idlerc', '.pylint.d')))))))
      i    i   (   R=   RF   R;   t   topology(   R>   t   leftt   rightt   firstt   second(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRL   þ   s    c         C   s™  |  j  d } |  j  d } |  j ƒ  | k r9 |  j ƒ  g St | t ƒ r� | j ƒ  | k r� t | t ƒ rƒ | j ƒ  g | j | ƒ S| j ƒ  g | g g Snd t | t ƒ r| j ƒ  | k rt | t ƒ rç | j | ƒ | j ƒ  g S| g g | j ƒ  g Sn  t | t ƒ r9t | t ƒ r9| j | ƒ | j | ƒ St | t ƒ r_| j | ƒ | g g St | t ƒ r…| g g | j | ƒ S| g | g g Sd S(   só  
      Retrieve all clusters up to a specific level threshold. This
      level-threshold represents the maximum distance between two clusters. So
      the lower you set this threshold, the more clusters you will receive and
      the higher you set it, you will receive less but bigger clusters.

      PARAMETERS
         threshold - The level threshold

      NOTE
         It is debatable whether the value passed into this method should
         really be as strongly linked to the real cluster-levels as it is right
         now. The end-user will not know the range of this value unless s/he
         first inspects the top-level cluster. So instead you might argue that
         a value ranging from 0 to 1 might be a more useful approach.
      i    i   N(   R=   RA   RG   RF   R;   t   getlevel(   R>   t	   thresholdRM   RN   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRQ     s&    !!N(   R   R   t   __doc__R?   R@   RC   R   R   RE   RG   RA   RJ   RL   RQ   (    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyR;   £   s   								t   BaseClusterMethodc           B   sJ   e  Z d  Z d „  Z d „  Z d „  Z e e ƒ Z d „  Z e e ƒ Z	 RS(   s1   
   The base class of all clustering methods.
   c         C   s    | |  _  | |  _ | |  _ d S(   sL  
      Constructs the object and starts clustering

      PARAMETERS
         input             - a list of objects
         distance_function - a function returning the distance - or opposite of
                             similarity ( distance = -similarity ) - of two
                             items from the input. In other words, the closer
                             the two items are related, the smaller this value
                             needs to be. With 0 meaning they are exactly the
                             same.

      NOTES
         The distance function should always return the absolute distance
         between two given items of the list. Say,

         distance(input[1], input[4]) = distance(input[4], input[1])

         This is very important for the clustering algorithm to work!
         Naturally, the data returned by the distance function MUST be a
         comparable datatype, so you can perform arithmetic comparisons on
         them (< or >)! The simplest examples would be floats or ints. But as
         long as they are comparable, it's ok.
      N(   t   distancet   _inputt   _data(   R>   t   inputt   distance_function(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRC   U  s    		c         C   s   |  j  d j ƒ  S(   sm   
      Returns the structure (topology) of the cluster.

      See Cluster.topology() for information.
      i    (   t   dataRL   (   R>   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   topor  s    c         C   s   |  j  S(   s<   
      Returns the data that is currently in process.
      (   RW   (   R>   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt
   __get_dataz  s    c         C   s   |  j  S(   sB   
      Returns the raw data (data without being clustered).
      (   RV   (   R>   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   __get_raw_data�  s    (
   R   R   RS   RC   R[   t   _BaseClusterMethod__get_datat   propertyRZ   t    _BaseClusterMethod__get_raw_datat   raw_data(    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRT   P  s   				t   HierarchicalClusteringc           B   sb   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d
 d
 d
 d „ Z
 d	 „  Z RS(   sõ  
   Implementation of the hierarchical clustering method as explained in
   http://www.elet.polimi.it/upload/matteucc/Clustering/tutorial_html/hierarchical.html

   USAGE
      >>> from cluster import HierarchicalClustering
      >>> # or: from cluster import *
      >>> cl = HierarchicalClustering([123,334,345,242,234,1,3], lambda x,y: float(abs(x-y)))
      >>> cl.getlevel(90)
      [[345, 334], [234, 242], [123], [3, 1]]

      Note that all of the returned clusters are more that 90 apart

   t   singlec         C   s-   t  j |  | | ƒ |  j | ƒ t |  _ d S(   sQ   
      Constructor

      See BaseClusterMethod.__init__ for more details.
      N(   RT   RC   t   setLinkageMethodt   Falset'   _HierarchicalClustering__clusterCreated(   R>   RZ   RY   t   linkage(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRC   ˜  s    c         C   sy   | d k r |  j  |  _ nZ | d k r6 |  j |  _ n? | d k rQ |  j |  _ n$ | d k rl |  j |  _ n	 t d ‚ d S(   sà   
      Sets the method to determine the distance between two clusters.

      PARAMETERS:
         method - The name of the method to use. It must be one of 'single',
                  'complete', 'average' or 'uclus'
      Rc   t   completet   averaget   uclussA   distance method must be one of single, complete, average of uclusN(   t   singleLinkageDistanceRg   t   completeLinkageDistancet   averageLinkageDistancet   uclusDistancet
   ValueError(   R>   R9   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRd   ¤  s    c         C   s™   t  | t ƒ s | g } n | j ƒ  } t  | t ƒ sB | g } n | j ƒ  } g  } x8 | D]0 } x' | D] } | j |  j | | ƒ ƒ qh Wq[ Wt | ƒ S(   s<  
      The method to determine the distance between one cluster an another
      item/cluster. The distance equals to the *average* (median) distance from
      any member of one cluster to any member of the other cluster.

      PARAMETERS
         x  -  first cluster/item
         y  -  second cluster/item
      (   RF   R;   RG   R   RU   R   (   R>   R   R   t	   distancest   kt   l(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRn   ·  s      !c         C   s™   t  | t ƒ s | g } n | j ƒ  } t  | t ƒ sB | g } n | j ƒ  } g  } x8 | D]0 } x' | D] } | j |  j | | ƒ ƒ qh Wq[ Wt | ƒ S(   s:  
      The method to determine the distance between one cluster an another
      item/cluster. The distance equals to the *average* (mean) distance from
      any member of one cluster to any member of the other cluster.

      PARAMETERS
         x  -  first cluster/item
         y  -  second cluster/item
      (   RF   R;   RG   R   RU   R   (   R>   R   R   Rp   Rq   Rr   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRm   Ï  s      !c         C   s©   t  | t ƒ s | g } n | j ƒ  } t  | t ƒ sB | g } n | j ƒ  } |  j | d | d ƒ } x: | D]2 } x) | D]! } t | |  j | | ƒ ƒ } q| Wqo W| S(   s3  
      The method to determine the distance between one cluster an another
      item/cluster. The distance equals to the *longest* distance from any
      member of one cluster to any member of the other cluster.

      PARAMETERS
         x  -  first cluster/item
         y  -  second cluster/item
      i    (   RF   R;   RG   RU   R)   (   R>   R   R   t   maxdistRq   Rr   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRl   ç  s      #c         C   s©   t  | t ƒ s | g } n | j ƒ  } t  | t ƒ sB | g } n | j ƒ  } |  j | d | d ƒ } x: | D]2 } x) | D]! } t | |  j | | ƒ ƒ } q| Wqo W| S(   s4  
      The method to determine the distance between one cluster an another
      item/cluster. The distance equals to the *shortest* distance from any
      member of one cluster to any member of the other cluster.

      PARAMETERS
         x  -  first cluster/item
         y  -  second cluster/item
      i    (   RF   R;   RG   RU   t   min(   R>   R   R   t   mindistRq   Rr   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRk     s      #c         C   s�  | d k r! d } d } g  } n  xlt | ƒ d k sB | g  k r�t |  j |  j t d ƒ } d } d } d } xr | D]j } d } xQ | D]I }	 | | k rÈ |	 | k  s³ | d k rÈ | | f } |	 } n  | d 7} q‰ W| d 7} qv W| d 7} | | d | d } t | |  j | d |  j | d ƒ }
 |  j j |  j t | d | d ƒ ƒ |  j j |  j t	 | d | d ƒ ƒ |  j j
 |
 ƒ q$ Wt |  _ d S(   s´  
      Perform hierarchical clustering. This method is automatically called by
      the constructor so you should not need to call it explicitly.

      PARAMETERS
         matrix   -  The 2D list that is currently under processing. The matrix
                     contains the distances of each item with each other
         level    -  The current level of clustering
         sequence -  The sequence number of the clustering
      i    i   i   N(   R   R   R'   RW   Rg   t   TrueR;   t   removeR)   Rt   R   Rf   (   R>   R!   RA   t   sequencet   smallestpairt   mindistancet   rowindexR$   t	   cellindexR/   t   cluster(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyR}     s2    	!$	
(	((	c         C   sF   t  |  j ƒ d k r |  j S|  j s2 |  j ƒ  n  |  j d j | ƒ S(   sç   
      Returns all clusters with a maximum distance of <threshold> in between
      each other

      PARAMETERS
         threshold - the maximum distance between clusters

      SEE-ALSO
         Cluster.getlevel(threshold)
      i   i    (   R   RV   Rf   R}   RW   RQ   (   R>   RR   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRQ   V  s
     	 N(   R   R   RS   RC   Rd   Rn   Rm   Rl   Rk   R   R}   RQ   (    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRb   ˆ  s   					9t   KMeansClusteringc           B   s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s;  
   Implementation of the kmeans clustering method as explained in
   http://www.elet.polimi.it/upload/matteucc/Clustering/tutorial_html/kmeans.html

   USAGE
   =====

     >>> from cluster import KMeansClustering
     >>> cl = KMeansClustering([(1,1), (2,1), (5,3), ...])
     >>> clusters = cl.getclusters(2)
   c         C   sò   | |  _  | |  _ t | ƒ |  _ t | ƒ d k r• t | d t ƒ r• t | d ƒ } x< | d D]- } t | ƒ | k ra t d | d ƒ ‚ qa qa Wn  t | ƒ d k rÖ t | d t ƒ rÖ | d k rÖ t d ƒ ‚ n | d k rî t |  _ n  d S(   sx  
      Constructor

      PARAMETERS
         data     - A list of tuples or integers.
         distance - A function determining the distance between two items.
                    Default: It assumes the tuples contain numeric values and
                             appiles a generalised form of the
                             euclidian-distance algorithm on them.
      i   i    sH   Each item in the data list must have the same amount of dimensions. Items   was out of line!sM   You supplied non-standard items but no distance function! We cannot continue!N(	   t   _KMeansClustering__dataRU   R   t!   _KMeansClustering__initial_lengthRF   R   Ro   R   R   (   R>   RZ   RU   t   control_lengthR#   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRC   w  s    		%2c         C   s	  | d k r t  d | ƒ ‚ n  |  j g  k sR t |  j ƒ d k sR | |  j k rY |  j S| |  j k r„ t  d |  j | f ƒ ‚ n  |  j |  j | ƒ t } xb | t k rt } xI |  j D]> } x5 | D]- } |  j | | ƒ } | t k rÉ | } qÉ qÉ Wq¼ Wq  W|  j S(   sš   
      Generates <n> clusters

      PARAMETERS
         n - The amount of clusters that should be generated.
             n must be greater than 1
      i   sL   When clustering, you need to ask for at least two clusters! You asked for %dsh   Unable to generate more clusters than items
available. You supplied %d items, and asked for %d clusters.(	   R   R   R   R€   t   initialiseClustersRv   Re   t   _KMeansClustering__clusterst   assign_item(   R>   R   t   items_movedR}   R#   t   res(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   getclusters“  s$    
3 c         C   s~   | } xJ |  j  D]? } |  j | t | ƒ ƒ |  j | t | ƒ ƒ k  r | } q q W| | k rv |  j | | | ƒ t St Sd S(   s´   
      Assigns an item from a given cluster to the closest located cluster

      PARAMETERS
         item   - the item to be moved
         origin - the originating cluster
      N(   Rƒ   RU   R:   t	   move_itemRv   Re   (   R>   R#   t   origint   closest_clusterR}   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyR„   ·  s    0c         C   s#   | j  | j | j | ƒ ƒ ƒ d S(   sÖ   
      Moves an item from one cluster to anoter cluster

      PARAMETERS

         item        - the item to be moved
         origin      - the originating cluster
         destination - the target cluster
      N(   R   t   popt   index(   R>   R#   R‰   t   destination(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyRˆ   Ê  s    
c         C   sm   g  |  _  x$ t | ƒ D] } |  j  j g  ƒ q Wd } x0 | D]( } |  j  | | j | ƒ | d 7} q= Wd S(   sê   
      Initialises the clusters by distributing the items from the data evenly
      across n clusters

      PARAMETERS
         input        - the data set (a list of tuples)
         clustercount - the amount of clusters (n)
      i    i   N(   Rƒ   t   xrangeR   (   R>   RX   t   clustercountR   t   countR#   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyR‚   Ö  s    
	 N(	   R   R   RS   R   RC   R‡   R„   Rˆ   R‚   (    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyR~   j  s   	$		N(    (    (    (   RS   t
   __future__R    t   typesR   t	   ExceptionR   R   R   R   R   Re   R   R'   R1   R5   R8   R:   R;   RT   Rb   R~   (    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/cluster.pyt   <module>   s    	
		)			­8â