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