Skip to content

Commit 465e372

Browse files
authored
rounding error fixes (#281)
1 parent 806fe23 commit 465e372

File tree

7 files changed

+7841
-6957
lines changed

7 files changed

+7841
-6957
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Changelog
55
Unreleased
66
==========
77

8+
Version 0.14.3, 2022-12-26
9+
==========================
10+
- fixing rounding error issues in metric calculations (PR `#281 <https://github.com/TUW-GEO/pytesmo/pull/281>`_)
11+
812
Version 0.14.2, 2022-12-14
913
==========================
1014
- small bug fixes/doc updates (PRs `#273 <https://github.com/TUW-GEO/pytesmo/pull/273>`_, `#275 <https://github.com/TUW-GEO/pytesmo/pull/275>`_, `#276 <https://github.com/TUW-GEO/pytesmo/pull/276>`_, `#278 <https://github.com/TUW-GEO/pytesmo/pull/278>`_)

src/pytesmo/metrics/_fast.c

Lines changed: 1742 additions & 1528 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pytesmo/metrics/_fast_pairwise.c

Lines changed: 4337 additions & 3891 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pytesmo/metrics/_fast_pairwise.pyx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ cpdef mse_corr(floating [:] x, floating [:] y):
132132
cpdef _mse_corr_from_moments(
133133
floating mx, floating my, floating vx, floating vy, floating cov
134134
):
135-
return 2 * sqrt(vx) * sqrt(vy) - 2 * cov
135+
return max(2 * sqrt(vx) * sqrt(vy) - 2 * cov, 0)
136136

137137

138138

@@ -322,7 +322,12 @@ cpdef _ubrmsd(floating [:] x, floating [:] y):
322322
cpdef _pearsonr_from_moments(floating varx, floating vary, floating cov, int n):
323323
cdef floating R, p_R, t_squared, df, z
324324

325-
R = cov / sqrt(varx * vary)
325+
# not defined in this case
326+
if varx == 0 or vary == 0:
327+
return np.nan, np.nan
328+
329+
# due to rounding errors, values slightly above 1 are possible
330+
R = max(-1, min(cov / sqrt(varx * vary), 1))
326331

327332
# p-value for R
328333
if fabs(R) == 1.0:

src/pytesmo/time_series/filters.c

Lines changed: 1748 additions & 1534 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pytesmo/validation_framework/error_handling.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
# Failure in temporal matching
2121
TEMPORAL_MATCHING_FAILED = 3
2222
# Temporal matching returned without error, but the data we need is not
23-
# available
23+
# available. This can happen if there is no temporal overlap of the required
24+
# datasets, or if there is no data at all for one of the required datasets.
2425
NO_TEMP_MATCHED_DATA = 4
2526
# the scaling failed
2627
SCALING_FAILED = 5

src/pytesmo/validation_framework/metric_calculators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ def _calc_pairwise_metrics(
14731473
mse_corr = _mse_corr_from_moments(mx, my, varx, vary, cov)
14741474
mse_var = _mse_var_from_moments(mx, my, varx, vary, cov)
14751475
mse_bias = _mse_bias_from_moments(mx, my, varx, vary, cov)
1476-
mse = mse_corr + mse_var + mse_bias
1476+
mse = max(mse_corr + mse_var + mse_bias, 0)
14771477
result["mse_corr" + suffix][0] = mse_corr
14781478
result["mse_var" + suffix][0] = mse_var
14791479
result["mse_bias" + suffix][0] = mse_bias

0 commit comments

Comments
 (0)