Skip to content

CircularRegionOfInterest.pixels_in_region omits boundary pixels that its own is_inside accepts #1889

Open

Description

@Arthur031221

Describe the bug

CircularRegionOfInterest.is_inside() accepts pixels that pixels_in_region()
never returns, so the region is not symmetric about its own centre. It looks
like a regression from b43026d rather than a deliberate choice, and the
example in examples/plot_imageseq.py is affected.

is_inside() tests a closed disc (<=), so a pixel exactly radius away is
inside. pixels_in_region() scans range(int(floor(c - r)), int(ceil(c + r))),
and range stops one short of its endpoint, so pixels at the +x and +y
extremes are never passed to is_inside() at all. They aren't rejected, they
are never asked about.

This only bites when a pixel centre lands at exactly distance radius, which
for a whole-number centre means a whole-number radius. radius=2.5 and
radius=1.01 are unaffected. Whole numbers are the common case though, and
both ROIs in examples/plot_imageseq.py use them.

To Reproduce

import numpy as np
import quantities as pq
from neo.core import ImageSequence
from neo.core.regionofinterest import CircularRegionOfInterest

seq = ImageSequence(np.zeros((2, 120, 120)), sampling_rate=1 * pq.Hz,
                    spatial_scale=1 * pq.micrometer, units="V")

roi = CircularRegionOfInterest(seq, 10, 10, 5)      # from examples/plot_imageseq.py
pixels = roi.pixels_in_region()

print(roi.is_inside(15, 10), [15, 10] in pixels)    # True False
print(roi.is_inside(5, 10), [5, 10] in pixels)      # True True

xs = [p[0] for p in pixels]
print(min(xs), max(xs))                             # 5 14, for a circle centred on 10

accepted = sum(1 for y in range(120) for x in range(120) if roi.is_inside(x, y))
print(len(pixels), accepted)                        # 79 81

(15, 10) sits exactly 5 pixels from the centre. is_inside() says it is in the
region. It is never enumerated. The same holds for (10, 15). The mirror pixels
(5, 10) and (10, 5) are both returned, so the region reaches 5 pixels to the
left of the centre and 4 to the right.

The other ROI in the same example, CircularRegionOfInterest(seq, 50, 50, 25),
returns 1959 pixels where is_inside() accepts 1961.

Expected behaviour

pixels_in_region() should return every pixel is_inside() accepts. For
radius=5 centred on 10 that is 81 pixels spanning x 5 to 15, symmetric about
the centre.

RectangularRegionOfInterest already behaves this way. Its is_inside() uses a
half-open interval, which matches what its own range() bounds enumerate, and
sweeping 1056 centre/width/height combinations turns up no disagreement between
its predicate and its enumeration.

Environment:

  • OS: Linux 6.17, Ubuntu
  • Python version: 3.12.3
  • Neo version: 0.15.dev0 at 35cbce7, and 0.14.5 (nothing has touched
    regionofinterest.py since that tag, so a pip install reproduces it)
  • NumPy version: 2.5.1

Additional context

The history suggests this was collateral damage rather than a decision, and
there is one thing only you can tell me.

Before b43026d the loop read:

for y in range(self.y - self.radius, self.y + self.radius + 1):

Inclusive, matching the <= that is_inside() has carried since the file was
added. b43026d ("Make RegionOfInterest work with float arguments",
2019-09-26 10:55) rewrote the bounds with floor/ceil and the +1 went with
it. Twenty-nine minutes later 52977d3 ("fix tests; pep8") changed the
radius-1 assertion in test_regionofinterest.py to the three-pixel list and
added a radius-1.01 case asserting the five-pixel plus shape.

So: was that assertion change a deliberate call that a pixel exactly on the
circle is outside it, or was it getting the suite green after the bounds
changed? If it was deliberate then is_inside() is what needs fixing, not the
enumeration, and the fix is the opposite of what I would have written.

PolygonRegionOfInterest has the same bounding box under-scan. A square with
vertices at (10,10), (20,10), (20,20), (10,20) enumerates x 10 to 19 and y 10
to 19. I haven't investigated whether that one matters, since polygon has no
is_inside() to disagree with.

I have a fix and regression tests ready, using
range(int(ceil(c - r)), int(floor(c + r)) + 1) so the scan covers the closed
interval exactly rather than over-scanning past it. Across 44 centre and radius
combinations, including half-integer centres and fractional radii, it returns
exactly the set is_inside() accepts, with nothing missing and nothing extra.

Against 35cbce7, with those two lines changed, the radius-1 assertion restored
to the five-pixel list and two regression tests added to
Test_CircularRegionOfInterest, neo/test/coretest runs 621 passed, 12
skipped, 0 failed, up from 619 passed, 12 skipped, 0 failed. Reverting the two
lines and leaving everything else gives 3 failures. Nothing else in the suite
moves either way.

Happy to open a PR, once you tell me which side of it you want changed.

Activity

  1. Arthur031221 commented on Aug 13, 2026

    @Arthur031221
    Author

    A correction to this issue, since #1895 now fixes it and someone reviewing that PR is likely to read this first.

    The bounds I recommended above, range(int(ceil(c - r)), int(floor(c + r)) + 1), are wrong in a small number of cases and should not be used. #1895 uses floor on the lower bound and ceil plus one on the upper, which is the correct shape.

    I checked both forms against is_inside over a tenths grid of 40,000 centre and radius combinations. The padded form in #1895 agreed with is_inside everywhere. The tighter form I proposed drops a boundary pixel in about 0.2 percent of cases, through float rounding rather than through the geometry. Centre 1.8 and radius 3.8 is one of them: 1.8 - 3.8 evaluates to -1.9999999999999998, ceil turns that into -1, and x = -2 is never tested even though is_inside accepts it. The author of #1895 ran an independent sweep and found the same direction. Padding costs a band of rejected candidates around the perimeter, up to 44 extra visits at radius 5 with a half integer centre, which is not worth optimising away at these sizes.

    On the question I asked at the end of the report, about whether the radius 1 assertion in test_regionofinterest.py was a deliberate decision or test cleanup: no maintainer needs to answer it for #1895 to be reviewable. The suite contradicts itself either way. Line 11 expects 3 pixels at radius 1 and line 13 expects 5 at radius 1.01, and under the closed disc that is_inside has always tested those two radii enclose exactly the same pixel centres, so the two lines cannot both be right. #1895 corrects the radius 1 line, which is the reading that matches is_inside as written. If the team would rather have an open disc, then is_inside is what changes and the test comes back the other way, but that is a behaviour change and a separate decision from this bug.

    Nothing else here needs updating. #1895 is green and I have no changes to ask of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions