
    J/Phҝ                        d dgZ ddlZddlZddlmZ ddlmZmZm	Z	 ddl
mZmZmZmZ ddl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 ddlmZ ddlmZ erddlZ G d de           Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d&d Z2d! Z3 G d" d#e           Z4dd$ ej5        e6          fd%Z7dS )'design_matrix_buildersbuild_design_matrices    N)
PatsyError)guess_categoricalCategoricalSniffercategorical_to_int)atleast_2d_column_defaulthave_pandasasarray_or_pandassafe_issubdtype)DesignMatrix
DesignInfo
FactorInfoSubtermInfo)pick_contrasts_for_term)EvalEnvironment)code_contrast_matrix	Treatment)OrderedDict)NAActionc                   "    e Zd ZddZd Zd ZdS )_MockFactorMOCKMOCKc                     || _         d S N_name)selfnames     K/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/patsy/build.py__init__z_MockFactor.__init__!   s    


    c                     |d         S )Nmock )r   stateenvs      r    evalz_MockFactor.eval$   s    6{r"   c                     | j         S r   r   r   s    r    r   z_MockFactor.name'   s
    zr"   N)r   __name__
__module____qualname__r!   r(   r   r%   r"   r    r   r       sF                 r"   r   c                     |j         | k    r2d|                                d|j         d| }t          ||          d S )Nzfactor 'z' evaluates to an z;-dimensional array; I only handle arrays with dimension <= )ndimr   r   )dimarrfactormsgs       r    _max_allowed_dimr5   +   sP    
x#~~~ 5;KKMMMM3888SSR 	 f%%% ~r"   c                     dd l } t                      }t          dt          j        d          |           t          dt          j        dg          |           |                     t          t          dt          j        dgg          |           |                     t          t          dt          j        dggg          |           t          dt          j        d          |           t          dt          j        dg          |           t          dt          j        dgg          |           |                     t          t          dt          j        dggg          |           d S )Nr         )pytestr   r5   nparrayraisesr   )r9   fs     r    test__max_allowed_dimr>   4   s!   MMMAQQ'''Q!q)))
MM*.28aSE??AFFF
MM*.28qcUG3D3DaHHHQQ'''Q!q)))Q1#+++
MM*.28qcUG3D3DaHHHHHr"   c           	         | j         }|                    | j        |          }| j        dk    rt	          |d          }t          d||           |j        d         | j        k    r<t          d|	                                d| j        d|j        d         d	|          t          t          j        |          j        t          j                  s.t          d
|	                                d|j        d|          ||                    |          fS | j        dk    sJ t!          || j        || j                   }|j        dk    sJ |t          j        |dk              fS )N	numericalT)preserve_pandasr8   r7   zwhen evaluating factor z, I got z columns instead of the z I was expectingzwhen evaluating numeric factor z", I got non-numeric data of type ''categorical)origin)r3   r(   r&   typer	   r5   shapenum_columnsr   r   r   r:   asarraydtypenumberis_numerical_NAr   
categoriesr0   )factor_infodata	NA_actionr3   results        r    _eval_factorrR   B   s   F[[*D11F;&&*64HHHFF+++<?k555* ;;====+"9"9"96<???L 	   rz&117CC 	*9?V  
 y008888 =0000#K*Ik>P
 
 
 {arz&B,////r"   c                  	   dd l } t                      }t                      }t          |di dd           }|j        |u sJ t          |dg di|          \  }}|j        dk    sJ t          j        |dgdgd	ggk              sJ |j        d
k    sJ t          j        |           sJ | 	                    t          t
          |ddgggi|           | 	                    t          t
          |dddggi|           | 	                    t          t
          |dddgi|           | 	                    t          t
          |dddgi|           t          t                      di dd           }t          |ddd	gddgd	dggi|          \  }}|j        dk    sJ t          j        |dd	gddgd	dggk              sJ |j        d
k    sJ t          j        |           sJ | 	                    t          t
          |dg di|           | 	                    t          t
          |dg dgi|           t          |dddt          j        git          dg                    \  }}t          j        |g d          sJ t          |dddt          j        git          g                     \  }}t          j        |g d          sJ t          r t          |dt          j        g dg d          i|          \  }	}
t!          |	t          j                  sJ t          j        |	dgdgd	gg          sJ t          j        |	j        g d          sJ t          |dt          j        dgdgd	ggg d          i|          \  }}
t!          |t          j                  sJ t          j        |dgdgd	gg          sJ t          j        |j        g d          sJ t          |dt          j        dd	gddgd	dggg d          i|          \  }}
t!          |t          j                  sJ t          j        |dd	gddgd	dgg          sJ t          j        |j        g d          sJ | 	                    t          t
          |dt          j        g dg d          i|           | 	                    t          t
          |dt          j        dd	gddgd	dggg d          i|           d S d S )Nr   r@   r7   rH   rM   r$   r7   r8      rV   r7   r8   rV   rV   abTFrV   r8   NaNNA_types)FFT)FFF)
         index)r`   r_   ra      rE   )r`   ra   r_   )r9   r   r   r   r3   rR   rG   r:   allr<   r   nanarray_equalr
   pandasSeries
isinstance	DataFramerc   )r9   naar=   fi1eval123is_NAfi2
eval123321ev_naneval_ser_eval_df1eval_df2s                r    test__eval_factor_numericalrw   a   s   MMM
**CA
QRQ4
H
H
HC:????!#			':C@@NGU=F""""6'qcA3_,-----;$65&>>
MM*lC&QC5'1BCHHH
MM*lC&Aq6(1CSIII
MM*lC&3*1EsKKK
MM*lC&4-1H#NNN
[]]Kt
T
T
TC$S6QFQFQF3K*LcRRJv%%%%6*!Q!Q!Q 8899999;$65&>>
MM*lC&)))1DcJJJ
MM*lC&999+1FLLL fq!RVn%x%'A'A'A MFE >%!5!5!566666 v1bf~&>RT@U@U@UVVMFE>%!6!6!677777 #
"&&-			FFFG
 
! (F$455555~h!qcA388888~hnlll;;;;;"&&*QC!qc?,,,OOOPRU
 
! (F$455555~h!qcA388888~hnlll;;;;;"V%1v1v2w&?|||TTTU
 
!
 (F$455555~h!Q!Q!R(ABBBBB~hnlll;;;;;V]999LLLAAAB	
 	
 	
 	V%1v1v2w&?|||TTTU	
 	
 	
 	
 	
;#
 #
r"   c                     dd l } ddlm} t                      }t	                      }t          |di d d          }|j        |u sJ t          |dg di|          \  }}|j        dk    sJ t          j
        |g d	k              sJ |                     t          t          |dd
gi|           |                     t          t          |d |dd
g          i|           |                     t          t          |d |ddgddg          i|           |                     t          t          |dg d	i|           t          j        g d          }|                    d           |                     t          t          |d|i|           t          |dg dit          dg                    \  }}	t          j        |	g d          sJ t          j        |g d          sJ |                     t          t          |dg dit          g                      t          t	                      di d ddg          }
t          |
dg di|          \  }}|j        dk    sJ t          j
        |g dk              sJ t           rt#          j        ddgddg          }t          |d|i|          \  }}t'          |t"          j                  sJ t          j        |ddg          sJ t          j        |j        ddg          sJ t#          j        ddgddg          }t          |
d|i|          \  }}t'          |t"          j                  sJ t          j        |ddg          sJ t          j        |j        ddg          sJ d S d S ) Nr   CrC   rY   rZ   rT   r$   )rZ   rY   rZ   rX   )r7   r   r7   crY   rZ   )levels)rZ   rY   rY   rZ   )r8   r8   )rY   NrZ   Noner]   )FTFr   rE   r7   FT)TFFT)rd   )r7   r   r   r7   r_   r`   rb   r7         )r9   patsy.categoricalrz   r   r   r   r3   rR   rG   r:   re   r<   r   rI   resizerg   r
   rh   ri   rj   rc   )r9   rz   rl   r=   rm   cat1rt   bad_catcat1_NAro   rp   cat2scat_ssbool	cat_sbools                   r    test__eval_factor_categoricalr      s   MMM######
**CA
Qrt

S
S
SC:????3 93??GD!:6$)))#$$$$$
MM*lC&3%#FFF
MM*lC&!!S#J--1H#NNN
MML#3*c3Z0P0P0P'QSV   MM*lC&)))1DcJJJj---..GNN6
MM*lC&'1BCHHH!f&&&'F8)D)D)D NGU >%!5!5!566666>':::.....
MML#0@0@0@'A8UWCXCXCX   }bdt}  C 3)C)C)C DcJJGD!:6$,,,&''''' 
9M3*RH555fa[#66q%/////~eaV,,,,,~ekB844444tUmB8<<<#C&%#>>	1)V]33333~i!Q00000~ioBx88888
9 
9 	98r"   c              #   x   K   d t          |           D             }t          j        | D ]}|d d d         V  d S )Nc                 ,    g | ]}t          |          S r%   )range).0ns     r    
<listcomp>z(_column_combinations.<locals>.<listcomp>   s    @@@aq@@@r"   rE   )reversed	itertoolsproduct)columns_per_factor	iteratorsreversed_combos      r    _column_combinationsr      s]      @@8,>#?#?@@@I#+Y7 # #TTrT"""""# #r"   c                      t          t          ddg                    g dk    sJ t          t          dg                    g dk    sJ t          t          g                     dgk    sJ d S )Nr8   rV   ))r   r   )r7   r   )r   r7   )r7   r7   )r   r8   )r7   r8   ))r   )r7   )r8   r%   )listr   r%   r"   r    test__column_combinationsr      s    $aV,,-- 2 2 2     $aS))**.@.@.@@@@@$R(())bT111111r"   c                     g }|j         D ]K}||j        v r|j        |         j        j        d         }n| |         j        }|                    |           Lt          |          S Nr7   )factorscontrast_matricesmatrixrG   rH   appendr   )factor_infossubtermr   r3   columnss        r    _subterm_column_combinationsr      st    / + +W.../7>DQGGG"6*6G!!'**** 2333r"   c              #     K   d}t          t          | |                    D ]\  }}g }t          |j        |          D ]\  }}| |         }|j        dk    ri|j        dk    r.|                    |                                d|d           Q|dk    sJ |                    |                                           |j        dk    sJ |j        |         }	|	j	        |         }
|                    |                                |
           |sdV  nd
                    |          V  |dz  }||j        k    sJ d S )	Nr   r@   r7   []rC   	Intercept:)	enumerater   zipr   rF   rH   r   r   r   column_suffixesjoin)r   r   totalicolumn_idxsname_piecesr3   
column_idxficontrastsuffixs              r    _subterm_column_names_iterr      s     E#$\7;;   ; "%go{"C"C 	E 	EFJf%Bw+%%>A%%&&6;;====***'MNNNN%????&&v{{}}5555w-////"4V<!1*=""V[[]]]FF#CDDDD 	(((;'''''
G'''''''r"   c                 Z   | j         |j        d         k    sJ d|d<   t          t          ||                     D ]\  }}t	          | j        |          D ]\  }}||         j        dk    rc| j        |         }t          j	        ||         dk               rt          d|          |d d |fxx         |j        ||         |f         z  cc<   y||         j        dk    sJ ||         j        d         ||         j         k    sJ |d d |fxx         ||         d d |f         z  cc<   Ռd S )Nr7   .rC   r   z5can't build a design matrix containing missing valuesr@   )rH   rG   r   r   r   r   rF   r   r:   anyr   r   )	r   r   factor_valuesoutr   r   r3   r   r   s	            r    _build_subtermr     s   #)A,....CH#$\7;;  B B; #&go{"C"C 	B 	BFJF#(M99"4V<6-/!344 $R   AAAqD			X_]6-BJ-NOO				#F+0K????!&)/2l66J6VVVVVAAAqD			]62111j=AA					BB Br"   c                  >   dd l } ddlm} ddlm} t          d          }t          d          }t          d          } |t          j        ddgddgg          d	d
g          }|t          |di dd           |t          |di d ddg          |t          |di dd           i}||i}t          |||g|d          }	t          t          ||	                    ddgk    sJ t          j        d          }
t          |	||t          g d          |t          j        g d          |t          g d          i|
           t          j        |
ddgddgddgg          sJ |                     t$          t          |	||t          g d          |t          j        g d          |t          g d          i|
           t'          |          }t          |di dd           ||<   t          |||g|d          }t          t          ||                    g dk    sJ t          j        d          }t          |||t          ddgddgdd gg          |t          j        g d          |t          g d          i|           t          j        |g d!g d"g d#g          sJ t          g i d          }t          t          i |                    d$gk    sJ t          j        d%          }t          |i |g d|g d|g di|           t          j        |d          sJ d S )&Nr   )ContrastMatrixry   f1f2f3g      ?rV   z[c1]z[c2]r@   r7   rT   rC   rY   rZ   r8   zf1:f2[c1]:f3zf1:f2[c2]:f3r[   rU   )r   r   r7   )      @r8   i      @g       @ir   rd   )zf1[0]:f2[c1]:f3zf1[1]:f2[c1]:f3zf1[0]:f2[c2]:f3zf1[1]:f2[c2]:f3rV   rd         )r   r   r   r   )r   r   g      @g      @)iLi(r   r   r   rW   )r9   patsy.contrastsr   r   rz   r   r:   r;   r   r   r   r   emptyr   r	   rI   allcloser<   r   dict)r9   r   rz   r   r   r   r   factor_infos1r   subterm1matfactor_infos2subterm2mat2subterm_intmat3s                   r    2test__subterm_column_names_iter_and__build_subtermr   ,  s   MMM......######	T		B	T		B	T		B~bhC1a&'9::VV<LMMH 	Jr;dKKK
Jr="$CQT:VVV
Jr;dKKKM
 XBB<):A>>H*=(CCDDI    
 (6

C))))44
999%%)---88	

 		 	 	 ;sa/![1AKQRCSTUUUUU
MM))))44
:::&&)---88	

 	   ''M"2{BARVWWWM"BB<):A>>H*=(CCDD I I I     8FD)Aq6Aq6Aq6*BCC
999%%)---88	

 		 	 	 ;000,,,,,,	
     b"a((K*2{;;<<MMMM8FD;RB			2yyy$QSWXXX;tQr"   c                    i }i }| D ]$}i }|                     ||          }|||<   |||<   %t                      }|                                D ] \  }}	|	dk    r|                    |           !d}|r |            D ]&}
|D ]!}||         }|                    |||
           "'t          |          D ]B}|                    ||         |           |||         dz
  k    r|                    |           C|dz  }||S )Nr   r7   )memorize_passes_neededsetitemsaddmemorize_chunkr   memorize_finishremove)r   data_iter_makereval_envfactor_statespasses_neededr3   r&   
which_passmemorize_neededpassesrO   s              r    _factors_memorizer     sl    MM + +225(CC
 %f *f eeO'--// ( (A::'''J
 	#O%% 	? 	?D) ? ?%f-%%eZ>>>>? ?++ 	/ 	/F""=#8*EEE]62Q666&&v...a
  	 r"   c            
          G d dt                     }  G d dt                     } |            } | dd          } | dd          } | d	d
          } | d	d          }t          t          ||||g          |i           }|j        d	k    sJ dg|j        z  }dg|j        z  }	|g dd||dgz   dd||dgz   |	z   dgz   d
d||dgz   |	z   dgz   ddi}
||
k    sJ d S )Nc                   &    e Zd Zd Zd Zd Zd ZdS )*test__factors_memorize.<locals>.MockFactorc                 >    || _         || _        d| _        d| _        d S )Nr   )_requested_passes_token_chunk_in_pass_seen_passes)r   requested_passestokens      r    r!   z3test__factors_memorize.<locals>.MockFactor.__init__  s'    %5D"DK"#D !Dr"   c                 .    g |d<   | j         |d<   | j        S )Ncallsr   )r   r   )r   r&   r   s      r    r   zAtest__factors_memorize.<locals>.MockFactor.memorize_passes_needed  s    E'N![E'N))r"   c                     |d                              d|f           |d         | j        k    sJ | xj        dz  c_        d S )Nr   r   chunkr7   r   r   )r   r&   r   rO   s       r    r   z9test__factors_memorize.<locals>.MockFactor.memorize_chunk  sR    'N!!#3Z"@AAA=D$777771$r"   c                 N    |d                              d|f           d| _        d S )Nr   r   r   r   )r   r&   r   s      r    r   z:test__factors_memorize.<locals>.MockFactor.memorize_finish  s.    'N!!#4j"ABBB"#Dr"   N)r,   r-   r.   r!   r   r   r   r%   r"   r    
MockFactorr     sP        	" 	" 	"	* 	* 	*
	% 	% 	%
	$ 	$ 	$ 	$ 	$r"   r   c                       e Zd ZdZd Zd ZdS )$test__factors_memorize.<locals>.DatarV   c                 Z    d| _         d t          | j                  D             | _        d S )Nr   c                     g | ]}d |iS )r   r%   )r   r   s     r    r   zAtest__factors_memorize.<locals>.Data.__init__.<locals>.<listcomp>  s    BBB!'1BBBr"   )r   r   CHUNKSrO   r*   s    r    r!   z-test__factors_memorize.<locals>.Data.__init__  s,    DJBBuT[/A/ABBBDIIIr"   c                 J    | xj         dz  c_         t          | j                  S r   )r   iterrO   r*   s    r    __call__z-test__factors_memorize.<locals>.Data.__call__  s    JJ!OJJ	??"r"   N)r,   r-   r.   r   r!   r   r%   r"   r    Datar     s:        	C 	C 	C	# 	# 	# 	# 	#r"   r   r   f0r7   r   r8   f2af2b)r   r   )r   r7   )r   r   )r   r   )r   r7   )objectr   r   r   r   )r   r   rO   r   r   r   r  r   mem_chunks0mem_chunks1expecteds              r    test__factors_memorizer    s   $ $ $ $ $V $ $ $*	# 	# 	# 	# 	#v 	# 	# 	# 466D	At		B	At		B
*Q

C
*Q

C%c2r3*<&=&=tRHHM:????()DK7K()DK7K

 
 	 $:#;;
 
 	 %&' &&' 
 
 	 %&' &&' 
 
!H0 H$$$$$$r"   c                 f   i }i }t          |           } |            D ]}t          |          D ]}|                    ||         |          }	||v st          |	          rO||vrt	          ||j                  ||<   ||                             |	          }
|
r|                    |           t          |	          }	t          d|	|           |	j
        d         }|||<   |                    |           |s ni }|                                D ]\  }}|                                ||<   ||fS )Nr8   r7   )r   r   r(   r   r   rD   sniffr   r	   r5   rG   r   levels_contrast)r   r   r   rP   num_column_countscat_sniffersexamine_neededrO   r3   valuedonecolumn_countcat_levels_contrastssniffers                 r    _examine_factor_typesr    s|   L\\N!!  >** 	. 	.FKKf 5t<<E%%):5)A)A%--+=i+W+WL(#F+11%88 2"))&111 2%88 E6222${1~,8!&)%%f---- 	E	 '--// A A'.'>'>'@'@V$$344r"   c                     ddl m}   G d dt                    } G d dt                    } |            } |            } |            } |            } |            } |            } |            }	t                      t                      t                      f}
|g dg df|d	gd
gdggdgdgdggf|t          j        d          t          j        d          f| | g ddd           | g ddd          f|g dg df|g dg df|	|
d         gdz  |
fi} |            }t          |                                ||t                                \  }}|j	        d
k    sJ d}||d	|d	|dik    sJ ||d|d|d|	t          t          |
t                              d fik    sJ  |            }|||||g}t          |||t                                \  }}|j	        dk    sJ ||d	|d	|dik    sJ ||d|dik    sJ  |            } |            } |            } |            }|t          j        d          t          j        d          f|g dgg dgf|t                      gggt                      gggfi}dd l}|D ]T} |            }	 t          |g||t                                 J # t          $ r}|j        |j        u sJ Y d }~Md }~ww xY wd S )Nr   ry   c                        e Zd Zd Zd Zd ZdS ).test__examine_factor_types.<locals>.MockFactorc                 6    ddl m}  |ddd          | _        d S )Nr   )OriginMOCKr7   r8   )patsy.originr  rD   )r   r  s     r    r!   z7test__examine_factor_types.<locals>.MockFactor.__init__  s-    ++++++ &A..DKKKr"   c                     ||         S r   r%   )r   r&   rO   s      r    r(   z3test__examine_factor_types.<locals>.MockFactor.eval  s    ;r"   c                     dS )Nz	MOCK MOCKr%   r*   s    r    r   z3test__examine_factor_types.<locals>.MockFactor.name  s    ;r"   Nr+   r%   r"   r    r   r    sA        	/ 	/ 	/	 	 		 	 	 	 	r"   r   c                   *    e Zd Zd Zd Zd Zd ZeZdS )1test__examine_factor_types.<locals>.DataIterMakerc                     d| _         d S )NrE   )r   r*   s    r    r!   z:test__examine_factor_types.<locals>.DataIterMaker.__init__  s    DFFFr"   c                     | S r   r%   r*   s    r    r   z:test__examine_factor_types.<locals>.DataIterMaker.__call__      Kr"   c                     | S r   r%   r*   s    r    __iter__z:test__examine_factor_types.<locals>.DataIterMaker.__iter__  r   r"   c                 T    | xj         dz  c_         | j         dk    rt          | j         S r   )r   StopIterationr*   s    r    nextz6test__examine_factor_types.<locals>.DataIterMaker.next  s)    FFaKFFvzz##6Mr"   N)r,   r-   r.   r!   r   r"  r%  __next__r%   r"   r    DataIterMakerr    sQ        	 	 		 	 		 	 		 	 	 r"   r'  rU   )rd   r   r   r7   r8   rV   rd   r   r   r   rY   rZ   r|   MOCK CONTRAST)r}   r   )r|   rZ   rY   )TTF)FTT)rY   rY   rY   )r(  r)  ))FTN)r(  Nkey)rV   rV   rV   )rZ   r|   rY   )r   rz   r  r:   zerosonesr  keysr   r   tuplesortedidr9   r   rD   )rz   r   r'  num_1dimnum_1colnum_4col
categ_1col	bool_1colstring_1colobject_1colobject_levelsr   itr
  r  
iterationsno_read_necessary	bool_3colnum_3dimstring_3colobject_3colillegal_factor_statesr9   illegal_factores                            r    test__examine_factor_typesrD    s!   ######    V          $ z||Hz||Hz||HJ
I*,,K*,,KXXvxx2M999iii(QC!qc?aS1#sO428F##RWV__5AooooPPPAooooPPP
 	''')<)<)<=ooo7}Q'(1,m<M 
B 	m0022M2xzzRR419999J1h8Q GGGGG6(,eF=b999::DA	$     
B!8Xz9M 	/HJJOO4199991h8Q GGGGG6($     
Iz||H *,,K*,,K28I&&	(:(:;'///):;
|n
|n5
 MMM/ 	 	]__	! "7XZZ   L  	5 	5 	58~4444444444	5	 	s   J44
K>KKc           
      ,   t                      }g }| D ]x}g }|j        D ]}||v r|                    |           t          |          }||vr|                    |           |                    |g                               |           yt                      |v rC|                    t                                 |                    dt                                 t                      }	g }
|D ]}||         }|                    d            |
|z  }
t                      }|D ]}g }t          |||          }|D ]}g }i }d}|j        D ]}||v r!|                    |           |||         z  }'||v rX|                    |           ||         \  }}t          ||         ||t                    }|||<   ||j        j        d         z  }|                    t          |||                     ||	|<   Ր|
t          |	          k    sJ |	S )Nr   c                 *    t          | j                  S r   )lenr   )ts    r    <lambda>z%_make_subterm_infos.<locals>.<lambda>  s    AI r"   r*  r7   )default)r   r   r   	frozenset
setdefaultr   insertsortr   r   r   r   r   rG   r   r   )termsr
  r  term_bucketsbucket_orderingtermnum_factorsr3   bucketterm_to_subterm_infosnew_term_orderbucket_termsused_subtermssubterm_infosfactor_codingsfactor_codingsubterm_factorsr   subterm_columnsr}   r   codeds                         r    _make_subterm_infosr_  s  s    ==LO 9 9l 	+ 	+F***""6***;''%%""6***++2248888 {{l""y{{+++q)++...'MMN " &8 &8#F+66777,&   	8  	8DM4' N "0  "$$&!"#
 #l A AF!222'..v666'+<V+DD=00'..v666+?+G( !5)&168Y! ! ! 5:)&1'5<+=a+@@$$1BOTT    +8!$''A 	8B T"7888888  r"   dropc                    t          j        |d          }t          |t                    rt	          |          }t                      }| D ]!}|D ]}|                    |j                   "t          |||          }t          ||||          \  }}	i }
|D ]Z}||v r!t          |d||         ||         d          }n.||	v sJ |	|         d         }t          |d||         d|          }||
|<   [g }| D ]}t          |||	          }t          |t                    sJ t          |          t          |          k    sJ i }|D ]}|j        D ]}|
|         ||<   g }|                                D ]/}|D ]*}t          |
|          D ]}|                    |           +0|                    t#          |||                     |S )	ag  Construct several :class:`DesignInfo` objects from termlists.

    This is one of Patsy's fundamental functions. This function and
    :func:`build_design_matrices` together form the API to the core formula
    interpretation machinery.

    :arg termlists: A list of termlists, where each termlist is a list of
      :class:`Term` objects which together specify a design matrix.
    :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 `termlists` that cannot be
      found in `data_iter_maker`, 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:`design_matrix_builders` 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 list of :class:`DesignInfo` objects, one for each
      termlist passed in.

    This function performs zero or more iterations over the data in order to
    sniff out any necessary information about factor types, set up stateful
    transforms, pick column names, etc.

    See :ref:`formulas` for details.

    .. versionadded:: 0.2.0
       The ``NA_action`` argument.
    .. versionadded:: 0.4.0
       The ``eval_env`` argument.
    r7   )	referencer@   NrT   r   rC   )r   term_codings)r   capturerj   strr   r   updater   r   r  r   r_  r   rK  valuesr   r   r   )	termlistsr   r   rP   all_factorstermlistrR  r   r
  r  r   r3   r   rM   design_infosrU  this_design_factor_infoscolumn_namessubtermsr   column_names                        r    r   r     s   V &x1===H)S!! (Y''	%%K - - 	- 	-Dt|,,,,	-%k?HMMM 1F]OY1 1-,
 L " "&&&f%-f5  BB 11111-f5a8Jf% %  B  "VL 
 
 3')=!
 !
 /=====.//9X3F3FFFFF#%  	H 	HD, H H3?3G(00H-4466 	5 	5H# 5 5#=lG#T#T 5 5K ''444455 	52  	
 	
 	
 	
 r"   c                 <   i }d}d }|                                 D ]T\  }}| j                            |j                  |ur'|||j        <   |||j        d         k    sJ G|j        d         }U|d}d}|t          | j                  f}t          t          j	        ||          |           }	d}
| j
                                         D ];\  }}|D ]3}|
|j        z   }|	d d |
|f         }t          || j        ||           |}
4<|
|	j        d         k    sJ ||	fS )NFr   r7   TrJ   )r   r   getr3   rG   rG  rm  r   r:   r   rc  rH   r   )design_infofactor_info_to_valuesrJ   factor_to_valuesneed_reshapenum_rowsrN   r  rG   mstart_columnrR  rn  r   
end_columnm_slices                   r    _build_design_matrixr|    sy   LH399;; & &U #''(:;;;NN/4+,u{1~-----{1~HH s;3445ERXe5111;??AL%288:: & &h 	& 	&G%(;;J<
223G7K$<>NPWXXX%LL		&
 171:%%%%?r"   c                       e Zd Zd Zd ZdS )_CheckMatchc                 L    || _         || _        d | _        d | _        d | _        d S r   )r   _eq_fnr  _value_desc_value_origin)r   r   eq_fns      r    r!   z_CheckMatch.__init__C  s-    

!r"   c                    | j         || _         || _        || _        d S |                     | j         |          sO| j        d| j        d|}t          | j         t                    r|d| j         d|dz  }t          ||          d S )Nz mismatch between z and z (z versus ))r  r  r  r  r   rj   intr   )r   
seen_valuedescrD   r4   s        r    checkz_CheckMatch.checkJ  s    :#DJ#D!'D;;tz:66 
.JJJ$$$D
 dj#.. HC


JJJGGC !f---
. 
.r"   N)r,   r-   r.   r!   r  r%   r"   r    r~  r~  B  s2        " " ". . . . .r"   r~  r   c                    t          |t                    rt          |          }|dk    rt          st	          d          |dvrt	          d|d          i }i }t          dd           }t          dd	           }t          rXt          |t          j                  r>|                    |j	        d
d           |                    |j
        d         dd           | D ]}	|	j                                        D ]}
|
|vrt          |
||          \  }}|||
<   |
j                                        }|
j        j        }|                    |j
        d         ||           t          rBt          |t          j        t          j        f          r|                    |j	        ||           t%          j        |          }|||
<   Ɍt)          |                                          }t)          |                                          }d |D             }|j        }|j        }|dk    r}|{|t%          j        |          }|                    |           |                    t%          j        t3          |          t4                               |                    d           |                    |||          }|r|d         j
        d         }|dk    r||                                }t;          t=          ||                    }g }| D ]&}	|                    t?          |	||                     'g }|D ]k\  }}|rO|j
        d         dk    sJ |+tA          t%          j!        ||d          |j"                  }nt	          d          |                    |           l|dk    rQt          sJ tG          |          D ]8\  }}|j"        }t          j        ||j$        |          ||<   |||         _"        9|S )a  Construct several design matrices from :class:`DesignMatrixBuilder`
    objects.

    This is one of Patsy's fundamental functions. This function and
    :func:`design_matrix_builders` together form the API to the core formula
    interpretation machinery.

    :arg design_infos: A list of :class:`DesignInfo` objects describing the
      design matrices to be built.
    :arg data: A dict-like object which will be used to look up data.
    :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.
    :arg dtype: The dtype of the returned matrix. Useful if you want to use
      single-precision or extended-precision.

    This function returns either a list of :class:`DesignMatrix` objects (for
    ``return_type="matrix"``) or a list of :class:`pandas.DataFrame` objects
    (for ``return_type="dataframe"``). In both cases, all returned design
    matrices will have ``.design_info`` attributes containing the appropriate
    :class:`DesignInfo` objects.

    Note that unlike :func:`design_matrix_builders`, this function takes only
    a simple data argument, not any kind of iterator. That's because this
    function doesn't need a global view of the data -- everything that depends
    on the whole data set is already encapsulated in the ``design_infos``. If
    you are incrementally processing a large data set, simply call this
    function for each chunk.

    Index handling: This function always checks for indexes in the following
    places:

    * If ``data`` is a :class:`pandas.DataFrame`, its ``.index`` attribute.
    * If any factors evaluate to a :class:`pandas.Series` or
      :class:`pandas.DataFrame`, then their ``.index`` attributes.

    If multiple indexes are found, they must be identical (same values in the
    same order). If no indexes are found, then a default index is generated
    using ``np.arange(num_rows)``. One way or another, we end up with a single
    index for all the data. If ``return_type="dataframe"``, then this index is
    used as the index of the returned DataFrame objects. Examining this index
    makes it possible to determine which rows were removed due to NAs.

    Determining the number of rows in design matrices: This is not as obvious
    as it might seem, because it's possible to have a formula like "~ 1" that
    doesn't depend on the data (it has no factors). For this formula, it's
    obvious what every row in the design matrix should look like (just the
    value ``1``); but, how many rows like this should there be? To determine
    the number of rows in a design matrix, this function always checks in the
    following places:

    * If ``data`` is a :class:`pandas.DataFrame`, then its number of rows.
    * The number of entries in any factors present in any of the design
    * matrices being built.

    All these values much match. In particular, if this function is called to
    generate multiple design matrices at once, then they must all have the
    same number of rows.

    .. versionadded:: 0.2.0
       The ``NA_action`` argument.

    	dataframez;pandas.DataFrame was requested, but pandas is not installed)r   r  zunrecognized output type z#, should be 'matrix' or 'dataframe'zNumber of rowsc                     | |k    S r   r%   r{   s     r    rI  z'build_design_matrices.<locals>.<lambda>  s
    a1f r"   Indexc                 ,    |                      |          S r   )equalsr{   s     r    rI  z'build_design_matrices.<locals>.<lambda>  s    ahhqkk r"   z
data.indexNr   zdata argumentc                 &    g | ]}|j         j        S r%   )r3   rD   )r   rN   s     r    r   z)build_design_matrices.<locals>.<listcomp>  s    RRR[{!(RRRr"   rq  r7   )axiszNo design matrix has any non-trivial factors, the data object is not a DataFrame. I can't tell how many rows the design matrix should have!)r   rc   )%rj   re  r   r
   r   r~  rh   rk   r  rc   rG   r   rg  rR   r3   r   rD   ri   r:   rI   r   r  aranger   r,  rG  bool	handle_NApopr   r   r|  r   repeatrs  r   rm  )rk  rO   rP   return_typerJ   rt  factor_info_to_isNAsrows_checkerindex_checkerrs  rN   r  ro   r   rD   rg  is_NAsoriginspandas_indexrw  
new_valuesresultsmatricesrv  r   r   dis                              r    r   r   ]  sy   H )S!! (Y''	k!!+!L
 
 	
 111j)47
 
 	

 /1D1DEEL)A)ABBM Az$(899 ADJd;;;4:a=/4@@@# ; ; '3::<< 	; 	;K"777+KyIIu49$[1")..00$+2""5;q>4@@@ C:efmVEU5V#W#W C!''T6BBB 
5))5:%k2#	;& '..0011F&--//00FRR<QRRRG &L!H k!!h&:9X..Ll###bhs<00===>>>t$$VVW==J *a=&q)k!!h&:!~~'' %:J!G!GHHG# X X+K9NPUVVWWWWH '    f 	
 <?a''''#%IfhQ7779K  !   	k!!"8,, 	) 	)IAv#B *|  HQK ')HQK##Or"   )r`  )8__all__r   numpyr:   patsyr   r   r   r   r   
patsy.utilr	   r
   r   r   patsy.design_infor   r   r   r   patsy.redundancyr   
patsy.evalr   r   r   r   patsy.compatr   patsy.missingr   rh   r  r   r5   r>   rR   rw   r   r   r   r   r   r   r   r   r  r  rD  r_  r   r|  r~  rJ   floatr   r%   r"   r    <module>r     s   $%<
=               W W W W W W W W W W            P O O O O O O O O O O O 4 4 4 4 4 4 & & & & & & ; ; ; ; ; ; ; ; $ $ $ $ $ $ " " " " " " MMM    &   & & &I I I0 0 0>E
 E
 E
P/9 /9 /9d# # #
2 
2 
24 4 4( ( (6B B B.S  S  S l  <B% B% B%J5 5 5:n n nb?! ?! ?!Dg g g gT     F. . . . .& . . .8 #)hhbhuooc c c c c cr"   