【问题标题】:Python/SciPy: Issues converting DataFrame from polar to Cartesian gridPython/SciPy:将 DataFrame 从极坐标网格转换为笛卡尔网格的问题
【发布时间】:2018-09-25 16:48:34
【问题描述】:

我使用多普勒测风激光雷达进行了测量(PPI 弧形扫描)。数据存储在 pandas 数据框中,其中行表示方位角,列表示径向距离(输入形状 = 30x197)。 Link to example scan, (csv)。我想将其转换为笛卡尔坐标系,并输出一个二维数组,该数组被重新网格化为 x,y 坐标而不是极坐标,并将值存储在适当的网格单元中。插值(最近邻)是可以的,零或不存在数据的区域的 NaN 填充也是如此。

理想情况下,X 和 Y 网格应该对应于点之间的实际距离,但现在我只是想让它工作。这应该不是非常困难,但我无法获得我想要的结果。 到目前为止,我的工作代码可以很好地绘制在极轴上(example image),但这不适用于我接下来的分析步骤。

我用scipy.interpolate.griddatascipy.ndimage.geometric_transformscipy.ndimage.map_coordinates 尝试了许多不同的方法,但没有得到正确的输出。这是我最近尝试的一个示例(df_polar 是链接的 csv 文件):

# Generate polar and cartesian meshgrids
r = df_polar.columns
theta = df_polar.index
theta = np.deg2rad(theta)

# Polar meshgrid
rad_c, theta_c = np.meshgrid(r,theta)

# Cartesian meshgrid
X = rad_c * np.cos(theta_c)
Y = rad_c * np.sin(theta_c)
x,y = np.meshgrid(X,Y)

# Interpolate from polar to cartesian grid
new_grid = scipy.interpolate.griddata(
    (rad_c.flatten(), theta_c.flatten()), 
    np.array(df_polar).flatten(), (x,y), method='nearest')

结果根本不正确,通过阅读文档和示例,我不明白为什么。我将不胜感激任何关于我哪里出错的提示。非常感谢!!

【问题讨论】:

  • 你的new_grid最后是什么形状?我得到(5910, 5910)
  • 我刚刚意识到一个非常相似的问题是answered before
  • 感谢您提供的链接,尽管搜索了很多我还没有看到那个帖子!

标签: python pandas numpy scipy


【解决方案1】:

我想你可能给griddata 喂错了点。它需要笛卡尔点,如果您想要在常规 x/y 网格上插值的值,您需要创建一个并提供它。

试试这个,如果它产生预期的结果,请告诉我。我很难判断这是否应该产生:

from scipy.interpolate import griddata
import pandas as pd
import numpy as np

df_polar = pd.read_csv('onescan.txt', index_col=0)

# Generate polar and cartesian meshgrids
r = pd.to_numeric(df_polar.columns)
theta = np.deg2rad(df_polar.index)

# Polar meshgrid
rad_c, theta_c = np.meshgrid(r, theta)

# Cartesian equivalents of polar co-ordinates
X = rad_c*np.cos(theta_c)
Y = rad_c*np.sin(theta_c)

# Cartesian (x/y) meshgrid
grid_spacing = 100.0   # You can change this
nx = (X.max() - X.min())/grid_spacing
ny = (Y.max() - Y.min())/grid_spacing
x = np.arange(X.min(), X.max() + grid_spacing, grid_spacing)
y = np.arange(Y.min(), Y.max() + grid_spacing, grid_spacing)
grid_x, grid_y = np.meshgrid(x, y)

# Interpolate from polar to cartesian grid
new_grid = griddata(
    (X.flatten(), Y.flatten()),
    df_polar.values.flatten(),
    (grid_x, grid_y),
    method='nearest'
)

结果值看起来像这样(grid_spacing = 10 并翻转 x 和 y):

import matplotlib.pyplot as plt
plt.imshow(new_grid.T, cmap='hot')

明确插入“最近”需要驯服...

【讨论】:

  • 嗨,比尔,非常感谢您在这方面的帮助!我现在明白了,我之前通过的(x,y)笛卡尔网格只是极点的变换,需要在正常间隔的平面上重新网格化。感谢您发现并纠正这一点。你是对的,最近邻插值的行为很奇怪。如果我使用线性插值重新运行(并反转值以使方向正确),那么这就是我的结果:i.imgur.com/LEh0xdS.png 这看起来不错,但我应该使用最近邻法。有什么建议吗?
  • 哇,看起来很棒。我无法提供更多建议,因为我以前没有使用过这个。最近的邻居是否提供任何选项,例如限制 NN 的距离?
  • 如果你真的不能让griddata 做你想做的事,你可以自己做最近邻插值。 scipy.spatial.KDTreesklearn.neighbors 可能有用。
  • 欢迎acknowledge my answer@Elliot。
  • 谢谢比尔,我对此进行了更多思考,你是对的,我需要在 scipy 的 griddata 函数的当前状态下手动进行 NN 插值。我将尝试检查这些点是否在扫描的凸包内:stackoverflow.com/questions/16750618/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多