-
Notifications
You must be signed in to change notification settings - Fork 0
Test whether provided error bound is satisfied #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7b20875
Add test to check whether error bound is satisfied
treigerm f3b5c78
Explicit string casting to make mypy happy
treigerm ee57113
Make error check inclusive
treigerm 7b0faae
Fix relative error bound bug
treigerm bd29d35
Add check that float parsing is successfull
treigerm 47c1ddc
Add float type assertion and infinity checks
treigerm 0b16283
Merge branch 'redo_tests' of github.com:ClimateBenchPress/compressor …
treigerm 460829d
Switch to logical operators to make mypy happy
treigerm 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 hidden or 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 hidden or 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,5 +1,6 @@ | ||
| from . import abc as abc | ||
| from .error_bound import ErrorBound | ||
| from .r2_correlation import R2 | ||
| from .spatial_relative_error import SRE | ||
|
|
||
| __all__ = ["SRE", "R2"] | ||
| __all__ = ["SRE", "R2", "ErrorBound"] |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
| from .abc import Test | ||
|
|
||
|
|
||
| class ErrorBound(Test): | ||
| def __init__(self, error_type: str, threshold: float = 0.05): | ||
| self.threshold = threshold | ||
| assert error_type in [ | ||
| "abs_error", | ||
| "rel_error", | ||
| ], f"error_type must be either 'abs_error' or 'rel_error', not {error_type}" | ||
| self.error_type = error_type | ||
|
|
||
| def __call__(self, x: xr.DataArray, y: xr.DataArray) -> tuple[bool, float]: | ||
| # Check that two arrays are both floats | ||
| assert x.dtype.kind == "f", f"Expected x to be float, got {x.dtype}" | ||
| assert y.dtype.kind == "f", f"Expected y to be float, got {y.dtype}" | ||
|
|
||
| abs_error = np.abs(x - y) | ||
| relative_error = abs_error / abs(x) | ||
|
|
||
| error_to_check = abs_error if self.error_type == "abs_error" else relative_error | ||
| satisfied = error_to_check <= self.threshold | ||
|
treigerm marked this conversation as resolved.
|
||
|
|
||
| # The comparison does not work for NaN values, as `np.nan < threshold` is False. | ||
| # This check ensures that if x contains a NaN then y must also contain a NaN at | ||
| # the same location. | ||
| # Note, it is an error to have a NaN in y and not in x which will be caught. | ||
| x_and_y_nan = np.isnan(x) & np.isnan(y) | ||
| satisfied = satisfied | x_and_y_nan | ||
|
|
||
| # Similarly, np.inf - np.inf is NaN but should pass the test. | ||
| # The x == y condition ensures that their sign is the same. | ||
| x_and_y_inf = np.isinf(x) & np.isinf(y) & x == y | ||
| satisfied = satisfied | x_and_y_inf | ||
|
|
||
| # Proportion of entries that exceed the threshold. | ||
| exceed_thresh = np.sum(~satisfied) / x.size | ||
|
juntyr marked this conversation as resolved.
|
||
| return bool(exceed_thresh == 0.0), float(exceed_thresh) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.