我问是因为我遇到了一些非常奇怪的事情。从脚本中调用以下方法时会引发异常:
def gaussian_blur(self, in_array, size):
# expand in_array to fit edge of kernel
padded_array = np.pad(in_array, (10,), 'reflect')
# build kernel
x, y = np.mgrid[-size:size + 1, -size:size + 1]
g = np.exp(-(x**2 / float(size) + y**2 / float(size)))
g = (g / g.sum()).astype(in_array.dtype)
# do the Gaussian blur
return fftconvolve(padded_array, g, mode='valid')
Traceback (most recent call last):
File "raster_tools/smooth.py", line 130, in <module>
blurred = ri.gaussian_blur(my_array, 10)
File "raster_tools/smooth.py", line 80, in gaussian_blur
padded_array = np.pad(in_array, (10,), 'reflect')
File "/home/.virtualenvs/geo/local/lib/python2.7/site-packages/numpy/lib/arraypad.py", line 1358, in pad
kwargs)
File "/home/.virtualenvs/geo/local/lib/python2.7/site-packages/numpy/lib/shape_base.py", line 91, in apply_along_axis
res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
TypeError: 'unicode' object is not callable
当我使用 ipdb 设置断点然后调用
padded_array = np.pad(in_array, (10,), 'reflect')
(顺便说一下,函数中的所有其他代码行)不会发生错误。由于在虚拟环境中调用 python 脚本,我可以想象它ipdb
正在使用不同的解释器。即使在虚拟环境之外运行代码也会产生相同的行为。