scipy fftconvolve 声称输入参数没有相同的维度。我在解析什么?
scipy fftconvolve claims input parameters don't have same dimensionality. What am I parsing?
我正在尝试创建一个 class,它使用 scipy.signal 中的 fftconvolve 来对一些数据进行卷积在 class 实例的方法中使用高斯函数。但是每次创建一个实例并调用方法 enlarge_smooth(这发生在右箭头键按下时),我从 fftconvolve 说明:ValueError:in1 和 in2 应具有相同的维度。当行 elif not in1.ndim == in2.ndim:
的计算结果为 True 时,函数 def fftconvolve(in1, in2, mode="full"):
就会发生这种情况。然而,我的行 print vals.ndim == gs.ndim
在调用 fftconvolve 之前打印 True,并且 vals 和gs 的维度为 (101,)。因此,如果我不将 vals 和 gs 解析为 fftconvolve 我在解析什么?为什么它不起作用?
class Smoother(object):
import sys
sys.path.append("/DATA/Pythonfunktioner")
from scipy.signal import fftconvolve
import pyximport; pyximport.install()
from fitting6 import gs_smooth1
"""
This class allows the user to smooth any function of one variable with a gaussian using fftconvolve while looking at the smoothed function. The smoothing parameter is changed with the arrow keys and finally chosen with enter.
"""
def __init__(self, data):
self.data = data
self.sigma = 1 #smallest possible sigma for this smoothing
self.arr = np.arange(len(self.data.get_ydata()), dtype='float64') - len(self.data.get_ydata())/2
self.stack = [data]
self.line = data
self.active = True
def connect(self):
self.cidkpress = self.data.figure.canvas.mpl_connect('key_press_event', self.key)
def key(self, event):
if event.key == 'right':
self.enlarge_smooth()
elif event.key == 'left':
self.lower_smooth()
elif event.key == 'enter':
self.term(event)
def enlarge_smooth(self):
if 0: #Check if larger smooth is already in stack
pass#set larger smooth as current
else:
gs = self.gs_smooth1(self.arr.copy(), self.sigma) #Gaussian core centered at 0
vals = self.data.get_ydata().copy()
print vals.ndim == gs.ndim
print vals.ndim, type(vals), vals.dtype
print gs.ndim, type(gs), gs.dtype
# print vals, type(vals), vals.dtype
# print gs, type(gs), gs.dtype
newsmooth = self.fftconvolve(vals, gs)
self.line = Line2D(self.data.get_xdata(), newsmooth)
self.stack.append(self.line)
def lower_smooth(self):
if 1: #Check if current smooth is lowest possible
print "Cannot smooth less. Least smooth already active."
else:
pass#Set lesser smooth as current
def term(self, event):
self.active = False
self.disconnect()
def disconnect(self):
self.data.figure.canvas.mpl_disconnect(self.cidkpress)
我也试过解析 vals[0]
和 gs[0]
来检查我是否会解析两个长度为 101 的列表。结果我真的只会解析两个标量,而 ftconvolve`将退出并出现错误:TypeError: *: 'Smoother' 和 'float'.
不支持的操作数类型
看起来我正在解析 class 本身的一个实例。我就是看不出来。
如果它有助于我通过调用以下函数 class 测试我的
def smoothBF(datalist):
from matplotlib import pyplot as plt
for i in xrange(len(datalist)):
fig, axs = plt.subplots(nrows=1, ncols=1)
data, = axs.plot(datalist[i][0], datalist[i][1])
smoother = Smoother(data)
smoother.connect()
while smoother.active:
plt.pause(0.1)
#Return current result
plt.close(fig)
其中 datalist 是一个仅包含元组 (np.arange(101), np.random.random(101))
的列表
更新:似乎与在 class 定义中导入 fftconvolve 有关。添加一些 print 语句以获取 scipy fftconvolve 函数内的类型和维数确认 in1 不知何故是 平滑类型。但是当我在模块的顶部而不是在 class 定义中编写 from scipy.signal import fftconvolve
并调用 newsmooth = fftconvolve(vals, gs)
而不是 newsmooth = self.fftconvolve(vals, gs)
时,它也会给出不同的结果。然后我从 fftconvolve.
收到错误消息 AttributeError: 'numpy.ndarray' object has no attribute 'ndims'
class 定义中包含 from scipy.signal import fftconvolve
的任何调用都会解析 class 本身的实例。在 class 定义之外进行导入并在我对 fftconvolve 的编辑中修复拼写错误(如我的问题更新中所述)使解析正确并且代码 运行.
你要在 class 定义中导入 fftconvolve
的 "trick" 最终会让你失望。除非您没有向我们展示您在别处定义 Smooth.fftconvolve
的位置。
这是您现在拥有的:
class Smooth(object):
from scipy.signal import fftconvolve
def enlarge_smooth(self):
# other stuff
self.fftconvolve(vals, gs)
当你打电话给
s = S()
s.enlarge_smooth()
fftconvolve
将被称为
fftconvolve(self, vals, gs)
解决办法很简单:不要做这种诡计。
相反,在 class 之外导入 fftconvolve
并直接调用该函数:
from scipy.signal import fftconvolve
class Smooth(object):
def enlarge_smooth(self):
# other stuff
fftconvolve(vals, gs)
我正在尝试创建一个 class,它使用 scipy.signal 中的 fftconvolve 来对一些数据进行卷积在 class 实例的方法中使用高斯函数。但是每次创建一个实例并调用方法 enlarge_smooth(这发生在右箭头键按下时),我从 fftconvolve 说明:ValueError:in1 和 in2 应具有相同的维度。当行 elif not in1.ndim == in2.ndim:
的计算结果为 True 时,函数 def fftconvolve(in1, in2, mode="full"):
就会发生这种情况。然而,我的行 print vals.ndim == gs.ndim
在调用 fftconvolve 之前打印 True,并且 vals 和gs 的维度为 (101,)。因此,如果我不将 vals 和 gs 解析为 fftconvolve 我在解析什么?为什么它不起作用?
class Smoother(object):
import sys
sys.path.append("/DATA/Pythonfunktioner")
from scipy.signal import fftconvolve
import pyximport; pyximport.install()
from fitting6 import gs_smooth1
"""
This class allows the user to smooth any function of one variable with a gaussian using fftconvolve while looking at the smoothed function. The smoothing parameter is changed with the arrow keys and finally chosen with enter.
"""
def __init__(self, data):
self.data = data
self.sigma = 1 #smallest possible sigma for this smoothing
self.arr = np.arange(len(self.data.get_ydata()), dtype='float64') - len(self.data.get_ydata())/2
self.stack = [data]
self.line = data
self.active = True
def connect(self):
self.cidkpress = self.data.figure.canvas.mpl_connect('key_press_event', self.key)
def key(self, event):
if event.key == 'right':
self.enlarge_smooth()
elif event.key == 'left':
self.lower_smooth()
elif event.key == 'enter':
self.term(event)
def enlarge_smooth(self):
if 0: #Check if larger smooth is already in stack
pass#set larger smooth as current
else:
gs = self.gs_smooth1(self.arr.copy(), self.sigma) #Gaussian core centered at 0
vals = self.data.get_ydata().copy()
print vals.ndim == gs.ndim
print vals.ndim, type(vals), vals.dtype
print gs.ndim, type(gs), gs.dtype
# print vals, type(vals), vals.dtype
# print gs, type(gs), gs.dtype
newsmooth = self.fftconvolve(vals, gs)
self.line = Line2D(self.data.get_xdata(), newsmooth)
self.stack.append(self.line)
def lower_smooth(self):
if 1: #Check if current smooth is lowest possible
print "Cannot smooth less. Least smooth already active."
else:
pass#Set lesser smooth as current
def term(self, event):
self.active = False
self.disconnect()
def disconnect(self):
self.data.figure.canvas.mpl_disconnect(self.cidkpress)
我也试过解析 vals[0]
和 gs[0]
来检查我是否会解析两个长度为 101 的列表。结果我真的只会解析两个标量,而 ftconvolve`将退出并出现错误:TypeError: *: 'Smoother' 和 'float'.
看起来我正在解析 class 本身的一个实例。我就是看不出来。
如果它有助于我通过调用以下函数 class 测试我的
def smoothBF(datalist):
from matplotlib import pyplot as plt
for i in xrange(len(datalist)):
fig, axs = plt.subplots(nrows=1, ncols=1)
data, = axs.plot(datalist[i][0], datalist[i][1])
smoother = Smoother(data)
smoother.connect()
while smoother.active:
plt.pause(0.1)
#Return current result
plt.close(fig)
其中 datalist 是一个仅包含元组 (np.arange(101), np.random.random(101))
更新:似乎与在 class 定义中导入 fftconvolve 有关。添加一些 print 语句以获取 scipy fftconvolve 函数内的类型和维数确认 in1 不知何故是 平滑类型。但是当我在模块的顶部而不是在 class 定义中编写 from scipy.signal import fftconvolve
并调用 newsmooth = fftconvolve(vals, gs)
而不是 newsmooth = self.fftconvolve(vals, gs)
时,它也会给出不同的结果。然后我从 fftconvolve.
class 定义中包含 from scipy.signal import fftconvolve
的任何调用都会解析 class 本身的实例。在 class 定义之外进行导入并在我对 fftconvolve 的编辑中修复拼写错误(如我的问题更新中所述)使解析正确并且代码 运行.
你要在 class 定义中导入 fftconvolve
的 "trick" 最终会让你失望。除非您没有向我们展示您在别处定义 Smooth.fftconvolve
的位置。
这是您现在拥有的:
class Smooth(object):
from scipy.signal import fftconvolve
def enlarge_smooth(self):
# other stuff
self.fftconvolve(vals, gs)
当你打电话给
s = S()
s.enlarge_smooth()
fftconvolve
将被称为
fftconvolve(self, vals, gs)
解决办法很简单:不要做这种诡计。
相反,在 class 之外导入 fftconvolve
并直接调用该函数:
from scipy.signal import fftconvolve
class Smooth(object):
def enlarge_smooth(self):
# other stuff
fftconvolve(vals, gs)