简体   繁体   English

fit 包中的 __init__ 文件出错

[英]Error with the __init__ file from the fit package

I'm trying to run a code I found on GitHub to achieve piecewise linear representation.我正在尝试运行我在 GitHub 上找到的代码来实现分段线性表示。 The problem is that there seem to be an error in the init file of the fit package.问题是fit包的init文件似乎有错误。 Here's the code :这是代码:

from matplotlib.pylab import gca, figure, plot, subplot, title, xlabel, ylabel, xlim,show
from matplotlib.lines import Line2D
import segment
import fit

Pull = pandas.read_csv('pull_kpi.csv', parse_dates=['pull_date'])
data = Pull[['pull_date', 'qty_late']]

from matplotlib.pylab import gca, figure, plot, subplot, title, xlabel, ylabel, xlim,show
from matplotlib.lines import Line2D
import segment
import fit

def draw_plot(data,plot_title):
    plot(range(len(data)),data,alpha=0.8,color='red')
    title(plot_title)
    xlabel("Samples")
    ylabel("Signal")
    xlim((0,len(data)-1))

def draw_segments(segments):
    ax = gca()
    for segment in segments:
        line = Line2D((segment[0],segment[2]),(segment[1],segment[3]))
        ax.add_line(line)

with open("example_data/16265-normalecg.txt") as f:
    file_lines = f.readlines()

data = [float(x.split("\t")[2].strip()) for x in file_lines[100:320]]

max_error = 0.005

#sliding window with regression
figure()
segments = segment.slidingwindowsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data,"Sliding window with regression")
draw_segments(segments)

#bottom-up with regression
figure()
segments = segment.bottomupsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data,"Bottom-up with regression")
draw_segments(segments)

#top-down with regression
figure()
segments = segment.topdownsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data,"Top-down with regression")
draw_segments(segments)



#sliding window with simple interpolation
figure()
segments = segment.slidingwindowsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
draw_plot(data,"Sliding window with simple interpolation")
draw_segments(segments)

#bottom-up with  simple interpolation
figure()
segments = segment.bottomupsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
draw_plot(data,"Bottom-up with simple interpolation")
draw_segments(segments)

#top-down with  simple interpolation
figure()
segments = segment.topdownsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
draw_plot(data,"Top-down with simple interpolation")
draw_segments(segments)


show()

The error seems to be an invalid syntax and Python sais it occurs on the line "raise exc_type, exc_val, exc_tb".该错误似乎是无效的语法,Python 说它发生在“raise exc_type, exc_val, exc_tb”这一行。

Here is the part of the init file that creates a bug :这是创建错误的初始化文件的一部分:

    # File I/O methods

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if not exc_val:
            return self.close()

        raise exc_type, exc_val, exc_tb

Anybody can help please ?有人可以帮忙吗?

The problem is that the fit package from PyPI ( GitHub ) you've installed is not compatible with Python 3 (and indeed, has last been updated 8 years ago, anyway).问题是您安装的来自fit ( GitHub ) 的 fit 包与 Python 3 不兼容(事实上,最近一次更新是在 8 年前,无论如何)。 ( Someone else is having the same problem with Python 3 with it. ) 其他人对 Python 3 也有同样的问题。

However, that's not the package you want anyway – it doesn't even export things like sumsquared_error .然而,这不是你想要的包——它甚至不导出sumsquared_error类的东西。

Those exports could be found in this fit.py , that repository presumably being where you've adapted your code from.这些导出可以在fit.py中找到,该存储库可能是您改编代码的地方。

In other words:换句话说:

  • pip uninstall fit – it's not the droid you're looking for pip uninstall fit – 这不是您要找的机器人
  • copy fit.py and wrappers.py from that repository to your project's directoryfit.pywrappers.py从该存储库复制到您的项目目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM