Skip to content

Commit

Permalink
MAINT: Reduce Cython/C compiler warnings
Browse files Browse the repository at this point in the history
- Builds on: 3dab5bf
- Related issue: #160
- Turn on & address some Cython compiler warnings
- Force explicit C type conversions
  • Loading branch information
ntfrgl committed Jun 27, 2023
1 parent 21b0b6f commit 402197f
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 160 deletions.
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
win = system() == 'Windows'
c_args = {
'include_dirs': [np.get_include()],
'extra_compile_args': ['/O2' if win else '-O3', '-D_GNU_SOURCE',
*([] if win else ['-std=c99'])],
'extra_compile_args': ['-D_GNU_SOURCE'] + (
['/O2']
if win else
['-O3', '-std=c99', '-Wconversion']),
'define_macros': [('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')]}
cy_args = {
'language_level': 3, 'embedsignature': True,
'boundscheck': True, 'wraparound': False,
'initializedcheck': True, 'nonecheck': True}
'initializedcheck': True, 'nonecheck': True,
'warn.unused': True, 'warn.unused_arg': True}


# ==============================================================================
Expand Down
16 changes: 9 additions & 7 deletions src/pyunicorn/climate/_ext/numerics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ from ...core._ext.types import FIELD, DFIELD
from ...core._ext.types cimport MASK_t, FIELD_t, DFIELD_t

