makhlaghi
makhlaghi

Reputation: 3986

Evaluate SmoothBivariateSpline over a grid

I have three columns of unstructured data and would like to do a bivariate spline fit over them. I am not yet too good with classes in Python so I don't understand exactly how to do this. To show my problem I have made a simple code:

#! /usr/bin/env python3

import numpy as np
from scipy import interpolate

#an array of 3 columns:
a=np.zeros((200, 3))
a[:,0]=np.random.uniform(0,1,200)
a[:,1]=np.random.uniform(3,5,200)
a[:,2]=np.random.uniform(10,12,200)

#find the boundries
min_x, max_x = np.amin(a[:,0]), np.amax(a[:,0])
min_y, max_y = np.amin(a[:,1]), np.amax(a[:,1])

#Set the resolution:
x_res=1000
y_res=int( ( (max_y-min_y) / (max_x-min_x) )*x_res )

#Make a grid
grid_x, grid_y = np.mgrid[min_x:max_x:x_res*1j, min_y:max_y:y_res*1j]

sbsp=interpolate.SmoothBivariateSpline(a[:,0], a[:,1], a[:,2])

b=sbsp.ev(4,5)
#c=sbsp.ev(grid_x, grid_y)
print(b)

This gives the interpolated value for one point, but if you comment out the second last line, it doesn't work. I would be very grateful if someone could guide me on how I can get the spline interpolation on the grid. Thanks in advance.

Upvotes: 7

Views: 7034

Answers (2)

fmw42
fmw42

Reputation: 53174

I have been struggling with something like this and finally solved it. The key for me is to create two 1D grids using bumpy linspace, one for x and one for y. And to evaluate on the grid, using .__call__ from https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.SmoothBivariateSpline.call.html#scipy.interpolate.SmoothBivariateSpline.call.

In my script below, I evaluate a spline or two x,y,z arrays, each to be output as an image.

#!/bin/python3.7

"""

Use spline interpolation to on grid of x,y,z value where z is either xdiff or ydiff for use as imagemagick 2D displacement maps

"""

import numpy as np
from scipy.interpolate import SmoothBivariateSpline
from skimage import io


