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

Fix dataset pickling #7

Merged
merged 2 commits into from
Apr 2, 2024
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
9 changes: 6 additions & 3 deletions src/sknnr_spatial/datasets/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ def _load_rasters_to_dataset(
"""Load a list of rasters from the data module as an xarray Dataset."""
das = []
for file_name, var_name in zip(file_names, var_names):
with resources.files(module_name).joinpath(file_name).open("rb") as bin:
da = rioxarray.open_rasterio(bin, chunks=chunks)
path = resources.files(module_name).joinpath(file_name)
da = (
rioxarray.open_rasterio(path, chunks=chunks)
.to_dataset(dim="band")
.rename({1: var_name})
)

da = da.to_dataset(dim="band").rename({1: var_name})
das.append(da)

return xr.merge(das)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_datasets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pickle
from dataclasses import dataclass

import pytest
Expand Down Expand Up @@ -36,6 +37,10 @@ def test_load_dataset(configuration: DatasetConfiguration, as_dataset: bool):
assert y.shape == (configuration.n_samples, configuration.n_targets)

if as_dataset:
# Some Dask schedulers require pickling, so ensure that the loaded dataset is
# pickleable during compute. We could try computing directly, but that is much
# slower.
assert pickle.dumps(X_image)
assert list(X.columns) == list(X_image.data_vars)
assert X_image.sizes == {
"y": configuration.image_size[0],
Expand Down
Loading