def __call__(self, polygon): x = np.array(np.concatenate((polygon[:, 0], [polygon[0, 0]]), axis=0)) y = np.concatenate((polygon[:, 1], [polygon[0, 1]]), axis=0) vertices = np.array([x, y]).transpose() c = np.dot(vertices, [1, 1j]) arclength = np.concatenate([[0], np.cumsum(abs(np.diff(c)))]) target = arclength[-1] * np.arange(self.n_samples) / self.n_samples gi_x = torch_interpolations.RegularGridInterpolator( [torch.tensor(arclength)], torch.tensor(x)) gi_y = torch_interpolations.RegularGridInterpolator( [torch.tensor(arclength)], torch.tensor(y)) fx = gi_x([torch.tensor(target)]) fy = gi_y([torch.tensor(target)]) return np.concatenate( (fx.numpy()[:, np.newaxis], fy.numpy()[:, np.newaxis]), axis=1)
def test_regular_grid_interpolator(): points = [torch.arange(-.5, 2.5, .1) * 1., torch.arange(-.5, 2.5, .2) * 1.] values = torch.sin(points[0])[:, None] + 2 * torch.cos(points[1])[None, :] + torch.sin(5 * points[0][:, None] @ points[1][None, :]) gi = torch_interpolations.RegularGridInterpolator(points, values) X, Y = np.meshgrid(np.arange(-.5, 2, .1), np.arange(-.5, 2, .1)) points_to_interp = [torch.from_numpy( X.flatten()).float(), torch.from_numpy(Y.flatten()).float()] fx = gi(points_to_interp) print(fx) rgi = RegularGridInterpolator( [p.numpy() for p in points], values.numpy(), bounds_error=False) np.testing.assert_allclose( rgi(np.vstack([X.flatten(), Y.flatten()]).T), fx.numpy(), atol=1e-6)
def __init__(self, domain, vfield): ''' The vfield give vector feild on domain. ''' shape = torch.tensor(domain.shape) spacing = torch.tensor(domain.spacing) origin = torch.tensor(domain.origin) points = list() self.calls = 0 for dim in range(len(domain.shape)): start = origin[dim] stop = origin[dim] + shape[dim] * spacing[dim] rang = torch.arange(start, stop, spacing[dim]) #print ("interp dim:",dim,rang.shape,vfield[dim].shape) points.append(rang) self.interp = [ ti.RegularGridInterpolator(points, component) for component in vfield ]
import torch import torch_interpolations import numpy as np import matplotlib.pyplot as plt points = [torch.arange(-.5, 2.5, .2) * 1., torch.arange(-.5, 2.5, .2) * 1.] values = torch.sin( points[0])[:, None] + 2 * torch.cos(points[1])[None, :] + torch.sin( 5 * points[0][:, None] @ points[1][None, :]) gi = torch_interpolations.RegularGridInterpolator(points, values) X, Y = np.meshgrid(np.arange(-.5, 2.5, .02), np.arange(-.5, 2.5, .01)) points_to_interp = [ torch.from_numpy(X.flatten()).float(), torch.from_numpy(Y.flatten()).float() ] fx = gi(points_to_interp) print(fx) fig, axes = plt.subplots(1, 2) axes[0].imshow(np.sin(X) + 2 * np.cos(Y) + np.sin(5 * X * Y)) axes[0].set_title("True") axes[1].imshow(fx.numpy().reshape(X.shape)) axes[1].set_title("Interpolated") plt.show()
def f(values): return torch_interpolations.RegularGridInterpolator( points, values)(points_to_interp)
def time_function(f, n=10): times = [] for _ in range(n): tic = time.time() f() toc = time.time() times.append(1000 * (toc - tic)) return times points = [torch.arange(-.5, 2.5, .01) * 1., torch.arange(-.5, 2.5, .01) * 1.] values = torch.sin( points[0])[:, None] + 2 * torch.cos(points[1])[None, :] + torch.sin( 5 * points[0][:, None] @ points[1][None, :]) gi = torch_interpolations.RegularGridInterpolator(points, values) X, Y = np.meshgrid(np.arange(-.5, 2.5, .002), np.arange(-.5, 2.5, .001)) points_to_interp = [ torch.from_numpy(X.flatten()).float(), torch.from_numpy(Y.flatten()).float() ] rgi = RegularGridInterpolator([p.numpy() for p in points], values.numpy(), bounds_error=False) input_rgi = np.vstack([X.flatten(), Y.flatten()]).T points_cuda = [p.cuda() for p in points] values_cuda = values.cuda() gi_cuda = torch_interpolations.RegularGridInterpolator(points_cuda, values_cuda)