B
    {d2                 @   s   d Z ddlZddlmZ yddlZW n ek
r<   dZY nX dZd"d	d
Zdd Z	dd Z
d#ddZd$ddZd%ddZd&ddZd'ddZd(ddZd)ddZdd Zd*dd Zed!kre  dS )+ab  Benchmarks for this package.

To run all benchmarks and print a report to the console::

    python -m healpix.bench

You can also run the benchmarks first, save the results dict
to disk as a JSON file (or share it with others) and then
print the results later, or compare them with other results.

We should now that this is not very comprehensive / flexible.

If your application depends on performance of HEALPix computations,
you should write benchmarks with cases relevant for that application
and check if HEALPix computations are really the bottleneck and if
this package is fast enough for you or not.
    N)TableFTpass   皙?c             C   s>   t | |}t||\}}|j|d |d}t||g | S )N   )repeatnumber)timeitTimer
autoscalerr   min)stmtsetupr   mintimetimerr   Ztime1Ztime2 r   b/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy_healpix/bench.py
autotimeit#   s    r   c             C   sD   d}x2t dD ]&}| |}||kr,||fS |d9 }qW tdd S )Nr      
   zfunction is too fast to test)ranger	   RuntimeError)r   r   r   itimer   r   r   r   +   s    
r   c             C   s    | dkrd| S d| S d S )Nastropy_healpixz#from astropy_healpix.healpy import zfrom healpy import r   )packagefunctionr   r   r   
get_import5   s    
r   c             C   sJ   d t|ddd| d|  dd| g}d}t||d	|rBd
nddS )N
pix2angzimport numpy as npznside=zipix=(np.random.random(z%) * 12 * nside ** 2).astype(np.int64)znest=zpix2ang(nside, ipix, nest)r   r   g?)r   r   r   r   )joinr   r   )sizensidenestr   fastr   r   r   r   r   bench_pix2ang<   s    
r%   c          	   C   sT   d t|ddd| d|  dd|  dd	| g}d
}t||d|rLdnddS )Nr   ang2pixzimport numpy as npznside=zlon=360 * np.random.random()zlat=180 * np.random.random(z) - 90znest=z+ang2pix(nside, lon, lat, nest, lonlat=True)r   r   g?)r   r   r   r   )r    r   r   )r!   r"   r#   r   r$   r   r   r   r   r   bench_ang2pixI   s    

r(   c             C   sB   d t|ddd| d|  dg}d}t||d|r:d	nd
dS )Nr   	nest2ringzimport numpy as npznside=zipix=(np.random.random(z%) * 12 * nside ** 2).astype(np.int64)znest2ring(nside, ipix)r   r   g?)r   r   r   r   )r    r   r   )r!   r"   r   r$   r   r   r   r   r   bench_nest2ringW   s    r*   c             C   sB   d t|ddd| d|  dg}d}t||d|r:d	nd
dS )Nr   	ring2nestzimport numpy as npznside=zipix=(np.random.random(z%) * 12 * nside ** 2).astype(np.int64)zring2nest(nside, ipix)r   r   g?)r   r   r   r   )r    r   r   )r!   r"   r   r$   r   r   r   r   r   bench_ring2nestc   s    r,   c          	   C   sT   d t|ddd| d|  dd|  dd	| g}d
}t||d|rLdnddS )Nr   get_interp_weightszimport numpy as npznside=zlon=360 * np.random.random(r'   zlat=180 * np.random.random(z) - 90znest=z;get_interp_weights(nside, lon, lat, nest=nest, lonlat=True)r   r   g?)r   r   r   r   )r    r   r   )r!   r"   r#   r   r$   r   r   r   r   r   bench_get_interp_weightso   s    

r.   c             K   sL   |f d|d|}t f | |d|}trHtf d|d|}||d< |S )Nr   )r   r$   )r   	time_selfhealpytime_healpy)dictHEALPY_INSTALLEDr(   )nameZ	benchmarkr$   kwargsr/   Zresults_singler1   r   r   r   
run_single}   s    r6   c             C   s\  g }| rdddg}n
dddg}xBdD ]:}x4|D ],}x&dD ]}| tdt| |||d q8W q.W q$W xBdD ]:}x4|D ],}x&dD ]}| td	t| |||d q|W qrW qhW x2|D ]*}x$dD ]}| td
t| ||d qW qW x2|D ]*}x$dD ]}| tdt| ||d qW qW xHdD ]@}x8|D ]0}x(dD ] }| tdt| |||d q(W qW qW |S )z-Run all benchmarks. Return results as a dict.r   i  i i@B )TF)r      r   )r$   r!   r"   r#   r&   r)   )r$   r!   r"   r+   r-   )appendr6   r%   r(   r*   r,   r.   )r$   resultsZSIZESr#   r!   r"   r   r   r   	bench_run   s:    














r:   c          	   C   s   t dddddddgdttttttgd	d
}x| D ]}|| q0W d|d _trx|d |d  |d< d|d _d|d _|jdd dS )z:Print a report for given benchmark results to the console.r   r#   r"   r!   r1   r/   ratioZS20T)namesZdtypeZmaskedz10.7fz7.2f)	max_linesN)r   boolintfloatadd_rowformatr3   pprint)r9   tablerowr   r   r   bench_report   s    




rG   c             C   s   t d t| d}t| dS )z3Run all benchmarks and print report to the console.zRunning benchmarks...
)r$   N)printr:   rG   )r$   r9   r   r   r   main   s    
rI   __main__)r   r   r   )NNNNF)NNNNF)NNNF)NNNF)NNNNF)F)F)F)__doc__r	   Zastropy.tabler   r0   hpImportErrorr3   r   r   r   r%   r(   r*   r,   r.   r6   r:   rG   rI   __name__r   r   r   r   <module>   s*   









)
