ó
 ‰\c           @   s  d  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 Z	 d d l m
 Z
 d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z d d l Z d d	 l m Z m Z m Z m Z m Z m  Z  d d l! Z! e j" d
 ƒ Z# e! j$ j% Z% e! j$ j& Z& e! j$ j' Z' e! j( Z( e! j) Z) e j* d ƒ d d k Z+ d d „ Z- d d „ Z. d „  Z/ d „  Z0 d d „ Z1 d d „ Z2 d „  Z3 d „  Z4 d d „ Z5 e6 d k re1 d ƒ e2 d ƒ e2 d d ƒ e2 d d ƒ n  d S(   s   
Testing utilities.
iÿÿÿÿN(   t   NamedTemporaryFile(   t   testing(   t   assert_array_equalt   assert_array_almost_equalt   assert_array_lesst   assert_array_almost_equal_nulpt   assert_equalt   TestCaset   assert_allcloset   assert_almost_equalt   assert_t   assert_warnst   assert_no_warningsi   (   t   expected_warningsi   (   t   datat   iot   img_as_uintt   img_as_floatt
   img_as_intt   img_as_ubytes%   (\s*>>>.*?)(\s*)#\s*skip\s+if\s+(.*)$t   Pi   i    c         C   sI   d |  | f } | d  k	 r- | d | 7} n  |  | k  sE t | ƒ ‚ d  S(   Ns   %r is not lower than %rs   : (   t   Nonet   AssertionError(   t   at   bt   msgt   message(    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   assert_less)   s    c         C   sI   d |  | f } | d  k	 r- | d | 7} n  |  | k sE t | ƒ ‚ d  S(   Ns   %r is not greater than %rs   : (   R   R   (   R   R   R   R   (    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   assert_greater0   s    c         C   sð   |  j  j d ƒ } g  } x¿ | D]· } t j | ƒ } | d k rS | j | ƒ q n  | j ƒ  \ } } } y' t | |  j ƒ rŽ | | d } n  Wn7 t	 k
 rÈ t | |  j
 j ƒ rÉ | | d } qÉ n X| j | ƒ q Wd j | ƒ |  _  |  S(   sD   Decorator replaces custom skip test markup in doctests

    Say a function has a docstring::

        >>> something, HAVE_AMODULE, HAVE_BMODULE = 0, False, False
        >>> something # skip if not HAVE_AMODULE
        0
        >>> something # skip if HAVE_BMODULE
        0

    This decorator will evaluate the expression after ``skip if``.  If this
    evaluates to True, then the comment is replaced by ``# doctest: +SKIP``. If
    False, then the comment is just removed. The expression is evaluated in the
    ``globals`` scope of `func`.

    For example, if the module global ``HAVE_AMODULE`` is False, and module
    global ``HAVE_BMODULE`` is False, the returned function will have docstring::

        >>> something # doctest: +SKIP
        >>> something + else # doctest: +SKIP
        >>> something # doctest: +SKIP

    s   
s   # doctest: +SKIPN(   t   __doc__t   splitt   SKIP_REt   matchR   t   appendt   groupst   evalt   __globals__t   AttributeErrort   __init__t   join(   t   funct   linest	   new_linest   lineR    t   codet   spacet   expr(    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   doctest_skip_parser7   s"    c         C   s•   d | k r d | } n  t  d | d t ƒ } | j } | j ƒ  t j | |  d | ƒt j | d | ƒ} y t j | ƒ Wn t	 k
 r n X| S(   s/   Save and read an image using a specified plugint   .t   suffixt   deletet   plugin(
   R    t   Falset   namet   closeR   t   imsavet   imreadt   ost   removet	   Exception(   t   imageR3   R1   t	   temp_filet   fnamet   new(    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt	   roundtripf   s    	
t   pngc         C   s±  t  t j ƒ  ƒ } t | |  | ƒ } t j | | ƒ | d k } t | |  | ƒ } t j | j t j ƒ | ƒ t	 | ƒ } t
 d g ƒ  t | |  | ƒ } Wd QXt j | | ƒ t
 d g ƒ  t | ƒ } Wd QX| j ƒ  d	 k r+| d 8} t
 d g ƒ  t | |  | ƒ }	 Wd QXt j |	 | ƒ n> t
 d g ƒ - t | |  | ƒ }	 t j |	 t  | ƒ ƒ Wd QXt | ƒ }
 t
 d g ƒ  t |
 |  | ƒ } Wd QXt j | | ƒ d S(
   sƒ   Check roundtrip behavior for color images.

    All major input types should be handled as ubytes and read
    back correctly.
    i€   s   precision lossNt   tift   tiffid   s	   sign losss   sign loss|precision loss(   RB   RC   (   R   R   t   chelseaR@   R   R   t   astypet   npt   uint8R   R   R   t   lowerR   (   R3   t   fmtt   imgt   r1t   img2t   r2t   img3t   r3t   img4t   r4t   img5t   r5(    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   color_checkv   s0    
c      
   C   sÆ  t  t j ƒ  ƒ } t | |  | ƒ } t j | | ƒ | d k } t | |  | ƒ } t j | j t j ƒ | ƒ t	 | ƒ } t
 d g ƒ  t | |  | ƒ } Wd QX| j j d k rÇ t j | | ƒ n t j | t | ƒ ƒ t
 d g ƒ  t | ƒ } Wd QX| j ƒ  d k rV| d 8} t
 d	 g ƒ  t | |  | ƒ }	 Wd QXt j |	 | ƒ n> t
 d
 g ƒ - t | |  | ƒ }	 t j |	 t | ƒ ƒ Wd QXt | ƒ }
 t |
 |  | ƒ } t j | |
 ƒ d S(   ss   Check the roundtrip behavior for images that support most types.

    All major input types should be handled.
    i€   s   precision|\A\ZNt   fs   precision lossRB   RC   id   s   sign loss|\A\Zs   precision loss|sign loss(   RB   RC   (   R   R   t   moonR@   R   R   RE   RF   RG   R   R   t   dtypet   kindR   R   RH   (   R3   RI   RJ   RK   RL   RM   RN   RO   RP   RQ   RR   RS   (    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt
   mono_check›   s2    
c          C   sv   t  j d ƒ d d l m }  m } m } m } m } d d l m	 } d d l
 m } t j j d ƒ t  j d ƒ d S(	   s½   Default package level setup routine for skimage tests.

    Import packages known to raise warnings, and then
    force warnings to raise errors.

    Also set the random seed to zero.
    t   defaultiÿÿÿÿ(   t   signalt   ndimaget   specialt   optimizet   linalg(   t   loadmat(   t   vieweri    t   errorN(   t   warningst   simplefiltert   scipyR[   R\   R]   R^   R_   t   scipy.ioR`   t   skimageRa   RF   t   randomt   seed(   R[   R\   R]   R^   R_   R`   Ra   (    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt
   setup_testÂ   s    (c           C   s   t  j d ƒ d S(   sh   Default package level teardown routine for skimage tests.

    Restore warnings to default behavior
    RZ   N(   Rc   Rd   (    (    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   teardown_testÕ   s    c            s%   ˆ  d k s t  ‚ ‡  f d †  } | S(   s>  Decorator to run the same function multiple times in parallel.

    This decorator is useful to ensure that separate threads execute
    concurrently and correctly while releasing the GIL.

    Parameters
    ----------
    num_threads : int, optional
        The number of times the function is run in parallel.

    i    c            s%   t  j ˆ  ƒ ‡  ‡ f d †  ƒ } | S(   Nc             s•   g  } xC t  ˆ d ƒ D]1 } t j d ˆ  d |  d | ƒ } | j | ƒ q Wx | D] } | j ƒ  qS Wˆ  |  | Ž  } x | D] } | j ƒ  q} W| S(   Ni   t   targett   argst   kwargs(   t   ranget	   threadingt   ThreadR!   t   startR'   (   Rm   Rn   t   threadst   it   threadt   result(   R(   t   num_threads(    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   innerí   s    (   t	   functoolst   wraps(   R(   Rx   (   Rw   (   R(   s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   wrapperì   s    !(   R   (   Rw   R{   (    (   Rw   s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   test_parallelÝ   s    t   __main__t   pilt   bmpRC   (7   R   R9   t   ret   structRp   Ry   t   tempfileR    t   numpyRF   R   t   numpy.testingR   R   R   R   R   R   R   R	   R
   R   R   t	   _warningsR   Rc   t    R   R   R   R   R   R   t   pytestt   compileR   t   markt   skipift   xfailt   parametrizet   raisest   fixturet   calcsizet   arch32R   R   R   R/   R@   RT   RY   Rj   Rk   R|   t   __name__(    (    (    s6   lib/python2.7/site-packages/skimage/_shared/testing.pyt   <module>   sD   L.			/	%'		%

