ó
þß|Uc           @   s¤   d  d d g Z  d d l Z d d l Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d „  Z d	 „  Z d
 „  Z d „  Z d e f d „  ƒ  YZ d „  Z d S(   t   balancedt	   demo_datat   LookupFactoriÿÿÿÿN(   t
   PatsyError(   t   C(   t   no_picklingt   assert_no_picklingc    
      K   sÉ   |  j  d d ƒ } g  } t |  ƒ } xO | D]G } |  | } | j g  t d | d ƒ D] } d | | f ^ qU ƒ q+ Wt t j | Œ  Œ  } i  } x1 t | | ƒ D]  \ } }	 t |	 ƒ | | | <q¡ W| S(   sT  balanced(factor_name=num_levels, [factor_name=num_levels, ..., repeat=1])

    Create simple balanced factorial designs for testing.

    Given some factor names and the number of desired levels for each,
    generates a balanced factorial design in the form of a data
    dictionary. For example:

    .. ipython::

       In [1]: balanced(a=2, b=3)
       Out[1]:
       {'a': ['a1', 'a1', 'a1', 'a2', 'a2', 'a2'],
        'b': ['b1', 'b2', 'b3', 'b1', 'b2', 'b3']}

    By default it produces exactly one instance of each combination of levels,
    but if you want multiple replicates this can be accomplished via the
    `repeat` argument:

    .. ipython::

       In [2]: balanced(a=2, b=2, repeat=2)
       Out[2]:
       {'a': ['a1', 'a1', 'a2', 'a2', 'a1', 'a1', 'a2', 'a2'],
        'b': ['b1', 'b2', 'b1', 'b2', 'b1', 'b2', 'b1', 'b2']}
    t   repeati   s   %s%s(   t   popt   sortedt   appendt   ranget   zipt	   itertoolst   productt   list(
   t   kwargsR   t   levelst   namest   namet   level_countt   it   valuest   datat   value(    (    s.   lib/python2.7/site-packages/patsy/user_util.pyR       s    
;c          C   sø   t  d d d d ƒ }  |  d d d d d d d g k s= t ‚ |  d d d d	 d d d	 g k se t ‚ t  d d d d d
 d ƒ }  |  d d d d d d d d d d d d d g k sº t ‚ |  d d d d	 d d d	 d d d	 d d d	 g k sô t ‚ d  S(   Nt   ai   t   bi   t   a1t   a2t   b1t   b2t   b3R   (   R    t   AssertionError(   R   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   test_balanced9   s    ((!c          O   sM  | j  d d ƒ } | j  d d ƒ } | r@ t d | f ƒ ‚ n  t ƒ  } i  } x^ |  D]V } | d d k ry | | | <qV | d d k r™ | j | ƒ qV t d	 | f ƒ ‚ qV Wt j t | j ƒ  ƒ d
 t	 ƒ} t	 t j
 | d | ƒ ƒ } | | }	 t d | |  }
 t j j d ƒ } x* t | ƒ D] } | j d |	 ƒ |
 | <q)W|
 S(   s@  demo_data(*names, nlevels=2, min_rows=5)

    Create simple categorical/numerical demo data.

    Pass in a set of variable names, and this function will return a simple
    data set using those variable names.

    Names whose first letter falls in the range "a" through "m" will be made
    categorical (with `nlevels` levels). Those that start with a "p" through
    "z" are numerical.

    We attempt to produce a balanced design on the categorical variables,
    repeating as necessary to generate at least `min_rows` data
    points. Categorical variables are returned as a list of strings.

    Numerical data is generated by sampling from a normal distribution. A
    fixed random seed is used, so that identical calls to demo_data() will
    produce identical results. Numerical data is returned in a numpy array.

    Example:

    .. ipython:

       In [1]: patsy.demo_data("a", "b", "x", "y")
       Out[1]: 
       {'a': ['a1', 'a1', 'a2', 'a2', 'a1', 'a1', 'a2', 'a2'],
        'b': ['b1', 'b2', 'b1', 'b2', 'b1', 'b2', 'b1', 'b2'],
        'x': array([ 1.76405235,  0.40015721,  0.97873798,  2.2408932 ,
                     1.86755799, -0.97727788,  0.95008842, -0.15135721]),
        'y': array([-0.10321885,  0.4105985 ,  0.14404357,  1.45427351,
                     0.76103773,  0.12167502,  0.44386323,  0.33367433])}
    t   nlevelsi   t   min_rowsi   s   unexpected keyword arguments %ri    t   abcdefghijklmnt   pqrstuvwxyzs   bad name %rt   dtypeg      ð?R   t   size(   R   t	   TypeErrort   sett   addR   t   npt   prodR   R   t   intt   ceilR    t   randomt   RandomStateR	   t   normal(   R   R   R"   R#   t	   numericalt   categoricalR   t   balanced_design_sizeR   t   num_rowsR   t   r(    (    s.   lib/python2.7/site-packages/patsy/user_util.pyR   C   s(    !	!
c       	   C   s  t  d d d ƒ }  t |  j ƒ  ƒ d d d g k s9 t ‚ |  d d d d d d d d d g k sg t ‚ |  d d d d d d d d d g k s• t ‚ |  d j t j t ƒ k s· t ‚ |  d j d k sÐ t ‚ t  d d	 ƒ } t | j ƒ  ƒ d d	 g k st ‚ t | d ƒ t | d	 ƒ k o.d
 k n s9t ‚ t t  d d d ƒd ƒ d k sat ‚ t t  d d d d d ƒd ƒ d k st ‚ t t  d d d d d d d ƒd ƒ d k sÃt ‚ d d l	 m
 } | t t  d d d ƒ | t t  d d d d ƒd  S(   NR   R   t   xR   R   R   R   i   t   yi   R#   i
   i   R"   i   i   iÿÿÿÿ(   t   assert_raisest   __123t   asdfasdfi{   (   i   (   R   R	   t   keysR    R&   R+   t   floatt   shapet   lent
   nose.toolsR9   R   R(   (   t   d1t   d2R9   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   test_demo_dataz   s    '.."$6(.4c           B   sz   e  Z d  Z e d d d d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z e Z RS(   s8  A simple factor class that simply looks up a named entry in the given
    data.

    Useful for programatically constructing formulas, and as a simple example
    of the factor protocol.  For details see
    :ref:`expert-model-specification`.

    Example::

      dmatrix(ModelDesc([], [Term([LookupFactor("x")])]), {"x": [1, 2, 3]})

    :arg varname: The name of this variable; used as a lookup key in the
      passed in data dictionary/DataFrame/whatever.
    :arg force_categorical: If True, then treat this factor as
      categorical. (Equivalent to using :func:`C` in a regular formula, but
      of course you can't do that with a :class:`LookupFactor`.
    :arg contrast: If given, the contrast to use; see :func:`C`. (Requires
      ``force_categorical=True``.)
    :arg levels: If given, the categorical levels; see :func:`C`. (Requires
      ``force_categorical=True``.)
    :arg origin: Either ``None``, or the :class:`Origin` of this factor for use
      in error reporting.

    .. versionadded:: 0.2.0
       The ``force_categorical`` and related arguments.
    c         C   ss   | |  _  | |  _ | |  _ | |  _ | |  _ |  j so | d  k	 rQ t d ƒ ‚ n  | d  k	 ro t d ƒ ‚ qo n  d  S(   Ns)   contrast= requires force_categorical=Trues'   levels= requires force_categorical=True(   t   _varnamet   _force_categoricalt	   _contrastt   _levelst   origint   Nonet
   ValueError(   t   selft   varnamet   force_categoricalt   contrastR   RH   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   __init__©   s    						c         C   s   |  j  S(   N(   RD   (   RK   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyR   ·   s    c         C   s   d |  j  j |  j f S(   Ns   %s(%r)(   t	   __class__t   __name__RD   (   RK   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   __repr__º   s    c         C   sU   t  | t ƒ oT |  j | j k oT |  j | j k oT |  j | j k oT |  j | j k S(   N(   t
   isinstanceR   RD   RE   RF   RG   (   RK   t   other(    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   __eq__½   s
    c         C   s   |  | k S(   N(    (   RK   RT   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   __ne__Ä   s    c         C   s%   t  t |  j |  j |  j |  j f ƒ S(   N(   t   hashR   RD   RE   RF   RG   (   RK   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   __hash__Ç   s    c         C   s   d S(   Ni    (    (   RK   t   statet   eval_env(    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   memorize_passes_neededË   s    c         C   s   t  s t ‚ d  S(   N(   t   FalseR    (   RK   RY   t
   which_passR   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   memorize_chunkÎ   s    c         C   s   t  s t ‚ d  S(   N(   R\   R    (   RK   RY   R]   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   memorize_finishÑ   s    c         C   s;   | |  j  } |  j r7 t | d |  j d |  j ƒ} n  | S(   NRN   R   (   RD   RE   R   RF   RG   (   RK   t   memorize_stateR   R   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   evalÔ   s    	!N(   RQ   t
   __module__t   __doc__R\   RI   RO   R   RR   RU   RV   RX   R[   R^   R_   Ra   R   t   __getstate__(    (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyR   Ž   s   										c       	   C   s  t  d ƒ }  |  j ƒ  d k s$ t ‚ |  t  d ƒ k s< t ‚ |  t  d ƒ k sT t ‚ t |  ƒ t t  d ƒ ƒ k sx t ‚ t |  ƒ t t  d ƒ ƒ k sœ t ‚ |  j i  i d d 6ƒ d k sÁ t ‚ |  j i  i d d 6ƒ d k sæ t ‚ t |  ƒ d k sþ t ‚ |  j d  k st ‚ t  d d d ƒ} | j d k s:t ‚ t  d d	 t d
 d d d ƒ} | j i  i d d d g d 6ƒ } | j	 d d d g k s˜t ‚ | j
 d k s­t ‚ | j d k sÂt ‚ d d l m } | t t  d d
 d ƒ| t t  d d d ƒt t  d ƒ ƒ d  S(   NR   R   i   i   s   LookupFactor('a')RH   t   asdft   cRM   RN   t   CONTRASTR   iÿÿÿÿ(   R9   t   nc(   i   i   (   i   i   (   i   i   (   R   R   R    RW   Ra   t   reprRH   RI   t   TrueR   RN   R   R@   R9   RJ   R   (   t   l_at   l_with_origint   l_ct   boxR9   (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   test_LookupFactorÜ   s,    $$%%"(   t   __all__R   t   numpyR+   t   patsyR   t   patsy.categoricalR   t
   patsy.utilR   R   R    R!   R   RC   t   objectR   Ro   (    (    (    s.   lib/python2.7/site-packages/patsy/user_util.pyt   <module>	   s   	(	
	7	N