-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy path__init__.py
78 lines (56 loc) · 2.51 KB
/
__init__.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
# -*- coding: utf-8 -*-
import os
import numexpr
from openfisca_core.indexed_enums import EnumArray
def assert_near(value, target_value, absolute_error_margin = None, message = '', relative_error_margin = None):
'''
:param value: Value returned by the test
:param target_value: Value that the test should return to pass
:param absolute_error_margin: Absolute error margin authorized
:param message: Error message to be displayed if the test fails
:param relative_error_margin: Relative error margin authorized
Limit : This function cannot be used to assert near dates or periods.
'''
import numpy as np
if absolute_error_margin is None and relative_error_margin is None:
absolute_error_margin = 0
if not isinstance(value, np.ndarray):
value = np.array(value)
if isinstance(value, EnumArray):
return assert_enum_equals(value, target_value, message)
if isinstance(target_value, str):
target_value = eval_expression(target_value)
target_value = np.array(target_value).astype(np.float32)
value = np.array(value).astype(np.float32)
diff = abs(target_value - value)
if absolute_error_margin is not None:
assert (diff <= absolute_error_margin).all(), \
'{}{} differs from {} with an absolute margin {} > {}'.format(message, value, target_value,
diff, absolute_error_margin)
if relative_error_margin is not None:
assert (diff <= abs(relative_error_margin * target_value)).all(), \
'{}{} differs from {} with a relative margin {} > {}'.format(message, value, target_value,
diff, abs(relative_error_margin * target_value))
def assert_enum_equals(value, target_value, message = ''):
value = value.decode_to_str()
assert (value == target_value).all(), '{}{} differs from {}.'.format(message, value, target_value)
def indent(text):
return " {}".format(text.replace(os.linesep, "{} ".format(os.linesep)))
def get_trace_tool_link(scenario, variables, api_url, trace_tool_url):
import json
import urllib
scenario_json = scenario.to_json()
simulation_json = {
'scenarios': [scenario_json],
'variables': variables,
}
url = trace_tool_url + '?' + urllib.urlencode({
'simulation': json.dumps(simulation_json),
'api_url': api_url,
})
return url
def eval_expression(expression):
try:
return numexpr.evaluate(expression)
except (KeyError, TypeError):
return expression