Bottom Page

• 1 Vote(s) - 5 Average
• 1
• 2
• 3
• 4
• 5
 interpolation takes very long time zengwl17206 Unladen Swallow Posts: 2 Threads: 1 Joined: Aug 2018 Reputation: 0 Likes received: 0 #1 Aug-18-2018, 12:52 AM (This post was last modified: Aug-18-2018, 12:57 AM by Larz60+. Edited 2 times in total. Edit Reason: added code tags ) 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 scidam Posts: 677 Threads: 1 Joined: Mar 2018 Reputation: 90 Likes received: 103 #2 Aug-18-2018, 05:37 AM 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... zengwl17206 Unladen Swallow Posts: 2 Threads: 1 Joined: Aug 2018 Reputation: 0 Likes received: 0 #3 Sep-06-2018, 05:21 AM 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 « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)