def test_initialize_error(): with pytest.raises(ValueError): class BadType: def __init__(self): self.bad = True KDTree.initialize([[0, 0, 0], [1, 1, 1], [0, 2, 0]], accept=BadType)
def test_2NN_2D(): tree = KDTree.initialize([[1, 1], [5, 5], [6, 5]]) nn = tree.nearest_neighbor([4, 5], n=2) exp = [[[5, 5], 1.0], [[6, 5], 2.0]] for i in range(len(nn)): assert np.all(nn[i][0] == np.asarray(exp[i][0])) assert nn[i][1] == exp[i][1]
def test_2NN_accept(): tree = KDTree.initialize( [KDSubType(1, 1), KDSubType(1, 2), KDSubType(1, 4)], accept=KDSubType) nn = tree.nearest_neighbor(KDSubType(1, 2), n=2) exp = [[KDSubType(1, 2), 0], [KDSubType(1, 1), 1]] for i in range(len(nn)): assert np.all(nn[i][0] == exp[i][0]) assert nn[i][1] == exp[i][1]
def test_KNN_mismatch(): tree = KDTree.initialize([[1], [2]]) with pytest.raises(ValueError): tree.nearest_neighbor([0, 0])
def test_2NN(): tree = KDTree.initialize([[1], [5], [6]]) assert np.all( tree.nearest_neighbor([4], n=2) == np.asarray([[[5], 1], [[6], 2]]))
def test_search_mismatch_accept(): tree = KDTree.initialize([KDSubType(1,0), KDSubType(1,1)], accept=KDSubType) with pytest.raises(ValueError): tree.search(KDSubType(2,0)) with pytest.raises(AttributeError): tree.search(0)
def test_insert_mismatch(): tree = KDTree.initialize([[1], [2]]) with pytest.raises(ValueError): tree.insert([0, 0])
def test_dPN_mismatch(): tree = KDTree.initialize([[1], [2]]) with pytest.raises(ValueError): assert tree.proximal_neighbor([0, 0])
def test_delete_2D(points_2d_delete, delete_2d, delete_2d_exp, capsys): tree = KDTree.initialize(points_2d_delete) tree = tree.delete(delete_2d) tree.visualize() captured = capsys.readouterr() assert captured.out == delete_2d_exp
def test_initialize_accept(points_type, init_type_exp): tree = KDTree.initialize(points_type, accept=KDSubType) assert np.all(tree.value == init_type_exp) assert np.all(tree.right.value == KDSubType(2, [1, 0])) assert np.all(tree.left.value == KDSubType(2, [-1, 1])) assert np.all(tree.left.left.value == KDSubType(2, [0, 0]))
def test_initialize_3D(points_3d, init_3d_exp): tree = KDTree.initialize(points_3d) assert np.all(tree.value == init_3d_exp)
def test_initialize_2D(points_2d, init_2d_exp, capsys): tree = KDTree.initialize(points_2d) tree.visualize() captured = capsys.readouterr() assert captured.out == init_2d_exp
def test_initialize_1D(points_1d, init_1d_exp): tree = KDTree.initialize(points_1d) assert np.all(tree.value == init_1d_exp)
def test_visualize(points_vis, vis_exp, capsys): tree = KDTree.initialize(points_vis) tree.visualize() captured = capsys.readouterr() assert captured.out == vis_exp
def test_d1PN(): tree = KDTree.initialize([[1], [4]]) assert np.all(tree.proximal_neighbor([3], d=1) == np.asarray([[[4], 1]]))
def test_delete_mismatch(): tree = KDTree.initialize([[1],[2]]) with pytest.raises(ValueError): tree.delete([0,0])
def test_d2PN(): tree = KDTree.initialize([[1], [5], [6]]) assert np.all( tree.proximal_neighbor([4], d=2) == np.asarray([[[5], 1], [[6], 2]]))
def test_search_mismatch(): tree = KDTree.initialize([[1],[2]]) with pytest.raises(ValueError): tree.search([0,0])
def test_1NN(): tree = KDTree.initialize([[1], [4]]) assert np.all(tree.nearest_neighbor([3]) == np.asarray([[[4], 1]]))
def test_insert_1D(points_1d_insert, insert_1d, insert_1d_exp, capsys): tree = KDTree.initialize(points_1d_insert) tree = tree.insert(insert_1d) tree.visualize() captured = capsys.readouterr() assert captured.out == insert_1d_exp