Created
October 16, 2020 21:53
-
-
Save Witiko/1f92c84b030f7ed2e5ff2b67a4710409 to your computer and use it in GitHub Desktop.
Creates a pie chart from a GNU Parallel joblog after running OCR-D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from itertools import dropwhile | |
import json | |
import re | |
import sys | |
import matplotlib.pyplot as plt | |
JOBLOG_FILENAME = sys.argv[1] | |
OUTPUT_FILENAME = sys.argv[2] | |
COMMAND_REGEX = re.compile(r'.* -O ([^" ]*).*|.* (ocrd-import) .*') | |
LABEL_MAP = { | |
"ocrd-import": "ocrd-import", | |
"OCR-D-BIN": "olena-binarize", | |
"OCR-D-CROP": "anybaseocr-crop", | |
"OCR-D-BIN2": "olena-binarize", | |
"OCR-D-BIN-DENOISE": "cis-ocropy-denoise", | |
"OCR-D-BIN-DENOISE-DESKEW": "cis-ocropy-deskew", | |
"OCR-D-SEG-REG": "tesserocr-segment-region", | |
"OCR-D-SEG-REPAIR": "segment-repair", | |
"OCR-D-SEG-REG-DESKEW": "cis-ocropy-deskew", | |
"OCR-D-SEG-REG-DESKEW-CLIP": "cis-ocropy-clip", | |
"OCR-D-SEG-LINE": "cis-ocropy-segment", | |
"OCR-D-SEG-REPAIR-LINE": "segment-repair", | |
"OCR-D-SEG-LINE-RESEG-DEWARP": "cis-ocropy-dewarp", | |
"OCR-D-OCR": "calamari-recognize", | |
"OCR-D-TEXT": "fileformat-transform", | |
"OCR-D-HOCR": "fileformat-transform", | |
} | |
def read_joblog(filename): | |
successfully_read = 0 | |
with open(filename, 'rt') as f: | |
lines = iter(f) | |
next(lines) | |
for line in lines: | |
line = line.rstrip('\r\n').split('\t') | |
runtime = float(line[3]) | |
exit_code = int(line[6]) | |
command = line[-1] | |
successfully_read += 1 | |
yield (command, runtime, exit_code) | |
def evaluate(): | |
runtimes = {} | |
joblog = read_joblog(JOBLOG_FILENAME) | |
for command, runtime, exit_code in joblog: | |
if exit_code != 0: | |
continue | |
match = re.fullmatch(COMMAND_REGEX, command) | |
command = list(dropwhile(lambda x: x is None, match.groups()))[0] | |
label = LABEL_MAP[command] | |
if label not in runtimes: | |
runtimes[label] = [] | |
runtimes[label].append(runtime) | |
return runtimes | |
def pie_chart(runtimes): | |
labels, sizes = zip(*runtimes.items()) | |
sizes = list(map(sum, sizes)) | |
colors = plt.get_cmap('tab20').colors | |
patches, texts = plt.pie(sizes, colors=colors) | |
plt.legend(patches, labels, loc='best') | |
plt.axis('equal') | |
plt.tight_layout() | |
plt.savefig(OUTPUT_FILENAME) | |
def main(): | |
runtimes = evaluate() | |
pie_chart(runtimes) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are right, we could simplify things and get rid of
LABEL_MAP
. Then again, the solution withCOMMAND_REGEX
andLABEL_MAP
is more general, will work for many sorts of joblogs beside OCR-D (in my code base, bothLABEL_MAP
andCOMMAND_REGEX
are command-line arguments), and allows you to name the labels independently on the command names. For example, you could mergecis-ocropy-clip
,cis-ocropy-segment
, andcis-ocropy-dewarp
into a single slice namedcis-ocropy
: