Skip to content

Commit 3ba3931

Browse files
committed
improve signatures
1 parent e38a2f4 commit 3ba3931

7 files changed

+31
-32
lines changed

_cmake/constants.cmake

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3") # -DNDEBUG
6262
#
6363
# C++ 14 or C++ 17 or...
6464
#
65+
# We need to use the same C++ version as scikit-learn to avoid crashes.
66+
set(CMAKE_CXX_SCIKITLEARN 11)
6567
if (PYTHON_MANYLINUX EQUAL "1")
6668
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6769
set(CMAKE_CXX_EXTENSIONS OFF)
68-
set(CMAKE_CXX_STANDARD 17)
70+
set(CMAKE_CXX_STANDARD ${CMAKE_CXX_SCIKITLEARN})
6971
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unknown-pragmas -Wextra")
7072
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-unknown-pragmas -Wextra")
7173
if(APPLE)
@@ -82,28 +84,32 @@ if (PYTHON_MANYLINUX EQUAL "1")
8284
else()
8385
if(MSVC)
8486
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
85-
set(CMAKE_CXX_STANDARD 17)
87+
set(CMAKE_CXX_STANDARD ${CMAKE_CXX_SCIKITLEARN})
8688
elseif(APPLE)
8789
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15")
8890
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
89-
set(CMAKE_CXX_STANDARD 17)
91+
set(CMAKE_CXX_STANDARD ${CMAKE_CXX_SCIKITLEARN})
9092
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unknown-pragmas -Wextra")
9193
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-unknown-pragmas -Wextra")
9294
else()
9395
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unknown-pragmas -Wextra")
9496
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-unknown-pragmas -Wextra")
9597
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "15")
9698
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++23")
97-
set(CMAKE_CXX_STANDARD 23)
99+
# set(CMAKE_CXX_STANDARD 23)
100+
set(CMAKE_CXX_STANDARD ${CMAKE_CXX_SCIKITLEARN})
98101
elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "11")
99102
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
100-
set(CMAKE_CXX_STANDARD 20)
103+
# set(CMAKE_CXX_STANDARD 20)
104+
set(CMAKE_CXX_STANDARD ${CMAKE_CXX_SCIKITLEARN})
101105
elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "9")
102106
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
103-
set(CMAKE_CXX_STANDARD 17)
107+
# set(CMAKE_CXX_STANDARD 17)
108+
set(CMAKE_CXX_STANDARD ${CMAKE_CXX_SCIKITLEARN})
104109
elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "6")
105110
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
106-
set(CMAKE_CXX_STANDARD 14)
111+
# set(CMAKE_CXX_STANDARD 14)
112+
set(CMAKE_CXX_STANDARD ${CMAKE_CXX_SCIKITLEARN})
107113
else()
108114
message(FATAL_ERROR "gcc>=6.0 is needed but "
109115
"${CMAKE_C_COMPILER_VERSION} was detected.")

_unittests/ut_mlmodel/test_piecewise_decision_tree_experiment.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,8 @@ def test_decision_tree_criterion(self):
175175
crit = SimpleRegressorCriterion(
176176
1 if len(y.shape) <= 1 else y.shape[1], X.shape[0]
177177
)
178-
print("F0")
179178
clr2 = DecisionTreeRegressor(criterion=crit, max_depth=1)
180-
print("F1")
181179
clr2.fit(X, y)
182-
print("F2")
183180
p2 = clr2.predict(X)
184181
self.assertEqual(p1, p2)
185182
self.assertEqual(clr1.tree_.node_count, clr2.tree_.node_count)

mlinsights/mlmodel/_piecewise_tree_regression_common.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ from sklearn.tree._criterion cimport SIZE_t, DOUBLE_t
1010
cdef class CommonRegressorCriterion(Criterion):
1111

1212
cdef void _update_weights(self, SIZE_t start, SIZE_t end,
13-
SIZE_t old_pos, SIZE_t new_pos) nogil
13+
SIZE_t old_pos, SIZE_t new_pos) noexcept nogil
1414

1515
cdef void _mean(self, SIZE_t start, SIZE_t end,
16-
DOUBLE_t *mean, DOUBLE_t *weight) nogil
16+
DOUBLE_t *mean, DOUBLE_t *weight) noexcept nogil
1717

1818
cdef double _mse(self, SIZE_t start, SIZE_t end,
1919
DOUBLE_t mean, DOUBLE_t weight) noexcept nogil
2020

2121
cdef void children_impurity_weights(self, double* impurity_left,
2222
double* impurity_right,
2323
double* weight_left,
24-
double* weight_right) nogil
24+
double* weight_right) noexcept nogil

mlinsights/mlmodel/_piecewise_tree_regression_common.pyx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from libc.stdio cimport printf
22
from libc.math cimport NAN
3-
4-
import numpy
5-
cimport numpy
6-
numpy.import_array()
3+
# import numpy
4+
cimport numpy as cnp
5+
cnp.import_array()
76

87
from sklearn.tree._criterion cimport Criterion
98
from sklearn.tree._criterion cimport SIZE_t, DOUBLE_t
@@ -44,7 +43,7 @@ cdef class CommonRegressorCriterion(Criterion):
4443
return inst
4544

