
    J/Ph                     2   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	 ddlm
Z
mZmZmZmZ ddlmZ ddlmZ dd	lmZmZ  G d
 d          Zd Z G d d          Zd Z G d de          Zd Zd Zd Zd Zd Z d Z! G d dej"                  Z#d Z$dS ))
DesignInfo
FactorInfoSubtermInfoDesignMatrix    N)
PatsyError)atleast_2d_column_default)OrderedDict)repr_pretty_delegaterepr_pretty_implsafe_issubdtypeno_picklingassert_no_pickling)linear_constraint)ContrastMatrix)	ModelDescTermc                   (    e Zd ZdZddZeZd ZeZ	dS )r   a3  A FactorInfo object is a simple class that provides some metadata about
    the role of a factor within a model. :attr:`DesignInfo.factor_infos` is
    a dictionary which maps factor objects to FactorInfo objects for each
    factor in the model.

    .. versionadded:: 0.4.0

    Attributes:

    .. attribute:: factor

       The factor object being described.

    .. attribute:: type

       The type of the factor -- either the string ``"numerical"`` or the
       string ``"categorical"``.

    .. attribute:: state

       An opaque object which holds the state needed to evaluate this
       factor on new data (e.g., for prediction). See
       :meth:`factor_protocol.eval`.

    .. attribute:: num_columns

       For numerical factors, the number of columns this factor produces. For
       categorical factors, this attribute will always be ``None``.

    .. attribute:: categories

       For categorical factors, a tuple of the possible categories this factor
       takes on, in order. For numerical factors, this attribute will always be
       ``None``.
    Nc                 h   || _         || _        | j        dvrt          d| j                  || _        | j        dk    r6t	          |t
                    st          d          |t          d          n-| j        dk    sJ |t          d          t          |          }|| _        || _        d S )N)	numericalcategoricalz:FactorInfo.type must be 'numerical' or 'categorical', not r   z5For numerical factors, num_columns must be an integerz.For numerical factors, categories must be Noner   z1For categorical factors, num_columns must be None)	factortype
ValueErrorstate
isinstanceinttuplenum_columns
categories)selfr   r   r   r   r   s         Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/patsy/design_info.py__init__zFactorInfo.__init__O   s    	9888*:>))F   
9##k3//  N   % !TUUU & 9----& !WXXXz**J&$    c                    |rJ  G d dt                     }d| j        fd| j        fd |            fg}| j        dk    r|                    d| j        f           n|                    d| j        f           t          || g |           d S )	Nc                       e Zd Zd ZdS )-FactorInfo._repr_pretty_.<locals>.FactorStatec                     dS )Nz<factor state> r    s    r!   __repr__z6FactorInfo._repr_pretty_.<locals>.FactorState.__repr__m   s    ''r#   N)__name__
__module____qualname__r*   r(   r#   r!   FactorStater&   l   s#        ( ( ( ( (r#   r.   r   r   r   r   r   r   )objectr   r   appendr   r   r   )r    pcycler.   kwlists        r!   _repr_pretty_zFactorInfo._repr_pretty_i   s    	( 	( 	( 	( 	(& 	( 	( 	(
 t{#TY kkmm$	
 9##MM=$*:;<<<<MM<9:::D"f-----r#   NN
r+   r,   r-   __doc__r"   r
   r*   r4   r   __getstate__r(   r#   r!   r   r   *   sJ        " "H% % % %0 $H. . .. LLLr#   r   c            	         t          ddddid          } | j        dk    sJ | j        ddik    sJ | j        dk    sJ | j        dk    sJ | j        J t          |            t          ddddid	d
g          }|j        dk    sJ |j        ddik    sJ |j        dk    sJ |j        J |j        dk    sJ t          |           dd l}|                    t          t           ddi            |                    t          t           ddi            |                    t          t           ddi d           |                    t          t           ddi dd           |                    t          t           ddi            |                    t          t           ddi d           |                    t          t           ddi d           d S )Nasdfr   a   
   r   r      zjr   )r@   rA   r   znon-numerical)r   r   )r   r   r   r   r   r   reprpytestraisesr   	TypeError)fi1fi2rD   s      r!   test_FactorInforI      s   
