-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
145 lines (113 loc) · 4.38 KB
/
setup.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
"""
This setup.py is used only for the building of the Stan
models. The rest of the metadata is in setup.cfg
"""
import os
import platform
from ast import literal_eval
from pathlib import Path
from shutil import copy, copytree, rmtree
from typing import Tuple
import cmdstanpy
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
from wheel.bdist_wheel import bdist_wheel
MODEL_DIR = "scikit_stan/stan_files"
MODELS = ["glm_continuous", "glm_discrete", "glm_binomial"]
CMDSTAN_VERSION = "2.34.1"
BINARIES_DIR = "bin"
BINARIES = ["diagnose", "print", "stanc", "stansummary"]
MATH_LIB = "stan/lib/stan_math/lib"
TBB_DIRS = ["tbb", "tbb_2020.3"]
def prune_cmdstan(cmdstan_dir: os.PathLike) -> None:
"""
Keep only the cmdstan executables and tbb files
(minimum required to run a cmdstanpy commands on a pre-compiled model).
"""
original_dir = Path(cmdstan_dir).resolve()
parent_dir = original_dir.parent
temp_dir = parent_dir / "temp"
if temp_dir.is_dir():
rmtree(temp_dir)
temp_dir.mkdir()
print("Copying ", original_dir, " to ", temp_dir, " for pruning")
copytree(original_dir / BINARIES_DIR, temp_dir / BINARIES_DIR)
copy(original_dir / "makefile", temp_dir / "makefile")
for f in (temp_dir / BINARIES_DIR).iterdir():
if f.is_dir():
rmtree(f)
elif f.is_file() and f.stem not in BINARIES:
os.remove(f)
for tbb_dir in TBB_DIRS:
copytree(original_dir / MATH_LIB / tbb_dir, temp_dir / MATH_LIB / tbb_dir)
rmtree(original_dir)
temp_dir.rename(original_dir)
def repackage_cmdstan() -> bool:
return os.environ.get("SKSTAN_REPACKAGE_CMDSTAN", "").lower() in ["true", "1"]
def maybe_install_cmdstan_toolchain() -> None:
"""Install C++ compilers required to build stan models on Windows machines."""
try:
cmdstanpy.utils.cxx_toolchain_path()
except Exception:
from cmdstanpy.install_cxx_toolchain import run_rtools_install
run_rtools_install({"version": None, "dir": None, "verbose": True})
cmdstanpy.utils.cxx_toolchain_path()
def install_cmdstan_deps(cmdstan_dir: Path) -> None:
from multiprocessing import cpu_count
if repackage_cmdstan():
if platform.platform().startswith("Win"):
maybe_install_cmdstan_toolchain()
print("Installing cmdstan to", cmdstan_dir)
if os.path.isdir(cmdstan_dir):
print("Removing existing dir", cmdstan_dir)
rmtree(cmdstan_dir)
if not cmdstanpy.install_cmdstan(
version=CMDSTAN_VERSION,
dir=cmdstan_dir.parent,
overwrite=True,
verbose=True,
cores=cpu_count(),
):
raise RuntimeError("CmdStan failed to install in repackaged directory")
else:
try:
cmdstanpy.cmdstan_path()
except ValueError as e:
raise SystemExit(
"CmdStan not installed, but the package is building from source"
) from e
def build_models(target_dir: str) -> None:
cmdstan_dir = (Path(target_dir) / f"cmdstan-{CMDSTAN_VERSION}").resolve()
install_cmdstan_deps(cmdstan_dir)
for model in MODELS:
sm = cmdstanpy.CmdStanModel(
stan_file=os.path.join(MODEL_DIR, model + ".stan"),
stanc_options={"O1": True},
)
copy(sm.exe_file, os.path.join(target_dir, model + ".exe"))
if repackage_cmdstan():
prune_cmdstan(cmdstan_dir)
class BuildModels(build_ext):
"""Custom build command to pre-compile Stan models."""
def run(self) -> None:
if not self.dry_run:
target_dir = os.path.join(self.build_lib, MODEL_DIR)
self.mkpath(target_dir)
build_models(target_dir)
# don't call build_ext.run, since we're not really building c files
class WheelABINone(bdist_wheel):
def finalize_options(self) -> None:
bdist_wheel.finalize_options(self)
self.root_is_pure = False
def get_tag(self) -> Tuple[str, str, str]:
_, _, plat = bdist_wheel.get_tag(self)
return "py3", "none", plat
# get version
VERSIONFILE = "scikit_stan/_version.py"
with open(VERSIONFILE, "rt") as f:
version = literal_eval(f.readline().split("= ")[1])
setup(
version=version,
ext_modules=[Extension("scikit-stan.stan_files", [])],
cmdclass={"build_ext": BuildModels, "bdist_wheel": WheelABINone},
)