-
Notifications
You must be signed in to change notification settings - Fork 134
/
install_stan.py
62 lines (50 loc) · 1.74 KB
/
install_stan.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
import json
import os
import platform
import shutil
from multiprocessing import cpu_count
import cmdstanpy
with open("orbit/config.json") as f:
config = json.load(f)
CMDSTAN_VERSION = config["CMDSTAN_VERSION"]
IS_WINDOWS = platform.platform().startswith("Win")
def remove_older_cmdstan(cmdstan_version):
"""
Checks the default CmdStan path (~/.cmdstan) for folders with older versions
and removes them.
Args:
cmdstan_version (str): The current CmdStan version.
"""
default_path = os.path.expanduser("~/.cmdstan")
if not os.path.exists(default_path):
# Path doesn't exist, nothing to remove
return
for folder in os.listdir(default_path):
if folder.startswith("cmdstan-"):
# Extract version number from folder name
folder_version = folder.split("-")[1]
if folder_version < cmdstan_version:
# Remove folder if version is older
full_path = os.path.join(default_path, folder)
try:
print(f"Removing older CmdStan version: {folder} : {full_path}")
shutil.rmtree(full_path)
print(f"Done.")
except OSError as e:
print(f"Error removing {folder}: {e}")
def install_stan():
"""
Compile and install stan backend
Reference from prophet
"""
remove_older_cmdstan(CMDSTAN_VERSION)
if not cmdstanpy.install_cmdstan(
version=CMDSTAN_VERSION,
overwrite=True,
verbose=True,
cores=cpu_count(),
progress=True,
compiler=IS_WINDOWS,
):
raise RuntimeError("CmdStan failed to install.")
print(f"Installed cmdstanpy (cmdstan v.{CMDSTAN_VERSION}) and compiler.")