V[3(
C
C
CC:9a    8{""""?b    >!!! 	III
V]S!H#s
L
L
LC:9a    8}$$$$?""">Z'''' 	IIIMMM
MM*j&/2FFF
MM*j&+rBBB
MM*j&+rvMVVV
MMJRQST     MM)ZCCC
MM*j&-QRMSSS
MM)ZqMQQQQQr#   c                   &    e Zd ZdZd ZeZd ZeZ	dS )r   a  A SubtermInfo object is a simple metadata container describing a single
    primitive interaction and how it is coded in our design matrix. Our final
    design matrix is produced by coding each primitive interaction in order
    from left to right, and then stacking the resulting columns. For each
    :class:`Term`, we have one or more of these objects which describe how
    that term is encoded. :attr:`DesignInfo.term_codings` is a dictionary
    which maps term objects to lists of SubtermInfo objects.

    To code a primitive interaction, the following steps are performed:

    * Evaluate each factor on the provided data.
    * Encode each factor into one or more proto-columns. For numerical
      factors, these proto-columns are identical to whatever the factor
      evaluates to; for categorical factors, they are encoded using a
      specified contrast matrix.
    * Form all pairwise, elementwise products between proto-columns generated
      by different factors. (For example, if factor 1 generated proto-columns
      A and B, and factor 2 generated proto-columns C and D, then our final
      columns are ``A * C``, ``B * C``, ``A * D``, ``B * D``.)
    * The resulting columns are stored directly into the final design matrix.

    Sometimes multiple primitive interactions are needed to encode a single
    term; this occurs, for example, in the formula ``"1 + a:b"`` when ``a``
    and ``b`` are categorical. See :ref:`formulas-building` for full details.

    .. versionadded:: 0.4.0

    Attributes:

    .. attribute:: factors

       The factors which appear in this subterm's interaction.

    .. attribute:: contrast_matrices

       A dict mapping factor objects to :class:`ContrastMatrix` objects,
       describing how each categorical factor in this interaction is coded.

    .. attribute:: num_columns

       The number of design matrix columns which this interaction generates.

    c                    t          |          | _        t          |          }t          |t                    st          d          |                                D ]?\  }}||vrt          d          t          |t                    st          d|          @|| _        t          |t                    st          d          || _
        d S )Nzcontrast_matrices must be dictz+Unexpected factor in contrast_matrices dictzExpected a ContrastMatrix, not znum_columns must be an integer)r   factors	frozensetr   dictr   itemsr   contrast_matricesr   r   )r    rL   rP   r   
factor_setr   contrast_matrixs          r!   r"   zSubtermInfo.__init__   s    W~~w''
+T22 	?=>>>'8'>'>'@'@ 	 	#FOZ'' !NOOOo~>>  j;J?L   "3+s++ 	?=>>>&r#   c           	      b    |rJ t          || g d| j        fd| j        fd| j        fg           d S )NrL   rP   r   )r   rL   rP   r   r    r1   r2   s      r!   r4   zSubtermInfo._repr_pretty_   sX    DL)$d&<= 01			
 		
 		
 		
 		
r#   Nr6   r(   r#   r!   r   r      sE        * *X' ' '" $H
 
 
 LLLr#   r   c                  f   t          t          j        d          ddg          } t          ddgd| id          }|j        dk    sJ |j        d| ik    sJ |j        dk    sJ t          |           dd l}|	                    t          t          d	i d	           |	                    t          t          ddgd	d	           |	                    t          t          ddgd
| id	           |	                    t          t          ddgdd	id	           |	                    t          t          ddgi d           d S )Nr?   r?   [1][2]r;   x   )r;   rY   r   r<   r@   g      ?)r   nponesr   rL   rP   r   rC   rD   rE   rF   r   )cmsrD   s      r!   test_SubtermInfor_      s*   	%	8	8BS#Jb	1--A9
""""3)++++=A 	GGGMMM
MM)[!R333
MM*kC:q!<<<
MM*kC:Ry!DDD
MM*kC:QxCCC
MM*kC:r3?????r#   c                       e Zd ZdZddZeZd Zed             Z	ed             Z
ed             Zed             Zed	             Zd
 Zd Zd Zd Zedd            ZeZdS )r   af  A DesignInfo object holds metadata about a design matrix.

    This is the main object that Patsy uses to pass metadata about a design
    matrix to statistical libraries, in order to allow further downstream
    processing like intelligent tests, prediction on new data, etc. Usually
    encountered as the `.design_info` attribute on design matrices.

    Nc           
      H   t          t          |t          t          |                                        | _        |d u |d u k    rt          d          || _        || _        | j        t          | j        t                    st          d          t          | j        t                     st          d          | j        
                                D ]\  }}t          |t                    st          d|          t          |t                    st          d          t          |j                  }|D ]R}t          |t                    st          d|          |                    |j                  st          d          St                      }| j        D ]}|                    |j                   |t          | j                  k    rt          d          | j        
                                D ]F\  }	}
t          |
t$                    st          d	|
          |	|
j        k    rt          d
          G| j        
                                D ]\  }}|D ]}d}t                      }|j        D ]}	| j        |	         }|j        dk    r||j        z  }%|j        dk    sJ |j        |	         j        }|j        d         t          |j                  k    rt          d|	          |                    |	           ||j        d         z  }|t          |j                  k    rt          d          ||j        k    rt          d          |Rd | _        |}d t          t          |                    D             }t          t          ||                    | _        nt                      | _        d}| j        
                                D ]6\  }}d}|D ]}||j        z  }t;          |||z             | j        |<   ||z  }7|t          | j                  k    rt          d          t          d | j        
                                D                       | _        | j        J | j        Pt          | j                                                  t          | j                                                  k    sJ d}| j                                        D ]:}|                     t          |                    \  }}}||k    sJ |dk    sJ |};|t          |          k    sJ | j        
                                D ]A\  }}|| j        v r3| j        |         }|t;          ||dz             k    rt          d          Bd S )NzFMust specify either both or neither of factor_infos= and term_codings=zfactor_infos should be a dictz#term_codings must be an OrderedDictzexpected a Term, not zterm_codings must contain listszexpected SubtermInfo, not zunexpected factors in subtermz3Provided Term objects and factor_infos do not matchz expected FactorInfo object, not zmismatched factor_info.factorr<   r   r   r   z&Mismatched contrast matrix for factor z:Mismatch between contrast_matrices and categorical factorszUnexpected num_columnsc                 4    g | ]}t          ||d z             S )r<   )slice).0is     r!   
<listcomp>z'DesignInfo.__init__.<locals>.<listcomp>e  s$    HHH!eAq1uooHHHr#   z>mismatch between column_names and columns coded by given termsc                 @    g | ]\  }}|                                 |fS r(   name)rd   termslice_s      r!   rf   z'DesignInfo.__init__.<locals>.<listcomp>v  s)    VVV>D&$))++v&VVVr#   zterm/column name collision)!r	   ziprangelencolumn_name_indexesr   factor_infosterm_codingsr   rN   rO   r   listsetrL   r   
issupersetupdater   r   r   r   rP   matrixshaper   addterm_slicesterm_name_slicesrc   column_namesvaluesindices)r    r{   rp   rq   rj   subtermsterm_factorssubtermall_factorsr   factor_infoexp_colscat_factorsfir]   
term_namesslicesidxsubterm_infosterm_columnssubterm_infocoveredrk   startstopstepcolumn_nameindexs                               r!   r"   zDesignInfo.__init__  sw   #.eC$5$56677$
 $
  D ld&:;;2  
 )(
 (d/66 B !@AAAd/== H !FGGG"&"3"9"9";"; 
J 
Jh!$-- J$*$$%HIII!(D11 H$%FGGG"4<00' J JG%g{;; Y(jgg)WXXX'227?CC J()HIIIJJ %%K) 1 1""4<0000c$"34444 L   (,'8'>'>'@'@ F F#!+z:: $*@KM   [///$%DEEE 0 #'"3"9"9";"; C Ch' C CG H"%%%K")/ 4 4!.v67k11$6HH#%7m#;#;#;#;!(!:6!B!IB!x{c"-.@.@@@&0j7=v%@'" '" !" (OOF333$3HH"c'*C&D&DDD(6    7#666()ABBB 7-C2 #D%JHHuS5F5F/G/GHHHF$/J0G0G$H$HD!!  +}}DC'+'8'>'>'@'@ $ $#m $1 = =L L$<<LL).sC,4F)G)G &|#c$+,,,, W   %0VVT=M=S=S=U=UVVV% %D! $000'(//1122d%,,..7 7     +2244 	 	F &s</@/@ A AE4G####19999GG#l++++++ #'":"@"@"B"B 	C 	CKd333.{;U5%!)4444$%ABBB		C 	Cr#   c                 ^    |rJ t          || | j        gd| j        fd| j        fg           d S )Nrp   rq   )r   r{   rp   rq   rT   s      r!   r4   zDesignInfo._repr_pretty_  sQ    d/0>4CT2UV		
 	
 	
 	
 	
r#   c                 *    t          | j                  S )z%A list of the column names, in order.)rr   ro   r)   s    r!   r{   zDesignInfo.column_names  s     D,---r#   c                 <    | j         dS t          | j                   S )z1A list of :class:`Terms`, in order, or else None.N)ry   rr   r)   s    r!   termszDesignInfo.terms  s#     #4D$%%%r#   c                 *    t          | j                  S )zA list of terms, in order.)rr   rz   r)   s    r!   r   zDesignInfo.term_names  s     D)***r#   c                 L    t          j        t          d          d           | S ).. deprecated:: 0.4.0zThe DesignInfo.builder attribute is deprecated starting in patsy v0.4.0; distinct builder objects have been eliminated and design_info.builder is now just a long-winded way of writing 'design_info' (i.e. the .builder attribute just returns self)r?   
stacklevelwarningswarnDeprecationWarningr)   s    r!   builderzDesignInfo.builder  s;     	   		
 		
 		
 		
 r#   c                 L    t          j        t          d          d           | S )r   zStarting in patsy v0.4.0, the DesignMatrixBuilder class has been merged into the DesignInfo class. So there's no need to use builder.design_info to access the DesignInfo; 'builder' already *is* a DesignInfo.r?   r   r   r)   s    r!   design_infozDesignInfo.design_info  s;     	-  	
 	
 	
 	
 r#   c                    t          |t                    r|S t          j        t	          |          t          j                  rt          ||dz             S | j        || j        v r| j        |         S || j        v r| j        |         S || j        v r | j        |         }t          ||dz             S t          d|d          )a
  Locate a subset of design matrix columns, specified symbolically.

        A patsy design matrix has two levels of structure: the individual
        columns (which are named), and the :ref:`terms <formulas>` in
        the formula that generated those columns. This is a one-to-many
        relationship: a single term may span several columns. This method
        provides a user-friendly API for locating those columns.

        (While we talk about columns here, this is probably most useful for
        indexing into other arrays that are derived from the design matrix,
        such as regression coefficients or covariance matrices.)

        The `columns_specifier` argument can take a number of forms:

        * A term name
        * A column name
        * A :class:`Term` object
        * An integer giving a raw index
        * A raw slice object

        In all cases, a Python :func:`slice` object is returned, which can be
        used directly for indexing.

        Example::

          y, X = dmatrices("y ~ a", demo_data("y", "a", nlevels=3))
          betas = np.linalg.lstsq(X, y)[0]
          a_betas = betas[X.design_info.slice("a")]

        (If you want to look up a single individual column by name, use
        ``design_info.column_name_indexes[name]``.)
        r<   Nzunknown column specified '')
r   rc   r[   
issubdtyper   integerry   rz   ro   r   )r    columns_specifierr   s      r!   rc   zDesignInfo.slice  s    B '// 	%$$=/00"*== 	C*,=,ABBB',=AQ,Q,Q#$566 555():;; 888*+<=CcAg&&&j<M<M<MOPPPr#   c                 ,    t          || j                  S )a  Construct a linear constraint in matrix form from a (possibly
        symbolic) description.

        Possible inputs:

        * A dictionary which is taken as a set of equality constraint. Keys
          can be either string column names, or integer column indexes.
        * A string giving a arithmetic expression referring to the matrix
          columns by name.
        * A list of such strings which are ANDed together.
        * A tuple (A, b) where A and b are array_likes, and the constraint is
          Ax = b. If necessary, these will be coerced to the proper
          dimensionality by appending dimensions with size 1.

        The string-based language has the standard arithmetic operators, / * +
        - and parentheses, plus "=" is used for equality and "," is used to
        AND together multiple constraint equations within a string. You can
        If no = appears in some expression, then that expression is assumed to
        be equal to zero. Division is always float-based, even if
        ``__future__.true_division`` isn't in effect.

        Returns a :class:`LinearConstraint` object.

        Examples::

          di = DesignInfo(["x1", "x2", "x3"])

          # Equivalent ways to write x1 == 0:
          di.linear_constraint({"x1": 0})  # by name
          di.linear_constraint({0: 0})  # by index
          di.linear_constraint("x1 = 0")  # string based
          di.linear_constraint("x1")  # can leave out "= 0"
          di.linear_constraint("2 * x1 = (x1 + 2 * x1) / 3")
          di.linear_constraint(([1, 0, 0], 0))  # constraint matrices

          # Equivalent ways to write x1 == 0 and x3 == 10
          di.linear_constraint({"x1": 0, "x3": 10})
          di.linear_constraint({0: 0, 2: 10})
          di.linear_constraint({0: 0, "x3": 10})
          di.linear_constraint("x1 = 0, x3 = 10")
          di.linear_constraint("x1, x3 = 10")
          di.linear_constraint(["x1", "x3 = 0"])  # list of strings
          di.linear_constraint("x1 = 0, x3 - 10 = x1")
          di.linear_constraint([[1, 0, 0], [0, 0, 1]], [0, 10])

          # You can also chain together equalities, just like Python:
          di.linear_constraint("x1 = x2 = 3")
        )r   r{   )r    constraint_likess     r!   r   zDesignInfo.linear_constraint  s    b !!143DEEEr#   c                     g }| j         D ]3}|dk    r|                    d           |                    |           4d                    |          S )a  Returns a human-readable string describing this design info.

        Example:

        .. ipython::

          In [1]: y, X = dmatrices("y ~ x1 + x2", demo_data("y", "x1", "x2"))

          In [2]: y.design_info.describe()
          Out[2]: 'y'

          In [3]: X.design_info.describe()
          Out[3]: '1 + x1 + x2'

        .. warning::

           There is no guarantee that the strings returned by this function
           can be parsed as formulas, or that if they can be parsed as a
           formula that they will produce a model equivalent to the one you
           started with. This function produces a best-effort description
           intended for humans to read.

        	Intercept1z + )r   r0   join)r    namesri   s      r!   describezDesignInfo.describe1  sa    2 O 	# 	#D{""S!!!!T""""zz%   r#   c                 d   t          |t                    r;t          j        |          }|j        rt          d          d |j        D             }| j        1g }|D ]}|| j        | j	        |                  z  }t          |          S i }| j        D ]}|||                                <   g }i }t                      }	|D ]_}
|                    |
|
          }| j        |         }|| j        |         z  }|j        D ]}| j        |         ||<   | j        |         |	|<   `t          |||	          S )an
  Create a new :class:`DesignInfo` for design matrices that contain a
        subset of the terms that the current :class:`DesignInfo` does.

        For example, if ``design_info`` has terms ``x``, ``y``, and ``z``,
        then::

          design_info2 = design_info.subset(["x", "z"])

        will return a new DesignInfo that can be used to construct design
        matrices with only the columns corresponding to the terms ``x`` and
        ``z``. After we do this, then in general these two expressions will
        return the same thing (here we assume that ``x``, ``y``, and ``z``
        each generate a single column of the output)::

          build_design_matrix([design_info], data)[0][:, [0, 2]]
          build_design_matrix([design_info2], data)[0]

        However, a critical difference is that in the second case, ``data``
        need not contain any values for ``y``. This is very useful when doing
        prediction using a subset of a model, in which situation R usually
        forces you to specify dummy values for ``y``.

        If using a formula to specify the terms to include, remember that like
        any formula, the intercept term will be included by default, so use
        ``0`` or ``-1`` in your formula if you want to avoid this.

        This method can also be used to reorder the terms in your design
        matrix, in case you want to do that for some reason. I can't think of
        any.

        Note that this method will generally *not* produce the same result as
        creating a new model directly. Consider these DesignInfo objects::

            design1 = dmatrix("1 + C(a)", data)
            design2 = design1.subset("0 + C(a)")
            design3 = dmatrix("0 + C(a)", data)

        Here ``design2`` and ``design3`` will both produce design matrices
        that contain an encoding of ``C(a)`` without any intercept term. But
        ``design3`` uses a full-rank encoding for the categorical term
        ``C(a)``, while ``design2`` uses the same reduced-rank encoding as
        ``design1``.

        :arg which_terms: The terms which should be kept in the new
          :class:`DesignMatrixBuilder`. If this is a string, then it is parsed
          as a formula, and then the names of the resulting terms are taken as
          the terms to keep. If it is a list, then it can contain a mixture of
          term names (as strings) and :class:`Term` objects.

        .. versionadded: 0.2.0
           New method on the class DesignMatrixBuilder.

        .. versionchanged: 0.4.0
           Moved from DesignMatrixBuilder to DesignInfo, as part of the
           removal of DesignMatrixBuilder.

        z%right-hand-side-only formula requiredc                 6    g | ]}|                                 S r(   rh   )rd   rj   s     r!   rf   z%DesignInfo.subset.<locals>.<listcomp>  s     EEE4499;;EEEr#   N)rp   rq   )r   strr   from_formulalhs_termlistr   rhs_termlistrq   r{   rz   r   ri   r	   getry   rL   rp   )r    which_termsdesc	new_namestterm_name_to_termrj   new_column_namesnew_factor_infosnew_term_codingsname_or_termr^   fs                r!   subsetzDesignInfo.subsetR  s   t k3'' 	F)+66D  J !HIIIEE43DEEEK$ I  I IT.t/DQ/GHH		i((( ") 6 615!$))++..!!*}} + A A(,,\<HH$T* D$5a$88  ? ?A*.*;A*>$Q'')-):4)@ && --   r#   columnc                    t          |d          rt          |j        |           r|j        S t          |d          }|j        dk    rt          d          t          |dt          |j        d                             }t          |d          r,t          |j
        t          j                  sd	 |D             }nfd
|D             }t          |          S )a  Find or construct a DesignInfo appropriate for a given array_like.

        If the input `array_like` already has a ``.design_info``
        attribute, then it will be returned. Otherwise, a new DesignInfo
        object will be constructed, using names either taken from the
        `array_like` (e.g., for a pandas DataFrame with named columns), or
        constructed using `default_column_prefix`.

        This is how :func:`dmatrix` (for example) creates a DesignInfo object
        if an arbitrary matrix is passed in.

        :arg array_like: An ndarray or pandas container.
        :arg default_column_prefix: If it's necessary to invent column names,
          then this will be used to construct them.
        :returns: a DesignInfo object
        r   T)preserve_pandasr?   z&design matrix can't have >2 dimensionscolumnsr<   dtypec                 ,    g | ]}t          |          S r(   )r   )rd   objs     r!   rf   z)DesignInfo.from_array.<locals>.<listcomp>  s    888CHH888r#   c                     g | ]}|	S r(   r(   )rd   re   default_column_prefixs     r!   rf   z)DesignInfo.from_array.<locals>.<listcomp>  s$    QQQA&;&;QQ?QQQr#   )hasattrr   r   r   ndimr   getattrrm   rw   r   r   r[   r   r   )cls
array_liker   arrr   r{   s     `   r!   
from_arrayzDesignInfo.from_array  s    $ :}-- 	**"C3
 3
 	* ))'
DIII8a<<EFFF#y%	!*=*=>>7G$$ 	R_W]BJ-W-W 	R88888LLQQQQQQQL,'''r#   r5   )r   )r+   r,   r-   r7   r"   r
   r*   r4   propertyr{   r   r   r   r   rc   r   r   r   classmethodr   r   r8   r(   r#   r!   r   r   
  sB        AC AC AC ACF $H
 
 
 . . X. & & X& + + X+   X   X,Q ,Q ,Q\1F 1F 1Ff! ! !B[ [ [z ( ( ( [(> LLLr#   r   c                     dd l }  G d dt                    } |d          } |d          }t          |g          }t          |g          }|t          |di d          |t          |di d	          i}t	          |t          |gi d          gf|t          |gi d	          gfg          }t          g d
||          }|j        g d
k    sJ |j        ddgk    sJ |j	        ||gk    sJ |j
        dd	ddd
k    sJ |j        t          dd          t          dd          dk    sJ |j        |t          dd          |t          dd          ik    sJ |                                dk    sJ |                    d	          t          d	d          k    sJ |                    d          t          dd	          k    sJ |                    d          t          d	d          k    sJ |                    d          t          dd          k    sJ |                    d          t          dd          k    sJ |                    |          t          dd          k    sJ |                    d          t          dd          k    sJ |                    |          t          dd          k    sJ |                    t          dd                    t          dd          k    sJ |                     t           |j        d           t#          |           t%          |           t          g d          }|j        g dk    sJ |j        g dk    sJ |j	        J |j
        dd	dddk    sJ |j        t          dd	          t          d	d          t          dd          t          dd          dk    sJ |j        J |                                dk    sJ |                    d	          t          d	d          k    sJ |                    d          t          dd	          k    sJ |                    d          t          d	d          k    sJ |                    d          t          dd          k    sJ |                    d          t          dd          k    sJ t          g d                                          dk    sJ |                     t&          t          g d
|           |                     t&          t          g d
|           |                     t&          t          g d
t)          |          |           |                     t&          t          g d||           |                     t&          t          g d||           |                     t&          t          g d||           |                     t&          t          g d ||           t	          |          }	|	|= |                     t&          t          g d||	            |d!          }
t          |
g          }t	          |          }t          |
gi d	          g||<   |                     t&          t          g d"||           t+          |          }d#||<   |                     t&          t          g d
||           t+          |          }t          |
di d          ||<   |                     t&          t          g d
||           |                     t&          t          g d
|t+          |                     t	          |          }||         |d$<   |                     t&          t          g d
||           t	          |          }t-          ||                   ||<   |                     t&          t          g d
||           t	          |          }d%||         d<   |                     t&          t          g d
||           t	          |          }t          |gi d	          ||         d<   |                     t&          t          g d
||           |
t          |
d&i ddg'          i}t	          |t          |
g|
t/          t1          j        d(          d)d*g          id          gfg          }|                     t&          t          d+d,g||           t          |
|g          }|
t          |
d&i ddg'          |t          |di d          i}t	          |t          |
|g|
t/          t1          j        d-          d)d*g          |t/          t1          j        d-          d)d*g          id          gfg          }|                     t&          t          g d.||           t	          |t          |
|gi d          gfg          }|                     t&          t4          ft          g d.||           t	          |t          |
|g|
t/          t1          j        d/          g d0          id1          gfg          }|                     t&          t          g d2||           d S )3Nr   c                       e Zd Zd Zd ZdS )$test_DesignInfo.<locals>._MockFactorc                     || _         d S N_name)r    ri   s     r!   r"   z-test_DesignInfo.<locals>._MockFactor.__init__  s    DJJJr#   c                     | j         S r   r   r)   s    r!   ri   z)test_DesignInfo.<locals>._MockFactor.name  s
    :r#   N)r+   r,   r-   r"   ri   r(   r#   r!   _MockFactorr     s2        	 	 		 	 	 	 	r#   r   rY   yr      r>   r<   )x1x2x3r   r?   rZ   )rY   r   zx + yr   r   r   r:   a1a2a3bza1 + a2 + a3 + br   r   r   r   )r   r;   r   z	1 + a + b)rp   )rq   )r   r   r   y1y2)r   r   r   )r   r   r   r   )r   r   r   r   r;   )r   r   r   r   r;   zwhat is this I don't evenzthis is a stringznot a SubtermInfor   rB   )r   r?   rW   rX   za[1]za[2]rV   )	a[1]:x[1]	a[2]:x[1]	a[1]:x[2]	a[2]:x[2])r?   r   )rW   rX   z[3]   )r   r   z	a[3]:x[1]r   r   z	a[3]:x[2])rD   r/   r   r   r	   r   r   r{   r   r   ro   rz   rc   ry   r   rE   r   rC   r   r   rr   rN   r   r   r[   r\   KeyError)rD   r   f_xf_yt_xt_yrp   rq   diterm_codings_x_onlyf_at_aterm_codings_with_anot_factor_infosmismatch_factor_infosnot_term_codingsnon_list_term_codingsnon_subterm_term_codingsbad_subtermfactor_codings_aterm_codings_a_bad_rowst_axfactor_codings_axterm_codings_ax_extra_cmterm_codings_ax_missing_cm%term_codings_ax_wrong_subterm_columnss                             r!   test_DesignInfor    s   MMM    f    +c

