apps130106_iotorus_performance.ionc_getdensityΒΆ

import time
import numpy as np
from pyana.pep import iotorus


def get_densities_via_function(xyzcoords):
    ''' Get the denisty via the get_density method.

    :param xyzcoords: (N, 3) numpy array.
    :returns: stat, vals
    '''

    torus = iotorus.TwoPeakModel()
    val = []

    t0 = time.time()

    x = xyzcoords[:, 0]
    y = xyzcoords[:, 1]
    z = xyzcoords[:, 2]
    n = torus.get_density(x, y, z)

    t1 = time.time()
    
    return t1 - t0, None


def get_density_via_function(xyzcoords):
    ''' Get the denisty via the get_density method.

    :param xyzcoords: (N, 3) numpy array.
    :returns: stat, vals
    '''

    torus = iotorus.TwoPeakModel()
    val = []

    t0 = time.time()

    for xyz in xyzcoords:
        x, y, z = xyz
#        print x, y, z,
        try:
            n = torus.get_density(x, y, z)
        except ValueError as e:
            n = 0.
#        print n
        val.append(n)

    t1 = time.time()
    
    return t1 - t0, None


def main():
    '''Main script'''

    nsample = 10000   # Call times.

    xyzcoords = np.random.random([nsample, 3]) * 30. - 15.
    # Domains are -15 to 15.

    stat0, vals0 = get_density_via_function(xyzcoords)
    print('get_density method took %f sec / %d call' % (stat0, nsample))

    stat1, vals1 = get_densities_via_function(xyzcoords)
    print('get_density method (multiple) took %f sec / %d call' % (stat1, nsample))


#    stat1, vals1 = get_density_via_interpolation(xyzcoords)

    

if __name__ == "__main__":
    main()