In this recipe, we are going to rewrite the calculate_asa
function, replacing a double-nested loop with NumPy and SciPy functions that have been heavily optimized for performance.
Take a moment and review the original calculate_asa
function in the asa.py
source file. Note the three levels of for
loops present. Copy the asa_np_v1.py
file to a new file named asa_np_v2.py
, and open this in your editor of choice. Again, if you are proficient with the code-versioning tool of your choice, feel free to use it to handle the versioning for you.
The following steps complete this chapter:
First, we heavily modify the
calculate_asa
function as shown in the following code:def calculate_asa(atoms, probe, n_sphere_point=960): """ Returns accessible surface areas for atoms, using the probe and atom radius to define the surface. """ sphere_points = np.array(generate_sphere_points(n_sphere_point)) points = np.array([ [a.pos.x, a.pos.y, a.pos...