Repository: BlueBrain/atlas-densities

Issue:
Check slices position during fitting
For the fitting of the densities, the user can provide a list of coronal positions that corresponds to the original ISH slices in a separate file.
There is no tests in atlas_densities/densities/fitting.py to check if these positions are in range of the annotation volume shape.

Suggestion: add at line 199:
```
slices = np.delete(slices, np.array(slices) >= volume_mask.shape[0])
slices = np.delete(slices, np.array(slices) < 0)
```

Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/BlueBrain/atlas-densities/pull/25?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BlueBrain) Report
> :exclamation: No coverage uploaded for pull request base (`main@9bfda2b`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BlueBrain#section-missing-base-commit).
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##             main      #25   +/-   ##
=======================================
  Coverage        ?   97.74%           
=======================================
  Files           ?       21           
  Lines           ?     1375           
  Branches        ?        0           
=======================================
  Hits            ?     1344           
  Misses          ?       31           
  Partials        ?        0           
```

| Flag | Coverage Δ | |
|---|---|---|
| pytest | `97.74% <0.00%> (?)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BlueBrain#carryforward-flags-in-the-pull-request-comment) to find out more.


Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BlueBrain). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BlueBrain)


Patch:
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):


Test patch:
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):