a
    ضaF                     @   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	Z	ddl
Z
ddlZddlmZm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 eeZg dZd	d
 Zdd Zdd Zd0ddZ e!dj"Z#dd Z$G dd de%Z&G dd dZ'G dd de'Z(G dd de'Z)dd Z*i Z+e*  dd  Z,d!d" Z-d#d$ Z.e/ d%d& Z0d'd( Z1d)d* Z2d1d,d-Z3d.d/ Z4dS )2z(
Utilities for comparing image results.
    N)Path)TemporaryDirectoryTemporaryFile)Image)cbook)ImageComparisonFailure)calculate_rmscomparable_formatscompare_imagesc                 C   s   t j| \}}d|||f S )zQ
    Make a new filename by inserting *purpose* before the file's extension.
    z%s-%s%s)ospathsplitext)fnamepurposebaseext r   j/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/matplotlib/testing/compare.pymake_test_filename   s    r   c                  C   s    t t d} | jddd | S )NZ
test_cacheT)parentsexist_ok)r   mplZget_cachedirmkdir)	cache_dirr   r   r   _get_cache_path&   s    r   c                   C   s
   t t S N)strr   r   r   r   r   get_cache_dir,   s    r      c                 C   s   t  }t| d,}||}|s$q0|| qW d    n1 sD0    Y  t| jdkrz|tt	dj
d n*t| jdkr|tt	dj
d | S )Nrbz.pdfgszutf-8z.svginkscape)hashlibmd5openreadupdater   suffixr   r   _get_executable_infoversionencode	hexdigest)r   
block_sizer#   fddatar   r   r   get_file_hash0   s    
*r/   s   [^a-zA-Z0-9_@%+=:,./-]c                 C   s$   t | d u r| S d| dd d S )N   's   '"'"')_find_unsafe_bytesreplace)br   r   r   _shlex_quote_bytesG   s    r4   c                   @   s   e Zd ZdS )_ConverterErrorN)__name__
__module____qualname__r   r   r   r   r5   L   s   r5   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )
_Converterc                 C   s   d | _ t| j d S r   )_procatexitregister__del__selfr   r   r   __init__Q   s    z_Converter.__init__c                 C   sN   | j rJ| j   | j   td | j j| j j| j jgD ]}|  q6d | _ d S r   )r:   killwaitfilterstdinstdoutstderrclose)r?   streamr   r   r   r=   Y   s    




z_Converter.__del__c                 C   s>   t  }| jjd}|st|| ||rt|S qdS )z!Read until the prompt is reached.   N)	bytearrayr:   rE   r%   r5   extendendswithbytes)r?   
terminatorbufcr   r   r   _read_untilc   s    

z_Converter._read_untilN)r6   r7   r8   r@   r=   rQ   r   r   r   r   r9   P   s   
r9   c                   @   s   e Zd Zdd ZdS )_GSConverterc              
   C   s  | j sjtjtdjdddgtjtjd| _ z| d W n. tyh } zt	d|W Y d }~n
d }~0 0 dd	 }| j j
d
|| d || d  | j j
  | d}|dr| dnd}|stj|s|rt|d d nd}| j j
d|  t|| t dd S )Nr    z	-dNOSAFERz	-dNOPAUSEz-sDEVICE=png16m)rD   rE   s   
GSzFailed to start Ghostscriptc                 S   s"   t | ddddddS )N   \s   \\   (s   \(   )s   \))r   fsencoder2   )namer   r   r   encode_and_escape|   s    
z0_GSConverter.__call__.<locals>.encode_and_escapes   << /OutputFile (s   ) >> setpagedevice (s   ) run flush
)   GS<s   GS>rY      >    r   s   pop
r2   )r:   
subprocessPopenr   r(   
executablePIPErQ   r5   OSErrorrD   writeflushrL   r   r   existsintr   decodesysgetfilesystemencoding)r?   origdesterrrX   stack
stack_sizer   r   r   __call__p   s@    
 
