-
Notifications
You must be signed in to change notification settings - Fork 1
/
osm_editor_graphs.py
executable file
·90 lines (74 loc) · 3.12 KB
/
osm_editor_graphs.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
#!/usr/bin/env python3
import argparse
import os
import re
from matplotlib import pyplot
def parse_num(num_str):
"""
Convert a string into a number.
The string may contain spaces.
"""
return int(num_str.replace(' ', ''))
def read_files(editor, files):
"""
Read *.lst files and extract relevant usage information about editor.
Returns lists of tuples (date, number) for changesets, user, and edits statistics.
"""
by_changesets = []
by_users = []
by_edits = []
for filename in files:
with open(filename) as f:
text = f.read()
results = re.findall(editor + r": ([\d\s]+)\(\d+.\d+%\)", text)
date = os.path.splitext(os.path.basename(filename))[0]
by_changesets.append((date, parse_num(results[0])))
by_users.append((date, parse_num(results[1])))
by_edits.append((date, parse_num(results[2])))
by_changesets.sort(key=lambda x: x[0])
by_users.sort(key=lambda x: x[0])
by_edits.sort(key=lambda x: x[0])
return by_changesets, by_users, by_edits
def plot_data(data):
"""
Plot and return a graph out of a list of tuples (x-label, y-point).
"""
fig, ax = pyplot.subplots(figsize=(13, 5))
x_data = range(len(data))
y_data = [x[1] for x in data]
ax.plot(x_data, y_data, marker='o', linestyle='dashed')
for i, j in zip(x_data, y_data):
ax.annotate(str(j), xy=(i, j), xytext=(-10, 4), textcoords='offset points')
ax.set_xticks(range(len(data)))
ax.set_xticklabels([x[0] for x in data])
return fig, ax
def main(args):
"""
Main function of editor graph creation script.
Read some .lst files, plot graphs, export to files.
"""
by_changesets, by_users, by_edits = read_files(args.editor, args.files)
pyplot.rc('font', size=11)
fig, ax = plot_data(by_changesets)
fig.suptitle(f"Number of Changesets done with {args.editor}")
ax.set_xlabel("Date")
ax.set_ylabel("Number of Changesets")
fig.savefig(os.path.join(args.outdir, f"{args.editor}_changesets.{args.format}"))
fig, ax = plot_data(by_users)
fig.suptitle(f"Distinct Users of {args.editor}")
ax.set_xlabel("Date")
ax.set_ylabel("Number of Distinct Users")
fig.savefig(os.path.join(args.outdir, f"{args.editor}_users.{args.format}"))
fig, ax = plot_data(by_edits)
fig.suptitle(f"Number of Edits done with {args.editor}")
ax.set_xlabel("Date")
ax.set_ylabel("Number of Edits")
fig.savefig(os.path.join(args.outdir, f"{args.editor}_edits.{args.format}"))
if __name__ == '__main__':
argparser = argparse.ArgumentParser(description="Create usage graphs for a given OpenStreetMap editor")
argparser.add_argument('-e', '--editor', required=True, help="OSM editor to create graphs for")
argparser.add_argument('-f', '--format', choices=['png', 'pdf', 'svg'], default='png', help="graph file format")
argparser.add_argument('-o', '--outdir', default='./', help="output folder for graphs")
argparser.add_argument('files', nargs='+', help="the .lst files generated by the editor-stats tool")
parsed_args = argparser.parse_args()
main(parsed_args)