Skip to content

Commit f5c66ee

Browse files
tests: use real photos of marked sketch map
instead of rendering digital marking onto sketch maps.
1 parent 407abd0 commit f5c66ee

File tree

7 files changed

+51
-50
lines changed

7 files changed

+51
-50
lines changed

tests/comparator.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,27 @@ def compare(self, received_path: str, approved_path: str) -> bool:
1717
df_received = geopandas.read_file(received_path).to_crs("EPSG:8857")
1818
df_approved = geopandas.read_file(approved_path).to_crs("EPSG:8857")
1919

20-
if len(df_approved.index) != len(df_received.index):
21-
print("Different numbers of features detected.")
20+
df_received_points = df_received[df_received.geometry.type == "Point"]
21+
df_approved_points = df_approved[df_approved.geometry.type == "Point"]
22+
23+
df_approved_polygons = df_approved[df_approved.geometry.type == "Polygon"]
24+
df_received_polygons = df_received[df_received.geometry.type == "Polygon"]
25+
26+
if len(df_approved_polygons.index) != len(df_received_polygons.index):
27+
logging.warning("Different number of polygon features detected.")
28+
return False
29+
30+
if len(df_approved_points.index) != len(df_received_points.index):
31+
logging.warning("Different number of point features detected.")
2232
return False
2333

2434
# NOTE: Hausdorff distance might be better to determine similarity
25-
area_diff = df_received.symmetric_difference(df_approved).area
26-
area_union = df_received.union(df_approved).area
35+
area_diff = df_received_polygons.symmetric_difference(df_approved_polygons).area
36+
area_union = df_received_polygons.union(df_approved_polygons).area
2737
diff = area_diff / area_union
2838
for d in diff.tolist():
29-
if d > 0.05:
30-
logging.warning(f"Area differs more by {d:.0%}")
39+
if d > 0.1:
40+
logging.warning(f"Area differs by more than {d:.0%}")
3141
return False
3242
return True
3343

5.48 MB
Loading
2.96 MB
Loading
5.12 MB
Loading

tests/integration/conftest.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import json
22
from dataclasses import astuple
33
from io import BytesIO
4-
from typing import Generator
4+
from pathlib import Path
55
from types import MappingProxyType
6+
from typing import Generator
67
from uuid import UUID
78

8-
import fitz
99
import pytest
1010
from celery.contrib.testing.tasks import ping # noqa: F401
1111
from flask import Flask
1212
from flask.testing import FlaskClient
1313
from flask_babel import Babel
1414
from numpy.typing import NDArray
15-
from PIL import Image, ImageOps
1615
from testcontainers.postgres import PostgresContainer
1716
from testcontainers.redis import RedisContainer
1817

@@ -27,9 +26,9 @@
2726
# are registered correctly.
2827
from sketch_map_tool.routes import app as smt_flask_app
2928
from sketch_map_tool.upload_processing import clip
29+
from sketch_map_tool.upload_processing.qr_code_reader import read_qr_code
3030
from tests import FIXTURE_DIR
3131
from tests import vcr_app as vcr
32-
from sketch_map_tool.upload_processing.qr_code_reader import read_qr_code
3332

3433

3534
#
@@ -261,31 +260,19 @@ def map_frame(uuid_create, flask_app, tmp_path_factory) -> BytesIO:
261260

262261

