Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
greyhatguy007
GitHub Repository: greyhatguy007/Machine-Learning-Specialization-Coursera
Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week3/C1W3A1/test_utils.py
3748 views
1
import numpy as np
2
from copy import deepcopy
3
4
5
def datatype_check(expected_output, target_output, error):
6
success = 0
7
if isinstance(target_output, dict):
8
for key in target_output.keys():
9
try:
10
success += datatype_check(expected_output[key],
11
target_output[key], error)
12
except:
13
print("Error: {} in variable {}. Got {} but expected type {}".format(error,
14
key,
15
type(
16
target_output[key]),
17
type(expected_output[key])))
18
if success == len(target_output.keys()):
19
return 1
20
else:
21
return 0
22
elif isinstance(target_output, tuple) or isinstance(target_output, list):
23
for i in range(len(target_output)):
24
try:
25
success += datatype_check(expected_output[i],
26
target_output[i], error)
27
except:
28
print("Error: {} in variable {}, expected type: {} but expected type {}".format(error,
29
i,
30
type(
31
target_output[i]),
32
type(expected_output[i]
33
)))
34
if success == len(target_output):
35
return 1
36
else:
37
return 0
38
39
else:
40
assert isinstance(target_output, type(expected_output))
41
return 1
42
43
44
def equation_output_check(expected_output, target_output, error):
45
success = 0
46
if isinstance(target_output, dict):
47
for key in target_output.keys():
48
try:
49
success += equation_output_check(expected_output[key],
50
target_output[key], error)
51
except:
52
print("Error: {} for variable {}.".format(error,
53
key))
54
if success == len(target_output.keys()):
55
return 1
56
else:
57
return 0
58
elif isinstance(target_output, tuple) or isinstance(target_output, list):
59
for i in range(len(target_output)):
60
try:
61
success += equation_output_check(expected_output[i],
62
target_output[i], error)
63
except:
64
print("Error: {} for variable in position {}.".format(error, i))
65
if success == len(target_output):
66
return 1
67
else:
68
return 0
69
70
else:
71
if hasattr(target_output, 'shape'):
72
np.testing.assert_array_almost_equal(
73
target_output, expected_output)
74
else:
75
assert target_output == expected_output
76
return 1
77
78
79
def shape_check(expected_output, target_output, error):
80
success = 0
81
if isinstance(target_output, dict):
82
for key in target_output.keys():
83
try:
84
success += shape_check(expected_output[key],
85
target_output[key], error)
86
except:
87
print("Error: {} for variable {}.".format(error, key))
88
if success == len(target_output.keys()):
89
return 1
90
else:
91
return 0
92
elif isinstance(target_output, tuple) or isinstance(target_output, list):
93
for i in range(len(target_output)):
94
try:
95
success += shape_check(expected_output[i],
96
target_output[i], error)
97
except:
98
print("Error: {} for variable {}.".format(error, i))
99
if success == len(target_output):
100
return 1
101
else:
102
return 0
103
104
else:
105
if hasattr(target_output, 'shape'):
106
assert target_output.shape == expected_output.shape
107
return 1
108
109
110
def single_test(test_cases, target):
111
success = 0
112
for test_case in test_cases:
113
try:
114
if test_case['name'] == "datatype_check":
115
assert isinstance(target(*test_case['input']),
116
type(test_case["expected"]))
117
success += 1
118
if test_case['name'] == "equation_output_check":
119
assert np.allclose(test_case["expected"],
120
target(*test_case['input']))
121
success += 1
122
if test_case['name'] == "shape_check":
123
assert test_case['expected'].shape == target(
124
*test_case['input']).shape
125
success += 1
126
except:
127
print("Error: " + test_case['error'])
128
129
if success == len(test_cases):
130
print("\033[92m All tests passed.")
131
else:
132
print('\033[92m', success, " Tests passed")
133
print('\033[91m', len(test_cases) - success, " Tests failed")
134
raise AssertionError(
135
"Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))
136
137
138
def multiple_test(test_cases, target):
139
success = 0
140
for test_case in test_cases:
141
try:
142
test_input = deepcopy(test_case['input'])
143
target_answer = target(*test_input)
144
if test_case['name'] == "datatype_check":
145
success += datatype_check(test_case['expected'],
146
target_answer, test_case['error'])
147
if test_case['name'] == "equation_output_check":
148
success += equation_output_check(
149
test_case['expected'], target_answer, test_case['error'])
150
if test_case['name'] == "shape_check":
151
success += shape_check(test_case['expected'],
152
target_answer, test_case['error'])
153
except:
154
print('\33[30m', "Error: " + test_case['error'])
155
156
if success == len(test_cases):
157
print("\033[92m All tests passed.")
158
else:
159
print('\033[92m', success, " Tests passed")
160
print('\033[91m', len(test_cases) - success, " Tests failed")
161
raise AssertionError(
162
"Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))
163
164
165