
    ,h\$                     x    / S Qr SSKrSSKrSSKJr  SSKJr  SSKJ	r	J
r
  S rS rS rS	 r " S
 S\5      rS rg))balanced	demo_dataLookupFactor    N)
PatsyError)C)no_picklingassert_no_picklingc                  N   U R                  SS5      n/ n[        U 5      nU H;  nX   nUR                  [        SUS-   5       Vs/ s H
  od< U< 3PM     sn5        M=     [	        [
        R                  " U6 6 n0 n[	        X75       H  u  pI[        U	5      U-  X'   M     U$ s  snf )a  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']}
repeat   )popsortedappendrangezip	itertoolsproductlist)
kwargsr   levelsnamesnamelevel_countivaluesdatavalues
             bC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\patsy/user_util.pyr   r      s    6 ZZ!$FF6NEl5K!O3LM3Laq)3LMN  )##V,-FD5)%[6)
 *K Ns   B"
c                      [        SSS9n U S   / SQ:X  d   eU S   / SQ:X  d   e[        SSSS9n U S   / S	Q:X  d   eU S   / S
Q:X  d   eg )N      )abr"   )a1r$   r$   a2r%   r%   r#   )b1b2b3r&   r'   r(   )r"   r#   r   )r$   r$   r$   r%   r%   r%   r$   r$   r$   r%   r%   r%   )r&   r'   r(   r&   r'   r(   r&   r'   r(   r&   r'   r(   )r   )r   s    r   test_balancedr)   ;   sz    a1D9<<<<9<<<<a1Q'D9     9        c                  d   UR                  SS5      nUR                  SS5      nU(       a  [        SU< 35      e[        5       n0 nU  H;  nUS   S;   a  X%U'   M  US   S;   a  UR                  U5        M.  [	        S	U< 35      e   [
        R                  " [        UR                  5       5      [        S
