Skip to content

MAINT Use cython language_level=3 directive #12873

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
Feb 28, 2019
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
4 changes: 3 additions & 1 deletion sklearn/__check_build/_check_build.pyx
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# cython: language_level=3

def check_build():
return
return
8 changes: 2 additions & 6 deletions sklearn/_isotonic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
# Uses the pool adjacent violators algorithm (PAVA), with the
# enhancement of searching for the longest decreasing subsequence to
# pool at each step.
#
# cython: language_level=3, boundscheck=False, wraparound=False, cdivision=True

import numpy as np
cimport numpy as np
cimport cython
from cython cimport floating


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def _inplace_contiguous_isotonic_regression(floating[::1] y, floating[::1] w):
cdef:
Py_ssize_t n = y.shape[0], i, k
Expand Down Expand Up @@ -64,9 +63,6 @@ def _inplace_contiguous_isotonic_regression(floating[::1] y, floating[::1] w):
i = k


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def _make_unique(np.ndarray[dtype=floating] X,
np.ndarray[dtype=floating] y,
np.ndarray[dtype=floating] sample_weights):
Expand Down
4 changes: 2 additions & 2 deletions sklearn/cluster/_dbscan_inner.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Fast inner loop for DBSCAN.
# Author: Lars Buitinck
# License: 3-clause BSD
#
# cython: language_level=3, boundscheck=False, wraparound=False

cimport cython
from libcpp.vector cimport vector
Expand All @@ -14,8 +16,6 @@ cdef inline void push(vector[np.npy_intp] &stack, np.npy_intp i) except +:
stack.push_back(i)


@cython.boundscheck(False)
@cython.wraparound(False)
def dbscan_inner(np.ndarray[np.uint8_t, ndim=1, mode='c'] is_core,
np.ndarray[object, ndim=1] neighborhoods,
np.ndarray[np.npy_intp, ndim=1, mode='c'] labels):
Expand Down
2 changes: 2 additions & 0 deletions sklearn/cluster/_hierarchical.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Author: Gael Varoquaux <gael.varoquaux@normalesup.org>
#
# cython: language_level=3

import numpy as np
cimport numpy as np
Expand Down
17 changes: 2 additions & 15 deletions sklearn/cluster/_k_means.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Lars Buitinck
#
# License: BSD 3 clause
#
# cython: language_level=3, boundscheck=False, wraparound=False, cdivision=True

