Skip to content

Commit 9a9e8c0

Browse files
Make Views picklable (#469)
* make Views picklable * update changelog * Update webknossos/tests/test_dataset.py Co-authored-by: Jonathan Striebel <[email protected]> * Update webknossos/tests/test_dataset.py Co-authored-by: Jonathan Striebel <[email protected]> Co-authored-by: Jonathan Striebel <[email protected]>
1 parent cbd1bed commit 9a9e8c0

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

webknossos/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ For upgrade instructions, please check the respective *Breaking Changes* section
2020
### Changed
2121
- `View`s now always open the `wkw.Dataset` lazily. All explicit calls to `View.open()` and `View.close()` must be removed. [#448](https://github.com/scalableminds/webknossos-libs/pull/448)
2222
### Fixed
23+
- Make Views picklable. We now ignore the file handle when we pickle Views. [#469](https://github.com/scalableminds/webknossos-libs/pull/469)
2324

2425

2526
## [0.8.18](https://github.com/scalableminds/webknossos-cuber/releases/tag/v0.8.18) - 2021-10-18

webknossos/tests/test_dataset.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import itertools
22
import json
33
import os
4+
import pickle
45
import warnings
56
from os.path import join
67
from pathlib import Path
@@ -1957,3 +1958,24 @@ def test_add_layer_like(tmp_path: Path) -> None:
19571958
)
19581959

19591960
assure_exported_properties(ds)
1961+
1962+
1963+
def test_pickle_view(tmp_path: Path) -> None:
1964+
ds = Dataset.create(tmp_path / "ds", scale=(1, 1, 1))
1965+
mag1 = ds.add_layer("color", LayerCategories.COLOR_TYPE).add_mag(1)
1966+
1967+
assert mag1._cached_wkw_dataset is None
1968+
data_to_write = (np.random.rand(1, 10, 10, 10) * 255).astype(np.uint8)
1969+
mag1.write(data_to_write)
1970+
assert mag1._cached_wkw_dataset is not None
1971+
1972+
pickle.dump(mag1, open(str(tmp_path / "save.p"), "wb"))
1973+
pickled_mag1 = pickle.load(open(str(tmp_path / "save.p"), "rb"))
1974+
1975+
# Make sure that the pickled mag can still read data
1976+
assert pickled_mag1._cached_wkw_dataset is None
1977+
assert np.array_equal(data_to_write, pickled_mag1.read())
1978+
assert pickled_mag1._cached_wkw_dataset is not None
1979+
1980+
# Make sure that the attributes of the MagView (not View) still exist
1981+
assert pickled_mag1.layer is not None

webknossos/webknossos/dataset/view.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import warnings
33
from pathlib import Path
44
from types import TracebackType
5-
from typing import TYPE_CHECKING, Callable, Optional, Tuple, Type, Union
5+
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple, Type, Union
66

77
import cluster_tools
88
import numpy as np
@@ -585,6 +585,15 @@ def _wkw_dataset(self) -> None:
585585
def __del__(self) -> None:
586586
del self._cached_wkw_dataset
587587

588+
def __getstate__(self) -> Dict[str, Any]:
589+
d = dict(self.__dict__)
590+
del d["_cached_wkw_dataset"]
591+
return d
592+
593+
def __setstate__(self, d: Dict[str, Any]) -> None:
594+
d["_cached_wkw_dataset"] = None
595+
self.__dict__ = d
596+
588597

589598
def _assert_positive_dimensions(offset: Vec3Int, size: Vec3Int) -> None:
590599
if any(x < 0 for x in offset):

0 commit comments

Comments
 (0)