File: generate_ufloat_method_cases.py

package info (click to toggle)
uncertainties 3.2.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: python: 3,195; makefile: 52; sh: 16
file content (55 lines) | stat: -rw-r--r-- 1,743 bytes parent folder | download | duplicates (2)
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
import json
from pathlib import Path
import random


unary_functions = ["__neg__", "__pos__"]
binary_functions = [
    "__add__",
    "__radd__",
    "__sub__",
    "__rsub__",
    "__mul__",
    "__rmul__",
    "__truediv__",  # Will fail if second value is 0 but that is unlikely
    "__rtruediv__",  # Will fail if first value is 0 but that is unlikely
    "__pow__",  # We will coerce the base to be positive
    "__rpow__",  # We will coerce the base to be positive
]

ufloat_method_cases_dict = {}

for func_name in unary_functions:
    cases_list = []
    for _ in range(10):
        nominal_value = random.uniform(-100, 100)
        std_dev = random.uniform(0, 100)
        ufloat_tuple = (nominal_value, std_dev)
        case = (ufloat_tuple,)
        cases_list.append(case)
    ufloat_method_cases_dict[func_name] = cases_list

for func_name in binary_functions:
    cases_list = []
    for _ in range(10):
        nominal_value_0 = random.uniform(-100, 100)
        if func_name == "__pow__":
            nominal_value_0 = abs(nominal_value_0)
        std_dev_0 = random.uniform(0, 100)
        ufloat_tuple_0 = (nominal_value_0, std_dev_0)

        nominal_value_1 = random.uniform(-100, 100)
        if func_name == "__rpow__":
            nominal_value_1 = abs(nominal_value_1)
        std_dev_1 = random.uniform(0, 100)
        ufloat_tuple_1 = (nominal_value_1, std_dev_1)

        case = (ufloat_tuple_0, ufloat_tuple_1)
        cases_list.append(case)
    ufloat_method_cases_dict[func_name] = cases_list

ufloat_method_cases_json_path = Path(Path.cwd(), "ufloat_method_cases.json")

if __name__ == "__main__":
    with open(ufloat_method_cases_json_path, "w") as f:
        json.dump(ufloat_method_cases_dict, f, indent=4)