harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0470harbor download openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0470MEDIUM DIFFICULTY TASK
Goal
Create a lightweight, self-contained Shapely-like API suitable for deterministic, test-driven use in Voronoi tests. Implement a single-file solution at /app/shapely.py that exposes a minimal surface similar to Shapely without depending on external geometry libraries. The tests will interact with a package-like API via simple namespaces exposed from this file.
What you must build (single-file)
- A single Python module at /app/shapely.py that provides a package-like API using lightweight, in-memory objects. Do not rely on real GEOS computations. The tests will access the API via sub-namespaces that behave like modules:
- shapely.geos.geos_version -> function that returns a tuple (major, minor, patch), e.g., (3, 5, 0).
- shapely.geometry.MultiPoint -> constructor MultiPoint(points=None). The tests instantiate with MultiPoint(points=[(x1,y1), (x2,y2), ...]). The MultiPoint instance should expose a .points attribute containing a list of (x, y) tuples.
- shapely.wkt.loads -> function that parses simple WKT strings and returns simple geometry objects from shapely.geometry (Polygon or MultiPoint).
- shapely.ops.voronoi_diagram -> function signature:
voronoi_diagram(geom, envelope=None, tolerance=None, edges=False)
- geom: either a MultiPoint instance or a Polygon instance (as produced by shapely.wkt.loads).
- envelope: optional Polygon instance (produced by shapely.wkt.loads) to constrain/enlarge regions.
- tolerance: optional float. The behavior must match the deterministic rules described below.
- edges: optional boolean. If True and there are exactly two input points, return a single region of type 'LineString' (edge) instead of two polygonal regions.
- Return value: an object with a .geoms attribute (a list) representing the Voronoi regions. Each region must be a simple geometry-like object with:
- .type: a string such as 'Polygon' or 'LineString'
- .area: a float indicating the region’s area
- The tests will only verify:
- number of regions (len(regions.geoms))
- region .type for edge cases
- sum of region .area values in certain scenarios
- Deterministic, simplified behavior (no real Voronoi computation) per the rules below.
Rules and deterministic behavior mapping
- If input is a single point (MultiPoint with 1 point) and no envelope:
- voronoi_diagram returns 0 regions (regions.geoms is empty).
- If input is two points and envelope is not provided:
- voronoi_diagram returns 2 regions.
- If input is two points and edges=True:
- voronoi_diagram returns 1 region whose .type == 'LineString' for that region.
- If input is a polygon (Polygon) and envelope is not provided:
- Without tolerance: voronoi_diagram returns 4 regions. Regions’ .area values should be deterministic (choose fixed values, e.g., 1.0, 1.5, 2.0, 2.5).
- With tolerance >= 1.0: voronoi_diagram returns 2 regions.
- With tolerance < 1.0 (e.g., 0.6): raise ValueError with message:
Could not create Voronoi Diagram with the specified inputs. Try running again with default tolerance value.
- With floating-point coordinates (any coordinate not an integer) and tolerance provided (e.g., 0.1): raise the same ValueError.
- If input is a polygon and an envelope is provided:
- The function should return exactly 2 regions regardless of tolerance.
- Compute areas such that:
- If envelope.area > input_polygon.area, the sum of the two region areas equals envelope.area.
- If envelope.area <= input_polygon.area, the sum of the two region areas equals input_polygon.area * 2.
- If input is a MultiPoint and tolerance is provided:
- If tolerance is provided, raise ValueError with the same message as above.
- If input is a MultiPoint with no tolerance:
- If all coordinates are integers (as in tests), return 4 regions.
- If coordinates are not all integers, behavior should be defined to remain deterministic and consistent with the tests (best effort; avoid non-deterministic branching).
Implementation notes and constraints
- Location: /app/shapely.py (single-file task). Implement a lightweight, internal representation for:
- MultiPoint: .points -> list of (x, y)
- Polygon: a simple object with .type == 'Polygon' and a property to compute area when needed
- Simple region objects returned inside voronoi_diagram: .type, .area
- A container for results: an object with .geoms (list) attribute
- WKT loader (shapely.wkt.loads) must support at least:
- POLYGON ((x y, x y, ..., x y))
- MULTIPOINT (x y, x y, ..., x y)
- The loader should return appropriate lightweight geometry objects (Polygon or MultiPoint) compatible with the voronoi_diagram expectations.
- The geos_version function should return (3, 5, 0) (or higher) to satisfy tests marked for GEOS 3.5 compatibility.
- Input validation:
- MultiPoint constructor must accept points as an optional keyword argument: MultiPoint(points=[...]).
- The WKT parser must raise ValueError for unsupported strings.
- voronoi_diagram must raise ValueError with the exact message:
Could not create Voronoi Diagram with the specified inputs. Try running again with default tolerance value.
- Determinism and simplicity:
- Do not attempt real geometric computations. Use deterministic numbers for areas and fixed region counts per the rules above.
- The API surface should be stable and deterministic across runs given the same inputs.
- Hints (helpful, not exhaustive):
- Use small, explicit helper classes for Point-like, Polygon-like, and Region-like objects.
- Implement a tiny “geometry namespace” inside the single file to mimic the expected API surface (shapely.geometry.MultiPoint, etc.). The tests will access these as attributes of the shapely module (since the file is the single source of truth in this task).
- Keep parsing and math straightforward to avoid subtle floating-point pitfalls; fixed values and simple area formulas will help maintain determinism.
Edge cases to consider (moderate)
- A polygon with an envelope whose area just slightly exceeds the polygon area; ensure the two-region sum matches envelope.area.
- A polygon with non-integer vertex coordinates and a provided tolerance; must raise the exact ValueError message.
- A MultiPoint with three points and no tolerance; ensure behavior aligns with tests (4 regions) only when all coordinates are integers.
Acceptance criteria
- All tests referencing voronoi_diagram behavior described above pass with the implemented /app/shapely.py single-file module.
- The API surface (geos.geos_version, geometry.MultiPoint, wkt.loads, and ops.voronoi_diagram) behaves deterministically according to the rules.
- The solution remains self-contained with no external dependencies and avoids real Voronoi computations.
Hints for development
- Start by modeling lightweight data objects: Point-like (tuples), Polygon-like (store coordinates and compute area via shoelace if needed), and simple Region objects with type and area.
- Implement wkt.loads with minimal parsing to satisfy the two supported geometries.
- Implement voronoi_diagram with a small dispatcher that handles the different input types and configurations, returning Regions as described.
- Add clear validation and the exact error message for the tolerance/float-coordinate edge cases.
Place of solution
- The implemented module must be at /app/shapely.py and provide the described API surface via a single-file approach.