Skip to content

Commit

Permalink
simplify get_z_scaled by use of xarray
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Aug 27, 2024
1 parent 7ce8db4 commit 60c8f02
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/depiction/image/multi_channel_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ def append_channels(self, other: MultiChannelImage) -> MultiChannelImage:
return MultiChannelImage(data=data)

def get_z_scaled(self) -> MultiChannelImage:
means = xarray.DataArray(self.channel_stats.mean["mean"], dims="c", coords={"c": self.channel_names})
stds = xarray.DataArray(self.channel_stats.std["std"], dims="c", coords={"c": self.channel_names})
"""Returns a copy of self with each feature z-scaled."""
eps = 1e-12
with xarray.set_options(keep_attrs=True):
return MultiChannelImage(data=(self._data - means + eps) / (stds + eps))
return MultiChannelImage(data=(self._data - self.channel_stats.mean + eps) / (self.channel_stats.std + eps))

# TODO reconsider:there is actually a problem, whether it should use bg_mask only or also replace individual values
# since both could be necessary it should be implemented in a sane and maintainable manner
Expand Down
1 change: 0 additions & 1 deletion tests/unit/image/test_multi_channel_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def test_append_channels(mock_image: MultiChannelImage) -> None:

def test_get_z_scaled(mock_image: MultiChannelImage) -> None:
result = mock_image.get_z_scaled()
# TODO I am not fully sure this is correct yet
np.testing.assert_almost_equal(
np.array([[-1.46385011, -0.87831007], [-0.29277002, 0.29277002], [0.87831007, 1.46385011]]),
result.data_spatial[:, :, 0].values,
Expand Down

0 comments on commit 60c8f02

Please sign in to comment.