In the last post we explained how to make a little more complex calculations with gpu.js. But, how efficient is?
The temperature calculation is a task I did many years ago, with pure python. Using pure python is a really bad idea in this case, having tools like numpy, cython, etc. The times were about 50 seconds or more, while gpu.js lasts about 1.5 seconds! More than an order of magnitude.
I made an example script to test the timing. The result should be the same as in gpu.js, but I made the residuals interpolation calculations in different alternatives, two of them may be different.
Note that the residuals are created applying the regression to the original data:
residuals = regr.predict(predictors) - temps
It’s a clean and fast way to do it and allows to access the results later in the script.
Applying the regression
Applying the regression results is easy with numpy, since it’s just adding several matrices:
Interpolating the residuals
Interpolating the residuals can be done in several ways. I’ve tested three, two after looking example around and the original I used both at my workplace and in the gpu.js example.
The radial basis function is the one most srecommended by scipy. The results can be a bit strange and the performance is poor, but:
The code, basically prepares the data for the Rbf function.
The inverse of the distance weighted code is taken from a GitHub repo. It’s really efficient and the result is good, but more difficult to understand than the regular inverse of the distance. Also, maintains steep changes, which is not the best situation in our case, where we want a smooth residuals field all around, even if a single station has a different local value:
Again, the code is basically preparing the data for the function.
Inverse of the distance using cython
This is the original code I used, and the one in the previous post. Calculating it with pure numpy was a bit difficult, so I made the original algorithm optimized with cython, so it’s as fast as coded in C. The code to call it is:
Note that I used geotransform, which turns things properly.
The cython code is:
You have to run
python setup.py build_ext --inplace
to compile it before running the script for the first time.
In my computer, which is not a new or powerful one, the times were, for the common steps:
Temperature field time
Final field time
With the different methods, the times were:
Residuals field time
So, in the first place, the residuals interpolation is, by far, the most expensive step. The IDW method I found is the fastest option, although I’m not sure that the result is as good as the cython method with the classical inverse of the distance.
The original gpu.js method lasted:
Multiple linear regression
Calculate the regression field
Calculate the residuals field
Calculate the final field
Draw the regression field
Draw residuals field
Draw final result
So it’s a really good performance if you think that it’s run on the browser using a non compiled language (although using the GPU, of course!)
Finally, it would be nice to check the performance against python + GPU, but I have never worked with it.