k-wave-python icon indicating copy to clipboard operation
k-wave-python copied to clipboard

Speed-up kwavearray functions

Open faberno opened this issue 1 year ago • 0 comments

Currently the get_array_binary_mask and combine_sensor_data of the kWaveArray are pretty slow for a large number of array elements. They would benefit from

  • a general refactor such that expensive functions are called more efficiently
  • parallized loops, as all element calculations are independent from each other

This (draft) PR implements a few ideas for the refactoring part.

get_array_binary_mask:

  • we first compute the combined integration points of all elements and then hand them to off_grid_points all at once, which saves many calls to off_grid_points and avoids or'ing the final mask

combine_sensor_data

  • added options to input the sensor_mask, so it doesn't have to be calculated a second time

  • added an option to return the grid_weights sparsely. Currently the weights are collected in an array, then matlab_find is used to find the non-zero voxels. Instead off_grid_points can compute the grid_weights in a collections.Counter, so that we don't have to additionally find them in a large array.

  • additional idea: make off_grid_points return a function that can quickly return the weights of an element without all the other stuff that happens in off_grid_points. This could be useful in the element loop of combine_sensor_data, where we have to compute the weights of each element individually.

Regarding parallelization: I think joblib would probably be a good choice, but I don't have a lot of experience with that.

faberno avatar Dec 26 '24 00:12 faberno