Skip to content

Commit f1a3441

Browse files
committed
Fix method signatures for Cython 3
Signed-off-by: Keith Battocchi <kebatt@microsoft.com>
1 parent fb7f44b commit f1a3441

File tree

13 files changed

+152
-152
lines changed

13 files changed

+152
-152
lines changed

econml/grf/_criterion.pxd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
5050
cdef int node_reset_jacobian(self, DOUBLE_t* J, DOUBLE_t* invJ, double* weighted_n_node_samples,
5151
const DOUBLE_t[:, ::1] pointJ,
5252
DOUBLE_t* sample_weight,
53-
SIZE_t* samples, SIZE_t start, SIZE_t end) nogil except -1
53+
SIZE_t* samples, SIZE_t start, SIZE_t end) except -1 nogil
5454
cdef int node_reset_parameter(self, DOUBLE_t* parameter, DOUBLE_t* parameter_pre,
5555
DOUBLE_t* invJ,
5656
const DOUBLE_t[:, ::1] alpha,
5757
DOUBLE_t* sample_weight, double weighted_n_node_samples,
58-
SIZE_t* samples, SIZE_t start, SIZE_t end) nogil except -1
58+
SIZE_t* samples, SIZE_t start, SIZE_t end) except -1 nogil
5959
cdef int node_reset_rho(self, DOUBLE_t* rho, DOUBLE_t* moment, SIZE_t* node_index_mapping,
6060
DOUBLE_t* parameter, DOUBLE_t* invJ, double weighted_n_node_samples,
6161
const DOUBLE_t[:, ::1] pointJ, const DOUBLE_t[:, ::1] alpha,
6262
DOUBLE_t* sample_weight, SIZE_t* samples,
63-
SIZE_t start, SIZE_t end) nogil except -1
63+
SIZE_t start, SIZE_t end) except -1 nogil
6464
cdef int node_reset_sums(self, const DOUBLE_t[:, ::1] y, DOUBLE_t* rho,
6565
DOUBLE_t* J,
6666
DOUBLE_t* sample_weight, SIZE_t* samples,
6767
DOUBLE_t* sum_total, DOUBLE_t* var_total,
6868
DOUBLE_t* sq_sum_total, DOUBLE_t* y_sq_sum_total,
69-
SIZE_t start, SIZE_t end) nogil except -1
69+
SIZE_t start, SIZE_t end) except -1 nogil
7070

7171
cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
7272
cdef DOUBLE_t* J_left # The jacobian of the left child: J(Left) = E[J | X in Left-Child]
@@ -79,4 +79,4 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
7979
cdef DOUBLE_t* parameter_right
8080

8181
cdef double _get_min_eigv(self, DOUBLE_t* J_child, DOUBLE_t* var_child,
82-
double weighted_n_child) nogil except -1
82+
double weighted_n_child) except -1 nogil

econml/grf/_criterion.pyx

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
145145

