I'm trying to run a code I found on GitHub to achieve piecewise linear representation. The problem is that there seem to be an error in the init file of the fit package. 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".
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). ( Someone else is having the same problem with Python 3 with it. )
However, that's not the package you want anyway – it doesn't even export things like sumsquared_error
.
Those exports could be found in this fit.py
, that repository presumably being where you've adapted your code from.
In other words:
pip uninstall fit
– it's not the droid you're looking for fit.py
and wrappers.py
from that repository to your project's directory
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.