C
+c

C
u++C
u++CZ["!<<<Z["!<<<L 
SE2q))*	+cKr14M4M3N-OP L 
+++\<	H	HB?5555555=S#J&&&&8Sz!!!!!AQaa%H%HHHHHa%1++"F"FFFFF>c5A;;U1a[[AAAAA;;==G####88A;;%1++%%%%88D>>U1a[[((((88D>>U1a[[((((88D>>U1a[[((((88C==E!QKK''''88C==E!QKK''''88C==E!QKK''''88C==E!QKK''''88E!QKK  E!QKK////
MM*bh/// 	HHHr 
+++	,	,B?5555555=33333338!AQaa%H%HHHHHAqkkAqkkAqkk1a[[	# #     >!!!;;==.....88A;;%1++%%%%88D>>U1a[[((((88D>>U1a[[((((88D>>U1a[[((((88C==E!QKK'''' ---..7799[HHHH MMJ 7 7 7l     MMJ 7 7 7l     MM\   MM&&&   MMJ 2 2 2L,   MMJ 7 7 7|   MMJ 7 7 7|  
 &l33C 
MMJ 2 2 2LBU  
 +c

C
u++C%l33 +SE2q 9 9:
MM$$$   L))7S
MMJ 7 7 79I<   !..!+Cba!P!P!P#
MM   MM\   #<00+7+<'(
MMJ 7 7 7GW   (55!&|C'8!9!9#
MM    +<88':S!!$
MM    l++K&ub!44La
MMJ 7 7 7{   	Z]BD$<HHH * ^BGFOOeU^%T%TUWX 		
  MM	   c
DZ]BD$<HHHZ["!<<<  + c
%!P!P%!P!P  		
   " MM<<<    "-tk3*bRS6T6T5U.V-W!X!X MM	X<<<"   -8 c
nRWV__>S>S>STTU	 
	
- -) MMVVV-    r#   c                  V   t                               g d          } | j        dgk    sJ t                               ddgddgddgg          }|j        ddgk    sJ t                               g dd	          }|j        d
gk    sJ t                               ddgddgddggd	          }|j        d
dgk    sJ t          g d|          }t                               |          |u sJ d|_        t                               |          }|j        dgk    sJ dd l}|                    t          t           j        t          j	        d                     ddl
m} |rdd l}t                               |                    ddggddg                    }	|	j        ddgk    sJ t                               |                    ddggddg                    }
|
j        ddgk    sJ |                    ddgg          }|
|_        t                               |          |
u sJ d S d S )N)r<   r?   r   column0r<   r?   r   rZ   column1rY   r   x0r   r:   r   )r?   r?   r?   )have_pandasr;   r   )r   r=   column10)r   r   r{   r   r   rD   rE   r   r[   r\   
patsy.utilr
  pandas	DataFrame)r   di2di3di4mdi_weirdrD   r
  r  di5di6dfs               r!   test_DesignInfo_from_arrayr    sx   			yyy	)	)B?yk))))


!Q!Q!Q 8
9
9C	955555


			

E
ECv%%%%


!Q!Q!Q 8PS

T
TCd|++++YYY$$A  ##s****AM$$Q''H YK////MMM
MM*j3RWY5G5GHHH&&&&&& 0 ##F$4$4q!fXSz$4$R$RSSC:----##F$4$4q!fX2w$4$O$OPPIz#:::::1vh''$$R((C////0 0 0/r#   c                     t          g d          } |                     ddg          }|j        g dk    sJ t          j        |j        g dg dgk              sJ t          j        |j        dgdggk              sJ d S )Nr   z2 * a1 = b + 1r   )r?   r   r   )r   r   r<   r   r<   r   )r   r   variable_namesr[   allcoefs	constants)r   cons     r!   !test_DesignInfo_linear_constraintr    s    	+++	,	,B


 0$7
8
8C!8!8!888886#)|||<<=====6#-QC!:-.......r#   c                  `    t          ddg          fd}  | d            | d           d S )Nr   r   c                 
   t          j        d          5 }t          j        d           t          |           u sJ 	 d d d            n# 1 swxY w Y   t	          |          dk    sJ |d         j        t          u sJ d S )NT)recordalwaysr<   r   )r   catch_warningssimplefilterr   rn   categoryr   )attrwds     r!   checkz4test_DesignInfo_deprecated_attributes.<locals>.check  s    $D111 	)Q!(+++1d##q(((((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 1vv{{{{t} 2222222s   )AAAr   r   )r   )r*  r)  s    @r!   %test_DesignInfo_deprecated_attributesr+    sU    D$<  A3 3 3 3 3 
