-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
378 lines (313 loc) · 12.3 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Much of the build system code was adapted from work done by the pandas
developers [1], which was in turn based on work done in pyzmq [2] and lxml [3].
[1] https://pandas.pydata.org
[2] https://zeromq.github.io/pyzmq/
[3] https://lxml.de/
"""
from collections import defaultdict
import fnmatch
import os
import shutil
import sys
from distutils.version import LooseVersion
from setuptools import setup, Command, find_packages
import pkg_resources
# ------------------------------------------------------------------
# Administrative
# versioning
import versioneer
cmdclass = versioneer.get_cmdclass()
curdir = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(curdir, "README.md")).read()
DISTNAME = 'sm2'
DESCRIPTION = 'Bugfix Fork of statsmodels'
LONG_DESCRIPTION = README
MAINTAINER = 'Brock Mendel'
MAINTAINER_EMAIL = 'jbrockmendel@gmail.com'
# URL = 'https://www.statsmodels.org/'
LICENSE = 'BSD License'
DOWNLOAD_URL = ''
classifiers = ['Development Status :: 4 - Beta',
'Environment :: Console',
'Programming Language :: Cython',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Operating System :: OS Independent',
'Intended Audience :: End Users/Desktop',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'Natural Language :: English',
'License :: OSI Approved :: BSD License',
'Topic :: Scientific/Engineering']
FILES_TO_INCLUDE_IN_PACKAGE = ['LICENSE.txt', 'setup.cfg']
FILES_COPIED_TO_PACKAGE = []
for filename in FILES_TO_INCLUDE_IN_PACKAGE:
if os.path.exists(filename):
dest = os.path.join('sm2', filename)
shutil.copy2(filename, dest)
FILES_COPIED_TO_PACKAGE.append(dest)
ADDITIONAL_PACKAGE_DATA = {
'sm2': FILES_TO_INCLUDE_IN_PACKAGE,
'sm2.datasets.tests': ['*.zip'],
'sm2.iolib.tests.results': ['*.dta'],
'sm2.stats.tests.results': ['*.json'],
'sm2.tsa.vector_ar.tests.results': ['*.npz', '*.dat'],
'sm2.stats.tests': ['*.txt'],
}
# ------------------------------------------------------------------
# Dependencies
# These version cutoffs are largely arbitrary, chosen to be
# almost-up-to-date as of 2018-03-10
extras = {'docs': ['sphinx>=1.3.5',
'matplotlib',
'numpydoc>=0.6.0']}
min_versions = {'numpy': '1.13.0',
'scipy': '0.19.0',
'pandas': '0.22.0',
'patsy': '0.4.0'}
setuptools_kwargs = {
'install_requires': [
"numpy >= {version}".format(version=min_versions['numpy']),
"scipy >= {version}".format(version=min_versions['scipy']),
"pandas >= {version}".format(version=min_versions['pandas']),
"patsy >= {version}".format(version=min_versions['patsy'])
],
'setup_requires': [
'numpy >= {version}'.format(version=min_versions['numpy'])
]}
# ------------------------------------------------------------------
# Cython Preparation & Specification
_pxifiles = ['sm2/tsa/regime_switching/_kim_smoother.pyx.in',
'sm2/tsa/regime_switching/_hamilton_filter.pyx.in',
'sm2/tsa/statespace/_tools.pyx.in']
# TODO: Can we just put this with the next (only) use of CYTHON_INSTALLED?
min_cython_ver = '0.24'
try:
import Cython
ver = Cython.__version__
_CYTHON_INSTALLED = ver >= LooseVersion(min_cython_ver)
except ImportError:
_CYTHON_INSTALLED = False
# These imports need to be here; setuptools needs to be imported first.
from distutils.extension import Extension # noqa:E402
from distutils.command.build import build # noqa:E402
from distutils.command.build_ext import build_ext as _build_ext # noqa:E402
try:
if not _CYTHON_INSTALLED:
raise ImportError('No supported version of Cython installed.')
try:
from Cython.Distutils.old_build_ext import old_build_ext as _build_ext # noqa:F811,E501
except ImportError:
# Pre 0.25
from Cython.Distutils import build_ext as _build_ext
from Cython.Build import cythonize
cython = True
try:
# TODO: Can we simplify this try/except?
from Cython import Tempita as tempita
except ImportError:
import tempita
except ImportError:
cython = False
class build_ext(_build_ext):
@classmethod
def render_templates(cls):
# if builing from c files, don't need to
# generate template output
if cython:
for pxifile in _pxifiles:
# build pxifiles first, template extension must be .pxi.in
assert pxifile.endswith(('.pxi.in', 'pyx.in'))
outfile = pxifile[:-3]
if (os.path.exists(outfile) and
os.stat(pxifile).st_mtime < os.stat(outfile).st_mtime):
# if .pxi.in is not updated, no need to output .pxi
continue
with open(pxifile, "r") as f:
tmpl = f.read()
pyxcontent = tempita.sub(tmpl)
with open(outfile, "w") as f:
f.write(pyxcontent)
def build_extensions(self):
self.render_templates()
numpy_incl = pkg_resources.resource_filename('numpy', 'core/include')
for ext in self.extensions:
if (hasattr(ext, 'include_dirs') and
numpy_incl not in ext.include_dirs):
ext.include_dirs.append(numpy_incl)
_build_ext.build_extensions(self)
class CleanCommand(Command):
"""Custom distutils command to clean the .so and .pyc files."""
# Identical to pandas version except for self._clean_exclude
user_options = [("all", "a", "")]
def initialize_options(self):
self.all = True
self._clean_me = []
self._clean_trees = []
self._clean_exclude = ["bspline_ext.c",
"bspline_impl.c"]
for root, dirs, files in list(os.walk('sm2')):
for f in files:
if f in self._clean_exclude:
continue
if os.path.splitext(f)[-1] in ('.pyc', '.so', '.o',
'.pyo',
'.pyd', '.c', '.orig'):
self._clean_me.append(os.path.join(root, f))
for d in dirs:
if d == '__pycache__':
self._clean_trees.append(os.path.join(root, d))
for d in ('build', 'dist'):
if os.path.exists(d):
self._clean_trees.append(d)
def finalize_options(self):
pass
def run(self):
for clean_me in self._clean_me:
try:
os.unlink(clean_me)
except Exception:
pass
for clean_tree in self._clean_trees:
try:
shutil.rmtree(clean_tree)
except Exception:
pass
class CheckingBuildExt(build_ext):
"""Subclass build_ext to get clearer report if Cython is necessary."""
# effectively identical to pandas version
def check_cython_extensions(self, extensions):
for ext in extensions:
for src in ext.sources:
if not os.path.exists(src):
print("{}: -> [{}]".format(ext.name, ext.sources))
raise Exception("""Cython-generated file '{src}' not found.
Cython is required to compile sm2 from a development branch.
Please install Cython or download a release package of sm2.
""".format(src=src))
def build_extensions(self):
self.check_cython_extensions(self.extensions)
build_ext.build_extensions(self)
class CythonCommand(build_ext):
"""Custom distutils command subclassed from Cython.Distutils.build_ext
to compile pyx->c, and stop there. All this does is override the
C-compile method build_extension() with a no-op."""
# Copied verbatim from pandas setup.py
def build_extension(self, ext):
pass
class DummyBuildSrc(Command):
""" numpy's build_src command interferes with Cython's build_ext.
"""
# identical to pandas version
user_options = []
def initialize_options(self):
self.py_modules_dict = {}
def finalize_options(self):
pass
def run(self):
pass
def _cythonize(extensions, *args, **kwargs):
"""
Render tempita templates before calling cythonize
Avoid running cythonize on `python setup.py clean`
See https://github.com/cython/cython/issues/1495
"""
if len(sys.argv) > 1 and 'clean' in sys.argv:
return
for ext in extensions:
if (hasattr(ext, 'include_dirs') and
numpy_incl not in ext.include_dirs):
ext.include_dirs.append(numpy_incl)
if cython:
build_ext.render_templates()
return cythonize(extensions, *args, **kwargs)
else:
return extensions
cmdclass['clean'] = CleanCommand
cmdclass['build'] = build
cmdclass['build_ext'] = CheckingBuildExt
if cython:
suffix = '.pyx'
cmdclass['cython'] = CythonCommand
else:
suffix = '.c'
cmdclass['build_src'] = DummyBuildSrc
# https://medium.com/@dfdeshom/\
# better-test-coverage-workflow-for-cython-modules-631615eb197a
# Set linetrace environment variable to enable coverage measurement
# for cython files
linetrace = os.environ.get('linetrace', False)
CYTHON_TRACE = str(int(bool(linetrace)))
directives = {'linetrace': False}
macros = []
if linetrace:
# https://pypkg.com/pypi/pytest-cython/f/tests/example-project/setup.py
directives['linetrace'] = True
macros = [('CYTHON_TRACE', '1'), ('CYTHON_TRACE_NOGIL', '1')]
numpy_incl = pkg_resources.resource_filename('numpy', 'core/include')
from numpy.distutils.misc_util import get_info # noqa:E402
# TODO: Can we do this without numpy import?
npymath = get_info("npymath")
extensions = [
Extension("sm2.tsa.kalmanf.kalman_loglike",
["sm2/tsa/kalmanf/kalman_loglike.pyx"],
include_dirs=["sm2/src", numpy_incl],
depends=["sm2/src/capsule.h"],
define_macros=macros)] + [
Extension(x.replace('/', '.').replace('.pyx.in', ''),
[x.replace('.pyx.in', '.pyx')],
include_dirs=["sm2/src", numpy_incl] + npymath['include_dirs'],
libraries=npymath["libraries"],
library_dirs=npymath["library_dirs"],
define_macros=macros)
for x in _pxifiles]
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# Construct package data
# ------------------------------------------------------------------
package_data = defaultdict(list)
filetypes = ['*.csv', '*.txt', '*.dta']
dsetdir = os.path.join(curdir, 'sm2', 'datasets')
for root, _, filenames in os.walk(dsetdir):
matches = []
for filetype in filetypes:
for filename in fnmatch.filter(filenames, filetype):
matches.append(filename)
if matches:
key = '.'.join(os.path.relpath(root).split(os.path.sep))
package_data[key] = filetypes
for root, _, _ in os.walk(os.path.join(curdir, 'sm2')):
if root.endswith('results'):
key = '.'.join(os.path.relpath(root).split(os.path.sep))
package_data[key] = filetypes
for path, filetypes in ADDITIONAL_PACKAGE_DATA.items():
package_data[path].extend(filetypes)
setup(name=DISTNAME,
version=versioneer.get_version(),
maintainer=MAINTAINER,
ext_modules=_cythonize(extensions, compiler_directives=directives),
maintainer_email=MAINTAINER_EMAIL,
description=DESCRIPTION,
license=LICENSE,
# url=URL,
download_url=DOWNLOAD_URL,
long_description=LONG_DESCRIPTION,
classifiers=classifiers,
platforms='any',
cmdclass=cmdclass,
packages=find_packages(),
package_data=package_data,
include_package_data=False, # True will install all files in repo
extras_require=extras,
zip_safe=False,
data_files=[('', ['LICENSE.txt', 'setup.cfg'])],
**setuptools_kwargs)
# Clean-up copied files
# GH#5195
for copy in FILES_COPIED_TO_PACKAGE:
os.unlink(copy)