Skip to content

Commit

Permalink
Cython3 (#128)
Browse files Browse the repository at this point in the history
* make all cdef also noexcept for cython 3
  • Loading branch information
samuelstjean committed Oct 31, 2023
1 parent 706a0be commit d044d67
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cibuildwheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
name: Build wheels on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

Expand Down
20 changes: 10 additions & 10 deletions nlsam/stabilizer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def multiprocess_stabilization(const floating[:,:,:] data, const floating1[:,:,:
return out, eta


cdef double chi_to_gauss(double m, double eta, double sigma, double N, double alpha, bint use_nan) nogil:
cdef double chi_to_gauss(double m, double eta, double sigma, double N, double alpha, bint use_nan) noexcept nogil:
"""Maps the noisy signal intensity from a Rician/Non central chi distribution
to its gaussian counterpart. See p. 4 of [1] eq. 12.
Expand Down Expand Up @@ -103,7 +103,7 @@ cdef double chi_to_gauss(double m, double eta, double sigma, double N, double al
return inv_cdf_gauss


cdef inline double _marcumq_cython(double a, double b, double M, double eps=1e-8) nogil:
cdef inline double _marcumq_cython(double a, double b, double M, double eps=1e-8) noexcept nogil:
"""Computes the generalized Marcum Q function of order M.
https://en.wikipedia.org/wiki/Marcum_Q-function
Expand Down Expand Up @@ -152,7 +152,7 @@ cdef inline double _marcumq_cython(double a, double b, double M, double eps=1e-8


cdef double fixed_point_finder(double m_hat, double sigma, double N,
bint clip_eta=True, int max_iter=100, double eps=1e-6) nogil:
bint clip_eta=True, int max_iter=100, double eps=1e-6) noexcept nogil:
"""Fixed point formula for finding eta. Table 1 p. 11 of [1]
Input
Expand Down Expand Up @@ -220,13 +220,13 @@ cdef double fixed_point_finder(double m_hat, double sigma, double N,
return t1


cdef inline double _beta(double N) nogil:
cdef inline double _beta(double N) noexcept nogil:
"""Helper function for xi, see p. 3 [1] just after eq. 8.
Generalized version for non integer N"""
return sqrt(2) * gamma(N + 0.5) / gamma(N)


cdef inline double _fixed_point_k(double eta, double m, double sigma, double N) nogil:
cdef inline double _fixed_point_k(double eta, double m, double sigma, double N) noexcept nogil:
"""Helper function for fixed_point_finder, see p. 11 [1] eq. D2."""
cdef:
double fpg, num, denom, h1f1m, h1f1p
Expand All @@ -242,7 +242,7 @@ cdef inline double _fixed_point_k(double eta, double m, double sigma, double N)
return eta - num / denom


cdef inline double _fixed_point_k_v2(double eta, double m, double sigma, double N) nogil:
cdef inline double _fixed_point_k_v2(double eta, double m, double sigma, double N) noexcept nogil:
"""Helper function for fixed_point_finder, see p. 11 [1] eq. D3.
This is a secret equation scheme which gives rise to a different fixed point iteration
Expand All @@ -260,7 +260,7 @@ cdef inline double _fixed_point_k_v2(double eta, double m, double sigma, double

return eta + num / denom

cdef inline double xi(double eta, double sigma, double N) nogil:
cdef inline double xi(double eta, double sigma, double N) noexcept nogil:
"""Standard deviation scaling factor formula, see p. 3 of [1], eq. 10.
Input
Expand Down Expand Up @@ -300,7 +300,7 @@ cdef inline double xi(double eta, double sigma, double N) nogil:


# Helper function for the root finding loop
cdef inline double k(double theta, double N, double r) nogil:
cdef inline double k(double theta, double N, double r) noexcept nogil:
cdef:
# Fake SNR value for xi
double eta = theta
Expand All @@ -317,7 +317,7 @@ cdef inline double k(double theta, double N, double r) nogil:
return theta - num / denom


cdef double root_finder(double r, double N, int max_iter=500, double eps=1e-6) nogil:
cdef double root_finder(double r, double N, int max_iter=500, double eps=1e-6) noexcept nogil:
cdef:
double lower_bound = sqrt((2*N / xi(0.0, 1.0, N)) - 1)
double t0, t1 = 0.0
Expand Down Expand Up @@ -354,7 +354,7 @@ def root_finder_loop(const floating[:] data, const floating1[:] sigma, const flo
return corrected_sigma


cdef inline bint check_high_SNR(double eta, double sigma, double N) nogil:
cdef inline bint check_high_SNR(double eta, double sigma, double N) noexcept nogil:
'''If the SNR is high enough against N, these corrections factors change basically nothing, so may as well return early.'''
cdef:
double SNR = eta / sigma
Expand Down
8 changes: 4 additions & 4 deletions nlsam/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

cdef void _im2col3D(double[:,:,::1] A, double[::1,:] R, int[:] size) nogil:
cdef void _im2col3D(double[:,:,::1] A, double[::1,:] R, int[:] size) noexcept nogil:

cdef:
Py_ssize_t k = 0, l = 0
Expand Down Expand Up @@ -50,7 +50,7 @@ cdef void _im2col3D_overlap(double[:,:,::1] A, double[::1,:] R, int[:] size, int
k += 1


cdef void _im2col4D(double[:,:,:,::1] A, double[::1,:] R, int[:] size) nogil:
cdef void _im2col4D(double[:,:,:,::1] A, double[::1,:] R, int[:] size) noexcept nogil:

cdef:
Py_ssize_t k = 0, l = 0
Expand Down Expand Up @@ -142,7 +142,7 @@ cdef void _col2im3D_overlap(double[:,:,::1] A, double[:,:,::1] div, double[::1,:
k += 1


cdef void _col2im3D(double[:,:,::1] A, double[:,:,::1] div, double[::1,:] R, double[:] weights, int[:] block_shape) nogil:
cdef void _col2im3D(double[:,:,::1] A, double[:,:,::1] div, double[::1,:] R, double[:] weights, int[:] block_shape) noexcept nogil:

cdef:
Py_ssize_t k = 0, l = 0
Expand All @@ -167,7 +167,7 @@ cdef void _col2im3D(double[:,:,::1] A, double[:,:,::1] div, double[::1,:] R, dou
k += 1


cdef void _col2im4D(double[:,:,:,::1] A, double[:,:,::1] div, double[::1,:] R, double[:] weights, int[:] block_shape) nogil:
cdef void _col2im4D(double[:,:,:,::1] A, double[:,:,::1] div, double[::1,:] R, double[:] weights, int[:] block_shape) noexcept nogil:
cdef:
Py_ssize_t k = 0, l = 0
Py_ssize_t a, b, c, d, m, n, o, p
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["Cython>=0.29.14",
requires = ["Cython>=0.29.33",
"scipy>=1.5",
"oldest-supported-numpy",
"setuptools",
Expand All @@ -19,7 +19,7 @@ license = {text = "GPLv3"}
dependencies = [
'numpy>=1.15.4',
'scipy>=1.5',
'cython>=0.29',
'cython>=0.29.33',
'nibabel>=2.0',
'joblib>=0.14.1',
'autodmri>=0.2.1',
Expand Down

0 comments on commit d044d67

Please sign in to comment.