-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
MNT Centralize common cython compiler directives #21512
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,6 @@ ctypedef np.float64_t DOUBLE | |
ctypedef np.npy_intp INTP | ||
ctypedef np.int8_t INT8 | ||
|
||
# Numpy must be initialized. When using numpy from C or Cython you must | ||
# _always_ do that, or you will have segfaults | ||
|
||
jjerphan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
np.import_array() | ||
|
||
from ..metrics._dist_metrics cimport DistanceMetric | ||
|
@@ -32,9 +29,6 @@ from numpy.math cimport INFINITY | |
############################################################################### | ||
# Utilities for computing the ward momentum | ||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
@cython.cdivision(True) | ||
def compute_ward_dist(np.ndarray[DOUBLE, ndim=1, mode='c'] m_1, | ||
np.ndarray[DOUBLE, ndim=2, mode='c'] m_2, | ||
np.ndarray[INTP, ndim=1, mode='c'] coord_row, | ||
|
@@ -101,8 +95,6 @@ def _hc_get_descendent(INTP node, children, INTP n_leaves): | |
return descendent | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def hc_get_heads(np.ndarray[INTP, ndim=1] parents, copy=True): | ||
"""Returns the heads of the forest, as defined by parents. | ||
|
||
|
@@ -135,8 +127,6 @@ def hc_get_heads(np.ndarray[INTP, ndim=1] parents, copy=True): | |
return parents | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def _get_parents(nodes, heads, np.ndarray[INTP, ndim=1] parents, | ||
np.ndarray[INT8, ndim=1, mode='c'] not_visited): | ||
"""Returns the heads of the given nodes, as defined by parents. | ||
|
@@ -176,8 +166,6 @@ def _get_parents(nodes, heads, np.ndarray[INTP, ndim=1] parents, | |
# as keys and edge weights as values. | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def max_merge(IntFloatDict a, IntFloatDict b, | ||
np.ndarray[ITYPE_t, ndim=1] mask, | ||
ITYPE_t n_a, ITYPE_t n_b): | ||
|
@@ -231,8 +219,6 @@ def max_merge(IntFloatDict a, IntFloatDict b, | |
return out_obj | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def average_merge(IntFloatDict a, IntFloatDict b, | ||
np.ndarray[ITYPE_t, ndim=1] mask, | ||
ITYPE_t n_a, ITYPE_t n_b): | ||
|
@@ -302,7 +288,6 @@ cdef class WeightedEdge: | |
self.a = a | ||
self.b = b | ||
|
||
@cython.nonecheck(False) | ||
def __richcmp__(self, WeightedEdge other, int op): | ||
"""Cython-specific comparison method. | ||
|
||
|
@@ -348,8 +333,6 @@ cdef class UnionFind(object): | |
self.size = np.hstack((np.ones(N, dtype=ITYPE), | ||
np.zeros(N - 1, dtype=ITYPE))) | ||
|
||
@cython.boundscheck(False) | ||
@cython.nonecheck(False) | ||
cdef void union(self, ITYPE_t m, ITYPE_t n): | ||
self.parent[m] = self.next_label | ||
self.parent[n] = self.next_label | ||
|
@@ -358,8 +341,7 @@ cdef class UnionFind(object): | |
|
||
return | ||
|
||
@cython.boundscheck(False) | ||
@cython.nonecheck(False) | ||
@cython.wraparound(True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method does negative indexing |
||
cdef ITYPE_t fast_find(self, ITYPE_t n): | ||
cdef ITYPE_t p | ||
p = n | ||
|
@@ -371,8 +353,7 @@ cdef class UnionFind(object): | |
p, self.parent[p] = self.parent[p], n | ||
return n | ||
|
||
@cython.boundscheck(False) | ||
@cython.nonecheck(False) | ||
|
||
cpdef np.ndarray[DTYPE_t, ndim=2] _single_linkage_label( | ||
np.ndarray[DTYPE_t, ndim=2] L): | ||
""" | ||
|
@@ -423,6 +404,7 @@ cpdef np.ndarray[DTYPE_t, ndim=2] _single_linkage_label( | |
return result_arr | ||
|
||
|
||
@cython.wraparound(True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function does negative indexing |
||
def single_linkage_label(L): | ||
""" | ||
Convert an linkage array or MST to a tree by labelling clusters at merges. | ||
|
@@ -452,8 +434,6 @@ def single_linkage_label(L): | |
|
||
|
||
# Implements MST-LINKAGE-CORE from https://arxiv.org/abs/1109.2378 | ||
@cython.boundscheck(False) | ||
@cython.nonecheck(False) | ||
def mst_linkage_core( | ||
const DTYPE_t [:, ::1] raw_data, | ||
DistanceMetric dist_metric): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
# cython: language_level=3 | ||
|
||
|
||
from cython cimport floating | ||
cimport numpy as np | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# cython: language_level=3 | ||
import numpy as np | ||
cimport numpy as np | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
# Author: Lars Buitinck | ||
# License: BSD 3 clause | ||
# | ||
# cython: boundscheck=False, cdivision=True | ||
|
||
import sys | ||
import array | ||
|
@@ -92,7 +90,7 @@ def transform(raw_X, Py_ssize_t n_features, dtype, | |
indices_a = np.frombuffer(indices, dtype=np.int32) | ||
indptr_a = np.frombuffer(indptr, dtype=indices_np_dtype) | ||
|
||
if indptr[-1] > np.iinfo(np.int32).max: # = 2**31 - 1 | ||
if indptr[len(indptr) - 1] > np.iinfo(np.int32).max: # = 2**31 - 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function used to do negative indexing but since there's only one and it's for indexing the last element I find it better to replace by a usual indexing instead of adding wraparound for the whole function |
||
# both indices and indptr have the same dtype in CSR arrays | ||
indices_a = indices_a.astype(np.int64, copy=False) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# cython: boundscheck=False | ||
|
||
from libc cimport math | ||
cimport cython | ||
import numpy as np | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super cool thing :)