
    g\$                     x    g d 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 G d
 de      Zd Zy))balanced	demo_dataLookupFactor    N)
PatsyError)C)no_picklingassert_no_picklingc                  D   | j                  dd      }g }t        |       }|D ]7  }| |   }|j                  t        d|dz         D cg c]  }||
 c}       9 t	        t        j                  |  }i }t	        ||      D ]  \  }}	t        |	      |z  ||<    |S c c}w )aT  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
             L/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/patsy/user_util.pyr   r      s    6 ZZ!$FF6NE OTl5K!O3LMaq)MNO )##V,-FD5&) *e%[6)T
*K Ns   B
c                      t        dd      } | d   g dk(  sJ | d   g dk(  sJ t        ddd      } | d   g d	k(  sJ | d   g d
k(  sJ y )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                  R   |j                  dd      }|j                  dd      }|rt        d|      t               }i }| D ]5  }|d   dv r|||<   |d   dv r|j                  |       )t	        d	|       t        j                  t        |j                               t        
      }t        t        j                  |dz  |z              }||z  }	t        dd|i|}
t
        j                  j                  d      }t        |      D ]  }|j                  |	      |
|<    |
S )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 67&& 'K!W%MM$d4556 774(:(:(<#=SIC*>>?@F,,H161[1D
		a Ay! -XX8X,T
-Kr*   c            	         t        ddd      } t        | j                               g dk(  sJ | d   g dk(  sJ | d   g dk(  sJ | d   j                  t	        j                  t
              k(  sJ | d   j                  dk(  sJ t        dd      }t        |j                               ddgk(  sJ t        |d         t        |d         cxk(  rd	k(  sJ  J t        t        dd
      d         d
k(  sJ t        t        dddd
      d         dk(  sJ t        t        dddd
d      d         dk(  sJ dd l}|j                  t        t         ddd       |j                  t        t         ddd       y )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                   T    e Zd ZdZ	 ddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd ZeZy)r   a8  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                     || _         || _        || _        || _        || _        | j                  s|t        d      |t        d      y y )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__zLookupFactor.__init__   s_      "3!&&# !LMM! !JKK " 'r*   c                     | j                   S N)rX   r^   s    r   r   zLookupFactor.name   s    }}r*   c                 N    | j                   j                  d| j                  dS )N())	__class____name__rX   re   s    r   __repr__zLookupFactor.__repr__   s    >>22DMMBBr*   c                     t        |t              xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S rd   )
isinstancer   rX   rY   rZ   r[   r^   others     r   __eq__zLookupFactor.__eq__   sm    ul+ ./.''5+C+CC. %//1. -	
r*   c                     | |k(   S rd   r3   rn   s     r   __ne__zLookupFactor.__ne__   s    5=  r*   c                 z    t        t        | j                  | j                  | j                  | j
                  f      S rd   )hashr   rX   rY   rZ   r[   re   s    r   __hash__zLookupFactor.__hash__   s5    ''
 	
r*   c                      y)Nr   r3   )r^   stateeval_envs      r   memorize_passes_neededz#LookupFactor.memorize_passes_needed   s    r*   c                     J rd   r3   )r^   rw   
which_passr   s       r   memorize_chunkzLookupFactor.memorize_chunk       ur*   c                     J rd   r3   )r^   rw   r{   s      r   memorize_finishzLookupFactor.memorize_finish   r}   r*   c                     || j                      }| j                  r"t        || j                  | j                        }|S )N)ra   r   )rX   rY   r   rZ   r[   )r^   memorize_stater   r   s       r   evalzLookupFactor.eval   s4    T]]#""ednnT\\JEr*   )FNNN)rj   
__module____qualname____doc__rb   r   rk   rp   rr   ru   ry   r|   r   r   r   __getstate__r3   r*   r   r   r      sI    8 TXLC
!	
 Lr*   r   c                  @   t        d      } | j                         dk(  sJ | t        d      k(  sJ | t        d      k7  sJ t        |       t        t        d            k(  sJ t        |       t        t        d            k7  sJ | j                  i ddi      dk(  sJ | j                  i ddi      dk(  sJ t	        |       dk(  sJ | j
                  J t        dd      }|j
                  dk(  sJ t        dd	d
d      }|j                  i dg di      }|j                  g dk(  sJ |j                  d
k(  sJ |j                  dk(  sJ dd l	}|j                  t        t         dd
       |j                  t        t         dd       t        t        d             y )Nr"   r#   r   r    zLookupFactor('a')asdf)r\   cTCONTRAST)r   r    )r`   ra   r   )r   r   r    r   nc)ra   )r   )r   r   rt   r   reprr\   r   ra   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*   