The following code is just to understand the context. My question doesn't require much understanding of this. It need a simple translation of one line of MATLAB code in to Python.
下面的代码只是为了理解上下文。我的问题不需要太多的理解。它需要简单地将一行MATLAB代码转换成Python代码。
us = np.linspace(-(1023)/2,(1023)/2,1024)
vs = np.linspace(-(1023)/2,(1023)/2,1024)
uu,vv = np.meshgrid(-us,vs)
pu = ((((rx*SDD)/(ry+SOD))+us[0])/(-du))+1
xs = np.linspace(-(360-1)/2,(nx-1)/2,360)
ys = np.linspace(-(360-1)/2,(ny-1)/2,360)
zs = np.linspace(-(360-1)/2,(ny-1)/2,360)
xx,yy = np.meshgrid(xs,ys)
angle_rad = np.linspace(0,359,360)
angle_rad = angle_rad*np.pi/180
for i in range(0,360) :
vol = np.zeros((360,360,360))
rx = xx*np.cos(angle_rad[i]-np.pi/2) + yy*np.sin(angle_rad[i]-np.pi/2)
ry = -xx*np.sin(angle_rad[i]-np.pi/2) + yy*np.cos(angle_rad[i]-np.pi/2)
pu = ((((rx*370)/(ry+9))+us[0])/(-51.2/1024))+1
for iz in range(0,360) :
pv = ((((zs[iz]*370)/(ry+9))-vs[0])/(51.2/1024)) +1
So after this step the code should do interpolation and in MATLAB it's like this:
在这一步之后代码应该做插值在MATLAB中它是这样的
vol(:,:,iz) = interp2(uu,vv ,proj',pu,pv,'linear'); This is in MATLAB
My proj, uu and vv are (1024,1024) and pu, pv are (360,360). I need to convert the above line to Python. I tried using scipy.interpolate but it gives the following errors on trying these :
我的proj, uu和vv是(1024,1024)和pu, pv是(360 360)。我需要将上面的代码转换为Python。我试过使用scipy。内插,但它给出了如下错误:
vol[:,:,iz] = Ratio*(interp2d(uu,vv,proj,pu,pv,'cubic'))
TypeError: unhashable type: 'numpy.ndarray'
TypeError:unhashable类型:“numpy.ndarray”
vol[:,:,iz] = Ratio*(interp2d(uu,vv,proj,'cubic'))
OverflowError: Too many data points to interpolate
OverflowError:过多的数据点来插入。
vol[:,:,iz] = Ratio*(interp2d(proj,pu,pv,'cubic'))
ValueError: x and y must have equal lengths for non rectangular grid
ValueError: x和y对非矩形网格的长度必须相等。
vol[:,:,iz] = Ratio*(interp2d(pu,pv,proj,'cubic'))
ValueError: Invalid length for input z for non rectangular grid
ValueError:非矩形网格输入z的长度无效。
I have read all the scipy.interpolate documentations and none seemed to help. Could anyone figure out what's wrong?
我已经阅读了所有的scipy.插值文档,似乎没有任何帮助。谁能弄明白是怎么回事?
1 个解决方案
#1
1
The problem on a wide scale is that you're looking at the documentation but you're not trying to understand it. If you're using a specific function interp2d
in the module scipy.interpolate
, then look at the function's documentation, as @pv also suggested in a comment. The fact that you're throwing the arguments all around the place clearly demonstrates that you're trying to use a function based on guesswork. It won't work: a function was implemented with a given syntax, and it will only work like that.
大规模的问题是你在看文档,但你并没有试图去理解它。如果您在模块scipy. insert中使用一个特定的函数interp2d,那么查看函数的文档,就像@pv在注释中建议的那样。事实是,你把所有的论点都抛在了脑后,这清楚地表明你在试图利用一种基于猜测的函数。它不会起作用:一个函数是用给定的语法实现的,它只会这样工作。
So look at the function's signature:
所以看看这个函数的签名:
class scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=nan)
类scipy.插值.interp2d(x, y, z, kind='线性',copy=True, bounds_error=False, fill_value=nan)
The meaning of the parameters is explained afterwards. You can clearly see that there are 3 mandatory parameters: x
, y
, z
. No other array-valued inputs are allowed This is because interp2d
only constructs an interpolating function, which you should then use to compute the interpolated values on a mesh (unlike MATLAB, where interp2d
gives you the interpolated values directly). So you can call
之后解释参数的含义。你可以清楚地看到有3强制参数:x,y,z。不允许有其他数组值输入这是因为interp2d只构造插值函数,然后你应该使用计算插值网格(不像MATLAB interp2d直接给你插入的值)。所以你可以叫
myfun = interp2(uu,vv,proj,'linear')
to get an interpolating function. You can then substitute at the given values, but note that the function myfun
will expect 1d input, and it will construct a mesh internally. So assuming that your output mesh is constructed as
得到一个插值函数。然后可以在给定的值上进行替换,但是注意函数myfun将期望1d输入,并且它将在内部构造一个网格。假设你的输出网格被构造成。
puu,puv = np.meshgrid(puu_vec,puv_vec)
(which is probably not the case, but I'll get back to this later), you need
(很可能不是这样,但我稍后再讲),你需要。
vol[:,:,iz] = Ratio*myfun(puu_vec,puv_vec)
to obtain the output you need.
获得所需的输出。
However, there are some important points you should note.
然而,你应该注意到一些重要的观点。
- In MATLAB you have
interp2d(uu,vv,proj',...)
, but make sure that forscipy
the elements ofuu
,vv
, andproj
are in the same order. To be on the safe side: in case of an asymmetric mesh size, the shape ofuu
,vv
andproj
should all be the same. - 在MATLAB中,你有interp2d(uu,vv,proj',…),但要确保uu,vv和proj的元素都是相同的顺序。为了安全起见:如果不对称的网目尺寸,uu, vv和proj的形状都应该是相同的。
- In MATLAB you're using
'linear'
interpolation, while in python you're using'cubic'
. I'm not sure this is really what you want, if you're porting your code to a new language. - 在MATLAB中,你使用的是“线性”插值,而在python中,你使用的是“立方”。我不确定这是否是您想要的,如果您将您的代码移植到一种新的语言中。
- It seems to me that your output mesh is not defined by a rectangular grid as if from
meshgrid
, which suggests thatinterp2d
might not be suitable for your case. Anyway, I've had some odd experiences withinterp2d
, and I wouldn't trust it. So if it's not suitable for your expected output, I'd strongly suggest usingscipy.interpolate.griddata
instead. This function gives you interpolated points directly: I suggest that you try to figure out its use based on the manual:) My linked answer can also help. You can set the kind of interpolation in the same way, but your output can be any set of scattered points if you like. - 在我看来,你的输出网格不是由一个矩形网格定义的,就像网格网格一样,这意味着interp2d可能不适合你的情况。不管怎样,我在interp2d上有过一些奇怪的经历,我不会相信它。因此,如果它不适合您的预期输出,我强烈建议使用scipy. insert .griddata代替。这个函数直接给你插入点:我建议你试着根据手册找出它的用法:)我的链接答案也可以帮助你。你可以用同样的方法来设置插值,但是如果你喜欢的话,你的输出可以是任意的散点集。
#1
1
The problem on a wide scale is that you're looking at the documentation but you're not trying to understand it. If you're using a specific function interp2d
in the module scipy.interpolate
, then look at the function's documentation, as @pv also suggested in a comment. The fact that you're throwing the arguments all around the place clearly demonstrates that you're trying to use a function based on guesswork. It won't work: a function was implemented with a given syntax, and it will only work like that.
大规模的问题是你在看文档,但你并没有试图去理解它。如果您在模块scipy. insert中使用一个特定的函数interp2d,那么查看函数的文档,就像@pv在注释中建议的那样。事实是,你把所有的论点都抛在了脑后,这清楚地表明你在试图利用一种基于猜测的函数。它不会起作用:一个函数是用给定的语法实现的,它只会这样工作。
So look at the function's signature:
所以看看这个函数的签名:
class scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=nan)
类scipy.插值.interp2d(x, y, z, kind='线性',copy=True, bounds_error=False, fill_value=nan)
The meaning of the parameters is explained afterwards. You can clearly see that there are 3 mandatory parameters: x
, y
, z
. No other array-valued inputs are allowed This is because interp2d
only constructs an interpolating function, which you should then use to compute the interpolated values on a mesh (unlike MATLAB, where interp2d
gives you the interpolated values directly). So you can call
之后解释参数的含义。你可以清楚地看到有3强制参数:x,y,z。不允许有其他数组值输入这是因为interp2d只构造插值函数,然后你应该使用计算插值网格(不像MATLAB interp2d直接给你插入的值)。所以你可以叫
myfun = interp2(uu,vv,proj,'linear')
to get an interpolating function. You can then substitute at the given values, but note that the function myfun
will expect 1d input, and it will construct a mesh internally. So assuming that your output mesh is constructed as
得到一个插值函数。然后可以在给定的值上进行替换,但是注意函数myfun将期望1d输入,并且它将在内部构造一个网格。假设你的输出网格被构造成。
puu,puv = np.meshgrid(puu_vec,puv_vec)
(which is probably not the case, but I'll get back to this later), you need
(很可能不是这样,但我稍后再讲),你需要。
vol[:,:,iz] = Ratio*myfun(puu_vec,puv_vec)
to obtain the output you need.
获得所需的输出。
However, there are some important points you should note.
然而,你应该注意到一些重要的观点。
- In MATLAB you have
interp2d(uu,vv,proj',...)
, but make sure that forscipy
the elements ofuu
,vv
, andproj
are in the same order. To be on the safe side: in case of an asymmetric mesh size, the shape ofuu
,vv
andproj
should all be the same. - 在MATLAB中,你有interp2d(uu,vv,proj',…),但要确保uu,vv和proj的元素都是相同的顺序。为了安全起见:如果不对称的网目尺寸,uu, vv和proj的形状都应该是相同的。
- In MATLAB you're using
'linear'
interpolation, while in python you're using'cubic'
. I'm not sure this is really what you want, if you're porting your code to a new language. - 在MATLAB中,你使用的是“线性”插值,而在python中,你使用的是“立方”。我不确定这是否是您想要的,如果您将您的代码移植到一种新的语言中。
- It seems to me that your output mesh is not defined by a rectangular grid as if from
meshgrid
, which suggests thatinterp2d
might not be suitable for your case. Anyway, I've had some odd experiences withinterp2d
, and I wouldn't trust it. So if it's not suitable for your expected output, I'd strongly suggest usingscipy.interpolate.griddata
instead. This function gives you interpolated points directly: I suggest that you try to figure out its use based on the manual:) My linked answer can also help. You can set the kind of interpolation in the same way, but your output can be any set of scattered points if you like. - 在我看来,你的输出网格不是由一个矩形网格定义的,就像网格网格一样,这意味着interp2d可能不适合你的情况。不管怎样,我在interp2d上有过一些奇怪的经历,我不会相信它。因此,如果它不适合您的预期输出,我强烈建议使用scipy. insert .griddata代替。这个函数直接给你插入点:我建议你试着根据手册找出它的用法:)我的链接答案也可以帮助你。你可以用同样的方法来设置插值,但是如果你喜欢的话,你的输出可以是任意的散点集。