def test_data_load_empty_maxEntries(): tree = RBush(4) tree.load(empty_array) assert tree.height == 2 items, _ = tree.all() assert sorted_equal(empty_array, items)
def test_root_split(): maxEntries = 9 items = some_data(9 + 1) tree = RBush(maxEntries) for i, item in enumerate(items): tree.insert(**item) assert tree.height == 2
def test_chain(): tree = RBush() new_data = [7, 13, 21, 17] tree.load(data_array).insert(*new_data).remove(*new_data) items, _ = tree.all() assert sorted_equal(data_array, items)
def test_data_insert_empty_maxEntries(): tree = RBush(4) for datum in empty_array: tree.insert(*datum) assert tree.height == 2 items, _ = tree.all() assert sorted_equal(empty_array, items)
def run(N_insert=None, N_remove=None, _load=None): from rbush import RBush from rbush.data import generate_data_items, generate_data_array N_insert = N_insert or N_INSERT N_remove = N_remove or N_REMOVE N_search = N_remove data_items = generate_data_items(N_insert, 1) data_array = generate_data_array(N_insert, 1) bboxes100 = generate_data_items(N_search, 10) bboxes10 = generate_data_items(N_search, 1) bboxes1 = generate_data_items(N_search, 0.01) if not _load: tree = RBush(MAX_ENTRIES) # insertion t_insertion = insertion(tree, data_items) print('{:d} insertions one-by-one: {:.5f}'.format(N_insert, t_insertion)) # Search items 10% t_search100 = search(tree, bboxes100) print('{:d} searches in ~10%: {:.5f}'.format(N_search, t_search100)) # Search items 1% t_search10 = search(tree, bboxes10) print('{:d} searches in ~1%: {:.5f}'.format(N_search, t_search10)) # Search items 0.01% t_search1 = search(tree, bboxes1) print('{:d} searches in ~0.01%: {:.5f}'.format(N_search, t_search1)) # removal t_removal = removal(tree, data_items[:1000]) print('{:d} removals: {:.5f}'.format(N_search, t_removal)) print('Bulk load:') tree = RBush(MAX_ENTRIES) # Bulk load t_bulk = load(tree, data_array) print('{:d} items bulk load: {:.5f}'.format(N_insert, t_bulk)) # Search items 10% t_search100 = search(tree, bboxes100) print('{:d} searches in ~10%: {:.5f}'.format(N_search, t_search100)) # Search items 1% t_search10 = search(tree, bboxes10) print('{:d} searches in ~1%: {:.5f}'.format(N_search, t_search10)) # Search items 0.01% t_search1 = search(tree, bboxes1) print('{:d} searches in ~0.01%: {:.5f}'.format(N_search, t_search1))
def test_split_root_on_merge(): tree = RBush(4) tree.load(data_array) tree.load(data_array) data = np.concatenate([data_array, data_array]) assert tree.height == 4 items, _ = tree.all() assert sorted_equal(data, items)
def test_insert_numpy_vectors(): numitems = 100 import numpy as np xmin = np.random.randint(0, 100, numitems) ymin = np.random.randint(0, 100, numitems) xmax = xmin + np.random.randint(0, 100, numitems) ymax = ymin + np.random.randint(0, 100, numitems) tree = RBush() tree.insert(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax) items, _ = tree.all() assert len(items) == numitems
def test_retrieve_all(): tree = RBush(4) tree.load(data_array) bbox = { 'xmin': -Infinity, 'ymin': -Infinity, 'xmax': Infinity, 'ymax': Infinity } items, _ = tree.search(**bbox) assert sorted_equal(items, data_array)
def test_insert_items(): tree1 = RBush(4) for i in range(len(data_array)): tree1.insert(*data_array[i]) tree2 = RBush(4) tree2.load(data_array) items1, _ = tree1.all() items2, _ = tree2.all() assert 0 <= tree1.height - tree2.height <= 1 assert sorted_equal(items1, items2)
def test_find_matching_bbox(): # 'search' finds items intersecting and inside the given bbox tree = RBush(4) tree.load(data_array) items, _ = tree.search(xmin=40, ymin=20, xmax=80, ymax=70) compare_data = [[70, 20, 70, 20], [75, 25, 75, 25], [45, 45, 45, 45], [50, 50, 50, 50], [60, 60, 60, 60], [70, 70, 70, 70], [45, 20, 45, 20], [45, 70, 45, 70], [75, 50, 75, 50], [50, 25, 50, 25], [60, 35, 60, 35], [70, 45, 70, 45]] compare_data = np.asarray(compare_data) assert sorted_equal(items, compare_data)
def test_default_maxentries(): # constructor uses 9 max entries by default tree = RBush() tree.load(data_array[:9]) assert tree.height == 1 tree2 = RBush() tree2.load(data_array[:10]) assert tree2.height == 2
def test_clean_tree(): tree = RBush(4) tree.load(data_array) for i in range(len(data_array)): tree.remove(*data_array[i]) assert tree.height == 1 assert tree.to_json() == RBush(4).to_json()
def test_insert_item(): data = [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [1, 1, 2, 2]] data = np.asarray(data) tree = RBush(4) tree.load(data[0:3]) tree.insert(*data[3]) assert tree.height == 1 items, _ = tree.all() assert sorted_equal(items, data[0:4]) tree.insert(*data[4]) assert tree.height == 2 items, _ = tree.all() assert sorted_equal(items, data)
def test_insert_none(): # 'insert' raises exception if empty input tree = RBush() tree.load(data_array) with pytest.raises(TypeError): tree.insert()
def test_remove_items(): tree = RBush(4) tree.load(data_array) len_ = len(data_array) items_removed = [] items_removed.extend(tree.remove(*data_array[0])) items_removed.extend(tree.remove(*data_array[1])) items_removed.extend(tree.remove(*data_array[2])) items_removed = np.asarray([i[0] for i in items_removed]) items, _ = tree.all() assert sorted_equal(data_array[3:], items) assert sorted_equal(data_array[:3], items_removed)
def test_against_brute_force_numba(): b = RBush() data = generate_data_array(100000, 10) b.load(data) search_box = (-1, -1, 1, 1) c = search_brute_force(b.data, b.boxes, *search_box) t1 = time() c = search_brute_force(b.data, b.boxes, *search_box) c = search_brute_force(b.data, b.boxes, *search_box) c = search_brute_force(b.data, b.boxes, *search_box) t2 = time() print('BRUTE FORCE;', len(c), 'time: {:.5f}'.format(t2 - t1)) c, d = b.search(*search_box) rbush_t1 = time() c, d = b.search(*search_box) c, d = b.search(*search_box) c, d = b.search(*search_box) rbush_t2 = time() print('RBUSH;', len(c), 'time: {:.5f}'.format(rbush_t2 - rbush_t1)) assert rbush_t2 - rbush_t1 < t2 - t1, 'Sorry not fast enough yet'
def test_load_insert(): tree = RBush(8, 4) tree.load(data_array[:17]) tree.load(data_array[17:20]) tree2 = RBush(8, 4) tree2.load(data_array[:17]) tree2.insert(*data_array[17]) tree2.insert(*data_array[18]) tree2.insert(*data_array[19]) assert tree.to_json() == tree2.to_json()
def test_load(): tree = RBush(4) tree.load(data_array) items, _ = tree.all() assert sorted_equal(data_array, items)
def test_data_load_empty(): tree = RBush() with pytest.raises(ValueError): tree.load([])
def test_merge_trees(): smaller = data_array[:10] tree1 = RBush(4) tree1.load(data_array) tree1.load(smaller) tree2 = RBush(4) tree2.load(smaller) tree2.load(data_array) assert tree1.height == tree2.height items1, _ = tree1.all() items2, _ = tree2.all() assert sorted_equal(items1, items2)
def test_clear_tree(): tree = RBush(4) tree.load(data_array) tree.clear() assert tree.to_json() == RBush(4).to_json()
def test_remove_nothing(): # 'remove' does nothing if nothing found tree1 = RBush() tree1.load(data_array) tree2 = RBush() tree2.load(data_array) tree2.remove(13, 13, 13, 13) items1, _ = tree1.all() items2, _ = tree2.all() assert sorted_equal(items1, items2)