-
Notifications
You must be signed in to change notification settings - Fork 40
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
Test raise if compute #224
Merged
Merged
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3d443ac
Test raise if compute
538e653
No eager compute
3df66c5
Remove whitespace from docstring
91a0c05
Convert comment to """
f19cf3a
Merge branch 'master' into no_compute
ahuang11 9489c6e
Update xskillscore.Contingency.rst
ahuang11 64b8475
Update minimum-tests.yml
raybellwaves 131378e
Update minimum-tests.yml
raybellwaves 6d922f8
Update minimum-tests.yml
raybellwaves 3a7399f
Update minimum-tests.yml
raybellwaves 7335ad3
Update minimum-tests.yml
raybellwaves 5c71d29
Update CHANGELOG.rst
raybellwaves 07b429e
Update dev.yml
raybellwaves b57f40c
Update dev.yml
raybellwaves b675549
Update doc.yml
raybellwaves f7f6df9
Update docs_notebooks.yml
raybellwaves 8f750b8
Update minimum-tests.yml
raybellwaves 1348a72
lint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import dask | ||
import numpy as np | ||
import pytest | ||
from xarray.tests import assert_allclose | ||
|
@@ -34,6 +35,31 @@ | |
NON_WEIGHTED_METRICS = [median_absolute_error] | ||
|
||
|
||
class CountingScheduler: | ||
"""Simple dask scheduler counting the number of computes. | ||
Reference: https://stackoverflow.com/questions/53289286/""" | ||
|
||
def __init__(self, max_computes=0): | ||
self.total_computes = 0 | ||
self.max_computes = max_computes | ||
|
||
def __call__(self, dsk, keys, **kwargs): | ||
self.total_computes += 1 | ||
if self.total_computes > self.max_computes: | ||
raise RuntimeError( | ||
"Too many computes. Total: %d > max: %d." | ||
% (self.total_computes, self.max_computes) | ||
) | ||
return dask.get(dsk, keys, **kwargs) | ||
|
||
|
||
def raise_if_dask_computes(max_computes=0): | ||
# return a dummy context manager so | ||
# that this can be used for non-dask objects | ||
scheduler = CountingScheduler(max_computes) | ||
return dask.config.set(scheduler=scheduler) | ||
|
||
|
||
def drop_nans(a, b, weights=None, dim="time"): | ||
""" | ||
Masks a and b where they have pairwise nans. | ||
|
@@ -51,8 +77,9 @@ def test_skipna_returns_same_value_as_dropped_pairwise_nans(a_1d_nan, b_1d_nan, | |
"""Tests that DataArrays with pairwise nans return the same result | ||
as the same two with those nans dropped.""" | ||
a_dropped, b_dropped, _ = drop_nans(a_1d_nan, b_1d_nan) | ||
res_with_nans = metric(a_1d_nan, b_1d_nan, "time", skipna=True) | ||
res_dropped_nans = metric(a_dropped, b_dropped, "time") | ||
with raise_if_dask_computes(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thats a nice one. could be implemented allover the package |
||
res_with_nans = metric(a_1d_nan, b_1d_nan, "time", skipna=True) | ||
res_dropped_nans = metric(a_dropped, b_dropped, "time") | ||
assert_allclose(res_with_nans, res_dropped_nans) | ||
|
||
|
||
|
@@ -65,20 +92,22 @@ def test_skipna_returns_same_value_as_dropped_pairwise_nans_with_weights( | |
a_dropped, b_dropped, weights_time_dropped = drop_nans( | ||
a_1d_nan, b_1d_nan, weights_time | ||
) | ||
res_with_nans = metric( | ||
a_1d_nan, b_1d_nan, "time", skipna=True, weights=weights_time | ||
) | ||
res_dropped_nans = metric( | ||
a_dropped, b_dropped, "time", weights=weights_time_dropped | ||
) | ||
with raise_if_dask_computes(): | ||
res_with_nans = metric( | ||
a_1d_nan, b_1d_nan, "time", skipna=True, weights=weights_time | ||
) | ||
res_dropped_nans = metric( | ||
a_dropped, b_dropped, "time", weights=weights_time_dropped | ||
) | ||
assert_allclose(res_with_nans, res_dropped_nans) | ||
|
||
|
||
@pytest.mark.parametrize("metric", WEIGHTED_METRICS + NON_WEIGHTED_METRICS) | ||
def test_skipna_returns_nan_when_false(a_1d_nan, b_1d_nan, metric): | ||
"""Tests that nan is returned if there's any nans in the time series | ||
and skipna is False.""" | ||
res = metric(a_1d_nan, b_1d_nan, "time", skipna=False) | ||
with raise_if_dask_computes(): | ||
res = metric(a_1d_nan, b_1d_nan, "time", skipna=False) | ||
assert np.isnan(res).all() | ||
|
||
|
||
|
@@ -88,10 +117,12 @@ def test_skipna_broadcast_weights_assignment_destination( | |
): | ||
"""Tests that 'assignment destination is read-only' is not raised | ||
https://github.com/xarray-contrib/xskillscore/issues/79""" | ||
metric(a_nan, b_nan, ["lat", "lon"], weights=weights_lonlat, skipna=True) | ||
with raise_if_dask_computes(): | ||
metric(a_nan, b_nan, ["lat", "lon"], weights=weights_lonlat, skipna=True) | ||
|
||
|
||
def test_nan_skipna(a, b): | ||
# Randomly add some nans to a | ||
a = a.where(np.random.random(a.shape) < 0.5) | ||
pearson_r(a, b, dim="lat", skipna=True) | ||
with raise_if_dask_computes(): | ||
pearson_r(a, b, dim="lat", skipna=True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm being nit-picky, you should change this to a
"""
docstring for style.