[SciPy-User] 3D scatter data interpolate

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] 3D scatter data interpolate

yellowhat
Hi all,
I would like to interpolate 3D scatter data.
I have some 3D points, each point has an associated value, I would like to interpolate these point to calculate the value of a random point.
This example may help to understand:
from math import cos, sin
from scipy.interpolate import Rbf
from numpy import linspace, pi
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
#%% Data
R = 10.0
x = list()
y = list()
z = list()
c = list()
for i in linspace(0.0,10.0,11):
    for t in linspace(0.0,2*pi,90):
        x.append(R*cos(t))
        y.append(R*sin(t))
        z.append(i)
        c.append(i)
#%% Plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
sc = ax.scatter(x, y, z, c=z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.colorbar(sc)
#%%
rbfi = Rbf(x, y, z, c)
xi = [0,0]
yi = [0,0]
zi = [0,1]
print rbfi(xi, yi, zi)
As you can see here we have some points over a cylinder, each point has associate a value (color), which in this case is the z-coordinate.
scipy.interpolate.Rbf should answer my problem, to better understsand how it works I gave it some points [[0,0,0],[0,0,1]], but the result of rbfi(xi, yi, zi) is [-1.04833984 -0.10205078].
I expected that a point along the axis, aligned with some point should return the z-coordinate of this point [0, 1] but got some negative number.
Any ideas?
Do you have any alternative that I can use to have a linear interpolation or by the nearest?

Thanks

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: 3D scatter data interpolate

Gary Ruben
Hi Vasco,

Sorry for not replying sooner. I thought I had a self-contained example of using the scipy.ndimage.map_coordinates() function to do this. I can't find it though. It may help to look at how I used map_coordinates() to interpolate a vector field to map the vector onto a sphere:
https://gist.github.com/gazzar/369ed55f15045bb99cae
That code is pretty old now and I don't have any small test data sets for it, so just take a look and see if it helps,

Gary



On 16 February 2016 at 06:48, Vasco Gervasi <[hidden email]> wrote:
Hi all,
I would like to interpolate 3D scatter data.
I have some 3D points, each point has an associated value, I would like to interpolate these point to calculate the value of a random point.
This example may help to understand:
from math import cos, sin
from scipy.interpolate import Rbf
from numpy import linspace, pi
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
#%% Data
R = 10.0
x = list()
y = list()
z = list()
c = list()
for i in linspace(0.0,10.0,11):
    for t in linspace(0.0,2*pi,90):
        x.append(R*cos(t))
        y.append(R*sin(t))
        z.append(i)
        c.append(i)
#%% Plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
sc = ax.scatter(x, y, z, c=z)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.colorbar(sc)
#%%
rbfi = Rbf(x, y, z, c)
xi = [0,0]
yi = [0,0]
zi = [0,1]
print rbfi(xi, yi, zi)
As you can see here we have some points over a cylinder, each point has associate a value (color), which in this case is the z-coordinate.
scipy.interpolate.Rbf should answer my problem, to better understsand how it works I gave it some points [[0,0,0],[0,0,1]], but the result of rbfi(xi, yi, zi) is [-1.04833984 -0.10205078].
I expected that a point along the axis, aligned with some point should return the z-coordinate of this point [0, 1] but got some negative number.
Any ideas?
Do you have any alternative that I can use to have a linear interpolation or by the nearest?

Thanks

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: 3D scatter data interpolate

yellowhat
Sorry but I am not able to understand how map_coordinates works.
Let's say that I am in 2D and I have the points [x=0, y=0] and [x=1, y=1], and each point has associated a value [x=0, y=0, v=0] and [x=1, y=1, v=2].
My input for map_coordinates should be:
in_data = np.array([[0.,0.,0.],
                    [1.,1.,2.]])
I would like to interpolate in the middle point [x=0.5, y=0.5] so the  coordinates should be;
lookpoints = np.array([[0.5, 0.5]]).T
 So:
s = map_coordinates(in_data, lookpoints, order=1)
But i get s=0.5 not 1.
What am I doing wrong?

Thanks 


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: 3D scatter data interpolate

Juan Nunez-Iglesias
Hi Vasco,

The input to map_coordinates should be a regularly-sampled array, like an image. In your case, you've provided an input image of shape (2, 3). In the format you're thinking, (x, y, val) tuples, the input you have provided would be expressed as:

[[0, 0, 0],
 [0, 1, 0],
 [0, 2, 0],
 [1, 0, 1],
 [1, 1, 1],
 [1, 2, 2]]

As you can see, using linear interpolation at (0.5, 0.5) in that image interpolates between the values 0, 0, 1, 1, thus resulting in 0.5.

Juan.



On Mon, Feb 22, 2016 at 1:48 AM, Vasco Gervasi <[hidden email]> wrote:
Sorry but I am not able to understand how map_coordinates works.
Let's say that I am in 2D and I have the points [x=0, y=0] and [x=1, y=1], and each point has associated a value [x=0, y=0, v=0] and [x=1, y=1, v=2].
My input for map_coordinates should be:
in_data = np.array([[0.,0.,0.],
                    [1.,1.,2.]])
I would like to interpolate in the middle point [x=0.5, y=0.5] so the  coordinates should be;
lookpoints = np.array([[0.5, 0.5]]).T
 So:
s = map_coordinates(in_data, lookpoints, order=1)
But i get s=0.5 not 1.
What am I doing wrong?

Thanks 


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: 3D scatter data interpolate

yellowhat

So how can do if the input is not a regularly-sampled array? Like some scattered data?
In my last mail I have just 2 points now are 6.

Thank you very much


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: 3D scatter data interpolate

Juan Nunez-Iglesias
Have a look at the functions in scipy.interpolate. `griddata` looks like what you need but is not defined for only two points in 2D: you need to define values for enough points to form a convex hull. (ie at a minimum, ndim + 1 points.)

hth!

Juan.

On Mon, Feb 22, 2016 at 6:58 PM, Vasco Gervasi <[hidden email]> wrote:

So how can do if the input is not a regularly-sampled array? Like some scattered data?
In my last mail I have just 2 points now are 6.

Thank you very much


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user



_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user