9n[        [
        R                  " US-  U-  5      5      nX-  n	[        SSU0UD6n
[
        R                  R                  S5      n[        U5       H  nUR                  U	S9X'   M     U
$ )a  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])}
nlevelsr    min_rows   zunexpected keyword arguments r   abcdefghijklmnpqrstuvwxyzz	bad name )dtypeg      ?r   )size )r   	TypeErrorsetaddr   npprodr   r   intceilr   randomRandomStater   normal)r   r   r,   r-   	numericalcategoricalr   balanced_design_sizer   num_rowsr   rs               r   r   r   ^   s   B jjA&Gzz*a(HVEFFIK7&& '!W%MM$d455  774(:(:(<#=SIC*>>?@F,H161[1D
		a Ay!XX8X,
 "Kr*   c            
         [        SSS5      n [        U R                  5       5      / SQ:X  d   eU S   / SQ:X  d   eU S   / SQ:X  d   eU S   R                  [        R                  " [
        5      :X  d   eU S   R                  S:X  d   e[        SS5      n[        UR                  5       5      SS/:X  d   e[        US   5      [        US   5      s=:X  a  S	:X  d   e   e[        [        SS
S9S   5      S
:X  d   e[        [        SSSS
S9S   5      S:X  d   e[        [        SSSS
SS9S   5      S:X  d   eSS KnUR                  [        [         SSS5        UR                  [        [         SSSS9  g )Nr"   r#   x)r"   r#   rD   )r$   r$   r%   r%   r$   r$   r%   r%   )r&   r'   r&   r'   r&   r'   r&   r'   )   yr.   
   )r-      r!   )r-   r,      r   __123{   )asdfasdf)r   r   keysr1   r7   floatshapelenpytestraisesr   r4   )d1d2rQ   s      r   test_demo_datarU      si   	3S	!B"'')///c7FFFFc7FFFFc7==BHHUO+++c7==D   	3	B"'')c
***r#w<3r#w<,1,,,,,yr*3/0B666yc34S9:b@@@yc3Q?DEKKK
MM*ic7;
MM)YS3M?r*   c                   ^    \ rS rSrSr SS jrS rS rS rS r	S	 r
S
 rS rS rS r\rSrg)r      a  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.
Nc                     Xl         X l        X0l        X@l        XPl        U R                  (       d  Ub  [        S5      eUb  [        S5      eg g )Nz)contrast= requires force_categorical=Truez'levels= requires force_categorical=True)_varname_force_categorical	_contrast_levelsorigin
ValueError)selfvarnameforce_categoricalcontrastr   r]   s         r   __init__LookupFactor.__init__   sU      "3!&&# !LMM! !JKK " 'r*   c                     U R                   $ N)rY   r_   s    r   r   LookupFactor.name   s    }}r*   c                 R    U R                   R                  < SU R                  < S3$ )N())	__class____name__rY   rg   s    r   __repr__LookupFactor.__repr__   s    >>22DMMBBr*   c                 "   [        U[        5      =(       ay    U R                  UR                  :H  =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R
                  UR
                  :H  $ rf   )
isinstancer   rY   rZ   r[   r\   r_   others     r   __eq__LookupFactor.__eq__   sm    ul+ ./.''5+C+CC. %//1. -	
r*   c                     X:X  + $ rf   r3   rr   s     r   __ne__LookupFactor.__ne__   s      r*   c                 z    [        [        U R                  U R                  U R                  U R
                  45      $ rf   )hashr   rY   rZ   r[   r\   rg   s    r   __hash__LookupFactor.__hash__   s5    ''
 	
r*   c                     g)Nr   r3   )r_   stateeval_envs      r   memorize_passes_needed#LookupFactor.memorize_passes_needed   s    r*   c                      erf   r3   )r_   r~   
which_passr   s       r   memorize_chunkLookupFactor.memorize_chunk       ur*   c                      erf   r3   )r_   r~   r   s      r   memorize_finishLookupFactor.memorize_finish   r   r*   c                     X R                      nU R                  (       a  [        X0R                  U R                  S9nU$ )N)rb   r   )rY   rZ   r   r[   r\   )r_   memorize_stater   r   s       r   evalLookupFactor.eval   s0    ]]#""ennT\\JEr*   )r[   rZ   r\   rY   r]   )FNNN)rm   
__module____qualname____firstlineno____doc__rc   r   rn   rt   rw   r{   r   r   r   r   r   __getstate____static_attributes__r3   r*   r   r   r      sI    8 TXLC
!	
 Lr*   r   c                  B   [        S5      n U R                  5       S:X  d   eU [        S5      :X  d   eU [        S5      :w  d   e[        U 5      [        [        S5      5      :X  d   e[        U 5      [        [        S5      5      :w  d   eU R                  0 SS05      S:X  d   eU R                  0 SS05      S:X  d   e[	        U 5      S:X  d   eU R
                  b   e[        SSS9nUR
                  S:X  d   e[        SS	S
SS9nUR                  0 S/ SQ05      nUR                  / SQ:X  d   eUR                  S
:X  d   eUR                  S:X  d   eSS K	nUR                  [        [         SS
S9  UR                  [        [         SSS9  [        [        S5      5        g )Nr"   r#   r   r    zLookupFactor('a')asdf)r]   cTCONTRAST)r   r    )ra   rb   r   )r   r   r    r   nc)rb   )r   )r   r   rz   r   reprr]   r   rb   r   rQ   rR   r^   r	   )l_al_with_originl_cboxrQ   s        r   test_LookupFactorr     s   
s
C88:,s####,s####9\#.////9\#.////88Ba!Q&&&88Ba!Q&&&9++++:: V4M6)))
sdZPV
WC
((2Y'
(C88y   <<:%%%::
MM*lD:MF
MM*lDM@|C()r*   )__all__r   numpyr7   patsyr   patsy.categoricalr   
patsy.utilr   r	   r   r)   r   rU   objectr   r   r3   r*   r   <module>r      sG    4     6&R F5p@,V6 Vr*r*   