File size: 2,962 Bytes
2b01b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Integrity checks for resumed writes and exact RGB batch storage."""
import tempfile
from pathlib import Path
import unittest
from unittest.mock import patch

import cv2
import numpy as np
import zarr

from export_oxt_dataset import AtomicLocalStore, write_images, write_rows, update_meta


class ExportStorageTests(unittest.TestCase):
    def test_failed_replacement_preserves_committed_shared_chunk(self):
        with tempfile.TemporaryDirectory() as temporary:
            store = AtomicLocalStore(Path(temporary) / "test.zarr")
            root = zarr.open_group(store=store, mode="w", zarr_format=2)
            data = root.require_group("data")
            write_rows(data, {"joint": np.arange(12).reshape(4, 3)}, 0, 10, {})
            before = data["joint"][:].copy()
            with patch.object(Path, "replace", side_effect=OSError("simulated interruption before rename")):
                with self.assertRaises(OSError):
                    write_rows(data, {"joint": np.full((2, 3), 88)}, 4, 10, {})
            np.testing.assert_array_equal(data["joint"][:], before)
            write_rows(data, {"joint": np.full((2, 3), 88)}, 4, 10, {})
            np.testing.assert_array_equal(data["joint"][:4], before[:4])
            np.testing.assert_array_equal(data["joint"][4:6], np.full((2, 3), 88))

    def test_batch_rgb_retains_channels_and_arbitrary_selected_frames(self):
        with tempfile.TemporaryDirectory() as temporary:
            cache = Path(temporary) / "images"
            cache.mkdir()
            rng = np.random.default_rng(5)
            pixels = rng.integers(0, 256, (40, 6, 8, 3), dtype=np.uint8)
            for i, rgb in enumerate(pixels):
                self.assertTrue(cv2.imwrite(str(cache / f"frame{i:06d}.png"), rgb[:, :, ::-1]))
            root = zarr.open_group(store=AtomicLocalStore(Path(temporary) / "image.zarr"), mode="w", zarr_format=2)
            indices = np.arange(1, 40, 2)
            write_images(root, "camera", cache, indices, 2, 24, 8, 6)
            np.testing.assert_array_equal(root["camera_rgb"][2:22], pixels[indices])

    def test_future_longer_strings_are_preserved_and_truncation_rejected(self):
        with tempfile.TemporaryDirectory() as temporary:
            root = zarr.open_group(store=AtomicLocalStore(temporary), mode="w", zarr_format=2)
            write_rows(root, {"text": np.array(["short"])}, 0, 3, {"text": 16})
            # Zarr 3's default UTF-8 object filter with a fixed U dtype is
            # unreadable by Zarr 2; fixed-width values must use plain bytes.
            self.assertIsNone(root["text"].metadata.filters)
            write_rows(root, {"text": np.array(["longer UTF-8 中文"])}, 1, 3, {"text": 16})
            self.assertEqual(root["text"][1], "longer UTF-8 中文")
            with self.assertRaises(ValueError):
                write_rows(root, {"text": np.array(["x" * 17])}, 2, 3, {"text": 16})


if __name__ == "__main__":
    unittest.main()