146146
cdef int init(self, const DOUBLE_t[:, ::1] y, DOUBLE_t* sample_weight,
147147
double weighted_n_samples,
148-
SIZE_t* samples) nogil except -1:
148+
SIZE_t* samples) except -1 nogil:
149149
cdef SIZE_t n_features = self.n_features
150150
cdef SIZE_t n_outputs = self.n_outputs
151151
cdef SIZE_t n_y = self.n_y
@@ -163,7 +163,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
163163
cdef int node_reset_jacobian(self, DOUBLE_t* J, DOUBLE_t* invJ, double* weighted_n_node_samples,
164164
const DOUBLE_t[:, ::1] pointJ,
165165
DOUBLE_t* sample_weight,
166-
SIZE_t* samples, SIZE_t start, SIZE_t end) nogil except -1:
166+
SIZE_t* samples, SIZE_t start, SIZE_t end) except -1 nogil:
167167
""" Calculate the node un-normalized jacobian::
168168
169169
J(node) := E[J[i] | X[i] in Node] weight(node) = sum_{i in Node} w[i] J[i]
@@ -233,7 +233,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
233233
DOUBLE_t* invJ,
234234
const DOUBLE_t[:, ::1] alpha,
235235
DOUBLE_t* sample_weight, double weighted_n_node_samples,
236-
SIZE_t* samples, SIZE_t start, SIZE_t end) nogil except -1:
236+
SIZE_t* samples, SIZE_t start, SIZE_t end) except -1 nogil:
237237
""" Calculate the node parameter and the un-normalized pre-conditioned parameter
238238
239239
theta_pre(node) := E[A[i] | X[i] in Node] weight(node) = sum_{i in Node} w[i] A[i]
@@ -283,7 +283,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
283283
cdef int node_reset_rho(self, DOUBLE_t* rho, DOUBLE_t* moment, SIZE_t* node_index_mapping,
284284
DOUBLE_t* parameter, DOUBLE_t* invJ, double weighted_n_node_samples,
285285
const DOUBLE_t[:, ::1] pointJ, const DOUBLE_t[:, ::1] alpha, DOUBLE_t* sample_weight,
286-
SIZE_t* samples, SIZE_t start, SIZE_t end) nogil except -1:
286+
SIZE_t* samples, SIZE_t start, SIZE_t end) except -1 nogil:
287287
""" Calculate the node proxy labels and moment for each sample i in the node
288288
289289
moment[i] := J[i] * theta(Node) - A[i]
@@ -343,7 +343,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
343343
DOUBLE_t* sample_weight, SIZE_t* samples,
344344
DOUBLE_t* sum_total, DOUBLE_t* var_total, DOUBLE_t* sq_sum_total,
345345
DOUBLE_t* y_sq_sum_total,
346-
SIZE_t start, SIZE_t end) nogil except -1:
346+
SIZE_t start, SIZE_t end) except -1 nogil:
347347
""" Initialize several sums of quantities that will be useful to speed up the `update()` method:
348348
349349
for k in {1..n_outputs}: sum_total[k] := sum_{i in Node} w[i] rho[i, k]
@@ -411,7 +411,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
411411

412412
return 0
413413

414-
cdef int node_reset(self, SIZE_t start, SIZE_t end) nogil except -1:
414+
cdef int node_reset(self, SIZE_t start, SIZE_t end) except -1 nogil:
415415
"""Initialize the criterion at node samples[start:end] and
416416
children samples[start:start] and samples[start:end]."""
417417

@@ -447,7 +447,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
447447
self.reset()
448448
return 0
449449

450-
cdef int reset(self) nogil except -1:
450+
cdef int reset(self) except -1 nogil:
451451
"""Reset the criterion at pos=start."""
452452
cdef SIZE_t n_bytes = self.n_outputs * sizeof(double)
453453

@@ -462,7 +462,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
462462

463463
return 0
464464

465-
cdef int reverse_reset(self) nogil except -1:
465+
cdef int reverse_reset(self) except -1 nogil:
466466
"""Reset the criterion at pos=end."""
467467
cdef SIZE_t n_bytes = self.n_outputs * sizeof(double)
468468
memset(self.sum_right, 0, n_bytes)
@@ -476,7 +476,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
476476

477477
return 0
478478

479-
cdef int update(self, SIZE_t new_pos) nogil except -1:
479+
cdef int update(self, SIZE_t new_pos) except -1 nogil:
480480
"""Updated statistics by moving samples[pos:new_pos] to the left."""
481481

482482
cdef double* sum_left = self.sum_left
@@ -554,11 +554,11 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
554554

555555
return 0
556556

557-
cdef void node_value(self, double* dest) nogil:
557+
cdef void node_value(self, double* dest) noexcept nogil:
558558
"""Return the estimated node parameter of samples[start:end] into dest."""
559559
memcpy(dest, self.parameter, self.n_outputs * sizeof(double))
560560

