/usr/share/pyshared/enthought/util/dp.py is in python-enthoughtbase 3.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought util package component>
#------------------------------------------------------------------------------
from numpy import arange, sqrt, argmax, zeros, nonzero, take, absolute
def decimate(x, y, tolerance):
""" Returns decimated x and y arrays.
This is Douglas and Peucker's algorithm rewritten to use Numeric arrays.
Tolerance is usually determined by determining the size that a single pixel
represents in the units of x and y.
Compression ratios for large seismic and well data sets can be significant.
"""
# Todo - we could improve the aesthetics by scaling (normalizing) the x and
# y arrays. eg in a well the curve varies by +/- 1 and the depths by 0,10000
# This affects the accuracy of the representation in sloping regions.
keep = zeros(len(x))
_decimate(x, y, keep, 0, len(x) - 1, tolerance)
ids = nonzero(keep)
return take(x,ids), take(y, ids)
def _decimate(x, y, keep, si, ei, tolerance):
keep[si] = 1
keep[ei] = 1
# check if the two data points are adjacent
if ei < (si + 2):
return
# now find the perp distance to each point
x0 = x[si+1:ei]
y0 = y[si+1:ei]
xei_minux_xsi = x[ei] - x[si]
yei_minux_ysi = y[ei] - y[si]
top = absolute( xei_minux_xsi * (y[si] - y0) - (x[si] - x0) * yei_minux_ysi )
# The algorithm currently does an expensive sqrt operation which is not
# strictly necessary except that it makes the tolerance correspond to a real
# world quantity.
bot = sqrt( xei_minux_xsi*xei_minux_xsi + yei_minux_ysi*yei_minux_ysi)
dist = top / bot
# find the point that is furthest from line between points si and ei
index = argmax(dist)
if dist[index] > tolerance:
abs_index = index + (si + 1)
keep[abs_index] = 1
_decimate(x, y, keep, si, abs_index, tolerance)
_decimate(x, y, keep, abs_index, ei, tolerance)
return
if __name__ == "__main__":
from numpy.random import random
x = arange(0,4,0.1)
y = zeros(len(x))
y = random(len(x))
tolerance = .1
print tolerance
nx,ny = decimate(x, y, tolerance)
print 'before ', len(x)
print 'after ', len(nx)
|