-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_optimization.py
94 lines (74 loc) · 3.53 KB
/
test_optimization.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
""" Optimization tests in form of graphs. For checking how our optimization performs"""
from timeit import default_timer as timer
from Bio import pairwise2
from lib import optimized_pairwise2
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import os.path
from itertools import cycle
from testdata.test_sequences import get_test_sequences_pairs
PLOT_SAFE_FOLDER = "plots"
def fullname(o):
""" Returns a full module name of an object"""
return o.__module__ + "." + o.__name__
def get_times(methods, seq1, seq2, *args, **kwargs):
""" Measures execution times of provided methods for given arguments
Returns tuple (length, times), where length = average length of seq1 and seq2
(useful in further computation)"""
run_times = 3
times = []
for method in methods:
start = timer()
for _ in xrange(run_times):
method(seq1, seq2, *args, **kwargs)
end = timer()
time = ((end - start) * 1000) / run_times
times.append(time)
length = (len(seq1) + len(seq2)) / 2
return length, times
def save_plot(directory, filename):
""" Saves current plot in given directory """
if not os.path.exists(directory):
os.makedirs(directory)
plt.savefig(os.path.join(directory, filename))
def run_compare_test(description, compared_methods, *args, **kwargs):
""" Runs test comparing our execution time with original. Plots result as a graph. """
results = []
for s in get_test_sequences_pairs(['performance']):
print("Testing {0}...".format(s[0]))
result = get_times(compared_methods, s[1], s[2], *args, **kwargs)
results.append(result)
results.sort(key=lambda x: x[0])
fig = plt.figure()
fig.canvas.set_window_title(description)
plt.title(description)
colors = cycle(['r','g','b','yellow','magenta','cyan'])
plot_legends = []
for i, method in enumerate(compared_methods):
color = next(colors)
plt.plot([r[0] for r in results], [r[1][i] for r in results], 'bo', color=color)
plt.plot([r[0] for r in results], [r[1][i] for r in results], color=color)
plot_legends.append(mpatches.Patch(color=color, label=fullname(method)))
plt.ylabel('Execution time (ms)')
plt.xlabel('Length of sequences')
plt.legend(handles=plot_legends)
save_plot(PLOT_SAFE_FOLDER, description)
print("Plot saved in \"{0}\"".format(PLOT_SAFE_FOLDER))
plt.show()
if __name__ == "__main__":
# Uncomment/Comment for choosing what to test
print("Test Bio.pairwise2 itself...")
run_compare_test("Test Bio.pairwise2 itself...", [pairwise2.align.globalxx, pairwise2.align.localxx])
run_compare_test("Test Bio.pairwise2 itself (score only)...", [pairwise2.align.globalxx, pairwise2.align.localxx], score_only=True)
description = "Compare localxx methods (score only)"
print(description)
run_compare_test(description, [pairwise2.align.localxx, optimized_pairwise2.align.localxx], score_only=True)
description = "Compare localxx methods (including alignments)"
print(description)
run_compare_test(description, [pairwise2.align.localxx, optimized_pairwise2.align.localxx])
description = "Compare globalxx methods (score only)"
print(description)
run_compare_test(description, [pairwise2.align.globalxx, optimized_pairwise2.align.globalxx], score_only=True)
description = "Compare globalxx methods (including alignments)"
print(description)
run_compare_test(description, [pairwise2.align.globalxx, optimized_pairwise2.align.globalxx])