561-
cdef void node_jacobian(self, double* dest) nogil:
561+
cdef void node_jacobian(self, double* dest) noexcept nogil:
562562
"""Return the node normalized Jacobian of samples[start:end] into dest in a C contiguous format."""
563563
cdef SIZE_t i, j
564564
# Jacobian is stored in f-contiguous format for fortran. We translate it to c-contiguous for
@@ -568,13 +568,13 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
568568
for j in range(n_outputs):
569569
dest[i * n_outputs + j] = self.J[i + j * n_outputs] / self.weighted_n_node_samples
570570

571-
cdef void node_precond(self, double* dest) nogil:
571+
cdef void node_precond(self, double* dest) noexcept nogil:
572572
"""Return the normalized node preconditioned value of samples[start:end] into dest."""
573573
cdef SIZE_t i
574574
for i in range(self.n_outputs):
575575
dest[i] = self.parameter_pre[i] / self.weighted_n_node_samples
576576

577-
cdef double node_impurity(self) nogil:
577+
cdef double node_impurity(self) noexcept nogil:
578578
"""Evaluate the impurity of the current node, i.e. the impurity of
579579
samples[start:end]. We use as node_impurity the proxy quantity:
580580
sum_{k=1}^{n_relevant_outputs} Var(rho[i, k] | i in Node) / n_relevant_outputs
@@ -591,7 +591,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
591591

592592
return impurity / self.n_relevant_outputs
593593

594-
cdef double proxy_impurity_improvement(self) nogil:
594+
cdef double proxy_impurity_improvement(self) noexcept nogil:
595595
"""Compute a proxy of the impurity reduction.
596596
This method is used to speed up the search for the best split. It is a proxy quantity such that the
597597
split that maximizes this value also maximizes the impurity improvement. It neglects all constant terms
@@ -620,7 +620,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
620620
proxy_impurity_right / self.weighted_n_right)
621621

622622
cdef void children_impurity(self, double* impurity_left,
623-
double* impurity_right) nogil:
623+
double* impurity_right) noexcept nogil:
624624
"""Evaluate the impurity in children nodes, i.e. the impurity of the
625625
left child (samples[start:pos]) and the impurity the right child
626626
(samples[pos:end]). Here we use the proxy child impurity:
@@ -673,7 +673,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
673673
impurity_left[0] /= self.n_relevant_outputs
674674
impurity_right[0] /= self.n_relevant_outputs
675675

676-
cdef double min_eig_left(self) nogil:
676+
cdef double min_eig_left(self) noexcept nogil:
677677
""" Calculate proxy for minimum eigenvalue of jacobian of left child. Here we simply
678678
use the minimum absolute value of the diagonals of the jacobian. This proxy needs to be
679679
super fast as this calculation happens for every candidate split. So we cannot afford
@@ -692,7 +692,7 @@ cdef class LinearMomentGRFCriterion(RegressionCriterion):
692692
# un-normalized jacobian of the left child. Thus we normalize by weight(left)
693693
return min / self.weighted_n_left
694694

695-
cdef double min_eig_right(self) nogil:
695+
cdef double min_eig_right(self) noexcept nogil:
696696
""" Calculate proxy for minimum eigenvalue of jacobian of right child
697697
(see min_eig_left for more details).
698698
"""
@@ -774,7 +774,7 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
774774
free(self.parameter_left)
775775
free(self.parameter_right)
776776

777-
cdef int reset(self) nogil except -1:
777+
cdef int reset(self) except -1 nogil:
778778
"""Reset the criterion at pos=start."""
779779
cdef SIZE_t n_bytes = self.n_outputs * self.n_outputs * sizeof(double)
780780
memset(self.J_left, 0, n_bytes)
@@ -785,7 +785,7 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
785785
memcpy(self.parameter_pre_right, self.parameter_pre, n_bytes)
786786
return LinearMomentGRFCriterion.reset(self)
787787

