U
    id                     @   sP   d dl Z d dlZd dlmZ G dd dZG dd dejZedkrLe  dS )    N)captured_stdoutc                   @   s   e Zd ZdZdddZdS )MatplotlibBlockerzBlocks the import of matplotlib, so that doc examples that attempt to
    plot the output don't result in plots popping up and blocking testing.Nc                 C   s   |dkrd}t |d S )NZ
matplotlibz/Blocked import of matplotlib for test suite run)ImportError)selffullnamepathtargetmsg r
   _/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/tests/doc_examples/test_examples.py	find_spec   s    zMatplotlibBlocker.find_spec)N)__name__
__module____qualname____doc__r   r
   r
   r
   r   r   	   s   r   c                       sL   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )DocsExamplesTestc                    s   t  j|| t | _d S N)super__init__r   _mpl_blocker)r   argskwargs	__class__r
   r   r      s    zDocsExamplesTest.__init__c                 C   s   t jd| j d S Nr   )sys	meta_pathinsertr   r   r
   r
   r   setUp   s    zDocsExamplesTest.setUpc                 C   s   t j| j d S r   )r   r   remover   r   r
   r
   r   tearDown   s    zDocsExamplesTest.tearDownc              	      s   t   ddlm} zddlm}m} d}W n tk
rD   d}Y nX dd l}ddlm	} |dddd	  |dd fd
d}|j
d|jd}| }	|dddd|d | }
t|
|	  |r|| |  W 5 Q R X d S )Nr   )default_timer)imshowshowTFjit)nopythonc                 S   sT   d}t | |}d}t|D ]4}|| | }|j|j |j|j  dkr|  S qdS )z
                Given the real and imaginary parts of a complex number,
                determine if it is a candidate for membership in the Mandelbrot
                set given a fixed number of iterations.
                r   y                      )complexrangerealimag)xyZ	max_itersiczr
   r
   r   mandel+   s    

z0DocsExamplesTest.test_mandelbrot.<locals>.mandelc                    s|   |j d }|j d }||  | }|| | }	t|D ]B}
| |
|  }t|D ](}|||	  } |||}||||
f< qLq4|S )Nr      )shaper+   )Zmin_xZmax_xZmin_yZmax_yimageZitersheightwidthZpixel_size_xZpixel_size_yr.   r,   r/   r-   colorr3   r
   r   create_fractal<   s    

z8DocsExamplesTest.test_mandelbrot.<locals>.create_fractal)  i  Zdtypeg       g      ?g         )r   timeitr"   Zmatplotlib.pylabr#   r$   r   numpynumbar&   ZzerosZuint8print)r   timerr#   r$   Zhave_mplnpr&   r;   r6   ser
   r:   r   test_mandelbrot   s*    

z DocsExamplesTest.test_mandelbrotc              	   C   sl   t  \ dd l}ddlm} |dgddd }|jd|jdd	d
}t| t||d W 5 Q R X d S )Nr   )guvectorizez%void(float64[:], intp[:], float64[:])z(n),()->(n)c                 S   sz   |d }d}d}t |D ]$}|| | 7 }|d7 }|| ||< qt |t| D ](}|| | | ||   7 }|| ||< qLd S )Nr   g        r4   )r+   len)aZ
window_arroutZwindow_widthZasumcountr0   r
   r
   r   	move_mean]   s    z7DocsExamplesTest.test_moving_average.<locals>.move_meanr>   r=      
      )r   r@   rA   rH   arangefloat64ZreshaperB   )r   rD   rH   rM   Zarrr
   r
   r   test_moving_averageV   s    
z$DocsExamplesTest.test_moving_averagec              	      s   t   dd l dd lddlm dd lddlm} d}d}fdd}|dd	d	d
 fdd}fdd}fdd}fdd}||}	|||}
