-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmdstan_perf_util.py
109 lines (88 loc) · 3.16 KB
/
cmdstan_perf_util.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
import os
from fnmatch import fnmatch
from difflib import SequenceMatcher
import subprocess
import platform
from time import time
weekly_test_only = frozenset(
[os.path.join("good","function-signatures","distributions","univariate","continuous", "exp_mod_normal")
, os.path.join("good","function-signatures","distributions","univariate","continuous", "pareto_type_2")
, os.path.join("good","function-signatures","distributions","univariate","continuous", "skew_normal")
, os.path.join("good","function-signatures","distributions","univariate","continuous", "student_t")
, os.path.join("good","function-signatures","distributions","univariate","continuous", "wiener")
])
def filter_out_weekly_models(models):
ret_models = []
for m in models:
out = False
for i in weekly_test_only:
if i in m:
out = True
break
if not out:
ret_models.append(m)
return ret_models
def isWin():
return platform.system().lower().startswith(
"windows"
) or os.name.lower().startswith("windows")
DIR_UP = os.path.join("..","")
CURR_DIR = os.path.join(".","")
SEP_RE = "\\\\" if os.sep == "\\" else "/"
EXE_FILE_EXT = ".exe" if isWin() else ""
def find_files(pattern, dirs):
res = []
for pd in dirs:
for d, _, flist in os.walk(pd):
for f in flist:
if fnmatch(f, pattern):
res.append(os.path.join(d, f))
return res
def str_dist(target):
def str_dist_internal(candidate):
return SequenceMatcher(None, candidate, target).ratio()
return str_dist_internal
def closest_string(target, candidates):
if candidates:
return max(candidates, key=str_dist(target))
def time_step(name, fn, *args, **kwargs):
start = time()
res = fn(*args, **kwargs)
end = time()
return end-start, res
class FailedCommand(Exception):
def __init__(self, returncode, command):
self.returncode = returncode
self.command = command
Exception(self, "return code '{}' from command '{}'!"
.format(returncode, command))
def shexec(command, wd = "."):
print(command)
returncode = subprocess.call(command, shell=True, cwd=wd)
if returncode != 0:
raise FailedCommand(returncode, command)
return returncode
def make(targets, j=8, ext=EXE_FILE_EXT, allow_failure=True):
for i in range(len(targets)):
prefix = ""
if not targets[i].startswith(os.sep):
prefix = DIR_UP
targets[i] = prefix + targets[i] + ext
try:
shexec("make -i -j{} {}"
.format(j, " ".join(targets)), wd = "cmdstan")
except FailedCommand:
if allow_failure:
print("Failed to make at least some targets")
else:
raise
batchSize = 20 if isWin() else 200
def batched(tests):
return [tests[i : i + batchSize] for i in range(0, len(tests), batchSize)]
def delete_temporary_exe_files(exes):
for exe in exes:
extensions = ["", ".hpp", ".o"]
for ext in extensions:
print("Removing " + exe + ext)
if os.path.exists(exe + ext):
os.remove(exe + ext)