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

fixing behaviour for group parameter in open_datatree #9666

Merged
merged 26 commits into from
Oct 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3429b2c
adding draft for fixing behaviour for group parameter
aladinor Oct 23, 2024
1507f4d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 23, 2024
e24e88b
new trial
aladinor Oct 23, 2024
34e74db
new trial
aladinor Oct 23, 2024
b6fac5b
new trial
aladinor Oct 23, 2024
ce83c89
fixing duplicate pahts and path in the root group
aladinor Oct 24, 2024
72fcee6
removing yield str(gpath)
aladinor Oct 24, 2024
bd853c8
implementing the proposed solution to hdf5 and netcdf backends
aladinor Oct 24, 2024
d6e5422
adding changes to whats-new.rst
aladinor Oct 24, 2024
12005e2
removing encoding['source_group'] line to avoid conflicts with PR #9660
aladinor Oct 24, 2024
e4384d6
adding test
aladinor Oct 24, 2024
0fab3c7
Merge branch 'main' into fix-group-param
TomNicholas Oct 24, 2024
e935e4e
adding test
aladinor Oct 24, 2024
2803f9f
Merge branch 'fix-group-param' of https://github.com/aladinor/xarray …
aladinor Oct 24, 2024
9a41b68
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2024
f5d3073
adding assert subgroup_tree.root.parent is None
aladinor Oct 24, 2024
a473778
Merge branch 'fix-group-param' of https://github.com/aladinor/xarray …
aladinor Oct 24, 2024
bb6d413
modifying tests
aladinor Oct 24, 2024
fcf3dc6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2024
195e036
Update xarray/tests/test_backends_datatree.py
aladinor Oct 24, 2024
5ef3a56
applying suggested changes
aladinor Oct 24, 2024
90c5b4d
updating test
aladinor Oct 24, 2024
38548b0
Merge branch 'main' into fix-group-param
TomNicholas Oct 24, 2024
e78d576
adding Justus and Alfonso to the list of contributors to the DataTree…
aladinor Oct 24, 2024
762587b
adding Justus and Alfonso to the list of contributors to the DataTree…
aladinor Oct 24, 2024
0cd22c5
Merge branch 'main' into fix-group-param
TomNicholas Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions xarray/tests/test_backends_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,38 @@ def test_open_groups_to_dict(self, tmpdir) -> None:
for ds in aligned_dict_of_datasets.values():
ds.close()

def test_open_datatree_specific_group(self, tmpdir, simple_datatree) -> None:
"""Test opening a specific group within a NetCDF file using `open_datatree`."""
# Open the specific group '/Group1/subgroup1' and check if it loads correctly
filepath = tmpdir / "test.nc"
group = "/set1"
original_dt = simple_datatree
original_dt.to_netcdf(filepath)

with open_datatree(filepath, group=group, engine=self.engine) as subgroup_tree:
# Check that the subtree is not None
assert subgroup_tree is not None
# Check the expected number of children within node
assert list(subgroup_tree.children) == list(original_dt[group].children)
# Check the dimensions of the group '/set1'
assert len(subgroup_tree.dataset.dims) == len(original_dt[group].dataset.dims)
# Check if the variables in this "set1" group are correctly read
assert list(subgroup_tree.dataset.data_vars) == list(original_dt[group].dataset.data_vars)
# Check the values of the variables 'a' and 'b'
assert subgroup_tree.dataset["a"].values == original_dt[group].dataset["a"].values
assert subgroup_tree.dataset["b"].values == original_dt[group].dataset["b"].values

def test_open_datatree_nonexistent_group(self, tmpdir, simple_datatree) -> None:
"""Test `open_datatree` behavior when attempting to open a non-existent group."""
filepath = tmpdir / "test.nc"
original_dt = simple_datatree
original_dt.to_netcdf(filepath, engine=self.engine)

# Attempt to open a non-existent group, which should raise a ValueError
with pytest.raises(OSError, match="Group '/nonexistent_group' not found"):
open_datatree(filepath, group="/nonexistent_group", engine=self.engine)



@requires_h5netcdf
class TestH5NetCDFDatatreeIO(DatatreeIOBase):
Expand Down Expand Up @@ -382,3 +414,23 @@ def test_open_groups(self, unaligned_datatree_zarr) -> None:

for ds in unaligned_dict_of_datasets.values():
ds.close()

def test_open_datatree_specific_group(self, tmpdir, simple_datatree) -> None:
"""Test opening a specific group within a Zarr store using `open_datatree`."""
filepath = tmpdir / "test.zarr"
group = "/set2"
original_dt = simple_datatree
original_dt.to_zarr(filepath)

with open_datatree(filepath, group=group, engine=self.engine) as subgroup_tree:
# Check that the subtree is not None
assert subgroup_tree is not None
aladinor marked this conversation as resolved.
Show resolved Hide resolved
# Check the expected number of children within node
assert list(subgroup_tree.children) == list(original_dt[group].children)
# Check the dimensions of the group '/set2'
assert len(subgroup_tree.dataset.dims) == len(original_dt[group].dataset.dims)
# Check if the variables in this "set2" group are correctly read
assert list(subgroup_tree.dataset.data_vars) == list(original_dt[group].dataset.data_vars)
# Check the values of the variables 'a' and 'b'
assert_equal(subgroup_tree.dataset["a"], original_dt[group].dataset["a"])
assert_equal(subgroup_tree.dataset["b"], original_dt[group].dataset["b"])