我试图创建一个使用一个类fftconvolve从scipy.signal与类实例的方法中高斯进行卷积一些数据。但是,每次创建实例并调用方法expand_smooth(在按右箭头键时发生)时,我从fftconvolve收到一条错误消息,指出:ValueError:in1和in2应该具有相同的维数。def fftconvolve(in1, in2, mode="full"):
当该行的elif not in1.ndim == in2.ndim:
值为True时,这就会在函数中发生。不过我行print vals.ndim == gs.ndim
打印真正调用之前fftconvolve,都丘壑和GS的尺寸是(101)。所以,如果我不解析瓦尔斯和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:不支持的操作数类型*:“平滑”和“浮动”。
好像我正在解析该类本身的实例。我只是不知道如何。
如果它有助于通过即时通讯测试我的班级对以下函数的调用
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))
更新:这似乎与在类定义中导入fftconvolve有关。添加一些打印语句以获取scipy fftconvolve函数内部的尺寸类型和数量,这表明in1某种程度上是一种更平滑的类型。但是,当我from scipy.signal import fftconvolve
在模块顶部newsmooth = fftconvolve(vals, gs)
而不是在类定义内部调用而不是时,它也会产生不同的结果newsmooth = self.fftconvolve(vals, gs)
。然后我收到错误消息AttributeError:'numpy.ndarray'对象没有来自fftconvolve的属性'ndims'。
您要fftconvolve
在类定义中导入的“技巧”最终会使您感到困惑。除非您没有向我们展示您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)
解决方案很简单:不要做这种骗术。相反,请导入fftconvolve
类外部,然后直接调用该函数:
from scipy.signal import fftconvolve
class Smooth(object):
def enlarge_smooth(self):
# other stuff
fftconvolve(vals, gs)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句