4645
cdef void _update_weights(self, SIZE_t start, SIZE_t end,
47-
SIZE_t old_pos, SIZE_t new_pos) nogil:
46+
SIZE_t old_pos, SIZE_t new_pos) noexcept nogil:
4847
"""
4948
Updates members `weighted_n_right` and `weighted_n_left`
5049
when `pos` changes. This method should be overloaded.
@@ -81,7 +80,7 @@ cdef class CommonRegressorCriterion(Criterion):
8180
self.pos = new_pos
8281

8382
cdef void _mean(self, SIZE_t start, SIZE_t end, DOUBLE_t *mean,
84-
DOUBLE_t *weight) nogil:
83+
DOUBLE_t *weight) noexcept nogil:
8584
"""
8685
Computes the mean of *y* between *start* and *end*.
8786
"""
@@ -98,7 +97,7 @@ cdef class CommonRegressorCriterion(Criterion):
9897
cdef void children_impurity_weights(self, double* impurity_left,
9998
double* impurity_right,
10099
double* weight_left,
101-
double* weight_right) nogil:
100+
double* weight_right) noexcept nogil:
102101
"""
103102
Calculates the impurity of children,
104103
evaluates the impurity in

mlinsights/mlmodel/piecewise_tree_regression_criterion.pyx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
from libc.stdlib cimport calloc, free
12
cimport cython
23
# import numpy as np
34
cimport numpy as cnp
45

56
cnp.import_array()
67

7-
from libc.stdlib cimport calloc, free
8-
from libc.stdio cimport printf
9-
108
from sklearn.tree._criterion cimport SIZE_t, DOUBLE_t
9+
from sklearn.tree._criterion cimport Criterion
1110
from ._piecewise_tree_regression_common cimport CommonRegressorCriterion
1211

1312

@@ -116,9 +115,7 @@ cdef class SimpleRegressorCriterion(CommonRegressorCriterion):
116115
self.end = end
117116
self.weighted_n_samples = weighted_n_samples
118117
# Fatal Python error: __pyx_fatalerror: Acquisition count is 0
119-
printf("init_with_X:A\n")
120118
self.y = y
121-
printf("init_with_X:B\n")
122119

123120
self.sample_sum_wy = 0.
124121
self.sample_sum_w = 0.
@@ -143,7 +140,7 @@ cdef class SimpleRegressorCriterion(CommonRegressorCriterion):
143140
return self.reset()
144141

145142
cdef void _update_weights(self, SIZE_t start, SIZE_t end, SIZE_t old_pos,
146-
SIZE_t new_pos) nogil:
143+
SIZE_t new_pos) noexcept nogil:
147144
"""
148145
Updates members `weighted_n_right` and `weighted_n_left`
149146
when `pos` changes.
@@ -156,7 +153,7 @@ cdef class SimpleRegressorCriterion(CommonRegressorCriterion):
156153
self.weighted_n_right += self.sample_w[k]
157154

158155
cdef void _mean(self, SIZE_t start, SIZE_t end, DOUBLE_t *mean,
159-
DOUBLE_t *weight) nogil:
156+
DOUBLE_t *weight) noexcept nogil:
160157
"""
161158
Computes the mean of *y* between *start* and *end*.
162159
"""

mlinsights/mlmodel/piecewise_tree_regression_criterion_fast.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ cdef class SimpleRegressorCriterionFast(CommonRegressorCriterion):
141141
return 0
142142

143143
cdef void _mean(self, SIZE_t start, SIZE_t end, DOUBLE_t *mean,
144-
DOUBLE_t *weight) nogil:
144+
DOUBLE_t *weight) noexcept nogil:
145145
"""
146146
Computes the mean of *y* between *start* and *end*.
147147
"""
@@ -176,7 +176,7 @@ cdef class SimpleRegressorCriterionFast(CommonRegressorCriterion):
176176
return 0. if weight == 0. else squ / weight - mean ** 2
177177

178178
cdef void _update_weights(self, SIZE_t start, SIZE_t end,
179-
SIZE_t old_pos, SIZE_t new_pos) nogil:
179+
SIZE_t old_pos, SIZE_t new_pos) noexcept nogil:
180180
"""
181181
Updates members `weighted_n_right` and `weighted_n_left`
182182
when `pos` changes.

mlinsights/mlmodel/piecewise_tree_regression_criterion_linear.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ cdef class LinearRegressorCriterion(CommonRegressorCriterion):
241241
return 0
242242

243243
cdef void _mean(self, SIZE_t start, SIZE_t end, DOUBLE_t *mean,
244-
DOUBLE_t *weight) nogil:
244+
DOUBLE_t *weight) noexcept nogil:
245245
"""
246246
Computes mean between *start* and *end*.
247247
"""
@@ -257,7 +257,7 @@ cdef class LinearRegressorCriterion(CommonRegressorCriterion):
257257
weight[0] = w
258258
mean[0] = 0. if w == 0. else m / w
259259

260-
cdef void _reglin(self, SIZE_t start, SIZE_t end, int low_rank) nogil:
260+
cdef void _reglin(self, SIZE_t start, SIZE_t end, int low_rank) noexcept nogil:
261261
"""
262262
Solves the linear regression between *start* and *end*
263263
assuming corresponding points are approximated by a line.
@@ -330,7 +330,7 @@ cdef class LinearRegressorCriterion(CommonRegressorCriterion):
330330
squ += d * d * self.sample_w[k]
331331
return 0. if weight == 0. else squ / weight
332332

333-
cdef void _node_beta(self, double* dest) nogil:
333+
cdef void _node_beta(self, double* dest) noexcept nogil:
334334
"""
335335
Stores the results of the linear regression
336336
in an allocated numpy array.

0 commit comments

Comments
 (0)