Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fairmode rounding issue #1142

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyaerocom/stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _prepare_statistics(
result = dict()
for name, stat in statistics.items():
if len(data) >= min_numvalid:
result[name] = stat(data, ref_data, weights)
result[name] = round(stat(data, ref_data, weights), 10)
else:
result[name] = np.nan

Expand Down
19 changes: 19 additions & 0 deletions tests/stats/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,22 @@ def test_calc_statistics_drop_stats(data, ref_data, drop):
stats = calculate_statistics(data, ref_data, drop_stats=drop)

assert all([x not in stats.keys() for x in drop])


def test_fairmode_R_in_expected_range():
# This test is meant to test for a specific issue where
# using fairmode=true would result in errors due to the calculated
# R values being slightly out of range -1<=R<=1. The error is caused
# because fairmode_stats() expects R values exactly in the range, but
# values may be outside due to floating point shenanigans.
# https://xkcd.com/217/
# As of the time of writing this test, the data used for this test is
# known to result in a failing test, IF *no* rounding is applied in
# `stats.stats._prepare_statistics().`
# https://github.com/metno/pyaerocom/pull/1142
data1 = np.asarray([17.2073523845202, 15.543783901903302, np.nan])
data2 = np.asarray([15.046913580246915, 18.462202650290884, np.nan])

stats = calculate_statistics(data1, data2)

assert -1 <= stats["R"] <= 1