Repository navigation
Conversation
ormsbee
force-pushed
the
ormsbee/remove-shapely
branch
from
November 20, 2026 04:15
f749fdf to
bb99bc8
Compare
ImageResponse was the only code that used shapely. It used it to check whether a click is strictly inside the convex hull of a region's points. That check is now is_inside_convex_hull() in capa/util.py, which only needs the standard library. The new check uses exact arithmetic, so it grades every well-formed region the same way shapely did, including clicks that are on or right next to an edge. Malformed regions are still an error, but some of the exception types are different, and a [NaN, NaN] point is now an error instead of being ignored. The regions of one <imageinput> can now have at most 1000 points between them, and more than that is an error. The new check is slower than shapely when there are a lot of points. With the limit, grading a click took at most about 17 ms when measured. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ormsbee
force-pushed
the
ormsbee/remove-shapely
branch
from
November 20, 2026 04:29
d7c1cdf to
1757ddb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
ImageResponse(<imageresponse>, "Image Mapped Input") was the only code in this repo that used shapely. For each entry in an<imageinput>'sregionsattribute, it built the convex hull of the points and checked whether the learner's click was strictly inside it.This PR replaces that with
is_inside_convex_hull()incapa/util.pyand removes shapely frompyproject.tomlanduv.lock. The new function is about 30 lines of standard-library Python: Andrew's monotone chain for the hull, then a cross-product test against each edge.The arithmetic uses
fractions.Fractioninstead of floats. GEOS, the C++ library under shapely, uses extended-precision predicates, so it gives the right answer when a click is on an edge or within rounding distance of one. Plain float arithmetic does not: for the region[[0, 0], [1.1, 5.5], [0, 9]]and a click at[1, 5], shapely says inside and a float cross product says not inside. Fractions are exact, so the new code matches shapely there (test_is_inside_convex_hull_is_exact).The regions of one
<imageinput>can have at most 1,000 points between them (ImageResponse.max_region_points). More than that is an error. See Performance below for why.numpy stays. It is a direct dependency, and
responsetypes.pystill imports it.Compatibility
Grading of well-formed regions: no change
I compared the new function against shapely 2.2.0 (GEOS 3.14.1) on 740,009 random (region, click) pairs and got 0 differences. The mix was:
These existing behaviors are kept, and now have tests:
"10"are accepted as coordinates.What does change
Exception types for malformed regions. These inputs raised before and still raise, but not always the same exception. Nothing in this repo catches shapely's exceptions.
submit_problemre-raises anything that isn't one of the capa error types, before and after.regionscontainsInfinity, or a float literal too big for a float, like1e400shapely.errors.GEOSExceptionOverflowErrorNaNas only one of x or yshapely.errors.GEOSExceptionValueError[x, y]point should beTypeErrorValueErrorValueErrorTypeErrornull, an integer too big for a floatValueError/TypeError/OverflowErrorA
[NaN, NaN]point is now an error. shapely treated it as an empty point. If it wasn't the first point, it was ignored and the rest of the region graded normally. If it was the first point, the whole region silently never matched. Now both raiseValueError. This needs a literalNaNin theregionsattribute, so I doubt any real content has it, but it is an input that used to grade and now doesn't.A click coordinate with more than 308 digits used to raise
OverflowErrorand is now graded as incorrect. The frontend can't produce this; it takes a hand-made request.More than 1,000 points in one
regionsattribute is now an error. The count is the total over all of the regions of one<imageinput>. Over the limit, grading a click on that input raisesValueError, wherever the click is, unless arectangleon the same input already matched it. Before, it was graded. I don't know of real content anywhere near this size, but I have no data on it.Performance
For a typical 4-point region, one check takes 0.016 ms, against 0.012 ms with shapely.
The new code falls behind as the number of points grows. For a single region with 1,000 points it is 4 to 8 times slower than shapely, and for 20,000 points it took 263 ms against 42 ms. That is why the total number of points in one
regionsattribute is limited to 1,000.Time to grade one click that matches nothing, which is the worst case, with 1,000 points in total:
All of these were measured on one machine.
Downstream
Shapelyin its ownpyproject.tomlwith the comment "used for image click regions in capa". Nothing in itsmasterimports shapely (checked at46c8c9b4c8), so that entry can be removed once openedx-platform picks up a release that has this change.Notes for reviewers
refactor:commit on its own, so this will ship with the nextfix:orfeat:. openedx-platform can't dropShapelyuntil then.uv.lock. Besides removing shapely,uv lock(0.13.7) rewrote the markers on theasgirefandsqlparsedependencies of the Django 5.2 entry. I didn't edit the lock by hand. The new markers select those two packages in the same cases as before, anduv sync --lockedaccepts the file.ImageResponseand did not fix, since they are separate from this change:rectangleattribute raisesTypeError: list indices must be integers or slices, not strinstead of the intended "Error in problem specification" message. The error path doesself.ielements[aid], which indexes a list with the input's id.regions="[[]]"raisesIndexErroratparsed_region[0][0].Testing
is_inside_convex_hull()intest_util.py: inside, outside, on an edge, on a vertex, point order, redundant points, fractional and negative coordinates, regions with no area, the exactness case above, and malformed points.ImageResponseTestfor borders, point order, fractional coordinates, regions with no area, and the 1,000-point limit (for one region, and across many regions). I ran all but the limit tests against the shapely implementation before switching, and they passed there too.toxpasses forquality,docs,py312-django42, andpy312-django52(899 tests) from the new lock file. I confirmed thatimport shapelyfails in those environments.Manual testing (not yet done)
I have not run this in an LMS. To check it by hand:
<imageresponse><imageinput src="..." width="400" height="400" regions="[[100,100], [300,100], [300,300], [100,300]]"/></imageresponse>.Merge checklist:
Check off if complete or not applicable:
🤖 Generated with Claude Code