8000 Use except * to propagate error in all cdef functions · scikit-learn/scikit-learn@086506c · GitHub
[go: up one dir, main page]

Skip to content

Commit 086506c

Browse files
committed
Use except * to propagate error in all cdef functions
1 parent b355ec0 commit 086506c

File tree

8 files changed

+164
-161
lines changed

8 files changed

+164
-161
lines changed

sklearn/tree/_criterion.pxd

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ cdef class Criterion:
5555
# Methods
5656
cdef void init(self, DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
5757
double weighted_n_samples, SIZE_t* samples, SIZE_t start,
58-
SIZE_t end) nogil
59-
cdef void reset(self) nogil
60-
cdef void reverse_reset(self) nogil
61-
cdef void update(self, SIZE_t new_pos) nogil
62-
cdef double node_impurity(self) nogil
58+
SIZE_t end) nogil except *
59+
cdef void reset(self) nogil except *
60+
cdef void reverse_reset(self) nogil except *
61+
cdef void update(self, SIZE_t new_pos) nogil except *
62+
cdef double node_impurity(self) nogil except *
6363
cdef void children_impurity(self, double* impurity_left,
64-
double* impurity_right) nogil
65-
cdef void node_value(self, double* dest) nogil
66-
cdef double impurity_improvement(self, double impurity) nogil
67-
cdef double proxy_impurity_improvement(self) nogil
64+
double* impurity_right) nogil except *
65+
cdef void node_value(self, double* dest) nogil except *
66+
cdef double impurity_improvement(self, double impurity) nogil except *
67+
cdef double proxy_impurity_improvement(self) nogil except *

sklearn/tree/_criterion.pyx

Lines changed: 39 additions & 39 deletions
80
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ cdef class Criterion:
5353

5454
cdef void init(self, DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
5555
double weighted_n_samples, SIZE_t* samples, SIZE_t start,
56-
SIZE_t end) nogil:
56+
SIZE_t end) nogil except *:
5757
"""Placeholder for a method which will initialize the criterion.
5858
5959
Parameters
@@ -79,22 +79,22 @@ cdef class Criterion:
7979

80
pass
8181

82-
cdef void reset(self) nogil:
82+
cdef void reset(self) nogil except *:
8383
"""Reset the criterion at pos=start.
8484
8585
This method must be implemented by the subclass.
8686
"""
8787

8888
pass
8989

90-
cdef void reverse_reset(self) nogil:
90+
cdef void reverse_reset(self) nogil except *:
9191
"""Reset the criterion at pos=end.
9292
9393
This method must be implemented by the subclass.
9494
"""
9595
pass
9696

97-
cdef void update(self, SIZE_t new_pos) nogil:
97+
cdef void update(self, SIZE_t new_pos) nogil except *:
9898
"""Updated statistics by moving samples[pos:new_pos] to the left child.
9999
100100
This updates the collected statistics by moving samples[pos:new_pos]
@@ -109,7 +109,7 @@ cdef class Criterion:
109109

110110
pass
111111

112-
cdef double node_impurity(self) nogil:
112+
cdef double node_impurity(self) nogil except *:
113113
"""Placeholder for calculating the impurity of the node.
114114
115115
Placeholder for a method which will evaluate the impurity of
@@ -120,7 +120,7 @@ cdef class Criterion:
120120
pass
121121

122122
cdef void children_impurity(self, double* impurity_left,
123-
double* impurity_right) nogil:
123+
double* impurity_right) nogil except *:
124124
"""Placeholder for calculating the impurity of children.
125125
126126
Placeholder for a method which evaluates the impurity in
@@ -139,7 +139,7 @@ cdef class Criterion:
139139

140140
pass
141141

142-
cdef void node_value(self, double* dest) nogil:
142+
cdef void node_value(self, double* dest) nogil except *:
143143
"""Placeholder for storing the node value.
144144
145145
Placeholder for a method which will compute the node value
@@ -153,7 +153,7 @@ cdef class Criterion:
153153

154154
pass
155155

156-
cdef double proxy_impurity_improvement(self) nogil:
156+
cdef double proxy_impurity_improvement(self) nogil except *:
157157
"""Compute a proxy of the impurity reduction
158158
159159
This method is used to speed up the search for the best split.
@@ -171,7 +171,7 @@ cdef class Criterion:
171171
return (- self.weighted_n_right * impurity_right
172172
- self.weighted_n_left * impurity_left)
173173

