nebius/SWE-bench-extra huggingface dataset:
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)
```

Comments:

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