forked from DavidLapous/multipers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
186 lines (157 loc) · 4.93 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
# import contextlib
import filecmp
import os
import shutil
import platform
import sys
from pathlib import Path
import numpy as np
from Cython import Tempita
from Cython.Build import cythonize
from Cython.Compiler import Options
from setuptools import Extension, setup
Options.docstrings = True
Options.embed_pos_in_docstring = True
Options.fast_fail = True
# Options.warning_errors = True
os.system("mkdir build/")
os.system("mkdir build/tmp")
def was_modified(file):
tail = os.path.basename(file)
new_file = "build/tmp/" + tail
if not os.path.isfile(new_file):
print(f"File {file} was modified.")
shutil.copyfile(file, new_file)
return True
else:
res = not filecmp.cmp(new_file, file, shallow=False)
if res:
print(f"File {file} was modified.")
shutil.copyfile(file, new_file)
return res
full_build = False
if was_modified("_tempita_grid_gen.py"):
full_build = True
# credit to sklearn with just a few modifications:
# https://github.com/scikit-learn/scikit-learn/blob/156ef1b7fe9bc0ee5b281634cfd56b9c54e83277/sklearn/_build_utils/tempita.py
# took it out to not having to depend on a sklearn version in addition to a cython version
def process_tempita(fromfile):
"""Process tempita templated file and write out the result.
The template file is expected to end in `.c.tp` or `.pyx.tp`:
E.g. processing `template.c.tp` generates `template.c`.
"""
if not was_modified(fromfile) and not full_build:
return
with open(fromfile, "r", encoding="utf-8") as f:
template_content = f.read()
template = Tempita.Template(template_content)
content = template.substitute()
outfile = os.path.splitext(fromfile)[0]
with open(outfile, "w", encoding="utf-8") as f:
f.write(content)
cython_modules = [
"simplex_tree_multi",
"io",
# "rank_invariant",
"function_rips",
"mma_structures",
"multiparameter_module_approximation",
"point_measure",
"grids",
"slicer",
]
templated_cython_modules = [
"filtration_conversions.pxd",
"slicer.pxd",
"mma_structures.pyx",
"simplex_tree_multi.pyx",
"slicer.pyx",
]
## generates some parameter files (Tempita fails with python<3.12)
# TODO: see if there is a way to avoid _tempita_grid_gen.py or a nicer way to do it
os.system("python _tempita_grid_gen.py")
for mod in templated_cython_modules:
process_tempita(f"multipers/{mod}.tp")
## Broken on mac
# n_jobs = 1
# with contextlib.suppress(ImportError):
# import joblib
# n_jobs = joblib.cpu_count()
cythonize_flags = {
# "depfile": True,
# "nthreads": n_jobs, # Broken on mac
# "show_all_warnings": True,
}
cython_compiler_directives = {
"language_level": 3,
"embedsignature": True,
"embedsignature.format": "python",
"binding": True,
"infer_types": True,
"boundscheck": False,
"wraparound": True,
"iterable_coroutine": True,
# "profile":True,
# "unraisable_tracebacks":True,
"annotation_typing": True,
"emit_code_comments": True,
"initializedcheck": False,
# "nonecheck": False,
"cdivision": True,
"profile": False,
}
# When venv is not properly set, we have to add the current python path
# removes lib / python3.x / site-packages
PYTHON_ENV_PATH = sys.prefix
cpp_dirs = [
"multipers/gudhi",
"multipers",
# "multipers/multiparameter_module_approximation",
# "multipers/multi_parameter_rank_invariant",
# "multipers/tensor",
np.get_include(),
PYTHON_ENV_PATH + "/include/", # Unix
PYTHON_ENV_PATH + "/Library/include/", # Windows
]
cpp_dirs = [str(Path(stuff).expanduser().resolve()) for stuff in cpp_dirs]
library_dirs = [
PYTHON_ENV_PATH + "/lib/", # Unix
PYTHON_ENV_PATH + "/Library/lib/", # Windows
]
library_dirs = [str(Path(stuff).expanduser().resolve()) for stuff in library_dirs]
print("Include dirs:")
print(cpp_dirs)
print("Library dirs:")
print(library_dirs)
extensions = [
Extension(
f"multipers.{module}",
sources=[
f"multipers/{module}.pyx",
],
language="c++",
extra_compile_args=[
"-O3", # -Ofast disables infinity values for filtration values
# "-g",
# "-march=native",
"/std:c++20" if platform.system() == "Windows" else "-std=c++20", # Windows doesn't support this yet. TODO: Wait.
# "-fno-aligned-new", # Uncomment this if you have trouble compiling on macos.
"-Wall",
],
extra_link_args=[], ## mvec for python312
include_dirs=cpp_dirs,
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")],
libraries=["tbb"],
library_dirs=library_dirs,
)
for module in cython_modules
]
if __name__ == "__main__":
setup(
name="multipers",
ext_modules=cythonize(
extensions,
compiler_directives=cython_compiler_directives,
**cythonize_flags,
),
)