Skip to content

Commit

Permalink
Restrict ISH slice positions that are inside the volume_mask volume. (#…
Browse files Browse the repository at this point in the history
…25)

* Use copy of slices instead of ref, fix lint.
Fixes #24
  • Loading branch information
drodarie authored Mar 8, 2023
1 parent 9bfda2b commit 3b224e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions atlas_densities/densities/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ def compute_average_intensity(
Compute the average of `intensity` within the volume defined by `volume_mask`.
If `slices` is a non-empty list of slice indices along the x-axis, then the average is
restricted to the subvolume of `volume_mask` enclosed by these slices.
restricted to the subvolume of `volume_mask` enclosed by these slices. Slices indices outside
the `volume_mask` will be ignored.
Args:
intensity: a float array of shape (W, H, D) where W, H and D are integer dimensions.
Expand All @@ -196,7 +197,9 @@ def compute_average_intensity(
restricted_mask = volume_mask
else:
restricted_mask = np.zeros_like(volume_mask)
restricted_mask[slices] = True
# remove slices indices outside the volume_mask
slices_ = [slice_ for slice_ in slices if 0 <= slice_ < volume_mask.shape[0]]
restricted_mask[slices_] = True
restricted_mask = np.logical_and(restricted_mask, volume_mask)

if np.any(restricted_mask):
Expand Down
4 changes: 4 additions & 0 deletions tests/densities/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ def test_compute_average_intensity():
assert np.allclose(actual, 0.3 / 2.0)
actual = tested.compute_average_intensity(intensity, volume_mask, slices=[1])
assert np.allclose(actual, 0.1 / 2.0)
actual = tested.compute_average_intensity(intensity, volume_mask, slices=[-1, 1, 3])
assert np.allclose(actual, 0.1 / 2.0)
actual = tested.compute_average_intensity(intensity, volume_mask, slices=[-1, 3])
assert actual == 0


def test_compute_average_intensities(hierarchy_info):
Expand Down

0 comments on commit 3b224e8

Please sign in to comment.