SCALE4D / examples /visualizer_code /source /tests /test_scale4d_viewer_bundle.py
dliu1014's picture Xuweiyi's picture meta-bot's picture
Initial data commit
9c890d3
Raw History Blame Contribute Delete
4.26 kB
import base64
import gzip
import json
from pathlib import Path
import numpy as np
import pytest
from demo.scale4d_view_count.bundle import (
build_viewer,
bundle_path,
file_sha256,
validate_scene,
verify_bundle,
)
def scene(dataset):
positions = [[[0.0, 0.0, 2.0], [0.01, 0.0, 2.0]]]
return {
"id": dataset,
"dataset": dataset,
"frames": 2,
"query_count": 1,
"position_source": "uv_depth",
"states": [
{"views": count, "positions": positions, "valid": [[True, True]]}
for count in range(1, 11)
],
"gt": {"positions": positions, "valid": [[True, True]]},
"camera_ids": [1],
"intrinsics": np.eye(3)[None, None].repeat(2, axis=1).tolist(),
"relative_cameras": np.eye(4)[None, None].repeat(2, axis=1).tolist(),
"video": dataset + ".webm",
}
def test_standalone_roundtrip_preserves_metric_tracks(tmp_path):
rows = []
for dataset in ("pstudio", "show3d"):
data = scene(dataset)
name = dataset + ".json.gz"
(tmp_path / name).write_bytes(gzip.compress(json.dumps(data).encode()))
(tmp_path / data["video"]).write_bytes(b"video bytes")
rows.append({"id": dataset, "dataset": dataset, "data": name})
(tmp_path / "scenes.json").write_text(json.dumps(rows))
output = build_viewer(tmp_path, tmp_path / "index.html")
text = output.read_text()
opening = '<script id="inline-assets" type="application/json">'
assets = json.loads(text.split(opening, 1)[1].split("</script>", 1)[0])
restored = json.loads(
gzip.decompress(base64.b64decode(assets["pstudio.json.gz"]["data"]))
)
assert restored == scene("pstudio")
assert "{{VIEWER_JS}}" not in text
with pytest.raises(FileExistsError):
build_viewer(tmp_path, output)
def test_scene_rejects_missing_views_and_wrong_track_order():
data = scene("pstudio")
data["states"].pop()
with pytest.raises(ValueError, match="1–10"):
validate_scene(data)
data = scene("pstudio")
data["states"][0]["positions"] = [[[0, 0, 2]], [[0, 0, 2]]]
with pytest.raises(ValueError, match="dimensions"):
validate_scene(data)
def test_matched_camera_modes_can_share_the_same_view_count():
import copy
data = scene("pstudio")
state = data["states"][2]
on, off = copy.deepcopy(state), copy.deepcopy(state)
on["camera"], off["camera"] = "on", "off"
data["states"] = [on, off]
data["available_view_counts"] = [3]
validate_scene(data)
data["states"].append(copy.deepcopy(off))
with pytest.raises(ValueError, match="declared"):
validate_scene(data)
def test_distinct_ema_teachers_support_native_xyz_at_the_same_input_setting():
import copy
data = scene("adt")
data["position_source"] = "xyz"
data["available_view_counts"] = [3]
original = data["states"][2]
data["states"] = []
for model in ("b2_ema", "b3_ema"):
for camera in ("on", "off"):
state = copy.deepcopy(original)
state.update(model=model, camera=camera)
data["states"].append(state)
validate_scene(data)
data["states"].append(copy.deepcopy(data["states"][0]))
with pytest.raises(ValueError, match="declared"):
validate_scene(data)
def test_manifest_detects_tampering_and_path_escape(tmp_path):
asset = tmp_path / "result.npz"
asset.write_bytes(b"original result")
manifest = {
"format_version": 1,
"files": [
{
"path": asset.name,
"bytes": asset.stat().st_size,
"sha256": file_sha256(asset),
}
],
}
(tmp_path / "manifest.json").write_text(json.dumps(manifest))
verify_bundle(tmp_path)
asset.write_bytes(b"modified result")
with pytest.raises(ValueError, match="checksum"):
verify_bundle(tmp_path)
for name in ("../token", "/tmp/token"):
with pytest.raises(ValueError, match="path"):
bundle_path(tmp_path, name)
(tmp_path / "escape").symlink_to(Path("/tmp"), target_is_directory=True)
with pytest.raises(ValueError, match="escapes"):
bundle_path(tmp_path, "escape/token")