174-
cdef double impurity_improvement(self, double impurity) nogil:
174+
cdef double impurity_improvement(self, double impurity) nogil except *:
175175
"""Compute the improvement in impurity
176176
177177
This method computes the improvement in impurity when a split occurs.
@@ -283,7 +283,7 @@ cdef class ClassificationCriterion(Criterion):
283283

284284
cdef void init(self, DOUBLE_t* y, SIZE_t y_stride,
285285
DOUBLE_t* sample_weight, double weighted_n_samples,
286-
SIZE_t* samples, SIZE_t start, SIZE_t end) nogil:
286+
SIZE_t* samples, SIZE_t start, SIZE_t end) nogil except *:
287287
"""Initialize the criterion at node samples[start:end] and
288288
children samples[start:start] and samples[start:end].
289289
@@ -348,7 +348,7 @@ cdef class ClassificationCriterion(Criterion):
348348
# Reset to pos=start
349349
self.reset()
350350

351-
cdef void reset(self) nogil:
351+
cdef void reset(self) nogil except *:
352352
"""Reset the criterion at pos=start."""
353353

354354
self.pos = self.start
@@ -371,7 +371,7 @@ cdef class ClassificationCriterion(Criterion):
371371
sum_left += self.sum_stride
372372
sum_right += self.sum_stride
373373

374-
cdef void reverse_reset(self) nogil:
374+
cdef void reverse_reset(self) nogil except *:
375375
"""Reset the criterion at pos=end."""
376376
self.pos = self.end
377377

@@ -393,7 +393,7 @@ cdef class ClassificationCriterion(Criterion):
393393
sum_left += self.sum_stride
394394
sum_right += self.sum_stride
395395

396-
cdef void update(self, SIZE_t new_pos) nogil:
396+
cdef void update(self, SIZE_t new_pos) nogil except *:
397397
"""Updated statistics by moving samples[pos:new_pos] to the left child.
398398
399399
Parameters
@@ -471,14 +471,14 @@ cdef class ClassificationCriterion(Criterion):
471471

472472
self.pos = new_pos
473473

474-
cdef double node_impurity(self) nogil:
474+
cdef double node_impurity(self) nogil except *:
475475
pass
476476

477477
cdef void children_impurity(self, double* impurity_left,
478-
double* impurity_right) nogil:
478+
double* impurity_right) nogil except *:
479479
pass
480480

481-
cdef void node_value(self, double* dest) nogil:
481+
cdef void node_value(self, double* dest) nogil except *:
482482
"""Compute the node value of samples[start:end] and save it into dest.
483483
484484
Parameters
@@ -513,7 +513,7 @@ cdef class Entropy(ClassificationCriterion):
513513
cross-entropy = -\sum_{k=0}^{K-1} count_k log(count_k)
514514
"""
515515

516-
cdef double node_impurity(self) nogil:
516+
cdef double node_impurity(self) nogil except *:
517517
"""Evaluate the impurity of the current node, i.e. the impurity of
518518
samples[start:end], using the cross-entropy criterion."""
519519

@@ -536,7 +536,7 @@ cdef class Entropy(ClassificationCriterion):
536536
return entropy / self.n_outputs
537537

538538
cdef void children_impurity(self, double* impurity_left,
539-
double* impurity_right) nogil:
539+
double* impurity_right) nogil except *:
540540
"""Evaluate the impurity in children nodes
541541
542542
i.e. the impurity of the left child (samples[start:pos]) and the
@@ -595,7 +595,7 @@ cdef class Gini(ClassificationCriterion):
595595
= 1 - \sum_{k=0}^{K-1} count_k ** 2
596596
"""
597597

598-
cdef double node_impurity(self) nogil:
598+
cdef double node_impurity(self) nogil except *:
599599
"""Evaluate the impurity of the current node, i.e. the impurity of
600600
samples[start:end] using the Gini criterion."""
601601

@@ -623,7 +623,7 @@ cdef class Gini(ClassificationCriterion):
623623
return gini / self.n_outputs
624624

625625
cdef void children_impurity(self, double* impurity_left,
626-
double* impurity_right) nogil:
626+
double* impurity_right) nogil except *:
627627
"""Evaluate the impurity in children nodes
628628
629629
i.e. the impurity of the left child (samples[start:pos]) and the
@@ -738,7 +738,7 @@ cdef class RegressionCriterion(Criterion):
738738

