U
    ,d^                     @   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
   J/tmp/pip-unpacked-wheel-eu7e0c37/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                       sD   e Zd Z f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r2   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%   zerosZuint8print)r   Ztimerr"   r#   Zhave_mplnpr%   r:   r5   ser
   r9   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        r3   )r*   len)aZ
window_arroutZwindow_widthZasumcountr/   r
   r
   r   	move_mean]   s    z7DocsExamplesTest.test_moving_average.<locals>.move_meanr=   r<      
      )r   r?   r@   rG   Zarangefloat64ZreshaperB   )r   rC   rG   rL   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)rI   brC   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.
                rS   rT   N)r*   rH   rU   )resultrI   rV   r/   )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>   rM   )numberrR   r;   )rB   ljustZallcloseAssertionErrorformatmin)correctrD   r_   r   r   res)rC   rR   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<   )rH   emptyrP   )r   lengthrY   )
inner_funcrC   r
   r   r_      s    zDDocsExamplesTest.test_nogil.<locals>.make_singlethread.<locals>.funcr
   )rm   r_   rW   )rm   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<   r3   c                    s    g | ]  fd dD qS )c                    s$   g | ]}|  d     qS )r3   r
   ).0arg)chunklenr/   r
   r   
<listcomp>   s     zeDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mt.<locals>.<listcomp>.<listcomp>r
   )ro   r   rq   )r/   r   rr      s   zZDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mt.<locals>.<listcomp>c                    s   g | ]}j  |d qS ))r   r   )Thread)ro   chunk)rm   	threadingr
   r   rr      s   )rH   rk   rP   r*   startjoin)r   rl   rY   chunksthreadsthread)rm   rC   
numthreadsrv   rs   r   func_mt   s    


zFDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mtr
   )rm   r|   r}   )rC   rv   )rm   r|   r   make_multithread   s    z5DocsExamplesTest.test_nogil.<locals>.make_multithreadznumpy (1 thread)znumba (1 thread)znumba (%d threads))
r   rZ   rv   r>   rR   r?   r@   r%   randomZrand)r   r%   ZnthreadssizerX   r[   rj   rn   r~   Zfunc_nbZ
func_nb_mtrI   rV   rh   r
   )rZ   rC   rR   rv   r   
test_nogilp   s.    
zDocsExamplesTest.test_nogil)
r   r   r   r   r   r    rF   rQ   r   __classcell__r
   r
   r   r   r      s   7r   __main__)	r   ZunittestZnumba.tests.supportr   r   ZTestCaser   r   mainr
   r
   r
   r   <module>   s   
 7