Skip to content

Regression test: make_valid() preprocessing protects precomputed table integrity #6

Closed

Description

@Polichinel

Context

Three _load_* methods in mapping.py call make_valid() on shapefile geometries at load time:

  • _load_and_preprocess_naturalearth (line ~450)
  • _load_priogrid (line ~498)
  • _load_admin_data (line ~1115)

This was added to fix C-12 (silent wrong-country assignment when geometry intersection fails) and D-04 (geometry correction strategy). make_valid() is the standard shapely operation that fixes self-intersecting polygons, degenerate boundaries, and other topological errors.

No test verifies that make_valid() is called or that it actually corrects invalid geometries. If someone removes the three make_valid() lines during refactoring, no test fails. The mapper loads invalid geometries, intersection failures are caught by the except Exception handlers and logged at DEBUG, and cells are silently misassigned.

This was identified by the falsification campaign (Claims 3.1 and 3.3, both flagged this gap).

Why now

ADR-011 will use the current mapper to precompute a ~65K-row lookup table. That precomputation runs the mapper against all PRIO-GRID cells with the current shapefiles. If make_valid() is absent during precomputation, the resulting lookup table contains wrong assignments for cells near invalid geometries — and those wrong assignments are permanent (baked into a static Parquet file).

The regression test ensures make_valid() is present when the precomputation runs.

Requirements

Test: test_make_valid_fixes_invalid_country_geometry

Setup:
Create a synthetic country GeoDataFrame with one country that has a deliberately self-intersecting polygon (bowtie shape):

from shapely.geometry import Polygon
bowtie = Polygon([(0, 0), (1, 1), (1, 0), (0, 1)])  # self-intersecting
assert not bowtie.is_valid  # precondition: geometry IS invalid before loading

Write this to a temporary shapefile and load it through the mapper's _load_and_preprocess_naturalearth method.

Assert:

  1. After loading, countries_gdf["geometry"].is_valid.all() is True
  2. The loaded geometry type is MultiPolygon (make_valid splits a bowtie into two triangles)
  3. The geometry area is > 0 (the fix didn't collapse it)

Test: test_make_valid_fixes_invalid_priogrid_geometry

Same pattern for PRIO-GRID data: create a cell with an invalid geometry, load through _load_priogrid, assert it's valid after loading.

Test: test_make_valid_fixes_invalid_admin_geometry

Same pattern for admin data: create an admin region with an invalid geometry, load through _load_admin_data, assert it's valid after loading.

Test: test_removing_make_valid_produces_invalid_geometries

This is the regression guard. Use the same invalid geometry from above but call gpd.read_file() directly (bypassing the mapper's loading), and assert the geometry is INVALID. This proves that the invalid geometry fixture is genuinely invalid and that make_valid() is doing real work — not that the shapefile happened to be valid already.

Implementation guidance

Use the existing synthetic shapefile infrastructure from conftest.py as a pattern. Create temporary shapefiles with gdf.to_file(), load them through the mapper's internal methods, and verify the output.

The key insight: these tests verify the preprocessing pipeline, not the spatial algorithm. They ensure that data entering the overlap calculation is geometrically valid.

Acceptance criteria

  • 3 tests verify make_valid for each shapefile type (countries, priogrid, admin)
  • 1 test proves the fixture is genuinely invalid (the regression guard)
  • All 4 tests pass
  • Removing any make_valid() call from mapping.py causes at least one test to fail
  • Tests are in tests/test_regression.py or tests/test_preprocessing.py

References

  • Falsification campaign Claim 3.1 (CONTESTED), Claim 3.3 (FALSIFIED)
  • Test stubs: tests/test_falsification_campaign_3_1.py, tests/test_falsification_campaign_3_3.py
  • Risk register: C-12 (geometry intersection failures), D-04 (resolved: make_valid at load time)
  • ADR-011: precomputed lookup table depends on make_valid being active during precomputation

Activity

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