# python lists of x,y dst control points and zx=xsrc-xdiff, zy=ysrc-ydiff offsets to be interpolated over full image of size 129x129
x = [8.5, 20.5, 33.5, 48.5, 64.5, 80.5, 95.5, 109.5, 121.5, 5.5, 17.5, 31.5, 46.5, 64.5, 81.5, 97.5, 111.5, 123.5, 2.5, 14.5, 29.5, 45.5, 64.5, 83.5, 99.5, 113.5, 125.5, 1.5, 12.5, 26.5, 43.5, 64.5, 85.5, 103.5, 116.5, 127.5, 0.5, 11.5, 24.5, 41.5, 64.5, 87.5, 103.5, 117.5, 128.5, 1.5, 12.5, 25.5, 42.5, 64.5, 86.5, 103.5, 116.5, 127.5, 2.5, 14.5, 28.5, 45.5, 64.5, 83.5, 100.5, 114.5, 125.5, 5.5, 17.5, 30.5, 46.5, 64.5, 81.5, 97.5, 111.5, 123.5, 8.5, 19.5, 33.5, 48.5, 64.5, 80.5, 95.5, 109.5, 121.5]
y = [7.5, 5.5, 3.5, 1.5, 1.5, 1.5, 3.5, 5.5, 7.5, 20.5, 16.5, 14.5, 12.5, 11.5, 12.5, 15.5, 16.5, 19.5, 33.5, 31.5, 28.5, 26.5, 24.5, 26.5, 28.5, 31.5, 33.5, 48.5, 47.5, 45.5, 42.5, 40.5, 42.5, 45.5, 46.5, 48.5, 64.5, 64.5, 64.5, 64.5, 64.5, 64.5, 64.5, 64.5, 64.5, 80.5, 81.5, 83.5, 86.5, 87.5, 86.5, 83.5, 81.5, 80.5, 95.5, 97.5, 100.5, 103.5, 104.5, 102.5, 100.5, 97.5, 95.5, 109.5, 111.5, 114.5, 116.5, 117.5, 116.5, 114.5, 111.5, 109.5, 121.5, 123.5, 125.5, 127.5, 127.5, 127.5, 125.5, 123.5, 120.5]
zx = [119.5, 123.5, 126.5, 127.5, 127.5, 127.5, 128.5, 130.5, 134.5, 122.5, 126.5, 128.5, 129.5, 127.5, 126.5, 126.5, 128.5, 132.5, 125.5, 129.5, 130.5, 130.5, 127.5, 124.5, 124.5, 126.5, 130.5, 126.5, 131.5, 133.5, 132.5, 127.5, 122.5, 120.5, 123.5, 128.5, 127.5, 132.5, 135.5, 134.5, 127.5, 120.5, 120.5, 122.5, 127.5, 126.5, 131.5, 134.5, 133.5, 127.5, 121.5, 120.5, 123.5, 128.5, 125.5, 129.5, 131.5, 130.5, 127.5, 124.5, 123.5, 125.5, 130.5, 122.5, 126.5, 129.5, 129.5, 127.5, 126.5, 126.5, 128.5, 132.5, 119.5, 124.5, 126.5, 127.5, 127.5, 127.5, 128.5, 130.5, 134.5]
zy = [120.5, 122.5, 124.5, 126.5, 126.5, 126.5, 124.5, 122.5, 120.5, 123.5, 127.5, 129.5, 131.5, 132.5, 131.5, 128.5, 127.5, 124.5, 126.5, 128.5, 131.5, 133.5, 135.5, 133.5, 131.5, 128.5, 126.5, 127.5, 128.5, 130.5, 133.5, 135.5, 133.5, 130.5, 129.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 126.5, 124.5, 121.5, 120.5, 121.5, 124.5, 126.5, 127.5, 128.5, 126.5, 123.5, 120.5, 119.5, 121.5, 123.5, 126.5, 128.5, 130.5, 128.5, 125.5, 123.5, 122.5, 123.5, 125.5, 128.5, 130.5, 134.5, 132.5, 130.5, 128.5, 128.5, 128.5, 130.5, 132.5, 135.5]

# convert python lists to numpy arrays
ax = np.asarray(x)
ay = np.asarray(y)
azx = np.asarray(zx)
azy = np.asarray(zy)


# define bbox of interpolated data
# bbox=[minx, maxx, miny, maxy]
bbox=[0, 129, 0, 129]

# convert bbox to numpy array
abbox = np.asarray(bbox)

# do interpolations
xd = SmoothBivariateSpline(ax, ay, azx, w=None, bbox=abbox, kx=3, ky=3)
yd = SmoothBivariateSpline(ax, ay, azy, w=None, bbox=abbox, kx=3, ky=3)

# define integer grid onto which to interpolate
grid_x=np.linspace(0, 129, num=129)
grid_y=np.linspace(0, 129, num=129)


# evaluate at grid points
xdisplace = xd.__call__(grid_x, grid_y, grid=True)
ydisplace = yd.__call__(grid_x, grid_y, grid=True)

# save output using skimage
io.imsave("xdimgs.png", xdisplace.astype('uint8'))
io.imsave("ydimgs.png", ydisplace.astype('uint8'))

# view output using skimage
io.imshow(xdisplace.astype('uint8')) 
io.show()
io.imshow(ydisplace.astype('uint8')) 
io.show()


Perhaps this will help your with your script.

Upvotes: 0

Marco
Marco

Reputation: 3401

The methodev(x,y) requires x and y to be a 1D array. In your code, grid_x and grid_y are 2D.

You could try the following:

c=sbsp.ev(grid_x[0,0], grid_y[0,0])

Upvotes: 1

Related Questions