Python Forum
interpolation takes very long time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: interpolation takes very long time (/thread-12290.html)



interpolation takes very long time - zengwl17206 - Aug-18-2018

Hello everyone,

I am trying to convert a set of data, which is expressed in polar coordinate, to Cartesian coordinate, so that it can be more easily used in both Paraview and VisIt. However, I notice there’s one step in my code, in which I use scipy.interpolate.griddata method, has been running for really long time. The for loop consumes super long time and overnight no variable has been converted. Here is that part of code.

xcoord = radius*np.sin(theta)*np.cos(phi)
  ycoord = radius*np.sin(theta)*np.sin(phi)
  zcoord = radius*np.cos(theta)
  xmin = -1*max(x1f)
  xmax = max(x1f)
  ymin = -1*max(x1f)
  ymax = max(x1f)
  zmin = -1*max(x1f)
  zmax = max(x1f)
  nx = 200
  ny = 200
  nz = 200
  xgrid = np.linspace(xmin, xmax, nx)
  ygrid = np.linspace(ymin, ymax, ny)
  zgrid = np.linspace(zmin, zmax, nz)
  xmesh, ymesh, zmesh = np.meshgrid(xgrid, ygrid, zgrid)
  print('start to convert to cartesian coordinate')
  for var_key in var_keys:
      vars()[var_key + '_cart'] = griddata(np.c_[xcoord, ycoord, zcoord], vars()[var_key + '_1D'], (xmesh, ymesh, zmesh), method='nearest')
Does anyone have idea why? Thank you for reading my question.

–Yunlin


RE: interpolation takes very long time - scidam - Aug-18-2018

What length of the var_keys array?!
griddata should be fast, but you are trying to interpolate 8M points..!

Access to local variables via vars(), locals() is slower than direct access to them:
> python -m timeit -s "dct={'val': None}" "dct['val']=23"
Output:
0.0532 mu.sec per loop
> python -m timeit -s "val=0" "val=23"
Output:
0.023 mu.sec per loop
> python -m timeit -s "val=0" "vars()['val']=23"
Output:
0.325 mu.sec per loop
Even if we will not consider using vars()/locals() in terms of efficiency (it isn't efficient),
using it in such context is still bad practice: 1) it pollutes local environment with
new variables 2) it makes code harder to analyze (it might be not easy to imagine what variables exists)
and finally...


RE: interpolation takes very long time - zengwl17206 - Sep-06-2018

Thank you very much for reply. I'm sorry I did not notice your reply until right now. So len(vars_keys)=8, and it takes about 6 hours to run my whole code. Also, thank you very much for the suggestion to improving the efficiency of code. It is really helpful to me!

--Yunlin