ó
±éRc           @   s  d  Z  d d l m 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	 l m Z d d
 l 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 m Z d d d g Z d „  Z d d d „ Z d „  Z d# d „ Z i  Z d „  Z e j  ƒ  \ Z! Z" e" d k	 rd „  Z# e e# ƒ e d <e e# ƒ e d <n  e j$ ƒ  d k	 r­d „  Z# e e# ƒ e d <n  d „  Z% d „  Z& i  Z' d „  Z( e j) ƒ  rðe* rðd „  e' d <n  d „  Z+ d  „  Z, e* d! „ Z- d" „  Z. d S($   s+    A set of utilities for comparing results.
iÿÿÿÿ(   t   divisionN(   t
   subprocess(   t   ImageComparisonFailure(   t
   image_util(   t   _png(   t   _get_cachedir(   t   cbook(   t   version(   t   reducet   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(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   make_test_filename#   s    c         C   sÒ  | d k rA | d k rA d } | d 7} | d 7} t | ƒ ‚ n  d } | d k	 r÷ t |  | ƒ } | | k  r÷ t |  ƒ } t | ƒ } t | ƒ }	 t | ƒ }
 | d 7} | d | d 7} | d | d 7} | d |	 d 7} | d	 |
 d 7} q÷ n  | d k	 rÀt |  | ƒ } |  r,| t |  ƒ } n  | | k  rÀt | ƒ } t | ƒ } t |  ƒ } t | ƒ } | d 7} | d | d 7} | d | d 7} | d
 | d 7} | d | d 7} qÀn  | rÊ| Sd Sd S(   s    Fail if the floating point values are not close enough, with
      the givem message.

   You can specify a relative tolerance, absolute tolerance, or both.
   s4   You haven't specified a 'relTol' relative tolerance s5   or a 'absTol' absolute tolerance function argument.  s   You must specify one.t    s   
s     Expected: s     Actual:   s     Abs Diff: s     Abs Tol:  s     Rel Diff: s     Rel Tol:  N(   t   Nonet
   ValueErrort   abst   str(   t   expectedt   actualt   relTolt   absTolt   exMsgt   msgt   absDifft   expectedStrt	   actualStrt
   absDiffStrt	   absTolStrt   relDifft
   relDiffStrt	   relTolStr(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyR	   +   sF    



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   t   joint   existsR   t   mkdirst   IOErrort   accesst   W_OK(   t   cachedirt	   cache_dir(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   get_cache_dirf   s    	i   i   c         C   sa   t  j ƒ  } t |  d ƒ �: } x0 t rP | j | ƒ } | s@ Pn  | j | ƒ q! WWd  QX| j ƒ  S(   Nt   rb(   t   hashlibt   md5t   opent   Truet   readt   updatet	   hexdigest(   R   t
   block_sizeR4   t   fdt   data(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   get_file_hasht   s    	c            s   ‡  f d †  } | S(   Nc            s»   ˆ  |  | ƒ } t  j | d t  j d t  j ƒ} | j ƒ  \ } } | j ƒ  } t j j | ƒ sg | r· d d j | ƒ } | r‘ | d | 7} n  | r¨ | d | 7} n  t	 | ƒ ‚ n  d  S(   Nt   stdoutt   stderrs   Conversion command failed:
%s
t    s   Standard output:
%s
s   Standard error:
%s
(
   R   t   Popent   PIPEt   communicatet   waitR   R   R*   R)   R,   (   t   oldt   newt   cmdlinet   pipeR>   R?   t   errcodeR   (   t   cmd(    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   convert�   s    !(    (   RJ   RK   (    (   RJ   sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt    make_external_conversion_command€   s    c         C   s   t  d d d d d | |  g S(   Ns   -qs   -sDEVICE=png16ms	   -dNOPAUSEs   -dBATCHs   -sOutputFile=(   t   gs(   RE   RF   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   <lambda>’   s    t   pdft   epsc         C   s   d d |  d | g S(   Nt   inkscapes   -zs   --export-png(    (   RE   RF   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyRN   ™   s    t   svgc           C   s   d g t  j ƒ  S(   sS   Returns the list of file formats that compare_images can compare
   on this system.t   png(   t	   convertert   keys(    (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyR   �   s    c   	      C   sd  |  j  d d ƒ \ } } | t k r7 t d | ƒ ‚ n  | d | d } t j j |  ƒ sn t d |  ƒ ‚ n  t j j | ƒ s¥ t j | ƒ j t j |  ƒ j k  r`| r· t	 ƒ  } n d } | d k	 r-t |  ƒ } t j j | ƒ d } t j j | | | ƒ } t j j | ƒ r-t j | | ƒ | Sn  t | |  | ƒ | d k	 r`t j | | ƒ q`n  | S(   si  
   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/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 pngt   _s   .pngs   '%s' does not existN(   t   rsplitRT   R   R   R   R*   R,   t   statt   st_mtimeR1   R   R=   R   R)   t   shutilt   copyfile(	   t   filenamet   cacheR   t	   extensiont   newnameR0   t   hasht   new_extt   cached_file(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyRK   ¢   s,    
$c   
      C   s	  t  j j |  ƒ s% t d |  ƒ ‚ n  |  j d d ƒ \ } } t j | d ƒ } | d k	 r| |  ƒ } t j	 | 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(   s?   
   Verify the file through some sort of verification tool.
   s   '%s' does not existRV   i   R>   R?   i    s%   File verification command failed:
%s
R@   s   Standard output:
%s
s   Standard error:
%s
N(   R   R   R*   R,   RX   t	   verifierst   getR   R   RA   RB   RC   RD   R)   (
   R]   R   R_   t   verifierRJ   RH   R>   R?   RI   R   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   verifyÍ   s     !c         C   s   d d d d |  g S(   Nt   xmllints   --valids   --nowarnings   --noout(    (   R]   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyRN   ä   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üÿÿÿRP   RO   i   (   t   shapet   int(   t   actual_patht   actual_imaget   expected_patht   expected_imaget   awt   aht   ewt   eh(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   crop_to_sameç   s
    &gc   	      C   sÒ   t  j |  j ƒ } t |  | ƒ } t j d ƒ } t j t  j ƒ } | | k rm t  j | j ƒ  d d ƒ} n" t  j	 | d t  j
 d ƒ ƒd } t  j | t  j
 t | ƒ ƒ d ƒ } t  j t | ƒ | ƒ } | S(   Ns   1.6t	   minlengthi   t   binsi  i    i   (   t   npt   prodRi   R   R   t   LooseVersiont   __version__t   bincountt   ravelt	   histogramt   aranget   sumt   lent   sqrtt   float(	   t   expectedImaget   actualImaget
   num_valuest   abs_diff_imaget   expected_versiont   found_versionR|   t   sum_of_squarest   rms(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   calculate_rmsð   s    "&c      
   C   s(  t  | ƒ |  j d ƒ d } t j j |  ƒ sB t d |  ƒ ‚ n  | d k ro t | t ƒ } t |  t ƒ }  n  t	 j
 |  ƒ } t	 j
 | ƒ } | d d … d d … d d … f } | d d … d d … d d … f } t | | |  | ƒ \ } } | j t j ƒ } | j t j ƒ } t j d ƒ } t j t j ƒ } t | | ƒ }	 t | d ƒ }
 |	 | k r‡t j j |
 ƒ rƒt j |
 ƒ n  d St |  | |
 ƒ | rÔt d	 |	 d
 t |  ƒ d t | ƒ d t |
 ƒ ƒ } | Sd t |	 ƒ d t |  ƒ d t | ƒ d t |
 ƒ d t | ƒ d } | Sd S(   s|  Compare two image files - not the greatest, but fast and good enough.

   = EXAMPLE

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

   = INPUT VARIABLES
   - expected  The filename of the expected image.
   - actual    The filename of the actual image.
   - tol       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 If called from image_comparison decorator, this should be
               True. (default=False)
   RV   iÿÿÿÿs!   Baseline image %r does not exist.RS   Ni   s   1.6s   failed-diffR‰   R   R   t   diffs1     Error: Image files did not match.
  RMS Value: s   
  Expected:
    s   
  Actual:
    s   
  Difference:
    s   
  Tolerance: s   
(   Rg   t   splitR   R   R*   R,   RK   t   FalseR6   R   t   read_png_intRs   t   astypeRv   t   int16R   Rx   Ry   RŠ   R   t   unlinkR   t   save_diff_imaget   dictR   (   R   R   t   tolt   in_decoratorR_   R‚   Rƒ   R†   R‡   R‰   t
   diff_imaget   resultsR   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyR
     s@    
%%Lc         C   s†  t  j |  ƒ } t  j | ƒ } t | | |  | ƒ \ } } t j | ƒ j t j ƒ } t j | ƒ j t j ƒ } | j | j k s‡ t ‚ | j	 | j	 k sŸ t ‚ t
 | | ƒ } | 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 | j ƒ  | | | ƒ d  S(   Niÿ   i
   i    i   i   t   dtypeiö	  (   R   t   read_pngRs   Rv   t   arrayR�   R�   t   ndimt   AssertionErrorRi   R   t   clipt   uint8t   emptyt	   write_pngt   tostring(   R   R   t   outputR‚   Rƒ   t   absDiffImaget   save_image_npt   heightt   widtht   deptht
   with_alpha(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyR’   R  s"    
!!%	i   (/   t   __doc__t
   __future__R    t
   matplotlibt   matplotlib.compatR   t   matplotlib.testing.noseclassesR   t   matplotlib.testingR   R   R   R   t	   distutilsR   R3   t   mathR   t   numpyRv   R[   t   syst	   functoolsR   t   __all__R   R   R	   R1   R=   RT   RL   t   checkdep_ghostscriptRM   t   gs_vRJ   t   checkdep_inkscapeR   RK   Rd   Rg   t   checkdep_xmllintR�   Rs   RŠ   R
   R’   (    (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/matplotlib/testing/compare.pyt   <module>   sV   		;	
					)				N