Skip to content

Commit

Permalink
add n_nonzero property
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Jul 17, 2024
1 parent 9f7e578 commit ac36268
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/depiction/image/multi_channel_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/image/test_multi_channel_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ac36268

Please sign in to comment.