E)	E-r#   c                    dt          |           z   dz   |j        dk    sJ t          d          t          j        fd|D             t
                    }t          j        fd|D                       }t          j        |          d         }d	t          j        |          rK	 t          j        fd||         D                       r|D ]}||         d d         ||<   n
d	k    rdnnJ|S )Nz%.r   r<   z+-0123456789.c                     g | ]}|fz  	S r(   r(   )rd   rY   
format_strs     r!   rf   z(_format_float_column.<locals>.<listcomp>,  s     888qqd*888r#   r   c                 B    g | ]}                     |          od |v S ).)rt   )rd   col_strsimple_float_charss     r!   rf   z(_format_float_column.<locals>.<listcomp>0  s@     	
 	
 	
 ))'22Esg~	
 	
 	
r#   r   0Tc                 :    g | ]}|                               S r(   )endswith)rd   r^   
strip_chars     r!   rf   z(_format_float_column.<locals>.<listcomp>9  s%    FFF!qzz*--FFFr#   r  r1  )	r   r   rs   r[   arrayr/   nonzeroanyr  )		precisioncolcol_strsmask
mask_idxesr   r.  r3  r7  s	         @@@r!   _format_float_columnr@  '  sC   I&,J8q====_--x8888C888GGGH 8	
 	
 	
 	
#	
 	
 	
 D D!!!$JJ	vd|| 		vFFFFx~FFFGG % 7 7C$,SM#2#$6HSMM7 $$!$JJ	 Or#   c                  $   d } dt           j        fz  } | ddddt           j        gddd	|g            | ddd
dt           j        gddd|g            | ddd
dt           j        gddd|g            | ddd
dt           j        gddd|g           d S )Nc                     t          | t          j        |                    }t          ||           t          j        ||          sJ d S r   )r@  r[   asarrayprintarray_equal)r;  numbersexpectedgots       r!   r   z$test__format_float_column.<locals>.tE  sI    "9bj.A.ABBc8~c8,,,,,,,r#   z%.3fr   r<   gx# @gH @z1.000z2.123z2.124r?   r   23gqh ?rZ   z1.0001z2.0000z3.0000)r[   nan)r   
nan_strings     r!   test__format_float_columnrM  D  s    - - - 26)#JAa!VVRV	$w*&MNNNAa!Q26	S#sJ7888Aa&!Q	#sC!<===Aa&!Q	(Hh
!KLLLLLr#   c                   (    e Zd ZdZddZeZd ZeZ	dS )r   a1  A simple numpy array subclass that carries design matrix metadata.

    .. attribute:: design_info

       A :class:`DesignInfo` object containing metadata about this design
       matrix.

    This class also defines a fancy __repr__ method with labeled
    columns. Otherwise it is identical to a regular numpy ndarray.

    .. warning::

       You should never check for this class using
       :func:`isinstance`. Limitations of the numpy API mean that it is
       impossible to prevent the creation of numpy arrays that have type
       DesignMatrix, but that are not actually design matrices (and such
       objects will behave like regular ndarrays in every way). Instead, check
       for the presence of a ``.design_info`` attribute -- this will be
       present only on "real" DesignMatrix objects.
    Nr   c                    t          |t                    rt          |d          r|S t          |                              |           }t          |j        t          j                  r.t          j	        |t                                        |           }|j        dk    rt          d          |j        dk    sJ |t                              ||          }t          |j                  |j        d         k    r3t          dt          |j                  d|j        d         d	          ||_        t          |j        t          j                  st          d
          |S )a  Create a DesignMatrix, or cast an existing matrix to a DesignMatrix.

        A call like::

          DesignMatrix(my_array)

        will convert an arbitrary array_like object into a DesignMatrix.

        The return from this function is guaranteed to be a two-dimensional
        ndarray with a real-valued floating point dtype, and a
        ``.design_info`` attribute which matches its shape. If the
        `design_info` argument is not given, then one is created via
        :meth:`DesignInfo.from_array` using the given
        `default_column_prefix`.

        Depending on the input array, it is possible this will pass through
        its input unchanged, or create a view.
        r   r/  r?   zDesignMatrix must be 2dNr<   z4wrong number of column names for design matrix (got z	, wanted )z0design matrix must be real-valued floating point)r   r   r   r   viewr   r   r[   r   rC  floatr   r   r   r   rn   r{   rw   r   floating)r   input_arrayr   r   r    s        r!   __new__zDesignMatrix.__new__j  sX   0 k<00 	W6
 6
 	 (55::3??4:rz22 	;:d%00055c::D9q==6777yA~~~~$//6KLLK{'((DJqM99*),[-E)F)F)F)F
STW   'tz2;77 	QOPPPr#   c                 L   t          | d          s)|                    t          j        |                      d S |rJ d}d}d}d}d| j        j        }d |D             }||| j        d         dz
  z  z   t          j        |          z   }	|	|k    rt          j        |           d |d d f         fdt          | j        d                   D             }
