Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
NickFoubert committed Feb 27, 2012
0 parents commit 64f2900
Show file tree
Hide file tree
Showing 6 changed files with 7,935 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Simple Sequence Segmenting
==========================

This repository contains Python code I wrote for segmenting 1-D time series. In other words,
it can be used for transforming a time series into a piecewise linear represenation.
The algorithms are Python implementations of the "classical" algorithms, as described in
[An Online Algorithm for Segmenting Time Series][keogh], including:

- the sliding window algorithm;
- the top-down algorithm; and
- the bottom-up algorithm.

The code is *not* optimized for performance in any way, but I've found it useful for
experimenting and data exploration.

Requirements
------------

The segmenting algorithms use NumPy's least squares fitting routine, so naturally it depends on NumPy.

Example
-------

You can run the code to see example output by running the example.py script. The script
requires [matplotlib][mpl] to display the plots.

The example uses ECG data I found on an [ECG data site][ecg].


References
----------

[keogh]: http://www.cs.ucr.edu/~eamonn/icdm-01.pdf
[numpy]: http://numpy.scipy.org
[mpl]: http://matplotlib.sourceforge.net
[ecg]: http://myweb.msoe.edu/~martynsc/signals/ecg/ecg.html
66 changes: 66 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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()

Loading

0 comments on commit 64f2900

Please sign in to comment.