ó
 ‰\c           @   s   d  d l  m Z d  d l m Z d g Z y d  d l j Z e Z	 Wn e
 k
 r[ e Z	 n Xd „  Z d d d d i  d „ Z d S(	   iÿÿÿÿ(   t   ceil(   t	   cpu_countt   apply_parallelNc   	      C   s¾   g  } t  t | d t |  ƒ ƒ ƒ } d } x… |  D]} } | | k  r | | } | | | } | d k ru | f } q™ | f | d | f } n	 | f } | j | ƒ | | 9} q3 Wt | ƒ S(   sà  Split the array into equal sized chunks based on the number of
    available processors. The last chunk in each dimension absorbs the
    remainder array elements if the number of CPUs does not divide evenly into
    the number of array elements.

    Examples
    --------
    >>> _get_chunks((4, 4), 4)
    ((2, 2), (2, 2))
    >>> _get_chunks((4, 4), 2)
    ((2, 2), (4,))
    >>> _get_chunks((5, 5), 2)
    ((2, 3), (5,))
    >>> _get_chunks((2, 4), 2)
    ((1, 1), (4,))
    g      ð?i   i    (   t   intR    t   lent   appendt   tuple(	   t   shapet   ncput   chunkst   nchunks_per_dimt   used_chunkst   it   regular_chunkt   remainder_chunkt
   chunk_lens(    (    s:   lib/python2.7/site-packages/skimage/util/apply_parallel.pyt   _get_chunks   s     
	i    c            sè   t  s t d ƒ ‚ n  | d k rc | j } y t ƒ  } Wn t k
 rP d } n Xt | | ƒ } n  | d k rx d } n* | d k r d } n | d k r¢ d } n  ‡  ‡ ‡ f d	 †  }	 t j | d
 | ƒ}
 |
 j	 |	 | d | ƒj
 ƒ  S(   sÏ  Map a function in parallel across an array.

    Split an array into possibly overlapping chunks of a given depth and
    boundary type, call the given function in parallel on the chunks, combine
    the chunks and return the resulting array.

    Parameters
    ----------
    function : function
        Function to be mapped which takes an array as an argument.
    array : numpy array
        Array which the function will be applied to.
    chunks : int, tuple, or tuple of tuples, optional
        A single integer is interpreted as the length of one side of a square
        chunk that should be tiled across the array.  One tuple of length
        ``array.ndim`` represents the shape of a chunk, and it is tiled across
        the array.  A list of tuples of length ``ndim``, where each sub-tuple
        is a sequence of chunk sizes along the corresponding dimension. If
        None, the array is broken up into chunks based on the number of
        available cpus. More information about chunks is in the documentation
        `here <https://dask.pydata.org/en/latest/array-design.html>`_.
    depth : int, optional
        Integer equal to the depth of the added boundary cells. Defaults to
        zero.
    mode : {'reflect', 'symmetric', 'periodic', 'wrap', 'nearest', 'edge'}, optional
        type of external boundary padding.
    extra_arguments : tuple, optional
        Tuple of arguments to be passed to the function.
    extra_keywords : dictionary, optional
        Dictionary of keyword arguments to be passed to the function.

    Notes
    -----
    Numpy edge modes 'symmetric', 'wrap', and 'edge' are converted to the
    equivalent `dask` boundary modes 'reflect', 'periodic' and 'nearest',
    respectively.
    sA   Could not import 'dask'.  Please install using 'pip install dask'i   t   wrapt   periodict	   symmetrict   reflectt   edget   nearestc            s   ˆ |  ˆ  ˆ Ž S(   N(    (   t   arr(   t   extra_argumentst   extra_keywordst   function(    s:   lib/python2.7/site-packages/skimage/util/apply_parallel.pyt   wrapped_funco   s    R	   t   boundaryN(   t   dask_availablet   RuntimeErrort   NoneR   R   t   NotImplementedErrorR   t   dat
   from_arrayt   map_overlapt   compute(   R   t   arrayR	   t   deptht   modeR   R   R   R   R   t   darr(    (   R   R   R   s:   lib/python2.7/site-packages/skimage/util/apply_parallel.pyR   5   s$    '	
			(    (   t   mathR    t   multiprocessingR   t   __all__t
   dask.arrayR%   R!   t   TrueR   t   ImportErrort   FalseR   R   R   (    (    (    s:   lib/python2.7/site-packages/skimage/util/apply_parallel.pyt   <module>   s   	

	'	