-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
355 lines (270 loc) · 10.8 KB
/
evaluation.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
from dataclasses import dataclass
from textwrap import indent, dedent
from typing import Any
from typing import Dict, List, Tuple, Set
import math
import lumberjack
from examples import ExampleInstance, parse, form
from helper_types import *
from randomiser import Randomiser
from reference_parser import FunctionReference
from runner import Function, Parameter, run_safe
class Generator:
"""
Generates examples
To generate examples this class produces a random input,
runs it on a "correct" version of the function,
and then retrieves the output produced by that code.
"""
def __init__(self, reference: FunctionReference, runner: Function):
self.reference = reference
self.runner = runner
random_seed = None # non-random seed doesn't play nicely with processes
self.randomiser = Randomiser(seed=random_seed)
def generate(self, n: int) -> List[ExampleInstance]:
"""
Used to generate multiple examples
:param n: the number of examples to make
:return: the examples generated
"""
assert n > 0
try:
examples = []
fails = 0
max_fails = n
while fails < max_fails and len(examples) < n:
example = self.generate_single()
if example is not None:
examples.append(example)
fails -= 1
else:
fails += 1
return examples
except FunctionRunError:
lumberjack.getLogger("error").error(f"issue calling function {self.runner.name}")
return []
def generate_input(self) -> ParameterMapping:
"""
Used to generate the input for one example
:return: the values for input parameters
"""
inputs = {}
for param in self.runner.safe_parameters():
inputs[param.name] = self.random(param, inputs)
return inputs
def generate_single(self) -> Optional[ExampleInstance]:
"""
Used to generate one example
NOTE: this spawns a new process so that if the function crashes on these inputs the rest of the program is safe
:return: the example generated
"""
inputs = self.generate_input()
if not self.runner.satisfied(inputs):
return None
results = run_safe(self.reference, self.runner.lib_path, inputs)
if results is None:
raise FunctionRunError(f"could not produce value from {self.reference.name}")
value, outputs = results
return ExampleInstance(inputs, value, outputs)
def random(self, parameter: Parameter, current: ParameterMapping) -> SomeValue:
"""
Generate a random value for an input parameter
As some parameters depend on others, this function needs to know the values of parameters already generated.
Hopefully this will be called in an order where dependencies are generated before their dependents.
This checks the constraints on a parameter too to limit the random sample space to valid values.
:param parameter: the parameter to generate a value for
:param current: the values already generated
:return: the new parameter value
"""
max_val: Optional[SomeValue] = None
min_val: Optional[SomeValue] = None
for constraint in parameter.constraints:
if constraint.op in {"<", "<="}:
if max_val is None or max_val < constraint.value:
max_val = constraint.value
elif constraint.op in {">", ">="}:
if min_val is None or min_val > constraint.value:
min_val = constraint.value
primitive = parameter.type.contents
if primitive == "int":
def gen():
return self.randomiser.random_int(min_val=min_val, max_val=max_val)
elif primitive == "float":
def gen():
return self.randomiser.random_float(min_val=min_val, max_val=max_val)
elif primitive == "double":
def gen():
return self.randomiser.random_double(min_val=min_val, max_val=max_val)
elif primitive == "char":
def gen():
return self.randomiser.random_char()
elif primitive == "bool":
def gen():
return self.randomiser.random_bool()
else:
raise UnsupportedTypeError(primitive)
if not parameter.is_array():
val = gen()
else:
size = parameter.get_size(None, current)
val = self.randomiser.random_array(size, gen)
val = val if primitive != "char" else ''.join(val)
return val
@dataclass
class Failure:
expected: ExampleInstance
value: Any
outputs: Dict[str, Any]
def __str__(self):
return f"input {self.expected.inputs} produced incorrect values (expected vs. real);\
{self.expected.value} vs. {self.value}; {self.expected.outputs} vs. {self.outputs}"
class Result:
"""
Contains the results of testing a series of inputs on a function
"""
def __init__(self, passes: int, tests: int, failures: List[Failure], name: str = None):
assert passes >= 0 and tests >= 0 # len(failures) is implicitly >= 0
assert passes + len(failures) == tests
self.passes = passes
self.tests = tests
self.failures = failures
self.name = name
def passed(self) -> bool:
"""
:return: :code:`True` if all tests passed
"""
return self.passes == self.tests
def is_trivial(self) -> bool:
"""
:return: :code:`True` if no tests were run
"""
return self.tests == 0
def full(self, show_fails: bool) -> str:
"""
Formats the result into a nice description
:param show_fails: set to :code:`True` to put any failed tests in the description
:return: the prettified result
"""
if show_fails and self.failures:
return dedent('''\
{summ}
{failures}\
''').format(summ=self, failures=self.show_failures())
else:
return str(self)
def show_failures(self) -> str:
"""
:return: any failures, formatted nicely
"""
return indent("\n".join(str(failure) for failure in self.failures), " >> ")
def __str__(self):
status = "OK" if self.passed else "NOT OK"
name = "" if self.name is None else f"{self.name}: "
return f"{name}passed {self.passes}/{self.tests} tests ({status})"
class Evaluator:
"""
Evaluates functions on examples
"""
def __init__(self, reference: FunctionReference, runner: Function):
self.reference = reference
self.runner = runner
@staticmethod
def read(example_file: str) -> List[ExampleInstance]:
"""
Parses an example file into examples to use
:param example_file: the file containing examples
:return: the examples in the file
"""
with open(example_file, "r") as f:
sig = f.readline()
examples = f.readlines()
return parse(sig, examples)
def transform(self, examples: List[ExampleInstance]):
"""
Currently useless, put all cleanup code for examples here
TODO: some smarter stuff
For example could pair up misnamed examples,
infer size variables for arrays,
etc.
:param examples: the examples to fix
:return: the fixed examples
"""
return examples
def check_example(self, example: ExampleInstance) -> Optional[Failure]:
"""
Runs an example and checks whether the result matches the expected
NOTE: this spawns a new process so that if the function crashes on these inputs the rest of the program is safe
:param example: the example to use
:return: whether or not the output of the example matches the expected output
"""
def check_value(expected_value: AnyValue, actual_value: AnyValue) -> bool:
if expected_value == actual_value:
return True
try:
return math.isnan(expected_value) == math.isnan(actual_value)
except TypeError:
return False
result = run_safe(self.reference, self.runner.lib_path, example.inputs)
if result is None:
raise FunctionRunError(f"no value produced by {self.reference.name}")
value, actual = result
expected = example.outputs
fail = Failure(example, value, actual)
if not check_value(example.value, value):
return fail
for param in expected:
if not check_value(expected[param], actual[param]):
return fail
return None
def check(self, examples: List[ExampleInstance]) -> Result:
"""
Evaluates many examples, uses :code:`check_example`
:param examples: the examples to evaluate
:return: a tuple of the form (number of successes, number of trials, [details of failures])
"""
examples = self.transform(examples)
passes = 0
failures = []
for example in examples:
try:
failure = self.check_example(example)
if failure is None:
passes += 1
else:
failures.append(failure)
except Exception as e: # TODO: check
failures.append(None) #TODO
return Result(passes, len(examples), failures)
def evaluate(ref: FunctionReference, run: Function, ex_file: str) -> Result:
"""
Helper method to check a reference function against examples
:param ref: the reference of the function
:param run: the executable of the function
:param ex_file: the examples to evaluate with
:return: the evaluation result of the test
"""
e = Evaluator(ref, run)
examples = e.read(ex_file)
result = e.check(examples)
lumberjack.getLogger("failure").error(result.show_failures())
return result
def generate(ref: FunctionReference, run: Function, n: int) -> List[ExampleInstance]:
"""
Helper method to produce examples to check against a function
:param ref: the reference of the function
:param run: the executable of the function
:param n: the number of examples to make
:return: the examples produced
"""
g = Generator(ref, run)
examples = g.generate(n)
return examples
def write_examples(ref: FunctionReference, examples: List[ExampleInstance], ex_file: str):
"""
Writes a collection of examples to a file
:param ref: the reference the examples are for
:param examples: the examples
:param ex_file: the file to write the examples into
"""
with open(ex_file, "w") as f:
f.write('\n'.join(form(ref, examples)))