Scipy has many functions that accept a python callable to perform some operation. In particular, I'm working with a mathematical optimization function scipy.optimize.leastsq that accepts a Python callable as objective function argument. This objective function can be called by leastsq lots of times during the minimization process.
My profiling shows that a lot of time is spent on that objective function. I have sped up some parts of the function using Cython. However, the function itself is still a Python function and calling it repeatedly (as leastsq does) has some overhead.
I think I could get a further increase in speed if the function was a Cython function (using cdef instead of def). So I put my call to leastsq inside the Cython extension and pass a Cython objective function to it. But when I do this I get a compile error at the leastsq call:
Cannot convert 'object (object, object, object)' to Python object
Is there any way to pass a Cython function as an argument to these Scipy functions that require python callables?
Or, in my case, is there any way to access the underlying leastsq implementation and pass the Cython objective function to it?