-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsplot.py
executable file
·479 lines (382 loc) · 15.4 KB
/
tsplot.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/env python
#coding: utf-8
from __future__ import print_function
import argparse
import json
import numpy as np
import sys
import re
import logging
import matplotlib as M
import matplotlib.pyplot as P
import numbers
from functools import reduce
from distutils.version import LooseVersion
from itertools import chain, islice, cycle
# Run-time check for matplot lib version for line style functionality.
if LooseVersion(M.__version__)<LooseVersion("1.5.0"):
logging.critical("require matplotlib version ≥ 1.5.0")
sys.exit(1)
# Read timeseries data from multiple files, plot each in one panel, with common
# time axis, and optionally sharing a vertical axis as governed by the configuration.
def parse_clargs():
def float_or_none(s):
try: return float(s)
except ValueError: return None
def parse_range_spec(s):
l, r = (float_or_none(x) for x in s.split(','))
return (l,r)
def parse_colour_spec(s):
colour, tests = s.split(':',1)
tests = tests.split(',')
return colour, tests
P = argparse.ArgumentParser(description='Plot time series data on one or more graphs.')
P.add_argument('inputs', metavar='FILE', nargs='+',
help='time series data in JSON format')
P.add_argument('-A', '--abscissa', metavar='AXIS', dest='axis',
help='use values from AXIS instead of \'time\' as abscissa')
P.add_argument('-t', '--trange', metavar='RANGE', dest='trange',
type=parse_range_spec,
help='restrict time axis to RANGE (see below)')
P.add_argument('-g', '--group', metavar='KEY,...', dest='groupby',
type=lambda s: s.split(','),
help='plot series with same KEYs on the same axes')
P.add_argument('-s', '--select', metavar='EXPR,...', dest='select',
type=lambda s: s.split(','),
action='append',
help='select only series matching EXPR')
P.add_argument('-c', '--colour', metavar='COLOUR:EXPR,...', dest='colours',
type=parse_colour_spec,
action='append',
help='use colour COLOUR a base for series matching EXPR')
P.add_argument('-o', '--output', metavar='FILE', dest='outfile',
help='save plot to file FILE')
P.add_argument('-l', '--list', action='store_true',
help='list selected time-series')
P.add_argument('--dpi', metavar='NUM', dest='dpi',
type=int,
help='set dpi for output image')
P.add_argument('--scale', metavar='NUM', dest='scale',
type=float,
help='scale size of output image by NUM')
P.add_argument('-x', '--exclude', metavar='NUM', dest='exclude',
type=float,
help='remove extreme points outside NUM times the 0.9-interquantile range of the median')
P.epilog = 'A range is specifed by a pair of floating point numbers min,max where '
P.epilog += 'either may be omitted to indicate the minimum or maximum of the corresponding '
P.epilog += 'values in the data.'
P.epilog += '\n'
P.epilog += 'Filter expressions are of the form KEY=VALUE. (Might add other ops later.)'
# modify args to avoid argparse having a fit when it encounters an option
# argument of the form '<negative number>,...'
argsbis = [' '+a if re.match(r'-[\d.]',a) else a for a in sys.argv[1:]]
return P.parse_args(argsbis)
def isstring(s):
return isinstance(s,str) or isinstance(s,unicode)
def take(n, s):
return islice((i for i in s), 0, n)
class TimeSeries:
def __init__(self, ts, ys, **kwargs):
self.t = np.array(ts)
n = self.t.shape[0]
self.y = np.full_like(self.t, np.nan)
ny = min(len(ys), len(self.y))
self.y[:ny] = ys[:ny]
self.meta = dict(kwargs)
self.ex_ts = None
def trestrict(self, bounds):
clip = range_meet(self.trange(), bounds)
self.t = np.ma.masked_outside(self.t, v1=clip[0], v2=clip[1])
self.y = np.ma.masked_array(self.y, mask=self.t.mask)
def exclude_outliers(self, iqr_factor):
yfinite = np.ma.masked_invalid(self.y).compressed()
l_, lq, median, uq, u_ = np.percentile(yfinite, [0, 5.0, 50.0, 95.0, 100])
lb = median - iqr_factor*(uq-lq)
ub = median + iqr_factor*(uq-lq)
np_err_save = np.seterr(all='ignore')
yex = np.ma.masked_where(np.isfinite(self.y)&(self.y<=ub)&(self.y>=lb), self.y)
np.seterr(**np_err_save)
tex = np.ma.masked_array(self.t, mask=yex.mask)
self.ex_ts = TimeSeries(tex.compressed(), yex.compressed())
self.ex_ts.meta = dict(self.meta)
self.y = np.ma.filled(np.ma.masked_array(self.y, mask=~yex.mask), np.nan)
def excluded(self):
return self.ex_ts
def name(self):
return self.meta.get('name',"") # value of 'name' attribute in source
def label(self):
return self.meta.get('label',"") # name of column in source
def units(self):
return self.meta.get('units',"")
def trange(self):
return self.t.min(), self.t.max()
def yrange(self):
return self.y.min(), self.y.max()
def run_select(expr, v):
m = re.match(r'([^=>!<~]+)(>=|<=|>|<|!=|=|!~|~)(.*)', expr)
if not m:
return True
key, op, test = m.groups()
if not key in v:
return False
val = v[key]
if op=='~':
return test in str(val)
elif op=='!~':
return test not in str(val)
else:
if isinstance(val, numbers.Number):
if re.match(r'true$', test, re.I):
test=True
elif re.match(r'false$', test, re.I):
test=False
else:
try:
test=int(test)
except ValueError:
test=float(test)
if op=='=':
return val==test
elif op=='!=':
return val!=test
elif op=='<':
return val<test
elif op=='>':
return val>test
elif op=='<=':
return val<=test
elif op=='>=':
return val>=test
else:
return False
def read_json_timeseries(j, axis='time', select=[]):
# Convention:
#
# Time series data is represented by an object with a subobject 'data' and optionally
# other key/value entries comprising metadata for the time series.
#
# The 'data' object holds one array of numbers 'time' and zero or more other
# numeric arrays of sample values corresponding to the values in 'time'. The
# names of these other arrays are taken to be the labels for the plots.
#
# Units can be specified by a top level entry 'units' which is either a string
# (units are common for all data series in the object) or by a map that
# takes a label to a unit string.
# If given a list instead of a hash, collect time series from each entry.
ts_list = []
if isinstance(j, list):
for o in j:
ts_list.extend(read_json_timeseries(o, axis, select))
return ts_list
try:
jdata = j['data']
ncol = len(jdata)
times = jdata[axis]
nsample = len(times)
except KeyError:
# This wasn't a time series after all.
return ts_list
def units(label):
try:
unitmap = j['units']
if isstring(unitmap):
return unitmap
else:
return unitmap[label]
except:
return ""
i = 1
for key in jdata.keys():
if key==axis: continue
meta = j.copy()
meta.update({'label': key, 'data': None, 'units': units(key)})
del meta['data']
if not select or any([all([run_select(s, meta) for s in term]) for term in select]):
ts_list.append(TimeSeries(times, jdata[key], **meta))
return ts_list
def min_(a,b):
if a is None:
return b
elif b is None:
return a
else:
return min(a,b)
def range_join(r, s):
return (min_(r[0], s[0]), max(r[1], s[1]))
def range_meet(r, s):
return (max(r[0], s[0]), min_(r[1], s[1]))
class PlotData:
def __init__(self, key_label=""):
self.series = []
self.group_key_label = key_label
def trange(self):
return reduce(range_join, [s.trange() for s in self.series])
def yrange(self):
return reduce(range_join, [s.yrange() for s in self.series])
def name(self):
return reduce(lambda n, s: n or s.name(), self.series, "")
def group_label(self):
return self.group_key_label
def unique_labels(self, formatter=lambda x: x):
# attempt to create unique labels for plots in the group based on
# meta data
labels = [s.label() for s in self.series]
if len(labels)<2:
return labels
n = len(labels)
keyset = reduce(lambda k, s: k.union(s.meta.keys()), self.series, set())
keyi = iter(keyset)
try:
while len(set(labels)) != n:
k = next(keyi)
if k=='label':
continue
vs = [s.meta.get(k,None) for s in self.series]
if len(set(vs))==1:
continue
for i in range(n):
prefix = '' if k=='name' else k+'='
if vs[i] is not None:
labels[i] += u', '+k+u'='+unicode(formatter(vs[i]))
except StopIteration:
pass
return labels
# Input: list of TimeSeries objects; collection of metadata keys to group on
# Return list of plot info (time series, data extents, metadata), one per plot.
def gather_ts_plots(tss, groupby):
group_lookup = {}
plot_groups = []
for ts in tss:
key = tuple([ts.meta.get(g) for g in groupby])
if key is () or None in key or key not in group_lookup:
pretty_key=', '.join([unicode(k)+u'='+unicode(v) for k,v in zip(groupby, key) if v is not None])
pd = PlotData(pretty_key)
pd.series = [ts]
plot_groups.append(pd)
group_lookup[key] = len(plot_groups)-1
else:
plot_groups[group_lookup[key]].series.append(ts)
return plot_groups
def make_palette(cm_name, n, cmin=0, cmax=1):
smap = M.cm.ScalarMappable(M.colors.Normalize(cmin/float(cmin-cmax),(cmin-1)/float(cmin-cmax)),
M.cm.get_cmap(cm_name))
return [smap.to_rgba((2*i+1)/float(2*n)) for i in range(n)]
def round_numeric_(x):
# Helper to round numbers in labels
if not isinstance(x,float): return x
return "{:6g}".format(x)
def plot_plots(plot_groups, axis='time', colour_overrides=[], save=None, dpi=None, scale=None):
nplots = len(plot_groups)
plot_groups = sorted(plot_groups, key=lambda g: g.group_label())
# use same global time scale for all plots
trange = reduce(range_join, [g.trange() for g in plot_groups])
# use group names for titles?
group_titles = any((g.group_label() for g in plot_groups))
figure = P.figure()
for i in range(nplots):
group = plot_groups[i]
plot = figure.add_subplot(nplots, 1, i+1)
title = group.group_label() if group_titles else group.name()
plot.set_title(title)
# y-axis label: use timeseries label and units if only
# one series in group, otherwise use a legend with labels,
# and units alone on the axes. At most two different unit
# axes can be drawn.
def ylabel(unit):
if len(group.series)==1:
lab = group.series[0].label()
if unit:
lab += ' (' + unit + ')'
else:
lab = unit
return lab
uniq_units = list(set([s.units() for s in group.series]))
uniq_units.sort()
if len(uniq_units)>2:
logging.warning('more than two different units on the same plot')
uniq_units = uniq_units[:2]
# store each series in a slot corresponding to one of the units,
# together with a best-effort label
series_by_unit = [[] for i in range(len(uniq_units))]
unique_labels = group.unique_labels(formatter=round_numeric_)
for si in range(len(group.series)):
s = group.series[si]
label = unique_labels[si]
try:
series_by_unit[uniq_units.index(s.units())].append((s,label))
except ValueError:
pass
# TODO: need to find a scheme of colour/line allocation that is
# double y-axis AND greyscale friendly.
palette = \
[make_palette(cm, n, 0, 0.5) for
cm, n in zip(['hot', 'winter'], [len(x) for x in series_by_unit])]
lines = cycle(["-",(0,(3,1))])
first_plot = True
for ui in range(len(uniq_units)):
if not first_plot:
plot = plot.twinx()
axis_color = palette[ui][0]
plot.set_ylabel(ylabel(uniq_units[ui]), color=axis_color)
for l in plot.get_yticklabels():
l.set_color(axis_color)
plot.get_yaxis().get_major_formatter().set_useOffset(False)
plot.get_yaxis().set_major_locator(M.ticker.MaxNLocator(nbins=6))
plot.set_xlim(trange)
colours = cycle(palette[ui])
line = next(lines)
for s, l in series_by_unit[ui]:
c = next(colours)
for colour, tests in colour_overrides:
if all([run_select(t, s.meta) for t in tests]):
c = colour
plot.plot(s.t, s.y, color=c, ls=line, label=l)
# treat exluded points especially
ex = s.excluded()
if ex is not None:
ymin, ymax = s.yrange()
plot.plot(ex.t, np.clip(ex.y, ymin, ymax), marker='x', ls='', color=c)
if first_plot:
plot.legend(loc=2, fontsize='small')
plot.grid()
else:
plot.legend(loc=1, fontsize='small')
first_plot = False
# adapted from http://stackoverflow.com/questions/6963035
axis_ymin = min([ax.get_position().ymin for ax in figure.axes])
figure.text(0.5, axis_ymin - float(3)/figure.dpi, axis, ha='center', va='center')
if save:
if scale:
base = figure.get_size_inches()
figure.set_size_inches((base[0]*scale, base[1]*scale))
figure.savefig(save, dpi=dpi)
else:
P.show()
args = parse_clargs()
tss = []
axis = args.axis if args.axis else 'time'
for filename in args.inputs:
select = args.select
with open(filename) as f:
j = json.load(f)
tss.extend(read_json_timeseries(j, axis, select))
if args.list:
for ts in tss:
print('name:', ts.meta['name'])
print('label:', ts.meta['label'])
for k in [x for x in sorted(ts.meta.keys()) if x not in ['name', 'label']]:
print(k+':', ts.meta[k])
print()
else:
if args.trange:
for ts in tss:
ts.trestrict(args.trange)
if args.exclude:
for ts in tss:
ts.exclude_outliers(args.exclude)
groupby = args.groupby if args.groupby else []
plots = gather_ts_plots(tss, groupby)
if not args.outfile:
M.interactive(False)
colours = args.colours if args.colours else []
plot_plots(plots, axis=axis, colour_overrides=colours, save=args.outfile, dpi=args.dpi, scale=args.scale)