forked from michelbierlaire/biogeme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
231 lines (196 loc) · 8.16 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""Instructions for the installation of Biogeme
:author: Michel Bierlaire
:date: Tue Mar 26 16:45:15 2019
"""
# Too constraining
# pylint: disable=invalid-name, protected-access, too-many-arguments
import os
import sys
import platform
#import distutils.ccompiler
import multiprocessing.pool
import setuptools
#from setuptools import setup, Extension
#import setuptools.command.test
import numpy
from biogeme.version import __version__
if platform.system() == 'Darwin':
os.environ["CC"] = 'clang'
os.environ["CXX"] = 'clang++'
else:
os.environ["CC"] = 'gcc'
os.environ["CXX"] = 'g++'
# monkey-patch for parallel compilation
def parallelCCompile(self,
sources,
output_dir=None,
macros=None,
include_dirs=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
depends=None):
""" those lines are copied from distutils.ccompiler.CCompiler directly"""
macros, objects, extra_postargs, pp_opts, build = \
self._setup_compile(output_dir,
macros,
include_dirs,
sources,
depends,
extra_postargs)
cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
# parallel code
N = 8 # number of parallel compilations
def _single_compile(obj):
try:
src, extension = build[obj]
except KeyError:
return
self._compile(obj, src, extension, cc_args, extra_postargs, pp_opts)
# convert to list, imap is evaluated on-demand
list(multiprocessing.pool.ThreadPool(N).imap(_single_compile, objects))
return objects
#distutils.ccompiler.CCompiler.compile = parallelCCompile
try:
from Cython.Build import cythonize
from Cython.Distutils import build_ext
except ImportError:
USE_CYTHON = False
print('Not using Cython')
else:
USE_CYTHON = True
print('Using Cython')
ext = '.pyx' if USE_CYTHON else '.cpp'
cmdclass = {}
source = ['src/biogeme.cc',
'src/evaluateExpressions.cc',
'src/bioMemoryManagement.cc',
'src/bioNormalCdf.cc',
'src/bioFormula.cc',
'src/bioSeveralFormulas.cc',
'src/bioThreadMemory.cc',
'src/bioThreadMemoryOneExpression.cc',
'src/bioThreadMemorySimul.cc',
'src/bioString.cc',
'src/bioExprNormalCdf.cc',
'src/bioExprIntegrate.cc',
'src/bioExprGaussHermite.cc',
'src/bioExprRandomVariable.cc',
'src/bioExprMontecarlo.cc',
'src/bioExprPanelTrajectory.cc',
'src/bioExprDraws.cc',
'src/bioExprDerive.cc',
'src/bioExprMin.cc',
'src/bioExprMax.cc',
'src/bioExprAnd.cc',
'src/bioExprOr.cc',
'src/bioExprEqual.cc',
'src/bioExprNotEqual.cc',
'src/bioExprLessOrEqual.cc',
'src/bioExprLess.cc',
'src/bioExprGreaterOrEqual.cc',
'src/bioExprGreater.cc',
'src/bioExprElem.cc',
'src/bioExprMultSum.cc',
'src/bioExprLiteral.cc',
'src/bioExprFreeParameter.cc',
'src/bioExprFixedParameter.cc',
'src/bioExprVariable.cc',
'src/bioExprPlus.cc',
'src/bioExprMinus.cc',
'src/bioExprTimes.cc',
'src/bioExprDivide.cc',
'src/bioExprPower.cc',
'src/bioExprUnaryMinus.cc',
'src/bioExprExp.cc',
'src/bioExprLog.cc',
'src/bioExprNumeric.cc',
'src/bioExprLogLogit.cc',
'src/bioExprLogLogitFullChoiceSet.cc',
'src/bioExprLinearUtility.cc',
'src/bioExpression.cc',
'src/bioSeveralExpressions.cc',
'src/bioExceptions.cc',
'src/bioDerivatives.cc',
'src/bioVectorOfDerivatives.cc',
'src/bioGaussHermite.cc',
'src/bioGhFunction.cc']
extra_compile_args = ['-std=c++11', '-Wall']
extra_link_args = []
if platform.system() == 'Darwin':
extra_compile_args.append('-stdlib=libc++')
extra_link_args.append('-lc++')
if sys.platform == 'win32':
# mismatch between library names
extra_compile_args.append('-D_hypot=hypot')
# as one cannot assume that libstdc++, libgcc and pthreads exists on windows,
# static link them so they are included in the compiled python extension.
extra_link_args.extend(['-mwindows',
'-mms-bitfields',
'-static-libstdc++',
'-static-libgcc',
'-Wl,-Bstatic',
'-lstdc++',
'-lpthread'])
biogeme_extension = setuptools.Extension('biogeme.cbiogeme',
['src/cbiogeme'+ext] + source,
include_dirs=[numpy.get_include()],
extra_compile_args=extra_compile_args,
language='c++11',
define_macros=[('NPY_NO_DEPRECATED_API',
'NPY_1_7_API_VERSION')],
extra_link_args=extra_link_args)
expressions_extension = setuptools.Extension('biogeme.cexpressions',
['src/cexpressions'+ext] + source,
include_dirs=[numpy.get_include()],
extra_compile_args=extra_compile_args,
language='c++11',
define_macros=[('NPY_NO_DEPRECATED_API',
'NPY_1_7_API_VERSION')],
extra_link_args=extra_link_args)
extensions = [biogeme_extension, expressions_extension]
#extensions = [Extension('biogeme.cbiogeme',
# ['src/cbiogeme'+ext]+source,
# include_dirs=[numpy.get_include()],
# extra_compile_args=extra_compile_args,
# language='c++11',
# extra_link_args = ['-lprofiler'])]
#extra_compile_args=['-O0'],
#extra_link_args=['-fsanitize=address','-O1','-fno-omit-frame-pointer','-g']
if USE_CYTHON:
# extensions = cythonize(extensions, language='c++', include_path=[numpy.get_include()])
# WARNING: recently, problems with parallel compilation in cython have
# been reported. Therefore, this feature is currently turned off.
# extensions = cythonize(extensions,
# compiler_directives={'language_level' : "3"},
# include_path=[numpy.get_include()],
# nthreads=8)
extensions = cythonize(extensions,
compiler_directives={'language_level' : "3"},
include_path=[numpy.get_include()])
cmdclass.update({'build_ext': build_ext})
#exec(open('biogeme/version.py').read())
# now we have a `__version__` variable
# later on we use: __version__
with open('README.md', 'r') as fh:
long_description = fh.read()
setuptools.setup(name='biogeme',
version=__version__,
description='Estimation and application of discrete choice models',
url='http://biogeme.epfl.ch',
author='Michel Bierlaire',
keywords='discrete choice maximum likelihood estimation',
author_email='[email protected]',
long_description=long_description,
long_description_content_type='text/markdown',
install_requires=['numpy', 'cython', 'unidecode', 'scipy', 'pandas', 'tqdm'],
packages=setuptools.find_packages(),
include_package_data=True,
package_data={'biogeme': ['_biogeme.pyd']},
package_dir={'biogeme.cbiogeme': 'src'},
cmdclass=cmdclass,
classifiers=[
'Programming Language :: Python :: 3',
'Operating System :: OS Independent',
],
ext_modules=extensions)