无法在 Python scipy.interp 中解释 MATLAB interp2d [英] Unable to interprete MATLAB interp2d in Python scipy.interp

查看:108
本文介绍了无法在 Python scipy.interp 中解释 MATLAB interp2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码只是为了理解上下文.我的问题不需要对此有太多了解.它需要将一行 MATLAB 代码简单地转换为 Python.

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.

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

所以在这一步之后,代码应该进行插值,在 MATLAB 中它是这样的:

So after this step the code should do interpolation and in MATLAB it's like this:

vol(:,:,iz) = interp2(uu,vv ,proj',pu,pv,'linear'); This is in MATLAB

我的 proj, uu 和 vv 是 (1024,1024) 而 pu, pv 是 (360,360).我需要将上面的行转换为 Python.我尝试使用 scipy.interpolate 但它在尝试这些时出现以下错误:

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 :

vol[:,:,iz] = Ratio*(interp2d(uu,vv,proj,pu,pv,'cubic'))

TypeError: unhashable type: 'numpy.ndarray'

TypeError: unhashable type: 'numpy.ndarray'

vol[:,:,iz] = Ratio*(interp2d(uu,vv,proj,'cubic'))

溢出错误:要插入的数据点太多

OverflowError: Too many data points to interpolate

vol[:,:,iz] = Ratio*(interp2d(proj,pu,pv,'cubic'))

ValueError: 对于非矩形网格,x 和 y 的长度必须相等

ValueError: x and y must have equal lengths for non rectangular grid

vol[:,:,iz] = Ratio*(interp2d(pu,pv,proj,'cubic'))

ValueError: Invalid length for input z for non-rectangle grid

ValueError: Invalid length for input z for non rectangular grid

我已经阅读了所有 scipy.interpolate 文档,但似乎没有任何帮助.谁能找出问题所在?

I have read all the scipy.interpolate documentations and none seemed to help. Could anyone figure out what's wrong?

推荐答案

大体上的问题是您正在查看文档,但并未尝试去理解它.如果您在 scipy.interpolate 模块中使用特定函数 interp2d,则 查看函数文档,如@pv 也在评论中建议.您到处抛出参数这一事实清楚地表明您正在尝试使用基于猜测的函数.它不会工作:一个函数是用给定的语法实现的,它只能这样工作.

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.

所以看看函数的签名:

class scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=nan)

class scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=nan)

后面会解释参数的含义.可以清楚地看到有3个强制参数:xyz.不允许使用其他数组值输入 这是因为 interp2d 仅构造一个插值 函数,然后您应该使用它来计算网格上的插值值(与 MATLAB 不同,其中interp2d 直接为您提供内插值).所以你可以打电话

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

myfun = interp2(uu,vv,proj,'linear')

获得一个插值函数.然后您可以替换给定的值,但请注意,函数 myfun 将需要 1d 输入,并且它将在内部构造一个网格.因此假设您的输出网格构造为

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

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)

获取您需要的输出.

但是,您应该注意一些要点.

However, there are some important points you should note.

  1. 在 MATLAB 中,您有 interp2d(uu,vv,proj',...),但要确保 scipy 的元素 uuvvproj 的顺序相同.为了安全起见:如果网格尺寸不对称,uuvvproj 的形状应该都相同.
  2. 在 MATLAB 中,您使用 'linear' 插值,而在 python 中,您使用 'cubic'.如果您要将代码移植到新语言,我不确定这是否真的是您想要的.
  3. 在我看来,您的输出网格不像 meshgrid 那样由矩形网格定义,这表明 interp2d 可能不适合您的情况.无论如何,我对 interp2d 有过一些奇怪的经历,我不相信它.因此,如果它不适合您预期的输出,我强烈建议使用 scipy.interpolate.griddata 代替.此功能直接为您提供插值点:我建议您尝试根据手册找出其用途:) 我的链接答案也可以提供帮助.您可以以相同的方式设置插值类型,但如果您愿意,您的输出可以是任何散点集.
  1. In MATLAB you have interp2d(uu,vv,proj',...), but make sure that for scipy the elements of uu, vv, and proj are in the same order. To be on the safe side: in case of an asymmetric mesh size, the shape of uu, vv and proj should all be the same.
  2. 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.
  3. It seems to me that your output mesh is not defined by a rectangular grid as if from meshgrid, which suggests that interp2d might not be suitable for your case. Anyway, I've had some odd experiences with interp2d, and I wouldn't trust it. So if it's not suitable for your expected output, I'd strongly suggest using scipy.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.

这篇关于无法在 Python scipy.interp 中解释 MATLAB interp2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