739739
cdef void init(self, DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
740740
double weighted_n_samples, SIZE_t* samples, SIZE_t start,
741-
SIZE_t end) nogil:
741+
SIZE_t end) nogil except *:
742742
"""Initialize the criterion at node samples[start:end] and
743743
children samples[start:start] and samples[start:end]."""
744744
# Initialize fields
@@ -779,7 +779,7 @@ cdef class RegressionCriterion(Criterion):
779779
# Reset to pos=start
780780
self.reset()
781781

782-
cdef void reset(self) nogil:
782+
cdef void reset(self) nogil except *:
783783
"""Reset the criterion at pos=start."""
784784
cdef SIZE_t n_bytes = self.n_outputs * sizeof(double)
785785
memset(self.sum_left, 0, n_bytes)
@@ -789,7 +789,7 @@ cdef class RegressionCriterion(Criterion):
789789
self.weighted_n_right = self.weighted_n_node_samples
790790
self.pos = self.start
791791

792-
cdef void reverse_reset(self) nogil:
792+
cdef void reverse_reset(self) nogil except *:
793793
"""Reset the criterion at pos=end."""
794794
cdef SIZE_t n_bytes = self.n_outputs * sizeof(double)
795795
memset(self.sum_right, 0, n_bytes)
@@ -799,7 +799,7 @@ cdef class RegressionCriterion(Criterion):
799799
self.weighted_n_left = self.weighted_n_node_samples
800800
self.pos = self.end
801801

802-
cdef void update(self, SIZE_t new_pos) nogil:
802+
cdef void update(self, SIZE_t new_pos) nogil except *:
803803
"""Updated statistics by moving samples[pos:new_pos] to the left."""
804804

805805
cdef double* sum_left = self.sum_left
@@ -860,14 +860,14 @@ cdef class RegressionCriterion(Criterion):
860860

861861
self.pos = new_pos
862862

863-
cdef double node_impurity(self) nogil:
863+
cdef double node_impurity(self) nogil except *:
864864
pass
865865

866866
cdef void children_impurity(self, double* impurity_left,
867-
double* impurity_right) nogil:
867+
double* impurity_right) nogil except *:
868868
pass
869869

870-
cdef void node_value(self, double* dest) nogil:
870+
cdef void node_value(self, double* dest) nogil except *:
871871
"""Compute the node value of samples[start:end] into dest."""
872872

873873
cdef SIZE_t k
@@ -882,7 +882,7 @@ cdef class MSE(RegressionCriterion):
882882
MSE = var_left + var_right
883883
"""
884884

885-
cdef double node_impurity(self) nogil:
885+
cdef double node_impurity(self) nogil except *:
886886
"""Evaluate the impurity of the current node, i.e. the impurity of
887887
samples[start:end]."""
888888

@@ -896,7 +896,7 @@ cdef class MSE(RegressionCriterion):
896896

897897
return impurity / self.n_outputs
898898

899-
cdef double proxy_impurity_improvement(self) nogil:
899+
cdef double proxy_impurity_improvement(self) nogil except *:
900900
"""Compute a proxy of the impurity reduction
901901
902902
This method is used to speed up the search for the best split.
@@ -923,7 +923,7 @@ cdef class MSE(RegressionCriterion):
923923
proxy_impurity_right / self.weighted_n_right)
924924

925925
cdef void children_impurity(self, double* impurity_left,
926-
double* impurity_right) nogil:
926+
double* impurity_right) nogil except *:
927927
"""Evaluate the impurity in children nodes, i.e. the impurity of the
928928
left child (samples[start:pos]) and the impurity the right child
929929
(samples[pos:end])."""
@@ -1030,7 +1030,7 @@ cdef class MAE(RegressionCriterion):
10301030

10311031
cdef void init(self, DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
10321032
double weighted_n_samples, SIZE_t* samples, SIZE_t start,
1033-
SIZE_t end) nogil:
1033+
SIZE_t end) nogil except *:
10341034
"""Initialize the criterion at node samples[start:end] and
10351035
children samples[start:start] and samples[start:end]."""
10361036

