Skip to content

Commit 891197c

Browse files
authored
Fix build steps (#244)
* first attempt to fix building; sdist works; wheel misses the compiled objects * bumps Cython version to 3.0.0; adds explicit Cython language levels and removes numpy deprecations warnings * build.py might need to be changed in the future, but this should make the package installable as a hot fix
1 parent 10f9a9a commit 891197c

File tree

7 files changed

+70
-50
lines changed

7 files changed

+70
-50
lines changed

build.py

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
1-
from setuptools import Extension
2-
from setuptools.command.build_ext import build_ext as _build_ext
1+
import os
2+
import shutil
33
import sys
4+
from distutils.core import Distribution, Extension
45

6+
from Cython.Build import build_ext, cythonize
7+
import numpy
58

6-
# bootstrap numpy
7-
# https://stackoverflow.com/questions/19919905/how-to-bootstrap-numpy-installation-in-setup-py
8-
class build_ext(_build_ext):
9-
def finalize_options(self):
10-
_build_ext.finalize_options(self)
11-
# Prevent numpy from thinking it is still in its setup process:
12-
#__builtins__.__NUMPY_SETUP__ = False
13-
import numpy
14-
self.include_dirs.append(numpy.get_include())
9+
cython_dir = "pyndl"
1510

11+
ndl_parallel = Extension("pyndl.ndl_parallel",
12+
["pyndl/ndl_parallel.pyx"],
13+
include_dirs=[numpy.get_include()])
14+
ndl_openmp = Extension("pyndl.ndl_openmp",
15+
["pyndl/ndl_openmp.pyx"],
16+
extra_compile_args=['-fopenmp'],
17+
extra_link_args=['-fopenmp'],
18+
include_dirs=[numpy.get_include()])
19+
corr_parallel = Extension("pyndl.correlation_openmp",
20+
["pyndl/correlation_openmp.pyx"],
21+
extra_compile_args=['-fopenmp'],
22+
extra_link_args=['-fopenmp'],
23+
include_dirs=[numpy.get_include()])
1624

17-
ndl_parallel = Extension("pyndl.ndl_parallel", ["pyndl/ndl_parallel.pyx"])
18-
ndl_openmp = Extension("pyndl.ndl_openmp", ["pyndl/ndl_openmp.pyx"],
19-
extra_compile_args=['-fopenmp'], extra_link_args=['-fopenmp'])
20-
corr_parallel = Extension("pyndl.correlation_openmp", ["pyndl/correlation_openmp.pyx"],
21-
extra_compile_args=['-fopenmp'], extra_link_args=['-fopenmp'])
22-
# by giving ``cython`` as ``install_requires`` this will be ``cythonized``
23-
# automagically
24-
25-
ext_modules = []
25+
extensions = []
26+
include_paths = []
2627
if sys.platform.startswith('linux'):
27-
ext_modules = [ndl_parallel, ndl_openmp, corr_parallel]
28+
extensions = [ndl_parallel, ndl_openmp, corr_parallel]
29+
include_paths = [cython_dir, cython_dir, cython_dir]
2830
elif sys.platform.startswith('win32'):
29-
ext_modules = [ndl_parallel] # skip openmp installation on windows for now
31+
extensions = [ndl_parallel] # skip openmp installation on windows for now
32+
include_paths = [cython_dir]
3033
elif sys.platform.startswith('darwin'):
31-
ext_modules = [ndl_parallel] # skip openmp installation on macos for now
34+
extensions = [ndl_parallel] # skip openmp installation on macos for now
35+
include_paths = [cython_dir]
36+
37+
ext_modules = cythonize(extensions, include_path=include_paths)
38+
dist = Distribution({"ext_modules": ext_modules})
39+
cmd = build_ext(dist)
40+
cmd.ensure_finalized()
41+
cmd.run()
3242

43+
for output in cmd.get_outputs():
44+
relative_extension = os.path.relpath(output, cmd.build_lib)
45+
shutil.copyfile(output, relative_extension)
3346

34-
def build(setup_kwargs):
35-
"""
36-
This function is mandatory in order to build the extensions.
37-
"""
38-
setup_kwargs.update({
39-
'ext_modules': ext_modules,
40-
'cmdclass': {
41-
'build_ext': build_ext
42-
}
43-
})

pyndl/correlation_openmp.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# cython: language_level=3
2+
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION
3+
14
import numpy as np
25
cimport numpy as np
36

pyndl/error_codes.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# cython: language_level=3
2+
13
cdef enum ErrorCode:
24
NO_ERROR = 0
35
MAGIC_NUMBER_DOES_NOT_MATCH = 1

pyndl/ndl_openmp.pyx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1+
# cython: language_level=3
2+
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION
3+
14
import numpy as np
25
import math
36
cimport numpy as np
47
ctypedef np.float64_t dtype_t
58
cimport cython
69
from cython.parallel cimport parallel, prange
710

8-
from ndl_parallel cimport (learn_inplace_binary_to_binary_ptr,
9-
learn_inplace_binary_to_real_ptr,
10-
learn_inplace_real_to_real_ptr,
11-
learn_inplace_real_to_binary_ptr)
12-
from error_codes cimport (ErrorCode,
13-
NO_ERROR,
14-
MAGIC_NUMBER_DOES_NOT_MATCH,
15-
VERSION_NUMBER_DOES_NOT_MATCH,
16-
INITIAL_ERROR_CODE,
17-
ONLY_ONE_OUTCOME_PER_EVENT,
18-
ERROR_CODES)
11+
from .ndl_parallel cimport (learn_inplace_binary_to_binary_ptr,
12+
learn_inplace_binary_to_real_ptr,
13+
learn_inplace_real_to_real_ptr,
14+
learn_inplace_real_to_binary_ptr)
15+
from .error_codes cimport (ErrorCode,
16+
NO_ERROR,
17+
MAGIC_NUMBER_DOES_NOT_MATCH,
18+
VERSION_NUMBER_DOES_NOT_MATCH,
19+
INITIAL_ERROR_CODE,
20+
ONLY_ONE_OUTCOME_PER_EVENT,
21+
ERROR_CODES)
1922

2023

2124
def learn_inplace_binary_to_binary(binary_file_paths,

pyndl/ndl_parallel.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# cython: language_level=3
2+
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION
3+
14
cimport numpy as np
25
ctypedef np.float64_t dtype_t
36
from error_codes cimport ErrorCode

pyndl/ndl_parallel.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# cython: language_level=3
2+
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION
3+
14
import numpy as np
25
import math
36
from libc.stdlib cimport abort, malloc, free

pyproject.toml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pyndl"
3-
version = "1.1.1"
3+
version = "1.1.2"
44
description = "Naive discriminative learning implements learning and classification models based on the Rescorla-Wagner equations."
55

66
license = "MIT"
@@ -27,16 +27,14 @@ classifiers = ['Development Status :: 5 - Production/Stable',
2727
'Topic :: Scientific/Engineering :: Artificial Intelligence',
2828
'Topic :: Scientific/Engineering :: Information Analysis',]
2929

30-
build = "build.py"
31-
3230
[tool.poetry.dependencies]
3331
python = ">=3.8,<3.12" # Compatible python versions must be declared here
3432
numpy = "^1.23.1"
3533
scipy = "^1.9.0"
3634
pandas = "^1.4.3"
3735
xarray = "^2022.6.0"
3836
netCDF4 = "^1.6.0"
39-
Cython = "^0.29.32"
37+
Cython = "^3.0.0"
4038

4139
[tool.poetry.dev-dependencies]
4240
pytest = "^7.0"
@@ -70,5 +68,10 @@ addopts = '--doctest-glob "*.rst"'
7068
disable = "E1101"
7169

7270
[build-system]
73-
requires = ["poetry>=1.0.0", "setuptools", "Cython", "numpy"]
74-
build-backend = "poetry.masonry.api"
71+
requires = ["poetry-core", "setuptools", "Cython", "numpy"]
72+
build-backend = "poetry.core.masonry.api"
73+
74+
[tool.poetry.build]
75+
generate-setup-file = false
76+
script = 'build.py'
77+

0 commit comments

Comments
 (0)