-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from melonora/decorator
Add decorator for skipping nodes without dimension
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
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
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 @@ | ||
from typing import Callable, Any | ||
from xarray import Dataset | ||
import functools | ||
|
||
|
||
def skip_non_dimension_nodes( | ||
func: Callable[[Dataset], Dataset], | ||
) -> Callable[[Dataset], Dataset]: | ||
"""Skip nodes in Datatree that do not contain dimensions. | ||
This function implements the workaround of https://github.com/pydata/xarray/issues/9693. In particular, | ||
we need this because of our DataTree representing multiscale image having a root node that does not have | ||
dimensions. Several functions need to be mapped over the datasets in the datatree that depend on having | ||
dimensions, e.g. a transpose. | ||
""" | ||
|
||
@functools.wraps(func) | ||
def _func(ds: Dataset, *args: Any, **kwargs: Any) -> Dataset: | ||
# check if dimensions are present otherwise return verbatim | ||
if len(ds.dims) == 0: | ||
return ds | ||
return func(ds, *args, **kwargs) | ||
|
||
return _func |
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,23 @@ | ||
import numpy as np | ||
from spatial_image import to_spatial_image | ||
from multiscale_spatial_image import skip_non_dimension_nodes, to_multiscale | ||
|
||
|
||
def test_skip_nodes(): | ||
data = np.zeros((2, 200, 200)) | ||
dims = ("c", "y", "x") | ||
scale_factors = [2, 2] | ||
image = to_spatial_image(array_like=data, dims=dims) | ||
multiscale_img = to_multiscale(image, scale_factors=scale_factors) | ||
|
||
@skip_non_dimension_nodes | ||
def transpose(ds, *args, **kwargs): | ||
return ds.transpose(*args, **kwargs) | ||
|
||
for scale in list(multiscale_img.keys()): | ||
assert multiscale_img[scale]["image"].dims == ("c", "y", "x") | ||
|
||
# applying this function without skipping the root node would fail as the root node does not have dimensions. | ||
result = multiscale_img.map_over_datasets(transpose, "y", "x", "c") | ||
for scale in list(result.keys()): | ||
assert result[scale]["image"].dims == ("y", "x", "c") |