-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
216 lines (187 loc) · 11.1 KB
/
test.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
import sys
import os
import warnings
import unittest
import Osiris
from Osiris.utils import return_fix_statement_for_random_statement
test_executability_notebook_path = 'tests/test_executability.ipynb'
test_reproducibility_notebook_path = 'tests/test_reproducibility.ipynb'
test_best_effort_notebook_path = 'tests/test_best_effort.ipynb' # It's for sub testset among reproducibility test set
test_repeatablility_notebook_path = 'tests/test_self_reproducibility.ipynb'
test_debug_for_a_cell_notebook_path = 'tests/test_debug_for_a_cell.ipynb'
test_IPythonDisplay_notebook_path = 'tests/test_IPythonDisplay.ipynb'
test_Matplotlib_notebook_path = 'tests/test_Matplotlib.ipynb'
# Global setting
verbose = False
root_path = os.getcwd()
class TestOsiris(unittest.TestCase):
def setUp(self):
warnings.simplefilter('ignore', category=ImportWarning)
warnings.simplefilter('ignore', category=DeprecationWarning)
warnings.simplefilter('ignore', category=ResourceWarning)
os.chdir(root_path)
'''
The following 3 unit tests focus executability
'''
def test_normal_executability(self):
interface = Osiris.UserInterface(test_executability_notebook_path, 'normal', verbose)
is_executable = interface.analyse_executability()
self.assertEqual(is_executable, True)
def test_OEC_executability(self):
interface = Osiris.UserInterface(test_executability_notebook_path, 'OEC', verbose)
is_executable = interface.analyse_executability()
self.assertEqual(is_executable, True)
def test_dependency_executability(self):
interface = Osiris.UserInterface(test_executability_notebook_path, 'dependency', verbose)
is_executable = interface.analyse_executability()
self.assertEqual(is_executable, True)
'''
The following 9 unit tests focus reproducibility
'''
def test_top_down_strong_reproducibility(self):
interface = Osiris.UserInterface(test_reproducibility_notebook_path, 'normal', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('strong')
self.assertEqual(num_of_matched_cells, 6)
self.assertEqual(num_of_cells, 8)
def test_top_down_weak_reproducibility(self):
interface = Osiris.UserInterface(test_reproducibility_notebook_path, 'normal', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('weak')
self.assertEqual(num_of_matched_cells, 8)
self.assertEqual(num_of_cells, 8)
def test_OEC_strong_reproducibility(self):
interface = Osiris.UserInterface(test_reproducibility_notebook_path, 'OEC', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('strong')
self.assertEqual(num_of_matched_cells, 8)
self.assertEqual(num_of_cells, 8)
def test_OEC_weak_reproducibility(self):
interface = Osiris.UserInterface(test_reproducibility_notebook_path, 'OEC', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('weak')
self.assertEqual(num_of_matched_cells, 8)
self.assertEqual(num_of_cells, 8)
def test_dependency_strong_reproducibility(self):
interface = Osiris.UserInterface(test_reproducibility_notebook_path, 'dependency', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('strong')
self.assertEqual(num_of_matched_cells, 6) # BUGGY STATEMENT
self.assertEqual(num_of_cells, 8)
def test_dependency_weak_reproducibility(self):
interface = Osiris.UserInterface(test_reproducibility_notebook_path, 'dependency', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('weak')
self.assertEqual(num_of_matched_cells, 8)
self.assertEqual(num_of_cells, 8)
# For best_effort match pattern, we carefully crafted a notebook for testing best_effort match pattern
def test_top_down_best_effort_reproducibility(self):
interface = Osiris.UserInterface(test_best_effort_notebook_path, 'normal', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('best_effort')
self.assertEqual(num_of_matched_cells, 8)
self.assertEqual(num_of_cells, 8)
def test_OEC_best_effort_reproducibility(self):
interface = Osiris.UserInterface(test_best_effort_notebook_path, 'OEC', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('best_effort')
self.assertEqual(num_of_matched_cells, 8)
self.assertEqual(num_of_cells, 8)
def test_dependency_best_effort_reproducibility(self):
interface = Osiris.UserInterface(test_best_effort_notebook_path, 'dependency', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('best_effort')
self.assertEqual(num_of_matched_cells, 8)
self.assertEqual(num_of_cells, 8)
'''
The following 3 unit tests focus repeatablility
'''
def test_top_down_repeatablility(self):
interface = Osiris.UserInterface(test_repeatablility_notebook_path, 'normal', verbose)
num_of_reproducible_cells, num_of_cells, _, reproducible_cell_idx = interface.analyse_repeatablility()
self.assertEqual(num_of_reproducible_cells, 2)
self.assertEqual(num_of_cells, 4)
self.assertEqual(reproducible_cell_idx, [0, 3])
def test_OEC_repeatablility(self):
interface = Osiris.UserInterface(test_repeatablility_notebook_path, 'OEC', verbose)
num_of_reproducible_cells, num_of_cells, _, reproducible_cell_idx = interface.analyse_repeatablility()
self.assertEqual(num_of_reproducible_cells, 2)
self.assertEqual(num_of_cells, 4)
self.assertEqual(reproducible_cell_idx, [0, 1])
def test_dependency_repeatablility(self):
interface = Osiris.UserInterface(test_repeatablility_notebook_path, 'dependency', verbose)
num_of_reproducible_cells, num_of_cells, _, _ = interface.analyse_repeatablility()
self.assertEqual(num_of_reproducible_cells, 2)
self.assertEqual(num_of_cells, 4)
'''
The following 3 unit tests focus status difference inspection
'''
def test_top_down_debug_for_a_cell(self):
interface = Osiris.UserInterface(test_debug_for_a_cell_notebook_path, 'normal', verbose)
problematic_statement_index = interface.analyse_status_difference_for_a_cell(1)
self.assertEqual(problematic_statement_index, 10)
def test_OEC_debug_for_a_cell(self):
interface = Osiris.UserInterface(test_debug_for_a_cell_notebook_path, 'OEC', verbose)
problematic_statement_index = interface.analyse_status_difference_for_a_cell(2)
self.assertEqual(problematic_statement_index, 10)
def test_dependency_debug_for_a_cell(self):
interface = Osiris.UserInterface(test_debug_for_a_cell_notebook_path, 'dependency', verbose)
problematic_statement_index = interface.analyse_status_difference_for_a_cell(1)
self.assertEqual(problematic_statement_index, 10)
'''
The following 2 unit tests focus on the correctness of Osiris for images
'''
def test_image_IPythonDisplay(self):
interface = Osiris.UserInterface(test_IPythonDisplay_notebook_path, 'OEC', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('weak')
self.assertEqual(num_of_matched_cells, 1)
self.assertEqual(num_of_cells, 1)
def test_image_Matplotlib(self):
interface = Osiris.UserInterface(test_Matplotlib_notebook_path, 'OEC', verbose)
num_of_matched_cells, num_of_cells, _, _, _ = interface.analyse_reproducibility('weak')
self.assertEqual(num_of_matched_cells, 2)
self.assertEqual(num_of_cells, 2)
'''
Below unit test focus on the util func: return_fix_statement
Note that this unit test should be removed in the future
'''
def test_return_fix_statement(self):
print()
# 1
statement, import_statement_lst = 'a = random.randint(0, 3)', ['import random']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 2
statement, import_statement_lst = 'numpy.random.randint(5)', ['import numpy']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 3
statement, import_statement_lst = 'np.random.randint(5)', ['import numpy as np']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 4
statement, import_statement_lst = 'd = np.random.normal(0, 0.2, 5000)', ['import numpy as np']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 5
statement, import_statement_lst = "datos = {\n 'valores': np.random.randn(100),\n 'frecuencia': dt.timedelta(minutes = 10),\n 'fecha_inicial': dt.datetime(2016, 1, 1, 0, 0),\n 'parametro': 'wind_speed',\n 'unidades': 'm/s'\n}", ['import numpy as np']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 6
statement, import_statement_lst = ' y_var2 = np.random.randint(5, 8, 10)', ['import numpy as np']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 7
statement, import_statement_lst = ' x_ = random.random()', ['import random']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 8
statement, import_statement_lst = ' s_ = scale*random.random()', ['import random']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 9
statement, import_statement_lst = 'X = np.random.uniform(0.,1.,N)', ['import numpy as np']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 10
statement, import_statement_lst = 'X = np.random.random(N)', ['import numpy as np']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
# 11
statement, import_statement_lst = 'y1 = 0.2*x + np.random.rand(1000)', ['import numpy as np']
fix_statement = return_fix_statement_for_random_statement(statement, import_statement_lst)
print(statement, import_statement_lst, fix_statement)
pass
if __name__ == '__main__':
unittest.main()