263262
@pytest.fixture(scope="session")
264-
def sketch_map_marked(uuid_create, sketch_map, tmp_path_factory) -> bytes:
265-
"""Sketch map with markings as PNG."""
266-
# TODO: increase resolution of PNG
267-
path = tmp_path_factory.getbasetemp() / uuid_create / "sketch-map-marked.png"
268-
269-
# Convert PDF to PNG
270-
pdf = fitz.open(stream=sketch_map) # type: ignore
271-
pag = pdf.load_page(0)
272-
mat = fitz.Matrix(2, 2)
273-
pag.get_pixmap(matrix=mat).save(path, output="png")
274-
275-
# Draw shapes on PNG (Sketch Map)
276-
img1 = Image.open(path) # Sketch Map (primary image)
277-
img2 = Image.open(
278-
FIXTURE_DIR / "upload-processing" / "markings-transparent.png"
279-
) # Markings (overlay image)
280-
img2 = ImageOps.cover(img2, img1.size) # type: ignore
281-
img1.paste(img2, (0, 0), mask=img2) # Overlay images starting at 0, 0
282-
283-
# Displaying the image
284-
# img1.show()
285-
286-
# TODO: what should be the return type of the fixture?
287-
img1.save(path)
288-
with open(path, "rb") as file:
263+
def sketch_map_marked_path(layer) -> Path:
264+
"""Photo of the sketch map with markings."""
265+
return FIXTURE_DIR / f"sketch-map-marked-{layer}.jpg"
266+
267+
268+
@pytest.fixture(scope="session")
269+
def sketch_map_marked(sketch_map_marked_path) -> bytes:
270+
"""Photo of the sketch map with markings."""
271+
# NOTE: If you want to change the markings of this fixture:
272+
# (1) Run tests. (2) Print PDF written to tmp dir by uuid_create fixture.
273+
# (3) Put markings on printout. (4) Make a photo.
274+
# (5) put photo file in fixture dir.
275+
with open(sketch_map_marked_path, "rb") as file:
289276
return file.read()
290277

291278

@@ -352,9 +339,13 @@ def read_qr_code_(image):
352339

353340

354341
@pytest.fixture(scope="session")
355-
def vector(uuid_digitize, tmp_path_factory) -> bytes:
356-
path = tmp_path_factory.getbasetemp() / uuid_digitize / "vector.geojson"
357-
with open(path, "rb") as file:
342+
def vector_path(uuid_digitize, tmp_path_factory) -> Path:
343+
return tmp_path_factory.getbasetemp() / uuid_digitize / "vector.geojson"
344+
345+
346+
@pytest.fixture(scope="session")
347+
def vector(vector_path) -> bytes:
348+
with open(vector_path, "rb") as file:
358349
return file.read()
359350

360351

tests/integration/test_approval.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
import pytest
24
from approvaltests import Options, verify_binary
35

@@ -7,22 +9,17 @@
79

810

911
@pytest.fixture(scope="session")
10-
def vector_path(tmp_path_factory, uuid_digitize) -> bytes:
12+
def vector_path(tmp_path_factory, uuid_digitize) -> Path:
1113
return tmp_path_factory.getbasetemp() / uuid_digitize / "vector.geojson"
1214

1315

14-
@pytest.mark.usefixtures("sketch_map_marked")
15-
@pytest.fixture(scope="session")
16-
def sketch_map_marked_path(tmp_path_factory, uuid_create) -> bytes:
17-
return tmp_path_factory.getbasetemp() / uuid_create / "sketch-map-marked.png"
18-
19-
20-
def test_smt_approver(sketch_map_marked_path, vector_path, layer):
16+
def test_smt_approver(sketch_map_marked_path, vector_path):
2117
options = (
2218
Options()
2319
.with_reporter(SketchMapToolReporter(sketch_map=sketch_map_marked_path))
2420
.with_comparator(GeoJSONComparator())
2521
.with_namer(PytestNamer())
2622
)
2723
with open(vector_path, "rb") as f:
24+
# TODO: One false positives for OAM based sketch map
2825
verify_binary(f.read(), ".geojson", options=options)

tests/integration/upload_processing/test_detect_markings.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@ def test_detect_markings(
8181
yolo_cls,
8282
sam_predictor,
8383
)
84-
if layer.startswith("esri"):
85-
# TODO: one marking for esri layer is not detected
84+
if layer.startswith("osm"):
85+
assert len(markings) == 8
86+
elif layer.startswith("esri"):
87+
assert len(markings) == 9
88+
elif layer.startswith("oam"):
8689
assert len(markings) == 5
8790
else:
88-
assert len(markings) == 6
91+
raise ValueError("Not reachable")
8992

9093
# NOTE: uncomment for manual/visual assessment of detected markings
91-
show_bbox_of_markings(map_frame_marked, markings)
94+
# show_bbox_of_markings(map_frame_marked, markings)
9295

9396

9497
def show_bbox_of_markings(map_frame_marked, markings):

0 commit comments

Comments
 (0)