o
    hG                  
   @   s  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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mZ d dlmZ d dlmZ e dd ZG d	d
 d
ejZdd Zejjdeedgdgddd Zdd Zej dG dd deZ!g dZ"dd Z#ejde# ejjdeedgdgddd Z$ejde# dd Z%ejdej&dejj'ej()  d d!d"ej&d#ejj'ej*)  d$d!d"gejd%g d&ejjdeedgdgdd'd( Z+ejjdd)e,e-d*d+gdgdd,d- Z.d.d/ Z/ejd0ej&d1ejj'ej0ej1d2  d3d!d"d4gejjded5d6gdgdd7d8 Z2ejd0ej&d1ejj'ej0ej1d2  d3d!d"d4gejjded5d6gdgdd9d: Z3ejj'e4d;du d<d!d=d> Z5ejd?d@dAgdBdC Z6ejdDg dEdFdG Z7dHdI Z8dJdK Z9edLgdMdNdO Z:ejjdeedgdgddPdQ Z;ejjdeedgdgddRdS Z<ejjdeedgdgddTdU Z=dVdW Z>dXdY Z?dS )Z    N)Path)pyplot)	animation)PillowWriter)check_figures_equalc                    s   t  \}}|g g \ |dd |dd  fdd} fdd}tt| d	i }|d
tj	}d|vr<d|d< |d|||d|S )z)Create a simple animation (with options).r   
      c                           g g   fS Nset_data liner   s/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/tests/test_animation.pyinit      zanim.<locals>.initc                    s.   t ddd}t ||  } ||  fS )Nr   r   d   )nplinspacesinr   )ixyr   r   r   animate!   s   zanim.<locals>.animateparamklassframes   )figfunc	init_funcNr   )
pltsubplotsplotset_xlimset_ylimdictgetattrpopr   FuncAnimation)requestr    axr   r   kwargsr   r   r   r   anim   s   r/   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	NullMovieWritera  
    A minimal MovieWriter.  It doesn't actually write anything.
    It just saves the arguments that were given to the setup() and
    grab_frame() methods as attributes, and counts how many times
    grab_frame() is called.

    This class doesn't have an __init__ method with the appropriate
    signature, and it doesn't define an isAvailable() method, so
    it cannot be added to the 'writers' registry.
    c                 G   s"   || _ || _|| _|| _d| _d S )Nr   )r    outfiledpiargs_count)selfr    r1   r2   r3   r   r   r   setup;   s
   
zNullMovieWriter.setupc                 K   s,   ddl m} || || _|  jd7  _d S )Nr   )_validate_grabframe_kwargsr	   )matplotlib.animationr7   savefig_kwargsr4   )r5   r9   r7   r   r   r   
grab_frameB   s   zNullMovieWriter.grab_framec                 C      d S r   r   r5   r   r   r   finishH      zNullMovieWriter.finishN)__name__
__module____qualname____doc__r6   r:   r=   r   r   r   r   r0   /   s
    r0   c                 C   s   dt jd< d}d}tdd}t }| j||||d |jt dks$J |j|ks+J |j|ks2J |j	d	ks9J |
 D ]\}}|j| |ksJJ q=|j| jksSJ d S )
Nautozsavefig.facecolorunused.null2   r   )foo)r2   writerr9   r	   r   )r#   rcParamsr(   r0   saver    figurer1   r2   r3   itemsr9   r4   _save_count)r/   filenamer2   r9   rG   kvr   r   r   test_null_movie_writerL   s   


