0

I am trying to perform an interpolation of a certain 3D magnitude ux which is allocated on a 3D meshgrid, given by xux, yux, zux. I want to interpolate ux to a meshgrid that is slightly displaced wrt the previous one, given by xuy, yuy, zuy, using scipy.interpolate.interpn.

I have this problem solved in MATLAB, with the built-in function interpn, and I have solved it in python but I find it to be much slower than its MATLAB counterpart (about 20 times slower). I was wondering why this is happening.

In order to provide an insight of the magnitude of the problem, the shape of the meshgrid is (385,130,130).

MATLAB code (fr is the class where the variables come from):

[xv,yv,zv] = ndgrid(fr.xuy,fr.yuy(1:end),fr.zuy);
tic
uu = interpn(fr.xux(1:end),fr.yux,fr.zux,...
             fr.ux(1:end,:,:),xv,yv,zv);
toc

>> Elapsed time is 0.172114 seconds.

Python code (x is the class where the variables come from):

xv,yv,zv = np.meshgrid(x.xuy,x.yuy,x.zuy, indexing = 'ij')
start_time = timeit.default_timer()

uu = scipy.interpolate.interpn((x.xux,x.yux,x.zux),x.ux, \
            (xv,yv,zv), bounds_error = False, fill_value = None, method = 'linear')

elapsed_time=timeit.default_timer() - start_time
print('Elapsed time:', elapsed_time)

>> Elapsed time: 5.302085509523749

As it can be seen, the elapsed time is much lower for the MATLAB code than for Python. I do not know if this is due to some lack of optimization in the scipy.interpolate function, which I've been looking at also, or it is a consequence of introducing the inputs for interpolation in a non-optimal way. I've tried using the points in which I want to interpolate (xv,yv,zv)in different ways, through a loop also, and this is the best result I can get in terms of time. I have also tried using:

interp = scipy.interpolate.RegularGridInterpolator((x.xux,x.yux,x.zux),x.ux, \
       method='linear', bounds_error=False, fill_value = None)
pts = (xv, yv, zv)
uu  = interp(pts)

instead of scipy,interpolate.interpn, which is the class that is used for this function, but same result as well. Any clue?

Thank you very much in advance

kattgoom
  • 1
  • 2
  • 1
    MATLAB is highly optimized why scipy is not that much... – Ander Biguri Jun 11 '20 at 15:13
  • Thanks for your reply @AnderBiguri. That's what I guess, but I was expecting a more detailed response. – kattgoom Jun 11 '20 at 15:40
  • Anyway @Adriaan has closed the question alluding it is a duplicate, which, as far as I'm concerned, is not. – kattgoom Jun 11 '20 at 15:40
  • 1
    I think the question linked is, in essence, a duplicate of this one. The answer is that the way the code that actually runs under the hood (C/C++/FORTRAN) is written in MATLAB is much more efficient than the way its written in python/scypy. The particulars of that are either impossible to answer (MATLAB is not open source) or simply require so much to explain that perhaps require a book, not an stackoverflow answer. The reason Mathworks can get away with charging very large license fees is because their code is very good, very fast. – Ander Biguri Jun 11 '20 at 15:45
  • As an example, I was testing some code in numpy and MATLAB and it was also these huge differences. I could see MATLAB using my 64 cores, why numpy only uses one. But the aswer to your question is the same as "why does code from Mr A run faster than code from Mr B" ? Well, because Mr A wrote it like that. – Ander Biguri Jun 11 '20 at 15:46
  • Thanks very much, @AnderBiguri. I'd have expected an answer like the one you've provided coming from the person that closed the question. – kattgoom Jun 11 '20 at 15:52
  • 1
    What I provided is a shorter version of what is written in the linked answer...... They have provided a better answer than me! :) Question duplication is not a bad thing, is someone telling you "hey! the answer that you want is here! Lucky you!" – Ander Biguri Jun 11 '20 at 15:57
  • I see. The fact is that the linked question neither says nothing at all about python nor does any comparison with it. From what I have read and learned, it is clear that Matlab has a lot of built-in optimized functions, but python has also its analogous functions through dedicated packages, so they are not unoptimized functions created in 'two minutes' in any case. I can understand that Matlab makes use of LAPACK libraries and so on in a more efficient way than python does, but I do not consider that question to be a fair and precise answer to my question. In any case, thanks again. – kattgoom Jun 11 '20 at 16:28
  • No one says they are written in two minutes ;) The question liked say "multi million dolar company that sells 10K$ licenses to their software can be better for some tings than free software" yup. – Ander Biguri Jun 11 '20 at 16:38
  • The docs for RegularGridInterpolator has a link to its source code. Often the `scipy` code has a python/numpy layer that handles parameters and adjusts data, and then passes the task to compiled (even LAPACK) code for the heavy lifting. I'm not sure if that's the case here or not. – hpaulj Jun 11 '20 at 16:53
  • I see. Thanks both of you for your time :) – kattgoom Jun 12 '20 at 08:14

0 Answers0