
    J/PhLB                         d gZ ddlZddlmZmZmZ ddlmZ erddl	Z	d Z
d Zd Z G d d	e          Z ee          Zd
 Zde_        d Zd ZdS )bs    N)have_pandasno_picklingassert_no_pickling)stateful_transformc                 `   	 ddl m} n# t          $ r t          d          w xY wt          j        t          j        |t                              }|j        dk    sJ |                                 t          |          }t          j        |           } | j        dk    r| j
        d         dk    r| d d df         } | j        dk    sJ t          j        |           t          j        |          k     s*t          j        |           t          j        |          k    rt          d          t          |          |dz   z
  }t          j        | j
        d         |ft                    }t!          |          D ]2}t          j        |f          }d||<    || |||f          |d d |f<   3|S )Nr   )splevz#spline functionality requires scipy)dtype      zksome data points fall outside the outermost knots, and I'm not sure how to handle them. (Patches accepted!))scipy.interpolater	   ImportErrornp
atleast_1dasarrayfloatndimsortintshapeminmaxNotImplementedErrorlenemptyrangezeros)xknotsdegreer	   n_basesbasisicoefss           M/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/patsy/splines.py_eval_bspline_basisr&      s   A+++++++ A A A?@@@A
 M"*U%88899E:????	JJLLL[[F
aAv{{qwqzQaaadG6Q;;;;
 
vayy26%==  BF1IIu$=$=!2
 
 	
" %jjFQJ'GHagaj'*%888E7^^ 7 7'$$aeAuf566aaadLs   	 #c                      t          j        |          }t          j         fd|                    d          D                       }|                    |j        d          S )Nc                 @    g | ]}t          j        d |z            S )d   )r   
percentile).0probr   s     r%   
<listcomp>z&_R_compat_quantile.<locals>.<listcomp>F   s)    III$q#*	%	%III    C)order)r   r   ravelreshaper   )r   probs	quantiless   `  r%   _R_compat_quantiler5   B   se    JuE
IIII%++C+2H2HIII I U[444r.   c                      d }  | ddgdd            | ddgdd            | ddgddgdd	g            | t          t          d                    ddgd
dg           d S )Nc                 P    t          j        t          | |          |          sJ d S N)r   allcloser5   )r   r,   expecteds      r%   tz"test__R_compat_quantile.<locals>.tL   s+    {-a66AAAAAAAr.   
      g      ?   g333333?   gffffff?   g@g333333@)listr   )r;   s    r%   test__R_compat_quantilerB   K   s    B B B Ar2hRAr2hRAr2hc