cdef extern from "src_numerics.c":
void _cython_calculate_mutual_information(
void _mutual_information(
float *anomaly, int n_samples, int N, int n_bins, double scaling,
double range_min, long *symbolic, long *hist, long *hist2d,
float *mi)
void _calculate_corr_fast(int m, int tmax, bint *final_mask,
void _spearman_corr(int m, int tmax, bint *final_mask,
float *time_series_ranked, float *spearman_rho)


# mutual_info =================================================================

def _calculate_mutual_information_cython(

def mutual_information(
np.ndarray[FIELD_t, ndim=2, mode='c'] anomaly not None,
int n_samples, int N, int n_bins, double scaling, double range_min):
int n_samples, int N, int n_bins, float scaling, float range_min):

cdef:
np.ndarray[DFIELD_t, ndim=2, mode='c'] symbolic = np.zeros(
Expand All @@ -47,7 +48,7 @@ def _calculate_mutual_information_cython(
np.ndarray[FIELD_t, ndim=2, mode='c'] mi = np.zeros(
(N, N), dtype=FIELD)

_cython_calculate_mutual_information(
_mutual_information(
<float*> np.PyArray_DATA(anomaly), n_samples, N, n_bins, scaling,
range_min, <long*> np.PyArray_DATA(symbolic),
<long*> np.PyArray_DATA(hist), <long*> np.PyArray_DATA(hist2d),
Expand All @@ -58,14 +59,15 @@ def _calculate_mutual_information_cython(

# rainfall ====================================================================

def _calculate_corr(int m, int tmax,

def spearman_corr(int m, int tmax,
np.ndarray[MASK_t, ndim=2, mode='c'] final_mask not None,
np.ndarray[FIELD_t, ndim=2, mode='c'] time_series_ranked not None):

cdef np.ndarray[FIELD_t, ndim=2, mode='c'] spearman_rho = np.zeros(
(m, m), dtype=FIELD)

_calculate_corr_fast(m, tmax,
_spearman_corr(m, tmax,
<bint*> np.PyArray_DATA(final_mask),
<float*> np.PyArray_DATA(time_series_ranked),
<float*> np.PyArray_DATA(spearman_rho))
Expand Down
21 changes: 11 additions & 10 deletions src/pyunicorn/climate/_ext/src_numerics.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// mutual_info ================================================================

void _cython_calculate_mutual_information(float *anomaly, int n_samples,
void _mutual_information(float *anomaly, int n_samples,
int N, int n_bins, float scaling, float range_min, long *symbolic,
long *hist, long *hist2d, float *mi) {

Expand Down Expand Up @@ -53,7 +53,7 @@ void _cython_calculate_mutual_information(float *anomaly, int n_samples,
// Calculate symbolic trajectories for each time series,
// where the symbols are bin numbers.
if (rescaled < 1.0) {
*p_symbolic = (long) rescaled * n_bins;
*p_symbolic = (long) (rescaled * n_bins);
}
else {
*p_symbolic = n_bins - 1;
Expand Down Expand Up @@ -129,17 +129,17 @@ void _cython_calculate_mutual_information(float *anomaly, int n_samples,
// Set pointer to hist2d(l,0)
p_hist2d = hist2d + ln_bins;

hpl = (*p_hist1) * norm;
hpl = (double) (*p_hist1) * norm;

if (hpl > 0.0) {
for (m = 0; m < n_bins; m++) {

hpm = (*p_hist2) * norm;
hpm = (double) (*p_hist2) * norm;

if (hpm > 0.0) {
plm = (*p_hist2d) * norm;
plm = (double) (*p_hist2d) * norm;
if (plm > 0.0) {
*p_mi += (float) plm * log(plm/hpm/hpl);
*p_mi += (float) (plm * log(plm/hpm/hpl));
}
}

Expand Down Expand Up @@ -193,7 +193,8 @@ void _cython_calculate_mutual_information(float *anomaly, int n_samples,

// rainfall ===================================================================

void _calculate_corr_fast(int m, int tmax, int *final_mask,

void _spearman_corr(int m, int tmax, int *final_mask,
float *time_series_ranked, float *spearman_rho) {

double cov = 0, sigmai = 0, sigmaj = 0, meani = 0, meanj = 0;
Expand All @@ -212,8 +213,8 @@ void _calculate_corr_fast(int m, int tmax, int *final_mask,
}

for (int t=0; t<tmax; t++) {
rankedi[t] = time_series_ranked[i*m+t] - zerocount;
rankedj[t] = time_series_ranked[j*m+t] - zerocount;
rankedi[t] = time_series_ranked[i*m+t] - (float) zerocount;
rankedj[t] = time_series_ranked[j*m+t] - (float) zerocount;
}

for (int t=0; t<tmax; t++) {
Expand All @@ -236,7 +237,7 @@ void _calculate_corr_fast(int m, int tmax, int *final_mask,
}
}
spearman_rho[i*m+j] = spearman_rho[j*m+i] =
(float) cov/sqrt(sigmai*sigmaj);
(float) (cov/sqrt(sigmai*sigmaj));
meani = 0;
meanj = 0;
cov = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/pyunicorn/climate/mutual_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import numpy as np

from ..core._ext.types import to_cy, FIELD
from ._ext.numerics import _calculate_mutual_information_cython
from ._ext.numerics import mutual_information

# Import progress bar for easy progress bar handling
from ..utils import progressbar
Expand Down Expand Up @@ -156,7 +156,7 @@ def _cython_calculate_mutual_information(self, anomaly, n_bins=32):
# using the maximum range of the whole dataset.
scaling = 1./(range_max - range_min)

mi = _calculate_mutual_information_cython(
mi = mutual_information(
to_cy(anomaly, FIELD), n_samples, N, n_bins, scaling, range_min)

if self.silence_level <= 1:
Expand Down
8 changes: 4 additions & 4 deletions src/pyunicorn/climate/rainfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# import numpy as np

from ..core._ext.types import to_cy, MASK, FIELD
from ._ext.numerics import _calculate_corr
from ._ext.numerics import spearman_corr

# Import cnTsonisClimateNetwork for TsonisClimateNetwork class
from .climate_network import ClimateNetwork
Expand Down Expand Up @@ -179,7 +179,7 @@ def _calculate_correlation(self, event_threshold, scale_fac, offset,
print("Calculating Spearman-Rho-Matrix using Cython...")

# Return the correlation matrix
return self.calculate_corr(final_mask, anomaly)
return self.spearman_corr(final_mask, anomaly)

@staticmethod
def calculate_rainfall(observable, scale_fac, offset):
Expand Down Expand Up @@ -262,7 +262,7 @@ def rank_time_series(anomaly):
rank_time_series = anomaly.argsort(axis=1).argsort(axis=1) + 1.0
return rank_time_series

def calculate_corr(self, final_mask, anomaly):
def spearman_corr(self, final_mask, anomaly):
"""
Return the Spearman Correlation Matrix at zero lag.
Expand All @@ -281,5 +281,5 @@ def calculate_corr(self, final_mask, anomaly):
# Get rank time series
time_series_ranked = self.rank_time_series(anomaly)
m, tmax = anomaly.shape
return _calculate_corr(
return spearman_corr(
m, tmax, to_cy(final_mask, MASK), to_cy(time_series_ranked, FIELD))
Loading

0 comments on commit 402197f

Please sign in to comment.