vaipatel / morphops

Geometric Morphometrics in Python
https://morphops.readthedocs.io/en/latest/
MIT License
27 stars 4 forks source link

speed up distance_matrix #10

Closed schlegelp closed 3 years ago

schlegelp commented 3 years ago

Hi!

First off: thanks for this library, I'm finding it very useful! I've been using your implementation of thin plate spine transforms and was looking into ways to speed it up. In my case, the bottle neck appears to be lmk_util.distance_matrix() which is called in K_matrix(). Looking at distance_matrix(), I was wondering if you could just use scipy.spatial.distance.cdist instead?

def distance_matrix(X,Y):
    """For (p1,k)-shaped X and (p2,k)-shaped Y, returns the (p1,p2) matrix 
    where the element at [i,j] is the distance between X[i,:] and Y[j,:].
    """
    return script.spatial.distance.cdist(X, Y)

Benchmarking this with X (16840, 3) and Y (26, 3), I get ~10s with the current and ~10ms with a scipy implementation of distance_matrix.

Best, Philipp

vaipatel commented 3 years ago

Hi there, apologies for the delay.. and thanks for looking into that! I'd left that nested loop in there for too long! It uses cdist now.. Thanks again for that suggestion.