rP   )r   )indirectc                 C   sf   t  dkrtj  tjdi | } tjt	dd ~ tj  W d    d S 1 s,w   Y  d S )NPyPyzAnimation was deletedmatchr   )
platformpython_implementationr   testingbreak_cyclesr   r+   pytestwarnsWarningr/   r   r   r   test_animation_deleteb   s   
"r]   c                  C   s^   G dd dt j} t }d}d}d}d}dg}| ||||}||| |j|jks-J d S )Nc                   @   s   e Zd Zdd ZdS )z7test_movie_writer_dpi_default.<locals>.DummyMovieWriterc                 S   r;   r   r   r<   r   r   r   _runq   r>   z<test_movie_writer_dpi_default.<locals>.DummyMovieWriter._runN)r?   r@   rA   r^   r   r   r   r   DummyMovieWriterp   s    r_   rD   r   unusedr	   )r   MovieWriterr#   rJ   r6   r2   )r_   r    rM   fpscodecbitrate
extra_argsrG   r   r   r   test_movie_writer_dpi_defaulto   s   rf   nullc                   @   s&   e Zd Z		dddZedd ZdS )RegisteredNullMovieWriterNc                 C   r;   r   r   )r5   rb   rc   rd   re   metadatar   r   r   __init__      z"RegisteredNullMovieWriter.__init__c                 C   s   dS )NTr   )clsr   r   r   isAvailable   rk   z%RegisteredNullMovieWriter.isAvailable)NNNNN)r?   r@   rA   rj   classmethodrm   r   r   r   r   rh      s    	
rh   ))ffmpeg	movie.mp4)ffmpeg_filerp   )imagemagick	movie.gif)imagemagick_filers   )pillowrs   )htmlz
movie.html)rg   z
movie.nullc                  c   s    t D ]J\} }tj| s0tjd|  d}tj| d ||gdV  tj| d t||gdV  qtj|  }t	|dd gD ]}| ||fV  | |t|fV  q<qd S )Nzwriter 'z' not available on this systemmarkssupported_formats)
WRITER_OUTPUTr   writersis_availablerY   markskipr   r   r)   )rG   outputr}   writer_classframe_formatr   r   r   gen_writers   s   

r   zwriter, frame_format, outputc              	   C   s   |d ur	|t jd< tjd	i |}d }d }|dkr#|jd d}d}|   |j|d|d||d W d    ~d S 1 s?w   Y  ~d S )
Nanimation.frame_formatro   g33333%@gQk"@      Y@h264   i  )rb   rG   rd   r2   rc   r   )r#   rH   r   r+   _figset_size_inchesas_cwdrI   )tmpdirrG   r   r   r/   r2   rc   r   r   r   test_save_animation_smoketest   s"   


r   c                 C   s  t j| }|d ur|tjd< t \}}d }d }|dkr%|d d}d}| }	|  V |	|||6 |	  dD ]'}
t	j
td|
d |	jd	i |
t i W d    n1 s]w   Y  q;W d    n1 smw   Y  W d    d S W d    d S 1 sw   Y  d S )
Nr   ro   r   r   r   >   r2   formatbbox_inchesz.grab_frame got an unexpected keyword argument rS   r   )r   r{   r#   rH   r$   r   r   savingr:   rY   raises	TypeErrorobject)r   rG   r   r   WriterClassr    r-   r2   rc   test_writerrN   r   r   r   test_grabframe   s6   



"r   rG   ro   Requires FFMpeg)reasonrw   rr   zRequires ImageMagickz
html, want))noneN)html5z<video width)jshtmlz<script c                 C   s   t  dkrtj  | dkr|dkrtj st	d tj
di |}t| |d | }W d    n1 s<w   Y  |d u ri|d u sKJ tt ~tj  W d    d S 1 sbw   Y  d S ||v soJ d S )NrR   rr   r   r   )animation.writerzanimation.htmlr   )rU   rV   r   rW   rX   r   FFMpegWriterrm   rY   r~   r+   r#   
rc_context_repr_html_rZ   UserWarning)rG   rv   wantr/   r   r   r   test_animation_repr_html   s&   



"r   r   r   )
save_countr   c                 C   s   | j dt d d S )NrD   rG   )rI   r0   r\   r   r   r   test_no_length_frames  s   r   c                  C   s^   t tjjdks
J dtjd< tjdrJ tjdkrdnd} | tjd< tjds-J d S )Nr   not_available_ever_xxxxzanimation.ffmpeg_pathro   win32truewhere)	lenr   r{   _registeredmplrH   r|   sysrU   )binr   r   r   test_movie_writer_registry  s   

