Skip to content

Commit

Permalink
MAINT Use C99 functions from Cython (#15098)
Browse files Browse the repository at this point in the history
  • Loading branch information
rth authored and thomasjpfan committed Sep 29, 2019
1 parent 7a48b67 commit af8a6e5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
5 changes: 1 addition & 4 deletions sklearn/cluster/_hierarchical.pyx
Expand Up @@ -18,17 +18,14 @@ from ..utils.fast_dict cimport IntFloatDict
# C++
from cython.operator cimport dereference as deref, preincrement as inc
from libcpp.map cimport map as cpp_map
from libc.math cimport fmax

DTYPE = np.float64
ctypedef np.float64_t DTYPE_t

ITYPE = np.intp
ctypedef np.intp_t ITYPE_t

# Reimplementation for MSVC support
cdef inline double fmax(double a, double b):
return max(a, b)

###############################################################################
# Utilities for computing the ward momentum

Expand Down
7 changes: 1 addition & 6 deletions sklearn/neighbors/binary_tree.pxi
Expand Up @@ -145,6 +145,7 @@
cimport cython
cimport numpy as np
from libc.math cimport fabs, sqrt, exp, cos, pow, log, lgamma
from libc.math cimport fmin, fmax
from libc.stdlib cimport calloc, malloc, free
from libc.string cimport memcpy

Expand Down Expand Up @@ -2610,12 +2611,6 @@ def nodeheap_sort(DTYPE_t[::1] vals):

return np.asarray(vals_sorted), np.asarray(indices)

# Reimplementation for MSVC support
cdef inline double fmin(double a, double b):
return min(a, b)

cdef inline double fmax(double a, double b) nogil:
return max(a, b)

cdef inline DTYPE_t _total_node_weight(NodeData_t* node_data,
DTYPE_t* sample_weight,
Expand Down
16 changes: 6 additions & 10 deletions sklearn/neighbors/quad_tree.pyx
Expand Up @@ -11,6 +11,7 @@ from cpython cimport Py_INCREF, PyObject, PyTypeObject
from libc.stdlib cimport malloc, free
from libc.string cimport memcpy
from libc.stdio cimport printf
from libc.stdint cimport SIZE_MAX

from ..tree._utils cimport safe_realloc, sizet_ptr_to_ndarray
from ..utils import check_array
Expand All @@ -29,11 +30,6 @@ cdef extern from "numpy/arrayobject.h":
void* data, int flags, object obj)


# XXX using (size_t)(-1) is ugly, but SIZE_MAX is not available in C89
# (i.e., older MSVC).
cdef SIZE_t DEFAULT = <SIZE_t>(-1)


# Repeat struct definition for numpy
CELL_DTYPE = np.dtype({
'names': ['parent', 'children', 'cell_id', 'point_index', 'is_leaf',
Expand Down Expand Up @@ -215,14 +211,14 @@ cdef class _QuadTree:
Cell* child
int i

# If the maximal capacity of the Tree have been reach, double the capacity
# If the maximal capacity of the Tree have been reached, double the capacity
# We need to save the current cell id and the current point to retrieve them
# in case the reallocation
if self.cell_count + 1 > self.capacity:
parent_id = cell.cell_id
for i in range(self.n_dimensions):
save_point[i] = point[i]
self._resize(DEFAULT)
self._resize(SIZE_MAX)
cell = &self.cells[parent_id]
point = save_point

Expand Down Expand Up @@ -305,7 +301,7 @@ cdef class _QuadTree:
cell.squared_max_width = 0
cell.cumulative_size = 0
for i in range(self.n_cells_per_cell):
cell.children[i] = DEFAULT
cell.children[i] = SIZE_MAX

cdef void _init_root(self, DTYPE_t[3] min_bounds, DTYPE_t[3] max_bounds
) nogil:
Expand Down Expand Up @@ -592,7 +588,7 @@ cdef class _QuadTree:
with gil:
raise MemoryError()

cdef int _resize_c(self, SIZE_t capacity=DEFAULT) nogil except -1:
cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) nogil except -1:
"""Guts of _resize
Returns -1 in case of failure to allocate memory (and raise MemoryError)
Expand All @@ -601,7 +597,7 @@ cdef class _QuadTree:
if capacity == self.capacity and self.cells != NULL:
return 0

if capacity == DEFAULT:
if capacity == SIZE_MAX:
if self.capacity == 0:
capacity = 9 # default initial value to min
else:
Expand Down
15 changes: 7 additions & 8 deletions sklearn/tree/_tree.pyx
Expand Up @@ -22,6 +22,7 @@ from libc.stdlib cimport free
from libc.math cimport fabs
from libc.string cimport memcpy
from libc.string cimport memset
from libc.stdint cimport SIZE_MAX

import numpy as np
cimport numpy as np
Expand Down Expand Up @@ -244,7 +245,7 @@ cdef class DepthFirstTreeBuilder(TreeBuilder):
split.threshold, impurity, n_node_samples,
weighted_n_node_samples)

if node_id == <SIZE_t>(-1):
if node_id == SIZE_MAX:
rc = -1
break

Expand Down Expand Up @@ -468,7 +469,7 @@ cdef class BestFirstTreeBuilder(TreeBuilder):
is_left, is_leaf,
split.feature, split.threshold, impurity, n_node_samples,
weighted_n_node_samples)
if node_id == <SIZE_t>(-1):
if node_id == SIZE_MAX:
return -1

# compute values also for split nodes (might become leafs later).
Expand Down Expand Up @@ -691,9 +692,7 @@ cdef class Tree:
with gil:
raise MemoryError()

# XXX using (size_t)(-1) is ugly, but SIZE_MAX is not available in C89
# (i.e., older MSVC).
cdef int _resize_c(self, SIZE_t capacity=<SIZE_t>(-1)) nogil except -1:
cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) nogil except -1:
"""Guts of _resize
Returns -1 in case of failure to allocate memory (and raise MemoryError)
Expand All @@ -702,7 +701,7 @@ cdef class Tree:
if capacity == self.capacity and self.nodes != NULL:
return 0

if capacity == <SIZE_t>(-1):
if capacity == SIZE_MAX:
if self.capacity == 0:
capacity = 3 # default initial value
else:
Expand Down Expand Up @@ -738,7 +737,7 @@ cdef class Tree:

if node_id >= self.capacity:
if self._resize_c() != 0:
return <SIZE_t>(-1)
return SIZE_MAX

cdef Node* node = &self.nodes[node_id]
node.impurity = impurity
Expand Down Expand Up @@ -1619,7 +1618,7 @@ cdef _build_pruned_tree(
node.impurity, node.n_node_samples,
node.weighted_n_node_samples)

if new_node_id == <SIZE_t>(-1):
if new_node_id == SIZE_MAX:
rc = -1
break

Expand Down

0 comments on commit af8a6e5

Please sign in to comment.