d	 fd
|
D             }d t          ||          D             }||| j        d         dz
  z  z   t          j        |          z   }||k     }nd}|
                    |d| j                   |                    dd|j        z  z              |rd|z  }|gt          t          |
           z   D ]d}d t          ||          D             }|                    |                    |                     |                    dd|j        z  z              e|| j        d         k     rG|                    d| j        d         |z
  d           |                    dd|j        z  z              n|
                    dd           |                    dd|j        z  z              |                    |           |                    dd           |                    dd|j        z  z              |
                    dd           |                    dd|j        z  z              | j        j                                        D ]\  }}|j        dk    r|                    d           |                    |           |j        |j        z
  dk    rd|j        }nd|j        d|j        }|                    d|d           |                    dd           |r| j        d         |k    r5|                    dd|j        z  z              |                    d           |                    |d           d S )Nr   N   r?      r   c                 ,    g | ]}t          |          S r(   rn   )rd   ri   s     r!   rf   z.DesignMatrix._repr_pretty_.<locals>.<listcomp>  s    :::Dc$ii:::r#   r<   c                 D    g | ]}t          d d |f                   S r   )r@  )rd   re   	PRECISIONprintable_parts     r!   rf   z.DesignMatrix._repr_pretty_.<locals>.<listcomp>  s@        %Yqqq!t0DEE  r#   c                 l    | j         dk    sJ | j        d         sdS t          d | D                       S )Nr<   r   c                 ,    g | ]}t          |          S r(   rZ  )rd   r^   s     r!   rf   zADesignMatrix._repr_pretty_.<locals>.max_width.<locals>.<listcomp>  s    4441A444r#   )r   rw   max)r<  s    r!   	max_widthz-DesignMatrix._repr_pretty_.<locals>.max_width  sA    x1}}}}y| 6144444555r#   c                 &    g | ]} |          S r(   r(   )rd   r<  ra  s     r!   rf   z.DesignMatrix._repr_pretty_.<locals>.<listcomp>  s!     J J JC3 J J Jr#   c                 4    g | ]\  }}t          ||          S r(   )r`  )rd   
