-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph-emerge-heatmap.py
executable file
·954 lines (795 loc) · 28.5 KB
/
graph-emerge-heatmap.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
#!/usr/bin/env python3
# Gentoo emerge Heatmap Generator
# Copyright (c) 2017 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import csv
import datetime as dt
import logging as log
import platform
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap as LSC
__description__ = 'Gentoo emerge Heatmap Generator'
DEFAULT_CSV = 'emerge.csv'
DEFAULT_WIDTH = 1280
DEFAULT_HEIGHT = 720
FIGURE_PARAMS = None
DEFAULT_RECT = [0.05, 0.1, 0.925, 0.825] # L, B, W, H
# from webpage, #62548f = rgb(98, 84, 143)
GENTOO_PURPLE1 = '#62548f'
GENTOO_PURPLE2 = '#dddaec'
GENTOO_PURPLES = np.array([98, 84, 143]) / 255
GENTOO_PURPLES = [[1.0] * 3, GENTOO_PURPLES]
GENTOO_PURPLES = LSC.from_list('Gentoo-Purples', GENTOO_PURPLES)
LOG_FORMAT = ('%(asctime)s.%(msecs)03d %(levelname)8s '
'%(funcName)s:%(lineno)d: %(message)s')
LOG_DATEFMT = '%H:%M:%S'
log.basicConfig(format=LOG_FORMAT, datefmt=LOG_DATEFMT, level='DEBUG')
# patch and insight for ax.xaxis and footer from:
# https://stackoverflow.com/a/17562747/242583 by tacaswell
# https://stackoverflow.com/a/30860027/242583 by eric
def _blit_draw(self, artists, bg_cache):
# Handles blitted drawing, which renders only the artists given instead
# of the entire figure.
updated_ax = []
for a in artists:
# If we haven't cached the background for this axes object, do
# so now. This might not always be reliable, but it's an attempt
# to automate the process.
if a.axes not in bg_cache:
# get bbox from a.axes.figure instead of a.axes
bbox = a.axes.figure.bbox
bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(bbox)
a.axes.draw_artist(a)
updated_ax.append(a.axes)
# After rendering all the needed artists, blit each axes individually.
for ax in set(updated_ax):
# get bbox from ax.figure instead of ax
ax.figure.canvas.blit(ax.figure.bbox)
# MONKEY PATCH!!
animation.Animation._blit_draw = _blit_draw
def read_emerges(csvfile):
emerges = []
for emerge in csv.reader(csvfile):
if emerge[0] == 'START':
continue
r = tuple(dt.datetime.fromtimestamp(float(ts)) for ts in emerge)
emerges.append(r)
return emerges
def bin_data(emerges):
'''Putting seconds into minute/hour bins for later data aggregation
Split am emerge time range (START, END) by minute or hour, e.g.,
14:03:21 to 14:05:13.
By minute, put
- 39 seconds in Bin 14:03,
- 60 seconds in Bin 14:04 and
- 13 seconds in Bin 14:05
By hour, put 112 seconds in Bin 14.
Returns a dict of the data:
- dts: list of dates (datetime.date)
- by_minute: array of minute bins (DAYS, minute bins)
- by_hour: array of hour bins (DAYS, hour bins)
'''
log.info('binning emerge sessions...')
bins = {}
BASE = emerges[0][0].replace(hour=0, minute=0, second=0)
td = emerges[-1][1] - BASE
DAYS = td.days + (td.seconds > 0)
dts = [BASE.date() + dt.timedelta(days=i) for i in range(DAYS)]
bins['dts'] = dts
bins['emerges'] = emerges
#############
# durations #
#############
sessions = np.array(emerges)
durations = [d.total_seconds() for d in sessions[:, 1] - sessions[:, 0]]
bins['durations'] = durations
###############
# bins_minute #
###############
# 24 * 60 = 1440 bins, bin width = 1 minute
# | 1st hour | | last hour |
# Day 1 [ 0 1 2 ... 59 ... 1380 ... 1439 ]
# Day 2 [ .............................. ]
# :
# DAYS [ .............................. ]
bins_minute = [x[:] for x in [[0] * 24 * 60] * DAYS]
for emerge in emerges:
fm = emerge[0]
to = emerge[1]
nt = fm.replace(second=0)
while fm <= to:
nt += dt.timedelta(minutes=1)
if nt > to:
n = (to - fm).seconds
else:
n = 60
bins_minute[(fm - BASE).days][fm.hour * 60 + fm.minute] += n
fm = nt
bins['by_minute'] = bins_minute
#############
# bins_hour #
#############
# 24 bins, bin width = 1 hour
# Day 1 [ 0 1 2 ... 23 ]
# Day 2 [ ............ ]
# :
# DAYS [ ............ ]
sum_hour = lambda mins: [sum(mins[i * 60:(i + 1) * 60]) for i in range(24)]
bins_hour = [sum_hour(day_minutes) for day_minutes in bins_minute]
bins['by_hour'] = bins_hour
############
# bins_day #
############
# 1 bin, bin width = 24 hour
# [ Day1 Day2 ... DAYS ]
bins['by_day'] = np.transpose(np.sum(bins_minute, axis=1))
return bins
def agg_likelihood(dts, bins, by_minute=False):
# 24 bins, bin width = 1 hour
# Mon [ 0 1 2 ... 23 ]
# Tue [ ............ ]
# :
# Sun [ ............ ]
# by_minute == True
# 24 * 60 = 1440 bins, bin width = 1 minute
# | 1st hour | | last hour |
# Mon [ 0 1 2 ... 59 ... 1380 ... 1439 ]
# Tue [ .............................. ]
# :
# Sun [ .............................. ]
#
# cbmax is the possible maximum of a minute = total weeks * 60. It has an
# error if DAYS % 7 != 0, but negligible if the number of weeks is high
# enough.
N = 24 * 60 if by_minute else 24
likelihood_by_weekday_timeofday = [x[:] for x in [[0] * N] * 7]
WD = dts[0].weekday()
for i in range(len(bins)):
wd = (i + WD) % 7
z = zip(likelihood_by_weekday_timeofday[wd], bins[i])
likelihood_by_weekday_timeofday[wd] = [x + y for x, y in z]
N = 60 if by_minute else 60 * 60
return {
'dts': dts,
'range': (dts[0], dts[-1]),
'data': likelihood_by_weekday_timeofday,
'cbmax': np.ceil(dts.size / 7) * N,
}
def agg_animated_likelihood(aggs, bins, by_minute=False):
dts = aggs['dts']
DAYS = dts.size
window_size = 52
interval = 1
windows = []
r1 = 0
msg = 'generating window at day index {}'
while r1 <= DAYS - window_size * 7:
log.info(msg.format(r1))
r2 = r1 + window_size * 7
windows.append(agg_likelihood(dts[r1:r2], bins[r1:r2], by_minute))
r1 += interval * 7
# last window
window = agg_likelihood(dts[-window_size:], bins[-window_size:], by_minute)
windows.append(window)
return windows
def agg_data(bins, figures):
'''Data aggregation
Returns a dict of the following data:
- dts: array of dates (datetime.date)
- start and end: start and end of data range
- see below
'''
log.info('aggregating data...')
aggs = {}
dts = np.array(bins['dts'])
aggs['dts'] = dts
fm = dts[0]
to = dts[-1]
agg_range = fm, to
aggs['range'] = agg_range
emerges = bins['emerges']
bins_minute = np.array(bins['by_minute'])
bins_hour = np.array(bins['by_hour'])
bins_day = np.array(bins['by_day'])
#############
# durations #
#############
aggs['durations'] = {
'dts': dts,
'range': agg_range,
'data': bins['durations'],
}
#####################
# likelihood_by_wtod #
######################
aggs['likelihood_by_wtod'] = agg_likelihood(dts, bins_hour)
#############################
# likelihood_by_wtod_minute #
#############################
agg = agg_likelihood(dts, bins_minute, by_minute=True)
aggs['likelihood_by_wtod_minute'] = agg
#######################
# animated_likelihood #
#######################
figure = 'animated_likelihood'
if figure in figures:
aggs[figure] = agg_animated_likelihood(aggs, bins_hour)
##############################
# animated_likelihood_minute #
##############################
figure = 'animated_likelihood_minute'
if figure in figures:
agg = agg_animated_likelihood(aggs, bins_minute, by_minute=True)
aggs[figure] = agg
##############
# historical #
##############
# Note:
# - February 29 is inserted to every year for alignments.
#
# Year 1 [ Jan1 Jan2 ... Feb29 ... Dec31 ]
# Year 2 [ ............................. ]
# :
# YEARS [ ............................. ]
YEARS = to.year - fm.year + 1
YEAR_NUMBERS = range(fm.year, to.year + 1)
aggs['YEAR_NUMBERS'] = YEAR_NUMBERS
YEAR_LABELS = list(map(str, YEAR_NUMBERS))
aggs['YEAR_LABELS'] = YEAR_LABELS
b = np.array(list(d.month != 2 or d.day != 29 for d in dts))
nb = np.logical_not(b)
# taking leaf days out and reshape into (YEARS, 365)
pad = (fm.replace(year=2001) - fm.replace(year=2001, month=1, day=1)).days
noleaf = np.hstack((np.zeros(pad), bins_day[b]))
noleaf.resize(YEARS, 365)
# copy noleaf into final array in (YEAR, 366)
# 59 = 31 + 28, 60 = 31 + 29
Feb29 = np.zeros((YEARS, 1))
historical = np.hstack((noleaf[:, :59], Feb29, noleaf[:, 59:]))
# putting leaf days back
leaf_dts = np.array(dts)[nb]
leaf_days = bins_day[nb]
for d, day in zip(leaf_dts, leaf_days):
historical[d.year - fm.year, 59] = day
aggs['historical'] = {
'dts': dts,
'range': agg_range,
'data': historical,
}
###############
# yearly_days #
###############
g = lambda y: (1 for d in dts if d.year == YEAR_NUMBERS[y])
yearly_days = np.array(list(sum(g(y)) for y in range(YEARS)))
##########
# yearly #
##########
yearly_seconds = np.sum(historical, axis=1)
yearly = yearly_seconds / 3600
aggs['yearly'] = {
'dts': dts,
'range': agg_range,
'data': yearly,
}
###############
# norm_yearly #
###############
norm_yearly = np.array(yearly) / yearly_days * 365
aggs['norm_yearly'] = {
'dts': dts,
'range': agg_range,
'data': norm_yearly,
}
###############
# yearly_runs #
###############
yearly_runs = np.zeros(YEARS)
years = list(y.year for y in np.array(emerges)[:, 0])
yearly_runs = list(sum(e == y for e in years) for y in YEAR_NUMBERS)
yearly_runs = np.array(yearly_runs)
aggs['yearly_runs'] = {
'dts': dts,
'range': agg_range,
'data': yearly_runs,
}
####################
# norm_yearly_runs #
####################
norm_yearly_runs = yearly_runs / yearly_days * 365
aggs['norm_yearly_runs'] = {
'dts': dts,
'range': agg_range,
'data': norm_yearly_runs,
}
#######################
# norm_average_per_run_by_year #
#######################
# in minute
norm_average_per_run_by_year = norm_yearly / norm_yearly_runs * 60
aggs['norm_average_per_run_by_year'] = {
'dts': dts,
'range': agg_range,
'data': norm_average_per_run_by_year,
}
#########################
# daily_average_by_year #
#########################
daily_average_by_year = yearly_seconds / 60
for y in range(YEARS):
days = sum(1 for d in dts if d.year == YEAR_NUMBERS[y])
daily_average_by_year[y] /= days
aggs['daily_average_by_year'] = {
'dts': dts,
'range': agg_range,
'data': daily_average_by_year,
}
#####################
# by_year_timeofday #
#####################
# 24 * 60 = 1440 bins, bin width = 1 minute
# | 1st hour | | last hour |
# Year 1 [ 0 1 2 ... 59 ... 1380 ... 1439 ]
# Year 2 [ .............................. ]
# :
# YEARS [ .............................. ]
by_year_timeofday = np.zeros((YEARS, 24 * 60))
for y in range(YEARS):
b = np.array(list(d.year == y + fm.year for d in dts))
year_minutes = bins_minute[b]
tsum = np.sum(year_minutes.reshape(-1, 24 * 60), axis=0)
by_year_timeofday[y] = tsum
aggs['by_year_timeofday'] = {
'dts': dts,
'range': agg_range,
'data': by_year_timeofday,
}
###################
# by_year_weekday #
###################
# Heatmap over weekdays by years
#
# 7 * 24 * 60 = 10,080 bins, bin width = 1 minute
# | Monday | | Sunday |
# Year 1 [ 0 1 ... 1439 ... 8640 ... 10079 ]
# Year 2 [ ............................... ]
# :
# YEARS [ ............................... ]
by_year_weekday = np.zeros((YEARS, 7 * 24 * 60))
for y in range(YEARS):
b = np.array(list(d.year == y + fm.year for d in dts))
minutes = bins_minute[b]
# padding to align
start_weekday = dts[b][0].weekday()
if start_weekday:
minutes = np.vstack((np.zeros((start_weekday, 24 * 60)), minutes))
minutes.resize((np.ceil(minutes.size / (7 * 24 * 60)), 7 * 24 * 60))
by_year_weekday[y] = np.sum(minutes, axis=0)
aggs['by_year_weekday'] = {
'dts': dts,
'range': agg_range,
'data': by_year_weekday,
}
return aggs
def footer_text(name, start, end):
diff = end - start
days = diff.days + 1
text = 'Range: {} to {} ({:,} days)'.format(start, end, days)
if name:
text += ' / Machine: {}'.format(name)
text += ' / Generated by GeHG'
return text
def plot_footer(ax, *args):
text = footer_text(*args)
footer = ax.text(0.9875, 0.025, text, color='gray',
transform=plt.gcf().transFigure,
horizontalalignment='right', verticalalignment='center')
return footer
def plot_heatmap(raw_data, title, ax_props=None, more_props=None):
more_props = {} if more_props is None else more_props
rows = len(ax_props['yticklabels'])
cbmax = raw_data.get('cbmax', None)
# normalized
MAX = np.max(raw_data['data'])
data = raw_data['data'] / MAX
fig = plt.figure()
fig.suptitle(title, fontsize=18)
rect = np.array(DEFAULT_RECT) + more_props['rect_adjust']
if cbmax:
rect += [0, 0.1, 0, -0.1]
ax = plt.axes(rect)
animated = more_props.get('animated', False)
IMSHOW_OPTS = {
'aspect': 'auto',
'cmap': GENTOO_PURPLES,
'interpolation': 'none',
'animated': animated,
}
imdata = ax.imshow(data, **IMSHOW_OPTS)
ax.xaxis.set_animated(animated)
ax.yaxis.set_animated(animated)
ax.set(yticks=np.arange(rows), **ax_props)
# don't show yticks
ax.tick_params(axis='y', length=0)
prop_name = 'last-xlabel-right-align'
if prop_name in more_props:
xlabels = ax.xaxis.get_ticklabels()
xlabels[-1].set_horizontalalignment('right')
prop_name = 'xminorticks'
if prop_name in more_props:
ax.xaxis.set_ticks(more_props[prop_name], minor=True)
if more_props.get('no-grid', False):
ax.tick_params(axis='x', length=0)
else:
ax.grid(axis='x', which='major', linestyle='-')
ax.grid(axis='x', which='minor', linestyle=':')
footer = plot_footer(ax, more_props['name'], *raw_data['range'])
footer.set_animated(animated)
if cbmax is None:
return fig, imdata, footer
# draw the scale / colorbar
rect[1] = 0.1
rect[3] = 0.05
ax = plt.axes(rect)
N = MAX / cbmax
colorbar = np.linspace(0, 1, 256)
colorbar = np.vstack((colorbar, colorbar))
ax.imshow(colorbar, **IMSHOW_OPTS)
ax.set_xlim(left=0, right=255.0)
ax.set_ylim(top=-0.5, bottom=0.5)
ax.yaxis.set_ticks([0])
ax.yaxis.set_ticklabels([more_props['cbmax_label']])
ax.tick_params(axis='y', length=0)
xlabels = list('{:%}'.format(x) for x in np.arange(5) / 4 * N)
ax.xaxis.set_ticks(np.hstack((np.arange(4) / 4 * 256, [255])))
ax.xaxis.set_ticklabels(xlabels)
ax.xaxis.set_animated(animated)
ax.yaxis.set_animated(animated)
prop_name = 'last-xlabel-right-align-cbmax'
if prop_name in more_props:
xlabels = ax.xaxis.get_ticklabels()
xlabels[-1].set_horizontalalignment('right')
ax.grid(axis='x', which='major', linestyle='-')
return fig, imdata, footer
def update_animated_heatmap(window, name, axes, im, footer, redraws):
data = window['data']
cbmax = window['cbmax']
MAX = np.max(data)
im.set_data(data / MAX)
ax_im, ax = axes
N = MAX / cbmax
xlabels = list('{:%}'.format(x) for x in np.arange(5) / 4 * N)
ax.xaxis.set_ticklabels(xlabels)
footer.set_text(footer_text(name, *window['range']))
return redraws
def plot_animated_heatmap(windows, title, ax_props=None, more_props=None):
fig, im, footer = plot_heatmap(windows[0], title, ax_props, more_props)
axes = fig.get_axes()
ax1, ax2 = axes
redraws = [im, footer, ax1.xaxis, ax1.yaxis, ax2.xaxis, ax2.yaxis]
anim = animation.FuncAnimation(
fig,
update_animated_heatmap,
frames=windows,
fargs=(more_props['name'], axes, im, footer, redraws),
interval=250,
blit=True,
)
return anim,
def plot_barh(raw_data, title, ax_props=None, more_props=None):
data = raw_data['data']
fig = plt.figure()
fig.suptitle(title, fontsize=18)
rect = np.array(DEFAULT_RECT) + more_props['rect_adjust']
ax = plt.axes(rect)
ypos = np.arange(data.size)
ax.barh(ypos, data, color=GENTOO_PURPLE1, edgecolor=GENTOO_PURPLE2,
align='center')
ax.invert_yaxis()
ax.set(yticks=ypos, **ax_props)
ax.grid(which='major', axis='x')
plot_footer(ax, more_props['name'], *raw_data['range'])
return fig,
def plot_histogram(raw_data, title, ax_props=None, more_props=None):
data = raw_data['data']
fig = plt.figure()
fig.suptitle(title, fontsize=18)
rect = np.array(DEFAULT_RECT) + more_props['rect_adjust']
ax = plt.axes(rect)
bins = [5, 10, 20, 30, 60, 120, 180, 300, 600, 1200, 1800, 3600, 7200,
10800, 3600 * 5]
ax.hist(data, bins=bins, normed=True, histtype='step',
color=GENTOO_PURPLE1, cumulative=True)
ax.set(**ax_props)
ax.grid(which='both', axis='both')
plot_footer(ax, more_props['name'], *raw_data['range'])
return fig,
def init_figure_params():
global FIGURE_PARAMS
WKD_LABELS = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday')
O24_LABELS = ('00:00', '06:00', '12:00', '18:00', '23:59')
O24_MAJORTICKS = list(range(0, 24 * 60, 6 * 60)) + [24 * 60 - 1]
O24_MINORTICKS = range(0, 24 * 60, 3 * 60)
H24_LABELS = list(O24_LABELS[:-1]) + ['23:00']
H24_MAJORTICKS = list(range(0, 24, 6)) + [24 - 1]
H24_MINORTICKS = range(0, 24, 3)
MONTH_DAYS = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
MONTH_LOCS = np.cumsum(np.array(MONTH_DAYS))
MONTH_LOCS = np.hstack(([0], MONTH_LOCS[:-1]))
MONTH_LABELS = ('January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November',
'December')
WKD_MAJORTICKS = range(0, 7 * 24 * 60, 24 * 60)
FIGURE_PARAMS = {
'durations': [
plot_histogram,
'Cumulative Gentoo emerge Running Duration Step Histogram',
{
'xlim': 0,
'xlabel': 'Running Duration (second)',
'ylabel': 'Likelihood of emerges',
'ylim': (0, 1),
},
{
'rect_adjust': [0] * 4,
},
],
'historical': [
plot_heatmap,
'Historical Gentoo emerge Running Time',
{
'xlim': 0,
'xticks': MONTH_LOCS,
'xticklabels': MONTH_LABELS,
},
{
'rect_adjust': [0] * 4,
},
],
'likelihood_by_wtod': [
plot_heatmap,
'Gentoo emerge Running Likelihood by Weekday and Time of Day',
{
'xlim': (-0.5, 23.5),
'xticks': H24_MAJORTICKS,
'xticklabels': H24_LABELS,
'yticklabels': WKD_LABELS,
},
{
'cbmax_label': 'Likelihood',
'last-xlabel-right-align-cbmax': True,
'no-grid': True,
'rect_adjust': [0.0375, 0, -0.0375, 0],
'xminorticks': H24_MINORTICKS,
},
],
'likelihood_by_wtod_minute': [
plot_heatmap,
'Gentoo emerge Running Likelihood by Weekday and Time of Day',
{
'xlim': 0,
'xticks': O24_MAJORTICKS,
'xticklabels': O24_LABELS,
'yticklabels': WKD_LABELS,
},
{
'cbmax_label': 'Likelihood',
'last-xlabel-right-align': True,
'last-xlabel-right-align-cbmax': True,
'rect_adjust': [0.0375, 0, -0.0375, 0],
'xminorticks': O24_MINORTICKS,
},
],
'animated_likelihood': [
plot_animated_heatmap,
('Animated Gentoo emerge Running Likelihood by Weekday and '
'Time of Day'),
{
'xlim': (-0.5, 23.5),
'xticks': H24_MAJORTICKS,
'xticklabels': H24_LABELS,
'yticklabels': WKD_LABELS,
},
{
'animated': True,
'cbmax_label': 'Likelihood',
'last-xlabel-right-align-cbmax': True,
'no-grid': True,
'rect_adjust': [0.0375, 0, -0.0375, 0],
'xminorticks': H24_MINORTICKS,
},
],
'animated_likelihood_minute': [
plot_animated_heatmap,
('Animated Gentoo emerge Running Likelihood by Weekday and '
'Time of Day'),
{
'xlim': 0,
'xticks': O24_MAJORTICKS,
'xticklabels': O24_LABELS,
'yticklabels': WKD_LABELS,
},
{
'animated': True,
'cbmax_label': 'Likelihood',
'last-xlabel-right-align': True,
'last-xlabel-right-align-cbmax': True,
'rect_adjust': [0.0375, 0, -0.0375, 0],
'xminorticks': O24_MINORTICKS,
},
],
'by_year_timeofday': [
plot_heatmap,
'Gentoo emerge Running Time by Year and Time of Day',
{
'xlim': 0,
'xticks': O24_MAJORTICKS,
'xticklabels': O24_LABELS,
},
{
'last-xlabel-right-align': True,
'last-xlabel-right-align-cbmax': True,
'rect_adjust': [0] * 4,
'xminorticks': O24_MINORTICKS,
},
],
'by_year_weekday': [
plot_heatmap,
'Gentoo emerge Running Time by Year and Weekday',
{
'xlim': 0,
'xticks': WKD_MAJORTICKS,
'xticklabels': WKD_LABELS,
},
{
'rect_adjust': [0] * 4,
},
],
'yearly': [
plot_barh,
'Yearly Gentoo emerge Running Time',
{
'xlabel': 'Yearly Running Time (hour)',
'xlim': 0,
},
{
'rect_adjust': [0, 0.025, 0, -0.025],
},
],
'norm_yearly': [
plot_barh,
'Normalized Yearly Gentoo emerge Running Time',
{
'xlabel': 'Normalized Running Time (hour)',
'xlim': 0,
},
{
'rect_adjust': [0, 0.025, 0, -0.025],
},
],
'yearly_runs': [
plot_barh,
'Yearly Gentoo emerge Runs',
{
'xlabel': 'Normalized Runs',
'xlim': 0,
},
{
'rect_adjust': [0, 0.025, 0, -0.025],
},
],
'norm_yearly_runs': [
plot_barh,
'Normalized Yearly Gentoo emerge Runs',
{
'xlabel': 'Normalized Runs',
'xlim': 0,
},
{
'rect_adjust': [0, 0.025, 0, -0.025],
},
],
'norm_average_per_run_by_year': [
plot_barh,
'Normalized Gentoo emerge Average Running Time Per Run by Year',
{
'xlabel': 'Normalized Average Running Time Per Run (minute)',
'xlim': 0,
},
{
'rect_adjust': [0, 0.025, 0, -0.025],
},
],
'daily_average_by_year': [
plot_barh,
'Gentoo emerge Daily Average Running Time by Year',
{
'xlabel': 'Daily Average Running Time (minute)',
'xlim': 0,
},
{
'rect_adjust': [0, 0.025, 0, -0.025],
},
],
}
def plot_graphs(aggs, args):
YEAR_LABELS = aggs['YEAR_LABELS']
for figure in ('historical', 'by_year_timeofday', 'by_year_weekday',
'yearly', 'norm_yearly', 'yearly_runs', 'norm_yearly_runs',
'norm_average_per_run_by_year', 'daily_average_by_year'):
FIGURE_PARAMS[figure][2]['yticklabels'] = YEAR_LABELS
for figure in FIGURE_PARAMS:
rect = DEFAULT_RECT.copy()
FIGURE_PARAMS[figure][3]['rect'] = rect
FIGURE_PARAMS[figure][3]['name'] = args.name
for name in args.figures:
item = FIGURE_PARAMS[name]
plot_func = item[0]
plot_args = item[1:]
log.info('plotting {}...'.format(name))
figure = plot_func(aggs[name], *plot_args)[0]
if args.figsave:
log.info('saving {}...'.format(name))
filename = '%s/GeHG-%s' % (args.saveto, name)
if plot_args[2].get('animated', False):
# figure.save(filename + '.gif', writer='imagemagick', fps=4)
# For GIF, needs about 3G memory to run it comfortably, perhaps
# using hours_bin would help a bit.
figure.save(filename + '.mp4', fps=26, bitrate=3000)
else:
figure.savefig(filename + '.png')
if not args.noshow:
plt.show()
def main():
init_figure_params()
parser = argparse.ArgumentParser(description=__description__)
parser.add_argument('-f', nargs='+', default=['all'], dest='figures',
choices=['all'] + list(FIGURE_PARAMS.keys()),
help='figures to generate (default %(default)s)')
parser.add_argument('-W', '--width', type=int, default=DEFAULT_WIDTH,
help='width of figures (default: %(default)s)')
parser.add_argument('-H', '--height', type=int, default=DEFAULT_HEIGHT,
help='height of figures (default: %(default)s)')
parser.add_argument('-S', '--noshow', action='store_true',
help='do not show figures')
parser.add_argument('-s', '--figsave', action='store_true',
help='save figures as images')
parser.add_argument('-t', '--saveto', default='/tmp',
help='where to save images (default: %(default)s)')
parser.add_argument('-n', '--name', default=platform.uname().node,
help=('name for identification '
'(default network name: %(default)s)'))
parser.add_argument('csvfile', nargs='?', type=open, default=DEFAULT_CSV)
args = parser.parse_args()
if 'all' in args.figures:
args.figures = list(FIGURE_PARAMS.keys())
args.saveto = args.saveto.rstrip('/')
emerges = read_emerges(args.csvfile)
bins = bin_data(emerges)
aggs = agg_data(bins, args.figures)
dpi = plt.rcParams['figure.dpi']
plt.rcParams['savefig.dpi'] = dpi
plt.rcParams["figure.figsize"] = (args.width / dpi, args.height / dpi)
plot_graphs(aggs, args)
if __name__ == '__main__':
main()