CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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