Skip to content

MNT remove unnecessary cython.import_array #28656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def check_package_status(package, min_version):
{"sources": ["_loss.pyx.tp"]},
],
"cluster": [
{"sources": ["_dbscan_inner.pyx"], "language": "c++", "include_np": True},
{"sources": ["_dbscan_inner.pyx"], "language": "c++"},
{"sources": ["_hierarchical_fast.pyx"], "language": "c++", "include_np": True},
{"sources": ["_k_means_common.pyx"], "include_np": True},
{"sources": ["_k_means_lloyd.pyx"], "include_np": True},
Expand All @@ -221,42 +221,42 @@ def check_package_status(package, min_version):
}
],
"decomposition": [
{"sources": ["_online_lda_fast.pyx"], "include_np": True},
{"sources": ["_online_lda_fast.pyx"]},
{"sources": ["_cdnmf_fast.pyx"], "include_np": True},
],
"ensemble": [
{"sources": ["_gradient_boosting.pyx"], "include_np": True},
],
"ensemble._hist_gradient_boosting": [
{"sources": ["_gradient_boosting.pyx"], "include_np": True},
{"sources": ["histogram.pyx"], "include_np": True},
{"sources": ["splitting.pyx"], "include_np": True},
{"sources": ["_binning.pyx"], "include_np": True},
{"sources": ["_predictor.pyx"], "include_np": True},
{"sources": ["_bitset.pyx"], "include_np": True},
{"sources": ["common.pyx"], "include_np": True},
{"sources": ["_gradient_boosting.pyx"]},
{"sources": ["histogram.pyx"]},
{"sources": ["splitting.pyx"]},
{"sources": ["_binning.pyx"]},
{"sources": ["_predictor.pyx"]},
{"sources": ["_bitset.pyx"]},
{"sources": ["common.pyx"]},
],
"feature_extraction": [
{"sources": ["_hashing_fast.pyx"], "language": "c++", "include_np": True},
],
"linear_model": [
{"sources": ["_cd_fast.pyx"], "include_np": True},
{"sources": ["_sgd_fast.pyx.tp"], "include_np": True},
{"sources": ["_sag_fast.pyx.tp"], "include_np": True},
{"sources": ["_cd_fast.pyx"]},
{"sources": ["_sgd_fast.pyx.tp"]},
{"sources": ["_sag_fast.pyx.tp"]},
],
"manifold": [
{"sources": ["_utils.pyx"], "include_np": True},
{"sources": ["_utils.pyx"]},
{"sources": ["_barnes_hut_tsne.pyx"], "include_np": True},
],
"metrics": [
{"sources": ["_pairwise_fast.pyx"], "include_np": True},
{"sources": ["_pairwise_fast.pyx"]},
{
"sources": ["_dist_metrics.pyx.tp", "_dist_metrics.pxd.tp"],
"include_np": True,
},
],
"metrics.cluster": [
{"sources": ["_expected_mutual_info_fast.pyx"], "include_np": True},
{"sources": ["_expected_mutual_info_fast.pyx"]},
],
"metrics._pairwise_distances_reduction": [
{
Expand Down Expand Up @@ -305,7 +305,6 @@ def check_package_status(package, min_version):
{"sources": ["_csr_polynomial_expansion.pyx"]},
{
"sources": ["_target_encoder_fast.pyx"],
"include_np": True,
"language": "c++",
"extra_compile_args": ["-std=c++11"],
},
Expand All @@ -320,7 +319,6 @@ def check_package_status(package, min_version):
"svm": [
{
"sources": ["_newrand.pyx"],
"include_np": True,
"include_dirs": [join("src", "newrand")],
"language": "c++",
# Use C++11 random number generator fix
Expand All @@ -341,7 +339,6 @@ def check_package_status(package, min_version):
],
"libraries": ["libsvm-skl"],
"extra_link_args": ["-lstdc++"],
"include_np": True,
},
{
"sources": ["_liblinear.pyx"],
Expand All @@ -351,7 +348,6 @@ def check_package_status(package, min_version):
join("src", "newrand"),
join("..", "utils"),
],
"include_np": True,
"depends": [
join("src", "liblinear", "tron.h"),
join("src", "liblinear", "linear.h"),
Expand All @@ -367,7 +363,6 @@ def check_package_status(package, min_version):
join("src", "libsvm"),
join("src", "newrand"),
],
"include_np": True,
"depends": [
join("src", "libsvm", "svm.h"),
join("src", "newrand", "newrand.h"),
Expand All @@ -388,22 +383,18 @@ def check_package_status(package, min_version):
{"sources": ["_utils.pyx"], "include_np": True, "optimization_level": "O3"},
],
"utils": [
{"sources": ["sparsefuncs_fast.pyx"], "include_np": True},
{"sources": ["sparsefuncs_fast.pyx"]},
{"sources": ["_cython_blas.pyx"]},
{"sources": ["arrayfuncs.pyx"]},
{
"sources": ["murmurhash.pyx", join("src", "MurmurHash3.cpp")],
"include_dirs": ["src"],
"include_np": True,
},
{"sources": ["_fast_dict.pyx"], "language": "c++"},
{"sources": ["_openmp_helpers.pyx"]},
{"sources": ["_seq_dataset.pyx.tp", "_seq_dataset.pxd.tp"], "include_np": True},
{
"sources": ["_weight_vector.pyx.tp", "_weight_vector.pxd.tp"],
"include_np": True,
},
{"sources": ["_random.pyx"], "include_np": True},
{"sources": ["_seq_dataset.pyx.tp", "_seq_dataset.pxd.tp"]},
{"sources": ["_weight_vector.pyx.tp", "_weight_vector.pxd.tp"]},
{"sources": ["_random.pyx"]},
{"sources": ["_typedefs.pyx"]},
{"sources": ["_heap.pyx"]},
{"sources": ["_sorting.pyx"]},
Expand Down
13 changes: 6 additions & 7 deletions sklearn/cluster/_dbscan_inner.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
# License: 3-clause BSD

from libcpp.vector cimport vector
cimport numpy as cnp

cnp.import_array()
from ..utils._typedefs cimport uint8_t, intp_t


def dbscan_inner(const cnp.uint8_t[::1] is_core,
def dbscan_inner(const uint8_t[::1] is_core,
object[:] neighborhoods,
cnp.npy_intp[::1] labels):
cdef cnp.npy_intp i, label_num = 0, v
cdef cnp.npy_intp[:] neighb
cdef vector[cnp.npy_intp] stack
intp_t[::1] labels):
cdef intp_t i, label_num = 0, v
cdef intp_t[:] neighb
cdef vector[intp_t] stack

for i in range(labels.shape[0]):
if labels[i] != -1 or not is_core[i]:
Expand Down
15 changes: 7 additions & 8 deletions sklearn/decomposition/_online_lda_fast.pyx
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
from cython cimport floating

cimport numpy as cnp
import numpy as np

cnp.import_array()

from cython cimport floating
from libc.math cimport exp, fabs, log

from ..utils._typedefs cimport float64_t, intp_t


def mean_change(const floating[:] arr_1, const floating[:] arr_2):
"""Calculate the mean difference between two arrays.

Equivalent to np.abs(arr_1 - arr2).mean().
"""

cdef cnp.float64_t total, diff
cdef cnp.npy_intp i, size
cdef float64_t total, diff
cdef intp_t i, size

size = arr_1.shape[0]
total = 0.0
Expand All @@ -41,7 +40,7 @@ def _dirichlet_expectation_1d(
"""

cdef floating dt, psi_total, total
cdef cnp.npy_intp i, size
cdef intp_t i, size

size = doc_topic.shape[0]

Expand All @@ -67,7 +66,7 @@ def _dirichlet_expectation_2d(const floating[:, :] arr):
"""
cdef floating row_total, psi_row_total
cdef floating[:, :] d_exp
cdef cnp.npy_intp i, j, n_rows, n_cols
cdef intp_t i, j, n_rows, n_cols

n_rows = arr.shape[0]
n_cols = arr.shape[1]
Expand Down
1 change: 0 additions & 1 deletion sklearn/decomposition/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
py.extension_module(
'_online_lda_fast',
'_online_lda_fast.pyx',
dependencies: [np_dep],
cython_args: cython_args,
subdir: 'sklearn/decomposition',
install: true
Expand Down
27 changes: 13 additions & 14 deletions sklearn/ensemble/_gradient_boosting.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ from libc.stdlib cimport free
from libc.string cimport memset

import numpy as np
cimport numpy as cnp
cnp.import_array()

from scipy.sparse import issparse

from ..utils._typedefs cimport float32_t, float64_t, intp_t, int32_t
from ..utils._typedefs cimport float32_t, float64_t, intp_t, int32_t, uint8_t
# Note: _tree uses cimport numpy, cnp.import_array, so we need to include
# numpy headers, see setup.py.
from ..tree._tree cimport Node
from ..tree._tree cimport Tree
from ..tree._utils cimport safe_realloc
Expand All @@ -30,7 +29,7 @@ cdef void _predict_regression_tree_inplace_fast_dense(
double *value,
double scale,
Py_ssize_t k,
cnp.float64_t[:, :] out
float64_t[:, :] out
) noexcept nogil:
"""Predicts output for regression tree and stores it in ``out[i, k]``.

Expand Down Expand Up @@ -79,7 +78,7 @@ def _predict_regression_tree_stages_sparse(
object[:, :] estimators,
object X,
double scale,
cnp.float64_t[:, :] out
float64_t[:, :] out
):
"""Predicts output for regression tree inplace and adds scaled value to ``out[i, k]``.

Expand Down Expand Up @@ -167,7 +166,7 @@ def predict_stages(
object[:, :] estimators,
object X,
double scale,
cnp.float64_t[:, :] out
float64_t[:, :] out
):
"""Add predictions of ``estimators`` to ``out``.

Expand Down Expand Up @@ -214,7 +213,7 @@ def predict_stage(
int stage,
object X,
double scale,
cnp.float64_t[:, :] out
float64_t[:, :] out
):
"""Add predictions of ``estimators[stage]`` to ``out``.

Expand All @@ -227,8 +226,8 @@ def predict_stage(


def _random_sample_mask(
cnp.npy_intp n_total_samples,
cnp.npy_intp n_total_in_bag,
intp_t n_total_samples,
intp_t n_total_in_bag,
random_state
):
"""Create a random sample mask where ``n_total_in_bag`` elements are set.
Expand All @@ -250,11 +249,11 @@ def _random_sample_mask(
An ndarray where ``n_total_in_bag`` elements are set to ``True``
the others are ``False``.
"""
cdef cnp.float64_t[::1] rand = random_state.uniform(size=n_total_samples)
cdef cnp.uint8_t[::1] sample_mask = np_zeros((n_total_samples,), dtype=bool)
cdef float64_t[::1] rand = random_state.uniform(size=n_total_samples)
cdef uint8_t[::1] sample_mask = np_zeros((n_total_samples,), dtype=bool)

cdef cnp.npy_intp n_bagged = 0
cdef cnp.npy_intp i = 0
cdef intp_t n_bagged = 0
cdef intp_t i = 0

for i in range(n_total_samples):
if rand[i] * (n_total_samples - i) < (n_total_in_bag - n_bagged):
Expand Down
2 changes: 1 addition & 1 deletion sklearn/ensemble/_hist_gradient_boosting/_predictor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ from cython.parallel import prange
from libc.math cimport isnan
import numpy as np

from ...utils._typedefs cimport intp_t
from .common cimport X_DTYPE_C
from .common cimport Y_DTYPE_C
from .common import Y_DTYPE
from .common cimport X_BINNED_DTYPE_C
from .common cimport BITSET_INNER_DTYPE_C
from .common cimport node_struct
from ._bitset cimport in_bitset_2d_memoryview
from sklearn.utils._typedefs cimport intp_t


def _predict_from_raw_data( # raw data = non-binned data
Expand Down
17 changes: 8 additions & 9 deletions sklearn/ensemble/_hist_gradient_boosting/common.pxd
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
cimport numpy as cnp
from sklearn.utils._typedefs cimport intp_t
from ...utils._typedefs cimport float32_t, float64_t, intp_t, uint8_t, uint32_t

cnp.import_array()


ctypedef cnp.npy_float64 X_DTYPE_C
ctypedef cnp.npy_uint8 X_BINNED_DTYPE_C
ctypedef cnp.npy_float64 Y_DTYPE_C
ctypedef cnp.npy_float32 G_H_DTYPE_C
ctypedef cnp.npy_uint32 BITSET_INNER_DTYPE_C
ctypedef float64_t X_DTYPE_C
ctypedef uint8_t X_BINNED_DTYPE_C
ctypedef float64_t Y_DTYPE_C
ctypedef float32_t G_H_DTYPE_C
ctypedef uint32_t BITSET_INNER_DTYPE_C
ctypedef BITSET_INNER_DTYPE_C[8] BITSET_DTYPE_C


cdef packed struct hist_struct:
# Same as histogram dtype but we need a struct to declare views. It needs
# to be packed since by default numpy dtypes aren't aligned
Expand Down Expand Up @@ -38,6 +36,7 @@ cdef packed struct node_struct:
# Only used if is_categorical is True
unsigned int bitset_idx


cpdef enum MonotonicConstraint:
NO_CST = 0
POS = 1
Expand Down
2 changes: 1 addition & 1 deletion sklearn/ensemble/_hist_gradient_boosting/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ foreach ext_name, ext_dict : hist_gradient_boosting_extension_metadata
py.extension_module(
ext_name,
ext_dict.get('sources'),
dependencies: [np_dep, openmp_dep],
dependencies: [openmp_dep],
cython_args: cython_args,
subdir: 'sklearn/ensemble/_hist_gradient_boosting',
install: true
Expand Down
6 changes: 2 additions & 4 deletions sklearn/ensemble/_hist_gradient_boosting/splitting.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

cimport cython
from cython.parallel import prange
cimport numpy as cnp
import numpy as np
from libc.math cimport INFINITY, ceil
from libc.stdlib cimport malloc, free, qsort
from libc.string cimport memcpy

from ...utils._typedefs cimport uint8_t
from .common cimport X_BINNED_DTYPE_C
from .common cimport Y_DTYPE_C
from .common cimport hist_struct
Expand All @@ -25,8 +25,6 @@ from ._bitset cimport init_bitset
from ._bitset cimport set_bitset
from ._bitset cimport in_bitset

cnp.import_array()


cdef struct split_info_struct:
# Same as the SplitInfo class, but we need a C struct to use it in the
Expand Down Expand Up @@ -490,7 +488,7 @@ cdef class Splitter:
int n_threads = self.n_threads
bint has_interaction_cst = False
Y_DTYPE_C feature_fraction_per_split = self.feature_fraction_per_split
cnp.npy_bool [:] subsample_mask
uint8_t [:] subsample_mask # same as npy_bool
int n_subsampled_features

has_interaction_cst = allowed_features is not None
Expand Down
3 changes: 0 additions & 3 deletions sklearn/linear_model/_cd_fast.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# License: BSD 3 clause

from libc.math cimport fabs
cimport numpy as cnp
import numpy as np

from cython cimport floating
Expand All @@ -22,8 +21,6 @@ from ..utils._typedefs cimport uint32_t
from ..utils._random cimport our_rand_r


cnp.import_array()

# The following two functions are shamelessly copied from the tree code.

cdef enum:
Expand Down
Loading