-
Notifications
You must be signed in to change notification settings - Fork 120
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
Extend the validate()
signature
#804
Merged
danielhuppmann
merged 13 commits into
IAMconsortium:main
from
danielhuppmann:feature/validate
Dec 12, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
68e7c09
Add deprecation warnings and update signature
danielhuppmann 7743695
Extend `slice` docstring
danielhuppmann e2ae13e
Implement translation of `criteria` to new signature
danielhuppmann a3c5ff9
Extend tests to cover new and legacy signature
danielhuppmann 01258db
Add test for "empty" validation
danielhuppmann 03d5555
Add log message if filters do not match any datapoints
danielhuppmann c31aad9
Add test for validating multiple criteria
danielhuppmann 0ae5719
Rewrite warning message
danielhuppmann e6a068e
Update first-steps tutorial
danielhuppmann ac42341
Add to release notes
danielhuppmann ea304a8
Fix a typo
danielhuppmann df54582
Harmonize with docs.ece.iiasa.ac.at
danielhuppmann a2fefa9
Refactor to `upper_bound` and `lower_bound` as suggested by @phackstock
danielhuppmann 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
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 |
---|---|---|
|
@@ -68,77 +68,166 @@ def test_require_data(test_df_year, kwargs, exclude_on_fail): | |
assert list(df.exclude) == [False, False] | ||
|
||
|
||
def test_validate_pass(test_df): | ||
obs = test_df.validate({"Primary Energy": {"up": 10}}, exclude_on_fail=True) | ||
# include args for deprecated legacy signature | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
dict(variable="Primary Energy"), | ||
dict(criteria={"Primary Energy": {}}), | ||
dict(variable="foo", upper_bound=10), | ||
dict(criteria={"foo": {"up": 10}}), | ||
), | ||
) | ||
def test_validate_none(test_df, args): | ||
# validation for non-existing variables or without upper or lower bound passes | ||
obs = test_df.validate(**args, exclude_on_fail=True) | ||
assert obs is None | ||
assert list(test_df.exclude) == [False, False] # none excluded | ||
|
||
|
||
# include args for deprecated legacy signature | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
dict(variable="Primary Energy", upper_bound=10), | ||
dict(criteria={"Primary Energy": {"up": 10}}), | ||
), | ||
) | ||
def test_validate_pass(test_df, args): | ||
obs = test_df.validate(**args, exclude_on_fail=True) | ||
assert obs is None | ||
assert list(test_df.exclude) == [False, False] # none excluded | ||
|
||
|
||
def test_validate_nonexisting(test_df): | ||
# include args for deprecated legacy signature | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
dict(variable="Primary Energy|Coal", upper_bound=2), | ||
dict(criteria={"Primary Energy|Coal": {"up": 2}}), | ||
), | ||
) | ||
def test_validate_nonexisting(test_df, args): | ||
# checking that a scenario with no relevant value does not fail validation | ||
obs = test_df.validate({"Primary Energy|Coal": {"up": 2}}, exclude_on_fail=True) | ||
obs = test_df.validate(**args, exclude_on_fail=True) | ||
# checking that the return-type is correct | ||
pdt.assert_frame_equal(obs, test_df.data[3:4].reset_index(drop=True)) | ||
# scenario with failed validation excluded, scenario with no value passes | ||
assert list(test_df.exclude) == [True, False] | ||
|
||
|
||
def test_validate_up(test_df): | ||
# include args for deprecated legacy signature | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
dict(variable="Primary Energy", upper_bound=6.5), | ||
dict(criteria={"Primary Energy": {"up": 6.5}}), | ||
), | ||
) | ||
def test_validate_up(test_df, args): | ||
# checking that the return-type is correct | ||
obs = test_df.validate({"Primary Energy": {"up": 6.5}}) | ||
obs = test_df.validate(**args) | ||
pdt.assert_frame_equal(obs, test_df.data[5:6].reset_index(drop=True)) | ||
assert list(test_df.exclude) == [False, False] | ||
|
||
# checking exclude on fail | ||
obs = test_df.validate({"Primary Energy": {"up": 6.5}}, exclude_on_fail=True) | ||
obs = test_df.validate(**args, exclude_on_fail=True) | ||
pdt.assert_frame_equal(obs, test_df.data[5:6].reset_index(drop=True)) | ||
assert list(test_df.exclude) == [False, True] | ||
|
||
|
||
def test_validate_lo(test_df): | ||
# include args for deprecated legacy signature | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
dict(variable="Primary Energy", upper_bound=8, lower_bound=2), | ||
dict(criteria={"Primary Energy": {"up": 8, "lo": 2}}), | ||
), | ||
) | ||
def test_validate_lo(test_df, args): | ||
# checking that the return-type is correct | ||
obs = test_df.validate({"Primary Energy": {"up": 8, "lo": 2}}) | ||
obs = test_df.validate(**args) | ||
pdt.assert_frame_equal(obs, test_df.data[0:1].reset_index(drop=True)) | ||
assert list(test_df.exclude) == [False, False] | ||
|
||
# checking exclude on fail | ||
obs = test_df.validate({"Primary Energy": {"up": 8, "lo": 2}}, exclude_on_fail=True) | ||
obs = test_df.validate(**args, exclude_on_fail=True) | ||
pdt.assert_frame_equal(obs, test_df.data[0:1].reset_index(drop=True)) | ||
assert list(test_df.exclude) == [True, False] | ||
|
||
|
||
def test_validate_both(test_df): | ||
# include args for deprecated legacy signature | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
dict(variable="Primary Energy", upper_bound=6.5, lower_bound=2), | ||
dict(criteria={"Primary Energy": {"up": 6.5, "lo": 2}}), | ||
), | ||
) | ||
def test_validate_both(test_df, args): | ||
# checking that the return-type is correct | ||
obs = test_df.validate({"Primary Energy": {"up": 6.5, "lo": 2}}) | ||
obs = test_df.validate(**args) | ||
pdt.assert_frame_equal(obs, test_df.data[0:6:5].reset_index(drop=True)) | ||
assert list(test_df.exclude) == [False, False] | ||
|
||
# checking exclude on fail | ||
obs = test_df.validate( | ||
{"Primary Energy": {"up": 6.5, "lo": 2}}, exclude_on_fail=True | ||
) | ||
obs = test_df.validate(**args, exclude_on_fail=True) | ||
pdt.assert_frame_equal(obs, test_df.data[0:6:5].reset_index(drop=True)) | ||
assert list(test_df.exclude) == [True, True] | ||
|
||
|
||
def test_validate_year(test_df): | ||
# include args for deprecated legacy signature | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
dict(variable="Primary Energy", year=2005, upper_bound=6), | ||
dict(criteria={"Primary Energy": {"up": 6, "year": 2005}}), | ||
), | ||
) | ||
def test_validate_year_2010(test_df, args): | ||
# checking that the year filter works as expected | ||
obs = test_df.validate({"Primary Energy": {"up": 6, "year": 2005}}) | ||
obs = test_df.validate(**args) | ||
assert obs is None | ||
|
||
|
||
# include args for deprecated legacy signature | ||
@pytest.mark.parametrize( | ||
"args", | ||
( | ||
dict(variable="Primary Energy", year=2010, upper_bound=6), | ||
dict(criteria={"Primary Energy": {"up": 6, "year": 2010}}), | ||
), | ||
) | ||
def test_validate_year_201ß(test_df, args): | ||
# checking that the return-type is correct | ||
obs = test_df.validate({"Primary Energy": {"up": 6, "year": 2010}}) | ||
obs = test_df.validate(**args) | ||
pdt.assert_frame_equal(obs, test_df.data[5:6].reset_index(drop=True)) | ||
assert list(test_df.exclude) == [False, False] | ||
|
||
# checking exclude on fail | ||
obs = test_df.validate( | ||
{"Primary Energy": {"up": 6, "year": 2010}}, exclude_on_fail=True | ||
) | ||
obs = test_df.validate(**args, exclude_on_fail=True) | ||
pdt.assert_frame_equal(obs, test_df.data[5:6].reset_index(drop=True)) | ||
assert list(test_df.exclude) == [False, True] | ||
|
||
|
||
def test_validate_multiple_criteria(test_df): | ||
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. You could assert here in addition that the deprecation warning is issued. |
||
# test that validating with multiple criteria works as expected (deprecated feature) | ||
criteria = { | ||
"Primary Energy": {"lo": 7, "year": 2010}, | ||
"Primary Energy|Coal": {"lo": 3}, | ||
} | ||
exp = test_df.data[1:3].reset_index(drop=True) | ||
|
||
obs = test_df.validate(criteria=criteria) | ||
pdt.assert_frame_equal(obs, exp) | ||
assert list(test_df.exclude) == [False, False] | ||
|
||
# checking exclude on fail | ||
obs = test_df.validate(criteria=criteria, exclude_on_fail=True) | ||
pdt.assert_frame_equal(obs, exp) | ||
assert list(test_df.exclude) == [True, False] | ||
|
||
|
||
def test_validate_top_level(test_df): | ||
obs = validate( | ||
test_df, | ||
|
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.
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.
Seems that Codecov and I came to the same conclusion here :D