ó
 m[c           @` s  d  Z  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
 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 Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d
 d d g Z d   Z d d d  Z d   Z d# d  Z d   Z  e j! d  j" Z# d   Z$ d e% f d     YZ& d   Z' i  Z( e'   d   Z) d   Z* i  Z+ e, rĢe j-   rĢd   e+ d <n  e j. d  d    Z/ d   Z0 d    Z1 e, d!  Z2 d"   Z3 d S($   sD   
Provides a collection of utilities for comparing (image) results.

i    (   t   absolute_importt   divisiont   print_functionN(   t   TemporaryFile(   t
   subprocess(   t   ImageComparisonFailure(   t   _png(   t   _get_cachedir(   t   cbookt   compare_floatt   compare_imagest   comparable_formatsc         C` s)   t  j j |   \ } } d | | | f S(   sU   
    Make a new filename by inserting `purpose` before the file's
    extension.
    s   %s-%s%s(   t   ost   patht   splitext(   t   fnamet   purposet   baset   ext(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   make_test_filename   s    c   	      C` s:  | d
 k r' | d
 k r' t d   n  d } | d
 k	 r¢ t |  |  } | | k  r¢ d d d d d g } | d j g  | D] } | j t     ^ qz  7} q¢ n  | d
 k	 r0t |  |  } |  r× | t |   } n  | | k  r0d d d d d	 g } | d j g  | D] } | j t     ^ q 7} q0n  | p9d
 S(   s¦   
    Fail if the floating point values are not close enough, with
    the given message.

    You can specify a relative tolerance, absolute tolerance, or both.

    s}   You haven't specified a 'relTol' relative tolerance or a 'absTol' absolute tolerance function argument. You must specify one.t    s   Expected: {expected}s   Actual:   {actual}s   Abs diff: {absDiff}s   Abs tol:  {absTol}s   
  s   Rel diff: {relDiff}s   Rel tol:  {relTol}N(   t   Nonet
   ValueErrort   abst   joint   formatt   locals(	   t   expectedt   actualt   relTolt   absTolt   msgt   absDifft   templatet   linet   relDiff(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyR	   (   s0    	8	8c          C` s   t    }  |  d  k r$ t d   n  t j j |  d  } t j j |  st y t j |  Wqt t	 k
 rp d  SXn  t j
 | t j  s d  S| S(   Ns1   Could not find a suitable configuration directoryt
   test_cache(   R   R   t   RuntimeErrorR   R   R   t   existsR   t   mkdirst   IOErrort   accesst   W_OK(   t   cachedirt	   cache_dir(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   get_cache_dirS   s    	i   i   c         C` sŪ   t  j   } t |  d  : } x0 t rP | j |  } | s@ Pn  | j |  q! WWd  QX|  j d  r d d l m } | j |   d j	 d   n; |  j d  rŃ d d l m
 } | j |   j	 d   n  | j   S(	   Nt   rbs   .pdfi    (   t   checkdep_ghostscripti   s   utf-8s   .svg(   t   checkdep_inkscape(   t   hashlibt   md5t   opent   Truet   readt   updatet   endswitht
   matplotlibR/   t   encodeR0   t	   hexdigest(   R   t
   block_sizeR2   t   fdt   dataR/   R0   (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   get_file_hashb   s    	 c         ` s     f d   } | S(   Nc         ` sĮ     |  |  } t  j | d t d t  j d t  j } | j   \ } } | j   } t j j |  sm | r½ d d j	 |  } | r | d | 7} n  | r® | d | 7} n  t
 |   n  d  S(   Nt   universal_newlinest   stdoutt   stderrs   Conversion command failed:
%s
t    s   Standard output:
%s
s   Standard error:
%s
(   R   t   PopenR4   t   PIPEt   communicatet   waitR   R   R&   R   R(   (   t   oldt   newt   cmdlinet   pipeR@   RA   t   errcodeR   (   t   cmd(    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   convertv   s    (    (   RL   RM   (    (   RL   s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt    make_external_conversion_commandu   s    s   [^a-zA-Z0-9_@%+=:,./-]c         C` s.   t  |   d  k r |  Sd |  j d d  d S(   Nt   's   '"'"'(   t   _find_unsafe_bytesR   t   replace(   t   b(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   _shlex_quote_bytes   s    t   _SVGConverterc           B` s,   e  Z d    Z d   Z d   Z d   Z RS(   c         C` s   d  |  _ t j |  j  d  S(   N(   R   t   _proct   atexitt   registert   __del__(   t   self(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   __init__   s    	c         C` sę   t  t j |  j j j d  d  } d } t |  } t j | |  } x6 t	 |  D]( \ } } t
 t j | | |  d  qU Wx^ t rį t t t
 |   } t |  | k rµ t S|  j j   d k	 rĪ t S| | k r t Sq Wd S(   s8   Did Inkscape reach the prompt without crashing?
        i   R   s   
t   >N(   s   
R[   (   t   itert	   functoolst   partialRU   R@   R5   t   lent	   itertoolst   teet	   enumeratet   nextt   isliceR   R4   t   tuplet   mapt   Falset   poll(   RY   t   streamt   promptt   nt   itst   it   itt   window(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   _read_to_prompt   s    $ 	c      
   C` s²  |  j  s |  j  j   d  k	 r½ t j j   } | j d d   t j | t d  <t	   |  _
 t j t d  d d g d t j d t j d |  j
 d	 | |  _  |  j   s½ t d
   q½ n  y t j } Wn t k
 rę d   } n Xt t t | | | g   \ } } d | k s#d | k r9t d    | |  S|  j  j j | d | d  |  j  j j   |  j   s®|  j
 j d  t |  j
 j   j t j   d    n  d  S(   Nt   DISPLAYt   INKSCAPE_PROFILE_DIRt   inkscapes   --without-guis   --shellt   stdinR@   RA   t   envs   Failed to start Inkscapec         S` s   |  j  t j    S(   N(   R9   t   syst   getfilesystemencoding(   t   s(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   fsencodeĢ   s    s   
c         S` s   t  d  d |  d | g S(   NRs   s   -zs   --export-png(   t   str(   RG   RH   (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   <lambda>×   s    s    --export-png=i    RQ   (   RU   Rh   R   R   t   environt   copyt   popt   devnullRz   R   t   _stderrR   RC   RD   Rp   t   OSErrorRy   t   AttributeErrorRf   RS   RN   Rt   t   writet   flusht   seekR   R5   t   decodeRv   Rw   (   RY   t   origt   destRu   Ry   t   orig_bt   dest_b(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   __call__°   s6    
$
c         C` sr   |  j  rn |  j  j   d  k r> |  j  j d  |  j  j   n  |  j  j j   |  j  j j   |  j j   n  d  S(   Ns   quit
(	   RU   Rh   R   RE   RF   Rt   t   closeR@   R   (   RY   (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyRX   å   s    	(   t   __name__t
   __module__RZ   Rp   R   RX   (    (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyRT      s   				5c          ` sv   t  j   \   }  |  d  k	 rP   f d   } t |  t d <t |  t d <n  t  j   d  k	 rr t   t d <n  d  S(   Nc         ` s#   t     d d d d d | |  g S(   Ns   -qs   -sDEVICE=png16ms	   -dNOPAUSEs   -dBATCHs   -sOutputFile=(   Rz   (   RG   RH   (   t   gs(    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyRL   ņ   s    t   pdft   epst   svg(   R8   R/   R   RN   t	   converterR0   RT   (   t   gs_vRL   (    (   R   s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   _update_converterļ   s    c           C` s   d g t  t  S(   s_   
    Returns the list of file formats that compare_images can compare
    on this system.

    t   png(   t   listR   (    (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyR     s    c         C` sÆ  |  j  d d  \ } } | t k r d | } d d l m } |   rc d d l } | j |  q d d l m } | |   n  | d | d	 } t j	 j
 |   s¹ t d
 |    n  t j	 j
 |  sš t j |  j t j |   j k  r«| rt   }	 n d }	 |	 d k	 rxt |   }
 t j	 j |  d } t j	 j |	 |
 |  } t j	 j
 |  rxt j | |  | Sn  t | |  |  |	 d k	 r«t j | |  q«n  | S(   s  
    Convert the named file into a png file.  Returns the name of the
    created file.

    If *cache* is True, the result of the conversion is cached in
    `matplotlib._get_cachedir() + '/test_cache/'`.  The caching is based
    on a hash of the exact contents of the input file.  The is no limit
    on the size of the cache, so it may need to be manually cleared
    periodically.

    t   .i   s)   Don't know how to convert %s files to png(   t   is_called_from_pytesti    N(   t   SkipTestt   _s   .pngs   '%s' does not exist(   t   rsplitR   R   R   t   pytestt   skipt   noseR   R   R   R&   R(   t   statt   st_mtimeR-   R   R>   R   R   t   shutilt   copyfile(   t   filenamet   cacheR   t	   extensiont   reasonR   R   R   t   newnameR,   t
   hash_valuet   new_extt   cached_file(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyRM     s8    
	$c         C` s   d d d d |  g S(   Nt   xmllints   --valids   --nowarnings   --noout(    (   R¤   (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyR{   G  s    R   s   2.1c   
      C` s  t  j j |   s% t d |    n  |  j d d  \ } } t j | d  } | d k	 r| |   } t j	 | d t
 d t j d t j } | j   \ } } | j   } | d k rd d	 j |  }	 | rā |	 d
 | 7}	 n  | rł |	 d | 7}	 n  t |	   qn  d S(   s7   Verify the file through some sort of verification tool.s   '%s' does not existR   i   R?   R@   RA   i    s%   File verification command failed:
%s
RB   s   Standard output:
%s
s   Standard error:
%s
N(   R   R   R&   R(   R   t	   verifierst   getR   R   RC   R4   RD   RE   RF   R   (
   R¤   R   R¦   t   verifierRL   RJ   R@   RA   RK   R   (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   verifyK  s"    c   
      C` s»   |  d d !d k r± | d d !d k r± | j  \ } } } | j  \ } } }	 | t | d | d  t | d | d   t | d | d  t | d | d   f } n  | | f S(   Nił’’’iü’’’R   R   i   (   t   shapet   int(
   t   actual_patht   actual_imaget   expected_patht   expected_imaget   awt   aht   adt   ewt   eht   ed(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   crop_to_samea  s    &Lc         C` sW   |  j  | j  k r3 t d j |  j  | j     n  t j |  | j t  d j    S(   sH   Calculate the per-pixel errors, then compute the root mean square error.s;   Image sizes do not match expected size: {0} actual size {1}i   (   R±   R   R   t   npt   sqrtt   astypet   floatt   mean(   t   expectedImaget   actualImage(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   calculate_rmsl  s
    c         C` sH  t  j j |  s% t d |   n  t  j |  j d k rP t d |   n  |  j d  d } t  j j |   s t d |    n  | d k rµ t | t	  } t |  t
  }  n  t j |   } t j |  } | d d  d d  d d	  f } | d d  d d  d d	  f } t | | |  |  \ } } t | d
  } | d k rlt j | |  rld Sn  | j t j  } | j t j  } t | |  } | | k rÆd St |  | |  t d | d t |   d t |  d t |  d |  }	 | sDd d d d d d g }
 d j g  |
 D] } | j |	   ^ q# }	 n  |	 S(   s_  
    Compare two "image" files checking differences within a tolerance.

    The two given filenames may point to files which are convertible to
    PNG via the `.converter` dictionary. The underlying RMS is calculated
    with the `.calculate_rms` function.

    Parameters
    ----------
    expected : str
        The filename of the expected image.
    actual :str
        The filename of the actual image.
    tol : float
        The tolerance (a color value difference, where 255 is the
        maximal difference).  The test fails if the average pixel
        difference is greater than this value.
    in_decorator : bool
        If called from image_comparison decorator, this should be
        True. (default=False)

    Examples
    --------
    img1 = "./baseline/plot.png"
    img2 = "./output/plot.png"
    compare_images( img1, img2, 0.001 ):

    s   Output image %s does not exist.i    s   Output image file %s is empty.R   i’’’’s!   Baseline image %r does not exist.R   Ni   s   failed-diffg        t   rmsR   R   t   difft   tols!   Error: Image files did not match.s   RMS Value: {rms}s   Expected:  
    {expected}s   Actual:    
    {actual}s   Difference:
    {diff}s   Tolerance: 
    {tol}s   
  (   R   R   R&   t	   ExceptionR    t   st_sizet   splitR(   RM   Rg   R4   R   t   read_png_intR½   R   R¾   t   array_equalR   RĄ   t   int16RÅ   t   save_diff_imaget   dictRz   R   R   (   R   R   RČ   t   in_decoratorR¦   RĆ   RÄ   t
   diff_imageRĘ   t   resultsR!   R"   (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyR
   v  sJ    %%!	.c         C` sz  t  j |   } t  j |  } t | | |  |  \ } } t j |  j t  } t j |  j t  } | j | j k r t d j	 | j | j    n  t j
 | |  } | d 9} t j | d d  j t j  } | j \ } } }	 |	 d k rGt j | | d f d t j }
 | |
 d  d   d  d   d d  f <|
 } n  d | d  d   d  d   d f <t  j | |  d  S(	   Ns;   Image sizes do not match expected size: {0} actual size {1}i’   i
   i    i   i   t   dtypeiö	  (   R   t   read_pngR½   R¾   t   arrayRĄ   RĮ   R±   R   R   R   t   clipt   uint8t   emptyt	   write_png(   R   R   t   outputRĆ   RÄ   t   absDiffImaget   save_image_npt   heightt   widtht   deptht
   with_alpha(    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyRĻ   Ķ  s(    
!!%	i   (4   t   __doc__t
   __future__R    R   R   t   sixRV   R]   R1   R`   R   t   reR¢   Rv   t   tempfileR   t   numpyR¾   R8   t   matplotlib.compatR   t   matplotlib.testing.exceptionsR   R   R   R   t   __all__R   R   R	   R-   R>   RN   t   compilet   searchRP   RS   t   objectRT   R   R   R   RM   R­   Rg   t   checkdep_xmllintt
   deprecatedR°   R½   RÅ   R
   RĻ   (    (    (    s9   lib/python2.7/site-packages/matplotlib/testing/compare.pyt   <module>   sP   		+			_				6		
W