Skip to content

Commit 9e5819a

Browse files
jeremiedbbadrinjalali
authored andcommitted
MNT Include all pxd files in the package (scikit-learn#15626)
* include all pxd files in package * _libsvm _liblinear to pxi + add a test * simplify stuff + use pathlib * cln * more cln
1 parent 6419f65 commit 9e5819a

File tree

8 files changed

+66
-16
lines changed

8 files changed

+66
-16
lines changed
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""Utility for testing presence and usability of .pxd files in the installation
2+
3+
Usage:
4+
------
5+
python check_pxd_in_installation.py path/to/install_dir/of/scikit-learn
6+
"""
7+
8+
import os
9+
import sys
10+
import pathlib
11+
import tempfile
12+
import textwrap
13+
import subprocess
14+
15+
16+
sklearn_dir = pathlib.Path(sys.argv[1])
17+
pxd_files = list(sklearn_dir.glob("**/*.pxd"))
18+
19+
print("> Found pxd files:")
20+
for pxd_file in pxd_files:
21+
print(' -', pxd_file)
22+
23+
print("\n> Trying to compile a cython extension cimporting all corresponding "
24+
"modules\n")
25+
with tempfile.TemporaryDirectory() as tmpdir:
26+
tmpdir = pathlib.Path(tmpdir)
27+
# A cython test file which cimports all modules corresponding to found
28+
# pxd files.
29+
# e.g. sklearn/tree/_utils.pxd becomes `cimport sklearn.tree._utils`
30+
with open(tmpdir / 'tst.pyx', 'w') as f:
31+
for pxd_file in pxd_files:
32+
to_import = str(pxd_file.relative_to(sklearn_dir))
33+
to_import = to_import.replace(os.path.sep, '.')
34+
to_import = to_import.replace('.pxd', '')
35+
f.write('cimport sklearn.' + to_import + '\n')
36+
37+
# A basic setup file to build the test file.
38+
# We set the language to c++ and we use numpy.get_include() because
39+
# some modules require it.
40+
with open(tmpdir / 'setup_tst.py', 'w') as f:
41+
f.write(textwrap.dedent(
42+
"""
43+
from distutils.core import setup
44+
from distutils.extension import Extension
45+
from Cython.Build import cythonize
46+
import numpy
47+
48+
extensions = [Extension("tst",
49+
sources=["tst.pyx"],
50+
language="c++",
51+
include_dirs=[numpy.get_include()])]
52+
53+
setup(ext_modules=cythonize(extensions))
54+
"""))
55+
56+
subprocess.run(["python", "setup_tst.py", "build_ext", "-i"],
57+
check=True, cwd=tmpdir)
58+
59+
print("\n> Compilation succeeded !")

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def setup_package():
259259
'scipy>={}'.format(SCIPY_MIN_VERSION),
260260
'joblib>={}'.format(JOBLIB_MIN_VERSION)
261261
],
262+
package_data={'': ['*.pxd']},
262263
**extra_setuptools_args)
263264

264265
if len(sys.argv) == 1 or (

sklearn/svm/_liblinear.pxd renamed to sklearn/svm/_liblinear.pxi

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
cimport numpy as np
2-
3-
41
cdef extern from "_cython_blas_helpers.h":
52
ctypedef double (*dot_func)(int, double*, int, double*, int)
63
ctypedef void (*axpy_func)(int, double, double*, int, double*, int)
@@ -12,6 +9,7 @@ cdef extern from "_cython_blas_helpers.h":
129
scal_func scal
1310
nrm2_func nrm2
1411

12+
1513
cdef extern from "linear.h":
1614
cdef struct feature_node
1715
cdef struct problem
@@ -28,6 +26,7 @@ cdef extern from "linear.h":
2826
void free_and_destroy_model (model **)
2927
void destroy_param (parameter *)
3028

29+
3130
cdef extern from "liblinear_helper.c":
3231
void copy_w(void *, model *, int)
3332
parameter *set_parameter(int, double, double, int, char *, char *, int, int, double)

sklearn/svm/_liblinear.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cimport numpy as np
99

1010
from ..utils._cython_blas cimport _dot, _axpy, _scal, _nrm2
1111

12+
include "_liblinear.pxi"
13+
1214
np.import_array()
1315

1416

sklearn/svm/_libsvm.pxd renamed to sklearn/svm/_libsvm.pxi

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
cimport numpy as np
2-
31
################################################################################
42
# Includes
53

sklearn/svm/_libsvm.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import numpy as np
3535
cimport numpy as np
3636
from libc.stdlib cimport free
3737

38+
include "_libsvm.pxi"
39+
3840
cdef extern from *:
3941
ctypedef struct svm_parameter:
4042
pass

sklearn/tree/setup.py

-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ def configuration(parent_package="", top_path=None):
3131
extra_compile_args=["-O3"])
3232

3333
config.add_subpackage("tests")
34-
config.add_data_files("_criterion.pxd")
35-
config.add_data_files("_splitter.pxd")
36-
config.add_data_files("_tree.pxd")
37-
config.add_data_files("_utils.pxd")
3834

3935
return config
4036

sklearn/utils/_weight_vector.pxd

-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
"""Efficient (dense) parameter vector implementation for linear models. """
22

3-
cimport numpy as np
4-
5-
6-
cdef extern from "math.h":
7-
cdef extern double sqrt(double x)
8-
9-
103
cdef class WeightVector(object):
114
cdef double *w_data_ptr
125
cdef double *aw_data_ptr

0 commit comments

Comments
 (0)