ó
î&]\c           @` su  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 m
 Z
 d  d l m Z d  d l Z d  d l Z e j d d „ Z d d d „ Z d  d „ Z e d d d	 „ Z d
 „  Z d e f d „  ƒ  YZ d „  Z e e e e e d „ Z y/ e j e
 d d d d d g ƒ Z d „  Z  Wn e! k
 rZd „  Z  n Xd e f d „  ƒ  YZ" d S(   i    (   t   divisiont   print_functiont   absolute_importN(   t
   namedtuple(   t   Poolc         C` s_   t  j |  d t ƒ| } | d k	 r7 | j | ƒ } n  t | t  j ƒ s[ t  j | ƒ } n  | S(   s"   Return an array of all value.
    t   dtypeN(   t   npt   onest   boolt   Nonet   astypet
   isinstancet   ndarrayt   asarray(   t   shapet   valuet   typecodet   out(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt	   _valarray   s    c   	      ` s(  | d k r3 | d k r' t d ƒ ‚ qN t j } n | d k	 rN t d ƒ ‚ n  t j | Œ  } t ‡  f d †  | Dƒ ƒ } t j g  | D] } | j j ^ q† ƒ } t	 t j
 | d ƒ d | d | ƒ} t j | ˆ  | | Œ  ƒ | d k	 r$t ‡  f d †  | Dƒ ƒ } t j | ˆ  | | Œ  ƒ n  | S(	   s¼  
    np.where(cond, x, fillvalue) always evaluates x even where cond is False.
    This one only evaluates f(arr1[cond], arr2[cond], ...).
    For example,
    >>> a, b = np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])
    >>> def f(a, b):
        return a*b
    >>> _lazywhere(a > 2, (a, b), f, np.nan)
    array([ nan,  nan,  21.,  32.])

    Notice it assumes that all `arrays` are of the same shape, or can be
    broadcasted together.

    s%   One of (fillvalue, f2) must be given.s)   Only one of (fillvalue, f2) can be given.c         3` s!   |  ] } t  j ˆ  | ƒ Vq d  S(   N(   R   t   extract(   t   .0t   arr(   t   cond(    s/   lib/python2.7/site-packages/scipy/_lib/_util.pys	   <genexpr>4   s    i    R   R   c         3` s"   |  ] } t  j ˆ  | ƒ Vq d  S(   N(   R   R   (   R   R   (   R   (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pys	   <genexpr>9   s    N(   R	   t
   ValueErrorR   t   nant   broadcast_arrayst   tuplet   mintypecodeR   t   charR   R   t   place(	   R   t   arrayst   ft	   fillvaluet   f2t   tempt   at   tcodeR   (    (   R   s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt
   _lazywhere   s    (%c         ` sþ   t  j | Œ  } t  j g  | D] } | j j ^ q ƒ } t t  j | d ƒ d | d | ƒ} x› t t |  ƒ ƒ D]‡ } | | |  | } ‰  t  j	 ˆ  t
 k ƒ r¥ qo n  t  j ˆ  | d ƒ \ ‰  }	 t ‡  f d †  | Dƒ ƒ }
 t  j | ˆ  | |
 Œ  ƒ qo W| S(   s?  
    Mimic `np.select(condlist, choicelist)`.

    Notice it assumes that all `arrays` are of the same shape, or can be
    broadcasted together.

    All functions in `choicelist` must accept array arguments in the order
    given in `arrays` and must return an array of the same shape as broadcasted
    `arrays`.

    Examples
    --------
    >>> x = np.arange(6)
    >>> np.select([x <3, x > 3], [x**2, x**3], default=0)
    array([  0,   1,   4,   0,  64, 125])

    >>> _lazyselect([x < 3, x > 3], [lambda x: x**2, lambda x: x**3], (x,))
    array([   0.,    1.,    4.,   0.,   64.,  125.])

    >>> a = -np.ones_like(x)
    >>> _lazyselect([x < 3, x > 3],
    ...             [lambda x, a: x**2, lambda x, a: a * x**3],
    ...             (x, a), default=np.nan)
    array([   0.,    1.,    4.,   nan,  -64., -125.])

    i    R   R   c         3` s!   |  ] } t  j ˆ  | ƒ Vq d  S(   N(   R   R   (   R   R   (   R   (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pys	   <genexpr>b   s    (   R   R   R   R   R   R   R   t   ranget   lent   allt   FalseR   R   (   t   condlistt
   choicelistR   t   defaultR#   R$   R   t   indext   funct   _R"   (    (   R   s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   _lazyselect?   s    (%t   Cc         C` sî   t  j | ƒ } | d k r' | j } n  t |  d ƒ sB |  f }  n  t j t j |  ƒ | j	 } t  j
 | | d t  j ƒ } | j d d | } | d k r© | | } n  | | | | d !d  } t  j |  | | d | ƒ} | j d ƒ | S(   sô   Allocate a new ndarray with aligned memory.

    Primary use case for this currently is working around a f2py issue
    in Numpy 1.9.1, where dtype.alignment is such that np.zeros() does
    not necessarily create arrays aligned up to it.

    t   __len__i   t   datai    iÿÿÿÿt   orderN(   R   R   R	   t	   alignmentt   hasattrt	   functoolst   reducet   operatort   mult   itemsizet   emptyt   uint8t   __array_interface__R   t   fill(   R   R   R4   t   alignt   sizet   buft   offsetR3   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   _aligned_zerosg   s    c         C` s6   |  j  d k	 r2 |  j |  j  j d k  r2 |  j ƒ  S|  S(   sÅ   Return an array equivalent to the input array. If the input
    array is a view of a much larger array, copy its contents to a
    newly allocated array. Otherwise, return the input unchanged.
    i   N(   t   baseR	   RA   t   copy(   t   array(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   _prune_array   s    (
t   DeprecatedImportc           B` s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s2  
    Deprecated import, with redirection + warning.

    Examples
    --------
    Suppose you previously had in some module::

        from foo import spam

    If this has to be deprecated, do::

        spam = DeprecatedImport("foo.spam", "baz")

    to redirect users to use "baz" module instead.

    c         C` s6   | |  _  | |  _ t |  j ƒ t j |  j |  _ d  S(   N(   t	   _old_namet	   _new_namet
   __import__t   syst   modulest   _mod(   t   selft   old_module_namet   new_module_name(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   __init__   s    		c         C` s   t  |  j ƒ S(   N(   t   dirRO   (   RP   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   __dir__£   s    c         C` s0   t  j d |  j |  j f t ƒ t |  j | ƒ S(   Ns'   Module %s is deprecated, use %s instead(   t   warningst   warnRJ   RK   t   DeprecationWarningt   getattrRO   (   RP   t   name(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   __getattr__¦   s    	(   t   __name__t
   __module__t   __doc__RS   RU   R[   (    (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyRI   ‹   s   		c         C` s€   |  d k s |  t j k r( t j j j St |  t j t j f ƒ rS t j j	 |  ƒ St |  t j j	 ƒ rl |  St
 d |  ƒ ‚ d S(   s6  Turn seed into a np.random.RandomState instance

    If seed is None (or np.random), return the RandomState singleton used
    by np.random.
    If seed is an int, return a new RandomState instance seeded with seed.
    If seed is already a RandomState instance, return it.
    Otherwise raise ValueError.
    s=   %r cannot be used to seed a numpy.random.RandomState instanceN(   R	   R   t   randomt   mtrandt   _randR   t   numberst   Integralt   integert   RandomStateR   (   t   seed(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   check_random_state®   s    	c   	      C` s÷   | s< d d l  } | j j |  ƒ r< d } t | ƒ ‚ q< n  | sf t j j |  ƒ rf t d ƒ ‚ qf n  | ru t j n t j } | |  ƒ }  | sº |  j	 t j	 d ƒ k rº t d ƒ ‚ qº n  | ró t j
 |  j	 t j ƒ só | |  d t j ƒ}  qó n  |  S(   sB  
    Helper function for scipy argument validation.

    Many scipy linear algebra functions do support arbitrary array-like
    input arguments.  Examples of commonly unsupported inputs include
    matrices containing inf/nan, sparse matrix representations, and
    matrices with complicated elements.

    Parameters
    ----------
    a : array_like
        The array-like input.
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
        Default: True
    sparse_ok : bool, optional
        True if scipy sparse matrices are allowed.
    objects_ok : bool, optional
        True if arrays with dype('O') are allowed.
    mask_ok : bool, optional
        True if masked arrays are allowed.
    as_inexact : bool, optional
        True to convert the input array to a np.inexact dtype.

    Returns
    -------
    ret : ndarray
        The converted validated array.

    i    Nsx   Sparse matrices are not supported by this function. Perhaps one of the scipy.sparse.linalg functions would work instead.s   masked arrays are not supportedt   Os   object arrays are not supportedR   (   t   scipy.sparset   sparset   issparseR   R   t   mat   isMaskedArrayt   asarray_chkfiniteR   R   t
   issubdtypet   inexactt   float_(	   R#   t   check_finitet	   sparse_okt
   objects_okt   mask_okt
   as_inexactt   scipyt   msgt   toarray(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   _asarray_validatedÁ   s"    #t   ArgSpect   argst   varargst   keywordst   defaultsc         C` sN  t  j |  ƒ } g  | j j ƒ  D]$ } | j t  j j k r | j ^ q } g  | j j ƒ  D]$ } | j t  j j k rY | j ^ qY } | r“ | d n d } g  | j j ƒ  D]$ } | j t  j j
 k r© | j ^ q© } | rã | d n d } g  | j j ƒ  D]6 } | j t  j j k rù | j | j k	 rù | j ^ qù p8d } t | | | | ƒ S(   sö  inspect.getargspec replacement using inspect.signature.

        inspect.getargspec is deprecated in python 3. This is a replacement
        based on the (new in python 3.3) `inspect.signature`.

        Parameters
        ----------
        func : callable
            A callable to inspect

        Returns
        -------
        argspec : ArgSpec(args, varargs, varkw, defaults)
            This is similar to the result of inspect.getargspec(func) under
            python 2.x.
            NOTE: if the first argument of `func` is self, it is *not*, I repeat
            *not* included in argspec.args.
            This is done for consistency between inspect.getargspec() under
            python 2.x, and inspect.signature() under python 3.x.
        i    N(   t   inspectt	   signaturet
   parameterst   valuest   kindt	   Parametert   POSITIONAL_OR_KEYWORDRZ   t   VAR_POSITIONALR	   t   VAR_KEYWORDR,   R<   R{   (   R.   t   sigt   pR|   R}   t   varkwR   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   getargspec_no_self  s    $$$!c         C` s9   t  j |  ƒ } | j d d k r5 | j j d ƒ n  | S(   sD  inspect.getargspec replacement for compatibility with python 3.x.

        inspect.getargspec is deprecated in python 3. This wraps it, and
        *removes* `self` from the argument list of `func`, if present.
        This is done for forward compatibility with python 3.

        Parameters
        ----------
        func : callable
            A callable to inspect

        Returns
        -------
        argspec : ArgSpec(args, varargs, varkw, defaults)
            This is similar to the result of inspect.getargspec(func) under
            python 2.x.
            NOTE: if the first argument of `func` is self, it is *not*, I repeat
            *not* included in argspec.args.
            This is done for consistency between inspect.getargspec() under
            python 2.x, and inspect.signature() under python 3.x.
        i    RP   (   R€   t
   getargspecR|   t   pop(   R.   t   argspec(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyRŒ   ;  s    t
   MapWrapperc           B` sY   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 RS(
   sv  
    Parallelisation wrapper for working with map-like callables, such as
    `multiprocessing.Pool.map`.

    Parameters
    ----------
    pool : int or map-like callable
        If `pool` is an integer, then it specifies the number of threads to
        use for parallelization. If ``int(pool) == 1``, then no parallel
        processing is used and the map builtin is used.
        If ``pool == -1``, then the pool will utilise all available CPUs.
        If `pool` is a map-like callable that follows the same
        calling sequence as the built-in map function, then this callable is
        used for parallelisation.
    i   c         C` sâ   d  |  _ t |  _ t |  _ t | ƒ r? | |  _ |  j |  _ nŸ t | ƒ d k rx t ƒ  |  _ |  j j |  _ t	 |  _ nf t | ƒ d k r nQ t | ƒ d k rÒ t d t | ƒ ƒ |  _ |  j j |  _ t	 |  _ n t
 d ƒ ‚ d  S(   Niÿÿÿÿi   t	   processessU   Number of workers specified must be -1, an int >= 1, or an object with a 'map' method(   R	   t   poolt   mapt   _mapfuncR)   t	   _own_poolt   callablet   intR   t   Truet   RuntimeError(   RP   R’   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyRS   g  s"    				c         C` s   |  S(   N(    (   RP   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt	   __enter__  s    c         C` s   |  j  ƒ  d  S(   N(   t   close(   RP   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   __del__„  s    c         C` s   |  j  r |  j j ƒ  n  d  S(   N(   R•   R’   t	   terminate(   RP   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyR   ‡  s    	c         C` s   |  j  r |  j j ƒ  n  d  S(   N(   R•   R’   t   join(   RP   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyRž   ‹  s    	c         C` s   |  j  r |  j j ƒ  n  d  S(   N(   R•   R’   R›   (   RP   (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyR›     s    	c         C` sF   |  j  rB | d  k r2 |  j j ƒ  |  j j ƒ  qB |  j j ƒ  n  d  S(   N(   R•   R	   R’   R›   Rž   R   (   RP   t   exc_typet	   exc_valuet	   traceback(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   __exit__“  s
    	c         C` s8   y |  j  | | ƒ SWn t k
 r3 t d ƒ ‚ n Xd  S(   Ns;   The map-like callable must be of the form f(func, iterable)(   R”   t	   TypeError(   RP   R.   t   iterable(    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   __call__›  s    (   R\   R]   R^   RS   Rš   Rœ   R   Rž   R›   R¢   R¥   (    (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyR   W  s   						(#   t
   __future__R    R   R   R7   R9   RM   RV   Rb   t   collectionsR   t   multiprocessingR   R€   t   numpyR   R   R	   R   R%   R0   t   floatRD   RH   t   objectRI   Rg   R˜   R)   Rz   R   R{   RŒ   t   AttributeErrorR   (    (    (    s/   lib/python2.7/site-packages/scipy/_lib/_util.pyt   <module>   s4   $(	
#		C+