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

[Numpy2] Preprocessor distance_metric returns value 0 instead of -- (masked element) with numpy==2.0.0 for metrics that use np.sqrt or da.sqrt #2460

Open
valeriupredoi opened this issue Jun 19, 2024 · 1 comment · May be fixed by #2395
Assignees
Labels
bug Something isn't working Numpy2
Milestone

Comments

@valeriupredoi
Copy link
Contributor

FAILED tests/unit/preprocessor/_compare_with_refs/test_compare_with_refs.py::test_distance_metric[weighted_pearsonr-nan-1.0-Pearson's r-pearsonr_tas-1] - AssertionError: 
FAILED tests/unit/preprocessor/_compare_with_refs/test_compare_with_refs.py::test_distance_metric_masked_data[emd-1.98625-0.0-EMD-emd_tas-K-True] - AssertionError: 
FAILED tests/unit/preprocessor/_compare_with_refs/test_compare_with_refs.py::test_distance_metric_fully_masked_data[rmse-2.34520788-0.0-RMSE-rmse_tas-K-True] - AssertionError: 
FAILED tests/unit/preprocessor/_compare_with_refs/test_compare_with_refs.py::test_distance_metric_fully_masked_data[rmse-2.34520788-0.0-RMSE-rmse_tas-K-False] - AssertionError: 
FAILED tests/unit/preprocessor/_compare_with_refs/test_compare_with_refs.py::test_distance_metric_fully_masked_data[weighted_rmse-2.0-0.0-RMSE-rmse_tas-K-True] - AssertionError: 
FAILED tests/unit/preprocessor/_compare_with_refs/test_compare_with_refs.py::test_distance_metric_fully_masked_data[weighted_rmse-2.0-0.0-RMSE-rmse_tas-K-False] - AssertionError:

could be Scipy could be something else, this needs a bit of due dilligence 🍺

@valeriupredoi valeriupredoi added the bug Something isn't working label Jun 19, 2024
@valeriupredoi valeriupredoi changed the title Preprocessor distance_metric returns value 0 instead of -- (masked element) with numpy==2.0.0 for certain metrics [Numpy2] Preprocessor distance_metric returns value 0 instead of -- (masked element) with numpy==2.0.0 for certain metrics Jun 19, 2024
@valeriupredoi
Copy link
Contributor Author

valeriupredoi commented Jun 21, 2024

This is a bug introduced with Numpy=2.0: in

rmse = npx.sqrt(npx.ma.average(squared_error, axis=axis, weights=weights))

the sqrt is not computed correctly in the case of masked arrays: one needs to account both for Numpy and Dask:

    npx = get_array_module(squared_error)

    # need masked sqrt for numpy >=2.0
    # and dask.array.reductions.safe_sqrt for Dask
    # otherwise results will be computed ignoring masks
    if npx.__name__ == "dask.array":
        da_squared_error = npx.ma.average(squared_error,
                                          axis=axis,
                                          weights=weights)
        rmse = npx.reductions.safe_sqrt(da_squared_error)
    else:
        rmse = npx.ma.sqrt(
            npx.ma.average(squared_error, axis=axis, weights=weights)
        )

this is fixed in #2395

ie if np.sqrt() ignores masks then da.sqrt() will too, see numpy/numpy#25635 that may be fixed, or not - the issue is pretty old now wrt Numpy2 timeline, so better we have the patch in our code

@valeriupredoi valeriupredoi added this to the v2.12.0 milestone Jun 21, 2024
@valeriupredoi valeriupredoi linked a pull request Jun 21, 2024 that will close this issue
@valeriupredoi valeriupredoi changed the title [Numpy2] Preprocessor distance_metric returns value 0 instead of -- (masked element) with numpy==2.0.0 for certain metrics [Numpy2] Preprocessor distance_metric returns value 0 instead of -- (masked element) with numpy==2.0.0 for metrics that use np.sqrt or da.sqrt Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Numpy2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants