"""Checks for source point cohorts and the compact coordinate format.""" import gzip import json from pathlib import Path import numpy as np import pytest from demo.scale4d_view_count.source_object_codec import ( coordinate_checksum, decode_positions, ) from demo.scale4d_view_count.source_objects import _validate_groups, scope_scores def coordinate_fixture(): gt = np.array([[[0, -128, 5000], [1, -127, 5001], [130, -256, 4999]]]) prediction = gt + np.array([[[100, 0, 0], [101, 0, 0], [102, 0, 0]]]) encoded = bytearray() for values in [gt, prediction - gt]: delta = values.copy() delta[:, 1:] -= values[:, :-1] for value in delta.ravel(): number = 2 * int(value) if value >= 0 else -2 * int(value) - 1 while number > 127: encoded.append((number % 128) + 128) number //= 128 encoded.append(number) scene = { "frames": 3, "query_count": 1, "gt": {}, "states": [{}], "position_codec": { "version": 1, "scale": 10000, "layers": [{"state": -1, "base": -1}, {"state": 0, "base": 0}], "checksums": [coordinate_checksum(gt), coordinate_checksum(prediction)], }, } return scene, gzip.compress(encoded), gt, prediction def test_signed_motion_and_reference_coordinates(): scene, blob, gt, prediction = coordinate_fixture() result = decode_positions(scene, blob) np.testing.assert_array_equal(result["gt"]["positions"], gt / 10000) np.testing.assert_array_equal(result["states"][0]["positions"], prediction / 10000) assert "positions" not in scene["gt"] def test_coordinate_corruption_is_rejected(): scene, blob, _, _ = coordinate_fixture() scene["position_codec"]["checksums"][1] = "00000000" with pytest.raises(ValueError, match="checksum mismatch"): decode_positions(scene, blob) def test_future_coordinate_reference_is_rejected(): scene, blob, _, _ = coordinate_fixture() scene["position_codec"]["layers"][1]["base"] = 1 with pytest.raises(ValueError, match="precede"): decode_positions(scene, blob) def test_source_ids_define_groups_after_query_remapping(tmp_path: Path): (tmp_path / "ids.json").write_text(json.dumps([0, 2047, 3072, 4095])) (tmp_path / "metadata.json").write_text( json.dumps( { "point_groups": [ {"name": "object", "start": 0, "count": 2048}, {"name": "hand1", "start": 3072, "count": 1024}, ] } ) ) scene = { "query_count": 4, "scopes": { "all": {"ids": [0, 1, 2, 3]}, "object": {"ids": [0, 1]}, "hand1": {"ids": [2, 3]}, }, } record = {"source_point_ids": "ids.json", "source_metadata": "metadata.json"} _validate_groups(tmp_path, record, scene) scene["scopes"]["object"]["ids"] = [0, 2] with pytest.raises(ValueError, match="Source point group changed"): _validate_groups(tmp_path, record, scene) def test_scope_scores_exclude_query_initialization_and_other_objects(): gt = np.zeros((3, 2, 3), dtype=np.float32) prediction = gt.copy() prediction[:, 0, 0] = [1.0, 0.01, 0.03] prediction[:, 1, 0] = 4.0 visible = np.ones((3, 2), dtype=bool) mask = np.array([[False, False], [True, True], [True, True]]) scores = scope_scores(gt, visible, visible, prediction, visible, mask, [0]) assert scores["epe"] == pytest.approx(2.0) assert scores["aj"] == 100.0 assert scores["apd"] == 100.0