ó
^{UZc           @   s»   d  g Z  d d l Z d d l m Z m Z m Z d d l m Z e rV d d l	 Z	 n  d „  Z
 d „  Z d „  Z d e f d	 „  ƒ  YZ e e ƒ Z d
 „  Z d e _ d „  Z d „  Z d S(   t   bsiÿÿÿÿN(   t   have_pandast   no_picklingt   assert_no_pickling(   t   stateful_transformc         C   s¼  y d d l  m } Wn t k
 r3 t d ƒ ‚ n Xt j t j | d t ƒƒ } | j d k sg t ‚ | j	 ƒ  t
 | ƒ } t j |  ƒ }  |  j d k rÇ |  j d d k rÇ |  d  d  … d f }  n  |  j d k sÜ t ‚ t j |  ƒ t j | ƒ k  st j |  ƒ t j | ƒ k r't d ƒ ‚ n  t | ƒ | d } t j |  j d | f d t ƒ} xX t | ƒ D]J } t j | f ƒ } d | | <| |  | | | f ƒ | d  d  … | f <qjW| S(	   Niÿÿÿÿ(   t   splevs#   spline functionality requires scipyt   dtypei   i   i    sk   some data points fall outside the outermost knots, and I'm not sure how to handle them. (Patches accepted!)(   t   scipy.interpolateR   t   ImportErrort   npt
   atleast_1dt   asarrayt   floatt   ndimt   AssertionErrort   sortt   intt   shapet   mint   maxt   NotImplementedErrort   lent   emptyt   ranget   zeros(   t   xt   knotst   degreeR   t   n_basest   basist   it   coefs(    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   _eval_bspline_basis   s*    
"<"
,c         C   sc   t  j | ƒ } t  j g  | j d d ƒ D] } t  j |  d | ƒ ^ q( ƒ } | j | j d d ƒS(   Nt   ordert   Cid   (   R	   R   t   ravelt
   percentilet   reshapeR   (   R   t   probst   probt	   quantiles(    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   _R_compat_quantile>   s    	5c          C   sƒ   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         S   s%   t  j t |  | ƒ | ƒ s! t ‚ d  S(   N(   R	   t   allcloseR)   R   (   R   R'   t   expected(    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   tF   s    i
   i   g      à?i   g333333Ó?i   gffffffæ?i   gš™™™™™@g333333@(   t   listR   (   R,   (    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   test__R_compat_quantileE   s
    	"t   BSc           B   s\   e  Z d  Z d „  Z d d d e d d d „ Z d „  Z d d d e d d d „ Z e	 Z
 RS(   s3  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         C   s   i  |  _  d  |  _ d  |  _ d  S(   N(   t   _tmpt   Nonet   _degreet
   _all_knots(   t   self(    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   __init__ˆ   s    		i   c   	      C   sÅ   i | d 6| d 6| d 6| d 6| d 6| d 6} | |  j  d <t j | ƒ } | j d k r‡ | j d	 d	 k r‡ | d  d  … d
 f } n  | j d	 k r¥ t d ƒ ‚ n  |  j  j d g  ƒ j | ƒ d  S(   Nt   dfR   R   t   include_interceptt   lower_boundt   upper_boundt   argsi   i   i    s1   input to 'bs' must be 1-d, or a 2-d column vectort   xs(   R0   R	   R
   R   R   t
   ValueErrort
   setdefaultt   append(	   R4   R   R6   R   R   R7   R8   R9   R:   (    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   memorize_chunk   s    

"c         C   s@  |  j  } | d } |  `  | d d k  rC t d | d f ƒ ‚ n  t | d ƒ | d k rv t d |  j f ƒ ‚ n  t j | d ƒ } | d d  k r¸ | d d  k r¸ t d	 ƒ ‚ n  | d d
 } | d d  k	 rÆ| d | } | d sû | d
 7} n  | d k  r:t d | d | d | d | d | f ƒ ‚ n  | d d  k	 r”t | d ƒ | k rÃt d | d | d | t | d ƒ f ƒ ‚ qÃqÆt j d d
 | d ƒ d
 d !} t	 | | ƒ } n  | d d  k	 rã| d } n  | d d  k	 r | d } n t j
 | ƒ } | d d  k	 r,| d }	 n t j | ƒ }	 | |	 k r`t d | |	 f ƒ ‚ n  t j | ƒ } | j d
 k rt d ƒ ‚ n  t j | | k  ƒ rÅt d | | | k  | f ƒ ‚ n  t j | |	 k ƒ rýt d | | |	 k |	 f ƒ ‚ n  t j | |	 g | | f ƒ }
 |
 j ƒ  | d |  _ |
 |  _ d  S(   NR:   R   i    s&   degree must be greater than 0 (not %r)s"   degree must be an integer (not %r)R;   R6   R   s   must specify either df or knotsi   R7   sH   df=%r is too small for degree=%r and include_intercept=%r; must be >= %ssA   df=%s with degree=%r implies %s knots, but %s knots were providedi   iÿÿÿÿR8   R9   s#   lower_bound > upper_bound (%r > %r)s   knots must be 1 dimensionals1   some knot values (%s) fall below lower bound (%r)s1   some knot values (%s) fall above upper bound (%r)(   R0   R<   R   R2   R	   t   concatenateR1   R   t   linspaceR)   R   R   R   R   t   anyR   R3   (   R4   t   tmpR:   R   R!   t   n_inner_knotst   knot_quantilest   inner_knotsR8   R9   t	   all_knots(    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   memorize_finish£   sp    	
 
  
c   	      C   sƒ   t  | |  j |  j ƒ } | s= | d  d  … d d  … f } n  t r t | t j t j f ƒ r t j | ƒ } | j | _ q n  | S(   Ni   (	   R    R3   R2   R   t
   isinstancet   pandast   Seriest	   DataFramet   index(	   R4   R   R6   R   R   R7   R8   R9   R   (    (    s,   lib/python2.7/site-packages/patsy/splines.pyt	   transformì   s    N(   t   __name__t
   __module__t   __doc__R5   R1   t   FalseR?   RH   RN   R   t   __getstate__(    (    (    s,   lib/python2.7/site-packages/patsy/splines.pyR/   M   s   :			I	
c          C   sæ  d d l  m }  d d l m } m } m } | j d ƒ } d } | j d ƒ } x}t rÏ| | d k sm Pn  | d 7} | j d | ƒ } | | | !} i  }	 x0 | D]( }
 |
 j d	 d ƒ \ } } | |	 | <q£ Wi t	 |	 d
 ƒ d
 6t
 |	 d ƒ d 6t
 |	 d ƒ d 6} |	 d d k rEt
 |	 d ƒ \ } } | | d <| | d <n  |	 d d k | d <t j t
 |	 d ƒ ƒ } | d d  k	 r¢| j d | d k s¢t ‚ n  |  t t | | |  | d 7} | d } qS W| | k sât ‚ d  S(   Niÿÿÿÿ(   t   check_stateful(   t   R_bs_test_xt   R_bs_test_datat   R_bs_num_testss   
i    s   --BEGIN TEST CASE--i   s   --END TEST CASE--t   =R   R6   R   s   Boundary.knotsR1   R8   R9   t	   interceptt   TRUER7   t   output(   t   patsy.test_stateRT   t   patsy.test_splines_bs_dataRU   RV   RW   t   splitRM   t   TrueR   t   evalR	   R   R1   R   R   R/   RR   (   RT   RU   RV   RW   t   linest	   tests_rant	   start_idxt   stop_idxt   blockt	   test_datat   linet   keyt   valuet   kwargst   lowert   upperR[   (    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   test_bs_compatü   s>    	

 
i   c       	   C   s  t  j d d d ƒ }  t |  d d d g d d d t ƒ} | j d d	 k sR t ‚ t  j d ƒ } d | |  d k  <t  j | d  d  … d f | ƒ s™ t ‚ t  j d ƒ } d | |  d k |  d k  @<t  j | d  d  … d f | ƒ sê t ‚ t  j d ƒ } d | |  d k <t  j | d  d  … d
 f | ƒ s1t ‚ t  j t d d d
 g d d d d g d t ƒd d g d d g d d g g ƒ sˆt ‚ t |  d d d g d d d t ƒ} t |  d d d g d d d t ƒ} t  j | d  d  … d d  … f | ƒ sþt ‚ d  S(   Niÿÿÿÿi   i
   R   i   R   i    R7   i   i   (	   R	   t   logspaceR    R_   R   R   R   t   array_equalRR   (   R   t   resultt
   expected_0t
   expected_1t
   expected_2t
   result_intt   result_no_int(    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   test_bs_0degree-  s(    $((('		$$c          C   sî  d d l  m }  t j d d d ƒ } |  t t | d d d ƒ|  t t | d d	 d ƒ|  t t | ƒ t | d
 d d t d d g d ƒt | d
 d d t d d g d ƒt | d
 d d t d d g d d d ƒt | d
 d d t d d g d d d ƒ|  t t | d
 d d t d d g d ƒ|  t t | d
 d d t d d g d ƒ|  t t | d
 d d t d d g d d d ƒ|  t t | d
 d d t d d g d d d ƒ|  t t | d
 d d t d d g d ƒ|  t t | d
 d d t d d g d ƒ|  t t | d
 d d t d d g d d d ƒ|  t t | d
 d d t d d g d d d ƒ|  t t | d
 d d d ƒ|  t t | d
 d d d ƒ|  t t | d
 d d d ƒ|  t t | d
 d d d ƒ|  t t | d d d d	 d ƒ|  t t t j	 | | f ƒ d ƒ t j
 t | d d d g ƒt | d d d g ƒƒ sLt ‚ |  t t | d d g d g g ƒ|  t t | d d d g ƒ|  t t | d d d g d	 d ƒ|  t t | d d d g ƒ|  t t | d d d g d d ƒd  S(   Niÿÿÿÿ(   t   assert_raisesiöÿÿÿi
   i   i   R8   i    R9   R6   R7   R   i   i   i	   R   i   i   i   g      ø?i   iìÿÿÿiüÿÿÿiýÿÿÿ(   t
   nose.toolsRw   R	   RA   R   R    R<   RR   R_   t   column_stackRo   R   (   Rw   R   (    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   test_bs_errorsH  sh    ##))##""##""<(   t   __all__t   numpyR	   t
   patsy.utilR   R   R   t   patsy.stateR   RJ   R    R)   R.   t   objectR/   R    Rm   t   slowRv   Rz   (    (    (    s,   lib/python2.7/site-packages/patsy/splines.pyt   <module>   s   		,		­	-		