ó
¡Š.Yc           @   sP   d  d l  m Z d  d l  m Z d Z d d	 d „  ƒ  YZ d d
 d „  ƒ  YZ d S(   iÿÿÿÿ(   t   defaultdict(   t   deques   0.2.1t   DictIQMc           B   sD   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sm  Computes the Interquartile Mean (IQM) of a dictionary containing
    containing number buckets (key) and the count of their occurrences (value).

    The number bucket supports variable precision:

    - round_digits is the digit supplied to the round() function.
    - tenth_precise=True will round to one less digit for the lowest 10%.
      For example: round_digits=-2, tenth_precise would result in numbers less
      than 100 being rounded to to the nearest 10th instead of 0 or 100.

    none_action determines how non-numbers are treated (all non-numbers are
    stored as None):

    - discard: discard them (default)
    - max: replace them with the maximum value
    - min: replace them with the minimum value

    This class is efficient for datasets in which many numbers are repeated.
    It should not be used for large datasets with a uniform distribution.
    c         C   sò   t  |  _ d  |  _ | d  k r' d } n  | d  k r< t  } n= | rs | d k r] t d ƒ ‚ n  t | ƒ d |  _ n t  } | d k  r‘ t |  _ n  | d  k r¦ d } n | d	 k rÁ t d ƒ ‚ n  t |  j ƒ |  _	 | |  _
 | |  _ | |  _ d  S(
   Niÿÿÿÿi    s/   tenth_precise only applies to round_digits < 0.i
   t   discardt   maxt   mins5   none_action must be one of 'discard', 'max', or 'min'(   s   discards   maxs   min(   t   Falset   as_intst   Nonet   boundt	   Exceptiont   abst   TrueR    t	   prep_datat   datat   none_actiont   round_digitst   tenth_precise(   t   selfR   R   R   (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyt   __init__   s*    							c         C   s=   |  j  r! i t t ƒ d 6d d 6Si t t ƒ d 6d d 6Sd  S(   Nt
   num_countsi    t   count(   R   R    t   intt   float(   R   (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyR   =   s    	c         C   sò   |  j  } |  j } |  j } |  j } |  j } y | j ƒ  } Wn n Xyk t | ƒ } | r¢ | r„ | | k  r„ t | | d ƒ } n t | | ƒ } t | ƒ } n t | | ƒ } Wn d } n X| | d | c d 7<| | d c d 7<d S(   sU   For the key provided, increment its num bucket (dict key) count
        (dict value).i   R   R   N(
   R   R	   R   R   R   t   stripR   t   roundR   R   (   R   t   keyt   numR   R	   R   R   R   (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyt   __call__C   s*    					
c         C   s‚   xo | D]g } | | | k r5 | | | } | | =n) | | | k r^ | | c | 8<d } n  | d k r | Sq Wt  d ƒ ‚ d S(   s¦   Removes the first quartile from a dictionary containing containing
        number buckets (dict key) and the count of their occurrences
        (dict value).
        i    s$   quartile was never decreased to zeroN(   R
   (   R   t   quartilet   numbersR   R   (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyt   remove_quartile^   s    
	c         C   sÌ  |  j  | d } |  j  | d } |  j } | s5 d Sd | k rÖ | d } | d =| d k r‹ | j ƒ  } | j ƒ  | d } | d } n  | d k rª | | c | 7<qÖ | d k rÉ | | c | 7<qÖ | | 8} n  | d k  rd }	 x  | D] }
 |	 |
 | |
 7}	 qï W|	 | } | St | d ƒ } | j ƒ  } | j ƒ  |  j | | | ƒ } | j ƒ  } | j d	 t ƒ |  j | | | ƒ } t | d
 ƒ } d } x  | D] }
 | |
 | |
 7} q¢W| | } | S(   s†   Compute the IQM of a dictionary containing number buckets (dict key)
        and the count of their occurrences (dict value).
        R   R   R   R   i    iÿÿÿÿi   g      Ð?t   reverseg      à?N(   s   maxs   min(   R   R   R   t   keyst   sortR   R   R   (   R   R   R   R   R   t
   none_countR   t   num_mint   num_maxt   num_sumR   t   avgR   t   iq_countt   iq_sumt   iqm(    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyt   reportn   sJ    	





N(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R+   (    (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyR      s   			t	   MovingIQMc           B   s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s  Computes the moving average of the Interquartile Mean (IQM) of a deque
    computed each period.

    none_action determines how non-numbers are treated (all non-numbers are
    stored as None):

    - discard: discard them (default)
    - max: replace them with the maximum value
    - min: replace them with the minimum value

    This class sacrifices accuracy for speed and low memory usage.
    c         C   s^   | d  k r d } n | d k r0 t d ƒ ‚ n  | |  _ t | ƒ |  _ t |  j ƒ |  _ d  S(   NR   R   R   s5   none_action must be one of 'discard', 'max', or 'min'(   s   discards   maxs   min(   R   R
   R   R   t   periodR    R   R   (   R   R0   R   (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyR   °   s    		c         C   s,   i t  d |  j ƒ d 6d d 6d d 6d d 6S(   Nt    t   decki    t
   deck_countt	   iqm_countg        t   iqm_sum(   R   R0   (   R   (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyR   »   s    c         C   s   y | j  ƒ  } Wn n Xy t | ƒ } Wn d } n X|  j | } | d j | ƒ | d c d 7<| d |  j k r‹ |  j | ƒ n  d S(   s/   For the key provided, add the num to its deque.R2   R3   i   N(   R   R   R   R   t   appendR0   R5   (   R   R   R   R   (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyR   ¿   s    
c   
      C   sy  |  j  | } t | d ƒ } |  j } | d d k r: d Sd | k ræ | j d ƒ } x d | k rt | j d ƒ qX W| d k rÉ | d k r« | d } | | g | } qÙ | d } | g | | } n | d c | 8<| j ƒ  n  | d d k  rt t | ƒ | d ƒ } n8 t	 d	 | d ƒ }	 | |	 |	 !} t | ƒ t
 | ƒ } | d
 c | 7<| d c d 7<d | d <d S(   s   Compute the key's new iqm sumR2   R3   i    NR   R   iÿÿÿÿi   g      Ð?R5   R4   i   (   s   maxs   min(   R   t   sortedR   R   R   t   removeR"   R   t   sumR   t   len(
   R   R   R   R2   R   R#   R%   R$   R*   R   (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyR5   Ï   s2    	

c         C   s0   |  j  | } |  j | ƒ | d | d } | S(   s=   Compute the key's average IQM from the iqm_sum and iqm_count.R5   R4   (   R   R5   (   R   R   R   t   iqm_avg(    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyR+   ô   s    N(	   R,   R-   R.   R   R   R   R   R5   R+   (    (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyR/   ¢   s   			%N(    (    (   t   collectionsR    R   t   VERSIONR   R/   (    (    (    s-   /usr/local/lib/python2.7/dist-packages/iqm.pyt   <module>   s   š