当使用scipy.ndimage.interpolation.shift通过周期性边界处理(mode = 'wrap'
)沿一个轴移动一个 numpy 数据数组时,我得到了一个意想不到的行为。该例程试图强制第一个像素 ( index 0
) 与最后一个 ( index N-1
) 相同,而不是“最后一个加一个 ( index N
)”。
最小的例子:
# module import
import numpy as np
from scipy.ndimage.interpolation import shift
import matplotlib.pyplot as plt
# print scipy.__version__
# 0.18.1
a = range(10)
plt.figure(figsize=(16,12))
for i, shift_pix in enumerate(range(10)):
# shift the data via spline interpolation
b = shift(a, shift=shift_pix, mode='wrap')
# plotting the data
plt.subplot(5,2,i+1)
plt.plot(a, marker='o', label='data')
plt.plot(np.roll(a, shift_pix), marker='o', label='data, roll')
plt.plot(b, marker='o',label='shifted data')
if i == 0:
plt.legend(loc=4,fontsize=12)
plt.ylim(-1,10)
ax = plt.gca()
ax.text(0.10,0.80,'shift %d pix' % i, transform=ax.transAxes)
蓝线:移位前的数据
绿线:预期的移位行为
红线:scipy.ndimage.interpolation.shift的实际移位输出
我如何调用该函数或我如何理解它的行为是否有一些错误mode = 'wrap'
?当前结果与相关scipy 教程页面和另一个StackOverflow 帖子中的模式参数描述形成对比。代码中是否存在错误?
使用的 Scipy 版本是 0.18.1,分布在 anaconda-2.2.0