Skip to content

Commit c1de8c6

Browse files
authored
TST: make testing more stable (#249)
1 parent cda9ee2 commit c1de8c6

File tree

1 file changed

+99
-52
lines changed

1 file changed

+99
-52
lines changed

tests/test_cx.py

+99-52
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def test_bounds2raster():
2626
-93.50721740722656,
2727
36.49387741088867,
2828
)
29-
_ = cx.bounds2raster(w, s, e, n, "test.tif", zoom=4, ll=True)
29+
_ = cx.bounds2raster(
30+
w, s, e, n, "test.tif", zoom=4, ll=True, source=cx.providers.CartoDB.Positron
31+
)
3032
rtr = rio.open("test.tif")
3133
img = np.array([band for band in rtr.read()]).transpose(1, 2, 0)
3234
solu = (
@@ -37,13 +39,13 @@ def test_bounds2raster():
3739
)
3840
for i, j in zip(rtr.bounds, solu):
3941
assert round(i - j, TOL) == 0
40-
assert img[0, 100, :].tolist() == [220, 217, 214, 255]
41-
assert img[20, 120, :].tolist() == [246, 245, 238, 255]
42-
assert img[200, 100, :].tolist() == [247, 246, 241, 255]
43-
assert img[:, :, :3].sum() == pytest.approx(44440377, rel=0.1)
44-
assert img.sum() == pytest.approx(61152057, rel=0.1)
45-
assert_array_almost_equal(img[:, :, :3].mean(), 226.0354461669922, decimal=0)
46-
assert_array_almost_equal(img.mean(), 233.27658462524414, decimal=0)
42+
assert img[0, 100, :].tolist() == [250, 250, 248, 255]
43+
assert img[20, 120, :].tolist() == [139, 153, 164, 255]
44+
assert img[200, 100, :].tolist() == [250, 250, 248, 255]
45+
assert img[:, :, :3].sum() == pytest.approx(47622796, rel=0.1)
46+
assert img.sum() == pytest.approx(64334476, rel=0.1)
47+
assert_array_almost_equal(img[:, :, :3].mean(), 242.2220662434896, decimal=0)
48+
assert_array_almost_equal(img.mean(), 245.4165496826172, decimal=0)
4749

4850
# multiple tiles for which result is not square
4951
w, s, e, n = (
@@ -52,7 +54,9 @@ def test_bounds2raster():
5254
6.15665815595878,
5355
51.47502370869813,
5456
)
55-
img, ext = cx.bounds2raster(w, s, e, n, "test2.tif", zoom=7, ll=True)
57+
img, ext = cx.bounds2raster(
58+
w, s, e, n, "test2.tif", zoom=7, ll=True, source=cx.providers.CartoDB.Positron
59+
)
5660
rtr = rio.open("test2.tif")
5761
rimg = np.array([band for band in rtr.read()]).transpose(1, 2, 0)
5862
assert rimg.shape == img.shape
@@ -84,7 +88,14 @@ def test_bounds2img(n_connections):
8488
16,
8589
]: # valid number of connections (test single and multiple connections)
8690
img, ext = cx.bounds2img(
87-
w, s, e, n, zoom=4, ll=True, n_connections=n_connections
91+
w,
92+
s,
93+
e,
94+
n,
95+
zoom=4,
96+
ll=True,
97+
n_connections=n_connections,
98+
source=cx.providers.CartoDB.Positron,
8899
)
89100
solu = (
90101
-12523442.714243276,
@@ -94,9 +105,9 @@ def test_bounds2img(n_connections):
94105
)
95106
for i, j in zip(ext, solu):
96107
assert round(i - j, TOL) == 0
97-
assert img[0, 100, :].tolist() == [220, 217, 214, 255]
98-
assert img[20, 120, :].tolist() == [246, 245, 238, 255]
99-
assert img[200, 100, :].tolist() == [247, 246, 241, 255]
108+
assert img[0, 100, :].tolist() == [250, 250, 248, 255]
109+
assert img[20, 120, :].tolist() == [139, 153, 164, 255]
110+
assert img[200, 100, :].tolist() == [250, 250, 248, 255]
100111
elif n_connections == 0: # no connections should raise an error
101112
with pytest.raises(ValueError):
102113
img, ext = cx.bounds2img(
@@ -112,7 +123,9 @@ def test_warp_tiles():
112123
-93.50721740722656,
113124
36.49387741088867,
114125
)
115-
img, ext = cx.bounds2img(w, s, e, n, zoom=4, ll=True)
126+
img, ext = cx.bounds2img(
127+
w, s, e, n, zoom=4, ll=True, source=cx.providers.CartoDB.Positron
128+
)
116129
wimg, wext = cx.warp_tiles(img, ext)
117130
assert_array_almost_equal(
118131
np.array(wext),
@@ -125,9 +138,9 @@ def test_warp_tiles():
125138
]
126139
),
127140
)
128-
assert wimg[100, 100, :].tolist() == [247, 246, 241, 255]
129-
assert wimg[100, 200, :].tolist() == [246, 246, 241, 255]
130-
assert wimg[20, 120, :].tolist() == [139, 128, 149, 255]
141+
assert wimg[100, 100, :].tolist() == [249, 249, 247, 255]
142+
assert wimg[100, 200, :].tolist() == [250, 250, 248, 255]
143+
assert wimg[20, 120, :].tolist() == [250, 250, 248, 255]
131144

