-
-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: LinearNDInterpolator >5 times slower than equivalent MATLAB's scatteredInterpolant #19625
Comments
If your data is on a meshgrid you should use scipy.interpolate.RegularGridInterpolator instead |
I agree but
So in another case I would use |
To add to what Jake said above:
Alternatively, if you can use GPUs, you may want to keep an eye on cupy/cupy#7985. |
Yeah that's the issue with closed source... It's also why we're moving our code base to Python. Thanks for letting me know where to find the relevant SciPy sources. I'll try to find some time to look at it, though I'm sure I won't be able to contribute much since you, @j-bowhay and the rest of the SciPy team have already well optimized everything beyond what little lambda users like me can provide. For my short-term use, I'll also look into CuPy as you suggested. Thanks! I'll update this issue if I find anything. |
Describe your issue.
Hi,
I have been updating an old MATLAB source base into a python one but I have come across some difficulty with interpolation.
We simulate a signal (ZZ) on a 2D mesh (XX, YY). We then create a linear interpolator using scattered data to get the following inversion map (x, z) -> y.
Roughly speaking, the ranges of X, Y, and Z are [10, 200], [100, 4095], and [-0.5, 0.5] respectively (important due to rescaling).
While I can retrieve the exact results from our MATLAB test suite by
LinearNDInterpolator
'srescale
parameter toTrue
(MATLAB does this automatically),scatteredInterpolant
extrapolation toFalse
(MATLAB does this by default)Creating the interpolator takes about the same amount of time (< 20 seconds). I found that evaluating the
LinearNDInterpolator
object takes about 5 times longer than evaluating thescatteredInterpolant
object when evaluating the object on my (somewhat large) input dataset.Disclaimer: I'm using the MATLAB software (R2023 Update 5) on Windows (31.7G RAM) and the same version on WSL2 (15.5G RAM), while I run the python code on WSL2 (15.5G RAM). Using the reproduction scripts below on 1 CPU in WSL2, I don't see any significant additional RAM usage during
LinearNDInterpolator
's evaluation (<0.01G RAM). UsingscatteredInterpolant
, I see about 0.20G additional RAM being used during evaluation.The input data, its data type etc. is all the same and I turned off the parallel pool in my MATLAB client to interference.
Still, I get runtimes of under 2 minutes with MATLAB and runtimes of close to 10 minutes with SciPy using a 40+ millions 2D input dataset.
I wondered if this was linked to a recent issue (#19547) but isn't QHull used when creating the Delaunay triangulation? Would it have an impact when evaluating the interpolant once the interpolator is created?
I don't really know what could make it that SciPy's evaluation takes so much longer than that of MATLAB when the rest of the code has been made much faster with Python/NumPy/SciPy, so I thought it may be useful submitting this.
In the meantime, I looked into ways to parallelize the evaluation of the interpolator using Python's
multiprocessing
'sstarmap
on 10 CPUs, taking #8856 into account (2018) and splitting the dataset in 100 batches. However, the RAM usage is perpetually increasing the evaluation of the 40+ millions points had not finished after 26 minutes when it started hitting the swap, so I terminated the script. I had checked the script finished when using a very small input dataset, so I'm not sure what's causing the RAM to increase like. Since it's not happening on the sequential version, I'm considering it may be more of amultiprocessing
issue though, like not freeing everything upon transferring the results to the main process.I also looked into using
Numba
on the interpolator but these functions aren't supported yet. I'm considering looking into thethreading
library next, so I'll update once it's done.Note: in the code block below, I reduced the number of 2D-points to evaluate the interpolant on down to 1 000 000. It took my computer 54 seconds using my WSL2 Python environment and only 4 seconds using my MATLAB environment (R2023a Update 5) in Windows and 3.5 seconds in WSL2. I'd have imagined the MATLAB speedup to be lower than x5 when using smaller datasets but this may be due to using a 1 million 2D points randomly generated dataset rather than our more behaved 40+ millions 2D points physical dataset. I could check tomorrow how the speedup scales using the randomly generated dataset.
Reproducing Code Example
SciPy/NumPy/Python version and system information
The text was updated successfully, but these errors were encountered: