From 3b224e83f4eefa638c5f757ab3734a714459df76 Mon Sep 17 00:00:00 2001 From: Dimitri RODARIE Date: Wed, 8 Mar 2023 13:17:12 +0100 Subject: [PATCH] Restrict ISH slice positions that are inside the volume_mask volume. (#25) * Use copy of slices instead of ref, fix lint. Fixes #24 --- atlas_densities/densities/fitting.py | 7 +++++-- tests/densities/test_fitting.py | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/atlas_densities/densities/fitting.py b/atlas_densities/densities/fitting.py index af117c5..775bdc4 100644 --- a/atlas_densities/densities/fitting.py +++ b/atlas_densities/densities/fitting.py @@ -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. @@ -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): diff --git a/tests/densities/test_fitting.py b/tests/densities/test_fitting.py index 4338969..8d1e8b5 100644 --- a/tests/densities/test_fitting.py +++ b/tests/densities/test_fitting.py @@ -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):