File size: 2,991 Bytes
e8055cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import json
import sys

from experiments import run


class FakeGeometry:
    @staticmethod
    def build_spatial_code(scene):
        return {"scene": scene["scene"]}, None

    @staticmethod
    def dump_spatial_code(code, path):
        with open(path, "w", encoding="utf-8") as stream:
            json.dump(code, stream)


def test_run_scene_routes_to_experiment_cache(monkeypatch, tmp_path):
    output = tmp_path / "spatial.json"
    monkeypatch.setattr(run.config, "spatial_code_path", lambda *args: output)
    monkeypatch.setattr(run, "load_existing_geometry", lambda *args: {"scene": args[0]})
    monkeypatch.setattr(run.loader, "load_hypothesis", lambda name: FakeGeometry)
    code, status, path = run.run_scene("scene0000_00", "A Hypothesis", frame_count=64)
    assert status == "built"
    assert path == output
    assert json.loads(output.read_text()) == code == {"scene": "scene0000_00"}


def test_run_scene_reuses_existing_spatial_code(monkeypatch, tmp_path):
    output = tmp_path / "spatial.json"
    output.write_text('{"cached": true}')
    monkeypatch.setattr(run.config, "spatial_code_path", lambda *args: output)
    monkeypatch.setattr(
        run,
        "load_existing_geometry",
        lambda *args: (_ for _ in ()).throw(AssertionError("source cache was read")),
    )
    code, status, _ = run.run_scene("scene0000_00", "A Hypothesis")
    assert status == "loaded"
    assert code == {"cached": True}


def test_load_existing_geometry_adapts_native_caches_in_memory(monkeypatch, tmp_path):
    da3 = tmp_path / "scene.pkl"
    sam3 = tmp_path / "scene.pt"
    da3.touch()
    sam3.touch()
    monkeypatch.setattr(
        run.encoder_config, "cache_file", lambda *args: tmp_path / "missing.pkl.gz"
    )
    monkeypatch.setattr(run.encoder_config, "da3_cache_file", lambda *args: da3)
    monkeypatch.setattr(run.encoder_config, "sam3_cache_file", lambda *args: sam3)
    monkeypatch.setattr(
        run.adapters, "adapt", lambda *args, **kwargs: {"scene": kwargs["scene"]}
    )
    monkeypatch.setattr(run.adapters, "validate", lambda geometry: geometry)
    geometry = run.load_existing_geometry(
        "scene0000_00", "metric", "uniform", "tracking", 64
    )
    assert geometry == {"scene": "scene0000_00"}


def test_da3_pickle_compatibility_installs_expected_class(monkeypatch):
    monkeypatch.delitem(sys.modules, "depth_anything_3", raising=False)
    monkeypatch.delitem(sys.modules, "depth_anything_3.specs", raising=False)
    monkeypatch.delitem(sys.modules, "addict", raising=False)
    monkeypatch.delitem(sys.modules, "addict.addict", raising=False)
    run.install_da3_pickle_compatibility()
    assert sys.modules["depth_anything_3.specs"].Prediction is run.CachedDA3Prediction
    assert sys.modules["addict.addict"].Dict is run.CachedAddictDict


def test_cached_addict_dict_does_not_invent_pickle_hooks():
    value = run.CachedAddictDict(answer=42)
    assert value.answer == 42
    assert not hasattr(value, "__setstate__")