-
Notifications
You must be signed in to change notification settings - Fork 1
/
gradefigure.py
370 lines (329 loc) · 10.2 KB
/
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
Module to grade a matplotlib.figure.Figure object.
"""
import numpy
from matplotlib import text
def ax_has_xlabel(ax):
"""
Checks the presence of a xlabel in a matplotlib.axes.Axes object.
Returns True if there is a non-empty string value for the xlabel.
Parameters
----------
ax: matplotlib.axes.Axes
The Axes object to inspect.
Returns
-------
ans: boolean
True if the xlabel is a non-empty string; otherwise False.
Examples
--------
>>> fig, ax = pyplot.subplots()
>>> ax_has_xlabel(ax)
False
>>> ax.set_xlabel('')
>>> ax_has_xlabel(ax)
False
>>> ax.set_xlabel('x')
>>> ax_has_xlabel(ax)
True
"""
return len(ax.get_xlabel()) > 0
def ax_has_ylabel(ax):
"""
Checks the presence of a ylabel in a matplotlib.axes.Axes object.
Returns True if there is a non-empty string value for the ylabel.
Parameters
----------
ax: matplotlib.axes.Axes
The Axes object to inspect.
Returns
-------
ans: boolean
True if the ylabel a non-empty string; otherwise False.
Examples
--------
>>> fig, ax = pyplot.subplots()
>>> ax_has_ylabel(ax)
False
>>> ax.set_ylabel('')
>>> ax_has_ylabel(ax)
False
>>> ax.set_ylabel('y')
>>> ax_has_ylabel(ax)
True
"""
return len(ax.get_ylabel()) > 0
def ax_has_title(ax):
"""
Checks the presence of a title in a matplotlib.axes.Axes object.
Returns True if there is a non-empty string value for the title.
Parameters
----------
ax: matplotlib.axes.Axes
The Axes object to inspect.
Returns
-------
ans: boolean
True if the title is a non-empty string; otherwise False.
Examples
--------
>>> fig, ax = pyplot.subplots()
>>> ax_has_title(ax)
False
>>> ax.set_title('')
>>> ax_has_title(ax)
False
>>> ax.set_title('title')
>>> ax_has_title(ax)
True
"""
return len(ax.get_title()) > 0
def ax_has_legend(ax):
"""
Checks the presence of a labels and Legend instance in a
matplotlib.axes.Axes object.
Returns True if there are labels and Legend instances exist.
Parameters
----------
ax: matplotlib.axes.Axes
The Axes object to inspect.
Returns
-------
ans: boolean
True if there are labels and Legend instances exist; otherwise False.
Examples
--------
>>> fig, ax = pyplot.subplots()
>>> ax_has_legend(ax)
False
>>> ax.legend()
>>> ax_has_legend(ax)
False
>>> ax.plot([0, 1, 2])
>>> ax_has_legend(ax)
False
>>> ax.plot([0, 1, 2], label='')
>>> ax_has_legend(ax)
False
>>> ax.plot([0, 1, 2], label='a')
>>> ax_has_legend(ax)
True
>>> fig, ax = pyplot.subplots()
>>> ax_has_legend(ax)
False
>>> ax.plot([0, 1, 2], label='a')
>>> ax_has_legend(ax)
False
>>> ax.legend()
>>> ax_has_legend(ax)
True
"""
_, labels = ax.get_legend_handles_labels()
return ax.get_legend() is not None and len(labels) > 0
def ax_has_data(ax, xref, yref):
"""
Checks for the presence of the data (xref, yref)
in a matplotlib.axes.Axes object.
Parameters
----------
ax: matplotlib.axes.Axes
The Axes object to inspect.
xref: list or numpy.ndarray of floats
The x-data to use as reference.
yref: list or numpy.ndarray of floats
The y-data to use as reference.
Returns
-------
ans: boolean
True if data (xref, yref) are present in the Axes object; other False.
Examples
--------
>>> x = numpy.linspace(0.0, 2.0 * numpy.pi, num=51)
>>> y = numpy.sin(x)
>>> fig, ax = pyplot.subplots()
>>> ax_has_data(ax, x, y)
False
>>> ax.plot(x, y)
>>> ax_has_data(ax, x, y)
True
>>> fig, ax = pyplot.subplots()
>>> ax_has_data(ax, x, y)
False
>>> ax.scatter(x, y)
>>> ax_has_data(ax, x, y)
True
"""
# Check in matplotlib.lines.Line2D objects.
for line in ax.get_lines():
x, y = line.get_data()
if len(x) == len(xref) and len(y) == len(yref):
if numpy.allclose(x, xref) and numpy.allclose(y, yref):
return True
# Check in matplotlib.collection.PathCollection objects.
for collection in ax.collections:
offsets = collection.get_offsets()
x, y = offsets[:, 0], offsets[:, 1]
if len(x) == len(xref) and len(y) == len(yref):
if numpy.allclose(x, xref) and numpy.allclose(y, yref):
return True
return False
def fig_has_text(fig):
"""
Checks for the presence of at least one matplotlib.text.Text object
in a matplotlib.figure.Figure object that is not in the
matplotlib.text.Text objects of the matplotlib.axes.Axes of the Figure.
The function will return True if the string used in one of the
matplotlib.text.Text objects is not empty.
Parameters
----------
fig: matplotlib.figure.Figure
The Figure object to inspect.
Returns
-------
ans: boolean
True if at least one of the Text objects of the Figure object
has a non-empty string value; otherwise False.
Examples
--------
>>> fig, ax = pyplot.subplots()
>>> fig_has_text(fig)
False
>>> fig.text(0.0, 0.0, "my text")
>>> fig_has_text(fig)
True
"""
# Find all Text objects present in the Figure.
fig_texts = set(fig.findobj(text.Text))
# Exclude Text objects that belongs to Axes objects.
for ax in fig.get_axes():
fig_texts -= set(ax.findobj(text.Text))
# Return False if no Text objects at the Figure level.
if len(fig_texts) == 0:
return False
# Check at least one Text object has a non-empty string.
for fig_text in fig_texts:
if len(fig_text.get_text()) > 0:
return True
# Otherwise return False.
return False
def check_figure(fig, ax_items=[], ax_data=[], title_or_text=False):
"""
Looks for the presence of certain items and numerical data in the
matplotlib.axes.Axes object of a matplotlib.figure.Figure object.
Parameters
----------
fig: matplotlib.figure.Figure
The Figure object to inspect.
ax_items: list of strings
The list of items to look for in the Axes objects of the Figure.
Currently supported: 'title', 'xlabel', and 'ylabel'.
ax_data: list of 2-tuples of arrays
The numerical data to look for in the Axes objects of the Figure.
Example: [(x1, y1), (x2, y2)].
title_or_text: boolean, optional
Check for the presence of a Text object with a non-empty string value in
the Figure object if no title was found;
default: False.
Returns
-------
log: dict
Dictionary with the Boolean result for each Axes items and data checked.
Examples
--------
>>> x = numpy.linspace(0.0, 2.0 * numpy.pi, num=51)
>>> y1, y2 = numpy.cos(x), numpy.sin(x)
>>> fig, ax = pyplot.subplots()
>>> ax.set_title('my title')
>>> ax.plot(x, y1)
>>> check_figure(fig, \
... ax_items=['title', 'xlabel', 'ylabel', 'legend'], \
... ax_data=[(x, y1), (x, y2)])
{'items': {'title': True, 'xlabel': False, 'ylabel': False, 'legend': False},
'data': {0: True, 1: False}}
>>> ax.set_xlabel('x')
>>> ax.set_ylabel('y')
>>> ax.scatter(x, y2, label='data')
>>> ax.legend()
>>> check_figure(fig, \
... ax_items=['title', 'xlabel', 'ylabel', 'legend'], \
... ax_data=[(x, y1), (x, y2)])
{'items': {'title': True, 'xlabel': True, 'ylabel': True, 'legend': True},
'data': {0: True, 1: True}}
"""
# Check provided items are supported.
supported_ax_items = {'xlabel', 'ylabel', 'title', 'legend'}
if len(set(ax_items) - supported_ax_items) > 0:
raise ValueError(f'Supported ax_items are {supported_ax_items}')
# Create a functions dispatcher for Axes items.
ax_items_dispatcher = {'xlabel': ax_has_xlabel, 'ylabel': ax_has_ylabel,
'title': ax_has_title, 'legend': ax_has_legend}
log = {'items': {}, 'data': {}}
# Loop over the Axes objects of the Figure.
for ax in fig.get_axes():
# Check for the presence of items in the Axes (such as labels and title).
for item in ax_items:
log['items'][item] = ax_items_dispatcher[item](ax)
# Check for the presence of a text caption if no title was found.
if title_or_text and not log['items']['title']:
log['items']['title'] = fig_has_text(fig)
# Check for the presence of numerical data in the Axes.
for i, (x, y) in enumerate(ax_data):
log['data'][i] = ax_has_data(ax, x, y)
return log
def grade_figure(fig, ax_items=[], ax_data=[], title_or_text=False,
item_points=1.0, data_points=1.0):
"""
Grades a matplotlib.figure.Figure object,
looking for the presence of provided items and numerical data
in the matplotlib.axes.Axes objects.
Parameters
----------
fig: matplotlib.figure.Figure
The Figure object to inspect.
ax_items: list of strings
The list of items to look for in the Axes objects of the Figure.
Currently supported: 'title', 'xlabel', and 'ylabel'.
ax_data: list of 2-tuples of arrays
The numerical data to look for in the Axes objects of the Figure.
Example: [(x1, y1), (x2, y2)].
title_or_text: boolean, optional
Check for the presence of a Text object with a non-empty string value in
the Figure object if no title was found;
default: False.
item_points: float, optional
Number of points to add to the grade when an item is found;
default: 1.0.
data_points: float, optional
Number of points to add to the grade when numerical data are found;
default: 1.0.
Returns
-------
grade: float
The grade in percent.
log: dict
Boolean result for each items and numerical data.
Examples
--------
>>> x = numpy.linspace(0.0, 2.0 * numpy.pi, num=51)
>>> y = numpy.sin(x)
>>> fig, ax = pyplot.subplots()
>>> ax.set_title('my title')
>>> ax.set_xlabel('x')
>>> ax.set_ylabel('y')
>>> ax.plot(x, y, label='data')
>>> grade_figure(fig, \
... ax_items=['title', 'xlabel', 'ylabel', 'legend'], \
... ax_data=[(x, y)])
(100.0, {'items': {'title': True, 'xlabel': True, 'ylabel': True,
'legend': True}, 'data': {0: True}})
"""
log = check_figure(fig, ax_items=ax_items, ax_data=ax_data,
title_or_text=title_or_text)
# Grade the Figure.
max_points = item_points * len(ax_items) + data_points * len(ax_data)
num_points = (item_points * sum(log['items'].values()) +
data_points * sum(log['data'].values()))
if abs(max_points) <= 1.0E-06:
return None, log
grade = num_points / max_points * 100.0
return grade, log