Python Forum
interpolation takes very long time
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
interpolation takes very long time
#1
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
Reply
#2
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...
Reply
#3
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020