@@ -1080,7 +1080,7 @@ cdef class MAE(RegressionCriterion):
10801080
# Reset to pos=start
10811081
self.reset()
10821082

1083-
cdef void reset(self) nogil:
1083+
cdef void reset(self) nogil except *:
10841084
"""Reset the criterion at pos=start."""
10851085

10861086
cdef SIZE_t i, k
@@ -1106,7 +1106,7 @@ cdef class MAE(RegressionCriterion):
11061106
(<WeightedMedianCalculator> right_child[k]).push(value,
11071107
weight)
11081108

1109-
cdef void reverse_reset(self) nogil:
1109+
cdef void reverse_reset(self) nogil except *:
11101110
"""Reset the criterion at pos=end."""
11111111

11121112
self.weighted_n_right = 0.0
@@ -1129,7 +1129,7 @@ cdef class MAE(RegressionCriterion):
11291129
(<WeightedMedianCalculator> left_child[k]).push(value,
11301130
weight)
11311131

1132-
cdef void update(self, SIZE_t new_pos) nogil:
1132+
cdef void update(self, SIZE_t new_pos) nogil except *:
11331133
"""Updated statistics by moving samples[pos:new_pos] to the left."""
11341134

11351135
cdef DOUBLE_t* sample_weight = self.sample_weight
@@ -1186,14 +1186,14 @@ cdef class MAE(RegressionCriterion):
11861186
self.weighted_n_left)
11871187
self.pos = new_pos
11881188

1189-
cdef void node_value(self, double* dest) nogil:
1189+
cdef void node_value(self, double* dest) nogil except *:
11901190
"""Computes the node value of samples[start:end] into dest."""
11911191

11921192
cdef SIZE_t k
11931193
for k in range(self.n_outputs):
11941194
dest[k] = <double> self.node_medians[k]
11951195

1196-
cdef double node_impurity(self) nogil:
1196+
cdef double node_impurity(self) nogil except *:
11971197
"""Evaluate the impurity of the current node, i.e. the impurity of
11981198
samples[start:end]"""
11991199

@@ -1216,7 +1216,7 @@ cdef class MAE(RegressionCriterion):
12161216
return impurity / (self.weighted_n_node_samples * self.n_outputs)
12171217

12181218
cdef void children_impurity(self, double* impurity_left,
1219-
double* impurity_right) nogil:
1219+
double* impurity_right) nogil except *:
12201220
"""Evaluate the impurity in children nodes, i.e. the impurity of the
12211221
left child (samples[start:pos]) and the impurity the right child
12221222
(samples[pos:end]).
@@ -1273,7 +1273,7 @@ cdef class FriedmanMSE(MSE):
12731273
improvement = n_left * n_right * diff^2 / (n_left + n_right)
12741274
"""
12751275

1276-
cdef double proxy_impurity_improvement(self) nogil:
1276+
cdef double proxy_impurity_improvement(self) nogil except *:
12771277
"""Compute a proxy of the impurity reduction
12781278
12791279
This method is used to speed up the search for the best split.
@@ -1303,7 +1303,7 @@ cdef class FriedmanMSE(MSE):
13031303

13041304
return diff * diff / (self.weighted_n_left * self.weighted_n_right)
13051305

1306-
cdef double impurity_improvement(self, double impurity) nogil:
1306+
cdef double impurity_improvement(self, double impurity) nogil except *:
13071307
cdef double* sum_left = self.sum_left
13081308
cdef double* sum_right = self.sum_right
13091309

sklearn/tree/_splitter.pxd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ cdef class Splitter:
8686
np.ndarray X_idx_sorted=*) except *
8787

8888
cdef void node_reset(self, SIZE_t start, SIZE_t end,
89-
double* weighted_n_node_samples) nogil
89+
double* weighted_n_node_samples) nogil except *
9090

9191
cdef void node_split(self,
9292
double impurity, # Impurity of the node
9393
SplitRecord* split,
94-
SIZE_t* n_constant_features) nogil
94+
SIZE_t* n_constant_features) nogil except *
9595

96-
cdef void node_value(self, double* dest) nogil
96+
cdef void node_value(self, double* dest) nogil except *
9797

98-
cdef double node_impurity(self) nogil
98+
cdef double node_impurity(self) nogil except *

0 commit comments

Comments
 (0)
0