annoy

Форк
0
/
on_disk_build_test.py 
52 строки · 1.3 Кб
1
# Copyright (c) 2013 Spotify AB
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4
# use this file except in compliance with the License. You may obtain a copy of
5
# the License at
6
#
7
# http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations under
13
# the License.
14

15
import os
16

17
import pytest
18

19
from annoy import AnnoyIndex
20

21

22
@pytest.fixture(scope="module", autouse=True)
23
def setUp():
24
    if os.path.exists("on_disk.ann"):
25
        os.remove("on_disk.ann")
26

27

28
def add_items(i):
29
    i.add_item(0, [2, 2])
30
    i.add_item(1, [3, 2])
31
    i.add_item(2, [3, 3])
32

33

34
def check_nns(i):
35
    assert i.get_nns_by_vector([4, 4], 3) == [2, 1, 0]
36
    assert i.get_nns_by_vector([1, 1], 3) == [0, 1, 2]
37
    assert i.get_nns_by_vector([4, 2], 3) == [1, 2, 0]
38

39

40
def test_on_disk():
41
    f = 2
42
    i = AnnoyIndex(f, "euclidean")
43
    i.on_disk_build("on_disk.ann")
44
    add_items(i)
45
    i.build(10)
46
    check_nns(i)
47
    i.unload()
48
    i.load("on_disk.ann")
49
    check_nns(i)
50
    j = AnnoyIndex(f, "euclidean")
51
    j.load("on_disk.ann")
52
    check_nns(j)
53

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.