o
    8ήc^                     @   sT   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r(e  dS 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 )N
matplotlibz/Blocked import of matplotlib for test suite run)ImportError)selffullnamepathtargetmsg r   M/tmp/pip-target-vg8gfxp4/lib/python/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|i | t | _d S r   )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  s ddlm} zddlm}m} d}W n ty!   d}Y nw 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|
|	  |rn|| |  W d    d S W d    d S 1 syw   Y  d S )Nr   )default_timer)imshowshowTFjit)nopythonc                 S   sT   d}t | |}d}t|D ]}|| | }|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y	max_itersiczr   r   r   mandel+   s   
z0DocsExamplesTest.test_mandelbrot.<locals>.mandelc                    s|   |j d }|j d }||  | }|| | }	t|D ]!}
| |
|  }t|D ]}|||	  } |||}||||
f< q&q|S )Nr      )shaper+   )min_xmax_xmin_ymax_yimageitersheightwidthpixel_size_xpixel_size_yr.   r,   r/   r-   colorr4   r   r   create_fractal<   s   

z8DocsExamplesTest.test_mandelbrot.<locals>.create_fractal)  i  dtypeg       g      ?g         )r   timeitr"   matplotlib.pylabr#   r$   r   numpynumbar&   zerosuint8print)r   timerr#   r$   have_mplnpr&   rC   r;   ser   rB   r   test_mandelbrot   s2   
1"z DocsExamplesTest.test_mandelbrotc                 C   s   t  4 dd l}ddlm} |dgddd }|jd|jdd	d
}t| t||d W d    d S 1 s:w   Y  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 }|| ||< q&d S )Nr   g        r5   )r+   len)a
window_arroutwindow_widthasumcountr1   r   r   r   	move_mean]   s   z7DocsExamplesTest.test_moving_average.<locals>.move_meanrG   rE      
      )r   rJ   rK   rU   arangefloat64reshaperN   )r   rQ   rU   r]   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 d    d S 1 sw   Y  d S )Nr   )repeatr%   r(   i@B c                    s     d|  d|  S )z?
                Control function using Numpy.
                 @皙	@)exp)rW   brQ   r   r   func_np}   s   z,DocsExamplesTest.test_nogil.<locals>.func_npz%void(double[:], double[:], double[:])T)r'   nogilc                    s8   t t| D ]} d||  d||   | |< qdS )z6
                Function under test.
                rg   rh   N)r+   rV   ri   )resultrW   rj   r1   )mathr   r   inner_func_nb   s   $z2DocsExamplesTest.test_nogil.<locals>.inner_func_nbc              	      sp   t |ddd  i }| dur || s J || ft dt fdddd	d
d  |S )zM
                Benchmark *func* and print out its runtime.
                rG    )endNz{:>5.0f} msc                      s    i S r   r   r   r   funcr   r   r   <lambda>   s    z?DocsExamplesTest.test_nogil.<locals>.timefunc.<locals>.<lambda>   r^   )numberrf   rD   )rN   ljustallcloseformatmin)correctrR   rt   r   r   res)rQ   rf   rs   r   timefunc   s   
z-DocsExamplesTest.test_nogil.<locals>.timefuncc                    s    fdd}|S )zP
                Run the given function inside a single thread.
                c                     s0   t | d }j|jd} |g| R   |S )Nr   rE   )rV   emptyrb   )r   lengthrn   )
inner_funcrQ   r   r   rt      s   zDDocsExamplesTest.test_nogil.<locals>.make_singlethread.<locals>.funcr   )r   rt   rk   )r   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 ]}|  q3|D ]}|  q<|S )Nr   rE   r5   c                    s    g | ]  fd dD qS )c                    s$   g | ]}|  d     qS )r5   r   ).0arg)chunklenr1   r   r   
<listcomp>   s   $ zeDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mt.<locals>.<listcomp>.<listcomp>r   )r   r   r   )r1   r   r      s
    
zZDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mt.<locals>.<listcomp>c                    s   g | ]	}j  |d qS ))r	   r   )Thread)r   chunk)r   	threadingr   r   r      s    )rV   r   rb   r+   startjoin)r   r   rn   chunksthreadsthread)r   rQ   
numthreadsr   r   r   func_mt   s   


zFDocsExamplesTest.test_nogil.<locals>.make_multithread.<locals>.func_mtr   )r   r   r   )rQ   r   )r   r   r   make_multithread   s   z5DocsExamplesTest.test_nogil.<locals>.make_multithreadznumpy (1 thread)znumba (1 thread)znumba (%d threads))
r   ro   r   rH   rf   rJ   rK   r&   randomrand)r   r&   nthreadssizerl   rp   r~   r   r   func_nb
func_nb_mtrW   rj   r|   r   )ro   rQ   rf   r   r   
test_nogilp   s0   
"zDocsExamplesTest.test_nogil)
r   r   r   r   r   r!   rT   re   r   __classcell__r   r   r   r   r      s    7r   __main__)	r   unittestnumba.tests.supportr   r   TestCaser   r   mainr   r   r   r   <module>   s   
 7