
    J/Ph\$                         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dS ))balanced	demo_dataLookupFactor    N)
PatsyError)C)no_picklingassert_no_picklingc            	      X   |                      dd          }g }t          |           }|D ]<|          }|                    fdt          d|dz             D                        =t	          t          j        |  }i }t	          ||          D ]\  }t          |          |z  |<   |S )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   c                     g | ]}|	S  r   ).0inames     O/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/patsy/user_util.py
<listcomp>zbalanced.<locals>.<listcomp>2   s"    MMMaqq)MMM    )popsortedappendrangezip	itertoolsproductlist)	kwargsr   levelsnameslevel_countvaluesdatavaluer   s	           @r   r   r      s    6 ZZ!$$FF6NNE O OTlMMMM5K!O3L3LMMMNNNN)#V,-FD5&)) * *e%[[6)T

Kr   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 d S )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.   ;   s    a1D9<<<<<<<9<<<<<<<a1Q'''D9        9         r   c                     |                     dd          }|                     dd          }|rt          d|          t                      }i }| D ]C}|d         dv r|||<   |d         dv r|                    |           2t	          d	|          t          j        t          |                                          t          
          }t          t          j
        |dz  |z                      }||z  }	t          dd|i|}
t
          j                            d          }t          |          D ]}|                    |	          |
|<   |
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   )sizer   )r   	TypeErrorsetaddr   npprodr   r!   intceilr   randomRandomStater   normal)r   r   r0   r1   	numericalcategoricalr   balanced_design_sizer   num_rowsr"   rs               r   r   r   ^   ss   B jjA&&Gzz*a((H GiVVEFFFIK 6 67&&& 'K!W%%MM$*dd455574(:(:(<(<#=#=SIIIC*>>??@@F,,H1161[11D
	a  Ay!! - -XX8X,,T

Kr   c            	         t          ddd          } t          |                                           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          |                                          ddgk    sJ t          |d                   t          |d                   cxk    rd	k    sn 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}|	                    t          t           ddd           |	                    t          t           ddd           d S )Nr'   r(   x)r'   r(   rG   )r)   r)   r*   r*   r)   r)   r*   r*   )r+   r,   r+   r,   r+   r,   r+   r,   )   yr2   
   )r1      r&   )r1   r0      r   __123{   )asdfasdf)r   r   keysr5   r:   floatshapelenpytestraisesr   r7   )d1d2rT   s      r   test_demo_datarX      s   	3S	!	!B"''))////c7FFFFFFFc7FFFFFFFc7=BHUOO++++c7=D    	3		B"''))c
****r#w<<3r#w<<,,,,1,,,,,,yr***3/00B6666yc3444S9::b@@@@yc3Q???DEEKKKKMMM
MM*ic7;;;
MM)YS3M?????r   c                   V    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dS )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.
    FNc                     || _         || _        || _        || _        || _        | j        s"|t          d          |t          d          d S d S )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__   sr      "3!& 	L# !LMMM! !JKKK		L 	L "!r   c                     | j         S N)r[   ra   s    r   r   zLookupFactor.name   s
    }r   c                 0    | j         j        d| j        dS )N())	__class____name__r[   rh   s    r   __repr__zLookupFactor.__repr__   s    >222DMMMBBr   c                     t          |t                    o?| j        |j        k    o/| j        |j        k    o| j        |j        k    o| j        |j        k    S rg   )
isinstancer   r[   r\   r]   r^   ra   others     r   __eq__zLookupFactor.__eq__   s^    ul++ ./.'5+CC. %/1. -	
r   c                     | |k     S rg   r   rq   s     r   __ne__zLookupFactor.__ne__   s    5=  r   c                 \    t          t          | j        | j        | j        | j        f          S rg   )hashr   r[   r\   r]   r^   rh   s    r   __hash__zLookupFactor.__hash__   s1    '
 
 	
r   c                     dS )Nr   r   )ra   stateeval_envs      r   memorize_passes_neededz#LookupFactor.memorize_passes_needed   s    qr   c                     J rg   r   )ra   rz   
which_passr"   s       r   memorize_chunkzLookupFactor.memorize_chunk       r   c                     J rg   r   )ra   rz   r~   s      r   memorize_finishzLookupFactor.memorize_finish   r   r   c                 f    || j                  }| j        rt          || j        | j                  }|S )N)rd   r   )r[   r\   r   r]   r^   )ra   memorize_stater"   r#   s       r   evalzLookupFactor.eval   s7    T]#" 	KednT\JJJEr   )FNNN)rm   
__module____qualname____doc__re   r   rn   rs   ru   rx   r|   r   r   r   r   __getstate__r   r   r   r   r      s         8 TXL L L L  C C C
 
 
! ! !	
 	
 	
         LLLr   r   c                     t          d          } |                                 dk    sJ | t          d          k    sJ | t          d          k    sJ t          |           t          t          d                    k    sJ t          |           t          t          d                    k    sJ |                     i ddi          dk    s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          }|                    i dg di          }|j        g dk    sJ |j        d
k    sJ |j        dk    sJ dd l	}|
                    t          t           dd
           |
                    t          t           dd           t          t          d                     d S )Nr'   r(   r   r%   zLookupFactor('a')asdf)r_   cTCONTRAST)r   r%   )rc   rd   r   )r   r   r%   r   nc)rd   )r   )r   r   rw   r   reprr_   r"   rd   r   rT   rU   r`   r	   )l_al_with_originl_cboxrT   s        r   test_LookupFactorr     s   
s

C88::,s######,s######99\#..//////99\#..//////88Ba!!Q&&&&88Ba!!Q&&&&99+++++: V444M6))))
sdZPV
W
W
WC
((2YYY'
(
(C8yyy    <:%%%%:MMM
MM*lD:MFFF
MM*lDM@@@|C(()))))r   )__all__r   numpyr:   patsyr   patsy.categoricalr   
patsy.utilr   r	   r   r.   r   rX   objectr   r   r   r   r   <module>r      s    4
3
3                     6 6 6 6 6 6 6 6& & &R     F5 5 5p@ @ @,V V V V V6 V V Vr* * * * *r   