Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements-optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ imageio
multiprocess>=0.70
statsmodels
prettytable
contextily>=1.0.0; python_version < '3.14'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, and this is what 0e87657a addresses.

Reproduced on develop with contextily installed and outbound sockets blocked:

6 failed, 12 passed in 0.90s

FAILED test_ellipses_background_types_display_successfully[satellite]
FAILED test_ellipses_background_types_display_successfully[street]
FAILED test_ellipses_background_types_display_successfully[terrain]
FAILED test_ellipses_background_types_display_successfully[CartoDB.Positron]
FAILED test_ellipses_background_works_with_custom_limits
FAILED test_ellipses_background_saves_file_successfully

All six come out of the contextily.bounds2img call at monte_carlo_plots.py:166,
surfaced through the generic handler at :198.

Same blocked-socket run on this branch, with the autouse mock_background_tiles
fixture in place:

18 passed in 0.38s

The fixture patches contextily.bounds2img to return a 2x2 zero-valued tile plus the
Web Mercator extent for the requested bounds, so provider resolution, extent handling
and the plotting path are all still exercised — only the tile server is gone. Wall
clock for the file went from 37s to under half a second.

Verified on Python 3.12.3, contextily 1.7.1. tests/unit/simulation is 191 passed,
4 skipped; pylint on the changed file is 10.00/10.

pyvista>=0.45
imageio-ffmpeg>=0.5
28 changes: 28 additions & 0 deletions tests/unit/simulation/test_monte_carlo_plots_background.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable=unused-argument,assignment-from-no-return
import math
import os
import urllib.error
from unittest.mock import MagicMock, patch
Expand All @@ -19,6 +20,33 @@
)


@pytest.fixture(autouse=True)
def mock_background_tiles(monkeypatch):
"""Return deterministic map tiles without contacting a tile provider."""
contextily = import_optional_dependency("contextily")

def mock_bounds2img(west, south, east, north, **kwargs):
earth_radius = 6378137.0

def to_mercator(longitude, latitude):
x = earth_radius * math.radians(longitude)
y = earth_radius * math.log(
math.tan(math.pi / 4 + math.radians(latitude) / 2)
)
return x, y

min_x, min_y = to_mercator(west, south)
max_x, max_y = to_mercator(east, north)
return np.zeros((2, 2, 3), dtype=np.uint8), (
min_x,
max_x,
min_y,
max_y,
)

monkeypatch.setattr(contextily, "bounds2img", mock_bounds2img)


class MockMonteCarlo(MonteCarlo):
"""Create a mock class to test the method without running a real simulation.

Expand Down
Loading