-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
196 lines (168 loc) · 5.91 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# This file is part of Estimation of Causal Effects in the Alzheimer's Continuum (Causal-AD).
#
# Causal-AD is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Causal-AD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Causal-AD. If not, see <https://www.gnu.org/licenses/>.
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os.path
from pathlib import Path
from setuptools import find_packages
from setuptools import Extension, setup
from Cython.Build import cythonize
requirements = (
'joblib',
'matplotlib',
'neuroCombat',
'numpy',
'pandas',
'patsy',
'pystan',
'scikit-learn',
'scipy',
'seaborn',
'tqdm',
)
# see https://github.com/stan-dev/pystan2/blob/1dd043db3c2618a9360a0f2ccbb57221634e5b08/pystan/model.py#L223
def get_stan_extension(stan_file: Path) -> Extension:
import pystan.api
import string
import numpy as np
stan_timestamp = os.path.getmtime(stan_file)
pyx_file = stan_file.parent / f"_{stan_file.stem}.pyx"
hpp_file = stan_file.with_suffix(".hpp")
if pyx_file.exists() and hpp_file.exists():
pyx_timestamp = min(os.path.getmtime(pyx_file), os.path.getmtime(hpp_file))
else:
pyx_timestamp = -1
pystan_dir = Path(pystan.api.__file__).parent
if pyx_timestamp < stan_timestamp:
stanc_ret = pystan.api.stanc(
file=str(stan_file),
charset="utf-8",
model_name=stan_file.stem,
verbose=True,
obfuscate_model_name=False,
)
if stanc_ret['status'] != 0: # success == 0
raise ValueError("stanc_ret is not a successfully returned "
"dictionary from stanc.")
pyx_template_file = pystan_dir / 'stanfit4model.pyx'
with open(pyx_template_file) as infile:
s = infile.read()
template = string.Template(s)
with open(pyx_file, 'w') as outfile:
s = template.safe_substitute(model_cppname=stan_file.stem)
outfile.write(s)
with open(hpp_file, 'w') as outfile:
outfile.write(stanc_ret['cppcode'])
stan_macros = [
("EIGEN_USE_MKL", None),
("EIGEN_USE_BLAS", None),
("BOOST_RESULT_OF_USE_TR1", None),
("BOOST_NO_DECLTYPE", None),
("BOOST_DISABLE_ASSERTS", None),
("BOOST_PHOENIX_NO_VARIADIC_EXPRESS", "ION"),
("NDEBUG", None),
]
extra_compile_args = [
"-O3",
"-fvisibility-inlines-hidden",
"-fmessage-length=0",
"-ftree-vectorize",
"-fstack-protector-strong",
"-fno-plt",
"-ffunction-sections",
"-Wno-unused-function",
"-Wno-uninitialized",
"-Wno-sign-compare",
"-Wno-ignored-attributes",
"-std=c++1y",
"-mtune=native",
]
from numpy.distutils.__config__ import get_info
blas_info = get_info('blas_mkl')
include_dirs = [
stan_file.parent,
pystan_dir,
pystan_dir / "stan" / "src",
pystan_dir / "stan" / "lib" / "stan_math",
pystan_dir / "stan" / "lib" / "stan_math" / "lib" / "eigen_3.3.3",
pystan_dir / "stan" / "lib" / "stan_math" / "lib" / "boost_1.69.0",
pystan_dir / "stan" / "lib" / "stan_math" / "lib" / "sundials_4.1.0" / "include",
np.get_include(),
] + blas_info["include_dirs"]
include_dirs = [str(d) for d in include_dirs]
libraries = [
"mkl_intel_lp64",
"mkl_tbb_thread",
"mkl_core",
"tbb",
"pthread",
"m",
"dl",
]
extension = Extension(
name=f"causalad.pystan_models._{stan_file.stem}",
language="c++",
sources=[str(pyx_file)],
define_macros=stan_macros,
include_dirs=include_dirs,
libraries=libraries,
library_dirs=blas_info["library_dirs"],
extra_compile_args=extra_compile_args,
)
return extension
def get_extensions():
import pystan
stan_dir = Path("causalad/pystan_models/stan_files")
extensions = [
"bpmf_reparameterized.stan",
"bpmf_reparameterized_adni.stan",
"logreg.stan",
"ppca4.stan",
"ppca4_adni.stan",
]
pystan_dir = Path(pystan.__file__).parent
cython_include_dirs = [str(pystan_dir)]
build_extension = cythonize(
[get_stan_extension(stan_dir / e) for e in extensions],
include_path=cython_include_dirs,
)
return build_extension
setup(
name='causalad',
license='GPLv3+',
description='Estimation of Causal Effects in the Presence of Unobserved Confounding in the Alzheimer\'s Continuum',
author='Sebastian Pölsterl',
author_email='sebastian.poelsterl@med.uni-muenchen.de',
packages=find_packages(),
ext_modules=get_extensions(),
package_data={"causalad.adni": ["notebooks/*.ipynb"], "causalad.ukb": ["notebooks/*.ipynb"]},
zip_safe=False,
classifiers=[
# complete classifier list: http://pypi.python.org/pypi?%3Aaction=list_classifiers
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: Apache Software License',
'Operating System :: Unix',
'Operating System :: POSIX',
'Operating System :: Microsoft :: Windows',
'Programming Language :: Python',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: Implementation :: CPython',
'Topic :: Utilities',
],
version='0.2.0',
python_requires='>=3.7',
install_requires=requirements,
)