Reputation: 572
Given a 4D array that represents a discrete coordinate transformation function such that
arr[x_in, y_in, z_in] = [x_out, y_out, z_out]
I would like to interpolate arr
to a grid with more elements (assuming that the samples in arr
were initially drawn from a regularly spaced grid of the higher-element cube).
I have tried the RegularGridInterpolator
from scipy, however this has been rather slow:
import numpy as np
from scipy.interpolate import RegularGridInterpolator
from time import time
target_size = 32
reduced_size = 5
small_shape = (reduced_size,reduced_size,reduced_size,3)
cube_small = np.random.randint(target_size, size=small_shape, dtype=np.uint8)
igrid = 3*[np.linspace(0, target_size-1, reduced_size)]
large_shape = (target_size, target_size, target_size,3)
cube_large = np.empty(large_shape)
t0 = time()
interpol = RegularGridInterpolator(igrid, cube_small)
for x in np.arange(target_size):
for y in np.arange(target_size):
for z in np.arange(target_size):
cube_large[x,y,z] = interpol([x,y,z])
print(time()-t0)
Are there any algorithms that come to mind that would be better suited for the task? Maybe there is something that could exploit the fact that in this case, I am only interested in the discrete integer values at each point.
Upvotes: 5
Views: 598
Reputation: 572
Indeed passing the grid as a whole, as suggested by
Crivella seems to be much more effective. I am adding a numpy only version which seems to be about 2 times faster than itertools
:
target_size = 256
... # Same as above
def do_new():
t0 = time()
grid = np.meshgrid(*3*[np.arange(target_size)], indexing='ij')
grid = grid[::-1]
grid = np.stack(grid).T
ret = interpol(grid)
print(f"{time()-t0}")
return ret
c1 = improved_method()
c2 = do_new()
print((c1==c2).all())
Output:
IMPROVED METHOD: 13.865211009979248
6.268443584442139
True
Upvotes: 0
Reputation: 1007
I can't really say for the generation of the grid as I am not totally sure about what you are trying to do. But with respect to just improving the efficiency, using a triple for loop instead of making use of broadcasting is massively slowing down your code
import itertools
import numpy as np
from scipy.interpolate import RegularGridInterpolator
from time import time
target_size = 32
reduced_size = 5
small_shape = (reduced_size,reduced_size,reduced_size,3)
cube_small = np.random.randint(target_size, size=small_shape, dtype=np.uint8)
igrid = 3*[np.linspace(0, target_size-1, reduced_size)]
large_shape = (target_size, target_size, target_size,3)
cube_large = np.empty(large_shape)
def original_method():
t0 = time()
interpol = RegularGridInterpolator(igrid, cube_small)
for x in np.arange(target_size):
for y in np.arange(target_size):
for z in np.arange(target_size):
cube_large[x,y,z] = interpol([x,y,z])
print('ORIGINAL METHOD: ', time()-t0)
return cube_large
def improved_method():
t1 = time()
interpol = RegularGridInterpolator(igrid, cube_small)
arr = np.arange(target_size)
grid = np.array(list(itertools.product(arr, repeat=3)))
cube_large = interpol(grid).reshape(target_size, target_size, target_size, 3)
print('IMPROVED METHOD:', time() - t1)
return cube_large
c1 = original_method()
c2 = improved_method()
print('Is the result the same? ', np.all(c1 == c2))
OUTPUT
ORIGINAL METHOD: 6.9040000438690186
IMPROVED METHOD: 0.026999950408935547
Is the result the same? True
Upvotes: 2