-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add load_dataset and save_dataset functions #392
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Any | ||
|
||
import xarray as xr | ||
from xarray import Dataset | ||
|
||
from sgkit.typing import PathType | ||
|
||
|
||
def save_dataset(ds: Dataset, path: PathType, **kwargs: Any) -> None: | ||
"""Save a dataset to Zarr storage. | ||
|
||
This function is a thin wrapper around :meth:`xarray.Dataset.to_zarr` | ||
that uses sensible defaults and makes it easier to use in a pipeline. | ||
|
||
Parameters | ||
---------- | ||
ds | ||
Dataset to save. | ||
path | ||
Path to directory in file system to save to. | ||
kwargs | ||
Additional arguments to pass to :meth:`xarray.Dataset.to_zarr`. | ||
""" | ||
store = str(path) | ||
for v in ds: | ||
# Workaround for https://github.com/pydata/xarray/issues/4380 | ||
ds[v].encoding.pop("chunks", None) | ||
ds.to_zarr(store, **kwargs) | ||
|
||
|
||
def load_dataset(path: PathType) -> Dataset: | ||
"""Load a dataset from Zarr storage. | ||
|
||
This function is a thin wrapper around :meth:`xarray.open_zarr` | ||
that uses sensible defaults and makes it easier to use in a pipeline. | ||
|
||
Parameters | ||
---------- | ||
path | ||
Path to directory in file system to load from. | ||
|
||
Returns | ||
------- | ||
Dataset | ||
The dataset loaded from the file system. | ||
""" | ||
store = str(path) | ||
ds: Dataset = xr.open_zarr(store, concat_characters=False) # type: ignore[no-untyped-call] | ||
for v in ds: | ||
# Workaround for https://github.com/pydata/xarray/issues/4386 | ||
if v.endswith("_mask"): # type: ignore | ||
ds[v] = ds[v].astype(bool) # type: ignore[no-untyped-call] | ||
return ds |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import xarray as xr | ||
from xarray import Dataset | ||
|
||
from sgkit import load_dataset, save_dataset | ||
from sgkit.testing import simulate_genotype_call_dataset | ||
|
||
|
||
def assert_identical(ds1: Dataset, ds2: Dataset) -> None: | ||
"""Assert two Datasets are identical, including dtypes for all variables.""" | ||
xr.testing.assert_identical(ds1, ds2) # type: ignore[no-untyped-call] | ||
assert all([ds1[v].dtype == ds2[v].dtype for v in ds1.data_vars]) | ||
|
||
|
||
def test_save_and_load_dataset(tmp_path): | ||
path = str(tmp_path / "ds.zarr") | ||
ds = simulate_genotype_call_dataset(n_variant=10, n_sample=10) | ||
save_dataset(ds, path) | ||
ds2 = load_dataset(path) | ||
assert_identical(ds, ds2) | ||
|
||
# save and load again to test https://github.com/pydata/xarray/issues/4386 | ||
path2 = str(tmp_path / "ds2.zarr") | ||
save_dataset(ds2, path2) | ||
assert_identical(ds, load_dataset(path2)) |
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.
Worth mentioning pydata/xarray#4380 in a comment?