Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def test_search_1D():
	tree = KDTree([1])
	right = KDTree([2])
	left = KDTree([0])
	tree.right = right
	tree.left = left
	assert tree.search([2]) == right
	assert tree.search([0]) == left
	assert tree.search([3]) is None
	assert tree.search([-1]) is None
Ejemplo n.º 3
0
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]
Ejemplo n.º 4
0
def test_init_with_params():
    tree = KDTree([0, 0], k=2, axis=1)
    assert tree.value == [0, 0]
    assert tree.k == 2
    assert tree.axis == 1
    assert tree.left == None
    assert tree.right == None
    assert tree.nodes == 1
Ejemplo n.º 5
0
def test_init():
    tree = KDTree(0)
    assert tree.value == 0
    assert tree.k == 1
    assert tree.axis == 0
    assert tree.left == None
    assert tree.right == None
    assert tree.nodes == 1
Ejemplo n.º 6
0
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]
Ejemplo n.º 7
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
Ejemplo n.º 8
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]]))
Ejemplo n.º 9
0
def test_visualize(points_vis, vis_exp, capsys):
    tree = KDTree.initialize(points_vis)
    tree.visualize()
    captured = capsys.readouterr()
    assert captured.out == vis_exp
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
def test_search_mismatch():
	tree = KDTree.initialize([[1],[2]])
	with pytest.raises(ValueError):
		tree.search([0,0])
Ejemplo n.º 12
0
def test_search_accept():
	tree = KDTree(KDSubType(1,1), accept=KDSubType)
	left = KDTree(KDSubType(1,0), accept=KDSubType)
	tree.left = left
	assert tree.search(KDSubType(1,0)) == left
	assert tree.search(KDSubType(1,3)) is None
Ejemplo n.º 13
0
def test_insert_mismatch():
    tree = KDTree.initialize([[1], [2]])
    with pytest.raises(ValueError):
        tree.insert([0, 0])
Ejemplo n.º 14
0
def test_dPN_mismatch():
    tree = KDTree.initialize([[1], [2]])
    with pytest.raises(ValueError):
        assert tree.proximal_neighbor([0, 0])
Ejemplo n.º 15
0
def test_d1PN():
    tree = KDTree.initialize([[1], [4]])
    assert np.all(tree.proximal_neighbor([3], d=1) == np.asarray([[[4], 1]]))
Ejemplo n.º 16
0
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]))
Ejemplo n.º 17
0
def test_initialize_3D(points_3d, init_3d_exp):
    tree = KDTree.initialize(points_3d)
    assert np.all(tree.value == init_3d_exp)
Ejemplo n.º 18
0
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
Ejemplo n.º 19
0
def test_initialize_1D(points_1d, init_1d_exp):
    tree = KDTree.initialize(points_1d)
    assert np.all(tree.value == init_1d_exp)
Ejemplo n.º 20
0
def test_KNN_mismatch():
    tree = KDTree.initialize([[1], [2]])
    with pytest.raises(ValueError):
        tree.nearest_neighbor([0, 0])
Ejemplo n.º 21
0
def test_delete_mismatch():
	tree = KDTree.initialize([[1],[2]])
	with pytest.raises(ValueError):
		tree.delete([0,0])
Ejemplo n.º 22
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]]))
Ejemplo n.º 23
0
def test_search_2D():
	tree = KDTree([1,1], k=2)
	right = KDTree([1,2], k=2, axis=1)
	tree.right = right
	assert tree.search([1,2]) == right
	assert tree.search([1,0]) is None
Ejemplo n.º 24
0
def test_1NN():
    tree = KDTree.initialize([[1], [4]])
    assert np.all(tree.nearest_neighbor([3]) == np.asarray([[[4], 1]]))
Ejemplo n.º 25
0
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