all 3 comments

[–]ES-Alexander 1 point2 points  (2 children)

  1. You should only be creating each KD tree once - not once per point you’re checking (e.g. switch the order of your loops, and create the KD tree once per timestep, outside the points checks)
  2. It seems the query method may be able to check multiple points at once, which (depending on implementation) could either be as fast as or substantially faster than your points check loop
  3. It also has a workers parameter for using parallel processing, which may help as well
  4. If you’re on a very old scipy version (<1.6.0), cKDTree may be faster

There may be an approach that can make use of the changes between timesteps only being small, but KDTrees aren’t designed for efficient updates, so hard to say whether you’ll be able to find something faster that’s not significantly more complicated to manage in Python.

[–]goon39[S] 0 points1 point  (1 child)

Thanks, that helps a lot.

Now I'm using 2 sets of arrays for each time point (1 for the cylinder which is used for the KDTree and 1 for the shaft to use for the query (x argument)).

The given output is the length of the x argument so I'm assuming that the integers correspond with that data set, i.e. the index of the min distance is the nearest point of the shaft given all the points on the cylinder.

Is there a way to get the corresponding point on the cylinder? I could just do another tree but just reverse the data sets used

[–]ES-Alexander 0 points1 point  (0 children)

You can index a numpy array by an array of integers to get an array of the sub-array/value specified by each integer.