r   method_nameto_html5_videor   zanimation writer not installed	to_jshtmlr	   )r   c              	   C   s   | d | % tddi t||   W d    n1 s"w   Y  W d    n1 s1w   Y  t|jdks?J |j\}|jdkrM|jdksOJ d S )NWARNINGzanimation.embed_limitgư>r	   zmatplotlib.animation)		set_levelr   r   r   r)   r   recordsname	levelname)r   caplogr   r/   recordr   r   r   test_embed_limit$  s   


r   c                 C   sV   |   t||   ttt| g ksJ W d    d S 1 s$w   Y  d S r   )r   r)   listr   striterdir)r   r   r/   r   r   r   test_cleanup_temporaries6  s   
"r   z/bin/shzrequires a POSIX OSc              	   C   s   |   I |ddtjd   tt| d}|d t|d t	t
j |d W d   n1 s8w   Y  W d   dS W d   dS 1 sPw   Y  dS )a!  
    Test that we correctly raise a CalledProcessError when ffmpeg fails.

    To do so, mock ffmpeg using a simple executable shell script that
    succeeds when called with no arguments (so that it gets registered by
    `isAvailable`), but fails otherwise, and add it to the $PATH.
    PATHz.:ro   s   #!/bin/sh
[[ $@ -eq 0 ]]
i  z	test.mpegN)r   setenvosenvironr   r   write_byteschmodrY   r   
subprocessCalledProcessErrorrI   )r   monkeypatchr/   exe_pathr   r   r   test_failing_ffmpegC  s   
	
"r   cache_frame_dataFTc           
         s   t  \}}|g g \G dd dt fdd}fdd}g  fdd}d	}tj||||| |d
}t }|jd|d tdksIJ t	j
  D ]}	|	 d u | ks[J qPd S )Nc                   @   s   e Zd ZdS )z2test_funcanimation_cache_frame_data.<locals>.FrameN)r?   r@   rA   r   r   r   r   FrameZ  s    r   c                      r
   r   r   r   r   r   r   r   ^  r   z1test_funcanimation_cache_frame_data.<locals>.initc                    s     | d | d   fS )Nr   r   r   framer   r   r   r   b  s   z4test_funcanimation_cache_frame_data.<locals>.animatec                  3   sP    t dD ] } tddd}tjd} ||d}t| |V  qd S )Nr   r   r   r   )r   r   )ranger   r   randomrandappendweakrefref)_r   r   r   )r   frames_generatedr   r   frames_generatorh  s   z=test_funcanimation_cache_frame_data.<locals>.frames_generatorr   )r"   r   r   r   rD   r   r   )r#   r$   r%   r(   r   r+   r0   rI   r   r   rW   rX   )
r   r    r-   r   r   r   
MAX_FRAMESr/   rG   fr   )r   r   r   r   #test_funcanimation_cache_frame_dataU  s(   

r   return_value)Nstringr	   )r   artistc                    sh   t  \}}|g \  fdd}tt tj||ddd W d    d S 1 s-w   Y  d S )Nc                    s$     ddgd| g dkr S S )Nr   r	   r   r   r   r   r   r   r   r     s   z test_draw_frame.<locals>.animateTF)blitr   )r#   r$   r%   rY   r   RuntimeErrorr   r+   )r   r    r-   r   r   r   r   test_draw_frame  s   	"r   c                 C   s   t  \}}dd }tj||ttdddd}|   |jddd W d    n1 s.w   Y  tj	t
d	d
 |  W d    d S 1 sJw   Y  d S )Nc                 S      g S r   r   r   r   r   r   update  r>   z(test_exhausted_animation.<locals>.updater   Fr   repeatr   test.gifru   r   	exhaustedrS   )r#   r$   r   r+   iterr   r   rI   rY   rZ   r   _startr   r    r-   r   r/   r   r   r   test_exhausted_animation  s   

