
    J/PhC5                         g d Z ddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZmZmZ erddlZd	 ZddZddZd Zi dd
dfdZi dd
dfdZdS ))dmatrix	dmatricesincr_dbuilderincr_dbuilders    N)
PatsyError)DesignMatrix
DesignInfo)EvalEnvironment)	ModelDesc)design_matrix_buildersbuild_design_matrices)have_pandasasarray_or_pandasatleast_2d_column_defaultc                    t          | t                    rt          g g|||          d         | fS t          | t                    rKt	          |           dk    r8t          | d         t                    rt          | d         t                    r| S t          | d          r=|                     |          } t          | t                    st          d| d          t          | t                    rt          j
        |           } t          | t                    r5t          |t                    sJ t          | j        | j        g|||          S d S )Nr         __patsy_get_model_desc__zbad value from z.__patsy_get_model_desc__)
isinstancer	   r   tuplelenhasattrr   r   r   strfrom_formular
   lhs_termlistrhs_termlist)formula_likedata_iter_makereval_env	NA_actions       O/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/patsy/highlevel.py_try_incr_buildersr"      sf   ,
++ 
"B4(INNqQ
 	

 	<''""|A
33 #|A
33 # |788 #<<XFF,	22 	*@LN   ,$$ < -l;;,	** 	(O44444%&(AB	
 
 	
 t    dropc                     t          j        |d          }t          | |||          }|t          d          t	          |d         j                  dk    rt          d          |d         S )a  Construct a design matrix builder incrementally from a large data set.

    :arg formula_like: Similar to :func:`dmatrix`, except that explicit
      matrices are not allowed. Must be a formula string, a
      :class:`ModelDesc`, a :class:`DesignInfo`, or an object with a
      ``__patsy_get_model_desc__`` method.
    :arg data_iter_maker: A zero-argument callable which returns an iterator
      over dict-like data objects. This must be a callable rather than a
      simple iterator because sufficiently complex formulas may require
      multiple passes over the data (e.g. if there are nested stateful
      transforms).
    :arg eval_env: Either a :class:`EvalEnvironment` which will be used to
      look up any variables referenced in `formula_like` that cannot be
      found in `data`, or else a depth represented as an
      integer which will be passed to :meth:`EvalEnvironment.capture`.
      ``eval_env=0`` means to use the context of the function calling
      :func:`incr_dbuilder` for lookups. If calling this function from a
      library, you probably want ``eval_env=1``, which means that variables
      should be resolved in *your* caller's namespace.
    :arg NA_action: An :class:`NAAction` object or string, used to determine
      what values count as 'missing' for purposes of determining the levels of
      categorical factors.
    :returns: A :class:`DesignInfo`

    Tip: for `data_iter_maker`, write a generator like::

      def iter_maker():
          for data_chunk in my_data_store:
              yield data_chunk

    and pass `iter_maker` (*not* `iter_maker()`).

    .. versionadded:: 0.2.0
       The ``NA_action`` argument.
    r   	referenceNbad formula-like objectr   Cencountered outcome variables for a model that does not expect themr
   capturer"   r   r   column_namesr   r   r   r    design_infoss        r!   r   r   B   s    H &x1===H%ox L 2333
<?'((1,,T
 
 	
 ?r#   c                     t          j        |d          }t          | |||          }|t          d          t	          |d         j                  dk    rt          d          |S )zConstruct two design matrix builders incrementally from a large data
    set.

    :func:`incr_dbuilders` is to :func:`incr_dbuilder` as :func:`dmatrices` is
    to :func:`dmatrix`. See :func:`incr_dbuilder` for details.
    r   r&   Nr(   r   +model is missing required outcome variablesr*   r-   s        r!   r   r   s   sw     &x1===H%ox L 2333
<?'((A--FGGGr#   c                    dk    rt           st          d          dvrt          dd          fd}t          | |||          }|t          ||          S t	          | t
                    r9t          |           dk    r t          d	t          |           d
          | \  }}nd t          | d          }}fd}	 |	|d          \  }}
|(t          j	        |j
        d         dft                    } |	|d          \  }}t	          t          |dd           t                    sJ t	          t          |dd           t                    sJ |j
        d         |j
        d         k    r,t          d|j
        d         d|j
        d         d          |
&|$|
                    |          st          d          dk    r |
||j        |_        |
||j        |_        ||fS )N	dataframez;pandas.DataFrame was requested, but pandas is not installed)matrixr2   zunrecognized output type z#, should be 'matrix' or 'dataframe'c                  $    t           g          S )N)iter)datas   r!   r   z-_do_highlevel_design.<locals>.data_iter_maker   s    TF||r#   )r    return_typer   z$don't know what to do with a length z matrices tupleT)subokc                 D   t          j        | |          }t          r.t          | t          j        t          j        f          r| j        }nd }dk    r<t          | d          } t	          j        |           } |j	        | _
        || _        | |fS t          | |          |fS )Nr2   T)preserve_pandas)r	   
from_arrayr   r   pandasSeries	DataFrameindexr   r,   columnsdesign_infor   )mdefault_column_prefixdi
orig_indexr7   s       r!   _regularize_matrixz0_do_highlevel_design.<locals>._regularize_matrix   s    &q*?@@B "z!fmV=M-NOO "W

