| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
Visualizes stacked timer output in as a hierarchical pie chart. |
| 5 |
|
| 6 |
For it to work, this script needs Matplotlib and the python package |
| 7 |
from https://github.com/klieret/pyplot-hierarchical-pie |
| 8 |
|
| 9 |
This script is not tested, so please report if it stops working. |
| 10 |
""" |
| 11 |
|
| 12 |
import re |
| 13 |
from argparse import ArgumentParser |
| 14 |
try: |
| 15 |
from hpie import HPie, stringvalues_to_pv, Path |
| 16 |
except ImportError: |
| 17 |
print("This scripts needs the python package from https://github.com/klieret/pyplot-hierarchical-pie") |
| 18 |
raise |
| 19 |
import matplotlib.pyplot as plt |
| 20 |
|
| 21 |
|
| 22 |
parser = ArgumentParser(description="Plot hierarchical pie chart for stacked timers. Left click for zooming in, right click for zooming out.") |
| 23 |
parser.add_argument('-r', '--plotRemainders', help="Plot remainder lines as well.", |
| 24 |
dest="plotRemainders", action="store_true", |
| 25 |
default=False) |
| 26 |
parser.add_argument('-n', '--non-interactive', help="Disable interactive features", |
| 27 |
dest='non_interactive', action="store_true", default=False) |
| 28 |
parser.add_argument('logFile', type=str, help="Log file with stacked timers") |
| 29 |
options = parser.parse_args() |
| 30 |
|
| 31 |
|
| 32 |
# parse log file with stacked timer output |
| 33 |
timerRegExp = re.compile(r'([\s|]*)(.*):\s([0-9]*(\.[0-9]*)?)\s-\s([0-9]*(\.[0-9]*)?)%') |
| 34 |
with open(options.logFile) as f: |
| 35 |
data = {} |
| 36 |
prevDepth = -1 |
| 37 |
stack = [] |
| 38 |
for line in f: |
| 39 |
match = timerRegExp.findall(line) |
| 40 |
if match: |
| 41 |
match = match[0] |
| 42 |
separator = match[0] |
| 43 |
label = match[1] |
| 44 |
if label == 'Remainder' and not options.plotRemainders: |
| 45 |
continue |
| 46 |
time = float(match[2]) |
| 47 |
depth = separator.count('|') |
| 48 |
stack = stack[:depth-1]+[label] |
| 49 |
prevDepth = depth |
| 50 |
data['/'.join(stack)] = time |
| 51 |
if len(stack) > 1: |
| 52 |
data['/'.join(stack[:-1])] -= time |
| 53 |
|
| 54 |
# create plot |
| 55 |
dataAll = stringvalues_to_pv(data) |
| 56 |
if not options.non_interactive: |
| 57 |
|
| 58 |
global hp, base |
| 59 |
|
| 60 |
ax = plt.gca() |
| 61 |
hp = HPie(dataAll, ax) |
| 62 |
hp.plot(setup_axes=True, interactive=True) |
| 63 |
|
| 64 |
# set up left and right click actions |
| 65 |
base = Path([]) |
| 66 |
|
| 67 |
def onClick(event): |
| 68 |
global hp, base |
| 69 |
if event.inaxes == hp.axes and event.button == 1: |
| 70 |
# left click |
| 71 |
for path in hp.wedges: |
| 72 |
cont, ind = hp.wedges[path].contains(event) |
| 73 |
if cont: |
| 74 |
path = Path(base[:]+path[:]) |
| 75 |
data = {p[len(path)-1:]: time for p, time in dataAll.items() if p.startswith(path)} |
| 76 |
ax.clear() |
| 77 |
hp = HPie(data, ax) |
| 78 |
hp.plot(setup_axes=True, interactive=True) |
| 79 |
ax.figure.canvas.draw_idle() |
| 80 |
base = Path(path[:-1]) |
| 81 |
break |
| 82 |
elif event.button == 3: |
| 83 |
# right click |
| 84 |
if len(base) > 0: |
| 85 |
path = base |
| 86 |
data = {p[len(path)-1:]: time for p, time in dataAll.items() if p.startswith(path)} |
| 87 |
else: |
| 88 |
path = Path([]) |
| 89 |
data = dataAll |
| 90 |
ax.clear() |
| 91 |
hp = HPie(data, ax) |
| 92 |
hp.plot(setup_axes=True, interactive=True) |
| 93 |
ax.figure.canvas.draw_idle() |
| 94 |
base = Path(path[:-1]) |
| 95 |
|
| 96 |
ax.figure.canvas.mpl_connect('button_press_event', onClick) |
| 97 |
else: |
| 98 |
ax = plt.gca() |
| 99 |
hp = HPie(dataAll, ax) |
| 100 |
hp.plot(setup_axes=True) |
| 101 |
|
| 102 |
plt.show() |