diff --git a/src/depiction/image/multi_channel_image.py b/src/depiction/image/multi_channel_image.py index 1d3f844..4dbe135 100644 --- a/src/depiction/image/multi_channel_image.py +++ b/src/depiction/image/multi_channel_image.py @@ -43,6 +43,12 @@ def n_channels(self) -> int: """Number of channels.""" return self._data.sizes["c"] + @property + def n_nonzero(self) -> int: + """Number of non-zero values.""" + # TODO efficient impl + return (~self.bg_mask).sum().item() + # TODO sparse_values, sparse_coordinates - these are currently widely used which i guess is a problem @property diff --git a/tests/unit/image/test_multi_channel_image.py b/tests/unit/image/test_multi_channel_image.py index d064a10..516c12e 100644 --- a/tests/unit/image/test_multi_channel_image.py +++ b/tests/unit/image/test_multi_channel_image.py @@ -44,6 +44,15 @@ def test_n_channels(mock_image: MultiChannelImage) -> None: assert mock_image.n_channels == 1 +def test_n_nonzero(mock_image: MultiChannelImage) -> None: + assert mock_image.n_nonzero == 6 + + +def test_n_nonzero_when_sparse(mock_image: MultiChannelImage) -> None: + mock_image.data_spatial[1, 0, 0] = 0 + assert mock_image.n_nonzero == 5 + + def test_dtype(mock_image: MultiChannelImage) -> None: assert mock_image.dtype == float