Newer
Older
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
# -*- coding: utf-8 -*-
from __future__ import division, print_function
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
class GradingComplete(Exception):
pass
class Feedback:
def __init__(self):
self.points = None
self.feedback_items = []
def set_points(self, points):
self.points = points
def add_feedback(self, text):
self.feedback_items.append(text)
def finish(self, points, fb_text):
self.add_feedback(fb_text)
self.set_points(points)
raise GradingComplete()
def check_numpy_array_sanity(self, name, num_axes, data):
import numpy as np
if not isinstance(data, np.ndarray):
self.finish(0, "'%s' is not a numpy array" % name)
if isinstance(data, np.matrix):
self.finish(0, "'%s' is a numpy matrix. Do not use those. "
"bit.ly/array-vs-matrix" % name)
if len(data.shape) != num_axes:
self.finish(
0, "'%s' does not have the correct number of axes--"
"got: %d, expected: %d" % (
name, len(data.shape), num_axes))
if data.dtype.kind not in "fc":
self.finish(
0, "'%s' does not consist of floating point numbers--"
"got: '%s'" % (name, data.dtype))
def check_numpy_array_features(self, name, ref, data):
import numpy as np
assert isinstance(ref, np.ndarray)
if not isinstance(data, np.ndarray):
self.finish(0, "'%s' is not a numpy array" % name)
if isinstance(data, np.matrix):
self.finish(0, "'%s' is a numpy matrix. Do not use those. "
"bit.ly/array-vs-matrix" % name)
if ref.shape != data.shape:
self.finish(
0, "'%s' does not have correct shape--"
"got: '%s', expected: '%s'" % (
name, data.shape, ref.shape))
if ref.dtype.kind != data.dtype.kind:
self.finish(
0, "'%s' does not have correct data type--"
"got: '%s', expected: '%s'" % (
name, data.dtype.kind, ref.dtype.kind))
def check_numpy_array_allclose(self, name, ref, data, accuracy_critical=True,
rtol=1e-05, atol=1e-08, report_success=True, report_failure=True):
import numpy as np
self.check_numpy_array_features(name, ref, data)
good = np.allclose(ref, data, rtol=rtol, atol=atol)
if not good:
if report_failure:
self.add_feedback("'%s' is inaccurate" % name)
else:
if report_success:
self.add_feedback("'%s' looks good" % name)
if accuracy_critical and not good:
self.set_points(0)
raise GradingComplete()
return good
def check_list(self, name, ref, data, entry_type=None):
assert isinstance(ref, list)
if not isinstance(data, list):
self.finish(0, "'%s' is not a list" % name)
if len(ref) != len(data):
self.finish(0, "'%s' has the wrong length--expected %d, got %d"
% (name, len(ref), len(data)))
if entry_type is not None:
for i, entry in enumerate(data):
if not isinstance(entry, entry_type):
self.finish(0, "'%s[%d]' has the wrong type" % (name, i))
def check_scalar(self, name, ref, data, accuracy_critical=True,
rtol=1e-5, atol=1e-8, report_success=True, report_failure=True):
import numpy as np
if not isinstance(data, (float, int, np.number)):
self.finish(0, "'%s' is not a number" % name)
good = False
if rtol is not None and abs(ref-data) < abs(data)*rtol:
good = True
if atol is not None and abs(ref-data) < atol:
good = True
if not good:
if report_failure:
self.add_feedback("'%s' is inaccurate" % name)
else:
if report_success:
self.add_feedback("'%s' looks good" % name)
if accuracy_critical and not good:
self.set_points(0)
raise GradingComplete()
return good