788-
cdef int reverse_reset(self) nogil except -1:
788+
cdef int reverse_reset(self) except -1 nogil:
789789
"""Reset the criterion at pos=end."""
790790
cdef SIZE_t n_bytes = self.n_outputs * self.n_outputs * sizeof(double)
791791
memset(self.J_right, 0, n_bytes)
@@ -796,7 +796,7 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
796796
memcpy(self.parameter_pre_left, self.parameter_pre, n_bytes)
797797
return LinearMomentGRFCriterion.reverse_reset(self)
798798

799-
cdef int update(self, SIZE_t new_pos) nogil except -1:
799+
cdef int update(self, SIZE_t new_pos) except -1 nogil:
800800
"""Updated statistics by moving samples[pos:new_pos] to the left.
801801
We need to re-implement this method, as now we need to keep track of the whole left
802802
and right jacobian matrices, not just the diagonals.
@@ -892,7 +892,7 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
892892

893893
return 0
894894

895-
cdef double proxy_node_impurity(self) nogil:
895+
cdef double proxy_node_impurity(self) noexcept nogil:
896896
""" For n_outputs > 2, we use parent class children impurity, which is slightly different than
897897
the MSE criterion. Thus for `min_impurity_decrease` purposes we also use the `node_impurity` of
898898
the parent class and not the MSE impurity.
@@ -902,7 +902,7 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
902902
else:
903903
return self.node_impurity()
904904

905-
cdef double node_impurity(self) nogil:
905+
cdef double node_impurity(self) noexcept nogil:
906906
"""Evaluate the impurity of the current node, i.e. the impurity of samples[start:end].
907907
Here we use the MSE criterion:
908908
@@ -930,7 +930,7 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
930930
impurity -= pk * self.parameter[m] * self.J[k + m * n_outputs] / self.weighted_n_node_samples
931931
return impurity
932932

933-
cdef double proxy_impurity_improvement(self) nogil:
933+
cdef double proxy_impurity_improvement(self) noexcept nogil:
934934
"""Compute a proxy of the impurity reduction
935935
This method is used to speed up the search for the best split. It is a proxy quantity such that the
936936
split that maximizes this value also maximizes the impurity improvement. It neglects all constant terms
@@ -995,7 +995,7 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
995995
return (proxy_impurity_left + proxy_impurity_right)
996996

997997
cdef void children_impurity(self, double* impurity_left,
998-
double* impurity_right) nogil:
998+
double* impurity_right) noexcept nogil:
999999
"""Evaluate the impurity in children nodes, i.e. the impurity of the
10001000
left child (samples[start:pos]) and the impurity the right child
10011001
(samples[pos:end]). For n_outputs > 2, we use the parent class children impurity.
@@ -1062,7 +1062,7 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
10621062

10631063

10641064
cdef double _get_min_eigv(self, DOUBLE_t* J_child, DOUBLE_t* var_child,
1065-
double weighted_n_child) nogil except -1:
1065+
double weighted_n_child) except -1 nogil:
10661066
""" Since in this class we keep track of the whole jacobian of each child, we can
10671067
afford a better proxy for the minimum eigenvalue. Here we propose that on top of the minimum of
10681068
diagonal entries, we also take the minimum of that and the sqrt of every pairwise determinant.
@@ -1122,14 +1122,14 @@ cdef class LinearMomentGRFCriterionMSE(LinearMomentGRFCriterion):
11221122
# un-normalized, we normalize by child weight.
11231123
return sqrt(min) / weighted_n_child
11241124

1125-
cdef double min_eig_left(self) nogil:
1125+
cdef double min_eig_left(self) noexcept nogil:
11261126
return self._get_min_eigv(self.J_left, self.var_left, self.weighted_n_left)
11271127

