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

Add load_dataset and save_dataset functions #392

Merged
merged 2 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions sgkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pkg_resources import DistributionNotFound, get_distribution

from .display import display_genotypes
from .io.dataset import load_dataset, save_dataset
from .io.vcfzarr_reader import read_vcfzarr
from .model import (
DIM_ALLELE,
Expand Down Expand Up @@ -59,4 +60,6 @@
"variables",
"pca",
"window",
"load_dataset",
"save_dataset",
]
53 changes: 53 additions & 0 deletions sgkit/io/dataset.py
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)
Copy link
Collaborator

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?

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
24 changes: 24 additions & 0 deletions sgkit/tests/io/test_dataset.py
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))