@@ -51,11 +51,14 @@ cdef class Criterion:
51
51
def __setstate__ (self , d ):
52
52
pass
53
53
54
- cdef void init(self , DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
55
- double weighted_n_samples, SIZE_t* samples, SIZE_t start,
56
- SIZE_t end) nogil:
54
+ cdef int init(self , DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
55
+ double weighted_n_samples, SIZE_t* samples, SIZE_t start,
56
+ SIZE_t end) nogil except - 1 :
57
57
""" Placeholder for a method which will initialize the criterion.
58
58
59
+ Returns -1 in case of failure to allocate memory (and raise MemoryError)
60
+ or 0 otherwise.
61
+
59
62
Parameters
60
63
----------
61
64
y : array-like, dtype=DOUBLE_t
@@ -79,22 +82,22 @@ cdef class Criterion:
79
82
80
83
pass
81
84
82
- cdef void reset(self ) nogil:
85
+ cdef int reset(self ) nogil except - 1 :
83
86
""" Reset the criterion at pos=start.
84
87
85
88
This method must be implemented by the subclass.
86
89
"""
87
90
88
91
pass
89
92
90
- cdef void reverse_reset(self ) nogil:
93
+ cdef int reverse_reset(self ) nogil except - 1 :
91
94
""" Reset the criterion at pos=end.
92
95
93
96
This method must be implemented by the subclass.
94
97
"""
95
98
pass
96
99
97
- cdef void update(self , SIZE_t new_pos) nogil:
100
+ cdef int update(self , SIZE_t new_pos) nogil except - 1 :
98
101
""" Updated statistics by moving samples[pos:new_pos] to the left child.
99
102
100
103
This updates the collected statistics by moving samples[pos:new_pos]
@@ -281,12 +284,15 @@ cdef class ClassificationCriterion(Criterion):
281
284
sizet_ptr_to_ndarray(self .n_classes, self .n_outputs)),
282
285
self .__getstate__())
283
286
284
- cdef void init(self , DOUBLE_t* y, SIZE_t y_stride,
285
- DOUBLE_t* sample_weight, double weighted_n_samples,
286
- SIZE_t* samples, SIZE_t start, SIZE_t end) nogil:
287
+ cdef int init(self , DOUBLE_t* y, SIZE_t y_stride,
288
+ DOUBLE_t* sample_weight, double weighted_n_samples,
289
+ SIZE_t* samples, SIZE_t start, SIZE_t end) nogil except - 1 :
287
290
""" Initialize the criterion at node samples[start:end] and
288
291
children samples[start:start] and samples[start:end].
289
292
293
+ Returns -1 in case of failure to allocate memory (and raise MemoryError)
294
+ or 0 otherwise.
295
+
290
296
Parameters
291
297
----------
292
298
y : array-like, dtype=DOUBLE_t
@@ -347,10 +353,14 @@ cdef class ClassificationCriterion(Criterion):
347
353
348
354
# Reset to pos=start
349
355
self .reset()
356
+ return 0
350
357
351
- cdef void reset(self ) nogil:
352
- """ Reset the criterion at pos=start. """
358
+ cdef int reset(self ) nogil except - 1 :
359
+ """ Reset the criterion at pos=start
353
360
361
+ Returns -1 in case of failure to allocate memory (and raise MemoryError)
362
+ or 0 otherwise.
363
+ """
354
364
self .pos = self .start
355
365
356
366
self .weighted_n_left = 0.0
@@ -370,9 +380,14 @@ cdef class ClassificationCriterion(Criterion):
370
380
sum_total += self .sum_stride
371
381
sum_left += self .sum_stride
372
382
sum_right += self .sum_stride
383
+ return 0
373
384
374
- cdef void reverse_reset(self ) nogil:
375
- """ Reset the criterion at pos=end."""
385
+ cdef int reverse_reset(self ) nogil except - 1 :
386
+ """ Reset the criterion at pos=end
387
+
388
+ Returns -1 in case of failure to allocate memory (and raise MemoryError)
389
+ or 0 otherwise.
390
+ """
376
391
self .pos = self .end
377
392
378
393
self .weighted_n_left = self .weighted_n_node_samples
@@ -392,10 +407,14 @@ cdef class ClassificationCriterion(Criterion):
392
407
sum_total += self .sum_stride
393
408
sum_left += self .sum_stride
394
409
sum_right += self .sum_stride
410
+ return 0
395
411
396
- cdef void update(self , SIZE_t new_pos) nogil:
412
+ cdef int update(self , SIZE_t new_pos) nogil except - 1 :
397
413
""" Updated statistics by moving samples[pos:new_pos] to the left child.
398
414
415
+ Returns -1 in case of failure to allocate memory (and raise MemoryError)
416
+ or 0 otherwise.
417
+
399
418
Parameters
400
419
----------
401
420
new_pos : SIZE_t
@@ -470,6 +489,7 @@ cdef class ClassificationCriterion(Criterion):
470
489
sum_total += self .sum_stride
471
490
472
491
self .pos = new_pos
492
+ return 0
473
493
474
494
cdef double node_impurity(self ) nogil:
475
495
pass
@@ -736,9 +756,9 @@ cdef class RegressionCriterion(Criterion):
736
756
def __reduce__ (self ):
737
757
return (type (self ), (self .n_outputs, self .n_samples), self .__getstate__())
738
758
739
- cdef void init(self , DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
740
- double weighted_n_samples, SIZE_t* samples, SIZE_t start,
741
- SIZE_t end) nogil:
759
+ cdef int init(self , DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
760
+ double weighted_n_samples, SIZE_t* samples, SIZE_t start,
761
+ SIZE_t end) nogil except - 1 :
742
762
""" Initialize the criterion at node samples[start:end] and
743
763
children samples[start:start] and samples[start:end]."""
744
764
# Initialize fields
@@ -778,8 +798,9 @@ cdef class RegressionCriterion(Criterion):
778
798
779
799
# Reset to pos=start
780
800
self .reset()
801
+ return 0
781
802
782
- cdef void reset(self ) nogil:
803
+ cdef int reset(self ) nogil except - 1 :
783
804
""" Reset the criterion at pos=start."""
784
805
cdef SIZE_t n_bytes = self .n_outputs * sizeof(double )
785
806
memset(self .sum_left, 0 , n_bytes)
@@ -788,8 +809,9 @@ cdef class RegressionCriterion(Criterion):
788
809
self .weighted_n_left = 0.0
789
810
self .weighted_n_right = self .weighted_n_node_samples
790
811
self .pos = self .start
812
+ return 0
791
813
792
- cdef void reverse_reset(self ) nogil:
814
+ cdef int reverse_reset(self ) nogil except - 1 :
793
815
""" Reset the criterion at pos=end."""
794
816
cdef SIZE_t n_bytes = self .n_outputs * sizeof(double )
795
817
memset(self .sum_right, 0 , n_bytes)
@@ -798,8 +820,9 @@ cdef class RegressionCriterion(Criterion):
798
820
self .weighted_n_right = 0.0
799
821
self .weighted_n_left = self .weighted_n_node_samples
800
822
self .pos = self .end
823
+ return 0
801
824
802
- cdef void update(self , SIZE_t new_pos) nogil:
825
+ cdef int update(self , SIZE_t new_pos) nogil except - 1 :
803
826
""" Updated statistics by moving samples[pos:new_pos] to the left."""
804
827
805
828
cdef double * sum_left = self .sum_left
@@ -859,6 +882,7 @@ cdef class RegressionCriterion(Criterion):
859
882
sum_right[k] = sum_total[k] - sum_left[k]
860
883
861
884
self .pos = new_pos
885
+ return 0
862
886
863
887
cdef double node_impurity(self ) nogil:
864
888
pass
@@ -1018,19 +1042,16 @@ cdef class MAE(RegressionCriterion):
1018
1042
# Allocate memory for the accumulators
1019
1043
safe_realloc(& self .node_medians, n_outputs)
1020
1044
1021
- if (self .node_medians == NULL ):
1022
- raise MemoryError ()
1023
-
1024
1045
self .left_child = np.empty(n_outputs, dtype = ' object' )
1025
1046
self .right_child = np.empty(n_outputs, dtype = ' object' )
1026
1047
# initialize WeightedMedianCalculators
1027
1048
for k in range (n_outputs):
1028
1049
self .left_child[k] = WeightedMedianCalculator(n_samples)
1029
1050
self .right_child[k] = WeightedMedianCalculator(n_samples)
1030
1051
1031
- cdef void init(self , DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
1032
- double weighted_n_samples, SIZE_t* samples, SIZE_t start,
1033
- SIZE_t end) nogil:
1052
+ cdef int init(self , DOUBLE_t* y, SIZE_t y_stride, DOUBLE_t* sample_weight,
1053
+ double weighted_n_samples, SIZE_t* samples, SIZE_t start,
1054
+ SIZE_t end) nogil except - 1 :
1034
1055
""" Initialize the criterion at node samples[start:end] and
1035
1056
children samples[start:start] and samples[start:end]."""
1036
1057
@@ -1068,6 +1089,7 @@ cdef class MAE(RegressionCriterion):
1068
1089
for k in range (self .n_outputs):
1069
1090
y_ik = y[i * y_stride + k]
1070
1091
1092
+ # push method ends up calling safe_realloc, hence `except -1`
1071
1093
# push all values to the right side,
1072
1094
# since pos = start initially anyway
1073
1095
(< WeightedMedianCalculator> right_child[k]).push(y_ik, w)
@@ -1079,9 +1101,14 @@ cdef class MAE(RegressionCriterion):
1079
1101
1080
1102
# Reset to pos=start
1081
1103
self .reset()
1104
+ return 0
1082
1105
1083
- cdef void reset(self ) nogil:
1084
- """ Reset the criterion at pos=start."""
1106
+ cdef int reset(self ) nogil except - 1 :
1107
+ """ Reset the criterion at pos=start
1108
+
1109
+ Returns -1 in case of failure to allocate memory (and raise MemoryError)
1110
+ or 0 otherwise.
1111
+ """
1085
1112
1086
1113
cdef SIZE_t i, k
1087
1114
cdef DOUBLE_t value
@@ -1103,11 +1130,17 @@ cdef class MAE(RegressionCriterion):
1103
1130
# remove everything from left and put it into right
1104
1131
(< WeightedMedianCalculator> left_child[k]).pop(& value,
1105
1132
& weight)
1133
+ # push method ends up calling safe_realloc, hence `except -1`
1106
1134
(< WeightedMedianCalculator> right_child[k]).push(value,
1107
1135
weight)
1136
+ return 0
1108
1137
1109
- cdef void reverse_reset(self ) nogil:
1110
- """ Reset the criterion at pos=end."""
1138
+ cdef int reverse_reset(self ) nogil except - 1 :
1139
+ """ Reset the criterion at pos=end
1140
+
1141
+ Returns -1 in case of failure to allocate memory (and raise MemoryError)
1142
+ or 0 otherwise.
1143
+ """
1111
1144
1112
1145
self .weighted_n_right = 0.0
1113
1146
self .weighted_n_left = self .weighted_n_node_samples
@@ -1126,11 +1159,17 @@ cdef class MAE(RegressionCriterion):
1126
1159
# remove everything from right and put it into left
1127
1160
(< WeightedMedianCalculator> right_child[k]).pop(& value,
1128
1161
& weight)
1162
+ # push method ends up calling safe_realloc, hence `except -1`
1129
1163
(< WeightedMedianCalculator> left_child[k]).push(value,
1130
1164
weight)
1165
+ return 0
1131
1166
1132
- cdef void update(self , SIZE_t new_pos) nogil:
1133
- """ Updated statistics by moving samples[pos:new_pos] to the left."""
1167
+ cdef int update(self , SIZE_t new_pos) nogil except - 1 :
1168
+ """ Updated statistics by moving samples[pos:new_pos] to the left
1169
+
1170
+ Returns -1 in case of failure to allocate memory (and raise MemoryError)
1171
+ or 0 otherwise.
1172
+ """
1134
1173
1135
1174
cdef DOUBLE_t* sample_weight = self .sample_weight
1136
1175
cdef SIZE_t* samples = self .samples
@@ -1162,6 +1201,7 @@ cdef class MAE(RegressionCriterion):
1162
1201
y_ik = y[i * self .y_stride + k]
1163
1202
# remove y_ik and its weight w from right and add to left
1164
1203
(< WeightedMedianCalculator> right_child[k]).remove(y_ik, w)
1204
+ # push method ends up calling safe_realloc, hence except -1
1165
1205
(< WeightedMedianCalculator> left_child[k]).push(y_ik, w)
1166
1206
1167
1207
self .weighted_n_left += w
@@ -1185,6 +1225,7 @@ cdef class MAE(RegressionCriterion):
1185
1225
self .weighted_n_right = (self .weighted_n_node_samples -
1186
1226
self .weighted_n_left)
1187
1227
self .pos = new_pos
1228
+ return 0
1188
1229
1189
1230
cdef void node_value(self , double * dest) nogil:
1190
1231
""" Computes the node value of samples[start:end] into dest."""
0 commit comments