z_GSConverter.__call__N)r6   r7   r8   rn   r   r   r   r   rR   o   s   rR   c                       s$   e Zd Zdd Z fddZ  ZS )_SVGConverterc           
   
   C   s  t djjdk }|rdnd}t| ds@t | _t| j| j	 | j
rT| j
 d uri tjdtjd}t }tj|rg dndd	gtjtj||| jjd
| _
|| j
_z| | W n. ty } ztd|W Y d }~n
d }~0 0 t| jjtd}t| jjtd}	z|t|  W n  tyD   t|| Y n0 | j
j|rXdnd | j
j   z| | W nR ty } z8| j
j!d t"| j
j# $t%& d|W Y d }~n
d }~0 0 t'| t(|	| d S )Nr!   rI   s   
>s   > _tmpdir )ZDISPLAYZINKSCAPE_PROFILE_DIR)r!   z--without-gui--shellrr   )rD   rE   rF   envcwdz,Failed to start Inkscape in interactive modes   f.svgs   f.pngs   f.svg --export-png=f.png
s;   file-open:f.svg;export-filename:f.png;export-do;file-close
r   r2   ))r   r(   r)   majorhasattrr   rp   weakreffinalizer=   r:   pollr   environdevnullr   r]   r^   r`   rW   rF   rQ   r5   ra   r   fsdecode
symlink_toresolveshutilcopyfilerD   rb   rc   seekr   r%   rf   rg   rh   removemove)
r?   ri   rj   Zold_inkscaperN   rs   rF   rk   Zinkscape_origZinkscape_destr   r   r   rn      sh    



z_SVGConverter.__call__c                    s"   t    t| dr| j  d S )Nrp   )superr=   rv   rp   cleanupr>   	__class__r   r   r=      s    

z_SVGConverter.__del__)r6   r7   r8   rn   r=   __classcell__r   r   r   r   ro      s   Aro   c                   C   sh   zt d W n t jy"   Y n0 t  td< td< zt d W n t jyX   Y n0 t td< d S )Nr    pdfepsr!   svg)r   r(   ZExecutableNotFoundErrorrR   	converterro   r   r   r   r   _update_converter   s    r   c                   C   s
   dgt S )z
    Return the list of file formats that `.compare_images` can compare
    on this system.

    Returns
    -------
    list of str
        E.g. ``['png', 'pdf', 'svg', 'eps']``.

    png)r   r   r   r   r   r	      s    r	   c                 C   s4  t | }| st| d|jdd tvrLddl}|d|j d |j|j d|jdd  d }| r|	 j
|	 j
k r,|rt nd}|durt  t|}|||j  }| rtd	|  t|| t|S td
|  t|jdd  || |dur,td|  t|| t|S )a  
    Convert the named file to png; return 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.  Old cache entries are
    automatically deleted as needed to keep the size of the cache capped to
    twice the size of all baseline images.
    z does not existrI   Nr   zDon't know how to convert z files to png_z.pngz"For %s: reusing cached conversion.zFor %s: converting to png.z"For %s: caching conversion result.)r   rd   IOErrorr'   r   pytestskipparentstemstatst_mtimer   '_register_conversion_cache_cleaner_oncer/   _logdebugr   r   r   )filenamecacher   r   newpathr   Z
hash_valueZcached_pathr   r   r   convert  s.    
"
r   c                     s   t dd ttjjdD } d|  }tt z dd t dD  t dd  	 D }t
  fd	d
dd}||kr| }| | j8 }|  qxW d    n1 s0    Y  d S )Nc                 s   s   | ]}|  jV  qd S r   )r   st_size.0r   r   r   r   	<genexpr>.  s   z*_clean_conversion_cache.<locals>.<genexpr>z**/baseline_images/**/*   c                 S   s   i | ]}||  qS r   )r   r   r   r   r   
<dictcomp>6  s   z+_clean_conversion_cache.<locals>.<dictcomp>*c                 s   s   | ]}|j V  qd S r   )r   )r   r   r   r   r   r   8  r[   c                    s
    |  j S r   )st_atime)r   Z
cache_statr   r   <lambda>:  r[   z)_clean_conversion_cache.<locals>.<lambda>T)keyreverse)sumr   r   __file__r   globr   Z
_lock_pathr   valuessortedpopr   unlink)Zbaseline_images_sizeZmax_cache_sizeZ
cache_sizeZpaths_by_atimer   r   r   r   _clean_conversion_cache+  s"    
r   c                   C   s   t t d S r   )r;   r<   r   r   r   r   r   r   B  s    r   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 }||fS )Nir   r   r   )shapere   )
Zactual_pathactual_imageZexpected_pathexpected_imageZawZahadewehZedr   r   r   crop_to_sameG  s     &r   c                 C   s<   | j |j kr td| j |j t| | td  S )zR
    Calculate the per-pixel errors, then compute the root mean square error.
    9Image sizes do not match expected size: {} actual size {}r   )r   r   formatnpsqrtastypefloatmean)r   r   r   r   r   r   R  s    r   Fc           
         sr  t |}t j|s"td| t |jdkr>td| t | } t j| s`td|  | dd }|dkrt	|dd	}t	| dd	} t
