How can I implement tangent distance for k-nearest neighbor in python/scikit-learn? My ultimate aim is to have a function which I can feed into scikit-learn's NearestNeighbor class as a custom metric parameter.

Existing packages

I have been researching existing libraries for a while. 

The only thing I found was this KMeans package, for python 2 and based on implementing a C library.  I could neither load it in with ctypes nor convert it into an executable with gcc.

I also found this other C code and this Matlab script but with similar results.

Implementation

I also looked into a few papers, to see if I can implement it by myself. For instance, based on this I understand that the main thing I need to do is to calculate the tangent matrix. But, I do not understand


how do I define $s(p, \alpha)$ and especially
how do I calculate the derivatives in python.


I would be glad for any help, comment, whatsoever.

Update

As suggested, I raised the following related issues/requests:


ComeOnGetMe
Scikit-learn


Update 2

@ComeOnGetMe rewrote his code so it can be used along the scikit-learn specifications (example code). Many thanks for that! Nonetheless, when I tried to use it in scikit-learn it underperformed and was very slow, so there is further work needed with that.

Since then I also found a more detailed explanation for code implementation, although based on the C code already mentioned.
ComOnGetMe's KMeans tangent distance metric looks good, if it didn't work for you initially you should fork it and work on the code. I would contact him directly, he probably would have insight for you. Scikit-learn doesn't have a distance metric for tangent distances but the documentation states you can call a user-defined distance (all be it with overhead). 

Contributing code might be a good direction to go if you would want to contact scikit-learn's developers and there are a couple of researchers with published papers implementing tangent distance as a metric, so you could have luck there too.