Skip to content

Commit

Permalink
correctly handle nan bg_value
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Aug 7, 2024
1 parent f986621 commit d5e7913
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/depiction/image/image_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def _normalize_single_xarray(self, image: xarray.DataArray, variant: ImageNormal
with xarray.set_options(keep_attrs=True):
if variant == ImageNormalizationVariant.VEC_NORM:
norm = ((image**2).sum(["c"])) ** 0.5
return xarray.where(norm != 0, image / norm, 0)
return xarray.where(norm != 0, image / norm, image.attrs.get("bg_value", 0))
elif variant == ImageNormalizationVariant.STD:
std = image.std("c")
return xarray.where(std != 0, (image - image.mean("c")) / std, 0)
return xarray.where(std != 0, (image - image.mean("c")) / std, image.attrs.get("bg_value", 0))
else:
raise NotImplementedError(f"Unknown variant: {variant}")

Expand Down
5 changes: 4 additions & 1 deletion src/depiction/tools/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def clustering(
full_features=image_features.data_flat.values.T,
)
label_image = MultiChannelImage.from_sparse(
values=full_labels[:, np.newaxis], coordinates=image_full_features.coordinates_flat, channel_names=["cluster"]
values=full_labels[:, np.newaxis],
coordinates=image_full_features.coordinates_flat,
channel_names=["cluster"],
bg_value=np.nan,
)

# write the result of the operation
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/image/test_image_normalization.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest
import xarray as xr

Expand Down Expand Up @@ -33,6 +34,22 @@ def test_normalize_xarray_single_vec_norm(image_normalizer, single_image):
assert norm_vec.attrs["bg_value"] == single_image.attrs["bg_value"]


def test_normalize_xarray_single_vec_norm_with_nans(image_normalizer):
image_with_nans = xr.DataArray(
data=[[[2, np.nan], [0, 2]], [[1, 1], [4, np.nan]], [[np.nan, 0], [0, 0]]],
dims=["y", "x", "c"],
attrs={"bg_value": np.nan},
)
norm_vec = image_normalizer.normalize_xarray(image_with_nans, variant=ImageNormalizationVariant.VEC_NORM)
expected = xr.DataArray(
data=[[[1, np.nan], [0, 1]], [[0.707107, 0.707107], [1, np.nan]], [[np.nan, np.nan], [np.nan, np.nan]]],
dims=["y", "x", "c"],
attrs={"bg_value": np.nan},
)
xr.testing.assert_allclose(expected, norm_vec)
assert np.isnan(norm_vec.attrs["bg_value"])


def test_normalize_xarray_single_std(image_normalizer, single_image):
norm_std = image_normalizer.normalize_xarray(single_image, variant=ImageNormalizationVariant.STD)
expected = xr.DataArray(
Expand Down

0 comments on commit d5e7913

Please sign in to comment.