This repository has been archived by the owner on Jan 23, 2020. It is now read-only.
forked from pennmem/ptsa
/
setup.py
241 lines (195 loc) · 6.66 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
from contextlib import contextmanager
import distutils
import json
import os
import os.path as osp
from setuptools import setup, Extension, find_packages
from setuptools.command.build_py import build_py
from setuptools.command.install import install
from setuptools.command.develop import develop
import site
import subprocess
import sys
import numpy as np
root_dir = osp.dirname(osp.abspath(__file__))
build_subdir = 'build'
morlet_dir = osp.join(root_dir, 'ptsa', 'extensions', 'morlet')
extensions_dir = osp.join(root_dir, 'ptsa', 'extensions')
circ_stat_dir = osp.join(root_dir, 'ptsa', 'extensions', 'circular_stat')
for path in site.getsitepackages():
if path.endswith("site-packages"):
site_packages = path
break
else:
raise RuntimeError("site-packages not found?!?")
# see recipe http://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module
# for windows install see http://stackoverflow.com/questions/2817869/error-unable-to-find-vcvarsall-bat
# for visual studio compilation you need to SET VS90COMNTOOLS=%VS140COMNTOOLS%
if sys.platform.startswith("win"):
os.environ["VS90COMNTOOLS"] = os.environ["VS140COMNTOOLS"]
@contextmanager
def chdir(path):
"""Change to a directory and then change back."""
orig_cwd = os.getcwd()
os.chdir(path)
yield
os.chdir(orig_cwd)
def check_dependencies():
"""Checks for dependencies that aren't installable via pip."""
if not distutils.spawn.find_executable('swig'):
raise OSError('Missing swig - please `conda install swig`')
def get_version_str():
from ptsa import __version__
return __version__
def get_include_dirs():
"""Return extra include directories for building extensions."""
dirs = [
np.get_include(),
osp.join(extensions_dir, 'ThreadPool'),
]
conda_include = []
try:
# Note: we can't just call conda because that might find the wrong one.
# Instead, we use the CONDA_EXE environment variable that conda should
# set.
p = subprocess.Popen([os.environ["CONDA_EXE"], "info", "--json"],
env=os.environ,
stdout=subprocess.PIPE)
stdout, _ = p.communicate()
info = json.loads(stdout)
conda_include.append(os.path.join(info["active_prefix"], "include"))
except Exception as e:
# looks like we're not using conda
pass
dirs += conda_include
return dirs
def get_fftw_libs():
if sys.platform.startswith("win"):
return ['libfftw3-3']
else:
return ['fftw3']
def get_compiler_args():
"""Return extra compiler arguments for building extensions."""
if sys.platform.startswith('darwin'):
return ['-std=c++14', '-stdlib=libc++', '-mmacosx-version-min=10.9']
elif sys.platform.startswith('win'):
return ['/EHsc'] # exception handling
else:
return ['-std=c++14']
class CustomBuild(build_py):
def run(self):
self.run_command("build_ext")
build_py.run(self)
class CustomDevelop(develop):
def run(self):
self.run_command("build_ext")
develop.run(self)
class CustomInstall(install):
def run(self):
self.run_command("build_ext")
install.run(self)
# FIXME: check if this is still necessary on windows
# if sys.platform.startswith("win"):
# dll_path = osp.join(third_party_install_dir, "libfftw3-3.dll")
# ext_path = osp.join(site_packages, "ptsa", "extensions")
# morlet_path = osp.join(ext_path, "morlet")
# circ_stat_path = osp.join(ext_path, "circular_stat")
# shutil.copy(dll_path, morlet_path)
# shutil.copy(dll_path, circ_stat_path)
def make_pybind_extension(module, **kwargs):
"""Create a pybind11 extension module.
This requires a compiler that supports C++11 or newer.
Parameters
----------
module : str
Name of the extension module to produce.
Returns
-------
Extension
Raises
------
ImportError
If pybind11 is not found.
Notes
-----
This will automatically include the pybind11 and numpy include directories.
Keyword arguments are passsed to the constructor of :class:`Extension`.
"""
import pybind11
include_dirs = kwargs.pop('include_dirs', [])
include_dirs += [
pybind11.get_include(),
pybind11.get_include(user=True),
np.get_include(),
]
compile_args = kwargs.pop('extra_compile_args', [])
compile_args += get_compiler_args()
return Extension(
module,
include_dirs=include_dirs,
extra_compile_args=compile_args,
language='c++',
**kwargs
)
ext_modules = [
Extension(
'ptsa.extensions.morlet._morlet',
sources=[osp.join(morlet_dir, 'morlet.cpp'),
osp.join(morlet_dir, 'MorletWaveletTransformMP.cpp'),
osp.join(morlet_dir, 'morlet.i')],
swig_opts=['-c++'],
include_dirs=get_include_dirs(),
extra_compile_args=get_compiler_args(),
libraries=get_fftw_libs(),
),
Extension(
'ptsa.extensions.circular_stat._circular_stat',
sources=[
osp.join(circ_stat_dir, 'circular_stat.cpp'),
osp.join(circ_stat_dir, 'circular_stat.i')
],
swig_opts=['-c++'],
include_dirs=get_include_dirs(),
extra_compile_args=get_compiler_args(),
),
]
# Try to add edffile extension
try:
ext_modules += [
make_pybind_extension(
'ptsa.extensions.edf.edffile',
sources=[
'ptsa/extensions/edf/edflib.cpp',
'ptsa/extensions/edf/edffile.cpp',
'ptsa/extensions/edf/wrap.cpp',
],
)
]
except ImportError:
print("\n\nWARNING\n\n"
"pybind11 not found - you will be unable to read EDF files")
check_dependencies()
setup(
name='ptsa',
version=get_version_str(),
maintainer=['Per B. Sederberg', 'Maciek Swat'],
maintainer_email=['psederberg@gmail.com', 'maciekswat@gmail.com'],
url='https://github.com/maciekswat/ptsa_new',
cmdclass={
'build_py': CustomBuild,
'install': CustomInstall,
'develop': CustomDevelop,
},
ext_modules=ext_modules,
# This doesn't seem to work because of custom commands. For now, just
# install the prereqs with conda/pip.
# See: http://stackoverflow.com/questions/20194565/running-custom-setuptools-build-during-install#20196065
install_requires=[
"numpy",
"scipy",
"xarray",
],
packages=find_packages(
exclude=['*.tests', 'tests', 'tests.*', '*.outdated_tests']
),
)