132145

133146
@pytest.mark.network
@@ -138,13 +151,15 @@ def test_warp_img_transform():
138151
-93.50721740722656,
139152
36.49387741088867,
140153
)
141-
_ = cx.bounds2raster(w, s, e, n, "test.tif", zoom=4, ll=True)
154+
_ = cx.bounds2raster(
155+
w, s, e, n, "test.tif", zoom=4, ll=True, source=cx.providers.CartoDB.Positron
156+
)
142157
rtr = rio.open("test.tif")
143158
img = np.array([band for band in rtr.read()])
144159
wimg, _ = cx.warp_img_transform(img, rtr.transform, rtr.crs, "epsg:4326")
145-
assert wimg[:, 100, 100].tolist() == [247, 246, 241, 255]
146-
assert wimg[:, 100, 200].tolist() == [246, 246, 241, 255]
147-
assert wimg[:, 20, 120].tolist() == [139, 128, 149, 255]
160+
assert wimg[:, 100, 100].tolist() == [249, 249, 247, 255]
161+
assert wimg[:, 100, 200].tolist() == [250, 250, 248, 255]
162+
assert wimg[:, 20, 120].tolist() == [250, 250, 248, 255]
148163

149164

150165
def test_howmany():
@@ -227,7 +242,7 @@ def test_validate_zoom():
227242

228243
@pytest.mark.network
229244
def test_place():
230-
expected_bbox = [-105.3014509, 39.9643513, -105.1780988, 40.094409]
245+
expected_bbox = [-105.3014509, 39.9569362, -105.1780988, 40.0944658]
231246
expected_bbox_map = [
232247
-11740727.544603072,
233248
-11701591.786121061,
@@ -356,7 +371,9 @@ def test_add_basemap_query():
356371

357372
assert ax.images[0].get_array().sum() == 64685390
358373
assert ax.images[0].get_array().shape == (256, 256, 4)
359-
assert_array_almost_equal(ax.images[0].get_array()[:, :, :3].mean(), 244.03656, decimal=0)
374+
assert_array_almost_equal(
375+
ax.images[0].get_array()[:, :, :3].mean(), 244.03656, decimal=0
376+
)
360377
assert_array_almost_equal(ax.images[0].get_array().mean(), 246.77742, decimal=0)
361378

362379

@@ -420,42 +437,68 @@ def test_add_basemap_auto_zoom():
420437
)
421438
assert_array_almost_equal(ax.images[0].get_array().mean(), 217.2718038, decimal=0)
422439

423-
@pytest.mark.network
424-
@pytest.mark.parametrize("zoom_adjust, expected_extent, expected_sum_1, expected_sum_2, expected_shape", [
425-
# zoom_adjust and expected values where zoom_adjust == 1
426-
(1, (
427-
-11740727.544603072,
428-
-11701591.786121061,
429-
4852834.051769271,
430-
4891969.810251278,
431-
), 648244877, 915631757, (1024, 1024, 4)),
432440

433-
# zoom_adjust and expected values where zoom_adjust == -1
434-
(-1, (
441+
@pytest.mark.network
442+
@pytest.mark.parametrize(
443+
"zoom_adjust, expected_extent, expected_sum_1, expected_sum_2, expected_shape",
444+
[
445+
# zoom_adjust and expected values where zoom_adjust == 1
446+
(
447+
1,
448+
(
449+
-11740727.544603072,
450+
-11701591.786121061,
451+
4852834.0517692715,
452+
4891969.810251278,
453+
),
454+
763769618,
455+
1031156498,
456+
(1024, 1024, 4),
457+
),
458+
# zoom_adjust and expected values where zoom_adjust == -1
459+
(
460+
-1,
461+
(
462+
-11740727.544603072,
463+
-11701591.786121061,
464+
4852834.0517692715,
465+
4891969.810251278,
466+
),
467+
47973710,
468+
64685390,
469+
(256, 256, 4),
470+
),
471+
],
472+
)
473+
def test_add_basemap_zoom_adjust(
474+
zoom_adjust, expected_extent, expected_sum_1, expected_sum_2, expected_shape
475+
):
476+
x1, x2, y1, y2 = [
435477
-11740727.544603072,
436478
-11701591.786121061,
437-
4852834.051769271,
479+
4852834.0517692715,
438480
4891969.810251278,
439-
), 40396582, 57108262, (256, 256, 4)),
440-
])
441-
def test_add_basemap_zoom_adjust(zoom_adjust, expected_extent, expected_sum_1, expected_sum_2, expected_shape):
442-
x1, x2, y1, y2 = [-11740727.544603072, -11701591.786121061, 4852834.0517692715, 4891969.810251278]
481+
]
443482