name_width	num_widths      r!   rf   z.DesignMatrix._repr_pretty_.<locals>.<listcomp>  s6       +Z J	**  r#   FzDesignMatrix with shape 
 c                 >    g | ]\  }}|                     |          S r(   )rjust)rd   widthcells      r!   rf   z.DesignMatrix._repr_pretty_.<locals>.<listcomp>  s(    XXX}tE**XXXr#   r   [z rows omitted]zColumns: zTerms:z, zcolumn zcolumns :z (rP  z-(to view full data, use np.asarray(this_obj)))r   prettyr[   rC  r   r{   rw   sumrm   rl   begin_group	breakableindentationrr   textr   	end_grouprz   rO   r   r   )r    r1   r2   MAX_TOTAL_WIDTHSEPINDENTMAX_ROWSr   column_name_widthsmin_total_widthformatted_colscolumn_num_widthscolumn_widthstotal_widthprint_numbersseprowcells	term_namespancoltextr\  ra  r]  s                        @@@r!   r4   zDesignMatrix._repr_pretty_  s   t]++ 	HHRZ%%&&&F 	 -::E:::SDJqMA-..8J1K1KK 	 o--Z--ixil;N    tz!}--  N
6 6 6 !K J J J> J J J /2&(90 0  M !3$*Q-!*;#<<rvm?T?TTK'/9MM!M	ffdjjJKKK	D3../// 	4)Cwc>&:!;!;; 3 3XXMSV@W@WXXXsxx'''tcAM112222$*Q-''djmh.F.F.FHIIItcAM11222MM!Z(((KKsQ]22333HHUOOOKK2KKsQ]22333	a"""	D3..///#/@FFHH 	) 	)OItzQD!!!HHYy4:%***)-5 + .2ZZZCFFFggg'((((	Ar 	D
1 8 8KKsQ]22333FFBCCC	FBr#   )Nr   )
r+   r,   r-   r7   rU  r
   r*   r4   r   
