-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add hypothesis test for netCDF4 roundtrip #3283
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
Open
takluyver
wants to merge
9
commits into
pydata:main
Choose a base branch
from
takluyver:hypothesis-netcdf-roundtrip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
57756ac
Add hypothesis test for roundtrip to netCDF4
takluyver 50d242e
Note how to run hypothesis tests
takluyver 3230c2f
Reformat code with black
takluyver 83bbb73
Remove redundant hypothesis config
takluyver 61bcc7f
Clarifications around character strategy
takluyver 67eaed1
Use context manager to open & close netCDF file
takluyver 1b6a271
Add unicode & bytes dtype to netCDF roundtrip test
takluyver 4b93267
Add datetime64 & timedelta64 to netCDF roundtrip test
takluyver fbeaf41
Remove unused import
takluyver 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Property-based tests for round-tripping data to netCDF | ||
""" | ||
import hypothesis.extra.numpy as npst | ||
import hypothesis.strategies as st | ||
from hypothesis import given | ||
|
||
import xarray as xr | ||
|
||
|
||
an_array = npst.arrays( | ||
dtype=st.one_of( | ||
npst.unsigned_integer_dtypes(), | ||
npst.integer_dtypes(), | ||
# NetCDF does not support float16 | ||
# https://www.unidata.ucar.edu/software/netcdf/docs/data_type.html | ||
npst.floating_dtypes(sizes=(32, 64)), | ||
npst.byte_string_dtypes(), | ||
npst.unicode_string_dtypes(), | ||
npst.datetime64_dtypes(), | ||
npst.timedelta64_dtypes(), | ||
), | ||
shape=npst.array_shapes(max_side=3), # max_side specified for performance | ||
) | ||
|
||
compatible_names = st.text( | ||
alphabet=st.characters( | ||
# Limit characters to upper & lowercase letters and decimal digits | ||
whitelist_categories=("Ll", "Lu", "Nd"), | ||
# It looks like netCDF should allow unicode names, but removing | ||
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. Where is this specified? It would be useful to provide a link and prehaps explain what's going on with the categories too 😄 |
||
# this causes a failure with 'ά' | ||
# https://www.unidata.ucar.edu/software/netcdf/docs/netcdf_data_set_components.html#Permitted | ||
max_codepoint=255, | ||
), | ||
min_size=1, | ||
) | ||
|
||
|
||
@given(st.data(), an_array) | ||
def test_netcdf_roundtrip(tmp_path, data, arr): | ||
names = data.draw( | ||
st.lists( | ||
compatible_names, min_size=arr.ndim, max_size=arr.ndim, unique=True | ||
).map(tuple) | ||
) | ||
var = xr.Variable(names, arr) | ||
original = xr.Dataset({"data": var}) | ||
original.to_netcdf(tmp_path / "test.nc") | ||
|
||
with xr.open_dataset(tmp_path / "test.nc") as roundtripped: | ||
xr.testing.assert_identical(original, roundtripped) |
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.