Debian Codegraph @

benchmark/reconstruction/evaluation/tartanair/package_tartanair_v2_test.py

raw
1 import hashlib
2 import io
3
4 import numpy as np
5 import pytest
6
7 PILImage = pytest.importorskip("PIL.Image")
8
9 from . import package_tartanair_v2 # noqa: E402
10 from .package_tartanair_v2 import ( # noqa: E402
11 DEPTH_SCALE,
12 DEPTH_SIZE,
13 encode_depth_png,
14 encode_image_jpeg,
15 )
16
17
18 class FakeResponse:
19 def __init__(self, content: bytes):
20 self.content = content
21
22 def raise_for_status(self):
23 pass
24
25
26 def test_download_tartanair_license(monkeypatch):
27 license_data = b"license text"
28 monkeypatch.setattr(
29 package_tartanair_v2,
30 "TARTANAIR_LICENSE_SHA256",
31 hashlib.sha256(license_data).hexdigest(),
32 )
33 monkeypatch.setattr(
34 package_tartanair_v2.requests,
35 "get",
36 lambda *args, **kwargs: FakeResponse(license_data),
37 )
38 assert package_tartanair_v2.download_tartanair_license() == license_data
39
40
41 def test_download_tartanair_license_rejects_unexpected_content(monkeypatch):
42 monkeypatch.setattr(
43 package_tartanair_v2.requests,
44 "get",
45 lambda *args, **kwargs: FakeResponse(b"unexpected"),
46 )
47 with pytest.raises(RuntimeError, match="Unexpected TartanAir license"):
48 package_tartanair_v2.download_tartanair_license()
49
50
51 def test_encode_depth_png_min_pools_and_quantizes():
52 depth = np.full((1024, 2048), 10.0, dtype="<f4")
53 depth[:4, :4] = 2.0
54 rgba = depth.view(np.uint8).reshape(1024, 2048, 4)
55 source = io.BytesIO()
56 PILImage.fromarray(rgba, mode="RGBA").save(source, format="PNG")
57
58 decoded = np.asarray(
59 PILImage.open(io.BytesIO(encode_depth_png(source.getvalue())))
60 )
61 assert decoded.shape == (DEPTH_SIZE[1], DEPTH_SIZE[0])
62 assert decoded.dtype == np.uint16
63 assert decoded[0, 0] == round(2.0 * DEPTH_SCALE)
64 assert decoded[0, 1] == round(10.0 * DEPTH_SCALE)
65
66
67 def test_encode_image_jpeg():
68 source = io.BytesIO()
69 PILImage.new("RGB", (64, 32), (10, 20, 30)).save(source, format="PNG")
70 encoded = encode_image_jpeg(source.getvalue())
71 image = PILImage.open(io.BytesIO(encoded))
72 assert image.format == "JPEG"
73 assert image.size == (64, 32)