-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_gradefigure.py
150 lines (136 loc) · 6.36 KB
/
test_gradefigure.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Tests for module gradefigure.
"""
import unittest
import numpy
from matplotlib import pyplot
import gradefigure
class TestGradeFigure(unittest.TestCase):
def setUp(self):
self.x = numpy.linspace(0.0, 1.0, 11)
self.y1 = numpy.exp(self.x**2)
self.y2 = numpy.sin(self.x)
def _figure_vanilla(self, title='', xlabel='', ylabel='', legend=False,
data=[], labels=()):
pyplot.figure()
if title:
pyplot.title(title)
if xlabel:
pyplot.xlabel(xlabel)
if ylabel:
pyplot.ylabel(ylabel)
for i, (x, y) in enumerate(data):
label = '' if len(labels) <= i else labels[i]
pyplot.plot(x, y, label=label)
if legend:
pyplot.legend()
fig = list(map(pyplot.figure, pyplot.get_fignums()))[-1]
return fig
def _figure_complex(self, title='', xlabel='', ylabel='', legend=False,
data=[], labels=()):
fig = pyplot.figure(figsize=(6.0, 6.0))
ax = fig.add_axes((0.0, 0.1, 0.8, 0.8))
if xlabel:
ax.set_xlabel(xlabel, fontsize=16)
if ylabel:
ax.set_ylabel(ylabel, fontsize=16)
for i, (x, y) in enumerate(data):
label = '' if len(labels) <= i else labels[i]
ax.plot(x, y, label=label, linewidth=1, linestyle='-')
if legend:
ax.legend(loc='upper left', prop={'size': 16})
if title:
fig.text(0.0, 0.0, title,
horizontalalignment='left', multialignment='left', fontsize=14)
return fig
def test_ax_has_title(self):
fig = self._figure_vanilla(title='title', data=[(self.x, self.y1)])
self.assertTrue(gradefigure.ax_has_title(fig.get_axes()[0]))
fig = self._figure_vanilla(data=[(self.x, self.y1)])
self.assertFalse(gradefigure.ax_has_title(fig.get_axes()[0]))
fig = self._figure_vanilla(title='', data=[(self.x, self.y1)])
self.assertFalse(gradefigure.ax_has_title(fig.get_axes()[0]))
def test_ax_has_xlabel(self):
fig = self._figure_vanilla(xlabel='x', data=[(self.x, self.y1)])
self.assertTrue(gradefigure.ax_has_xlabel(fig.get_axes()[0]))
fig = self._figure_vanilla(data=[(self.x, self.y1)])
self.assertFalse(gradefigure.ax_has_xlabel(fig.get_axes()[0]))
fig = self._figure_vanilla(xlabel='', data=[(self.x, self.y1)])
self.assertFalse(gradefigure.ax_has_xlabel(fig.get_axes()[0]))
def test_ax_has_ylabel(self):
fig = self._figure_vanilla(ylabel='y', data=[(self.x, self.y1)])
self.assertTrue(gradefigure.ax_has_ylabel(fig.get_axes()[0]))
fig = self._figure_vanilla(data=[(self.x, self.y1)])
self.assertFalse(gradefigure.ax_has_ylabel(fig.get_axes()[0]))
fig = self._figure_vanilla(ylabel='', data=[(self.x, self.y1)])
self.assertFalse(gradefigure.ax_has_ylabel(fig.get_axes()[0]))
def test_ax_has_legend(self):
fig = self._figure_vanilla(legend=True,
data=[(self.x, self.y1)], labels=('data 1'))
self.assertTrue(gradefigure.ax_has_legend(fig.get_axes()[0]))
fig = self._figure_vanilla(legend=False,
data=[(self.x, self.y1)], labels=('data 1'))
self.assertFalse(gradefigure.ax_has_legend(fig.get_axes()[0]))
fig = self._figure_vanilla(legend=True,
data=[(self.x, self.y1)], labels=(''))
self.assertFalse(gradefigure.ax_has_legend(fig.get_axes()[0]))
fig = self._figure_vanilla(legend=True, data=[(self.x, self.y1)])
self.assertFalse(gradefigure.ax_has_legend(fig.get_axes()[0]))
fig = self._figure_vanilla(data=[(self.x, self.y1)])
self.assertFalse(gradefigure.ax_has_legend(fig.get_axes()[0]))
def test_ax_has_data(self):
fig = self._figure_vanilla(data=[(self.x, self.y1)])
self.assertTrue(gradefigure.ax_has_data(fig.get_axes()[0],
self.x, self.y1))
self.assertFalse(gradefigure.ax_has_data(fig.get_axes()[0],
self.x, self.y2))
fig = self._figure_vanilla(data=[(self.x, self.y1), (self.x, self.y2)])
self.assertTrue(gradefigure.ax_has_data(fig.get_axes()[0],
self.x, self.y1))
self.assertTrue(gradefigure.ax_has_data(fig.get_axes()[0],
self.x, self.y2))
def test_gradefigure_vanilla(self):
fig = self._figure_vanilla(title='title', xlabel='x', ylabel='y',
legend=True,
data=[(self.x, self.y1), (self.x, self.y2)],
labels=('data 1', 'data 2'))
ax_items, ax_data = [], []
ans, _ = gradefigure.grade_figure(fig, ax_items=ax_items, ax_data=ax_data)
self.assertIsNone(ans)
for item in ['title', 'xlabel', 'ylabel', 'legend']:
ax_items.append(item)
ans, _ = gradefigure.grade_figure(fig,
ax_items=ax_items, ax_data=ax_data)
self.assertEqual(ans, 100.0)
for data in [(self.x, self.y1), (self.x, self.y2)]:
ax_data.append(data)
ans, _ = gradefigure.grade_figure(fig,
ax_items=ax_items, ax_data=ax_data)
self.assertEqual(ans, 100.0)
def test_gradefigure_complex(self):
fig = self._figure_complex(title='title', xlabel='x', ylabel='y',
legend=True,
data=[(self.x, self.y1), (self.x, self.y2)],
labels=('data 1', 'data 2'))
ax_items, ax_data = [], []
ans, _ = gradefigure.grade_figure(fig, ax_items=ax_items, ax_data=ax_data)
self.assertIsNone(ans)
for item in ['title', 'xlabel', 'ylabel', 'legend']:
ax_items.append(item)
ans, _ = gradefigure.grade_figure(fig,
ax_items=ax_items, ax_data=ax_data,
title_or_text=True)
self.assertEqual(ans, 100.0)
if item == 'title':
ans, log = gradefigure.grade_figure(fig,
ax_items=ax_items, ax_data=ax_data)
self.assertFalse(log['items']['title'])
self.assertEqual(ans, 100.0 * (1.0 - 1 / len(ax_items)))
for data in [(self.x, self.y1), (self.x, self.y2)]:
ax_data.append(data)
ans, _ = gradefigure.grade_figure(fig,
ax_items=ax_items, ax_data=ax_data,
title_or_text=True)
self.assertEqual(ans, 100.0)
if __name__ == '__main__':
unittest.main()