t| 	d
}t
t|	d
}t||| |\}}t|d}|dkrt
||rdS |t
j}|t
j}t||}||krdS t| || t|t| t|t||d |sng d}	d fdd|	D   S )a+  
    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
        Determines the output format. If called from image_comparison
        decorator, this should be True. (default=False)

    Returns
    -------
    None or dict or str
        Return *None* if the images are equal within the given tolerance.

        If the images differ, the return value depends on  *in_decorator*.
        If *in_decorator* is true, a dict with the following entries is
        returned:

        - *rms*: The RMS of the image difference.
        - *expected*: The filename of the expected image.
        - *actual*: The filename of the actual image.
        - *diff_image*: The filename of the difference image.
        - *tol*: The comparison tolerance.

        Otherwise, a human-readable multi-line string representation of this
        information is returned.

    Examples
    --------
    ::

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

    zOutput image %s does not exist.r   zOutput image file %s is empty.z!Baseline image %r does not exist..r\   r   T)r   RGBzfailed-diffN)rmsexpectedactualdifftol)z!Error: Image files did not match.zRMS Value: {rms}zExpected:  
    {expected}zActual:    
    {actual}zDifference:
    {diff}zTolerance: 
    {tol}z
  c                    s   g | ]}|j f i  qS r   r   )r   lineresultsr   r   
<listcomp>  r[   z"compare_images.<locals>.<listcomp>)r   fspathr   rd   	Exceptionr   r   r   splitr   r   asarrayr   r$   r   r   Zarray_equalr   Zint16r   save_diff_imagedictr   join)
r   r   r   Zin_decorator	extensionr   r   Z
diff_imager   templater   r   r   r
   b  sD    1





r
   c                 C   s   t t| d}t t|d}t||| |\}}t |t}t |t}|j	|j	kr~t
d|j	|j	t || }|d9 }t |ddt j}|j	\}}}	|	dkrt j||dft jd}
||
d	d	d	d	ddf< |
}d|d	d	d	d	df< t|j|d
d d	S )z
    Parameters
    ----------
    expected : str
        File path of expected image.
    actual : str
        File path of actual image.
    output : str
        File path to save difference image to.
    r   r   i	  r            )ZdtypeNr   r   )r   r   r   r$   r   r   arrayr   r   r   r   r   absZclipZuint8emptyZ	fromarraysave)r   r   outputr   r   Zabs_diff_imageZsave_image_npheightwidthdepthZ
with_alphar   r   r   r     s.    r   )r   )F)5__doc__r;   	functoolsr"   loggingr   pathlibr   rer   r]   rg   tempfiler   r   rw   Znumpyr   ZPILr   Z
matplotlibr   r   Zmatplotlib.testing.exceptionsr   	getLoggerr6   r   __all__r   r   r   r/   compilesearchr1   r4   r   r5   r9   rR   ro   r   r   r	   r   r   	lru_cacher   r   r   r
   r   r   r   r   r   <module>   sR   

%H*

h