"r   c                 C   sd   t  \}}dd }tj||g ddd}tjtdd |  W d    d S 1 s+w   Y  d S )Nc                 S   r   r   r   r   r   r   r   r     r>   z%test_no_frame_warning.<locals>.updateFr   r   rS   )r#   r$   r   r+   rY   rZ   r   r   r   r   r   r   test_no_frame_warning  s   
"r   png)
extensionsc                    s   |  }|ddtj  |dd tddtj d|g g \  fdd} fdd	}tj|||d
ddd}| 	  |
d W d    n1 sPw   Y  |  }|ddtj  |dd |td  d S )Nr      r   r	   r   c                      r
   r   r   r   r   r   r   r     r   z"test_animation_frame.<locals>.initc                    s      t| d    fS )Nr   )r   r   r   r   r   r   r   r   r     s   z%test_animation_frame.<locals>.animater   TF)r"   r   r   r   r   g{Gz?)add_subplotr&   r   pir'   r   r%   r   r+   r   rI   r   )r   fig_testfig_refr-   r   r   r/   r   r   r   test_animation_frame  s$   
r   c                 C   s   d}t td}d|dt|d}tjtt|d tj	di i | ||d} W d    n1 s7w   Y  | j
t|ksEJ |   d S )	Nr   r   %You passed in an explicit save_count=z0 which is being ignored in favor of len(frames)=.rS   r   r   r   )r   r   r   rY   rZ   r   reescaper   r+   rL   
_init_drawr/   r   r   match_targetr   r   r   ,test_save_count_override_warnings_has_length  s   r   c                 C   s   d}d}d|dd d|d }t jtt|d tjd
i i | ||d	} W d    n1 s6w   Y  | j|ksBJ |   d S )Nr      r    z#which is being ignored in favor of frames=r   rS   r   r   )	rY   rZ   r   r   r   r   r+   rL   r   r   r   r   r   (test_save_count_override_warnings_scaler  s   

r  c                 C   s   d}t td}d|d|d}tjtt|d tjd	i i | ||d} W d    n1 s5w   Y  | j	du sAJ | 
  d S )
NTr   r   ze which we can infer the length of, did not pass an explicit *save_count* and passed cache_frame_data=z.  To avoid a possibly unbounded cache, frame data caching has been disabled. To suppress this warning either pass `cache_frame_data=False` or `save_count=MAX_FRAMES`.rS   )r   r   Fr   )r   r   rY   rZ   r   r   r   r   r+   _cache_frame_datar   )r/   r   r   r   r   r   r   test_disable_cache_warning  s   r  c                 C   sZ   t jdkrd}nd}tjt|d | jdt d W d    d S 1 s&w   Y  d S )Nr   z,\[WinError 3] .*'\\\\foo\\\\bar\\\\aardvark'z\[Errno 2] .*'/foorS   z+/foo/bar/aardvark/thiscannotreallyexist.mp4r   )r   rU   rY   r   FileNotFoundErrorrI   r   FFMpegFileWriter)r/   	match_strr   r   r   test_movie_writer_invalid_path#  s   
"r  c                  C   s   t  \} }t jdddddd}|| |dd |dd tdd}|j| d	d
d |jdd |j	d }|
 d d dk sEJ t |  dS )zGTest animation exhaustion with transparency using PillowWriter directly)r   r   r	   redg      ?)coloralphar   r   )rb   z
unused.gifr   )r2   T)transparentr         N)r#   r$   	Rectangle	add_patchr&   r'   r   r6   r:   _frames
getextremaclose)r    r-   rectrG   r   r   r   r    test_animation_with_transparency-  s   


r  )@r   pathlibr   rU   r   shutilr   r   r   numpyr   rY   
matplotlibr   r   r#   r   r8   r   matplotlib.testing.decoratorsr   fixturer/   AbstractMovieWriterr0   rP   r}   parametrizer(   r]   rf   r{   registerrh   rz   r   r   r   r   skipifr   rm   ImageMagickWriterr   r   r   r   r   r|   rH   r   r   whichr   r   r   r   r   r   r   r  r  r  r  r   r   r   r   <module>   s    










/


!