1128-
cdef double min_eig_right(self) nogil:
1128+
cdef double min_eig_right(self) noexcept nogil:
11291129
return self._get_min_eigv(self.J_right, self.var_right, self.weighted_n_right)
11301130

11311131

1132-
cdef void _fast_invJ(DOUBLE_t* J, DOUBLE_t* invJ, SIZE_t n, double clip) nogil:
1132+
cdef void _fast_invJ(DOUBLE_t* J, DOUBLE_t* invJ, SIZE_t n, double clip) noexcept nogil:
11331133
cdef double det
11341134
if n == 1:
11351135
invJ[0] = 1.0 / J[0] if fabs(J[0]) >= clip else 1/clip # Explicit inverse calculation

econml/grf/_utils.pxd

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ ctypedef np.npy_intp SIZE_t # Type for indices and counters
1010
ctypedef np.npy_int32 INT32_t # Signed 32 bit integer
1111
ctypedef np.npy_uint32 UINT32_t # Unsigned 32 bit integer
1212

13-
cpdef bint matinv(DOUBLE_t[::1, :] a, DOUBLE_t[::1, :] inv_a) nogil
13+
cpdef bint matinv(DOUBLE_t[::1, :] a, DOUBLE_t[::1, :] inv_a) noexcept nogil
1414

15-
cdef bint matinv_(DOUBLE_t* a, DOUBLE_t* inv_a, int m) nogil
15+
cdef bint matinv_(DOUBLE_t* a, DOUBLE_t* inv_a, int m) noexcept nogil
1616

17-
cpdef void lstsq(DOUBLE_t[::1,:] a, DOUBLE_t[::1,:] b, DOUBLE_t[::1, :] sol, bint copy_b=*) nogil
17+
cpdef void lstsq(DOUBLE_t[::1,:] a, DOUBLE_t[::1,:] b, DOUBLE_t[::1, :] sol, bint copy_b=*) noexcept nogil
1818

19-
cdef void lstsq_(DOUBLE_t* a, DOUBLE_t* b, DOUBLE_t* sol, int m, int n, int ldb, int nrhs, bint copy_b=*) nogil
19+
cdef void lstsq_(DOUBLE_t* a, DOUBLE_t* b, DOUBLE_t* sol, int m, int n, int ldb, int nrhs, bint copy_b=*) noexcept nogil
2020

21-
cpdef void pinv(DOUBLE_t[::1,:] a, DOUBLE_t[::1, :] sol) nogil
21+
cpdef void pinv(DOUBLE_t[::1,:] a, DOUBLE_t[::1, :] sol) noexcept nogil
2222

23-
cdef void pinv_(DOUBLE_t* a, DOUBLE_t* sol, int m, int n) nogil
23+
cdef void pinv_(DOUBLE_t* a, DOUBLE_t* sol, int m, int n) noexcept nogil
2424

25-
cpdef double fast_max_eigv(DOUBLE_t[::1, :] A, int reps, UINT32_t random_state) nogil
25+
cpdef double fast_max_eigv(DOUBLE_t[::1, :] A, int reps, UINT32_t random_state) noexcept nogil
2626

27-
cdef double fast_max_eigv_(DOUBLE_t* A, int n, int reps, UINT32_t* random_state) nogil
27+
cdef double fast_max_eigv_(DOUBLE_t* A, int n, int reps, UINT32_t* random_state) noexcept nogil
2828

29-
cpdef double fast_min_eigv(DOUBLE_t[::1, :] A, int reps, UINT32_t random_state) nogil
29+
cpdef double fast_min_eigv(DOUBLE_t[::1, :] A, int reps, UINT32_t random_state) noexcept nogil
3030

31-
cdef double fast_min_eigv_(DOUBLE_t* A, int n, int reps, UINT32_t* random_state) nogil
31+
cdef double fast_min_eigv_(DOUBLE_t* A, int n, int reps, UINT32_t* random_state) noexcept nogil

0 commit comments

Comments
 (0)