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_insert_none(): # 'insert' raises exception if empty input tree = RBush() tree.load(data_array) with pytest.raises(TypeError): tree.insert()
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_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_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_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_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_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)
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_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_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_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_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_data_load_empty(): tree = RBush() with pytest.raises(ValueError): tree.load([])
def test_load(): tree = RBush(4) tree.load(data_array) items, _ = tree.all() assert sorted_equal(data_array, items)
def test_clear_tree(): tree = RBush(4) tree.load(data_array) tree.clear() assert tree.to_json() == RBush(4).to_json()