MCPcopy Create free account
hub / github.com/Ropedia/SpatialBench / build_loop_batches

Function build_loop_batches

benchmark/models/scal3r/utils/loop/loop_utils.py:17–86  ·  view source on GitHub ↗
(
    sequence,
    indices,
    loop_list,
    loop_size: int,
    height: int,
    width: int,
    dataset_cfg,
    build_block_fn: Callable,
    log_fn: Callable[[str], None],
)

Source from the content-addressed store, hash-verified

15
16
17def build_loop_batches(
18 sequence,
19 indices,
20 loop_list,
21 loop_size: int,
22 height: int,
23 width: int,
24 dataset_cfg,
25 build_block_fn: Callable,
26 log_fn: Callable[[str], None],
27):
28 batches_loop = []
29 indices_loop = []
30 starts = [item[0] for item in indices]
31
32 def locate_block(target: int):
33 import bisect
34
35 position = bisect.bisect_right(starts, target) - 1
36 if position > len(starts) - 1 or position < 0 or target > indices[position][1] or target < indices[position][0]:
37 raise ValueError(f"Cannot find the block for target index {target}.")
38 return position
39
40 def create_loop_block(block: int, target: int):
41 start, end, num_samples = indices[block][0], indices[block][1], loop_size * 2
42 if target - loop_size < start:
43 end = min(end, start + num_samples)
44 elif target + loop_size > end:
45 start = max(start, end - num_samples)
46 else:
47 start, end = target - loop_size, target + loop_size
48 return start, end
49
50 def overlap(window_a: tuple, window_b: tuple):
51 return max(window_a[0], window_b[0]) < min(window_a[1], window_b[1])
52
53 detected_loop = {}
54 for i, j in loop_list:
55 try:
56 block0 = locate_block(i)
57 start0, end0 = create_loop_block(block0, i)
58 sampler_indices = list(range(start0, end0))
59
60 block1 = locate_block(j)
61 start1, end1 = create_loop_block(block1, j)
62 sampler_indices.extend(list(range(start1, end1)))
63
64 if block0 == block1:
65 log_fn(f"Loop pair ({i}, {j}) falls inside block {block0}, skipping pose-graph loop loading.")
66 continue
67
68 key = tuple(sorted((block0, block1)))
69 windows = ((start0, end0), (start1, end1)) if key[0] == block0 else ((start1, end1), (start0, end0))
70 if key not in detected_loop:
71 detected_loop[key] = []
72 if any(overlap(windows[0], prev[0]) and overlap(windows[1], prev[1]) for prev in detected_loop[key]):
73 log_fn(f"Loop pair {key} with windows {windows} overlaps an existing loop block, skipping.")
74 continue

Callers 1

load_dataFunction · 0.90

Calls 3

locate_blockFunction · 0.85
create_loop_blockFunction · 0.85
overlapFunction · 0.85

Tested by

no test coverage detected