
    phҝ                        S S/r SSKrSSKrSSKJr  SSKJrJrJ	r	  SSK
JrJrJrJr  SSKJrJrJrJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  \(       a  SSKr " S S\ 5      r!S r"S r#S r$S r%S r&S r'S r(S r)S r*S r+S r,S r-S r.S r/S r0S r1S&S  jr2S! r3 " S" S#\ 5      r4SS$\Rj                  " \65      4S% jr7g)'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                   *    \ rS rSrSS jrS rS rSrg)_MockFactor    c                     Xl         g N_name)selfnames     cC:\Users\julio\OneDrive\Documentos\Trabajo\IdeasFrscas\Cabanna\env\Lib\site-packages\patsy/build.py__init___MockFactor.__init__!   s    
    c                     US   $ )Nmock )r   stateenvs      r    eval_MockFactor.eval$   s    6{r#   c                     U R                   $ r   r   r   s    r    r   _MockFactor.name'   s    zzr#   r   N)MOCKMOCK__name__
__module____qualname____firstlineno__r!   r)   r   __static_attributes__r&   r#   r    r   r       s    r#   r   c                     UR                   U :  a1  SUR                  5       < SUR                   < SU < 3n[        X25      eg )Nzfactor 'z' evaluates to an z;-dimensional array; I only handle arrays with dimension <= )ndimr   r   )dimarrfactormsgs       r    _max_allowed_dimr;   +   s=    
xx#~ 5;KKM388SR 	 %% r#   c                     SS K n [        5       n[        S[        R                  " S5      U5        [        S[        R                  " S/5      U5        U R                  [        [        S[        R                  " S//5      U5        U R                  [        [        S[        R                  " S///5      U5        [        S[        R                  " S5      U5        [        S[        R                  " S/5      U5        [        S[        R                  " S//5      U5        U R                  [        [        S[        R                  " S///5      U5        g )Nr         )pytestr   r;   nparrayraisesr   )r?   fs     r    test__max_allowed_dimrD   4   s    AQQ'Q!q)
MM*.288aSE?AF
MM*.288qcUG3DaHQQ'Q!q)Q1#+
MM*.288qcUG3DaHr#   c           	         U R                   nUR                  U R                  U5      nU R                  S:X  a  [	        USS9n[        SXC5        UR                  S   U R                  :w  a>  [        SUR                  5       < SU R                  < SUR                  S   < S	3U5      e[        [        R                  " U5      R                  [        R                  5      (       d-  [        S
UR                  5       < SUR                  < S3U5      eXBR                  U5      4$ U R                  S:X  d   e[!        X@R"                  X R                   S9nUR$                  S:X  d   eU[        R                  " US:H  5      4$ )N	numericalT)preserve_pandasr>   r=   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)r9   r)   r'   typer	   r;   shapenum_columnsr   r   r   r@   asarraydtypenumberis_numerical_NAr   
categoriesr6   )factor_infodata	NA_actionr9   results        r    _eval_factorrY   B   sL   F[[**D1F;&*64HF+<<?k555 ;;=+"9"96<<?L 	  rzz&177CC9?V 
 00888 =000#**I>P>P
 {{arzz&B,///r#   c                  	   SS K n [        5       n[        5       n[        US0 SS S9nUR                  UL d   e[        US/ SQ0U5      u  pEUR                  S:X  d   e[        R                  " US/S/S	//:H  5      (       d   eUR                  S
:X  d   e[        R                  " U) 5      (       d   eU R                  [        [
        USS///0U5        U R                  [        [
        USSS//0U5        U R                  [        [
        USSS/0U5        U R                  [        [
        USSS/0U5        [        [        5       S0 SS S9n[        USSS	/SS/S	S//0U5      u  puUR                  S:X  d   e[        R                  " USS	/SS/S	S//:H  5      (       d   eUR                  S
:X  d   e[        R                  " U) 5      (       d   eU R                  [        [
        US/ SQ0U5        U R                  [        [
        US/ SQ/0U5        [        USSS[        R                  /0[        S/S95      u  p[        R                  " U/ SQ5      (       d   e[        USSS[        R                  /0[        / S95      u  p[        R                  " U/ SQ5      (       d   e[        (       GaG  [        US[        R                  " / SQ/ SQS90U5      u  p[!        U	[        R"                  5      (       d   e[        R                  " U	S/S/S	//5      (       d   e[        R                  " U	R$                  / SQ5      (       d   e[        US[        R"                  " S/S/S	/// SQS90U5      u  p[!        U[        R"                  5      (       d   e[        R                  " US/S/S	//5      (       d   e[        R                  " UR$                  / SQ5      (       d   e[        US[        R"                  " SS	/SS/S	S/// SQS90U5      u  p[!        U[        R"                  5      (       d   e[        R                  " USS	/SS/S	S//5      (       d   e[        R                  " UR$                  / SQ5      (       d   eU R                  [        [
        US[        R                  " / SQ/ SQS90U5        U R                  [        [
        US[        R"                  " SS	/SS/S	S/// SQS90U5        g g )Nr   rF   r=   rO   rT   r%   r=   r>      r]   r=   r>   r]   r]   abTFr]   r>   NaNNA_types)FFT)FFF)
         index)rg   rf   rh      rL   )rg   rh   rf   )r?   r   r   r   r9   rY   rN   r@   allrB   r   nanarray_equalr
   pandasSeries
isinstance	DataFramerj   )r?   naarC   fi1eval123is_NAfi2
eval123321ev_naneval_ser_eval_df1eval_df2s                r    test__eval_factor_numericalr~   a   s   
*CA
QRQ4
HC::??!#	':C@NG==F"""66'qcA3_,----;;$665&>>>
MM*lC&QC5'1BCH
MM*lC&Aq6(1CSI
MM*lC&3*1EsK
MM*lC&4-1H#N
[]Kt
TC$S6QFQFQF3K*LcRJv%%%66*!Q!Q!Q 889999;;$665&>>>
MM*lC&)1DcJ
MM*lC&9+1FL fq!RVVn%x%'AMF >>%!56666 v1bff~&>RT@UVMF>>%!67777{"&&--	FG
 (F$4$45555~~h!qcA38888~~hnnl;;;;"&&**QC!qc?,OPRU
 (F$4$45555~~h!qcA38888~~hnnl;;;;"V%%1v1v2w&?|TU

 (F$4$45555~~h!Q!Q!R(ABBBB~~hnnl;;;;V]]9LAB	
 	V%%1v1v2w&?|TU	
; r#   c                     SS K n SSKJn  [        5       n[	        5       n[        US0 S SS9nUR                  UL d   e[        US/ SQ0U5      u  pVUR                  S:X  d   e[        R                  " U/ S	Q:H  5      (       d   eU R                  [        [        USS
/0U5        U R                  [        [        USU" SS
/5      0U5        U R                  [        [        USU" SS/SS/S90U5        U R                  [        [        US/ S	Q0U5        [        R                  " / SQ5      nUR                  S5        U R                  [        [        USU0U5        [        US/ SQ0[        S/S95      u  p[        R                  " U	/ SQ5      (       d   e[        R                  " U/ SQ5      (       d   eU R                  [        [        US/ SQ0[        / S95        [        [	        5       S0 S SS/S9n
[        U
S/ SQ0U5      u  pUR                  S:X  d   e[        R                  " U/ SQ:H  5      (       d   e[         (       Ga+  ["        R$                  " SS/SS/S9n[        USU0U5      u  p['        U["        R$                  5      (       d   e[        R                  " USS/5      (       d   e[        R                  " UR(                  SS/5      (       d   e["        R$                  " SS/SS/S9n[        U
SU0U5      u  p['        U["        R$                  5      (       d   e[        R                  " USS/5      (       d   e[        R                  " UR(                  SS/5      (       d   eg g ) Nr   CrI   r`   ra   r[   r%   )ra   r`   ra   r_   )r=   r   r=   cr`   ra   )levels)ra   r`   r`   ra   )r>   r>   )r`   Nra   Nonerd   )FTFr   rL   r=   FT)TFFT)rk   )r=   r   r   r=   rf   rg   ri   r=         )r?   patsy.categoricalr   r   r   r   r9   rY   rN   r@   rl   rB   r   rP   resizern   r
   ro   rp   rq   rj   )r?   r   rs   rC   rt   cat1r{   bad_catcat1_NArv   rw   cat2scat_ssbool	cat_sbools                   r    test__eval_factor_categoricalr      s   #
*CA
Qrt

SC::??3 93?GD::66$)#$$$$
MM*lC&3%#F
MM*lC&!S#J-1H#N
MML#3*c3Z0P'QSV MM*lC&)1DcJjj-.GNN6
MM*lC&'1BCH!f&'F8)DNG >>%!56666>>':....
MML#0@'A8UWCX }bdt}C 3)C DcJGD::66$,&''''{MM3*RH5fa[#6%////~~eaV,,,,~~ekkB84444tUmB8<#C&%#>	)V]]3333~~i!Q0000~~iooBx8888 r#   c              #      #    [        U 5       Vs/ s H  n[        U5      PM     nn[        R                  " U6  H  nUS S S2   v   M     g s  snf 7fNrL   )reversedrange	itertoolsproduct)columns_per_factorn	iteratorsreversed_combos       r    _column_combinationsr      sO     #+,>#?@#?aq#?I@#++Y7TrT"" 8 As   AA,Ac                      [        [        SS/5      5      / SQ:X  d   e[        [        S/5      5      / SQ:X  d   e[        [        / 5      5      S/:X  d   eg )Nr>   r]   ))r   r   )r=   r   )r   r=   )r=   r=   )r   r>   )r=   r>   ))r   )r=   )r>   r&   )listr   r&   r#   r    test__column_combinationsr      sa    $aV,- 2    $aS)*.@@@@$R()bT111r#   c                     / nUR                    HX  nX1R                  ;   a'  UR                  U   R                  R                  S   nOX   R                  nUR                  U5        MZ     [        U5      $ Nr=   )factorscontrast_matricesmatrixrN   rO   appendr   )factor_infossubtermr   r9   columnss        r    _subterm_column_combinationsr      sm    //...//7>>DDQGG"*66G!!'* "   233r#   c              #     #    Sn[        [        X5      5       GH  u  p4/ n[        UR                  U5       H  u  pgX   nUR                  S:X  ab  UR
                  S:  a)  UR                  UR                  5       < SU< S35        MR  US:X  d   eUR                  UR                  5       5        M{  UR                  S:X  d   eUR                  U   n	U	R                  U   n
UR                  UR                  5       < U
< 35        M     U(       d  Sv   OSR                  U5      v   US-  nGM     X!R
                  :X  d   eg 7f)	Nr   rF   r=   []rI   	Intercept:)	enumerater   zipr   rM   rO   r   r   r   column_suffixesjoin)r   r   totalicolumn_idxsname_piecesr9   
column_idxficontrastsuffixs              r    _subterm_column_names_iterr      s    E#$\; "%goo{"CF%Bww+%>>A%&&6;;=*'MN%?*?&&v{{}5ww-///"44V<!11*=""V[[]F#CD #D ((;''
+, '''''s   EEc                 H   U R                   UR                  S   :X  d   eSUS'   [        [        X5      5       H  u  pE[	        U R
                  U5       H  u  pgX   R                  S:X  a_  U R                  U   n[        R                  " X&   S:  5      (       a  [        SU5      eUS S 2U4==   UR                  X&   U4   -  ss'   Mv  X   R                  S:X  d   eX&   R                  S   X   R                   :X  d   eUS S 2U4==   X&   S S 2U4   -  ss'   M     M     g )Nr=   .rI   r   z5can't build a design matrix containing missing valuesrF   )rO   rN   r   r   r   r   rM   r   r@   anyr   r   )	r   r   factor_valuesoutr   r   r9   r   r   s	            r    _build_subtermr     s'   #))A,...CH#$\; #&goo{"CF#((M9"44V<66-/!344$R  AqD	X__]-BJ-NOO	#+00K???!)//2l6J6V6VVVAqD	]21j=AA	 #Dr#   c                  |   SS K n SSKJn  SSKJn  [        S5      n[        S5      n[        S5      nU" [        R                  " SS/SS//5      S	S
/5      nU[        US0 SS S9U[        US0 S SS/S9U[        US0 SS S90nXF0n[        X4U/US5      n	[        [        Xy5      5      SS/:X  d   e[        R                  " S5      n
[        U	UU[        / SQ5      U[        R                  " / SQ5      U[        / SQ5      0U
5        [        R                   " U
SS/SS/SS//5      (       d   eU R#                  [$        [        U	UU[        / SQ5      U[        R                  " / SQ5      U[        / SQ5      0U
5        ['        U5      n[        US0 SS S9X'   [        X4U/US5      n[        [        X5      5      / SQ:X  d   e[        R                  " S5      n[        UUU[        SS/SS/SS //5      U[        R                  " / SQ5      U[        / SQ5      0U5        [        R                   " U/ S!Q/ S"Q/ S#Q/5      (       d   e[        / 0 S5      n[        [        0 U5      5      S$/:X  d   e[        R                  " S%5      n[        U0 U/ SQU/ SQU/ SQ0U5        [        R                   " US5      (       d   eg )&Nr   )ContrastMatrixr   f1f2f3g      ?r]   z[c1]z[c2]rF   r=   r[   rI   r`   ra   r>   zf1:f2[c1]:f3zf1:f2[c2]:f3rb   r\   )r   r   r=   )      @r>   i      @g       @ir   rk   )zf1[0]:f2[c1]:f3zf1[1]:f2[c1]:f3zf1[0]:f2[c2]:f3zf1[1]:f2[c2]:f3r]   rk         )r   r   r   r   )r   r   g      @g      @)iLi(r   r   r   r^   )r?   patsy.contrastsr   r   r   r   r@   rA   r   r   r   r   emptyr   r	   rP   allcloserB   r   dict)r?   r   r   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   .#	T	B	T	B	T	BbhhC1a&'9:VV<LMH 	Jr;dK
Jr="$CQT:V
Jr;dKM
 BB<):A>H*=CDI   
 ((6
C))4

9%)-8	

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

:&)-8	

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

9%)-8	

 		 ;;0,,	
    b"a(K*2{;<MMM88FD;RB	2y$QSWX;;tQr#   c                    0 n0 nU  H  n0 nUR                  Xb5      nXcU'   XtU'   M      [        5       nUR                  5        H  u  pYU	S:  d  M  UR                  U5        M      SnU(       a  U" 5        H"  n
U H  nX5   nUR	                  XgU
5        M     M$     [        U5       H5  nUR                  X5   U5        XtU   S-
  :X  d  M$  UR                  U5        M7     US-  nU(       a  M  U$ )Nr   r=   )memorize_passes_neededsetitemsaddmemorize_chunkr   memorize_finishremove)r   data_iter_makereval_envfactor_statespasses_neededr9   r'   
which_passmemorize_neededpassesrV   s              r    _factors_memorizer     s    MM225C
 %f *f	  eO'--/A:' 0 J
#%D)%-%%e> * & ?+F""=#8*E62Q66&&v. , 	a
 / r#   c            
          " S S[         5      n  " S S[         5      nU" 5       nU " SS5      nU " SS5      nU " S	S
5      nU " S	S5      n[        [        X4XV/5      U0 5      nUR                  S	:X  d   eS/UR                  -  nS/UR                  -  n	U/ SS.UUS/-   SS.UUS/-   U	-   S/-   S
S.UUS/-   U	-   S/-   SS.0n
Xz:X  d   eg )Nc                   ,    \ rS rSrS rS rS rS rSrg)*test__factors_memorize.<locals>.MockFactori  c                 8    Xl         X l        SU l        SU l        g )Nr   )_requested_passes_token_chunk_in_pass_seen_passes)r   requested_passestokens      r    r!   3test__factors_memorize.<locals>.MockFactor.__init__  s    %5"K"#D !Dr#   c                 B    / US'   U R                   US'   U R                  $ )Ncallsr   )r   r   )r   r'   r   s      r    r   Atest__factors_memorize.<locals>.MockFactor.memorize_passes_needed  s$    E'N![[E'N)))r#   c                     US   R                  SU45        US   U R                  :X  d   eU =R                  S-  sl        g )Nr   r   chunkr=   r   r   )r   r'   r   rV   s       r    r   9test__factors_memorize.<locals>.MockFactor.memorize_chunk  sC    'N!!#3Z"@A=D$7$77771$r#   c                 >    US   R                  SU45        SU l        g )Nr   r   r   r   )r   r'   r   s      r    r   :test__factors_memorize.<locals>.MockFactor.memorize_finish  s"    'N!!#4j"AB"#Dr#   )r   r   r   r   N)	r0   r1   r2   r3   r!   r   r   r   r4   r&   r#   r    
MockFactorr     s    	"	*
	%
	$r#   r   c                   $    \ rS rSrSrS rS rSrg)$test__factors_memorize.<locals>.Datai  r]   c                 v    SU l         [        U R                  5       Vs/ s H  nSU0PM	     snU l        g s  snf )Nr   r   )r   r   CHUNKSrV   )r   r   s     r    r!   -test__factors_memorize.<locals>.Data.__init__  s1    DJ/4T[[/AB/A!'1/ABDIBs   6c                 V    U =R                   S-  sl         [        U R                  5      $ r   )r   iterrV   r,   s    r    __call__-test__factors_memorize.<locals>.Data.__call__  s    JJ!OJ		?"r#   )r   rV   N)r0   r1   r2   r3   r  r!   r  r4   r&   r#   r    Datar     s    	C	#r#   r  r   f0r=   r   r>   f2af2b)r   r   )r   r=   )r   r   )r   r   )r   r=   )objectr   r   r   r  )r   r  rV   r	  r   r
  r  r   mem_chunks0mem_chunks1expecteds              r    test__factors_memorizer    s=   $V $*	#v 	# 6D	At	B	At	B
Q
C
Q
C%c23*<&=tRHM::??()DKK7K()DKK7K

 	 $:#;;
 	 %&' &&' 
 	 %&' &&' 
!H0 $$$r#   c                 0   0 n0 n[        U 5      nU" 5        H  n[        U5       H  nUR                  X   U5      n	X;   d  [        U	5      (       aK  X;  a  [	        X8R
                  5      XX'   XX   R                  U	5      n
U
(       a  UR                  U5        Mu  Mw  [        U	5      n	[        SX5        U	R                  S   nXU'   UR                  U5        M     U(       a  M    O   0 nUR                  5        H  u  pUR                  5       X'   M     XL4$ )Nr>   r=   )r   r   r)   r   r   rK   sniffr   r	   r;   rN   r   levels_contrast)r   r   r   rW   num_column_countscat_sniffersexamine_neededrV   r9   valuedonecolumn_countcat_levels_contrastssniffers                 r    _examine_factor_typesr    s   L\N!>*FKK 5t<E%):5)A)A-+=i+WL(#+11%8"))&1  2%8 E2${{1~,8&)%%f- + ~# "& '--/'.'>'>'@$ 044r#   c                     SSK Jn    " S S[        5      n " S S[        5      nU" 5       nU" 5       nU" 5       nU" 5       nU" 5       nU" 5       nU" 5       n	[        5       [        5       [        5       4n
U/ SQ/ SQ4US	/S
/S//S/S/S//4U[        R                  " S5      [        R
                  " S5      4UU " / SQSSS9U " / SQSSS94U/ SQ/ SQ4U/ SQ/ SQ4XS   /S-  U
40nU" 5       n[        UR                  5       X[        5       5      u  nnUR                  S
:X  d   eSnXS	US	US0:X  d   eUUSUSUSU	[        [        U
[        S95      S 40:X  d   eU" 5       nX4XVU/n[        UX[        5       5      u  nnUR                  S:X  d   eXS	US	US0:X  d   eUUSUS0:X  d   eU" 5       nU" 5       nU" 5       nU" 5       nU[        R                  " S5      [        R
                  " S5      4U/ SQ// SQ/4U[        5       ///[        5       ///40nSS KnU H"  nU" 5       n [        U/UU[        5       5         e   g ! [         a&  nUR                  UR                  L d   e S nAMR  S nAff = f)Nr   r   c                   &    \ rS rSrS rS rS rSrg).test__examine_factor_types.<locals>.MockFactori  c                 .    SSK Jn  U" SSS5      U l        g )Nr   )OriginMOCKr=   r>   )patsy.originr!  rK   )r   r!  s     r    r!   7test__examine_factor_types.<locals>.MockFactor.__init__  s    + A.DKr#   c                 
    X   $ r   r&   )r   r'   rV   s      r    r)   3test__examine_factor_types.<locals>.MockFactor.eval  s
    ;r#   c                     g)Nz	MOCK MOCKr&   r,   s    r    r   3test__examine_factor_types.<locals>.MockFactor.name  s    r#   rJ   Nr/   r&   r#   r    r   r    s    	/		r#   r   c                   0    \ rS rSrS rS rS rS r\rSr	g)1test__examine_factor_types.<locals>.DataIterMakeri  c                     SU l         g r   r   r,   s    r    r!   :test__examine_factor_types.<locals>.DataIterMaker.__init__  s	    DFr#   c                     U $ r   r&   r,   s    r    r  :test__examine_factor_types.<locals>.DataIterMaker.__call__      Kr#   c                     U $ r   r&   r,   s    r    __iter__:test__examine_factor_types.<locals>.DataIterMaker.__iter__  r0  r#   c                 p    U =R                   S-  sl         U R                   S:  a  [        eU R                   $ r   )r   StopIterationr,   s    r    next6test__examine_factor_types.<locals>.DataIterMaker.next  s)    FFaKFvvz##66Mr#   r,  N)
r0   r1   r2   r3   r!   r  r2  r6  __next__r4   r&   r#   r    DataIterMakerr*    s    				 r#   r9  r\   )rk   r   r   r=   r>   r]   rk   r   r   r   r`   ra   r   MOCK CONTRAST)r   r   )r   ra   r`   )TTF)FTT)r`   r`   r`   )r:  r;  ))FTN)r:  Nkey)r]   r]   r]   )ra   r   r`   )r   r   r  r@   zerosonesr  keysr   r   tuplesortedidr?   r   rK   )r   r   r9  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_statesr?   illegal_factores                            r    test__examine_factor_typesrV    s   #V  $ |H|H|HJI,K,KXvx2M9i(QC!qc?aS1#sO4288F#RWWV_5ooPooP
 	')<=o7Q'(1,m<M 
B 	m002MxzR44199J1h8Q GGGG6(,eF=b9:DA	$    
B!X9M 	/HJO441991h8Q GGGG6($    I|H ,K,K288I&	(:;'/):;
|n
|n5
 /_	! "7XZ 5 0  	588~44444	5s   *I
I6I11I6c                    [        5       n/ nU  Hr  n/ nUR                   H  nXq;   d  M
  UR                  U5        M     [        U5      nX;  a  UR                  U5        UR	                  U/ 5      R                  U5        Mt     [        5       U;   a3  UR                  [        5       5        UR                  S[        5       5        [        5       n	/ n
U H  nX8   nUR                  S S9  X-  n
[        5       nU H  n/ n[        XQU5      nU H  n/ n0 nSnUR                   Ht  nXq;   a  UR                  U5        UX   -  nM"  X;   d  M)  UR                  U5        X'   u  nn[        X   UU[        S9nUUU'   UUR                  R                  S   -  nMv     UR                  [        UUU5      5        M     XU'   M     M     U
[        U	5      :X  d   eU	$ )Nr   c                 ,    [        U R                  5      $ r   )lenr   )ts    r    <lambda>%_make_subterm_infos.<locals>.<lambda>  s    AIIr#   r<  r=   )default)r   r   r   	frozenset
setdefaultr   insertsortr   r   r   r   r   rN   r   r   )termsr  r  term_bucketsbucket_orderingtermnum_factorsr9   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_infosrr  s  s    =LOllF*""6* # ;'%""6*+2248  {l"y{+q)+.'MN "#+67& DM4N "0"$$&!"#
 #llF2'..v6'+<+DD0'..v6+?+G( !5)168Y! 5:)&1'5<<+=+=a+@@ + $$1BOT/ "04 +8$'A ! "N T"78888  r#   dropc           	      2   [         R                  " USS9n[        U[        5      (       a  [	        U5      n[        5       nU  H'  nU H  nUR                  UR                  5        M      M)     [        XAU5      n[        XGX5      u  p0 n
U H;  nX;   a  [        USX{   X   SS9nOX;   d   eX   S   n[        USX{   SUS9nXU'   M=     / nU  H  n[        XXU	5      n[        U[        5      (       d   e[        U5      [        U5      :X  d   e0 nU H  nUR                   H
  nX   UU'   M     M     / nUR                  5        H0  nU H'  n[        U
U5       H  nUR!                  U5        M     M)     M2     UR!                  [#        UUUS95        M     U$ )	a  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.
r=   )	referencerF   Nr[   r   rI   )r   term_codings)r   capturerq   strr   r   updater   r   r  r   rr  r   r^  valuesr   r   r   )	termlistsr   r   rW   all_factorstermlistre  r   r  r  r   r9   r   rT   design_infosrh  this_design_factor_infoscolumn_namessubtermsr   column_names                        r    r   r     s   V &&x1=H)S!!Y'	%KDt||,   &kHMM 1FO1-
 L&%-5B 111-5a8J% %B  "V' * L 3)=!
 /====./9X3FFFF#% D,,3?3G(0 '  -446H##=lG#TK ''4 $U $ 7 	52	
 , r#   c                 b   0 nSnS nUR                  5        He  u  pgU R                  R                  UR                  5      ULa  M/  XsUR                  '   Ub  XWR                  S   :X  d   eMV  UR                  S   nMg     Uc  SnSnU[        U R                  5      4n[        [        R                  " XS9U 5      n	Sn
U R                  R                  5        H>  u  pU H3  nXR                  -   nU	S S 2X24   n[        XR                  X?5        Un
M5     M@     XR                  S   :X  d   eXI4$ )NFr   r=   TrQ   )r   r   getr9   rN   rY  r  r   r@   r   rv  rO   r   )design_infofactor_info_to_valuesrQ   factor_to_valuesneed_reshapenum_rowsrU   r  rN   mstart_columnre  r  r   
end_columnm_slices                   r    _build_design_matrixr    s=   LH399; ##''(:(:;;N/4++,{{1~---{{1~H <  s;3345ERXXe1;?AL%2288:G%(;(;;J<223G7$<$<>NX%L	   ; 771:%%%?r#   c                        \ rS rSrS rS rSrg)_CheckMatchiB  c                 F    Xl         X l        S U l        S U l        S U l        g r   )r   _eq_fnr  _value_desc_value_origin)r   r   eq_fns      r    r!   _CheckMatch.__init__C  s#    

!r#   c                 L   U R                   c  Xl         X l        X0l        g U R                  U R                   U5      (       dc  U R                  < SU R                  < SU< 3n[        U R                   [        5      (       a  USU R                   < SU< S3-  n[        XC5      eg )Nz mismatch between z and z (z versus ))r  r  r  r  r   rq   intr   )r   
seen_valuedescrK   r:   s        r    check_CheckMatch.checkJ  s    ::#J#!';;tzz:66JJ$$
 djj#..

JGGC !-- 7r#   )r  r   r  r  r  N)r0   r1   r2   r3   r!   r  r4   r&   r#   r    r  r  B  s    ".r#   r  r   c           	      4   [        U[        5      (       a  [        U5      nUS:X  a  [        (       d  [	        S5      eUS;  a  [	        SU< S35      e0 n0 n[        SS 5      n[        SS	 5      n[        (       a\  [        U[        R                  5      (       a=  UR                  UR                  S
S5        UR                  UR                  S   SS5        U  H  n	U	R                  R                  5        H  n
X;  d  M
  [        XU5      u  pXU
'   U
R                  R                  5       nU
R                  R                   nUR                  UR                  S   X5        [        (       aK  [        U[        R"                  [        R                  45      (       a  UR                  UR                  X5        [$        R&                  " U5      nXU
'   M     M     [)        UR                  5       5      n[)        UR                  5       5      nU V
s/ s H  oR                  R                   PM     nn
UR*                  nUR*                  nUS:X  ao  Ubl  Uc  [$        R,                  " U5      nUR/                  U5        UR/                  [$        R0                  " [3        U5      [4        S95        UR/                  S5        UR7                  UUU5      nU(       a  US   R                  S   nUS:X  a  Ub  UR9                  5       n[;        [=        UU5      5      n/ nU  H  n	UR/                  [?        XU5      5        M      / nU Hl  u  nnU(       aN  UR                  S   S:X  d   eUb+  [A        [$        RB                  " UUSS9URD                  5      nO[	        S5      eUR/                  U5        Mn     US:X  a[  [        (       d   e[G        U5       H?  u  nnURD                  n[        R                  " UURH                  US9UU'   UUU   l"        MA     U$ s  sn
f )a7  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                 
    X:H  $ r   r&   r   s     r    r[  'build_design_matrices.<locals>.<lambda>  s    afr#   Indexc                 $    U R                  U5      $ r   )equalsr   s     r    r[  r    s    ahhqkr#   z
data.indexNr   zdata argumentr  r=   )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   rj   )%rq   rx  r   r
   r   r  ro   rr   r  rj   rN   r   rz  rY   r9   r   rK   rp   r@   rP   r   r  aranger   r>  rY  bool	handle_NApopr   r   r  r   repeatr  r   r  )r~  rV   rW   return_typerQ   r  factor_info_to_isNAsrows_checkerindex_checkerr  rU   r  rv   r   rK   rz  is_NAsoriginspandas_indexr  
new_valuesresultsmatricesr  r   r   dis                              r    r   r   ]  s   H )S!!Y'	k!++L
 	
 11)47
 	

 /1DEL)ABM{z$(8(899DJJd;4::a=/4@# '33::<K7+KyI49[1"))..0$++22""5;;q>4@;:efmmVEUEU5V#W#W!''TB 

5)5:k2# =	 $. '..01F&--/0F<QR<Q[!!((<QGR &&L!!H k!h&:99X.Ll#bhhs<0=>t$$VVW=Ja=&&q)k!h&:!~~' %:J!GHG#+KPUVW $H 'f
 <<?a'''#%IIfhQ79K9K !  	% !(& k!{{"8,IAv##B **|HQK ')HQK# - Og Ss   P)rs  )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   ro   r  r   r;   rD   rY   r~   r   r   r   r   r   r   r   r   r  r  rV  rr  r   r  r  rQ   floatr   r&   r#   r    <module>r     s    $%<
=    W W  P O 4 & ; $ "& &I0>E
P/9d#
24(6B.S l<B%J5:nb?!DgT F.& .8 #)hbhhuocr#   