RH%%%Ad599ooSzC:.....r.   c                   J    e Zd ZdZd Z	 	 	 	 	 	 d	dZd Z	 	 	 	 	 	 d	dZeZ	dS )
BSa3  bs(x, df=None, knots=None, degree=3, include_intercept=False, lower_bound=None, upper_bound=None)

    Generates a B-spline basis for ``x``, allowing non-linear fits. The usual
    usage is something like::

      y ~ 1 + bs(x, 4)

    to fit ``y`` as a smooth function of ``x``, with 4 degrees of freedom
    given to the smooth.

    :arg df: The number of degrees of freedom to use for this spline. The
      return value will have this many columns. You must specify at least one
      of ``df`` and ``knots``.
    :arg knots: The interior knots to use for the spline. If unspecified, then
      equally spaced quantiles of the input data are used. You must specify at
      least one of ``df`` and ``knots``.
    :arg degree: The degree of the spline to use.
    :arg include_intercept: If ``True``, then the resulting
      spline basis will span the intercept term (i.e., the constant
      function). If ``False`` (the default) then this will not be the case,
      which is useful for avoiding overspecification in models that include
      multiple spline terms and/or an intercept term.
    :arg lower_bound: The lower exterior knot location.
    :arg upper_bound: The upper exterior knot location.

    A spline with ``degree=0`` is piecewise constant with breakpoints at each
    knot, and the default knot positions are quantiles of the input. So if you
    find yourself in the situation of wanting to quantize a continuous
    variable into ``num_bins`` equal-sized bins with a constant effect across
    each bin, you can use ``bs(x, num_bins - 1, degree=0)``. (The ``- 1`` is
    because one degree of freedom will be taken by the intercept;
    alternatively, you could leave the intercept term out of your model and
    use ``bs(x, num_bins, degree=0, include_intercept=True)``.

    A spline with ``degree=1`` is piecewise linear with breakpoints at each
    knot.

    The default is ``degree=3``, which gives a cubic b-spline.

    This is a stateful transform (for details see
    :ref:`stateful-transforms`). If ``knots``, ``lower_bound``, or
    ``upper_bound`` are not specified, they will be calculated from the data
    and then the chosen values will be remembered and re-used for prediction
    from the fitted model.

    Using this function requires scipy be installed.

    .. note:: This function is very similar to the R function of the same
      name. In cases where both return output at all (e.g., R's ``bs`` will
      raise an error if ``degree=0``, while patsy's will not), they should
      produce identical output given identical input and parameter settings.

    .. warning:: I'm not sure on what the proper handling of points outside
      the lower/upper bounds is, so for now attempting to evaluate a spline
      basis at such points produces an error. Patches gratefully accepted.

    .. versionadded:: 0.2.0
    c                 0    i | _         d | _        d | _        d S r8   )_tmp_degree
_all_knots)selfs    r%   __init__zBS.__init__   s    	r.   N   Fc                 4   ||||||d}|| j         d<   t          j        |          }|j        dk    r|j        d         dk    r|d d df         }|j        dk    rt          d          | j                             dg                               |           d S )N)dfr   r    include_interceptlower_boundupper_boundargsr   r   r   z1input to 'bs' must be 1-d, or a 2-d column vectorxs)rF   r   r   r   r   
ValueError
setdefaultappend)	rI   r   rM   r   r    rN   rO   rP   rQ   s	            r%   memorize_chunkzBS.memorize_chunk   s     !2&&
 
 !	&M!6Q;;171:??!!!Q$A6A::STTT 		T2&&--a00000r.   c                    | j         }|d         }| ` |d         dk     rt          d|d         d          t          |d                   |d         k    rt          d| j        d          t	          j        |d                   }|d         |d	         t          d
          |d         dz   }|d         |d         |z
  }|d         s|dz  }|dk     r6t          d|d         d|d         d|d         d|d         |z
            |d	         Ut          |d	                   |k    r;t          d|d         d|d         d|dt          |d	                   d	          n1t	          j        dd|dz             dd         }t          ||          }|d	         |d	         }|d         	|d         }nt	          j	        |          }|d         	|d         }	nt	          j
        |          }	||	k    rt          d|d|	d          t	          j        |          }|j        dk    rt          d          t	          j        ||k               r t          d|||k              d|d          t	          j        ||	k              r t          d|||	k             d|	d          t	          j        ||	g|z  |f          }
|
                                 |d         | _        |
| _        d S )NrQ   r    r   z#degree must be greater than 0 (not )zdegree must be an integer (not rR   rM   r   zmust specify either df or knotsr   rN   zdf=z is too small for degree=z and include_intercept=z; must be >= z with degree=z	 implies z knots, but z knots were providedr   rO   rP   zlower_bound > upper_bound (z > zknots must be 1 dimensionalzsome knot values (z) fall below lower bound (z) fall above upper bound ()rF   rS   r   rG   r   concatenater   linspacer5   r   r   r   r   anyr   rH   )rI   tmprQ   r   r0   n_inner_knotsknot_quantilesinner_knotsrO   rP   	all_knotss              r%   memorize_finishzBS.memorize_finish   s   i6{I>A*<@NNNL   tH~$x.00*T\\\STTT N3t9%%:$w-"7>???X":! J.M+, #"q   j T


X0111 T
]22
   G}(tG}%%66$* !JJJ NNN)MMW....	 	 	 7 "$Q=13D!E!Ead!K0NCC=$w-K*}-KK&))K*}-KK&))K$$*9DkkkR   j--a:;;;6++,, 	*%kK&?@@@+++O   6++,, 	*%kK&?@@@+++O   N[+$>$F#TUU	H~#r.   c                     t          || j        | j                  }|s|d d dd f         }t          rFt	          |t
          j        t
          j        f          r t          j        |          }|j        |_        |S )Nr   )	r&   rH   rG   r   
isinstancepandasSeries	DataFrameindex)	rI   r   rM   r   r    rN   rO   rP   r"   s	            r%   	transformzBS.transform  sx     $AtEE  	!!!!QRR%LE 	&!fmV-=>?? &(//gr.   )NNrK   FNN)
__name__
__module____qualname____doc__rJ   rV   rb   ri   r   __getstate__ r.   r%   rD   rD   U   s        9 9v   1 1 1 1:R$ R$ R$n    & LLLr.   rD   c                     ddl m}  ddlm}m}m} |                    d          }d}|                    d          }	 ||         dk    sn+|dz  }|                    d|          }|||         }i }	|D ] }
|
                    d	d          \  }}||	|<   !t          |	d
                   t          |	d                   t          |	d                   d}|	d         dk    r"t          |	d                   \  }}||d<   ||d<   |	d         dk    |d<   t          j        t          |	d                             }|d         |j        d         |d         k    sJ  | t          d||fi | |dz  }|dz   }9||k    sJ d S )Nr   )check_stateful)R_bs_test_xR_bs_test_dataR_bs_num_tests
z--BEGIN TEST CASE--Tr   z--END TEST CASE--=r    rM   r   )r    rM   r   zBoundary.knotsNonerO   rP   	interceptTRUErN   outputF)patsy.test_staterq   patsy.test_splines_bs_datarr   rs   rt   splitrh   r   evalr   r   r   rD   )rq   rr   rs   rt   lines	tests_ran	start_idxstop_idxblock	test_datalinekeyvaluekwargslowerupperrz   s                    r%   test_bs_compatr      s   //////VVVVVVVVVV  &&EI122I"!Y#888Q	;;2I>>i()	 	# 	#DC++JC"IcNN )H-..y'')G,--
 
 %&&00	*: ;<<LE5$)F=!$)F=!&/&<&F"#
 D8!45566$<#<?fTl2222r5+v@@@@@Q	qL	E"!F &&&&&&r.   r   c                     t          j        ddd          } t          | ddgdd          }|j        d         dk    sJ t          j        d          }d|| dk     <   t          j        |d d df         |          sJ t          j        d          }d|| dk    | dk     z  <   t          j        |d d df         |          sJ t          j        d          }d|| dk    <   t          j        |d d d	f         |          sJ t          j        t          g d
ddgd          ddgddgddgg          sJ t          | ddgdd          }t          | ddgdd          }t          j        |d d dd f         |          sJ d S )NrY   r   r<      r   T)r   r    rN   rK   r   )r   r   r   )r    r   rN   F)r   logspacer   r   r   array_equal)r   result
expected_0
expected_1
expected_2
result_intresult_no_ints          r%   test_bs_0degreer   R  s   
B2A!QTBBBF<?a"JJq1u>&A,
33333"J%&JQ1q5!">&A,
33333"JJqAv>&A,
33333
 >
999QqcTBBB
Q!Q!Q     
 AaVAFFFJqAqEJJJM>*QQQU+];;;;;;;r.   c            	      "   dd l } t          j        ddd          }|                     t          t
          |dd           |                     t          t
          |dd           |                     t          t
          |           t          |dddgd	z  
           t          |dddgdz  
           t          |dddgdz  d           t          |dddgdz  d           |                     t          t
          |dddgdz  
           |                     t          t
          |dddgd	z  
           |                     t          t
          |dddgdz  d           |                     t          t
          |dddgdz  d           |                     t          t
          |dddgdz  
           |                     t          t
          |dddgdz  
           |                     t          t
          |dddgdz  d           |                     t          t
          |dddgd	z  d           |                     t          t
          |dd           |                     t          t
          |dd           |                     t          t
          |dd           |                     t          t
          |dd           |                     t          t
          |ddd           |                     t          t
          t          j        ||f          d           t          j        t          |ddg          t          |ddg                    sJ |                     t          t
          |dgdgg           |                     t          t
          |ddg           |                     t          t
          |ddgd           |                     t          t
          |ddg           |                     t          t
          |ddgd           d S )Nr   ir<   r=   rK   )rO   )rP   F   )rM   rN   r   T   	   r   )rM   rN   r   r          )rM   r    rY   g      ?)rO   rP   r   )r   )r   rP   i)r   rO   )	pytestr   r[   raisesr   r   rS   column_stackr   )r   r   s     r%   test_bs_errorsr   m  s   MMM
CR  A MM%r1aQM???
MM%r1aQM???
MM*b!$$$ qR5a8888qR4sQw7777qR5aBBBBqR4sQwqAAAA
MM*b!eA3QR7MSSS
MM*b!d1#PQ'MRRR
MMBbE!rRS     MMBbDaPQ     MM*b!eA3QR7MSSS
MM*b!d1#PQ'MRRR
MMBbE!qQR     MMBbDaPQ     MM*b!!M444
MM*b!!M444
MM*b!2M666
MM*b!3M777
MM*b!QA2MFFF
MM*b"/1a&"9"91===>"Qq!f---r!Aq6/B/B/BCCCCC
MM*b!QC";M777
MM*b!Ar7M333
MM*b!Aq6qMAAA
MM*b!C8M444
MM*b!B7MCCCCCr.   )__all__numpyr   
patsy.utilr   r   r   patsy.stater   re   r&   r5   rB   objectrD   r   r   slowr   r   ro   r.   r%   <module>r      s   &     C C C C C C C C C C * * * * * * MMM, , ,^5 5 5/ / /E E E E E E E EP *' *' *'Z  
< < <65D 5D 5D 5D 5Dr.   