File: view_comparison_graph.py

package info (click to toggle)
synfigstudio 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 162,048 kB
  • sloc: cpp: 208,474; javascript: 25,487; ansic: 13,216; python: 7,509; sh: 6,391; makefile: 2,999; objc: 1,400; csh: 486; perl: 238; ruby: 73; xml: 11
file content (134 lines) | stat: -rwxr-xr-x 4,053 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python3
#
# Be sure to read the `test_render_all_perf.py` script first.  You'll need two
# generated CSV files from that script.  You'll also need matplotlib/pyplot
# installed.
#
# This script takes two arguments, the filepaths of two runs.  The first one
# provided is considered the "reference" run.  The second is the "test" run.
# Interpreting the graph is pretty simple,  Whichever bar is short means that
# it was faster. Make sure you have enough passes from the
# `test_render_all_perf.py` script (default should be set to 10).  The `mean`
# of all runs will be plotted as a bar's value.  But black line will appear
# on the edge of the bar's tip, as to mark the varriance/error.

import sys
import matplotlib.pyplot as plt
import csv
import statistics

plt.style.use('ggplot')


def main():
    if len(sys.argv) < 3:
        print('Please supply at least two CSV files of runs')
        print('  First one should be the `master` reference, second is the comparison')
        sys.exit(0)

    run_data_filenames = sys.argv[1:]

    # First one is the reference
    ref_run_filename = run_data_filenames[0]

    # Chart data
    sif_files = []
    ref_mean_runs = []
    ref_fastest_runs = []
    ref_slowest_runs = []

    # Get the data (for the reference run)
    past_first_row = False
    with open(ref_run_filename) as csvfile:
        r = csv.reader(csvfile)

        for row in r:
            # First row only has meta-data
            if not past_first_row:
                past_first_row = True
                continue

            # Else, we've got data, first column is the filename
            sif_files.append(row[0])

            # The rest are the runs of the data
            all_runs = [float(x) for x in row[1:]]
            mean_run = statistics.mean(all_runs)
            fastest_run = min(all_runs)
            slowest_run = max(all_runs)

            ref_mean_runs.append(mean_run)
            ref_fastest_runs.append(mean_run - fastest_run)
            ref_slowest_runs.append(slowest_run - mean_run)

    # test run
    # TODO, this should be put in a reusable function!!
    test_run_filename = run_data_filenames[1]
    test_mean_runs = []
    test_fastest_runs = []
    test_slowest_runs = []
    past_first_row = False

    with open(test_run_filename) as csvfile:
        r = csv.reader(csvfile)

        for row in r:
            # First row only has meta-data
            if not past_first_row:
                past_first_row = True
                continue

            # The rest are the runs of the data
            all_runs = [float(x) for x in row[1:]]
            mean_run = statistics.mean(all_runs)
            fastest_run = min(all_runs)
            slowest_run = max(all_runs)

            test_mean_runs.append(mean_run)
            test_fastest_runs.append(mean_run - fastest_run)
            test_slowest_runs.append(slowest_run - mean_run)

    # Reverse the order (os that we have 000 at the top, and NNN at the bottom)
    for x in [sif_files, ref_mean_runs, ref_fastest_runs, ref_slowest_runs, test_mean_runs, test_fastest_runs, test_slowest_runs]:
        x.reverse()

    # Create and show the graph
    sif_spacing = 2
    bar_spacing = 0
    bar_height = 0.5
    y = [(x * sif_spacing) for x in range(0, len(sif_files))]

    # Plot the Reference
    plt.barh(
        y,
        ref_mean_runs,
        bar_height,
        xerr=[ref_fastest_runs, ref_slowest_runs],
        color='darkorchid',
        label='Reference',
    )

    # Plot the test
    y = [(x - bar_height - bar_spacing) for x in y]
    plt.barh(
        y,
        test_mean_runs,
        bar_height,
        xerr=[test_fastest_runs, test_slowest_runs],
        color='dodgerblue',
        label='Test',
    )

    # Rest of the chart
    y = [(x + (bar_height / 2)) for x in y]
    plt.xlabel('Run time (seconds), smaller is better')
    plt.ylabel('.sif file')
    plt.yticks(y, sif_files)
    plt.title('Run Time Comparison')
    plt.legend(loc='best')

    plt.show()


if __name__ == '__main__':
    main()