__reduce__r(   r#   r!   r   r   T  sN         *- - - -^ $HW  W  W z JJJr#   r   c                     dd l } t          g d          }t          g dg|          }|j        j        g dk    sJ t          dg          }|                     t          t          g dg|           t          g dg          }|j        j        g dk    sJ t          g d          }|j        dk    sJ |                     t          t          dggg           t          |          }||u sJ t          |                                          }||usJ t          g dgd	          }|j        j        g d
k    sJ t          |           |                     t          t          g d           |                     t          t          g d           |                     t          t          ddt                      g           t          |           t          t          t          j        d                               t          t          t          j        d          dz                       t          |dd d d f                    t          t          t          j        d                              d                               t          t          t          j        t          j        g                     t          t          t          j        dddg                     t          t          t          j        d                               t          t          t          j        d                               t          t          t          j        d                               d S )Nr   r   )            r   )r  r  column2column3)rZ   r<   r<   rY   r  )r	  r   r   r   )r<   r?   y              @)r;   r   cr?   d   g       @)r<   r  g@xDg     4@)r<   r   )r   r<   )r   r   )rD   r   r   r   r{   rE   r   rw   diagonalr   r/   rC   r[   arangereshaperK  infzeros)	rD   r   mmbad_dimm2mm3mm4mm5mm6s	            r!   test_design_matrixr    s   MMM	+++	,	,B	'''("	-	-B>&*A*A*AAAAAF
MM*l-=-=-=,>GGG
((()
*
*C?'+W+W+WWWWW
'''
(
(C9 MM*lqcUG444 r

C"9999
r{{}}
%
%Cb====
((()
E
E
EC?'+C+C+CCCCCs MM*lJJJ777
MM*lOOO<<<
MM*lQ688,<=== 	HHHbinn	%	%&&&binns*	+	+,,,ABBEOOObinn,,X66	7	7888rvrv&	'	'(((rvq$-	.	.///bhv&&	'	'(((bhv&&	'	'(((bhv&&	'	'(((((r#   )%__all__r   numpyr[   patsyr   r  r   patsy.compatr	   r
   r   r   r   r   patsy.constraintr   patsy.contrastsr   
patsy.descr   r   r   rI   r   r_   r/   r   r  r  r  r+  r@  rM  ndarrayr   r  r(   r#   r!   <module>r     sL  * F
E
E            0 0 0 0 0 0 $ $ $ $ $ $              / . . . . . * * * * * * & & & & & & & &V V V V V V V Vr!R !R !RHM M M M M M M M`@ @ @&E E E E E E E EPS S Sl"0 "0 "0J/ / /  $  :M M M d d d d d2: d d dN/) /) /) /) /)r#   