!
k))-aFFF$Q''O	 ":&$Q++Z88r#   xr   )dtypeyrA   z#shape mismatch: outcome matrix has z rows, predictor matrix has z rowsz?index mismatch: outcome and predictor have incompatible indexes)r   r   r"   r   r   r   r   r   npzerosshapefloatgetattrr	   equalsr?   )r   r6   r   r    r7   r   r.   lhsrhsrF   rhs_orig_indexlhs_orig_indexs    `  `       r!   _do_highlevel_designrT      s   k!!+!L
 
 	
 111j)47
 
 	

     &ox L $$)
 
 
 	

 lE** 
	M<  A%% j(+L(9(9(9(9<   &JS##  1,d K K K#S	9 	9 	9 	9 	9 10c::^;(CIaL!,E:::C00c::^'#}d;;ZHHHHH'#}d;;ZHHHHH9Q<39Q<''*25)A,,,	!N   %.*D!((88  X   +%%)n.DI	%.*DI	Szr#   r3   c                     t          j        |d          }t          | ||||          \  }}|j        d         dk    rt	          d          |S )a  Construct a single design matrix given a formula_like and data.

    :arg formula_like: An object that can be used to construct a design
      matrix. See below.
    :arg data: A dict-like object that can be used to look up variables
      referenced in `formula_like`.
    :arg eval_env: Either a :class:`EvalEnvironment` which will be used to
      look up any variables referenced in `formula_like` that cannot be
      found in `data`, or else a depth represented as an
      integer which will be passed to :meth:`EvalEnvironment.capture`.
      ``eval_env=0`` means to use the context of the function calling
      :func:`dmatrix` for lookups. If calling this function from a library,
      you probably want ``eval_env=1``, which means that variables should be
      resolved in *your* caller's namespace.
    :arg NA_action: What to do with rows that contain missing values. You can
      ``"drop"`` them, ``"raise"`` an error, or for customization, pass an
      :class:`NAAction` object. See :class:`NAAction` for details on what
      values count as 'missing' (and how to alter this).
    :arg return_type: Either ``"matrix"`` or ``"dataframe"``. See below.

    The `formula_like` can take a variety of forms. You can use any of the
    following:

    * (The most common option) A formula string like ``"x1 + x2"`` (for
      :func:`dmatrix`) or ``"y ~ x1 + x2"`` (for :func:`dmatrices`). For
      details see :ref:`formulas`.
    * A :class:`ModelDesc`, which is a Python object representation of a
      formula. See :ref:`formulas` and :ref:`expert-model-specification` for
      details.
    * A :class:`DesignInfo`.
    * An object that has a method called :meth:`__patsy_get_model_desc__`.
      For details see :ref:`expert-model-specification`.
    * A numpy array_like (for :func:`dmatrix`) or a tuple
      (array_like, array_like) (for :func:`dmatrices`). These will have
      metadata added, representation normalized, and then be returned
      directly. In this case `data` and `eval_env` are
      ignored. There is special handling for two cases:

      * :class:`DesignMatrix` objects will have their :class:`DesignInfo`
        preserved. This allows you to set up custom column names and term
        information even if you aren't using the rest of the patsy
        machinery.
      * :class:`pandas.DataFrame` or :class:`pandas.Series` objects will have
        their (row) indexes checked. If two are passed in, their indexes must
        be aligned. If ``return_type="dataframe"``, then their indexes will be
        preserved on the output.

    Regardless of the input, the return type is always either:

    * A :class:`DesignMatrix`, if ``return_type="matrix"`` (the default)
    * A :class:`pandas.DataFrame`, if ``return_type="dataframe"``.

    The actual contents of the design matrix is identical in both cases, and
    in both cases a :class:`DesignInfo` object will be available in a
    ``.design_info`` attribute on the return value. However, for
    ``return_type="dataframe"``, any pandas indexes on the input (either in
    `data` or directly passed through `formula_like`) will be preserved, which
    may be useful for e.g. time-series models.

    .. versionadded:: 0.2.0
       The ``NA_action`` argument.
    r   r&   r   r)   r
   r+   rT   rL   r   r   r6   r   r    r7   rP   rQ   s          r!   r   r      sg    ~ &x1===H%dHi JS# y|qT
 
 	
 Jr#   c                     t          j        |d          }t          | ||||          \  }}|j        d         dk    rt	          d          ||fS )aW  Construct two design matrices given a formula_like and data.

    This function is identical to :func:`dmatrix`, except that it requires
    (and returns) two matrices instead of one. By convention, the first matrix
    is the "outcome" or "y" data, and the second is the "predictor" or "x"
    data.

    See :func:`dmatrix` for details.
    r   r&   r   r0   rV   rW   s          r!   r   r   2  sc     &x1===H%dHi JS# y|qFGGG:r#   )r   r$   )__all__numpyrJ   patsyr   patsy.design_infor   r	   
patsy.evalr
   
patsy.descr   patsy.buildr   r   
patsy.utilr   r   r   r<   r"   r   r   rT   r   r    r#   r!   <module>rb      sP   F
E
E           6 6 6 6 6 6 6 6 & & & & & &             E E E E E E E E P P P P P P P P P P MMM     F. . . .b   FO O Od  "AX G G G GV AX     r#   