from libc.math cimport sqrt
import numpy as np
Expand All @@ -25,9 +27,6 @@ ctypedef np.int32_t INT
np.import_array()


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef DOUBLE _assign_labels_array(np.ndarray[floating, ndim=2] X,
np.ndarray[floating, ndim=1] sample_weight,
np.ndarray[floating, ndim=1] x_squared_norms,
Expand Down Expand Up @@ -94,9 +93,6 @@ cpdef DOUBLE _assign_labels_array(np.ndarray[floating, ndim=2] X,
return inertia


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cpdef DOUBLE _assign_labels_csr(X, np.ndarray[floating, ndim=1] sample_weight,
np.ndarray[DOUBLE, ndim=1] x_squared_norms,
np.ndarray[floating, ndim=2] centers,
Expand Down Expand Up @@ -158,9 +154,6 @@ cpdef DOUBLE _assign_labels_csr(X, np.ndarray[floating, ndim=1] sample_weight,
return inertia


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def _mini_batch_update_csr(X, np.ndarray[floating, ndim=1] sample_weight,
np.ndarray[DOUBLE, ndim=1] x_squared_norms,
np.ndarray[floating, ndim=2] centers,
Expand Down Expand Up @@ -262,9 +255,6 @@ def _mini_batch_update_csr(X, np.ndarray[floating, ndim=1] sample_weight,
return squared_diff


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def _centers_dense(np.ndarray[floating, ndim=2] X,
np.ndarray[floating, ndim=1] sample_weight,
np.ndarray[INT, ndim=1] labels, int n_clusters,
Expand Down Expand Up @@ -332,9 +322,6 @@ def _centers_dense(np.ndarray[floating, ndim=2] X,
return centers


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def _centers_sparse(X, np.ndarray[floating, ndim=1] sample_weight,
np.ndarray[INT, ndim=1] labels, n_clusters,
np.ndarray[floating, ndim=1] distances):
Expand Down
2 changes: 1 addition & 1 deletion sklearn/cluster/_k_means_elkan.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# cython: cdivision=True
# cython: boundscheck=False
# cython: wraparound=False
# cython: profile=True
# cython: language_level=3
#
# Author: Andreas Mueller
#
Expand Down
4 changes: 2 additions & 2 deletions sklearn/datasets/_svmlight_format.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Lars Buitinck
# Olivier Grisel <olivier.grisel@ensta.org>
# License: BSD 3 clause
#
# cython: language_level=2, boundscheck=False, wraparound=False

import array
from cpython cimport array
Expand All @@ -21,8 +23,6 @@ cdef bytes COMMA = u','.encode('ascii')
cdef bytes COLON = u':'.encode('ascii')


@cython.boundscheck(False)
@cython.wraparound(False)
def _load_svmlight_file(f, dtype, bint multilabel, bint zero_based,
bint query_id, long long offset, long long length):
cdef array.array data, indices, indptr
Expand Down
9 changes: 3 additions & 6 deletions sklearn/decomposition/_online_lda.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#
# cython: language_level=3, boundscheck=False, wraparound=False

cimport cython
cimport numpy as np
import numpy as np
Expand All @@ -8,8 +11,6 @@ from libc.math cimport exp, fabs, log
from numpy.math cimport EULER


@cython.boundscheck(False)
@cython.wraparound(False)
def mean_change(np.ndarray[ndim=1, dtype=np.float64_t] arr_1,
np.ndarray[ndim=1, dtype=np.float64_t] arr_2):
"""Calculate the mean difference between two arrays.
Expand All @@ -29,8 +30,6 @@ def mean_change(np.ndarray[ndim=1, dtype=np.float64_t] arr_1,
return total / size


@cython.boundscheck(False)
@cython.wraparound(False)
def _dirichlet_expectation_1d(np.ndarray[ndim=1, dtype=np.float64_t] doc_topic,
double doc_topic_prior,
np.ndarray[ndim=1, dtype=np.float64_t] out):
Expand Down Expand Up @@ -59,8 +58,6 @@ def _dirichlet_expectation_1d(np.ndarray[ndim=1, dtype=np.float64_t] doc_topic,
out[i] = exp(psi(doc_topic[i]) - psi_total)


@cython.boundscheck(False)
@cython.wraparound(False)
def _dirichlet_expectation_2d(np.ndarray[ndim=2, dtype=np.float64_t] arr):
"""Dirichlet expectation for multiple samples:
E[log(theta)] for theta ~ Dir(arr).
Expand Down
1 change: 1 addition & 0 deletions sklearn/decomposition/cdnmf_fast.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# cython: cdivision=True
# cython: boundscheck=False
# cython: wraparound=False
# cython: language_level=3

# Author: Mathieu Blondel, Tom Dupre la Tour
# License: BSD 3 clause
Expand Down
1 change: 1 addition & 0 deletions sklearn/ensemble/_gradient_boosting.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# cython: cdivision=True
# cython: boundscheck=False
# cython: wraparound=False
# cython: language_level=3
#
# Author: Peter Prettenhofer
#
Expand Down
4 changes: 2 additions & 2 deletions sklearn/feature_extraction/_hashing.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Author: Lars Buitinck
# License: BSD 3 clause
#
# cython: language_level=3, boundscheck=False, cdivision=True

import sys
import array
Expand All @@ -15,8 +17,6 @@ from sklearn.utils.fixes import sp_version
np.import_array()


@cython.boundscheck(False)
@cython.cdivision(True)
def transform(raw_X, Py_ssize_t n_features, dtype, bint alternate_sign=1):
"""Guts of FeatureHasher.transform.

Expand Down
14 changes: 2 additions & 12 deletions sklearn/linear_model/cd_fast.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Manoj Kumar <manojkumarsivaraj334@gmail.com>
#
# License: BSD 3 clause
#
# cython: language_level=3, boundscheck=False, wraparound=False, cdivision=True

from libc.math cimport fabs
cimport numpy as np
Expand Down Expand Up @@ -100,9 +102,6 @@ cdef floating diff_abs_max(int n, floating* a, floating* b) nogil:
return m


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def enet_coordinate_descent(floating[::1] w,
floating alpha, floating beta,
floating[::1, :] X,
Expand Down Expand Up @@ -258,9 +257,6 @@ def enet_coordinate_descent(floating[::1] w,
return w, gap, tol, n_iter + 1


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def sparse_enet_coordinate_descent(floating [::1] w,
floating alpha, floating beta,
np.ndarray[floating, ndim=1, mode='c'] X_data,
Expand Down Expand Up @@ -475,9 +471,6 @@ def sparse_enet_coordinate_descent(floating [::1] w,
return w, gap, tol, n_iter + 1


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def enet_coordinate_descent_gram(floating[::1] w,
floating alpha, floating beta,
np.ndarray[floating, ndim=2, mode='c'] Q,
Expand Down Expand Up @@ -630,9 +623,6 @@ def enet_coordinate_descent_gram(floating[::1] w,
return np.asarray(w), gap, tol, n_iter + 1


@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def enet_coordinate_descent_multi_task(floating[::1, :] W, floating l1_reg,
floating l2_reg,
np.ndarray[floating, ndim=2, mode='fortran'] X,
Expand Down
17 changes: 12 additions & 5 deletions sklearn/linear_model/sag_fast.pyx.tp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,23 @@ def get_dispatch(dtypes):

}}


"""
SAG and SAGA implementation
WARNING: Do not edit .pyx file directly, it is generated from .pyx.tp
"""
#------------------------------------------------------------------------------

# cython: cdivision=True
# cython: boundscheck=False
# cython: wraparound=False
# cython: language_level=3
#
# Authors: Danny Sullivan <dbsullivan23@gmail.com>
# Tom Dupre la Tour <tom.dupre-la-tour@m4x.org>
# Arthur Mensch <arthur.mensch@m4x.org
#
# License: BSD 3 clause

"""
SAG and SAGA implementation
WARNING: Do not edit .pyx file directly, it is generated from .pyx.tp
"""

cimport numpy as np
import numpy as np
Expand Down
4 changes: 3 additions & 1 deletion sklearn/linear_model/sgd_fast.pxd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Helper to load LossFunction from sgd_fast.pyx to sag_fast.pyx"""
# License: BSD 3 clause
#
# cython: language_level=3
"""Helper to load LossFunction from sgd_fast.pyx to sag_fast.pyx"""

cdef class LossFunction:
cdef double loss(self, double p, double y) nogil
Expand Down
1 change: 1 addition & 0 deletions sklearn/linear_model/sgd_fast.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# cython: cdivision=True
# cython: boundscheck=False
# cython: wraparound=False
# cython: language_level=3
#
# Author: Peter Prettenhofer <peter.prettenhofer@gmail.com>
# Mathieu Blondel (partial_fit support)
Expand Down
2 changes: 2 additions & 0 deletions sklearn/manifold/_barnes_hut_tsne.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# cython: boundscheck=False
# cython: wraparound=False
# cython: cdivision=True
# cython: language_level=3
#
# Author: Christopher Moody <chrisemoody@gmail.com>
# Author: Nick Travers <nickt@squareup.com>
# Implementation by Chris Moody & Nick Travers
Expand Down
3 changes: 2 additions & 1 deletion sklearn/manifold/_utils.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# cython: language_level=3, boundscheck=False

from libc cimport math
cimport cython
import numpy as np
Expand All @@ -10,7 +12,6 @@ cdef extern from "numpy/npy_math.h":
cdef float EPSILON_DBL = 1e-8
cdef float PERPLEXITY_TOLERANCE = 1e-5

@cython.boundscheck(False)
cpdef np.ndarray[np.float32_t, ndim=2] _binary_search_perplexity(
np.ndarray[np.float32_t, ndim=2] affinities,
np.ndarray[np.int64_t, ndim=2] neighbors,
Expand Down
5 changes: 3 additions & 2 deletions sklearn/metrics/cluster/expected_mutual_info_fast.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# cython: cdivision=True
# cython: boundscheck=False
# cython: wraparound=False
# cython: language_level=3
#
# Authors: Robert Layton <robertlayton@gmail.com>
# Corey Lynch <coreylynch9@gmail.com>
# License: BSD 3 clause
Expand All @@ -15,8 +17,7 @@ from sklearn.utils.lgamma cimport lgamma
np.import_array()
ctypedef np.float64_t DOUBLE

@cython.boundscheck(False)
@cython.wraparound(False)

def expected_mutual_information(contingency, int n_samples):
"""Calculate the expected mutual information for two labelings."""
cdef int R, C
Expand Down
3 changes: 2 additions & 1 deletion sklearn/metrics/pairwise_fast.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#cython: boundscheck=False
#cython: cdivision=True
#cython: wraparound=False

# cython: language_level=3
#
# Author: Andreas Mueller <amueller@ais.uni-bonn.de>
# Lars Buitinck
#
Expand Down
1 change: 1 addition & 0 deletions sklearn/neighbors/ball_tree.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#cython: boundscheck=False
#cython: wraparound=False
#cython: cdivision=True
# cython: language_level=3

# Author: Jake Vanderplas <vanderplas@astro.washington.edu>
# License: BSD 3 clause
Expand Down
10 changes: 6 additions & 4 deletions sklearn/neighbors/binary_tree.pxi
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!python
# cython: language_level=3


# KD Tree and Ball Tree
# =====================
Expand Down Expand Up @@ -152,10 +154,10 @@ import numpy as np
import warnings
from ..utils import check_array

from typedefs cimport DTYPE_t, ITYPE_t, DITYPE_t
from typedefs import DTYPE, ITYPE
from .typedefs cimport DTYPE_t, ITYPE_t, DITYPE_t
from .typedefs import DTYPE, ITYPE

from dist_metrics cimport (DistanceMetric, euclidean_dist, euclidean_rdist,
from .dist_metrics cimport (DistanceMetric, euclidean_dist, euclidean_rdist,
euclidean_dist_to_rdist, euclidean_rdist_to_dist)

cdef extern from "numpy/arrayobject.h":
Expand Down Expand Up @@ -999,7 +1001,7 @@ def newObj(obj):

######################################################################
# define the reverse mapping of VALID_METRICS
from dist_metrics import get_valid_metric_ids
from .dist_metrics import get_valid_metric_ids
VALID_METRIC_IDS = get_valid_metric_ids(VALID_METRICS)


Expand Down
Loading