j	|}j	|}|d d|||}||d|	|| ||d| |
|| W 5 Q R X d S )Nr   )repeatr%   r(   i@B c                    s     d|  d|  S )z?
                Control function using Numpy.
                 @皙	@)exp)rJ   brD   r
   r   func_np}   s    z,DocsExamplesTest.test_nogil.<locals>.func_npz%void(double[:], double[:], double[:])T)r'   Znogilc                    s8   t t| D ]&} d||  d||   | |< qdS )z6
                Function under test.
                rU   rV   N)r+   rI   rW   )resultrJ   rX   r0   )mathr
   r   inner_func_nb   s    z2DocsExamplesTest.test_nogil.<locals>.inner_func_nbc              	      sl   t |ddd  }| dk	r<|| s<t|| ft dt fdddd	d
d  |S )zM
                Benchmark *func* and print out its runtime.
                r>    )endNz{:>5.0f} msc                      s
    S r   r
   r
   r   funcr   r
   r   <lambda>       z?DocsExamplesTest.test_nogil.<locals>.timefunc.<locals>.<lambda>   rN   )numberrT   r<   )rB   ljustZallcloseAssertionErrorformatmin)correctrE   ra   r   r   res)rD   rT   r`   r   timefunc   s    

  z-DocsExamplesTest.test_nogil.<locals>.timefuncc                    s    fdd}|S )zP
                Run the given function inside a single thread.
                c                     s.   t | d }j|jd} |f|   |S )Nr   r=   )rI   emptyrR   )r   lengthr[   )
inner_funcrD   r
   r   ra      s    zDDocsExamplesTest.test_nogil.<locals>.make_singlethread.<locals>.funcr
   )ro   ra   rY   )ro   r   make_singlethread   s    z6DocsExamplesTest.test_nogil.<locals>.make_singlethreadc                    s    fdd}|S )z
                Run the given function inside *numthreads* threads, splitting
                its arguments into equal-sized chunks.
                c                     s   t  d }j|jd}|f   | d   fddtD }fdd|D }|D ]}|  qf|D ]}|  qx|S )Nr   r=   r4   c                    s    g | ]  fd dD qS )c                    s$   g | ]}|  d     qS )r4   r
   ).0arg)chunklenr0   r
   r   
<listcomp>   s     zeDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mt.<locals>.<listcomp>.<listcomp>r
   )rq   r   rs   )r0   r   rt      s   zZDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mt.<locals>.<listcomp>c                    s   g | ]}j  |d qS ))r   r   )Thread)rq   chunk)ro   	threadingr
   r   rt      s   )rI   rm   rR   r+   startjoin)r   rn   r[   chunksthreadsthread)ro   rD   
numthreadsrx   ru   r   func_mt   s    


zFDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mtr
   )ro   r~   r   )rD   rx   )ro   r~   r   make_multithread   s    z5DocsExamplesTest.test_nogil.<locals>.make_multithreadznumpy (1 thread)znumba (1 thread)znumba (%d threads))
r   r\   rx   r?   rT   r@   rA   r&   randomZrand)r   r&   ZnthreadssizerZ   r]   rl   rp   r   Zfunc_nbZ
func_nb_mtrJ   rX   rj   r
   )r\   rD   rT   rx   r   
test_nogilp   s.    
zDocsExamplesTest.test_nogilc              	   C   s   t  v ddlm}m} dd l}||d d  ||d d  fgddd }|d}||d}| ||j | |d W 5 Q R X d S )	Nr   )rH   int64z
(n),()->()c                 S   s4   d}t | jd D ]}|| | | 7 }q||d< d S r   )r+   r5   )r.   r/   rk   accr0   r
   r
   r   g   s    z:DocsExamplesTest.test_guvectorize_scalar_return.<locals>.grd   rN   r>   )	r   rA   rH   r   r@   rQ   ZassertIsInstanceintegerassertEqual)r   rH   r   rD   r   rJ   r[   r
   r
   r   test_guvectorize_scalar_return   s     


z/DocsExamplesTest.test_guvectorize_scalar_return)r   r   r   r   r   r!   rG   rS   r   r   __classcell__r
   r
   r   r   r      s   7Xr   __main__)	r   ZunittestZnumba.tests.supportr   r   ZTestCaser   r   mainr
   r
   r
   r   <module>   s   
 N