Skip to content

Regression test: zero-area grid geometry guard #8

Closed

Description

@Polichinel

Context

Seven overlap calculation sites in mapping.py now have a zero-area guard:

if grid_geometry.area == 0.0:
    logger.warning(f"Zero-area grid geometry for GID {gid}, skipping {level} overlap")
    return None

These guards were added to fix C-20 (ZeroDivisionError on degenerate cells). Without them, a cell with zero area causes overlap_ratio = overlap_area / total_area to raise ZeroDivisionError, caught by the broad except Exception, logged at DEBUG, and all countries skipped — the cell silently vanishes from the output.

No test verifies the guards work. If someone removes them during refactoring (they appear in 7 locations due to the cache duplication — C-06), no test fails. The ZeroDivisionError returns, and degenerate cells silently vanish.

This was identified by the falsification campaign (Claims 3.1 and 3.3).

Why now

The zero-area guard is at all 7 overlap sites including _find_dominant_country_gids (the reverse lookup). This is the third most impactful regression to protect because:

  1. It was found across 3 falsification rounds (the fix was repeatedly incomplete)
  2. Code duplication (C-06) means the guard exists in 7 places — high probability of partial removal during refactoring
  3. ADR-011's precomputation will process all ~65K cells — a degenerate cell would silently vanish from the lookup table

Requirements

Test: test_zero_area_cell_returns_none_with_warning

Setup:
Create a synthetic PRIO-GRID with a degenerate cell — a polygon collapsed to a line:

from shapely.geometry import Polygon
degenerate = Polygon([(0, 0), (1, 0), (0, 0)])  # line, not polygon
assert degenerate.area == 0.0  # precondition

Add this cell (e.g., GID=99) to the synthetic priogrid GeoDataFrame alongside the normal cells.

Act:
Call mapper.find_country_for_gid(99) on the mapper loaded with this modified synthetic data.

Assert:

  1. Returns None (not a country assignment, not an exception)
  2. A WARNING-level log message containing "Zero-area" and "GID 99" was emitted
  3. The WARNING fires exactly once (not N times per candidate country — this was a previous bug)

Test: test_zero_area_cell_admin1_returns_none

Same pattern but for find_admin1_for_gid(99). Assert None return with WARNING.

Test: test_zero_area_cell_admin2_returns_none

Same pattern but for find_admin2_for_gid(99). Assert None return with WARNING.

Test: test_zero_area_cell_excluded_from_reverse_lookup

Call find_gids_for_country("AAA"). Assert GID 99 is NOT in the result (a zero-area cell should not be counted in any country's cell list). A WARNING should be logged for GID 99 during the reverse lookup.

Implementation guidance

The cleanest approach is a new fixture in conftest.py that creates a mapper with one extra degenerate cell. Alternatively, create the degenerate GeoDataFrame inline in the test.

The WARNING log assertion should use caplog (pytest's log capture):

def test_zero_area_cell_returns_none_with_warning(mapper_with_degenerate_cell, caplog):
    with caplog.at_level(logging.WARNING):
        result = mapper_with_degenerate_cell.find_country_for_gid(99)
    assert result is None
    assert any("Zero-area" in r.message and "99" in r.message for r in caplog.records)

Acceptance criteria

  • 4 tests covering country, admin1, admin2, and reverse lookup for zero-area cells
  • All 4 pass
  • Removing ANY zero-area guard from mapping.py causes at least one test to fail (the test should detect the ZeroDivisionError or wrong behavior)
  • Tests are in tests/test_regression.py
  • WARNING log assertion is present (not just None return — the logging IS the regression signal)

References

  • Falsification campaign Claims 3.1 (CONTESTED), 3.3 (FALSIFIED)
  • Test stubs: tests/test_falsification_campaign_3_1.py, tests/test_falsification_campaign_3_3.py
  • Risk register: C-20 (zero-area cells, Tier 3 — mitigated)
  • Falsification rounds 1-3: zero-area guard was added incrementally across 3 rounds due to C-06 duplication

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