resampling image takes a large amount of time
Although my money would be on the interpolation function taking a fair while, here are a few other ideas:

1. Is that resize_factor matrix going to be different for every single image? You could otherwise pre-compute it one for all images of a given size and then just use it, instead of re-computing it for every image.

2. One aspect, which may answer your other related question on DS), is memory usage, that can take time if the arrays are large enough. Those matrix divisions that you perform will return a new matrix, that means a new chunk of memory. If it turns out (from the profiling idea below), that those division operations are costly, you could instead try pre-allocating an array to hold all your results (assuming you know the relaevant shapes), then filling the array with the output.

3. ``scipy.interpolation.zoomreturns anndarray, so you don't need to callnp.arrayon it again in your return statement of theresample` function.



I would suggest profiling your script. This will allow you to see which function calls are called most often and which take the most time. You can then focus on making making changes that will have the biggest effect on the runtime. Here is a nice example tutorial if you're new to profiling in Python.

You can use cProfile, a built-in python module. You can do this in the terminal quite simply (if your script is designed to be run that way) by running:

python -m cProfile -o profiling_results.prof your_script.py


In words (flags shown in bold): python run the module cProfile, to run your_script.py, producing the output file profiling_results.prof.

The generic version of this command it:

python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)


You can open and read the output file in a normal text editor. The suffix is a common convention, because you can pass it to a tool like SnakeViz, which will allow you to visualise the results and introspect the method tree of your script; the order in which functions call each other.



There is a notebook on Kaggle, which you may find interesting, as it performs similar tasks on the same LUNA16 dataset.