2
# Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
4
# Jockey is free software; you can redistribute it and/or modify it
5
# under the terms of the GNU General Public License as published by the
6
# Free Software Foundation; either version 2, or (at your option) any
9
# Jockey is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18
def _convert_item(v, typ, line):
22
except ValueError: # non-number
29
raise ValueError, "Can't convert %s to int; line=%s" % (v, line)
34
raise ValueError, "Can't convert %s to float; line=%s" % (v, line)
38
raise ValueError, "Unknown conversion type, type=%s; line=%s" % (typ,line)
40
def parse_line(line, delim):
41
if delim.find("%") < 0:
42
return [ _convert_item(item, "a", None) for item in line.split(delim) ]
45
idx = 0 # indexes delim
49
while idx < len(delim):
51
raise ValueError, "bad delimitor: '" + delim + "'"
55
while idx < len(delim) and delim[idx] != '%':
58
xx = line.split(sep, 1)
59
data.append(_convert_item(xx[0], ch, line))
67
for item in line.split(sep):
68
data.append(_convert_item(item, ch, line))
71
def escape_string(str):
72
return str.replace("/", "//")
74
def extract_rows(data, *rows):
75
"""Extract rows specified in the argument list.
77
>>> chart_data.extract_rows([[10,20], [30,40], [50,60]], 1, 2)
82
# return [data[r] for r in rows]
88
raise IndexError, "data=%s rows=%s" % (data, rows)
91
def extract_columns(data, *cols):
92
"""Extract columns specified in the argument list.
94
>>> chart_data.extract_columns([[10,20], [30,40], [50,60]], 0)
100
# return [ [r[c] for c in cols] for r in data]
107
raise IndexError, "data=%s col=%s" % (data, col)
113
def moving_average(data, xcol, ycol, width):
114
"""Compute the moving average of YCOL'th column of each sample point
115
in DATA. In particular, for each element I in DATA,
116
this function extracts up to WIDTH*2+1 elements, consisting of
117
I itself, WIDTH elements before I, and WIDTH
118
elements after I. It then computes the mean of the YCOL'th
119
column of these elements, and it composes a two-element sample
120
consisting of XCOL'th element and the mean.
122
>>> data = [[10,20], [20,30], [30,50], [40,70], [50,5]]
123
... chart_data.moving_average(data, 0, 1, 1)
124
[(10, 25.0), (20, 33.333333333333336), (30, 50.0), (40, 41.666666666666664), (50, 37.5)]
126
The above value actually represents:
128
[(10, (20+30)/2), (20, (20+30+50)/3), (30, (30+50+70)/3),
129
(40, (50+70+5)/3), (50, (70+5)/2)]
136
for i in range(len(data)):
139
for j in range(i-width, i+width+1):
140
if j >= 0 and j < len(data):
141
total += data[j][ycol]
143
out.append((data[i][xcol], float(total) / n))
145
raise IndexError, "bad data: %s,xcol=%d,ycol=%d,width=%d" % (data,xcol,ycol,width)
149
def filter(func, data):
150
"""Parameter <func> must be a single-argument
151
function that takes a sequence (i.e.,
152
a sample point) and returns a boolean. This procedure calls <func> on
153
each element in <data> and returns a list comprising elements for
154
which <func> returns True.
156
>>> data = [[1,5], [2,10], [3,13], [4,16]]
157
... chart_data.filter(lambda x: x[1] % 2 == 0, data)
167
def transform(func, data):
168
"""Apply <func> on each element in <data> and return the list
169
consisting of the return values from <func>.
171
>>> data = [[10,20], [30,40], [50,60]]
172
... chart_data.transform(lambda x: [x[0], x[1]+1], data)
173
[[10, 21], [30, 41], [50, 61]]
181
def aggregate_rows(data, col):
182
out = copy.deepcopy(data)
190
return s.strip() == ""
192
def fread_csv(fd, delim = ','):
193
"""This function is similar to read_csv, except that it reads from
194
an open file handle <fd>, or any object that provides method "readline".
196
fd = open("foo", "r")
197
data = chart_data.fread_csv(fd, ",") """
202
if line[0] != '#' and not empty_line_p(line):
203
data.append(parse_line(line, delim))
207
def read_csv(path, delim = ','):
208
"""This function reads
209
comma-separated values from file <path>. Empty lines and lines
210
beginning with "#" are ignored. Parameter <delim> specifies how
211
a line is separated into values. If it does not contain the
212
letter "%", then <delim> marks the end of a value.
213
Otherwise, this function acts like scanf in C:
215
chart_data.read_csv("file", "%d,%s:%d")
217
Paramter <delim> currently supports
218
only three conversion format specifiers:
219
"d"(int), "f"(double), and "s"(string)."""
222
data = fread_csv(f, delim)
226
def fwrite_csv(fd, data):
227
"""This function writes comma-separated <data> to <fd>. Parameter <fd> must be a file-like object
228
that supports the |write()| method."""
230
fd.write(",".join([str(x) for x in v]))
233
def write_csv(path, data):
234
"""This function writes comma-separated values to <path>."""
239
def read_str(delim = ',', *lines):
240
"""This function is similar to read_csv, but it reads data from the
243
fd = open("foo", "r")
244
data = chart_data.read_str(",", fd.readlines())"""
248
com = parse_line(line, delim)
252
def func(f, xmin, xmax, step = None):
253
"""Create sample points from function <f>, which must be a
254
single-parameter function that returns a number (e.g., math.sin).
255
Parameters <xmin> and <xmax> specify the first and last X values, and
256
<step> specifies the sampling interval.
258
>>> chart_data.func(math.sin, 0, math.pi * 4, math.pi / 2)
259
[(0, 0.0), (1.5707963267948966, 1.0), (3.1415926535897931, 1.2246063538223773e-16), (4.7123889803846897, -1.0), (6.2831853071795862, -2.4492127076447545e-16), (7.8539816339744828, 1.0), (9.4247779607693793, 3.6738190614671318e-16), (10.995574287564276, -1.0)]
266
step = (xmax - xmin) / 100.0
268
data.append((x, f(x)))
272
def _nr_data(data, col):
278
def median(data, freq_col=1):
279
"""Compute the median of the <freq_col>'th column of the values is <data>.
281
>>> chart_data.median([(10,20), (20,4), (30,5)], 0)
283
>>> chart_data.median([(10,20), (20,4), (30,5)], 1)
287
nr_data = _nr_data(data, freq_col)
288
median_idx = nr_data / 2
294
raise Exception, "??? median ???"
296
def cut_extremes(data, cutoff_percentage, freq_col=1):
297
nr_data = _nr_data(data, freq_col)
298
min_idx = nr_data * cutoff_percentage / 100.0
299
max_idx = nr_data * (100 - cutoff_percentage) / 100.0
305
if i + d[freq_col] >= min_idx:
307
x[freq_col] = x[freq_col] - (min_idx - i)
311
elif i + d[freq_col] >= max_idx:
312
if i < max_idx and i + d[freq_col] >= max_idx:
314
x[freq_col] = x[freq_col] - (max_idx - i)
321
def mean(data, val_col, freq_col):
325
sum += d[val_col] * d[freq_col]
326
nr_data += d[freq_col]
328
raise IndexError, "data is empty"
330
return sum / float(nr_data)
332
def mean_samples(data, xcol, ycollist):
333
"""Create a sample list that contains
334
the mean of the original list.
336
>>> chart_data.mean_samples([ [1, 10, 15], [2, 5, 10], [3, 8, 33] ], 0, (1, 2))
337
[(1, 12.5), (2, 7.5), (3, 20.5)]
340
numcol = len(ycollist)
346
out.append( (elem[xcol], float(v) / numcol) )
348
raise IndexError, "bad data: %s,xcol=%d,ycollist=%s" % (data,xcol,ycollist)
352
def stddev_samples(data, xcol, ycollist, delta = 1.0):
353
"""Create a sample list that contains the mean and standard deviation of the original list. Each element in the returned list contains following values: [MEAN, STDDEV, MEAN - STDDEV*delta, MEAN + STDDEV*delta].
355
>>> chart_data.stddev_samples([ [1, 10, 15, 12, 15], [2, 5, 10, 5, 10], [3, 32, 33, 35, 36], [4,16,66, 67, 68] ], 0, range(1,5))
356
[(1, 13.0, 2.1213203435596424, 10.878679656440358, 15.121320343559642), (2, 7.5, 2.5, 5.0, 10.0), (3, 34.0, 1.5811388300841898, 32.418861169915807, 35.581138830084193), (4, 54.25, 22.094965489902897, 32.155034510097103, 76.344965489902904)]
359
numcol = len(ycollist)
365
mean = float(total) / numcol
368
variance += (mean - elem[col]) ** 2
369
stddev = math.sqrt(variance / numcol) * delta
370
out.append( (elem[xcol], mean, stddev, mean-stddev, mean+stddev) )
375
raise IndexError, "bad data: %s,xcol=%d,ycollist=%s" % (data,xcol,ycollist)
378
def nearest_match(data, col, val):
383
if min_delta == None or abs(d[col] - val) < min_delta:
384
min_delta = abs(d[col] - val)
386
pychart_util.warn("XXX ", match)