Given a set of points defined in (X, Y, Z) coordinates, interpolate Z-value at arbitrary (X, Y)

2024/9/8 11:00:58

Given a set of points in (X, Y, Z) coordinates that are points on a surface, I would like to be able to interpolate Z-values at arbitrary (X, Y) coordinates. I've found some success using mlab.griddata for interpolating values on a grid, but I want to be able to call a general use function for any (X, Y) coordinate.

The set of points form a roughly hemispherical surface. To simplify the problem, I am trying to write a method that interpolates values between known points of the hemisphere defined by the x, y, and z coordinates below. Although there is an analytical solution to find z = f(x, y) for a perfect sphere, such that you don't have to interpolate, the actual set of points will not be a perfect sphere, so we should assume that we need to interpolate values at unknown (X, Y) coordinates. Link to IPython notebook with point data

resolution = 10
u = np.linspace(-np.pi / 2, np.pi / 2, resolution)
v = np.linspace(0, np.pi, resolution)U, V = np.meshgrid(u, v)xs = np.sin(U) * np.cos(V) 
ys = np.sin(U) * np.sin(V)
zs = np.cos(U)

I have been using scipy.interpolate.interp2d, which "returns a function whose call method uses spline interpolation to find the value of new points."

def polar(xs, ys, zs, resolution=10):rs = np.sqrt(np.multiply(xs, xs) + np.multiply(ys, ys))ts = np.arctan2(ys, xs)func = interp2d(rs, ts, zs, kind='cubic')vectorized = np.vectorize(func)# Guessesri = np.linspace(0, rs.max(), resolution)ti = np.linspace(0, np.pi * 2, resolution)R, T = np.meshgrid(ri, ti)Z = vectorized(R, T)return R * np.cos(T), R * np.sin(T), Z

Unfortunately I get pretty weird results, similarly to another StackOverflow user who tried to use interp2d.

Polar result

The most success I have found thus far is using inverse squares to estimate values of Z at (X, Y). But the function is not perfect for estimating values of Z near Z=0.

Inverse Squares result

What can I do to get a function z = f(x, y) given a set of points in (x, y, z)? Am I missing something here... do I need more than a point cloud to reliably estimate a value on a surface?

EDIT:

This is the function that I ended up writing. The function takes input arrays of xs, ys, zs and interpolates at x, y using scipy.interpolate.griddata, which does not require a regular grid. I'm sure there is a smarter way to do this and would appreciate any updates, but it works and I'm not concerned with performance. Including a snippet in case it helps anyone in the future.

def interpolate(x, y, xs, ys, zs):r = np.sqrt(x*x + y*y)t = np.arctan2(y, x)rs = np.sqrt(np.multiply(xs, xs) + np.multiply(ys, ys))ts = np.arctan2(ys, xs)rs = rs.ravel()ts = ts.ravel()zs = zs.ravel()ts = np.concatenate((ts - np.pi * 2, ts, ts + np.pi * 2))rs = np.concatenate((rs, rs, rs))zs = np.concatenate((zs, zs, zs))Z = scipy.interpolate.griddata((rs, ts), zs, (r, t))Z = Z.ravel()R, T = np.meshgrid(r, t)return Z
Answer

You're saying that you've tried using griddata. So why was that not working? griddata also works if the new points are not regularly spaced. For example,

# Definitions of xs, ys and zs
nx, ny = 20, 30
x = np.linspace(0, np.pi, nx)
y = np.linspace(0, 2*np.pi, ny)X,Y = np.meshgrid(x, y)xs = X.reshape((nx*ny, 1))
ys = Y.reshape((nx*ny, 1))## Arbitrary definition of zs
zs = np.cos(3*xs/2.)+np.sin(5*ys/3.)**2## new points where I want the interpolations
points = np.random.rand(1000, 2)import scipy.interpolate
zs2 = scipy.interpolate.griddata(np.hstack((xs, ys)), zs, points)

Is this not what you are after?

https://en.xdnf.cn/q/73139.html

Related Q&A

Python multiprocessing speed

I wrote this bit of code to test out Pythons multiprocessing on my computer:from multiprocessing import Poolvar = range(5000000) def test_func(i):return i+1if __name__ == __main__:p = Pool()var = p.map…

Using os.forkpty() to create a pseudo-terminal to ssh to a remote server and communicate with it

Im trying to write a python script that can ssh into remote server and can execute simple commands like ls,cd from the python client. However, Im not able to read the output from the pseudo-terminal af…

Calculating the sum of a series?

This is my assignment and for the life of me i cant seem to think of a way to do it. This is the code I have so far:sum = 0 k = 1 while k <= 0.0001:if k % 2 == 1:sum = sum + 1.0/kelse:sum = sum - 1.…

get all the partitions of the set python with itertools

How to get all partitions of a set? For example, I have array [1, 2, 3]. I need to get [[1], [2], [3]], [[1], [2, 3]], [[2], [1,3]], [[3], [1, 2]], [[1, 2, 3]]. Now, I wrote this code:def neclusters(S…

Installing Python binary modules to a custom location in Windows

Suppose that I want to install a binary module for Python on Windows. Suppose that the module is distributed as a pre-built installer xxx-n.n.n.win32-py2.7.exe, prepared using distutils.My problem is t…

tkinter tkMessageBox html link

I got a tkMEssagebox.showerror showing up in a python tkinter application, when somebody failed to login with the application. Is it possible to have a url link in the tkMessageBox.showerror?ie.tkMess…

How to build a MultiIndex Pandas DataFrame from a nested dictionary with lists

I have the following dictionary.d= {key1: {sub-key1: [a,b,c,d,e]},key2: {sub-key2: [1,2,3,5,8,9,10]}}With the help of this post, I managed to successfully convert this dictionary to a DataFrame.df = pd…

Functions and if - else in python. Mutliple conditions. Codeacademy

Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, wed use s for string).The shut_down function should return "Shutting down..." when it gets …

ipython up and down arrow strange behaviour

In my installation of ipython I have this strange problem where I cannot reliably move through command history with up and down arrows... a lot of the time it just doesnt work (nothing happens on the k…

Python for loop slows and evenutally hangs

Im totally new to Python (as of half an hour ago) and trying to write a simple script to enumerate users on an SMTP server.The users file is a simple list (one per line) of usernames.The script runs fi…