444483
f, ax = matplotlib.pyplot.subplots(1)
445484
ax.set_xlim(x1, x2)
446485
ax.set_ylim(y1, y2)
447-
cx.add_basemap(ax, zoom="auto", zoom_adjust=zoom_adjust)
486+
cx.add_basemap(
487+
ax, zoom="auto", zoom_adjust=zoom_adjust, source=cx.providers.CartoDB.Positron
488+
)
448489

449490
ax_extent = expected_extent
450491
assert_array_almost_equal(ax_extent, ax.images[0].get_extent())
451492

452-
assert ax.images[0].get_array()[:, :, :3].sum() == pytest.approx(expected_sum_1, rel=0.1)
493+
assert ax.images[0].get_array()[:, :, :3].sum() == pytest.approx(
494+
expected_sum_1, rel=0.1
495+
)
453496
assert ax.images[0].get_array().sum() == pytest.approx(expected_sum_2, rel=0.1)
454497
assert ax.images[0].get_array().shape == expected_shape
455498
assert_array_almost_equal(
456-
ax.images[0].get_array()[:, :, :3].mean(), 204.695738, decimal=0
499+
ax.images[0].get_array()[:, :, :3].mean(), 242.79582, decimal=0
457500
)
458-
assert_array_almost_equal(ax.images[0].get_array().mean(), 217.2718038, decimal=0)
501+
assert_array_almost_equal(ax.images[0].get_array().mean(), 245.8468, decimal=0)
459502

460503

461504
@pytest.mark.network
@@ -466,38 +509,42 @@ def test_add_basemap_warping():
466509
f, ax = matplotlib.pyplot.subplots(1)
467510
ax.set_xlim(x1, x2)
468511
ax.set_ylim(y1, y2)
469-
cx.add_basemap(ax, crs="epsg:4326", attribution=None)
512+
cx.add_basemap(
513+
ax, crs="epsg:4326", attribution=None, source=cx.providers.CartoDB.Positron
514+
)
470515
assert ax.get_xlim() == (x1, x2)
471516
assert ax.get_ylim() == (y1, y2)
472-
assert ax.images[0].get_array()[:, :, :3].sum() == pytest.approx(811443707, rel=0.1)
517+
assert ax.images[0].get_array()[:, :, :3].sum() == pytest.approx(978096737, rel=0.1)
473518
assert ax.images[0].get_array().shape == (1135, 1183, 4)
474519
assert_array_almost_equal(
475-
ax.images[0].get_array()[:, :, :3].mean(), 201.445020, decimal=0
520+
ax.images[0].get_array()[:, :, :3].mean(), 242.8174808, decimal=0
476521
)
477-
assert_array_almost_equal(ax.images[0].get_array().mean(), 214.8337650, decimal=0)
522+
assert_array_almost_equal(ax.images[0].get_array().mean(), 245.8631, decimal=0)
478523

479524

480525
@pytest.mark.network
481526
def test_add_basemap_warping_local():
482527
# Test local source warping
483528
x1, x2 = -105.5, -105.00
484529
y1, y2 = 39.56, 40.13
485-
_ = cx.bounds2raster(x1, y1, x2, y2, "./test2.tif", ll=True)
530+
_ = cx.bounds2raster(
531+
x1, y1, x2, y2, "./test2.tif", ll=True, source=cx.providers.CartoDB.Positron
532+
)
486533
f, ax = matplotlib.pyplot.subplots(1)
487534
ax.set_xlim(x1, x2)
488535
ax.set_ylim(y1, y2)
489536
cx.add_basemap(ax, source="./test2.tif", crs="epsg:4326", attribution=None)
490537
assert ax.get_xlim() == (x1, x2)
491538
assert ax.get_ylim() == (y1, y2)
492539

493-
assert ax.images[0].get_array()[:, :, :3].sum() == pytest.approx(515569833, rel=0.1)
540+
assert ax.images[0].get_array()[:, :, :3].sum() == pytest.approx(613344449, rel=0.1)
494541
assert ax.images[0].get_array().shape == (980, 862, 4)
495542
assert_array_almost_equal(
496-
ax.images[0].get_array()[:, :, :3].mean(), 203.4383860, decimal=0
543+
ax.images[0].get_array()[:, :, :3].mean(), 242.0192121, decimal=0
497544
)
498545

499-
assert ax.images[0].get_array().sum() == pytest.approx(730014888, rel=0.1)
500-
assert_array_almost_equal(ax.images[0].get_array().mean(), 216.0420971, decimal=0)
546+
assert ax.images[0].get_array().sum() == pytest.approx(827789504, rel=0.1)
547+
assert_array_almost_equal(ax.images[0].get_array().mean(), 244.9777167, decimal=0)
501548

502549

503550
@pytest.mark.network

0 commit comments

Comments
 (0)